acpi.c revision 1.287 1 /* $NetBSD: acpi.c,v 1.287 2020/12/07 10:57:41 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum of By Noon Software, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2003 Wasabi Systems, Inc.
34 * All rights reserved.
35 *
36 * Written by Frank van der Linden for Wasabi Systems, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed for the NetBSD Project by
49 * Wasabi Systems, Inc.
50 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51 * or promote products derived from this software without specific prior
52 * written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67 /*
68 * Copyright 2001, 2003 Wasabi Systems, Inc.
69 * All rights reserved.
70 *
71 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed for the NetBSD Project by
84 * Wasabi Systems, Inc.
85 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
86 * or promote products derived from this software without specific prior
87 * written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
91 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
92 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
93 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
94 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
95 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
96 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
97 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
98 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
99 * POSSIBILITY OF SUCH DAMAGE.
100 */
101
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.287 2020/12/07 10:57:41 jmcneill Exp $");
104
105 #include "pci.h"
106 #include "opt_acpi.h"
107 #include "opt_pcifixup.h"
108
109 #include <sys/param.h>
110 #include <sys/device.h>
111 #include <sys/kernel.h>
112 #include <sys/kmem.h>
113 #include <sys/malloc.h>
114 #include <sys/module.h>
115 #include <sys/mutex.h>
116 #include <sys/sysctl.h>
117 #include <sys/systm.h>
118 #include <sys/timetc.h>
119
120 #include <dev/acpi/acpireg.h>
121 #include <dev/acpi/acpivar.h>
122 #include <dev/acpi/acpi_mcfg.h>
123 #include <dev/acpi/acpi_osd.h>
124 #include <dev/acpi/acpi_pci.h>
125 #include <dev/acpi/acpi_power.h>
126 #include <dev/acpi/acpi_timer.h>
127 #include <dev/acpi/acpi_wakedev.h>
128
129 #include <machine/acpi_machdep.h>
130
131 #include "ioconf.h"
132
133 #define _COMPONENT ACPI_BUS_COMPONENT
134 ACPI_MODULE_NAME ("acpi")
135
136 /*
137 * The acpi_active variable is set when the ACPI subsystem is active.
138 * Machine-dependent code may wish to skip other steps (such as attaching
139 * subsystems that ACPI supercedes) when ACPI is active.
140 */
141 int acpi_active = 0;
142 int acpi_suspended = 0;
143 int acpi_force_load = 0;
144 int acpi_verbose_loaded = 0;
145
146 struct acpi_softc *acpi_softc = NULL;
147 static uint64_t acpi_root_pointer;
148 extern kmutex_t acpi_interrupt_list_mtx;
149 static ACPI_HANDLE acpi_scopes[4];
150 ACPI_TABLE_HEADER *madt_header;
151 ACPI_TABLE_HEADER *gtdt_header;
152
153 /*
154 * This structure provides a context for the ACPI
155 * namespace walk performed in acpi_build_tree().
156 */
157 struct acpi_walkcontext {
158 struct acpi_softc *aw_sc;
159 struct acpi_devnode *aw_parent;
160 };
161
162 /*
163 * Ignored HIDs.
164 */
165 static const char * const acpi_ignored_ids[] = {
166 #if defined(i386) || defined(x86_64)
167 "ACPI0007", /* ACPI CPUs do not attach to acpi(4) */
168 "PNP0000", /* AT interrupt controller is handled internally */
169 "PNP0001", /* EISA interrupt controller is handled internally */
170 "PNP0200", /* AT DMA controller is handled internally */
171 "PNP0A??", /* PCI Busses are handled internally */
172 "PNP0B00", /* AT RTC is handled internally */
173 "PNP0C02", /* PnP motherboard resources */
174 "PNP0C0F", /* ACPI PCI link devices are handled internally */
175 #endif
176 #if defined(x86_64)
177 "PNP0C04", /* FPU is handled internally */
178 #endif
179 #if defined(__aarch64__)
180 "ACPI0004", /* ACPI module devices are handled internally */
181 "PNP0C0F", /* ACPI PCI link devices are handled internally */
182 #endif
183 NULL
184 };
185
186 /*
187 * Devices that should be attached early.
188 */
189 static const char * const acpi_early_ids[] = {
190 "PNP0C09", /* acpiec(4) */
191 NULL
192 };
193
194 static int acpi_match(device_t, cfdata_t, void *);
195 static int acpi_submatch(device_t, cfdata_t, const int *, void *);
196 static void acpi_attach(device_t, device_t, void *);
197 static int acpi_detach(device_t, int);
198 static void acpi_childdet(device_t, device_t);
199 static bool acpi_suspend(device_t, const pmf_qual_t *);
200 static bool acpi_resume(device_t, const pmf_qual_t *);
201
202 static void acpi_build_tree(struct acpi_softc *);
203 static void acpi_config_tree(struct acpi_softc *);
204 static void acpi_config_dma(struct acpi_softc *);
205 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, uint32_t,
206 void *, void **);
207 static ACPI_STATUS acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
208 void *, void **);
209 static void acpi_make_name(struct acpi_devnode *, uint32_t);
210
211 static int acpi_rescan(device_t, const char *, const int *);
212 static void acpi_rescan_early(struct acpi_softc *);
213 static void acpi_rescan_nodes(struct acpi_softc *);
214 static void acpi_rescan_capabilities(device_t);
215 static int acpi_print(void *aux, const char *);
216
217 static void acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
218
219 static void acpi_register_fixed_button(struct acpi_softc *, int);
220 static void acpi_deregister_fixed_button(struct acpi_softc *, int);
221 static uint32_t acpi_fixed_button_handler(void *);
222 static void acpi_fixed_button_pressed(void *);
223
224 static void acpi_sleep_init(struct acpi_softc *);
225
226 static int sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
227 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
228 static int sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
229
230 static bool acpi_is_scope(struct acpi_devnode *);
231 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
232 static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
233
234 void acpi_print_verbose_stub(struct acpi_softc *);
235 void acpi_print_dev_stub(const char *);
236
237 static void acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
238 ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE);
239
240 void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
241 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
242
243 bus_dma_tag_t acpi_default_dma_tag(struct acpi_softc *, struct acpi_devnode *);
244 bus_dma_tag_t acpi_default_dma64_tag(struct acpi_softc *, struct acpi_devnode *);
245 pci_chipset_tag_t acpi_default_pci_chipset_tag(struct acpi_softc *, int, int);
246
247 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
248 acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
249
250 /*
251 * Probe for ACPI support.
252 *
253 * This is called by the machine-dependent ACPI front-end.
254 * Note: this is not an autoconfiguration interface function.
255 */
256 int
257 acpi_probe(void)
258 {
259 ACPI_TABLE_HEADER *rsdt;
260 ACPI_STATUS rv;
261 int quirks;
262
263 if (acpi_softc != NULL)
264 panic("%s: already probed", __func__);
265
266 mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
267
268 /*
269 * Start up ACPICA.
270 */
271 AcpiGbl_EnableInterpreterSlack = true;
272
273 rv = AcpiInitializeSubsystem();
274
275 if (ACPI_FAILURE(rv)) {
276 aprint_error("%s: failed to initialize subsystem\n", __func__);
277 return 0;
278 }
279
280 /*
281 * Allocate space for RSDT/XSDT and DSDT,
282 * but allow resizing if more tables exist.
283 */
284 rv = AcpiInitializeTables(NULL, 2, true);
285
286 if (ACPI_FAILURE(rv)) {
287 aprint_error("%s: failed to initialize tables\n", __func__);
288 goto fail;
289 }
290
291 rv = AcpiLoadTables();
292
293 if (ACPI_FAILURE(rv)) {
294 aprint_error("%s: failed to load tables\n", __func__);
295 goto fail;
296 }
297
298 rsdt = acpi_map_rsdt();
299
300 if (rsdt == NULL) {
301 aprint_error("%s: failed to map RSDT\n", __func__);
302 goto fail;
303 }
304
305 quirks = acpi_find_quirks();
306
307 if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_BROKEN) != 0) {
308
309 aprint_normal("ACPI: BIOS is listed as broken:\n");
310 aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
311 "AslId <%4.4s,%08x>\n", rsdt->OemId, rsdt->OemTableId,
312 rsdt->OemRevision, rsdt->AslCompilerId,
313 rsdt->AslCompilerRevision);
314 aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
315
316 acpi_unmap_rsdt(rsdt);
317 goto fail;
318 }
319
320 if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_OLDBIOS) != 0) {
321
322 aprint_normal("ACPI: BIOS is too old (%s). "
323 "Set acpi_force_load to use.\n",
324 pmf_get_platform("bios-date"));
325
326 acpi_unmap_rsdt(rsdt);
327 goto fail;
328 }
329
330 acpi_unmap_rsdt(rsdt);
331
332 rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
333
334 if (ACPI_FAILURE(rv)) {
335 aprint_error("%s: failed to enable subsystem\n", __func__);
336 goto fail;
337 }
338
339 return 1;
340
341 fail:
342 (void)AcpiTerminate();
343
344 return 0;
345 }
346
347 void
348 acpi_disable(void)
349 {
350
351 if (acpi_softc == NULL)
352 return;
353
354 KASSERT(acpi_active != 0);
355
356 if (AcpiGbl_FADT.SmiCommand != 0)
357 AcpiDisable();
358 }
359
360 int
361 acpi_check(device_t parent, const char *ifattr)
362 {
363 return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
364 }
365
366 int
367 acpi_reset(void)
368 {
369 struct acpi_softc *sc = acpi_softc;
370 ACPI_GENERIC_ADDRESS *ResetReg;
371 ACPI_PCI_ID PciId;
372 ACPI_STATUS status;
373
374 if (sc == NULL)
375 return ENXIO;
376
377 ResetReg = &AcpiGbl_FADT.ResetRegister;
378
379 /* Check if the reset register is supported */
380 if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
381 !ResetReg->Address) {
382 return ENOENT;
383 }
384
385 switch (ResetReg->SpaceId) {
386 case ACPI_ADR_SPACE_PCI_CONFIG:
387 PciId.Segment = PciId.Bus = 0;
388 PciId.Device = ACPI_GAS_PCI_DEV(ResetReg->Address);
389 PciId.Function = ACPI_GAS_PCI_FUNC(ResetReg->Address);
390 status = AcpiOsWritePciConfiguration(&PciId,
391 ACPI_GAS_PCI_REGOFF(ResetReg->Address),
392 AcpiGbl_FADT.ResetValue, ResetReg->BitWidth);
393 break;
394 case ACPI_ADR_SPACE_SYSTEM_IO:
395 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
396 status = AcpiReset();
397 break;
398 default:
399 status = AE_TYPE;
400 break;
401 }
402
403 return ACPI_FAILURE(status) ? EIO : 0;
404 }
405
406 /*
407 * Autoconfiguration.
408 */
409 static int
410 acpi_match(device_t parent, cfdata_t match, void *aux)
411 {
412 /*
413 * XXX: Nada; MD code has called acpi_probe().
414 */
415 return 1;
416 }
417
418 static int
419 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
420 {
421 struct cfattach *ca;
422
423 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
424
425 return (ca == &acpi_ca);
426 }
427
428 static void
429 acpi_attach(device_t parent, device_t self, void *aux)
430 {
431 struct acpi_softc *sc = device_private(self);
432 struct acpibus_attach_args *aa = aux;
433 ACPI_TABLE_HEADER *rsdt;
434 ACPI_STATUS rv;
435
436 aprint_naive("\n");
437 aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
438
439 if (acpi_softc != NULL)
440 panic("%s: already attached", __func__);
441
442 rsdt = acpi_map_rsdt();
443
444 if (rsdt == NULL)
445 aprint_error_dev(self, "X/RSDT: Not found\n");
446 else {
447 aprint_verbose_dev(self,
448 "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
449 rsdt->OemId, rsdt->OemTableId,
450 rsdt->OemRevision,
451 rsdt->AslCompilerId, rsdt->AslCompilerRevision);
452 }
453
454 acpi_unmap_rsdt(rsdt);
455
456 sc->sc_dev = self;
457 sc->sc_root = NULL;
458
459 sc->sc_sleepstate = ACPI_STATE_S0;
460 sc->sc_quirks = acpi_find_quirks();
461
462 sysmon_power_settype("acpi");
463
464 sc->sc_iot = aa->aa_iot;
465 sc->sc_memt = aa->aa_memt;
466 sc->sc_pciflags = aa->aa_pciflags;
467 sc->sc_ic = aa->aa_ic;
468 sc->sc_dmat = aa->aa_dmat;
469 sc->sc_dmat64 = aa->aa_dmat64;
470
471 SIMPLEQ_INIT(&sc->ad_head);
472
473 acpi_softc = sc;
474
475 if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
476 aprint_error_dev(self, "couldn't establish power handler\n");
477
478 /*
479 * Bring ACPICA on-line.
480 */
481
482 rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
483
484 if (ACPI_FAILURE(rv))
485 goto fail;
486
487 /*
488 * Early initialization of acpiec(4) via ECDT.
489 */
490 (void)config_found_ia(self, "acpiecdtbus", aa, NULL);
491
492 rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
493
494 if (ACPI_FAILURE(rv))
495 goto fail;
496
497 /*
498 * Scan the namespace and build our device tree.
499 */
500 acpi_build_tree(sc);
501
502 #if NPCI > 0
503 /*
504 * Probe MCFG table
505 */
506 acpimcfg_probe(sc);
507 #endif
508
509 acpi_md_callback(sc);
510
511 /*
512 * Early initialization of the _PDC control method
513 * that may load additional SSDT tables dynamically.
514 */
515 (void)acpi_md_pdc();
516
517 /*
518 * Install global notify handlers.
519 */
520 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
521 ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
522
523 if (ACPI_FAILURE(rv))
524 goto fail;
525
526 rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
527 ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
528
529 if (ACPI_FAILURE(rv))
530 goto fail;
531
532 acpi_active = 1;
533
534 if (!AcpiGbl_ReducedHardware) {
535 /* Show SCI interrupt. */
536 aprint_verbose_dev(self, "SCI interrupting at int %u\n",
537 AcpiGbl_FADT.SciInterrupt);
538
539 /*
540 * Install fixed-event handlers.
541 */
542 acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
543 acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
544 }
545
546 acpitimer_init(sc);
547 acpi_config_tree(sc);
548 acpi_sleep_init(sc);
549
550 #ifdef ACPI_DEBUG
551 acpi_debug_init();
552 #endif
553
554 /*
555 * Print debug information.
556 */
557 acpi_print_verbose(sc);
558
559 return;
560
561 fail:
562 aprint_error("%s: failed to initialize ACPI: %s\n",
563 __func__, AcpiFormatException(rv));
564 }
565
566 /*
567 * XXX: This is incomplete.
568 */
569 static int
570 acpi_detach(device_t self, int flags)
571 {
572 struct acpi_softc *sc = device_private(self);
573 ACPI_STATUS rv;
574 int rc;
575
576 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
577 ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
578
579 if (ACPI_FAILURE(rv))
580 return EBUSY;
581
582 rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
583 ACPI_DEVICE_NOTIFY, acpi_notify_handler);
584
585 if (ACPI_FAILURE(rv))
586 return EBUSY;
587
588 if ((rc = config_detach_children(self, flags)) != 0)
589 return rc;
590
591 if ((rc = acpitimer_detach()) != 0)
592 return rc;
593
594 if (!AcpiGbl_ReducedHardware) {
595 acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
596 acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
597 }
598
599 pmf_device_deregister(self);
600
601 acpi_softc = NULL;
602
603 return 0;
604 }
605
606 static void
607 acpi_childdet(device_t self, device_t child)
608 {
609 struct acpi_softc *sc = device_private(self);
610 struct acpi_devnode *ad;
611
612 if (sc->sc_apmbus == child)
613 sc->sc_apmbus = NULL;
614
615 if (sc->sc_hpet == child)
616 sc->sc_hpet = NULL;
617
618 if (sc->sc_wdrt == child)
619 sc->sc_wdrt = NULL;
620
621 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
622
623 if (ad->ad_device == child)
624 ad->ad_device = NULL;
625 }
626 }
627
628 static bool
629 acpi_suspend(device_t dv, const pmf_qual_t *qual)
630 {
631
632 acpi_suspended = 1;
633
634 return true;
635 }
636
637 static bool
638 acpi_resume(device_t dv, const pmf_qual_t *qual)
639 {
640
641 acpi_suspended = 0;
642
643 return true;
644 }
645
646 /*
647 * Namespace scan.
648 */
649 static void
650 acpi_build_tree(struct acpi_softc *sc)
651 {
652 struct acpi_walkcontext awc;
653
654 /*
655 * Get the root scope handles.
656 */
657 KASSERT(__arraycount(acpi_scopes) == 4);
658
659 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
660 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
661 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
662 (void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
663
664 /*
665 * Make the root node.
666 */
667 awc.aw_sc = sc;
668 awc.aw_parent = NULL;
669
670 (void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
671
672 KASSERT(sc->sc_root == NULL);
673 KASSERT(awc.aw_parent != NULL);
674
675 sc->sc_root = awc.aw_parent;
676
677 /*
678 * Build the internal namespace.
679 */
680 (void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
681 acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
682
683 /*
684 * Scan the internal namespace.
685 */
686 (void)acpi_pcidev_scan(sc->sc_root);
687 }
688
689 static void
690 acpi_config_tree(struct acpi_softc *sc)
691 {
692 /*
693 * Assign bus_dma resources
694 */
695 acpi_config_dma(sc);
696
697 /*
698 * Configure all everything found "at acpi?".
699 */
700 (void)acpi_rescan(sc->sc_dev, NULL, NULL);
701
702 /*
703 * Update GPE information.
704 *
705 * Note that this must be called after
706 * all GPE handlers have been installed.
707 */
708 (void)AcpiUpdateAllGpes();
709
710 /*
711 * Defer rest of the configuration.
712 */
713 (void)config_defer(sc->sc_dev, acpi_rescan_capabilities);
714 }
715
716 static void
717 acpi_config_dma(struct acpi_softc *sc)
718 {
719 struct acpi_devnode *ad;
720
721 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
722
723 if (ad->ad_device != NULL)
724 continue;
725
726 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
727 continue;
728
729 ad->ad_dmat = acpi_get_dma_tag(sc, ad);
730 ad->ad_dmat64 = acpi_get_dma64_tag(sc, ad);
731 }
732 }
733
734 static ACPI_STATUS
735 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
736 void *context, void **status)
737 {
738 struct acpi_walkcontext *awc = context;
739 struct acpi_softc *sc = awc->aw_sc;
740 struct acpi_devnode *ad;
741 ACPI_DEVICE_INFO *devinfo;
742 ACPI_OBJECT_TYPE type;
743 ACPI_STATUS rv;
744
745 rv = AcpiGetObjectInfo(handle, &devinfo);
746
747 if (ACPI_FAILURE(rv))
748 return AE_OK; /* Do not terminate the walk. */
749
750 type = devinfo->Type;
751
752 switch (type) {
753
754 case ACPI_TYPE_DEVICE:
755 acpi_activate_device(handle, &devinfo);
756 /* FALLTHROUGH */
757
758 case ACPI_TYPE_PROCESSOR:
759 case ACPI_TYPE_THERMAL:
760 case ACPI_TYPE_POWER:
761
762 ad = kmem_zalloc(sizeof(*ad), KM_SLEEP);
763
764 ad->ad_device = NULL;
765 ad->ad_notify = NULL;
766 ad->ad_pciinfo = NULL;
767 ad->ad_wakedev = NULL;
768
769 ad->ad_type = type;
770 ad->ad_handle = handle;
771 ad->ad_devinfo = devinfo;
772
773 ad->ad_root = sc->sc_dev;
774 ad->ad_parent = awc->aw_parent;
775
776 acpi_match_node_init(ad);
777 acpi_make_name(ad, devinfo->Name);
778
779 /*
780 * Identify wake GPEs from the _PRW. Note that
781 * AcpiUpdateAllGpes() must be called afterwards.
782 */
783 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE)
784 acpi_wakedev_init(ad);
785
786 SIMPLEQ_INIT(&ad->ad_child_head);
787 SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
788
789 if (ad->ad_parent != NULL) {
790
791 SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
792 ad, ad_child_list);
793 }
794
795 awc->aw_parent = ad;
796 break;
797
798 default:
799 ACPI_FREE(devinfo);
800 break;
801 }
802
803 return AE_OK;
804 }
805
806 static ACPI_STATUS
807 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
808 void *context, void **status)
809 {
810 struct acpi_walkcontext *awc = context;
811
812 KASSERT(awc != NULL);
813 KASSERT(awc->aw_parent != NULL);
814
815 if (handle == awc->aw_parent->ad_handle)
816 awc->aw_parent = awc->aw_parent->ad_parent;
817
818 return AE_OK;
819 }
820
821 static void
822 acpi_make_name(struct acpi_devnode *ad, uint32_t name)
823 {
824 ACPI_NAME_UNION *anu;
825 int clear, i;
826
827 anu = (ACPI_NAME_UNION *)&name;
828 ad->ad_name[4] = '\0';
829
830 for (i = 3, clear = 0; i >= 0; i--) {
831
832 if (clear == 0 && anu->Ascii[i] == '_')
833 ad->ad_name[i] = '\0';
834 else {
835 ad->ad_name[i] = anu->Ascii[i];
836 clear = 1;
837 }
838 }
839
840 if (ad->ad_name[0] == '\0')
841 ad->ad_name[0] = '_';
842 }
843
844 bus_dma_tag_t
845 acpi_default_dma_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
846 {
847 return sc->sc_dmat;
848 }
849 __weak_alias(acpi_get_dma_tag,acpi_default_dma_tag);
850
851 bus_dma_tag_t
852 acpi_default_dma64_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
853 {
854 return sc->sc_dmat64;
855 }
856 __weak_alias(acpi_get_dma64_tag,acpi_default_dma64_tag);
857
858 pci_chipset_tag_t
859 acpi_default_pci_chipset_tag(struct acpi_softc *sc, int seg, int bbn)
860 {
861 return NULL;
862 }
863 __weak_alias(acpi_get_pci_chipset_tag,acpi_default_pci_chipset_tag);
864
865 /*
866 * Device attachment.
867 */
868 static int
869 acpi_rescan(device_t self, const char *ifattr, const int *locators)
870 {
871 struct acpi_softc *sc = device_private(self);
872 struct acpi_attach_args aa;
873
874 /*
875 * Try to attach hpet(4) first via a specific table.
876 */
877 aa.aa_memt = sc->sc_memt;
878
879 if (ifattr_match(ifattr, "acpihpetbus") && sc->sc_hpet == NULL)
880 sc->sc_hpet = config_found_ia(sc->sc_dev,
881 "acpihpetbus", &aa, NULL);
882
883 /*
884 * A two-pass scan for acpinodebus.
885 */
886 if (ifattr_match(ifattr, "acpinodebus")) {
887 acpi_rescan_early(sc);
888 acpi_rescan_nodes(sc);
889 }
890
891 /*
892 * Attach APM emulation and acpiwdrt(4).
893 */
894 if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
895 sc->sc_apmbus = config_found_ia(sc->sc_dev,
896 "acpiapmbus", NULL, NULL);
897
898 if (ifattr_match(ifattr, "acpiwdrtbus") && sc->sc_wdrt == NULL)
899 sc->sc_wdrt = config_found_ia(sc->sc_dev,
900 "acpiwdrtbus", NULL, NULL);
901
902 return 0;
903 }
904
905 static void
906 acpi_rescan_early(struct acpi_softc *sc)
907 {
908 struct acpi_attach_args aa;
909 struct acpi_devnode *ad;
910
911 /*
912 * First scan for devices such as acpiec(4) that
913 * should be always attached before anything else.
914 * We want these devices to attach regardless of
915 * the device status and other restrictions.
916 */
917 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
918
919 if (ad->ad_device != NULL)
920 continue;
921
922 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
923 continue;
924
925 if (acpi_match_hid(ad->ad_devinfo, acpi_early_ids) == 0)
926 continue;
927
928 aa.aa_node = ad;
929 aa.aa_iot = sc->sc_iot;
930 aa.aa_memt = sc->sc_memt;
931 if (ad->ad_pciinfo != NULL) {
932 aa.aa_pc = ad->ad_pciinfo->ap_pc;
933 aa.aa_pciflags = sc->sc_pciflags;
934 }
935 aa.aa_ic = sc->sc_ic;
936 aa.aa_dmat = ad->ad_dmat;
937 aa.aa_dmat64 = ad->ad_dmat64;
938
939 ad->ad_device = config_found_ia(sc->sc_dev,
940 "acpinodebus", &aa, acpi_print);
941 }
942 }
943
944 static void
945 acpi_rescan_nodes(struct acpi_softc *sc)
946 {
947 const char * const hpet_ids[] = { "PNP0103", NULL };
948 struct acpi_attach_args aa;
949 struct acpi_devnode *ad;
950 ACPI_DEVICE_INFO *di;
951
952 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
953
954 if (ad->ad_device != NULL)
955 continue;
956
957 /*
958 * There is a bug in ACPICA: it defines the type
959 * of the scopes incorrectly for its own reasons.
960 */
961 if (acpi_is_scope(ad) != false)
962 continue;
963
964 di = ad->ad_devinfo;
965
966 /*
967 * We only attach devices which are present, enabled, and
968 * functioning properly. However, if a device is enabled,
969 * it is decoding resources and we should claim these,
970 * if possible. This requires changes to bus_space(9).
971 */
972 if (di->Type == ACPI_TYPE_DEVICE &&
973 !acpi_device_present(ad->ad_handle)) {
974 continue;
975 }
976
977 if (di->Type == ACPI_TYPE_POWER)
978 continue;
979
980 if (di->Type == ACPI_TYPE_PROCESSOR)
981 continue;
982
983 if (acpi_match_hid(di, acpi_early_ids) != 0)
984 continue;
985
986 if (acpi_match_hid(di, acpi_ignored_ids) != 0)
987 continue;
988
989 if (acpi_match_hid(di, hpet_ids) != 0 && sc->sc_hpet != NULL)
990 continue;
991
992 aa.aa_node = ad;
993 aa.aa_iot = sc->sc_iot;
994 aa.aa_memt = sc->sc_memt;
995 if (ad->ad_pciinfo != NULL) {
996 aa.aa_pc = ad->ad_pciinfo->ap_pc;
997 aa.aa_pciflags = sc->sc_pciflags;
998 }
999 aa.aa_ic = sc->sc_ic;
1000 aa.aa_dmat = ad->ad_dmat;
1001 aa.aa_dmat64 = ad->ad_dmat64;
1002
1003 ad->ad_device = config_found_ia(sc->sc_dev,
1004 "acpinodebus", &aa, acpi_print);
1005 }
1006 }
1007
1008 static void
1009 acpi_rescan_capabilities(device_t self)
1010 {
1011 struct acpi_softc *sc = device_private(self);
1012 struct acpi_devnode *ad;
1013 ACPI_HANDLE tmp;
1014 ACPI_STATUS rv;
1015
1016 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1017
1018 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
1019 continue;
1020
1021 /*
1022 * Scan power resource capabilities.
1023 *
1024 * If any power states are supported,
1025 * at least _PR0 and _PR3 must be present.
1026 */
1027 rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
1028
1029 if (ACPI_SUCCESS(rv)) {
1030 ad->ad_flags |= ACPI_DEVICE_POWER;
1031 acpi_power_add(ad);
1032 }
1033
1034 /*
1035 * Scan wake-up capabilities.
1036 */
1037 if (ad->ad_wakedev != NULL) {
1038 ad->ad_flags |= ACPI_DEVICE_WAKEUP;
1039 acpi_wakedev_add(ad);
1040 }
1041
1042 /*
1043 * Scan docking stations.
1044 */
1045 rv = AcpiGetHandle(ad->ad_handle, "_DCK", &tmp);
1046
1047 if (ACPI_SUCCESS(rv))
1048 ad->ad_flags |= ACPI_DEVICE_DOCK;
1049
1050 /*
1051 * Scan devices that are ejectable.
1052 */
1053 rv = AcpiGetHandle(ad->ad_handle, "_EJ0", &tmp);
1054
1055 if (ACPI_SUCCESS(rv))
1056 ad->ad_flags |= ACPI_DEVICE_EJECT;
1057 }
1058 }
1059
1060 static int
1061 acpi_print(void *aux, const char *pnp)
1062 {
1063 struct acpi_attach_args *aa = aux;
1064 struct acpi_devnode *ad;
1065 const char *hid, *uid;
1066 ACPI_DEVICE_INFO *di;
1067
1068 ad = aa->aa_node;
1069 di = ad->ad_devinfo;
1070
1071 hid = di->HardwareId.String;
1072 uid = di->UniqueId.String;
1073
1074 if (pnp != NULL) {
1075
1076 if (di->Type != ACPI_TYPE_DEVICE) {
1077
1078 aprint_normal("%s (ACPI Object Type '%s') at %s",
1079 ad->ad_name, AcpiUtGetTypeName(ad->ad_type), pnp);
1080
1081 return UNCONF;
1082 }
1083
1084 if ((di->Valid & ACPI_VALID_HID) == 0 || hid == NULL)
1085 return 0;
1086
1087 aprint_normal("%s (%s) ", ad->ad_name, hid);
1088 acpi_print_dev(hid);
1089 aprint_normal("at %s", pnp);
1090
1091 return UNCONF;
1092 }
1093
1094 aprint_normal(" (%s", ad->ad_name);
1095
1096 if ((di->Valid & ACPI_VALID_HID) != 0 && hid != NULL) {
1097
1098 aprint_normal(", %s", hid);
1099
1100 if ((di->Valid & ACPI_VALID_UID) != 0 && uid != NULL) {
1101
1102 if (uid[0] == '\0')
1103 uid = "<null>";
1104
1105 aprint_normal("-%s", uid);
1106 }
1107 }
1108
1109 aprint_normal(")");
1110
1111 return UNCONF;
1112 }
1113
1114 /*
1115 * Notify.
1116 */
1117 static void
1118 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
1119 {
1120 struct acpi_softc *sc = acpi_softc;
1121 struct acpi_devnode *ad;
1122
1123 KASSERT(sc != NULL);
1124 KASSERT(aux == NULL);
1125 KASSERT(acpi_active != 0);
1126
1127 if (acpi_suspended != 0)
1128 return;
1129
1130 /*
1131 * System: 0x00 - 0x7F.
1132 * Device: 0x80 - 0xFF.
1133 */
1134 switch (event) {
1135
1136 case ACPI_NOTIFY_BUS_CHECK:
1137 case ACPI_NOTIFY_DEVICE_CHECK:
1138 case ACPI_NOTIFY_DEVICE_WAKE:
1139 case ACPI_NOTIFY_EJECT_REQUEST:
1140 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
1141 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1142 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1143 case ACPI_NOTIFY_POWER_FAULT:
1144 case ACPI_NOTIFY_CAPABILITIES_CHECK:
1145 case ACPI_NOTIFY_DEVICE_PLD_CHECK:
1146 case ACPI_NOTIFY_RESERVED:
1147 case ACPI_NOTIFY_LOCALITY_UPDATE:
1148 break;
1149 }
1150
1151 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
1152 "%s (%p)\n", event, acpi_name(handle), handle));
1153
1154 /*
1155 * We deliver notifications only to drivers
1156 * that have been successfully attached and
1157 * that have registered a handler with us.
1158 * The opaque pointer is always the device_t.
1159 */
1160 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
1161
1162 if (ad->ad_device == NULL)
1163 continue;
1164
1165 if (ad->ad_notify == NULL)
1166 continue;
1167
1168 if (ad->ad_handle != handle)
1169 continue;
1170
1171 (*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
1172
1173 return;
1174 }
1175
1176 aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
1177 "for %s (%p)\n", event, acpi_name(handle), handle);
1178 }
1179
1180 bool
1181 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
1182 {
1183 struct acpi_softc *sc = acpi_softc;
1184
1185 KASSERT(sc != NULL);
1186 KASSERT(acpi_active != 0);
1187
1188 if (acpi_suspended != 0)
1189 goto fail;
1190
1191 if (ad == NULL || notify == NULL)
1192 goto fail;
1193
1194 ad->ad_notify = notify;
1195
1196 return true;
1197
1198 fail:
1199 aprint_error_dev(sc->sc_dev, "failed to register notify "
1200 "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
1201
1202 return false;
1203 }
1204
1205 void
1206 acpi_deregister_notify(struct acpi_devnode *ad)
1207 {
1208
1209 ad->ad_notify = NULL;
1210 }
1211
1212 /*
1213 * Fixed buttons.
1214 */
1215 static void
1216 acpi_register_fixed_button(struct acpi_softc *sc, int event)
1217 {
1218 struct sysmon_pswitch *smpsw;
1219 ACPI_STATUS rv;
1220 int type;
1221
1222 switch (event) {
1223
1224 case ACPI_EVENT_POWER_BUTTON:
1225
1226 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
1227 return;
1228
1229 type = PSWITCH_TYPE_POWER;
1230 smpsw = &sc->sc_smpsw_power;
1231 break;
1232
1233 case ACPI_EVENT_SLEEP_BUTTON:
1234
1235 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
1236 return;
1237
1238 type = PSWITCH_TYPE_SLEEP;
1239 smpsw = &sc->sc_smpsw_sleep;
1240 break;
1241
1242 default:
1243 rv = AE_TYPE;
1244 goto fail;
1245 }
1246
1247 smpsw->smpsw_type = type;
1248 smpsw->smpsw_name = device_xname(sc->sc_dev);
1249
1250 if (sysmon_pswitch_register(smpsw) != 0) {
1251 rv = AE_ERROR;
1252 goto fail;
1253 }
1254
1255 AcpiClearEvent(event);
1256
1257 rv = AcpiInstallFixedEventHandler(event,
1258 acpi_fixed_button_handler, smpsw);
1259
1260 if (ACPI_FAILURE(rv)) {
1261 sysmon_pswitch_unregister(smpsw);
1262 goto fail;
1263 }
1264
1265 aprint_normal_dev(sc->sc_dev, "fixed %s button present\n",
1266 (type != PSWITCH_TYPE_SLEEP) ? "power" : "sleep");
1267
1268 return;
1269
1270 fail:
1271 aprint_error_dev(sc->sc_dev, "failed to register "
1272 "fixed event %d: %s\n", event, AcpiFormatException(rv));
1273 }
1274
1275 static void
1276 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
1277 {
1278 struct sysmon_pswitch *smpsw;
1279 ACPI_STATUS rv;
1280
1281 switch (event) {
1282
1283 case ACPI_EVENT_POWER_BUTTON:
1284 smpsw = &sc->sc_smpsw_power;
1285
1286 if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
1287 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
1288 return;
1289 }
1290
1291 break;
1292
1293 case ACPI_EVENT_SLEEP_BUTTON:
1294 smpsw = &sc->sc_smpsw_sleep;
1295
1296 if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
1297 KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
1298 return;
1299 }
1300
1301 break;
1302
1303 default:
1304 rv = AE_TYPE;
1305 goto fail;
1306 }
1307
1308 rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
1309
1310 if (ACPI_SUCCESS(rv)) {
1311 sysmon_pswitch_unregister(smpsw);
1312 return;
1313 }
1314
1315 fail:
1316 aprint_error_dev(sc->sc_dev, "failed to deregister "
1317 "fixed event: %s\n", AcpiFormatException(rv));
1318 }
1319
1320 static uint32_t
1321 acpi_fixed_button_handler(void *context)
1322 {
1323 static const int handler = OSL_NOTIFY_HANDLER;
1324 struct sysmon_pswitch *smpsw = context;
1325
1326 (void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
1327
1328 return ACPI_INTERRUPT_HANDLED;
1329 }
1330
1331 static void
1332 acpi_fixed_button_pressed(void *context)
1333 {
1334 struct sysmon_pswitch *smpsw = context;
1335
1336 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
1337 (smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
1338 "power" : "sleep"));
1339
1340 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
1341 }
1342
1343 /*
1344 * Sleep.
1345 */
1346 static void
1347 acpi_sleep_init(struct acpi_softc *sc)
1348 {
1349 uint8_t a, b, i;
1350 ACPI_STATUS rv;
1351
1352 CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
1353 CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
1354 CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
1355
1356 /*
1357 * Evaluate supported sleep states.
1358 */
1359 for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
1360
1361 rv = AcpiGetSleepTypeData(i, &a, &b);
1362
1363 if (ACPI_SUCCESS(rv))
1364 sc->sc_sleepstates |= __BIT(i);
1365 }
1366 }
1367
1368 /*
1369 * Must be called with interrupts enabled.
1370 */
1371 void
1372 acpi_enter_sleep_state(int state)
1373 {
1374 struct acpi_softc *sc = acpi_softc;
1375 ACPI_STATUS rv;
1376
1377 if (acpi_softc == NULL)
1378 return;
1379
1380 if (state == sc->sc_sleepstate)
1381 return;
1382
1383 if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5)
1384 return;
1385
1386 aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
1387
1388 switch (state) {
1389
1390 case ACPI_STATE_S0:
1391 sc->sc_sleepstate = ACPI_STATE_S0;
1392 return;
1393
1394 case ACPI_STATE_S1:
1395 case ACPI_STATE_S2:
1396 case ACPI_STATE_S3:
1397 case ACPI_STATE_S4:
1398
1399 if ((sc->sc_sleepstates & __BIT(state)) == 0) {
1400 aprint_error_dev(sc->sc_dev, "sleep state "
1401 "S%d is not available\n", state);
1402 return;
1403 }
1404
1405 /*
1406 * Evaluate the _TTS method. This should be done before
1407 * pmf_system_suspend(9) and the evaluation of _PTS.
1408 * We should also re-evaluate this once we return to
1409 * S0 or if we abort the sleep state transition in the
1410 * middle (see ACPI 3.0, section 7.3.6). In reality,
1411 * however, the _TTS method is seldom seen in the field.
1412 */
1413 rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
1414
1415 if (ACPI_SUCCESS(rv))
1416 aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
1417
1418 if (state != ACPI_STATE_S1 &&
1419 pmf_system_suspend(PMF_Q_NONE) != true) {
1420 aprint_error_dev(sc->sc_dev, "aborting suspend\n");
1421 break;
1422 }
1423
1424 /*
1425 * This will evaluate the _PTS and _SST methods,
1426 * but unlike the documentation claims, not _GTS,
1427 * which is evaluated in AcpiEnterSleepState().
1428 * This must be called with interrupts enabled.
1429 */
1430 rv = AcpiEnterSleepStatePrep(state);
1431
1432 if (ACPI_FAILURE(rv)) {
1433 aprint_error_dev(sc->sc_dev, "failed to prepare "
1434 "S%d: %s\n", state, AcpiFormatException(rv));
1435 break;
1436 }
1437
1438 /*
1439 * After the _PTS method has been evaluated, we can
1440 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
1441 */
1442 acpi_wakedev_commit(sc, state);
1443
1444 sc->sc_sleepstate = state;
1445
1446 if (state == ACPI_STATE_S1) {
1447
1448 /*
1449 * Before the transition to S1, CPU caches
1450 * must be flushed (see ACPI 4.0, 7.3.4.2).
1451 *
1452 * Note that interrupts must be off before
1453 * calling AcpiEnterSleepState(). Conversely,
1454 * AcpiLeaveSleepState() should always be
1455 * called with interrupts enabled.
1456 */
1457 acpi_md_OsDisableInterrupt();
1458
1459 ACPI_FLUSH_CPU_CACHE();
1460 rv = AcpiEnterSleepState(state);
1461
1462 if (ACPI_FAILURE(rv))
1463 aprint_error_dev(sc->sc_dev, "failed to "
1464 "enter S1: %s\n", AcpiFormatException(rv));
1465
1466 /*
1467 * Clear fixed events and disable all GPEs before
1468 * interrupts are enabled.
1469 */
1470 AcpiClearEvent(ACPI_EVENT_PMTIMER);
1471 AcpiClearEvent(ACPI_EVENT_GLOBAL);
1472 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1473 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
1474 AcpiClearEvent(ACPI_EVENT_RTC);
1475 #if (!ACPI_REDUCED_HARDWARE)
1476 AcpiHwDisableAllGpes();
1477 #endif
1478
1479 acpi_md_OsEnableInterrupt();
1480 rv = AcpiLeaveSleepState(state);
1481
1482 } else {
1483
1484 (void)acpi_md_sleep(state);
1485
1486 if (state == ACPI_STATE_S4)
1487 AcpiEnable();
1488
1489 (void)pmf_system_bus_resume(PMF_Q_NONE);
1490 (void)AcpiLeaveSleepState(state);
1491 (void)AcpiSetFirmwareWakingVector(0, 0);
1492 (void)pmf_system_resume(PMF_Q_NONE);
1493 }
1494
1495 /*
1496 * No wake GPEs should be enabled at runtime.
1497 */
1498 acpi_wakedev_commit(sc, ACPI_STATE_S0);
1499 break;
1500
1501 case ACPI_STATE_S5:
1502
1503 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
1504
1505 rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1506
1507 if (ACPI_FAILURE(rv)) {
1508 aprint_error_dev(sc->sc_dev, "failed to prepare "
1509 "S%d: %s\n", state, AcpiFormatException(rv));
1510 break;
1511 }
1512
1513 (void)AcpiDisableAllGpes();
1514
1515 DELAY(1000000);
1516
1517 sc->sc_sleepstate = state;
1518 acpi_md_OsDisableInterrupt();
1519
1520 (void)AcpiEnterSleepState(ACPI_STATE_S5);
1521
1522 aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
1523
1524 break;
1525 }
1526
1527 sc->sc_sleepstate = ACPI_STATE_S0;
1528
1529 (void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
1530 }
1531
1532 /*
1533 * Sysctl.
1534 */
1535 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1536 {
1537 const struct sysctlnode *rnode, *snode;
1538 int err;
1539
1540 err = sysctl_createv(clog, 0, NULL, &rnode,
1541 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1542 "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
1543 NULL, 0, NULL, 0,
1544 CTL_HW, CTL_CREATE, CTL_EOL);
1545
1546 if (err != 0)
1547 return;
1548
1549 (void)sysctl_createv(NULL, 0, &rnode, NULL,
1550 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1551 "root", SYSCTL_DESCR("ACPI root pointer"),
1552 NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
1553 CTL_CREATE, CTL_EOL);
1554
1555 err = sysctl_createv(clog, 0, &rnode, &snode,
1556 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1557 "sleep", SYSCTL_DESCR("ACPI sleep"),
1558 NULL, 0, NULL, 0,
1559 CTL_CREATE, CTL_EOL);
1560
1561 if (err != 0)
1562 return;
1563
1564 (void)sysctl_createv(NULL, 0, &snode, NULL,
1565 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
1566 "state", SYSCTL_DESCR("System sleep state"),
1567 sysctl_hw_acpi_sleepstate, 0, NULL, 0,
1568 CTL_CREATE, CTL_EOL);
1569
1570 (void)sysctl_createv(NULL, 0, &snode, NULL,
1571 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
1572 "states", SYSCTL_DESCR("Supported sleep states"),
1573 sysctl_hw_acpi_sleepstates, 0, NULL, 0,
1574 CTL_CREATE, CTL_EOL);
1575
1576 err = sysctl_createv(clog, 0, &rnode, &rnode,
1577 CTLFLAG_PERMANENT, CTLTYPE_NODE,
1578 "stat", SYSCTL_DESCR("ACPI statistics"),
1579 NULL, 0, NULL, 0,
1580 CTL_CREATE, CTL_EOL);
1581
1582 if (err != 0)
1583 return;
1584
1585 (void)sysctl_createv(clog, 0, &rnode, NULL,
1586 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1587 "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
1588 NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
1589 CTL_CREATE, CTL_EOL);
1590
1591 (void)sysctl_createv(clog, 0, &rnode, NULL,
1592 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1593 "sci", SYSCTL_DESCR("Number of SCI interrupts"),
1594 NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
1595 CTL_CREATE, CTL_EOL);
1596
1597 (void)sysctl_createv(clog, 0, &rnode, NULL,
1598 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1599 "fixed", SYSCTL_DESCR("Number of fixed events"),
1600 sysctl_hw_acpi_fixedstats, 0, NULL, 0,
1601 CTL_CREATE, CTL_EOL);
1602
1603 (void)sysctl_createv(clog, 0, &rnode, NULL,
1604 CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
1605 "method", SYSCTL_DESCR("Number of methods executed"),
1606 NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
1607 CTL_CREATE, CTL_EOL);
1608
1609 CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
1610 CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
1611 }
1612
1613 static int
1614 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
1615 {
1616 struct sysctlnode node;
1617 uint64_t t;
1618 int err, i;
1619
1620 for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
1621 t += AcpiFixedEventCount[i];
1622
1623 node = *rnode;
1624 node.sysctl_data = &t;
1625
1626 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1627
1628 if (err || newp == NULL)
1629 return err;
1630
1631 return 0;
1632 }
1633
1634 static int
1635 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1636 {
1637 struct acpi_softc *sc = acpi_softc;
1638 struct sysctlnode node;
1639 int err, t;
1640
1641 if (acpi_softc == NULL)
1642 return ENOSYS;
1643
1644 node = *rnode;
1645 t = sc->sc_sleepstate;
1646 node.sysctl_data = &t;
1647
1648 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1649
1650 if (err || newp == NULL)
1651 return err;
1652
1653 if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
1654 return EINVAL;
1655
1656 acpi_enter_sleep_state(t);
1657
1658 return 0;
1659 }
1660
1661 static int
1662 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
1663 {
1664 struct acpi_softc *sc = acpi_softc;
1665 struct sysctlnode node;
1666 char t[3 * 6 + 1];
1667 int err;
1668
1669 if (acpi_softc == NULL)
1670 return ENOSYS;
1671
1672 (void)memset(t, '\0', sizeof(t));
1673
1674 (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
1675 ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
1676 ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
1677 ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
1678 ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
1679 ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
1680 ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
1681
1682 node = *rnode;
1683 node.sysctl_data = &t;
1684
1685 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1686
1687 if (err || newp == NULL)
1688 return err;
1689
1690 return 0;
1691 }
1692
1693 /*
1694 * Tables.
1695 */
1696 ACPI_PHYSICAL_ADDRESS
1697 acpi_OsGetRootPointer(void)
1698 {
1699 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
1700
1701 /*
1702 * We let MD code handle this since there are multiple ways to do it:
1703 *
1704 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
1705 *
1706 * IA-64: Use the EFI.
1707 */
1708 PhysicalAddress = acpi_md_OsGetRootPointer();
1709
1710 if (acpi_root_pointer == 0)
1711 acpi_root_pointer = PhysicalAddress;
1712
1713 return PhysicalAddress;
1714 }
1715
1716 static ACPI_TABLE_HEADER *
1717 acpi_map_rsdt(void)
1718 {
1719 ACPI_PHYSICAL_ADDRESS paddr;
1720 ACPI_TABLE_RSDP *rsdp;
1721
1722 paddr = AcpiOsGetRootPointer();
1723
1724 if (paddr == 0)
1725 return NULL;
1726
1727 rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
1728
1729 if (rsdp == NULL)
1730 return NULL;
1731
1732 if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
1733 paddr = rsdp->XsdtPhysicalAddress;
1734 else
1735 paddr = rsdp->RsdtPhysicalAddress;
1736
1737 AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
1738
1739 return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
1740 }
1741
1742 /*
1743 * XXX: Refactor to be a generic function that unmaps tables.
1744 */
1745 static void
1746 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
1747 {
1748
1749 if (rsdt == NULL)
1750 return;
1751
1752 AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
1753 }
1754
1755 /*
1756 * XXX: Refactor to be a generic function that maps tables.
1757 */
1758 ACPI_STATUS
1759 acpi_madt_map(void)
1760 {
1761 ACPI_STATUS rv;
1762
1763 if (madt_header != NULL)
1764 return AE_ALREADY_EXISTS;
1765
1766 rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
1767
1768 if (ACPI_FAILURE(rv))
1769 return rv;
1770
1771 return AE_OK;
1772 }
1773
1774 void
1775 acpi_madt_unmap(void)
1776 {
1777 madt_header = NULL;
1778 }
1779
1780 ACPI_STATUS
1781 acpi_gtdt_map(void)
1782 {
1783 ACPI_STATUS rv;
1784
1785 if (gtdt_header != NULL)
1786 return AE_ALREADY_EXISTS;
1787
1788 rv = AcpiGetTable(ACPI_SIG_GTDT, 1, >dt_header);
1789
1790 if (ACPI_FAILURE(rv))
1791 return rv;
1792
1793 return AE_OK;
1794 }
1795
1796 void
1797 acpi_gtdt_unmap(void)
1798 {
1799 gtdt_header = NULL;
1800 }
1801
1802 /*
1803 * XXX: Refactor to be a generic function that walks tables.
1804 */
1805 void
1806 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
1807 {
1808 ACPI_SUBTABLE_HEADER *hdrp;
1809 char *madtend, *where;
1810
1811 madtend = (char *)madt_header + madt_header->Length;
1812 where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
1813
1814 while (where < madtend) {
1815
1816 hdrp = (ACPI_SUBTABLE_HEADER *)where;
1817
1818 if (hdrp->Length == 0 || ACPI_FAILURE(func(hdrp, aux)))
1819 break;
1820
1821 where += hdrp->Length;
1822 }
1823 }
1824
1825 void
1826 acpi_gtdt_walk(ACPI_STATUS (*func)(ACPI_GTDT_HEADER *, void *), void *aux)
1827 {
1828 ACPI_GTDT_HEADER *hdrp;
1829 char *gtdtend, *where;
1830
1831 gtdtend = (char *)gtdt_header + gtdt_header->Length;
1832 where = (char *)gtdt_header + sizeof (ACPI_TABLE_GTDT);
1833
1834 while (where < gtdtend) {
1835
1836 hdrp = (ACPI_GTDT_HEADER *)where;
1837
1838 if (hdrp->Length == 0 || ACPI_FAILURE(func(hdrp, aux)))
1839 break;
1840
1841 where += hdrp->Length;
1842 }
1843 }
1844
1845 /*
1846 * Miscellaneous.
1847 */
1848 static bool
1849 acpi_is_scope(struct acpi_devnode *ad)
1850 {
1851 int i;
1852
1853 /*
1854 * Return true if the node is a root scope.
1855 */
1856 if (ad->ad_parent == NULL)
1857 return false;
1858
1859 if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
1860 return false;
1861
1862 for (i = 0; i < __arraycount(acpi_scopes); i++) {
1863
1864 if (acpi_scopes[i] == NULL)
1865 continue;
1866
1867 if (ad->ad_handle == acpi_scopes[i])
1868 return true;
1869 }
1870
1871 return false;
1872 }
1873
1874 bool
1875 acpi_device_present(ACPI_HANDLE handle)
1876 {
1877 ACPI_STATUS rv;
1878 ACPI_INTEGER sta;
1879
1880 rv = acpi_eval_integer(handle, "_STA", &sta);
1881
1882 if (ACPI_FAILURE(rv)) {
1883 /* No _STA method -> must be there */
1884 return rv == AE_NOT_FOUND;
1885 }
1886
1887 return (sta & ACPI_STA_OK) == ACPI_STA_OK;
1888 }
1889
1890 /*
1891 * ACPIVERBOSE.
1892 */
1893 void
1894 acpi_load_verbose(void)
1895 {
1896
1897 if (acpi_verbose_loaded == 0)
1898 module_autoload("acpiverbose", MODULE_CLASS_MISC);
1899 }
1900
1901 void
1902 acpi_print_verbose_stub(struct acpi_softc *sc)
1903 {
1904
1905 acpi_load_verbose();
1906
1907 if (acpi_verbose_loaded != 0)
1908 acpi_print_verbose(sc);
1909 }
1910
1911 void
1912 acpi_print_dev_stub(const char *pnpstr)
1913 {
1914
1915 acpi_load_verbose();
1916
1917 if (acpi_verbose_loaded != 0)
1918 acpi_print_dev(pnpstr);
1919 }
1920
1921 MALLOC_DECLARE(M_ACPI); /* XXX: ACPI_ACTIVATE_DEV should use kmem(9). */
1922
1923 /*
1924 * ACPI_ACTIVATE_DEV.
1925 */
1926 static void
1927 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
1928 {
1929
1930 #ifndef ACPI_ACTIVATE_DEV
1931 return;
1932 }
1933 #else
1934 static const int valid = ACPI_VALID_HID;
1935 ACPI_DEVICE_INFO *newdi;
1936 ACPI_STATUS rv;
1937
1938
1939 /*
1940 * If the device is valid and present,
1941 * but not enabled, try to activate it.
1942 */
1943 if (((*di)->Valid & valid) != valid)
1944 return;
1945
1946 if (!acpi_device_present(handle))
1947 return;
1948
1949 rv = acpi_allocate_resources(handle);
1950
1951 if (ACPI_FAILURE(rv))
1952 goto fail;
1953
1954 rv = AcpiGetObjectInfo(handle, &newdi);
1955
1956 if (ACPI_FAILURE(rv))
1957 goto fail;
1958
1959 ACPI_FREE(*di);
1960 *di = newdi;
1961
1962 aprint_verbose_dev(acpi_softc->sc_dev,
1963 "%s activated\n", (*di)->HardwareId.String);
1964
1965 return;
1966
1967 fail:
1968 aprint_error_dev(acpi_softc->sc_dev, "failed to "
1969 "activate %s\n", (*di)->HardwareId.String);
1970 }
1971
1972 /*
1973 * XXX: This very incomplete.
1974 */
1975 ACPI_STATUS
1976 acpi_allocate_resources(ACPI_HANDLE handle)
1977 {
1978 ACPI_BUFFER bufp, bufc, bufn;
1979 ACPI_RESOURCE *resp, *resc, *resn;
1980 ACPI_RESOURCE_IRQ *irq;
1981 #if 0
1982 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1983 #endif
1984 ACPI_STATUS rv;
1985 uint delta;
1986
1987 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1988 if (ACPI_FAILURE(rv))
1989 goto out;
1990 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1991 if (ACPI_FAILURE(rv)) {
1992 goto out1;
1993 }
1994
1995 bufn.Length = 1000;
1996 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1997 resp = bufp.Pointer;
1998 resc = bufc.Pointer;
1999 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
2000 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
2001 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
2002 resp = ACPI_NEXT_RESOURCE(resp);
2003 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
2004 break;
2005 /* Found identical Id */
2006 resn->Type = resc->Type;
2007 switch (resc->Type) {
2008 case ACPI_RESOURCE_TYPE_IRQ:
2009 memcpy(&resn->Data, &resp->Data,
2010 sizeof(ACPI_RESOURCE_IRQ));
2011 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
2012 irq->Interrupts[0] =
2013 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
2014 Interrupts[irq->InterruptCount-1];
2015 irq->InterruptCount = 1;
2016 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
2017 break;
2018 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2019 memcpy(&resn->Data, &resp->Data,
2020 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
2021 #if 0
2022 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
2023 /*
2024 * XXX: Not duplicating the interrupt logic above
2025 * because its not clear what it accomplishes.
2026 */
2027 xirq->Interrupts[0] =
2028 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
2029 Interrupts[irq->NumberOfInterrupts-1];
2030 xirq->NumberOfInterrupts = 1;
2031 #endif
2032 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
2033 break;
2034 case ACPI_RESOURCE_TYPE_IO:
2035 memcpy(&resn->Data, &resp->Data,
2036 sizeof(ACPI_RESOURCE_IO));
2037 resn->Length = resp->Length;
2038 break;
2039 default:
2040 aprint_error_dev(acpi_softc->sc_dev,
2041 "%s: invalid type %u\n", __func__, resc->Type);
2042 rv = AE_BAD_DATA;
2043 goto out2;
2044 }
2045 resc = ACPI_NEXT_RESOURCE(resc);
2046 resn = ACPI_NEXT_RESOURCE(resn);
2047 resp = ACPI_NEXT_RESOURCE(resp);
2048 delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
2049 if (delta >=
2050 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
2051 bufn.Length *= 2;
2052 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
2053 M_ACPI, M_WAITOK);
2054 resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
2055 delta);
2056 }
2057 }
2058
2059 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
2060 aprint_error_dev(acpi_softc->sc_dev,
2061 "%s: resc not exhausted\n", __func__);
2062 rv = AE_BAD_DATA;
2063 goto out3;
2064 }
2065
2066 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
2067 rv = AcpiSetCurrentResources(handle, &bufn);
2068
2069 if (ACPI_FAILURE(rv))
2070 aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
2071 "resources: %s\n", __func__, AcpiFormatException(rv));
2072
2073 out3:
2074 free(bufn.Pointer, M_ACPI);
2075 out2:
2076 ACPI_FREE(bufc.Pointer);
2077 out1:
2078 ACPI_FREE(bufp.Pointer);
2079 out:
2080 return rv;
2081 }
2082
2083 #endif /* ACPI_ACTIVATE_DEV */
2084