Mon Dec 30 19:49:38 2019 UTC ()
MD code may allocate a new dma tag for attach args, so destroy it if the device node is not claimed


(jmcneill)
diff -r1.279 -r1.280 src/sys/dev/acpi/acpi.c

cvs diff -r1.279 -r1.280 src/sys/dev/acpi/acpi.c (switch to unified diff)

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