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