]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/i386/acpica/acpi_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / i386 / acpica / acpi_machdep.c
1 /*-
2  * Copyright (c) 2001 Mitsuru IWASAKI
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/condvar.h>
33 #include <sys/conf.h>
34 #include <sys/fcntl.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/poll.h>
39 #include <sys/sysctl.h>
40 #include <sys/uio.h>
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/actables.h>
47
48 #include <dev/acpica/acpivar.h>
49 #include <dev/acpica/acpiio.h>
50
51 #include <machine/nexusvar.h>
52
53 /*
54  * APM driver emulation 
55  */
56
57 #include <machine/apm_bios.h>
58 #include <machine/pc/bios.h>
59
60 #include <i386/bios/apm.h>
61
62 SYSCTL_DECL(_debug_acpi);
63
64 uint32_t acpi_resume_beep;
65 TUNABLE_INT("debug.acpi.resume_beep", &acpi_resume_beep);
66 SYSCTL_UINT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RW, &acpi_resume_beep,
67     0, "Beep the PC speaker when resuming");
68 uint32_t acpi_reset_video;
69 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
70
71 static int intr_model = ACPI_INTR_PIC;
72 static int apm_active;
73 static struct clonedevs *apm_clones;
74
75 MALLOC_DEFINE(M_APMDEV, "apmdev", "APM device emulation");
76
77 static d_open_t         apmopen;
78 static d_close_t        apmclose;
79 static d_write_t        apmwrite;
80 static d_ioctl_t        apmioctl;
81 static d_poll_t         apmpoll;
82 static d_kqfilter_t     apmkqfilter;
83 static void             apmreadfiltdetach(struct knote *kn);
84 static int              apmreadfilt(struct knote *kn, long hint);
85 static struct filterops apm_readfiltops =
86         { 1, NULL, apmreadfiltdetach, apmreadfilt };
87
88 static struct cdevsw apm_cdevsw = {
89         .d_version =    D_VERSION,
90         .d_flags =      D_TRACKCLOSE | D_NEEDMINOR,
91         .d_open =       apmopen,
92         .d_close =      apmclose,
93         .d_write =      apmwrite,
94         .d_ioctl =      apmioctl,
95         .d_poll =       apmpoll,
96         .d_name =       "apm",
97         .d_kqfilter =   apmkqfilter
98 };
99
100 static int
101 acpi_capm_convert_battstate(struct  acpi_battinfo *battp)
102 {
103         int     state;
104
105         state = APM_UNKNOWN;
106
107         if (battp->state & ACPI_BATT_STAT_DISCHARG) {
108                 if (battp->cap >= 50)
109                         state = 0;      /* high */
110                 else
111                         state = 1;      /* low */
112         }
113         if (battp->state & ACPI_BATT_STAT_CRITICAL)
114                 state = 2;              /* critical */
115         if (battp->state & ACPI_BATT_STAT_CHARGING)
116                 state = 3;              /* charging */
117
118         /* If still unknown, determine it based on the battery capacity. */
119         if (state == APM_UNKNOWN) {
120                 if (battp->cap >= 50)
121                         state = 0;      /* high */
122                 else
123                         state = 1;      /* low */
124         }
125
126         return (state);
127 }
128
129 static int
130 acpi_capm_convert_battflags(struct  acpi_battinfo *battp)
131 {
132         int     flags;
133
134         flags = 0;
135
136         if (battp->cap >= 50)
137                 flags |= APM_BATT_HIGH;
138         else {
139                 if (battp->state & ACPI_BATT_STAT_CRITICAL)
140                         flags |= APM_BATT_CRITICAL;
141                 else
142                         flags |= APM_BATT_LOW;
143         }
144         if (battp->state & ACPI_BATT_STAT_CHARGING)
145                 flags |= APM_BATT_CHARGING;
146         if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
147                 flags = APM_BATT_NOT_PRESENT;
148
149         return (flags);
150 }
151
152 static int
153 acpi_capm_get_info(apm_info_t aip)
154 {
155         int     acline;
156         struct  acpi_battinfo batt;
157
158         aip->ai_infoversion = 1;
159         aip->ai_major       = 1;
160         aip->ai_minor       = 2;
161         aip->ai_status      = apm_active;
162         aip->ai_capabilities= 0xff00;   /* unknown */
163
164         if (acpi_acad_get_acline(&acline))
165                 aip->ai_acline = APM_UNKNOWN;   /* unknown */
166         else
167                 aip->ai_acline = acline;        /* on/off */
168
169         if (acpi_battery_get_battinfo(NULL, &batt) != 0) {
170                 aip->ai_batt_stat = APM_UNKNOWN;
171                 aip->ai_batt_life = APM_UNKNOWN;
172                 aip->ai_batt_time = -1;          /* unknown */
173                 aip->ai_batteries = ~0U;         /* unknown */
174         } else {
175                 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
176                 aip->ai_batt_life = batt.cap;
177                 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
178                 aip->ai_batteries = acpi_battery_get_units();
179         }
180
181         return (0);
182 }
183
184 static int
185 acpi_capm_get_pwstatus(apm_pwstatus_t app)
186 {
187         device_t dev;
188         int     acline, unit, error;
189         struct  acpi_battinfo batt;
190
191         if (app->ap_device != PMDV_ALLDEV &&
192             (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
193                 return (1);
194
195         if (app->ap_device == PMDV_ALLDEV)
196                 error = acpi_battery_get_battinfo(NULL, &batt);
197         else {
198                 unit = app->ap_device - PMDV_BATT0;
199                 dev = devclass_get_device(devclass_find("battery"), unit);
200                 if (dev != NULL)
201                         error = acpi_battery_get_battinfo(dev, &batt);
202                 else
203                         error = ENXIO;
204         }
205         if (error)
206                 return (1);
207
208         app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
209         app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
210         app->ap_batt_life = batt.cap;
211         app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
212
213         if (acpi_acad_get_acline(&acline))
214                 app->ap_acline = APM_UNKNOWN;
215         else
216                 app->ap_acline = acline;        /* on/off */
217
218         return (0);
219 }
220
221 /* Create single-use devices for /dev/apm and /dev/apmctl. */
222 static void
223 apm_clone(void *arg, struct ucred *cred, char *name, int namelen,
224     struct cdev **dev)
225 {
226         int ctl_dev, unit;
227
228         if (*dev != NULL)
229                 return;
230         if (strcmp(name, "apmctl") == 0)
231                 ctl_dev = TRUE;
232         else if (strcmp(name, "apm") == 0)
233                 ctl_dev = FALSE;
234         else
235                 return;
236
237         /* Always create a new device and unit number. */
238         unit = -1;
239         if (clone_create(&apm_clones, &apm_cdevsw, &unit, dev, 0)) {
240                 if (ctl_dev) {
241                         *dev = make_dev(&apm_cdevsw, unit,
242                             UID_ROOT, GID_OPERATOR, 0660, "apmctl%d", unit);
243                 } else {
244                         *dev = make_dev(&apm_cdevsw, unit,
245                             UID_ROOT, GID_OPERATOR, 0664, "apm%d", unit);
246                 }
247                 if (*dev != NULL) {
248                         dev_ref(*dev);
249                         (*dev)->si_flags |= SI_CHEAPCLONE;
250                 }
251         }
252 }
253
254 /* Create a struct for tracking per-device suspend notification. */
255 static struct apm_clone_data *
256 apm_create_clone(struct cdev *dev, struct acpi_softc *acpi_sc)
257 {
258         struct apm_clone_data *clone;
259
260         clone = malloc(sizeof(*clone), M_APMDEV, M_WAITOK);
261         clone->cdev = dev;
262         clone->acpi_sc = acpi_sc;
263         clone->notify_status = APM_EV_NONE;
264         bzero(&clone->sel_read, sizeof(clone->sel_read));
265         knlist_init_mtx(&clone->sel_read.si_note, &acpi_mutex);
266
267         /*
268          * The acpi device is always managed by devd(8) and is considered
269          * writable (i.e., ack is required to allow suspend to proceed.)
270          */
271         if (strcmp("acpi", devtoname(dev)) == 0)
272                 clone->flags = ACPI_EVF_DEVD | ACPI_EVF_WRITE;
273         else
274                 clone->flags = ACPI_EVF_NONE;
275
276         ACPI_LOCK(acpi);
277         STAILQ_INSERT_TAIL(&acpi_sc->apm_cdevs, clone, entries);
278         ACPI_UNLOCK(acpi);
279         return (clone);
280 }
281
282 static int
283 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
284 {
285         struct  acpi_softc *acpi_sc;
286         struct  apm_clone_data *clone;
287
288         acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
289         clone = apm_create_clone(dev, acpi_sc);
290         dev->si_drv1 = clone;
291
292         /* If the device is opened for write, record that. */
293         if ((flag & FWRITE) != 0)
294                 clone->flags |= ACPI_EVF_WRITE;
295
296         return (0);
297 }
298
299 static int
300 apmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
301 {
302         struct  apm_clone_data *clone;
303         struct  acpi_softc *acpi_sc;
304
305         clone = dev->si_drv1;
306         acpi_sc = clone->acpi_sc;
307
308         /* We are about to lose a reference so check if suspend should occur */
309         if (acpi_sc->acpi_next_sstate != 0 &&
310             clone->notify_status != APM_EV_ACKED)
311                 acpi_AckSleepState(clone, 0);
312
313         /* Remove this clone's data from the list and free it. */
314         ACPI_LOCK(acpi);
315         STAILQ_REMOVE(&acpi_sc->apm_cdevs, clone, apm_clone_data, entries);
316         knlist_destroy(&clone->sel_read.si_note);
317         ACPI_UNLOCK(acpi);
318         free(clone, M_APMDEV);
319         destroy_dev_sched(dev);
320         return (0);
321 }
322
323 static int
324 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
325 {
326         int     error;
327         struct  apm_clone_data *clone;
328         struct  acpi_softc *acpi_sc;
329         struct  apm_info info;
330         struct  apm_event_info *ev_info;
331         apm_info_old_t aiop;
332
333         error = 0;
334         clone = dev->si_drv1;
335         acpi_sc = clone->acpi_sc;
336
337         switch (cmd) {
338         case APMIO_SUSPEND:
339                 if ((flag & FWRITE) == 0)
340                         return (EPERM);
341                 if (acpi_sc->acpi_next_sstate == 0) {
342                         if (acpi_sc->acpi_suspend_sx != ACPI_STATE_S5) {
343                                 error = acpi_ReqSleepState(acpi_sc,
344                                     acpi_sc->acpi_suspend_sx);
345                         } else {
346                                 printf(
347                         "power off via apm suspend not supported\n");
348                                 error = ENXIO;
349                         }
350                 } else
351                         error = acpi_AckSleepState(clone, 0);
352                 break;
353         case APMIO_STANDBY:
354                 if ((flag & FWRITE) == 0)
355                         return (EPERM);
356                 if (acpi_sc->acpi_next_sstate == 0) {
357                         if (acpi_sc->acpi_standby_sx != ACPI_STATE_S5) {
358                                 error = acpi_ReqSleepState(acpi_sc,
359                                     acpi_sc->acpi_standby_sx);
360                         } else {
361                                 printf(
362                         "power off via apm standby not supported\n");
363                                 error = ENXIO;
364                         }
365                 } else
366                         error = acpi_AckSleepState(clone, 0);
367                 break;
368         case APMIO_NEXTEVENT:
369                 printf("apm nextevent start\n");
370                 ACPI_LOCK(acpi);
371                 if (acpi_sc->acpi_next_sstate != 0 && clone->notify_status ==
372                     APM_EV_NONE) {
373                         ev_info = (struct apm_event_info *)addr;
374                         if (acpi_sc->acpi_next_sstate <= ACPI_STATE_S3)
375                                 ev_info->type = PMEV_STANDBYREQ;
376                         else
377                                 ev_info->type = PMEV_SUSPENDREQ;
378                         ev_info->index = 0;
379                         clone->notify_status = APM_EV_NOTIFIED;
380                         printf("apm event returning %d\n", ev_info->type);
381                 } else
382                         error = EAGAIN;
383                 ACPI_UNLOCK(acpi);
384                 break;
385         case APMIO_GETINFO_OLD:
386                 if (acpi_capm_get_info(&info))
387                         error = ENXIO;
388                 aiop = (apm_info_old_t)addr;
389                 aiop->ai_major = info.ai_major;
390                 aiop->ai_minor = info.ai_minor;
391                 aiop->ai_acline = info.ai_acline;
392                 aiop->ai_batt_stat = info.ai_batt_stat;
393                 aiop->ai_batt_life = info.ai_batt_life;
394                 aiop->ai_status = info.ai_status;
395                 break;
396         case APMIO_GETINFO:
397                 if (acpi_capm_get_info((apm_info_t)addr))
398                         error = ENXIO;
399                 break;
400         case APMIO_GETPWSTATUS:
401                 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
402                         error = ENXIO;
403                 break;
404         case APMIO_ENABLE:
405                 if ((flag & FWRITE) == 0)
406                         return (EPERM);
407                 apm_active = 1;
408                 break;
409         case APMIO_DISABLE:
410                 if ((flag & FWRITE) == 0)
411                         return (EPERM);
412                 apm_active = 0;
413                 break;
414         case APMIO_HALTCPU:
415                 break;
416         case APMIO_NOTHALTCPU:
417                 break;
418         case APMIO_DISPLAY:
419                 if ((flag & FWRITE) == 0)
420                         return (EPERM);
421                 break;
422         case APMIO_BIOS:
423                 if ((flag & FWRITE) == 0)
424                         return (EPERM);
425                 bzero(addr, sizeof(struct apm_bios_arg));
426                 break;
427         default:
428                 error = EINVAL;
429                 break;
430         }
431
432         return (error);
433 }
434
435 static int
436 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
437 {
438         return (uio->uio_resid);
439 }
440
441 static int
442 apmpoll(struct cdev *dev, int events, struct thread *td)
443 {
444         struct  apm_clone_data *clone;
445         int revents;
446
447         revents = 0;
448         ACPI_LOCK(acpi);
449         clone = dev->si_drv1;
450         if (clone->acpi_sc->acpi_next_sstate)
451                 revents |= events & (POLLIN | POLLRDNORM);
452         else
453                 selrecord(td, &clone->sel_read);
454         ACPI_UNLOCK(acpi);
455         return (revents);
456 }
457
458 static int
459 apmkqfilter(struct cdev *dev, struct knote *kn)
460 {
461         struct  apm_clone_data *clone;
462
463         ACPI_LOCK(acpi);
464         clone = dev->si_drv1;
465         kn->kn_hook = clone;
466         kn->kn_fop = &apm_readfiltops;
467         knlist_add(&clone->sel_read.si_note, kn, 0);
468         ACPI_UNLOCK(acpi);
469         return (0);
470 }
471
472 static void
473 apmreadfiltdetach(struct knote *kn)
474 {
475         struct  apm_clone_data *clone;
476
477         ACPI_LOCK(acpi);
478         clone = kn->kn_hook;
479         knlist_remove(&clone->sel_read.si_note, kn, 0);
480         ACPI_UNLOCK(acpi);
481 }
482
483 static int
484 apmreadfilt(struct knote *kn, long hint)
485 {
486         struct  apm_clone_data *clone;
487         int     sleeping;
488
489         ACPI_LOCK(acpi);
490         clone = kn->kn_hook;
491         sleeping = clone->acpi_sc->acpi_next_sstate ? 1 : 0;
492         ACPI_UNLOCK(acpi);
493         return (sleeping);
494 }
495
496 int
497 acpi_machdep_init(device_t dev)
498 {
499         struct  acpi_softc *acpi_sc;
500
501         acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
502
503         /* Create a clone for /dev/acpi also. */
504         STAILQ_INIT(&acpi_sc->apm_cdevs);
505         acpi_sc->acpi_clone = apm_create_clone(acpi_sc->acpi_dev_t, acpi_sc);
506         clone_setup(&apm_clones);
507         EVENTHANDLER_REGISTER(dev_clone, apm_clone, 0, 1000);
508         acpi_install_wakeup_handler(acpi_sc);
509
510         if (intr_model == ACPI_INTR_PIC)
511                 BUS_CONFIG_INTR(dev, AcpiGbl_FADT.SciInterrupt,
512                     INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
513         else
514                 acpi_SetIntrModel(intr_model);
515
516         SYSCTL_ADD_UINT(&acpi_sc->acpi_sysctl_ctx,
517             SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
518             "reset_video", CTLFLAG_RW, &acpi_reset_video, 0,
519             "Call the VESA reset BIOS vector on the resume path");
520
521         return (0);
522 }
523
524 void
525 acpi_SetDefaultIntrModel(int model)
526 {
527
528         intr_model = model;
529 }
530
531 /* Check BIOS date.  If 1998 or older, disable ACPI. */
532 int
533 acpi_machdep_quirks(int *quirks)
534 {
535         char *va;
536         int year;
537
538         /* BIOS address 0xffff5 contains the date in the format mm/dd/yy. */
539         va = pmap_mapbios(0xffff0, 16);
540         sscanf(va + 11, "%2d", &year);
541         pmap_unmapbios((vm_offset_t)va, 16);
542
543         /* 
544          * Date must be >= 1/1/1999 or we don't trust ACPI.  Note that this
545          * check must be changed by my 114th birthday.
546          */
547         if (year > 90 && year < 99)
548                 *quirks = ACPI_Q_BROKEN;
549
550         return (0);
551 }
552
553 void
554 acpi_cpu_c1()
555 {
556         __asm __volatile("sti; hlt");
557 }
558
559 /*
560  * Support for mapping ACPI tables during early boot.  This abuses the
561  * crashdump map because the kernel cannot allocate KVA in
562  * pmap_mapbios() when this is used.  This makes the following
563  * assumptions about how we use this KVA: pages 0 and 1 are used to
564  * map in the header of each table found via the RSDT or XSDT and
565  * pages 2 to n are used to map in the RSDT or XSDT.  This has to use
566  * 2 pages for the table headers in case a header spans a page
567  * boundary.
568  *
569  * XXX: We don't ensure the table fits in the available address space
570  * in the crashdump map.
571  */
572
573 /*
574  * Map some memory using the crashdump map.  'offset' is an offset in
575  * pages into the crashdump map to use for the start of the mapping.
576  */
577 static void *
578 table_map(vm_paddr_t pa, int offset, vm_offset_t length)
579 {
580         vm_offset_t va, off;
581         void *data;
582
583         off = pa & PAGE_MASK;
584         length = roundup(length + off, PAGE_SIZE);
585         pa = pa & PG_FRAME;
586         va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
587             (offset * PAGE_SIZE);
588         data = (void *)(va + off);
589         length -= PAGE_SIZE;
590         while (length > 0) {
591                 va += PAGE_SIZE;
592                 pa += PAGE_SIZE;
593                 length -= PAGE_SIZE;
594                 pmap_kenter(va, pa);
595                 invlpg(va);
596         }
597         return (data);
598 }
599
600 /* Unmap memory previously mapped with table_map(). */
601 static void
602 table_unmap(void *data, vm_offset_t length)
603 {
604         vm_offset_t va, off;
605
606         va = (vm_offset_t)data;
607         off = va & PAGE_MASK;
608         length = roundup(length + off, PAGE_SIZE);
609         va &= ~PAGE_MASK;
610         while (length > 0) {
611                 pmap_kremove(va);
612                 invlpg(va);
613                 va += PAGE_SIZE;
614                 length -= PAGE_SIZE;
615         }
616 }
617
618 /*
619  * Map a table at a given offset into the crashdump map.  It first
620  * maps the header to determine the table length and then maps the
621  * entire table.
622  */
623 static void *
624 map_table(vm_paddr_t pa, int offset, const char *sig)
625 {
626         ACPI_TABLE_HEADER *header;
627         vm_offset_t length;
628         void *table;
629
630         header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
631         if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
632                 table_unmap(header, sizeof(ACPI_TABLE_HEADER));
633                 return (NULL);
634         }
635         length = header->Length;
636         table_unmap(header, sizeof(ACPI_TABLE_HEADER));
637         table = table_map(pa, offset, length);
638         if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
639                 if (bootverbose)
640                         printf("ACPI: Failed checksum for table %s\n", sig);
641 #if (ACPI_CHECKSUM_ABORT)
642                 table_unmap(table, length);
643                 return (NULL);
644 #endif
645         }
646         return (table);
647 }
648
649 /*
650  * See if a given ACPI table is the requested table.  Returns the
651  * length of the able if it matches or zero on failure.
652  */
653 static int
654 probe_table(vm_paddr_t address, const char *sig)
655 {
656         ACPI_TABLE_HEADER *table;
657
658         table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER));
659         if (table == NULL) {
660                 if (bootverbose)
661                         printf("ACPI: Failed to map table at 0x%jx\n",
662                             (uintmax_t)address);
663                 return (0);
664         }
665         if (bootverbose)
666                 printf("Table '%.4s' at 0x%jx\n", table->Signature,
667                     (uintmax_t)address);
668
669         if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
670                 table_unmap(table, sizeof(ACPI_TABLE_HEADER));
671                 return (0);
672         }
673         table_unmap(table, sizeof(ACPI_TABLE_HEADER));
674         return (1);
675 }
676
677 /*
678  * Try to map a table at a given physical address previously returned
679  * by acpi_find_table().
680  */
681 void *
682 acpi_map_table(vm_paddr_t pa, const char *sig)
683 {
684
685         return (map_table(pa, 0, sig));
686 }
687
688 /* Unmap a table previously mapped via acpi_map_table(). */
689 void
690 acpi_unmap_table(void *table)
691 {
692         ACPI_TABLE_HEADER *header;
693
694         header = (ACPI_TABLE_HEADER *)table;
695         table_unmap(table, header->Length);
696 }
697
698 /*
699  * Return the physical address of the requested table or zero if one
700  * is not found.
701  */
702 vm_paddr_t
703 acpi_find_table(const char *sig)
704 {
705         ACPI_PHYSICAL_ADDRESS rsdp_ptr;
706         ACPI_TABLE_RSDP *rsdp;
707         ACPI_TABLE_RSDT *rsdt;
708         ACPI_TABLE_XSDT *xsdt;
709         ACPI_TABLE_HEADER *table;
710         vm_paddr_t addr;
711         int i, count;
712
713         if (resource_disabled("acpi", 0))
714                 return (0);
715
716         /*
717          * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
718          * calls pmap_mapbios() to find the RSDP, we assume that we can use
719          * pmap_mapbios() to map the RSDP.
720          */
721         if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
722                 return (0);
723         rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
724         if (rsdp == NULL) {
725                 if (bootverbose)
726                         printf("ACPI: Failed to map RSDP\n");
727                 return (0);
728         }
729
730         /*
731          * For ACPI >= 2.0, use the XSDT if it is available.
732          * Otherwise, use the RSDT.  We map the XSDT or RSDT at page 2
733          * in the crashdump area.  Pages 0 and 1 are used to map in the
734          * headers of candidate ACPI tables.
735          */
736         addr = 0;
737         if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
738                 /*
739                  * AcpiOsGetRootPointer only verifies the checksum for
740                  * the version 1.0 portion of the RSDP.  Version 2.0 has
741                  * an additional checksum that we verify first.
742                  */
743                 if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
744                         if (bootverbose)
745                                 printf("ACPI: RSDP failed extended checksum\n");
746                         return (0);
747                 }
748                 xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
749                 if (xsdt == NULL) {
750                         if (bootverbose)
751                                 printf("ACPI: Failed to map XSDT\n");
752                         return (0);
753                 }
754                 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
755                     sizeof(UINT64);
756                 for (i = 0; i < count; i++)
757                         if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
758                                 addr = xsdt->TableOffsetEntry[i];
759                                 break;
760                         }
761                 acpi_unmap_table(xsdt);
762         } else {
763                 rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT);
764                 if (rsdt == NULL) {
765                         if (bootverbose)
766                                 printf("ACPI: Failed to map RSDT\n");
767                         return (0);
768                 }
769                 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
770                     sizeof(UINT32);
771                 for (i = 0; i < count; i++)
772                         if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
773                                 addr = rsdt->TableOffsetEntry[i];
774                                 break;
775                         }
776                 acpi_unmap_table(rsdt);
777         }
778         pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
779         if (addr == 0) {
780                 if (bootverbose)
781                         printf("ACPI: No %s table found\n", sig);
782                 return (0);
783         }
784         if (bootverbose)
785                 printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
786
787         /*
788          * Verify that we can map the full table and that its checksum is
789          * correct, etc.
790          */
791         table = map_table(addr, 0, sig);
792         if (table == NULL)
793                 return (0);
794         acpi_unmap_table(table);
795
796         return (addr);
797 }
798
799 /*
800  * ACPI nexus(4) driver.
801  */
802 static int
803 nexus_acpi_probe(device_t dev)
804 {
805         int error;
806
807         error = acpi_identify();
808         if (error)
809                 return (error);
810
811         return (BUS_PROBE_DEFAULT);
812 }
813
814 static int
815 nexus_acpi_attach(device_t dev)
816 {
817
818         nexus_init_resources();
819         bus_generic_probe(dev);
820         if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL)
821                 panic("failed to add acpi0 device");
822
823         return (bus_generic_attach(dev));
824 }
825
826 static device_method_t nexus_acpi_methods[] = {
827         /* Device interface */
828         DEVMETHOD(device_probe,         nexus_acpi_probe),
829         DEVMETHOD(device_attach,        nexus_acpi_attach),
830
831         { 0, 0 }
832 };
833
834 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
835 static devclass_t nexus_devclass;
836
837 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0);