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: releng/8.3/sys/i386/acpica/acpi_machdep.c 225585 2011-09-15 12:27:26Z attilio $");
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 seldrain(&clone->sel_read);
317 knlist_destroy(&clone->sel_read.si_note);
318 ACPI_UNLOCK(acpi);
319 free(clone, M_APMDEV);
320 destroy_dev_sched(dev);
321 return (0);
322 }
323
324 static int
325 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
326 {
327 int error;
328 struct apm_clone_data *clone;
329 struct acpi_softc *acpi_sc;
330 struct apm_info info;
331 struct apm_event_info *ev_info;
332 apm_info_old_t aiop;
333
334 error = 0;
335 clone = dev->si_drv1;
336 acpi_sc = clone->acpi_sc;
337
338 switch (cmd) {
339 case APMIO_SUSPEND:
340 if ((flag & FWRITE) == 0)
341 return (EPERM);
342 if (acpi_sc->acpi_next_sstate == 0) {
343 if (acpi_sc->acpi_suspend_sx != ACPI_STATE_S5) {
344 error = acpi_ReqSleepState(acpi_sc,
345 acpi_sc->acpi_suspend_sx);
346 } else {
347 printf(
348 "power off via apm suspend not supported\n");
349 error = ENXIO;
350 }
351 } else
352 error = acpi_AckSleepState(clone, 0);
353 break;
354 case APMIO_STANDBY:
355 if ((flag & FWRITE) == 0)
356 return (EPERM);
357 if (acpi_sc->acpi_next_sstate == 0) {
358 if (acpi_sc->acpi_standby_sx != ACPI_STATE_S5) {
359 error = acpi_ReqSleepState(acpi_sc,
360 acpi_sc->acpi_standby_sx);
361 } else {
362 printf(
363 "power off via apm standby not supported\n");
364 error = ENXIO;
365 }
366 } else
367 error = acpi_AckSleepState(clone, 0);
368 break;
369 case APMIO_NEXTEVENT:
370 printf("apm nextevent start\n");
371 ACPI_LOCK(acpi);
372 if (acpi_sc->acpi_next_sstate != 0 && clone->notify_status ==
373 APM_EV_NONE) {
374 ev_info = (struct apm_event_info *)addr;
375 if (acpi_sc->acpi_next_sstate <= ACPI_STATE_S3)
376 ev_info->type = PMEV_STANDBYREQ;
377 else
378 ev_info->type = PMEV_SUSPENDREQ;
379 ev_info->index = 0;
380 clone->notify_status = APM_EV_NOTIFIED;
381 printf("apm event returning %d\n", ev_info->type);
382 } else
383 error = EAGAIN;
384 ACPI_UNLOCK(acpi);
385 break;
386 case APMIO_GETINFO_OLD:
387 if (acpi_capm_get_info(&info))
388 error = ENXIO;
389 aiop = (apm_info_old_t)addr;
390 aiop->ai_major = info.ai_major;
391 aiop->ai_minor = info.ai_minor;
392 aiop->ai_acline = info.ai_acline;
393 aiop->ai_batt_stat = info.ai_batt_stat;
394 aiop->ai_batt_life = info.ai_batt_life;
395 aiop->ai_status = info.ai_status;
396 break;
397 case APMIO_GETINFO:
398 if (acpi_capm_get_info((apm_info_t)addr))
399 error = ENXIO;
400 break;
401 case APMIO_GETPWSTATUS:
402 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
403 error = ENXIO;
404 break;
405 case APMIO_ENABLE:
406 if ((flag & FWRITE) == 0)
407 return (EPERM);
408 apm_active = 1;
409 break;
410 case APMIO_DISABLE:
411 if ((flag & FWRITE) == 0)
412 return (EPERM);
413 apm_active = 0;
414 break;
415 case APMIO_HALTCPU:
416 break;
417 case APMIO_NOTHALTCPU:
418 break;
419 case APMIO_DISPLAY:
420 if ((flag & FWRITE) == 0)
421 return (EPERM);
422 break;
423 case APMIO_BIOS:
424 if ((flag & FWRITE) == 0)
425 return (EPERM);
426 bzero(addr, sizeof(struct apm_bios_arg));
427 break;
428 default:
429 error = EINVAL;
430 break;
431 }
432
433 return (error);
434 }
435
436 static int
437 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
438 {
439 return (uio->uio_resid);
440 }
441
442 static int
443 apmpoll(struct cdev *dev, int events, struct thread *td)
444 {
445 struct apm_clone_data *clone;
446 int revents;
447
448 revents = 0;
449 ACPI_LOCK(acpi);
450 clone = dev->si_drv1;
451 if (clone->acpi_sc->acpi_next_sstate)
452 revents |= events & (POLLIN | POLLRDNORM);
453 else
454 selrecord(td, &clone->sel_read);
455 ACPI_UNLOCK(acpi);
456 return (revents);
457 }
458
459 static int
460 apmkqfilter(struct cdev *dev, struct knote *kn)
461 {
462 struct apm_clone_data *clone;
463
464 ACPI_LOCK(acpi);
465 clone = dev->si_drv1;
466 kn->kn_hook = clone;
467 kn->kn_fop = &apm_readfiltops;
468 knlist_add(&clone->sel_read.si_note, kn, 0);
469 ACPI_UNLOCK(acpi);
470 return (0);
471 }
472
473 static void
474 apmreadfiltdetach(struct knote *kn)
475 {
476 struct apm_clone_data *clone;
477
478 ACPI_LOCK(acpi);
479 clone = kn->kn_hook;
480 knlist_remove(&clone->sel_read.si_note, kn, 0);
481 ACPI_UNLOCK(acpi);
482 }
483
484 static int
485 apmreadfilt(struct knote *kn, long hint)
486 {
487 struct apm_clone_data *clone;
488 int sleeping;
489
490 ACPI_LOCK(acpi);
491 clone = kn->kn_hook;
492 sleeping = clone->acpi_sc->acpi_next_sstate ? 1 : 0;
493 ACPI_UNLOCK(acpi);
494 return (sleeping);
495 }
496
497 int
498 acpi_machdep_init(device_t dev)
499 {
500 struct acpi_softc *acpi_sc;
501
502 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
503
504 /* Create a clone for /dev/acpi also. */
505 STAILQ_INIT(&acpi_sc->apm_cdevs);
506 acpi_sc->acpi_clone = apm_create_clone(acpi_sc->acpi_dev_t, acpi_sc);
507 clone_setup(&apm_clones);
508 EVENTHANDLER_REGISTER(dev_clone, apm_clone, 0, 1000);
509 acpi_install_wakeup_handler(acpi_sc);
510
511 if (intr_model == ACPI_INTR_PIC)
512 BUS_CONFIG_INTR(dev, AcpiGbl_FADT.SciInterrupt,
513 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
514 else
515 acpi_SetIntrModel(intr_model);
516
517 SYSCTL_ADD_UINT(&acpi_sc->acpi_sysctl_ctx,
518 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
519 "reset_video", CTLFLAG_RW, &acpi_reset_video, 0,
520 "Call the VESA reset BIOS vector on the resume path");
521
522 return (0);
523 }
524
525 void
526 acpi_SetDefaultIntrModel(int model)
527 {
528
529 intr_model = model;
530 }
531
532 /* Check BIOS date. If 1998 or older, disable ACPI. */
533 int
534 acpi_machdep_quirks(int *quirks)
535 {
536 char *va;
537 int year;
538
539 /* BIOS address 0xffff5 contains the date in the format mm/dd/yy. */
540 va = pmap_mapbios(0xffff0, 16);
541 sscanf(va + 11, "%2d", &year);
542 pmap_unmapbios((vm_offset_t)va, 16);
543
544 /*
545 * Date must be >= 1/1/1999 or we don't trust ACPI. Note that this
546 * check must be changed by my 114th birthday.
547 */
548 if (year > 90 && year < 99)
549 *quirks = ACPI_Q_BROKEN;
550
551 return (0);
552 }
553
554 void
555 acpi_cpu_c1()
556 {
557 __asm __volatile("sti; hlt");
558 }
559
560 /*
561 * Support for mapping ACPI tables during early boot. This abuses the
562 * crashdump map because the kernel cannot allocate KVA in
563 * pmap_mapbios() when this is used. This makes the following
564 * assumptions about how we use this KVA: pages 0 and 1 are used to
565 * map in the header of each table found via the RSDT or XSDT and
566 * pages 2 to n are used to map in the RSDT or XSDT. This has to use
567 * 2 pages for the table headers in case a header spans a page
568 * boundary.
569 *
570 * XXX: We don't ensure the table fits in the available address space
571 * in the crashdump map.
572 */
573
574 /*
575 * Map some memory using the crashdump map. 'offset' is an offset in
576 * pages into the crashdump map to use for the start of the mapping.
577 */
578 static void *
579 table_map(vm_paddr_t pa, int offset, vm_offset_t length)
580 {
581 vm_offset_t va, off;
582 void *data;
583
584 off = pa & PAGE_MASK;
585 length = roundup(length + off, PAGE_SIZE);
586 pa = pa & PG_FRAME;
587 va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
588 (offset * PAGE_SIZE);
589 data = (void *)(va + off);
590 length -= PAGE_SIZE;
591 while (length > 0) {
592 va += PAGE_SIZE;
593 pa += PAGE_SIZE;
594 length -= PAGE_SIZE;
595 pmap_kenter(va, pa);
596 invlpg(va);
597 }
598 return (data);
599 }
600
601 /* Unmap memory previously mapped with table_map(). */
602 static void
603 table_unmap(void *data, vm_offset_t length)
604 {
605 vm_offset_t va, off;
606
607 va = (vm_offset_t)data;
608 off = va & PAGE_MASK;
609 length = roundup(length + off, PAGE_SIZE);
610 va &= ~PAGE_MASK;
611 while (length > 0) {
612 pmap_kremove(va);
613 invlpg(va);
614 va += PAGE_SIZE;
615 length -= PAGE_SIZE;
616 }
617 }
618
619 /*
620 * Map a table at a given offset into the crashdump map. It first
621 * maps the header to determine the table length and then maps the
622 * entire table.
623 */
624 static void *
625 map_table(vm_paddr_t pa, int offset, const char *sig)
626 {
627 ACPI_TABLE_HEADER *header;
628 vm_offset_t length;
629 void *table;
630
631 header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
632 if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
633 table_unmap(header, sizeof(ACPI_TABLE_HEADER));
634 return (NULL);
635 }
636 length = header->Length;
637 table_unmap(header, sizeof(ACPI_TABLE_HEADER));
638 table = table_map(pa, offset, length);
639 if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
640 if (bootverbose)
641 printf("ACPI: Failed checksum for table %s\n", sig);
642 #if (ACPI_CHECKSUM_ABORT)
643 table_unmap(table, length);
644 return (NULL);
645 #endif
646 }
647 return (table);
648 }
649
650 /*
651 * See if a given ACPI table is the requested table. Returns the
652 * length of the able if it matches or zero on failure.
653 */
654 static int
655 probe_table(vm_paddr_t address, const char *sig)
656 {
657 ACPI_TABLE_HEADER *table;
658
659 table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER));
660 if (table == NULL) {
661 if (bootverbose)
662 printf("ACPI: Failed to map table at 0x%jx\n",
663 (uintmax_t)address);
664 return (0);
665 }
666 if (bootverbose)
667 printf("Table '%.4s' at 0x%jx\n", table->Signature,
668 (uintmax_t)address);
669
670 if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
671 table_unmap(table, sizeof(ACPI_TABLE_HEADER));
672 return (0);
673 }
674 table_unmap(table, sizeof(ACPI_TABLE_HEADER));
675 return (1);
676 }
677
678 /*
679 * Try to map a table at a given physical address previously returned
680 * by acpi_find_table().
681 */
682 void *
683 acpi_map_table(vm_paddr_t pa, const char *sig)
684 {
685
686 return (map_table(pa, 0, sig));
687 }
688
689 /* Unmap a table previously mapped via acpi_map_table(). */
690 void
691 acpi_unmap_table(void *table)
692 {
693 ACPI_TABLE_HEADER *header;
694
695 header = (ACPI_TABLE_HEADER *)table;
696 table_unmap(table, header->Length);
697 }
698
699 /*
700 * Return the physical address of the requested table or zero if one
701 * is not found.
702 */
703 vm_paddr_t
704 acpi_find_table(const char *sig)
705 {
706 ACPI_PHYSICAL_ADDRESS rsdp_ptr;
707 ACPI_TABLE_RSDP *rsdp;
708 ACPI_TABLE_RSDT *rsdt;
709 ACPI_TABLE_XSDT *xsdt;
710 ACPI_TABLE_HEADER *table;
711 vm_paddr_t addr;
712 int i, count;
713
714 if (resource_disabled("acpi", 0))
715 return (0);
716
717 /*
718 * Map in the RSDP. Since ACPI uses AcpiOsMapMemory() which in turn
719 * calls pmap_mapbios() to find the RSDP, we assume that we can use
720 * pmap_mapbios() to map the RSDP.
721 */
722 if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
723 return (0);
724 rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
725 if (rsdp == NULL) {
726 if (bootverbose)
727 printf("ACPI: Failed to map RSDP\n");
728 return (0);
729 }
730
731 /*
732 * For ACPI >= 2.0, use the XSDT if it is available.
733 * Otherwise, use the RSDT. We map the XSDT or RSDT at page 2
734 * in the crashdump area. Pages 0 and 1 are used to map in the
735 * headers of candidate ACPI tables.
736 */
737 addr = 0;
738 if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
739 /*
740 * AcpiOsGetRootPointer only verifies the checksum for
741 * the version 1.0 portion of the RSDP. Version 2.0 has
742 * an additional checksum that we verify first.
743 */
744 if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
745 if (bootverbose)
746 printf("ACPI: RSDP failed extended checksum\n");
747 return (0);
748 }
749 xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
750 if (xsdt == NULL) {
751 if (bootverbose)
752 printf("ACPI: Failed to map XSDT\n");
753 return (0);
754 }
755 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
756 sizeof(UINT64);
757 for (i = 0; i < count; i++)
758 if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
759 addr = xsdt->TableOffsetEntry[i];
760 break;
761 }
762 acpi_unmap_table(xsdt);
763 } else {
764 rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT);
765 if (rsdt == NULL) {
766 if (bootverbose)
767 printf("ACPI: Failed to map RSDT\n");
768 return (0);
769 }
770 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
771 sizeof(UINT32);
772 for (i = 0; i < count; i++)
773 if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
774 addr = rsdt->TableOffsetEntry[i];
775 break;
776 }
777 acpi_unmap_table(rsdt);
778 }
779 pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
780 if (addr == 0) {
781 if (bootverbose)
782 printf("ACPI: No %s table found\n", sig);
783 return (0);
784 }
785 if (bootverbose)
786 printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
787
788 /*
789 * Verify that we can map the full table and that its checksum is
790 * correct, etc.
791 */
792 table = map_table(addr, 0, sig);
793 if (table == NULL)
794 return (0);
795 acpi_unmap_table(table);
796
797 return (addr);
798 }
799
800 /*
801 * ACPI nexus(4) driver.
802 */
803 static int
804 nexus_acpi_probe(device_t dev)
805 {
806 int error;
807
808 error = acpi_identify();
809 if (error)
810 return (error);
811
812 return (BUS_PROBE_DEFAULT);
813 }
814
815 static int
816 nexus_acpi_attach(device_t dev)
817 {
818
819 nexus_init_resources();
820 bus_generic_probe(dev);
821 if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL)
822 panic("failed to add acpi0 device");
823
824 return (bus_generic_attach(dev));
825 }
826
827 static device_method_t nexus_acpi_methods[] = {
828 /* Device interface */
829 DEVMETHOD(device_probe, nexus_acpi_probe),
830 DEVMETHOD(device_attach, nexus_acpi_attach),
831
832 { 0, 0 }
833 };
834
835 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
836 static devclass_t nexus_devclass;
837
838 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0);
Cache object: 3a476eba7f24deb9d57b671e3ad2d7e4
|