/
acpi.c
2117 lines (1665 loc) · 47.8 KB
/
acpi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: acpi.c,v 1.292 2021/05/12 23:22:33 thorpej Exp $ */
/*-
* Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum of By Noon Software, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Frank van der Linden for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright 2001, 2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.292 2021/05/12 23:22:33 thorpej Exp $");
#include "pci.h"
#include "opt_acpi.h"
#include "opt_pcifixup.h"
#include <sys/param.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/timetc.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_mcfg.h>
#include <dev/acpi/acpi_osd.h>
#include <dev/acpi/acpi_pci.h>
#include <dev/acpi/acpi_power.h>
#include <dev/acpi/acpi_timer.h>
#include <dev/acpi/acpi_wakedev.h>
#include <machine/acpi_machdep.h>
#include "ioconf.h"
#define _COMPONENT ACPI_BUS_COMPONENT
ACPI_MODULE_NAME ("acpi")
/*
* The acpi_active variable is set when the ACPI subsystem is active.
* Machine-dependent code may wish to skip other steps (such as attaching
* subsystems that ACPI supercedes) when ACPI is active.
*/
int acpi_active = 0;
int acpi_suspended = 0;
int acpi_force_load = 0;
int acpi_verbose_loaded = 0;
struct acpi_softc *acpi_softc = NULL;
static uint64_t acpi_root_pointer;
extern kmutex_t acpi_interrupt_list_mtx;
static ACPI_HANDLE acpi_scopes[4];
ACPI_TABLE_HEADER *madt_header;
ACPI_TABLE_HEADER *gtdt_header;
/*
* This structure provides a context for the ACPI
* namespace walk performed in acpi_build_tree().
*/
struct acpi_walkcontext {
struct acpi_softc *aw_sc;
struct acpi_devnode *aw_parent;
};
/*
* Ignored HIDs.
*/
static const char * const acpi_ignored_ids[] = {
#if defined(i386) || defined(x86_64)
"ACPI0007", /* ACPI CPUs do not attach to acpi(4) */
"PNP0000", /* AT interrupt controller is handled internally */
"PNP0001", /* EISA interrupt controller is handled internally */
"PNP0200", /* AT DMA controller is handled internally */
"PNP0A??", /* PCI Busses are handled internally */
"PNP0B00", /* AT RTC is handled internally */
"PNP0C02", /* PnP motherboard resources */
"PNP0C0F", /* ACPI PCI link devices are handled internally */
#endif
#if defined(x86_64)
"PNP0C04", /* FPU is handled internally */
#endif
#if defined(__aarch64__)
"ACPI0004", /* ACPI module devices are handled internally */
"PNP0C0F", /* ACPI PCI link devices are handled internally */
#endif
NULL
};
/*
* Devices that should be attached early.
*/
static const char * const acpi_early_ids[] = {
"PNP0C09", /* acpiec(4) */
NULL
};
static int acpi_match(device_t, cfdata_t, void *);
static int acpi_submatch(device_t, cfdata_t, const int *, void *);
static void acpi_attach(device_t, device_t, void *);
static int acpi_detach(device_t, int);
static void acpi_childdet(device_t, device_t);
static bool acpi_suspend(device_t, const pmf_qual_t *);
static bool acpi_resume(device_t, const pmf_qual_t *);
static void acpi_build_tree(struct acpi_softc *);
static void acpi_config_tree(struct acpi_softc *);
static void acpi_config_dma(struct acpi_softc *);
static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, uint32_t,
void *, void **);
static ACPI_STATUS acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
void *, void **);
static void acpi_make_name(struct acpi_devnode *, uint32_t);
static int acpi_rescan(device_t, const char *, const int *);
static void acpi_rescan_early(struct acpi_softc *);
static void acpi_rescan_nodes(struct acpi_softc *);
static void acpi_rescan_capabilities(device_t);
static int acpi_print(void *aux, const char *);
static void acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
static void acpi_register_fixed_button(struct acpi_softc *, int);
static void acpi_deregister_fixed_button(struct acpi_softc *, int);
static uint32_t acpi_fixed_button_handler(void *);
static void acpi_fixed_button_pressed(void *);
static void acpi_sleep_init(struct acpi_softc *);
static int sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
static int sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
static int sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
static bool acpi_is_scope(struct acpi_devnode *);
static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
void acpi_print_verbose_stub(struct acpi_softc *);
void acpi_print_dev_stub(const char *);
static void acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
ACPI_STATUS acpi_allocate_resources(ACPI_HANDLE);
void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
bus_dma_tag_t acpi_default_dma_tag(struct acpi_softc *, struct acpi_devnode *);
bus_dma_tag_t acpi_default_dma64_tag(struct acpi_softc *, struct acpi_devnode *);
pci_chipset_tag_t acpi_default_pci_chipset_tag(struct acpi_softc *, int, int);
CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
/*
* Probe for ACPI support.
*
* This is called by the machine-dependent ACPI front-end.
* Note: this is not an autoconfiguration interface function.
*/
int
acpi_probe(void)
{
ACPI_TABLE_HEADER *rsdt;
ACPI_STATUS rv;
int quirks;
if (acpi_softc != NULL)
panic("%s: already probed", __func__);
mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
/*
* Start up ACPICA.
*/
AcpiGbl_EnableInterpreterSlack = true;
rv = AcpiInitializeSubsystem();
if (ACPI_FAILURE(rv)) {
aprint_error("%s: failed to initialize subsystem\n", __func__);
return 0;
}
/*
* Allocate space for RSDT/XSDT and DSDT,
* but allow resizing if more tables exist.
*/
rv = AcpiInitializeTables(NULL, 2, true);
if (ACPI_FAILURE(rv)) {
aprint_error("%s: failed to initialize tables\n", __func__);
goto fail;
}
rv = AcpiLoadTables();
if (ACPI_FAILURE(rv)) {
aprint_error("%s: failed to load tables\n", __func__);
goto fail;
}
rsdt = acpi_map_rsdt();
if (rsdt == NULL) {
aprint_error("%s: failed to map RSDT\n", __func__);
goto fail;
}
quirks = acpi_find_quirks();
if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_BROKEN) != 0) {
aprint_normal("ACPI: BIOS is listed as broken:\n");
aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
"AslId <%4.4s,%08x>\n", rsdt->OemId, rsdt->OemTableId,
rsdt->OemRevision, rsdt->AslCompilerId,
rsdt->AslCompilerRevision);
aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
acpi_unmap_rsdt(rsdt);
goto fail;
}
if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_OLDBIOS) != 0) {
aprint_normal("ACPI: BIOS is too old (%s). "
"Set acpi_force_load to use.\n",
pmf_get_platform("bios-date"));
acpi_unmap_rsdt(rsdt);
goto fail;
}
acpi_unmap_rsdt(rsdt);
rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
if (ACPI_FAILURE(rv)) {
aprint_error("%s: failed to enable subsystem\n", __func__);
goto fail;
}
return 1;
fail:
(void)AcpiTerminate();
return 0;
}
void
acpi_disable(void)
{
if (acpi_softc == NULL)
return;
KASSERT(acpi_active != 0);
if (AcpiGbl_FADT.SmiCommand != 0)
AcpiDisable();
}
int
acpi_check(device_t parent, const char *ifattr)
{
return config_search(parent, NULL,
CFARG_SUBMATCH, acpi_submatch,
CFARG_IATTR, ifattr,
CFARG_EOL) != NULL;
}
int
acpi_reset(void)
{
struct acpi_softc *sc = acpi_softc;
ACPI_GENERIC_ADDRESS *ResetReg;
ACPI_PCI_ID PciId;
ACPI_STATUS status;
if (sc == NULL)
return ENXIO;
ResetReg = &AcpiGbl_FADT.ResetRegister;
/* Check if the reset register is supported */
if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
!ResetReg->Address) {
return ENOENT;
}
switch (ResetReg->SpaceId) {
case ACPI_ADR_SPACE_PCI_CONFIG:
PciId.Segment = PciId.Bus = 0;
PciId.Device = ACPI_GAS_PCI_DEV(ResetReg->Address);
PciId.Function = ACPI_GAS_PCI_FUNC(ResetReg->Address);
status = AcpiOsWritePciConfiguration(&PciId,
ACPI_GAS_PCI_REGOFF(ResetReg->Address),
AcpiGbl_FADT.ResetValue, ResetReg->BitWidth);
break;
case ACPI_ADR_SPACE_SYSTEM_IO:
case ACPI_ADR_SPACE_SYSTEM_MEMORY:
status = AcpiReset();
break;
default:
status = AE_TYPE;
break;
}
return ACPI_FAILURE(status) ? EIO : 0;
}
/*
* Autoconfiguration.
*/
static int
acpi_match(device_t parent, cfdata_t match, void *aux)
{
/*
* XXX: Nada; MD code has called acpi_probe().
*/
return 1;
}
static int
acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
{
struct cfattach *ca;
ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
return (ca == &acpi_ca);
}
static void
acpi_attach(device_t parent, device_t self, void *aux)
{
struct acpi_softc *sc = device_private(self);
struct acpibus_attach_args *aa = aux;
ACPI_TABLE_HEADER *rsdt, *hdr;
ACPI_STATUS rv;
int i;
aprint_naive("\n");
aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
if (acpi_softc != NULL)
panic("%s: already attached", __func__);
rsdt = acpi_map_rsdt();
if (rsdt == NULL)
aprint_error_dev(self, "X/RSDT: Not found\n");
else {
aprint_verbose_dev(self,
"X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
rsdt->OemId, rsdt->OemTableId,
rsdt->OemRevision,
rsdt->AslCompilerId, rsdt->AslCompilerRevision);
}
acpi_unmap_rsdt(rsdt);
sc->sc_dev = self;
sc->sc_root = NULL;
sc->sc_sleepstate = ACPI_STATE_S0;
sc->sc_quirks = acpi_find_quirks();
sysmon_power_settype("acpi");
sc->sc_iot = aa->aa_iot;
sc->sc_memt = aa->aa_memt;
sc->sc_pciflags = aa->aa_pciflags;
sc->sc_ic = aa->aa_ic;
sc->sc_dmat = aa->aa_dmat;
sc->sc_dmat64 = aa->aa_dmat64;
SIMPLEQ_INIT(&sc->ad_head);
acpi_softc = sc;
if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
aprint_error_dev(self, "couldn't establish power handler\n");
/*
* Bring ACPICA on-line.
*/
rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
if (ACPI_FAILURE(rv))
goto fail;
/*
* Early initialization of acpiec(4) via ECDT.
*/
config_found(self, aa, NULL,
CFARG_IATTR, "acpiecdtbus",
CFARG_EOL);
rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
if (ACPI_FAILURE(rv))
goto fail;
/*
* Scan the namespace and build our device tree.
*/
acpi_build_tree(sc);
#if NPCI > 0
/*
* Probe MCFG table
*/
acpimcfg_probe(sc);
#endif
acpi_md_callback(sc);
/*
* Early initialization of the _PDC control method
* that may load additional SSDT tables dynamically.
*/
(void)acpi_md_pdc();
/*
* Install global notify handlers.
*/
rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
if (ACPI_FAILURE(rv))
goto fail;
rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
if (ACPI_FAILURE(rv))
goto fail;
acpi_active = 1;
if (!AcpiGbl_ReducedHardware) {
/* Show SCI interrupt. */
aprint_verbose_dev(self, "SCI interrupting at int %u\n",
AcpiGbl_FADT.SciInterrupt);
/*
* Install fixed-event handlers.
*/
acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
}
/*
* Load drivers that operate on System Description Tables.
*/
for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; ++i) {
rv = AcpiGetTableByIndex(i, &hdr);
if (ACPI_FAILURE(rv)) {
continue;
}
config_found(sc->sc_dev, hdr, NULL,
CFARG_IATTR, "acpisdtbus",
CFARG_EOL);
AcpiPutTable(hdr);
}
acpitimer_init(sc);
acpi_config_tree(sc);
acpi_sleep_init(sc);
#ifdef ACPI_DEBUG
acpi_debug_init();
#endif
/*
* Print debug information.
*/
acpi_print_verbose(sc);
return;
fail:
aprint_error("%s: failed to initialize ACPI: %s\n",
__func__, AcpiFormatException(rv));
}
/*
* XXX: This is incomplete.
*/
static int
acpi_detach(device_t self, int flags)
{
struct acpi_softc *sc = device_private(self);
ACPI_STATUS rv;
int rc;
rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
if (ACPI_FAILURE(rv))
return EBUSY;
rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
ACPI_DEVICE_NOTIFY, acpi_notify_handler);
if (ACPI_FAILURE(rv))
return EBUSY;
if ((rc = config_detach_children(self, flags)) != 0)
return rc;
if ((rc = acpitimer_detach()) != 0)
return rc;
if (!AcpiGbl_ReducedHardware) {
acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
}
pmf_device_deregister(self);
acpi_softc = NULL;
return 0;
}
static void
acpi_childdet(device_t self, device_t child)
{
struct acpi_softc *sc = device_private(self);
struct acpi_devnode *ad;
if (sc->sc_apmbus == child)
sc->sc_apmbus = NULL;
if (sc->sc_hpet == child)
sc->sc_hpet = NULL;
if (sc->sc_wdrt == child)
sc->sc_wdrt = NULL;
SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
if (ad->ad_device == child)
ad->ad_device = NULL;
}
}
static bool
acpi_suspend(device_t dv, const pmf_qual_t *qual)
{
acpi_suspended = 1;
return true;
}
static bool
acpi_resume(device_t dv, const pmf_qual_t *qual)
{
acpi_suspended = 0;
return true;
}
/*
* Namespace scan.
*/
static void
acpi_build_tree(struct acpi_softc *sc)
{
struct acpi_walkcontext awc;
/*
* Get the root scope handles.
*/
KASSERT(__arraycount(acpi_scopes) == 4);
(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
/*
* Make the root node.
*/
awc.aw_sc = sc;
awc.aw_parent = NULL;
(void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
KASSERT(sc->sc_root == NULL);
KASSERT(awc.aw_parent != NULL);
sc->sc_root = awc.aw_parent;
/*
* Build the internal namespace.
*/
(void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
/*
* Scan the internal namespace.
*/
(void)acpi_pcidev_scan(sc->sc_root);
}
static void
acpi_config_tree(struct acpi_softc *sc)
{
/*
* Assign bus_dma resources
*/
acpi_config_dma(sc);
/*
* Configure all everything found "at acpi?".
*/
(void)acpi_rescan(sc->sc_dev, NULL, NULL);
/*
* Update GPE information.
*
* Note that this must be called after
* all GPE handlers have been installed.
*/
(void)AcpiUpdateAllGpes();
/*
* Defer rest of the configuration.
*/
(void)config_defer(sc->sc_dev, acpi_rescan_capabilities);
}
static void
acpi_config_dma(struct acpi_softc *sc)
{
struct acpi_devnode *ad;
SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
if (ad->ad_device != NULL)
continue;
if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
continue;
ad->ad_dmat = acpi_get_dma_tag(sc, ad);
ad->ad_dmat64 = acpi_get_dma64_tag(sc, ad);
}
}
static ACPI_STATUS
acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
void *context, void **status)
{
struct acpi_walkcontext *awc = context;
struct acpi_softc *sc = awc->aw_sc;
struct acpi_devnode *ad;
ACPI_DEVICE_INFO *devinfo;
ACPI_OBJECT_TYPE type;
ACPI_STATUS rv;
rv = AcpiGetObjectInfo(handle, &devinfo);
if (ACPI_FAILURE(rv))
return AE_OK; /* Do not terminate the walk. */
type = devinfo->Type;
switch (type) {
case ACPI_TYPE_DEVICE:
acpi_activate_device(handle, &devinfo);
/* FALLTHROUGH */
case ACPI_TYPE_PROCESSOR:
case ACPI_TYPE_THERMAL:
case ACPI_TYPE_POWER:
ad = kmem_zalloc(sizeof(*ad), KM_SLEEP);
ad->ad_device = NULL;
ad->ad_notify = NULL;
ad->ad_pciinfo = NULL;
ad->ad_wakedev = NULL;
ad->ad_type = type;
ad->ad_handle = handle;
ad->ad_devinfo = devinfo;
ad->ad_root = sc->sc_dev;
ad->ad_parent = awc->aw_parent;
acpi_match_node_init(ad);
acpi_make_name(ad, devinfo->Name);
/*
* Identify wake GPEs from the _PRW. Note that
* AcpiUpdateAllGpes() must be called afterwards.
*/
if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE)
acpi_wakedev_init(ad);
SIMPLEQ_INIT(&ad->ad_child_head);
SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
if (ad->ad_parent != NULL) {
SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
ad, ad_child_list);
}
awc->aw_parent = ad;
break;
default:
ACPI_FREE(devinfo);
break;
}
return AE_OK;
}
static ACPI_STATUS
acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
void *context, void **status)
{
struct acpi_walkcontext *awc = context;
KASSERT(awc != NULL);
KASSERT(awc->aw_parent != NULL);
if (handle == awc->aw_parent->ad_handle)
awc->aw_parent = awc->aw_parent->ad_parent;
return AE_OK;
}
static void
acpi_make_name(struct acpi_devnode *ad, uint32_t name)
{
ACPI_NAME_UNION *anu;
int clear, i;
anu = (ACPI_NAME_UNION *)&name;
ad->ad_name[4] = '\0';
for (i = 3, clear = 0; i >= 0; i--) {
if (clear == 0 && anu->Ascii[i] == '_')
ad->ad_name[i] = '\0';
else {
ad->ad_name[i] = anu->Ascii[i];
clear = 1;
}
}
if (ad->ad_name[0] == '\0')
ad->ad_name[0] = '_';
}
bus_dma_tag_t
acpi_default_dma_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
{
return sc->sc_dmat;
}
__weak_alias(acpi_get_dma_tag,acpi_default_dma_tag);
bus_dma_tag_t
acpi_default_dma64_tag(struct acpi_softc *sc, struct acpi_devnode *ad)
{
return sc->sc_dmat64;
}
__weak_alias(acpi_get_dma64_tag,acpi_default_dma64_tag);
pci_chipset_tag_t
acpi_default_pci_chipset_tag(struct acpi_softc *sc, int seg, int bbn)
{
return NULL;
}
__weak_alias(acpi_get_pci_chipset_tag,acpi_default_pci_chipset_tag);
/*
* Device attachment.
*/
static int
acpi_rescan(device_t self, const char *ifattr, const int *locators)
{
struct acpi_softc *sc = device_private(self);
struct acpi_attach_args aa;
/*
* Try to attach hpet(4) first via a specific table.
*/
aa.aa_memt = sc->sc_memt;
if (ifattr_match(ifattr, "acpihpetbus") && sc->sc_hpet == NULL) {
sc->sc_hpet = config_found(sc->sc_dev, &aa, NULL,
CFARG_IATTR, "acpihpetbus",
CFARG_EOL);
}
/*
* A two-pass scan for acpinodebus.
*/
if (ifattr_match(ifattr, "acpinodebus")) {
acpi_rescan_early(sc);
acpi_rescan_nodes(sc);
}
/*
* Attach APM emulation and acpiwdrt(4).
*/
if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL) {
sc->sc_apmbus = config_found(sc->sc_dev, NULL, NULL,
CFARG_IATTR, "acpiapmbus",
CFARG_EOL);
}
if (ifattr_match(ifattr, "acpiwdrtbus") && sc->sc_wdrt == NULL) {
sc->sc_wdrt = config_found(sc->sc_dev, NULL, NULL,
CFARG_IATTR, "acpiwdrtbus",
CFARG_EOL);
}
return 0;
}
static void
acpi_rescan_early(struct acpi_softc *sc)
{
struct acpi_attach_args aa;
struct acpi_devnode *ad;
/*
* First scan for devices such as acpiec(4) that
* should be always attached before anything else.
* We want these devices to attach regardless of
* the device status and other restrictions.
*/
SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
if (ad->ad_device != NULL)
continue;
if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
continue;
if (acpi_match_hid(ad->ad_devinfo, acpi_early_ids) == 0)
continue;
KASSERT(ad->ad_handle != NULL);
aa.aa_node = ad;
aa.aa_iot = sc->sc_iot;
aa.aa_memt = sc->sc_memt;
if (ad->ad_pciinfo != NULL) {
aa.aa_pc = ad->ad_pciinfo->ap_pc;
aa.aa_pciflags = sc->sc_pciflags;
}
aa.aa_ic = sc->sc_ic;
aa.aa_dmat = ad->ad_dmat;
aa.aa_dmat64 = ad->ad_dmat64;
ad->ad_device = config_found(sc->sc_dev, &aa, acpi_print,
CFARG_IATTR, "acpinodebus",
CFARG_DEVHANDLE, devhandle_from_acpi(ad->ad_handle),
CFARG_EOL);
}
}
static void
acpi_rescan_nodes(struct acpi_softc *sc)
{
const char * const hpet_ids[] = { "PNP0103", NULL };
struct acpi_attach_args aa;
struct acpi_devnode *ad;
ACPI_DEVICE_INFO *di;
SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
if (ad->ad_device != NULL)
continue;
/*
* There is a bug in ACPICA: it defines the type
* of the scopes incorrectly for its own reasons.
*/
if (acpi_is_scope(ad) != false)
continue;
di = ad->ad_devinfo;
/*
* We only attach devices which are present, enabled, and
* functioning properly. However, if a device is enabled,
* it is decoding resources and we should claim these,
* if possible. This requires changes to bus_space(9).