/
acpi.c
3563 lines (3037 loc) · 84.2 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
/* $OpenBSD: acpi.c,v 1.418 2022/09/13 17:14:54 kettenis Exp $ */
/*
* Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com>
* Copyright (c) 2005 Jordan Hargrave <jordan@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/pool.h>
#include <sys/fcntl.h>
#include <sys/event.h>
#include <sys/signalvar.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/sched.h>
#ifdef HIBERNATE
#include <sys/hibernate.h>
#endif
#include <machine/conf.h>
#include <machine/cpufunc.h>
#include <dev/pci/pcivar.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/amltypes.h>
#include <dev/acpi/acpidev.h>
#include <dev/acpi/dsdt.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/ppbreg.h>
#include <dev/pci/pciidevar.h>
#include <machine/apmvar.h>
#define APMUNIT(dev) (minor(dev)&0xf0)
#define APMDEV(dev) (minor(dev)&0x0f)
#define APMDEV_NORMAL 0
#define APMDEV_CTL 8
#include "wd.h"
#ifdef ACPI_DEBUG
int acpi_debug = 16;
#endif
int acpi_poll_enabled;
int acpi_hasprocfvs;
int acpi_haspci;
struct pool acpiwqpool;
#define ACPIEN_RETRIES 15
struct aml_node *acpi_pci_match(struct device *, struct pci_attach_args *);
pcireg_t acpi_pci_min_powerstate(pci_chipset_tag_t, pcitag_t);
void acpi_pci_set_powerstate(pci_chipset_tag_t, pcitag_t, int, int);
int acpi_pci_notify(struct aml_node *, int, void *);
int acpi_submatch(struct device *, void *, void *);
int acpi_print(void *, const char *);
void acpi_map_pmregs(struct acpi_softc *);
void acpi_unmap_pmregs(struct acpi_softc *);
int acpi_loadtables(struct acpi_softc *, struct acpi_rsdp *);
int _acpi_matchhids(const char *, const char *[]);
int acpi_inidev(struct aml_node *, void *);
int acpi_foundprt(struct aml_node *, void *);
int acpi_enable(struct acpi_softc *);
void acpi_init_states(struct acpi_softc *);
void acpi_gpe_task(void *, int);
void acpi_sbtn_task(void *, int);
void acpi_pbtn_task(void *, int);
int acpi_enabled;
void acpi_init_gpes(struct acpi_softc *);
void acpi_disable_allgpes(struct acpi_softc *);
struct gpe_block *acpi_find_gpe(struct acpi_softc *, int);
void acpi_enable_onegpe(struct acpi_softc *, int);
int acpi_gpe(struct acpi_softc *, int, void *);
void acpi_enable_rungpes(struct acpi_softc *);
int acpi_foundec(struct aml_node *, void *);
int acpi_foundsony(struct aml_node *node, void *arg);
int acpi_foundhid(struct aml_node *, void *);
int acpi_add_device(struct aml_node *node, void *arg);
void acpi_thread(void *);
void acpi_create_thread(void *);
#ifndef SMALL_KERNEL
void acpi_init_pm(struct acpi_softc *);
int acpi_founddock(struct aml_node *, void *);
int acpi_foundpss(struct aml_node *, void *);
int acpi_foundtmp(struct aml_node *, void *);
int acpi_foundprw(struct aml_node *, void *);
int acpi_foundvideo(struct aml_node *, void *);
int acpi_foundsbs(struct aml_node *node, void *);
int acpi_foundide(struct aml_node *node, void *arg);
int acpiide_notify(struct aml_node *, int, void *);
void wdcattach(struct channel_softc *);
int wdcdetach(struct channel_softc *, int);
int is_ejectable_bay(struct aml_node *node);
int is_ata(struct aml_node *node);
int is_ejectable(struct aml_node *node);
struct idechnl {
struct acpi_softc *sc;
int64_t addr;
int64_t chnl;
int64_t sta;
};
/*
* This is a list of Synaptics devices with a 'top button area'
* based on the list in Linux supplied by Synaptics
* Synaptics clickpads with the following pnp ids will get a unique
* wscons mouse type that is used to define trackpad regions that will
* emulate mouse buttons
*/
static const char *sbtn_pnp[] = {
"LEN0017",
"LEN0018",
"LEN0019",
"LEN0023",
"LEN002A",
"LEN002B",
"LEN002C",
"LEN002D",
"LEN002E",
"LEN0033",
"LEN0034",
"LEN0035",
"LEN0036",
"LEN0037",
"LEN0038",
"LEN0039",
"LEN0041",
"LEN0042",
"LEN0045",
"LEN0047",
"LEN0049",
"LEN2000",
"LEN2001",
"LEN2002",
"LEN2003",
"LEN2004",
"LEN2005",
"LEN2006",
"LEN2007",
"LEN2008",
"LEN2009",
"LEN200A",
"LEN200B",
};
int mouse_has_softbtn;
#endif /* SMALL_KERNEL */
struct acpi_softc *acpi_softc;
extern struct aml_node aml_root;
struct cfdriver acpi_cd = {
NULL, "acpi", DV_DULL
};
uint8_t
acpi_pci_conf_read_1(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
uint32_t val = pci_conf_read(pc, tag, reg & ~0x3);
return (val >> ((reg & 0x3) << 3));
}
uint16_t
acpi_pci_conf_read_2(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
uint32_t val = pci_conf_read(pc, tag, reg & ~0x2);
return (val >> ((reg & 0x2) << 3));
}
uint32_t
acpi_pci_conf_read_4(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
return pci_conf_read(pc, tag, reg);
}
void
acpi_pci_conf_write_1(pci_chipset_tag_t pc, pcitag_t tag, int reg, uint8_t val)
{
uint32_t tmp = pci_conf_read(pc, tag, reg & ~0x3);
tmp &= ~(0xff << ((reg & 0x3) << 3));
tmp |= (val << ((reg & 0x3) << 3));
pci_conf_write(pc, tag, reg & ~0x3, tmp);
}
void
acpi_pci_conf_write_2(pci_chipset_tag_t pc, pcitag_t tag, int reg, uint16_t val)
{
uint32_t tmp = pci_conf_read(pc, tag, reg & ~0x2);
tmp &= ~(0xffff << ((reg & 0x2) << 3));
tmp |= (val << ((reg & 0x2) << 3));
pci_conf_write(pc, tag, reg & ~0x2, tmp);
}
void
acpi_pci_conf_write_4(pci_chipset_tag_t pc, pcitag_t tag, int reg, uint32_t val)
{
pci_conf_write(pc, tag, reg, val);
}
int
acpi_gasio(struct acpi_softc *sc, int iodir, int iospace, uint64_t address,
int access_size, int len, void *buffer)
{
uint8_t *pb;
bus_space_tag_t iot;
bus_space_handle_t ioh;
pci_chipset_tag_t pc;
pcitag_t tag;
int reg, idx;
dnprintf(50, "gasio: %.2x 0x%.8llx %s\n",
iospace, address, (iodir == ACPI_IOWRITE) ? "write" : "read");
KASSERT((len % access_size) == 0);
pb = (uint8_t *)buffer;
switch (iospace) {
case GAS_SYSTEM_MEMORY:
case GAS_SYSTEM_IOSPACE:
if (iospace == GAS_SYSTEM_MEMORY)
iot = sc->sc_memt;
else
iot = sc->sc_iot;
if (acpi_bus_space_map(iot, address, len, 0, &ioh) != 0) {
printf("%s: unable to map iospace\n", DEVNAME(sc));
return (-1);
}
for (reg = 0; reg < len; reg += access_size) {
if (iodir == ACPI_IOREAD) {
switch (access_size) {
case 1:
*(uint8_t *)(pb + reg) =
bus_space_read_1(iot, ioh, reg);
dnprintf(80, "os_in8(%llx) = %x\n",
reg+address, *(uint8_t *)(pb+reg));
break;
case 2:
*(uint16_t *)(pb + reg) =
bus_space_read_2(iot, ioh, reg);
dnprintf(80, "os_in16(%llx) = %x\n",
reg+address, *(uint16_t *)(pb+reg));
break;
case 4:
*(uint32_t *)(pb + reg) =
bus_space_read_4(iot, ioh, reg);
break;
default:
printf("%s: rdio: invalid size %d\n",
DEVNAME(sc), access_size);
return (-1);
}
} else {
switch (access_size) {
case 1:
bus_space_write_1(iot, ioh, reg,
*(uint8_t *)(pb + reg));
dnprintf(80, "os_out8(%llx,%x)\n",
reg+address, *(uint8_t *)(pb+reg));
break;
case 2:
bus_space_write_2(iot, ioh, reg,
*(uint16_t *)(pb + reg));
dnprintf(80, "os_out16(%llx,%x)\n",
reg+address, *(uint16_t *)(pb+reg));
break;
case 4:
bus_space_write_4(iot, ioh, reg,
*(uint32_t *)(pb + reg));
break;
default:
printf("%s: wrio: invalid size %d\n",
DEVNAME(sc), access_size);
return (-1);
}
}
}
acpi_bus_space_unmap(iot, ioh, len);
break;
case GAS_PCI_CFG_SPACE:
/*
* The ACPI standard says that a function number of
* FFFF can be used to refer to all functions on a
* device. This makes no sense though in the context
* of accessing PCI config space. Yet there is AML
* out there that does this. We simulate a read from
* a nonexistent device here. Writes will panic when
* we try to construct the tag below.
*/
if (ACPI_PCI_FN(address) == 0xffff && iodir == ACPI_IOREAD) {
memset(buffer, 0xff, len);
return (0);
}
pc = pci_lookup_segment(ACPI_PCI_SEG(address));
tag = pci_make_tag(pc,
ACPI_PCI_BUS(address), ACPI_PCI_DEV(address),
ACPI_PCI_FN(address));
reg = ACPI_PCI_REG(address);
for (idx = 0; idx < len; idx += access_size) {
if (iodir == ACPI_IOREAD) {
switch (access_size) {
case 1:
*(uint8_t *)(pb + idx) =
acpi_pci_conf_read_1(pc, tag, reg + idx);
break;
case 2:
*(uint16_t *)(pb + idx) =
acpi_pci_conf_read_2(pc, tag, reg + idx);
break;
case 4:
*(uint32_t *)(pb + idx) =
acpi_pci_conf_read_4(pc, tag, reg + idx);
break;
default:
printf("%s: rdcfg: invalid size %d\n",
DEVNAME(sc), access_size);
return (-1);
}
} else {
switch (access_size) {
case 1:
acpi_pci_conf_write_1(pc, tag, reg + idx,
*(uint8_t *)(pb + idx));
break;
case 2:
acpi_pci_conf_write_2(pc, tag, reg + idx,
*(uint16_t *)(pb + idx));
break;
case 4:
acpi_pci_conf_write_4(pc, tag, reg + idx,
*(uint32_t *)(pb + idx));
break;
default:
printf("%s: wrcfg: invalid size %d\n",
DEVNAME(sc), access_size);
return (-1);
}
}
}
break;
case GAS_EMBEDDED:
if (sc->sc_ec == NULL) {
printf("%s: WARNING EC not initialized\n", DEVNAME(sc));
return (-1);
}
if (iodir == ACPI_IOREAD)
acpiec_read(sc->sc_ec, (uint8_t)address, len, buffer);
else
acpiec_write(sc->sc_ec, (uint8_t)address, len, buffer);
break;
}
return (0);
}
int
acpi_inidev(struct aml_node *node, void *arg)
{
struct acpi_softc *sc = (struct acpi_softc *)arg;
int64_t sta;
/*
* Per the ACPI spec 6.5.1, only run _INI when device is there or
* when there is no _STA. We terminate the tree walk (with return 1)
* early if necessary.
*/
/* Evaluate _STA to decide _INI fate and walk fate */
sta = acpi_getsta(sc, node->parent);
/* Evaluate _INI if we are present */
if (sta & STA_PRESENT)
aml_evalnode(sc, node, 0, NULL, NULL);
/* If we are functioning, we walk/search our children */
if (sta & STA_DEV_OK)
return 0;
/* If we are not enabled, or not present, terminate search */
if (!(sta & (STA_PRESENT|STA_ENABLED)))
return 1;
/* Default just continue search */
return 0;
}
int
acpi_foundprt(struct aml_node *node, void *arg)
{
struct acpi_softc *sc = (struct acpi_softc *)arg;
struct device *self = (struct device *)arg;
struct acpi_attach_args aaa;
int64_t sta;
dnprintf(10, "found prt entry: %s\n", node->parent->name);
/* Evaluate _STA to decide _PRT fate and walk fate */
sta = acpi_getsta(sc, node->parent);
if (sta & STA_PRESENT) {
memset(&aaa, 0, sizeof(aaa));
aaa.aaa_iot = sc->sc_iot;
aaa.aaa_memt = sc->sc_memt;
aaa.aaa_node = node;
aaa.aaa_name = "acpiprt";
config_found(self, &aaa, acpi_print);
}
/* If we are functioning, we walk/search our children */
if (sta & STA_DEV_OK)
return 0;
/* If we are not enabled, or not present, terminate search */
if (!(sta & (STA_PRESENT|STA_ENABLED)))
return 1;
/* Default just continue search */
return 0;
}
TAILQ_HEAD(, acpi_pci) acpi_pcidevs =
TAILQ_HEAD_INITIALIZER(acpi_pcidevs);
TAILQ_HEAD(, acpi_pci) acpi_pcirootdevs =
TAILQ_HEAD_INITIALIZER(acpi_pcirootdevs);
int acpi_getpci(struct aml_node *node, void *arg);
int acpi_getminbus(int crsidx, union acpi_resource *crs, void *arg);
int
acpi_getminbus(int crsidx, union acpi_resource *crs, void *arg)
{
int *bbn = arg;
int typ = AML_CRSTYPE(crs);
/* Check for embedded bus number */
if (typ == LR_WORD && crs->lr_word.type == 2) {
/* If _MIN > _MAX, the resource is considered to be invalid. */
if (crs->lr_word._min > crs->lr_word._max)
return -1;
*bbn = crs->lr_word._min;
}
return 0;
}
int
acpi_matchcls(struct acpi_attach_args *aaa, int class, int subclass,
int interface)
{
struct acpi_softc *sc = acpi_softc;
struct aml_value res;
if (aaa->aaa_dev == NULL || aaa->aaa_node == NULL)
return (0);
if (aml_evalname(sc, aaa->aaa_node, "_CLS", 0, NULL, &res))
return (0);
if (res.type != AML_OBJTYPE_PACKAGE || res.length != 3 ||
res.v_package[0]->type != AML_OBJTYPE_INTEGER ||
res.v_package[1]->type != AML_OBJTYPE_INTEGER ||
res.v_package[2]->type != AML_OBJTYPE_INTEGER)
return (0);
if (res.v_package[0]->v_integer == class &&
res.v_package[1]->v_integer == subclass &&
res.v_package[2]->v_integer == interface)
return (1);
return (0);
}
int
_acpi_matchhids(const char *hid, const char *hids[])
{
int i;
for (i = 0; hids[i]; i++)
if (!strcmp(hid, hids[i]))
return (1);
return (0);
}
int
acpi_matchhids(struct acpi_attach_args *aa, const char *hids[],
const char *driver)
{
if (aa->aaa_dev == NULL || aa->aaa_node == NULL)
return (0);
if (_acpi_matchhids(aa->aaa_dev, hids)) {
dnprintf(5, "driver %s matches at least one hid\n", driver);
return (2);
}
if (aa->aaa_cdev && _acpi_matchhids(aa->aaa_cdev, hids)) {
dnprintf(5, "driver %s matches at least one cid\n", driver);
return (1);
}
return (0);
}
int64_t
acpi_getsta(struct acpi_softc *sc, struct aml_node *node)
{
int64_t sta;
if (aml_evalinteger(sc, node, "_STA", 0, NULL, &sta))
sta = STA_PRESENT | STA_ENABLED | STA_SHOW_UI |
STA_DEV_OK | STA_BATTERY;
return sta;
}
/* Map ACPI device node to PCI */
int
acpi_getpci(struct aml_node *node, void *arg)
{
const char *pcihid[] = { ACPI_DEV_PCIB, ACPI_DEV_PCIEB, "HWP0002", 0 };
struct acpi_pci *pci, *ppci;
struct aml_value res;
struct acpi_softc *sc = arg;
pci_chipset_tag_t pc;
pcitag_t tag;
uint64_t val;
int64_t sta;
uint32_t reg;
sta = acpi_getsta(sc, node);
if ((sta & STA_PRESENT) == 0)
return 0;
if (!node->value || node->value->type != AML_OBJTYPE_DEVICE)
return 0;
if (!aml_evalhid(node, &res)) {
/* Check if this is a PCI Root node */
if (_acpi_matchhids(res.v_string, pcihid)) {
aml_freevalue(&res);
pci = malloc(sizeof(*pci), M_DEVBUF, M_WAITOK|M_ZERO);
pci->bus = -1;
if (!aml_evalinteger(sc, node, "_SEG", 0, NULL, &val))
pci->seg = val;
if (!aml_evalname(sc, node, "_CRS", 0, NULL, &res)) {
aml_parse_resource(&res, acpi_getminbus,
&pci->bus);
dnprintf(10, "%s post-crs: %d\n",
aml_nodename(node), pci->bus);
}
if (!aml_evalinteger(sc, node, "_BBN", 0, NULL, &val)) {
dnprintf(10, "%s post-bbn: %d, %lld\n",
aml_nodename(node), pci->bus, val);
if (pci->bus == -1)
pci->bus = val;
}
pci->sub = pci->bus;
node->pci = pci;
dnprintf(10, "found PCI root: %s %d\n",
aml_nodename(node), pci->bus);
TAILQ_INSERT_TAIL(&acpi_pcirootdevs, pci, next);
}
aml_freevalue(&res);
return 0;
}
/* If parent is not PCI, or device does not have _ADR, return */
if (!node->parent || (ppci = node->parent->pci) == NULL)
return 0;
if (aml_evalinteger(sc, node, "_ADR", 0, NULL, &val))
return 0;
pci = malloc(sizeof(*pci), M_DEVBUF, M_WAITOK|M_ZERO);
pci->seg = ppci->seg;
pci->bus = ppci->sub;
pci->dev = ACPI_ADR_PCIDEV(val);
pci->fun = ACPI_ADR_PCIFUN(val);
pci->node = node;
pci->sub = -1;
dnprintf(10, "%.2x:%.2x.%x -> %s\n",
pci->bus, pci->dev, pci->fun,
aml_nodename(node));
/* Collect device power state information. */
if (aml_evalinteger(sc, node, "_S3D", 0, NULL, &val) == 0)
pci->_s3d = val;
else
pci->_s3d = -1;
if (aml_evalinteger(sc, node, "_S3W", 0, NULL, &val) == 0)
pci->_s3w = val;
else
pci->_s3w = -1;
if (aml_evalinteger(sc, node, "_S4D", 0, NULL, &val) == 0)
pci->_s4d = val;
else
pci->_s4d = -1;
if (aml_evalinteger(sc, node, "_S4W", 0, NULL, &val) == 0)
pci->_s4w = val;
else
pci->_s4w = -1;
/* Check if PCI device exists */
if (pci->dev > 0x1F || pci->fun > 7) {
free(pci, M_DEVBUF, sizeof(*pci));
return (1);
}
pc = pci_lookup_segment(pci->seg);
tag = pci_make_tag(pc, pci->bus, pci->dev, pci->fun);
reg = pci_conf_read(pc, tag, PCI_ID_REG);
if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID) {
free(pci, M_DEVBUF, sizeof(*pci));
return (1);
}
node->pci = pci;
TAILQ_INSERT_TAIL(&acpi_pcidevs, pci, next);
/* Check if this is a PCI bridge */
reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI) {
reg = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
pci->sub = PPB_BUSINFO_SECONDARY(reg);
dnprintf(10, "found PCI bridge: %s %d\n",
aml_nodename(node), pci->sub);
/* Continue scanning */
return (0);
}
/* Device does not have children, stop scanning */
return (1);
}
struct aml_node *
acpi_find_pci(pci_chipset_tag_t pc, pcitag_t tag)
{
struct acpi_pci *pdev;
int bus, dev, fun;
pci_decompose_tag(pc, tag, &bus, &dev, &fun);
TAILQ_FOREACH(pdev, &acpi_pcidevs, next) {
if (pdev->bus == bus && pdev->dev == dev && pdev->fun == fun)
return pdev->node;
}
return NULL;
}
struct aml_node *
acpi_pci_match(struct device *dev, struct pci_attach_args *pa)
{
struct acpi_pci *pdev;
int state;
TAILQ_FOREACH(pdev, &acpi_pcidevs, next) {
if (pdev->bus != pa->pa_bus ||
pdev->dev != pa->pa_device ||
pdev->fun != pa->pa_function)
continue;
dnprintf(10,"%s at acpi0 %s\n", dev->dv_xname,
aml_nodename(pdev->node));
pdev->device = dev;
/*
* If some Power Resources are dependent on this device
* initialize them.
*/
state = pci_get_powerstate(pa->pa_pc, pa->pa_tag);
acpi_pci_set_powerstate(pa->pa_pc, pa->pa_tag, state, 1);
acpi_pci_set_powerstate(pa->pa_pc, pa->pa_tag, state, 0);
aml_register_notify(pdev->node, NULL, acpi_pci_notify, pdev, 0);
return pdev->node;
}
return NULL;
}
pcireg_t
acpi_pci_min_powerstate(pci_chipset_tag_t pc, pcitag_t tag)
{
struct acpi_pci *pdev;
int bus, dev, fun;
int state = -1, defaultstate = pci_get_powerstate(pc, tag);
pci_decompose_tag(pc, tag, &bus, &dev, &fun);
TAILQ_FOREACH(pdev, &acpi_pcidevs, next) {
if (pdev->bus == bus && pdev->dev == dev && pdev->fun == fun) {
switch (acpi_softc->sc_state) {
case ACPI_STATE_S3:
defaultstate = PCI_PMCSR_STATE_D3;
state = MAX(pdev->_s3d, pdev->_s3w);
break;
case ACPI_STATE_S4:
state = MAX(pdev->_s4d, pdev->_s4w);
break;
case ACPI_STATE_S5:
default:
break;
}
if (state >= PCI_PMCSR_STATE_D0 &&
state <= PCI_PMCSR_STATE_D3)
return state;
}
}
return defaultstate;
}
void
acpi_pci_set_powerstate(pci_chipset_tag_t pc, pcitag_t tag, int state, int pre)
{
#if NACPIPWRRES > 0
struct acpi_softc *sc = acpi_softc;
struct acpi_pwrres *pr;
struct acpi_pci *pdev;
int bus, dev, fun;
char name[5];
pci_decompose_tag(pc, tag, &bus, &dev, &fun);
TAILQ_FOREACH(pdev, &acpi_pcidevs, next) {
if (pdev->bus == bus && pdev->dev == dev && pdev->fun == fun)
break;
}
/* XXX Add a check to discard nodes without Power Resources? */
if (pdev == NULL)
return;
SIMPLEQ_FOREACH(pr, &sc->sc_pwrresdevs, p_next) {
if (pr->p_node != pdev->node)
continue;
/*
* If the firmware is already aware that the device
* is in the given state, there's nothing to do.
*/
if (pr->p_state == state)
continue;
if (pre) {
/*
* If a Resource is dependent on this device for
* the given state, make sure it is turned "_ON".
*/
if (pr->p_res_state == state)
acpipwrres_ref_incr(pr->p_res_sc, pr->p_node);
} else {
/*
* If a Resource was referenced for the state we
* left, drop a reference and turn it "_OFF" if
* it was the last one.
*/
if (pr->p_res_state == pr->p_state)
acpipwrres_ref_decr(pr->p_res_sc, pr->p_node);
if (pr->p_res_state == state) {
snprintf(name, sizeof(name), "_PS%d", state);
aml_evalname(sc, pr->p_node, name, 0,
NULL, NULL);
}
pr->p_state = state;
}
}
#endif /* NACPIPWRRES > 0 */
}
int
acpi_pci_notify(struct aml_node *node, int ntype, void *arg)
{
struct acpi_pci *pdev = arg;
pci_chipset_tag_t pc;
pcitag_t tag;
pcireg_t reg;
int offset;
/* We're only interested in Device Wake notifications. */
if (ntype != 2)
return (0);
pc = pci_lookup_segment(pdev->seg);
tag = pci_make_tag(pc, pdev->bus, pdev->dev, pdev->fun);
if (pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, 0)) {
/* Clear the PME Status bit if it is set. */
reg = pci_conf_read(pc, tag, offset + PCI_PMCSR);
pci_conf_write(pc, tag, offset + PCI_PMCSR, reg);
}
return (0);
}
void
acpi_pciroots_attach(struct device *dev, void *aux, cfprint_t pr)
{
struct acpi_pci *pdev;
struct pcibus_attach_args *pba = aux;
KASSERT(pba->pba_busex != NULL);
TAILQ_FOREACH(pdev, &acpi_pcirootdevs, next) {
if (extent_alloc_region(pba->pba_busex, pdev->bus,
1, EX_NOWAIT) != 0)
continue;
pba->pba_bus = pdev->bus;
config_found(dev, pba, pr);
}
}
/* GPIO support */
struct acpi_gpio_event {
struct aml_node *node;
uint16_t tflags;
uint16_t pin;
};
void
acpi_gpio_event_task(void *arg0, int arg1)
{
struct acpi_softc *sc = acpi_softc;
struct acpi_gpio_event *ev = arg0;
struct acpi_gpio *gpio = ev->node->gpio;
struct aml_value evt;
uint16_t pin = arg1;
char name[5];
if (pin < 256) {
if ((ev->tflags & LR_GPIO_MODE) == LR_GPIO_LEVEL) {
snprintf(name, sizeof(name), "_L%.2X", pin);
if (aml_evalname(sc, ev->node, name, 0, NULL, NULL)) {
if (gpio->intr_enable)
gpio->intr_enable(gpio->cookie, pin);
return;
}
} else {
snprintf(name, sizeof(name), "_E%.2X", pin);
if (aml_evalname(sc, ev->node, name, 0, NULL, NULL)) {
if (gpio->intr_enable)
gpio->intr_enable(gpio->cookie, pin);
return;
}
}
}
memset(&evt, 0, sizeof(evt));
evt.v_integer = pin;
evt.type = AML_OBJTYPE_INTEGER;
aml_evalname(sc, ev->node, "_EVT", 1, &evt, NULL);
if ((ev->tflags & LR_GPIO_MODE) == LR_GPIO_LEVEL) {
if (gpio->intr_enable)
gpio->intr_enable(gpio->cookie, pin);
}
}
int
acpi_gpio_event(void *arg)
{
struct acpi_gpio_event *ev = arg;
struct acpi_gpio *gpio = ev->node->gpio;
if ((ev->tflags & LR_GPIO_MODE) == LR_GPIO_LEVEL) {
if(gpio->intr_disable)
gpio->intr_disable(gpio->cookie, ev->pin);
}
acpi_addtask(acpi_softc, acpi_gpio_event_task, ev, ev->pin);
acpi_wakeup(acpi_softc);
return 1;
}
int
acpi_gpio_parse_events(int crsidx, union acpi_resource *crs, void *arg)
{
struct aml_node *devnode = arg;
struct aml_node *node;
uint16_t pin;
switch (AML_CRSTYPE(crs)) {
case LR_GPIO:
node = aml_searchname(devnode,
(char *)&crs->pad[crs->lr_gpio.res_off]);
pin = *(uint16_t *)&crs->pad[crs->lr_gpio.pin_off];
if (crs->lr_gpio.type == LR_GPIO_INT &&
node && node->gpio && node->gpio->intr_establish) {
struct acpi_gpio *gpio = node->gpio;
struct acpi_gpio_event *ev;
ev = malloc(sizeof(*ev), M_DEVBUF, M_WAITOK);
ev->node = devnode;
ev->tflags = crs->lr_gpio.tflags;
ev->pin = pin;
gpio->intr_establish(gpio->cookie, pin,
crs->lr_gpio.tflags, acpi_gpio_event, ev);
}
break;
default:
printf("%s: unknown resource type %d\n", __func__,
AML_CRSTYPE(crs));
}
return 0;
}
void
acpi_register_gpio(struct acpi_softc *sc, struct aml_node *devnode)
{
struct aml_value arg[2];
struct aml_node *node;
struct aml_value res;
/* Register GeneralPurposeIO address space. */
memset(&arg, 0, sizeof(arg));
arg[0].type = AML_OBJTYPE_INTEGER;
arg[0].v_integer = ACPI_OPREG_GPIO;
arg[1].type = AML_OBJTYPE_INTEGER;
arg[1].v_integer = 1;
node = aml_searchname(devnode, "_REG");
if (node && aml_evalnode(sc, node, 2, arg, NULL))
printf("%s: _REG failed\n", node->name);
/* Register GPIO signaled ACPI events. */
if (aml_evalname(sc, devnode, "_AEI", 0, NULL, &res))
return;
aml_parse_resource(&res, acpi_gpio_parse_events, devnode);
}
#ifndef SMALL_KERNEL
void
acpi_register_gsb(struct acpi_softc *sc, struct aml_node *devnode)
{
struct aml_value arg[2];
struct aml_node *node;
/* Register GenericSerialBus address space. */
memset(&arg, 0, sizeof(arg));
arg[0].type = AML_OBJTYPE_INTEGER;
arg[0].v_integer = ACPI_OPREG_GSB;
arg[1].type = AML_OBJTYPE_INTEGER;
arg[1].v_integer = 1;
node = aml_searchname(devnode, "_REG");
if (node && aml_evalnode(sc, node, 2, arg, NULL))
printf("%s: _REG failed\n", node->name);
}
#endif
void
acpi_attach_common(struct acpi_softc *sc, paddr_t base)
{
struct acpi_mem_map handle;
struct acpi_rsdp *rsdp;
struct acpi_q *entry;
struct acpi_dsdt *p_dsdt;