madt: File relocate
[dragonfly.git] / sys / platform / pc64 / acpica5 / acpi_madt.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/param.h>
36#include <sys/bus.h>
37#include <sys/kernel.h>
38#include <sys/systm.h>
39
40#include <machine/pmap.h>
41#include <machine/smp.h>
42#include <machine/md_var.h>
43#include <machine/specialreg.h>
44#include <machine_base/apic/mpapic.h>
45
46#define MADT_VPRINTF(fmt, arg...) \
47do { \
48 if (bootverbose) \
49 kprintf("ACPI MADT: " fmt , ##arg); \
50} while (0)
51
52#define ACPI_RSDP_EBDA_MAPSZ 1024
53#define ACPI_RSDP_BIOS_MAPSZ 0x20000
54#define ACPI_RSDP_BIOS_MAPADDR 0xe0000
55
56#define ACPI_RSDP_ALIGN 16
57
58#define ACPI_RSDP_SIGLEN 8
59#define ACPI_RSDP_SIG "RSD PTR "
60
61#define ACPI_SDTH_SIGLEN 4
62#define ACPI_RSDT_SIG "RSDT"
63#define ACPI_XSDT_SIG "XSDT"
64#define ACPI_MADT_SIG "APIC"
65
66/* Root System Description Pointer */
67struct acpi_rsdp {
68 uint8_t rsdp_sig[ACPI_RSDP_SIGLEN];
69 uint8_t rsdp_cksum;
70 uint8_t rsdp_oem_id[6];
71 uint8_t rsdp_rev;
72 uint32_t rsdp_rsdt;
73 uint32_t rsdp_len;
74 uint64_t rsdp_xsdt;
75 uint8_t rsdp_ext_cksum;
76 uint8_t rsdp_rsvd[3];
77} __packed;
78
79/* System Description Table Header */
80struct acpi_sdth {
81 uint8_t sdth_sig[ACPI_SDTH_SIGLEN];
82 uint32_t sdth_len;
83 uint8_t sdth_rev;
84 uint8_t sdth_cksum;
85 uint8_t sdth_oem_id[6];
86 uint8_t sdth_oem_tbid[8];
87 uint32_t sdth_oem_rev;
88 uint32_t sdth_crt_id;
89 uint32_t sdth_crt_rev;
90} __packed;
91
92/* Extended System Description Table */
93struct acpi_xsdt {
94 struct acpi_sdth xsdt_hdr;
95 uint64_t xsdt_ents[1];
96} __packed;
97
98/* Root System Description Table */
99struct acpi_rsdt {
100 struct acpi_sdth rsdt_hdr;
101 uint32_t rsdt_ents[1];
102} __packed;
103
104/* Multiple APIC Description Table */
105struct acpi_madt {
106 struct acpi_sdth madt_hdr;
107 uint32_t madt_lapic_addr;
108 uint32_t madt_flags;
109 uint8_t madt_ents[1];
110} __packed;
111
112/* Common parts of MADT APIC structure */
113struct acpi_madt_ent {
114 uint8_t me_type; /* MADT_ENT_ */
115 uint8_t me_len;
116} __packed;
117
118#define MADT_ENT_LAPIC 0
119#define MADT_ENT_IOAPIC 1
120#define MADT_ENT_INTSRC 2
121#define MADT_ENT_LAPIC_ADDR 5
122
123/* MADT Processor Local APIC */
124struct acpi_madt_lapic {
125 struct acpi_madt_ent ml_hdr;
126 uint8_t ml_cpu_id;
127 uint8_t ml_apic_id;
128 uint32_t ml_flags; /* MADT_LAPIC_ */
129} __packed;
130
131#define MADT_LAPIC_ENABLED 0x1
132
133/* MADT I/O APIC */
134struct acpi_madt_ioapic {
135 struct acpi_madt_ent mio_hdr;
136 uint8_t mio_apic_id;
137 uint8_t mio_reserved;
138 uint32_t mio_addr;
139 uint32_t mio_gsi_base;
140} __packed;
141
142/* MADT Interrupt Source Override */
143struct acpi_madt_intsrc {
144 struct acpi_madt_ent mint_hdr;
145 uint8_t mint_bus; /* MADT_INT_BUS_ */
146 uint8_t mint_src;
147 uint32_t mint_gsi;
148 uint16_t mint_flags; /* MADT_INT_ */
149} __packed;
150
151#define MADT_INT_BUS_ISA 0
152
153#define MADT_INT_POLA_MASK 0x3
154#define MADT_INT_POLA_SHIFT 0
155#define MADT_INT_POLA_CONFORM 0
156#define MADT_INT_POLA_HIGH 1
157#define MADT_INT_POLA_RSVD 2
158#define MADT_INT_POLA_LOW 3
159#define MADT_INT_TRIG_MASK 0xc
160#define MADT_INT_TRIG_SHIFT 2
161#define MADT_INT_TRIG_CONFORM 0
162#define MADT_INT_TRIG_EDGE 1
163#define MADT_INT_TRIG_RSVD 2
164#define MADT_INT_TRIG_LEVEL 3
165
166/* MADT Local APIC Address Override */
167struct acpi_madt_lapic_addr {
168 struct acpi_madt_ent mla_hdr;
169 uint16_t mla_reserved;
170 uint64_t mla_lapic_addr;
171} __packed;
172
173typedef vm_paddr_t (*madt_search_t)(vm_paddr_t);
174typedef int (*madt_iter_t)(void *,
175 const struct acpi_madt_ent *);
176
177static const struct acpi_rsdp *madt_rsdp_search(const uint8_t *, int);
178static void *madt_sdth_map(vm_paddr_t);
179static void madt_sdth_unmap(struct acpi_sdth *);
180static vm_paddr_t madt_search_xsdt(vm_paddr_t);
181static vm_paddr_t madt_search_rsdt(vm_paddr_t);
182static int madt_check(vm_paddr_t);
183static int madt_iterate_entries(struct acpi_madt *,
184 madt_iter_t, void *);
185
186static vm_offset_t madt_lapic_pass1(void);
187static int madt_lapic_pass2(int);
188
189static void madt_lapic_enumerate(struct lapic_enumerator *);
190static int madt_lapic_probe(struct lapic_enumerator *);
191
192static void madt_ioapic_enumerate(
193 struct ioapic_enumerator *);
194static int madt_ioapic_probe(struct ioapic_enumerator *);
195
196extern u_long ebda_addr;
197
198static vm_paddr_t madt_phyaddr;
199
200static void
201madt_probe(void)
202{
203 const struct acpi_rsdp *rsdp;
204 madt_search_t search;
205 vm_paddr_t search_paddr, madt_paddr;
206 vm_size_t mapsz;
207 uint8_t *ptr;
208
209 KKASSERT(madt_phyaddr == 0);
210
211 if (ebda_addr != 0) {
212 mapsz = ACPI_RSDP_EBDA_MAPSZ;
213 ptr = pmap_mapdev(ebda_addr, mapsz);
214
215 rsdp = madt_rsdp_search(ptr, mapsz);
216 if (rsdp == NULL) {
217 MADT_VPRINTF("RSDP not in EBDA\n");
218 pmap_unmapdev((vm_offset_t)ptr, mapsz);
219
220 ptr = NULL;
221 mapsz = 0;
222 } else {
223 MADT_VPRINTF("RSDP in EBDA\n");
224 goto found_rsdp;
225 }
226 }
227
228 mapsz = ACPI_RSDP_BIOS_MAPSZ;
229 ptr = pmap_mapdev(ACPI_RSDP_BIOS_MAPADDR, mapsz);
230
231 rsdp = madt_rsdp_search(ptr, mapsz);
232 if (rsdp == NULL) {
233 kprintf("madt_probe: no RSDP\n");
234 pmap_unmapdev((vm_offset_t)ptr, mapsz);
235 return;
236 } else {
237 MADT_VPRINTF("RSDP in BIOS mem\n");
238 }
239
240found_rsdp:
241 if (rsdp->rsdp_rev != 2) {
242 search_paddr = rsdp->rsdp_rsdt;
243 search = madt_search_rsdt;
244 } else {
245 search_paddr = rsdp->rsdp_xsdt;
246 search = madt_search_xsdt;
247 }
248 pmap_unmapdev((vm_offset_t)ptr, mapsz);
249
250 madt_paddr = search(search_paddr);
251 if (madt_paddr == 0) {
252 kprintf("madt_probe: can't locate MADT\n");
253 return;
254 }
255
256 /* Preliminary checks */
257 if (madt_check(madt_paddr)) {
258 kprintf("madt_probe: madt_check failed\n");
259 return;
260 }
261
262 madt_phyaddr = madt_paddr;
263}
264SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_FIRST, madt_probe, 0);
265
266static const struct acpi_rsdp *
267madt_rsdp_search(const uint8_t *target, int size)
268{
269 const struct acpi_rsdp *rsdp;
270 int i;
271
272 KKASSERT(size > sizeof(*rsdp));
273
274 for (i = 0; i < size - sizeof(*rsdp); i += ACPI_RSDP_ALIGN) {
275 rsdp = (const struct acpi_rsdp *)&target[i];
276 if (memcmp(rsdp->rsdp_sig, ACPI_RSDP_SIG,
277 ACPI_RSDP_SIGLEN) == 0)
278 return rsdp;
279 }
280 return NULL;
281}
282
283static void *
284madt_sdth_map(vm_paddr_t paddr)
285{
286 struct acpi_sdth *sdth;
287 vm_size_t mapsz;
288
289 sdth = pmap_mapdev(paddr, sizeof(*sdth));
290 mapsz = sdth->sdth_len;
291 pmap_unmapdev((vm_offset_t)sdth, sizeof(*sdth));
292
293 if (mapsz < sizeof(*sdth))
294 return NULL;
295
296 return pmap_mapdev(paddr, mapsz);
297}
298
299static void
300madt_sdth_unmap(struct acpi_sdth *sdth)
301{
302 pmap_unmapdev((vm_offset_t)sdth, sdth->sdth_len);
303}
304
305static vm_paddr_t
306madt_search_xsdt(vm_paddr_t xsdt_paddr)
307{
308 struct acpi_xsdt *xsdt;
309 vm_paddr_t madt_paddr = 0;
310 int i, nent;
311
312 if (xsdt_paddr == 0) {
313 kprintf("madt_search_xsdt: XSDT paddr == 0\n");
314 return 0;
315 }
316
317 xsdt = madt_sdth_map(xsdt_paddr);
318 if (xsdt == NULL) {
319 kprintf("madt_search_xsdt: can't map XSDT\n");
320 return 0;
321 }
322
323 if (memcmp(xsdt->xsdt_hdr.sdth_sig, ACPI_XSDT_SIG,
324 ACPI_SDTH_SIGLEN) != 0) {
325 kprintf("madt_search_xsdt: not XSDT\n");
326 goto back;
327 }
328
329 if (xsdt->xsdt_hdr.sdth_rev != 1) {
330 kprintf("madt_search_xsdt: unsupported XSDT revision %d\n",
331 xsdt->xsdt_hdr.sdth_rev);
332 goto back;
333 }
334
335 nent = (xsdt->xsdt_hdr.sdth_len - sizeof(xsdt->xsdt_hdr)) /
336 sizeof(xsdt->xsdt_ents[0]);
337 for (i = 0; i < nent; ++i) {
338 struct acpi_sdth *sdth;
339
340 if (xsdt->xsdt_ents[i] == 0)
341 continue;
342
343 sdth = madt_sdth_map(xsdt->xsdt_ents[i]);
344 if (sdth != NULL) {
345 int ret;
346
347 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
348 ACPI_SDTH_SIGLEN);
349 madt_sdth_unmap(sdth);
350
351 if (ret == 0) {
352 MADT_VPRINTF("MADT in XSDT\n");
353 madt_paddr = xsdt->xsdt_ents[i];
354 break;
355 }
356 }
357 }
358back:
359 madt_sdth_unmap(&xsdt->xsdt_hdr);
360 return madt_paddr;
361}
362
363static vm_paddr_t
364madt_search_rsdt(vm_paddr_t rsdt_paddr)
365{
366 struct acpi_rsdt *rsdt;
367 vm_paddr_t madt_paddr = 0;
368 int i, nent;
369
370 if (rsdt_paddr == 0) {
371 kprintf("madt_search_rsdt: RSDT paddr == 0\n");
372 return 0;
373 }
374
375 rsdt = madt_sdth_map(rsdt_paddr);
376 if (rsdt == NULL) {
377 kprintf("madt_search_rsdt: can't map RSDT\n");
378 return 0;
379 }
380
381 if (memcmp(rsdt->rsdt_hdr.sdth_sig, ACPI_RSDT_SIG,
382 ACPI_SDTH_SIGLEN) != 0) {
383 kprintf("madt_search_rsdt: not RSDT\n");
384 goto back;
385 }
386
387 if (rsdt->rsdt_hdr.sdth_rev != 1) {
388 kprintf("madt_search_rsdt: unsupported RSDT revision %d\n",
389 rsdt->rsdt_hdr.sdth_rev);
390 goto back;
391 }
392
393 nent = (rsdt->rsdt_hdr.sdth_len - sizeof(rsdt->rsdt_hdr)) /
394 sizeof(rsdt->rsdt_ents[0]);
395 for (i = 0; i < nent; ++i) {
396 struct acpi_sdth *sdth;
397
398 if (rsdt->rsdt_ents[i] == 0)
399 continue;
400
401 sdth = madt_sdth_map(rsdt->rsdt_ents[i]);
402 if (sdth != NULL) {
403 int ret;
404
405 ret = memcmp(sdth->sdth_sig, ACPI_MADT_SIG,
406 ACPI_SDTH_SIGLEN);
407 madt_sdth_unmap(sdth);
408
409 if (ret == 0) {
410 MADT_VPRINTF("MADT in RSDT\n");
411 madt_paddr = rsdt->rsdt_ents[i];
412 break;
413 }
414 }
415 }
416back:
417 madt_sdth_unmap(&rsdt->rsdt_hdr);
418 return madt_paddr;
419}
420
421static int
422madt_check(vm_paddr_t madt_paddr)
423{
424 struct acpi_madt *madt;
425 int error = 0;
426
427 KKASSERT(madt_paddr != 0);
428
429 madt = madt_sdth_map(madt_paddr);
430 KKASSERT(madt != NULL);
431
432 /*
433 * MADT in ACPI specification 1.0 - 4.0
434 */
435 if (madt->madt_hdr.sdth_rev < 1 || madt->madt_hdr.sdth_rev > 3) {
436 kprintf("madt_check: unsupported MADT revision %d\n",
437 madt->madt_hdr.sdth_rev);
438 error = EOPNOTSUPP;
439 goto back;
440 }
441
442 if (madt->madt_hdr.sdth_len <
443 sizeof(*madt) - sizeof(madt->madt_ents)) {
444 kprintf("madt_check: invalid MADT length %u\n",
445 madt->madt_hdr.sdth_len);
446 error = EINVAL;
447 goto back;
448 }
449back:
450 madt_sdth_unmap(&madt->madt_hdr);
451 return error;
452}
453
454static int
455madt_iterate_entries(struct acpi_madt *madt, madt_iter_t func, void *arg)
456{
457 int size, cur, error;
458
459 size = madt->madt_hdr.sdth_len -
460 (sizeof(*madt) - sizeof(madt->madt_ents));
461 cur = 0;
462 error = 0;
463
464 while (size - cur > sizeof(struct acpi_madt_ent)) {
465 const struct acpi_madt_ent *ent;
466
467 ent = (const struct acpi_madt_ent *)&madt->madt_ents[cur];
468 if (ent->me_len < sizeof(*ent)) {
469 kprintf("madt_iterate_entries: invalid MADT "
470 "entry len %d\n", ent->me_len);
471 error = EINVAL;
472 break;
473 }
474 if (ent->me_len > (size - cur)) {
475 kprintf("madt_iterate_entries: invalid MADT "
476 "entry len %d, > table length\n", ent->me_len);
477 error = EINVAL;
478 break;
479 }
480
481 cur += ent->me_len;
482
483 /*
484 * Only Local APIC, I/O APIC and Interrupt Source Override
485 * are defined in ACPI specification 1.0 - 4.0
486 */
487 switch (ent->me_type) {
488 case MADT_ENT_LAPIC:
489 if (ent->me_len < sizeof(struct acpi_madt_lapic)) {
490 kprintf("madt_iterate_entries: invalid MADT "
491 "lapic entry len %d\n", ent->me_len);
492 error = EINVAL;
493 }
494 break;
495
496 case MADT_ENT_IOAPIC:
497 if (ent->me_len < sizeof(struct acpi_madt_ioapic)) {
498 kprintf("madt_iterate_entries: invalid MADT "
499 "ioapic entry len %d\n", ent->me_len);
500 error = EINVAL;
501 }
502 break;
503
504 case MADT_ENT_INTSRC:
505 if (ent->me_len < sizeof(struct acpi_madt_intsrc)) {
506 kprintf("madt_iterate_entries: invalid MADT "
507 "intsrc entry len %d\n",
508 ent->me_len);
509 error = EINVAL;
510 }
511 break;
512 }
513 if (error)
514 break;
515
516 error = func(arg, ent);
517 if (error)
518 break;
519 }
520 return error;
521}
522
523static int
524madt_lapic_pass1_callback(void *xarg, const struct acpi_madt_ent *ent)
525{
526 const struct acpi_madt_lapic_addr *lapic_addr_ent;
527 uint64_t *addr64 = xarg;
528
529 if (ent->me_type != MADT_ENT_LAPIC_ADDR)
530 return 0;
531 if (ent->me_len < sizeof(*lapic_addr_ent)) {
532 kprintf("madt_lapic_pass1: "
533 "invalid LAPIC address override length\n");
534 return 0;
535 }
536 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
537
538 *addr64 = lapic_addr_ent->mla_lapic_addr;
539 return 0;
540}
541
542static vm_offset_t
543madt_lapic_pass1(void)
544{
545 struct acpi_madt *madt;
546 vm_offset_t lapic_addr;
547 uint64_t lapic_addr64;
548 int error;
549
550 KKASSERT(madt_phyaddr != 0);
551
552 madt = madt_sdth_map(madt_phyaddr);
553 KKASSERT(madt != NULL);
554
555 MADT_VPRINTF("LAPIC address 0x%08x, flags %#x\n",
556 madt->madt_lapic_addr, madt->madt_flags);
557 lapic_addr = madt->madt_lapic_addr;
558
559 lapic_addr64 = 0;
560 error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
561 &lapic_addr64);
562 if (error)
563 panic("madt_iterate_entries(pass1) failed\n");
564
565 if (lapic_addr64 != 0) {
566 kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
567 lapic_addr64);
568 lapic_addr = lapic_addr64;
569 }
570
571 madt_sdth_unmap(&madt->madt_hdr);
572
573 return lapic_addr;
574}
575
576struct madt_lapic_pass2_cbarg {
577 int cpu;
578 int bsp_found;
579 int bsp_apic_id;
580};
581
582static int
583madt_lapic_pass2_callback(void *xarg, const struct acpi_madt_ent *ent)
584{
585 const struct acpi_madt_lapic *lapic_ent;
586 struct madt_lapic_pass2_cbarg *arg = xarg;
587
588 if (ent->me_type != MADT_ENT_LAPIC)
589 return 0;
590
591 lapic_ent = (const struct acpi_madt_lapic *)ent;
592 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED) {
593 MADT_VPRINTF("cpu id %d, apic id %d\n",
594 lapic_ent->ml_cpu_id, lapic_ent->ml_apic_id);
595 if (lapic_ent->ml_apic_id == arg->bsp_apic_id) {
596 mp_set_cpuids(0, lapic_ent->ml_apic_id);
597 arg->bsp_found = 1;
598 } else {
599 mp_set_cpuids(arg->cpu, lapic_ent->ml_apic_id);
600 arg->cpu++;
601 }
602 }
603 return 0;
604}
605
606static int
607madt_lapic_pass2(int bsp_apic_id)
608{
609 struct acpi_madt *madt;
610 struct madt_lapic_pass2_cbarg arg;
611 int error;
612
613 MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);
614
615 KKASSERT(madt_phyaddr != 0);
616
617 madt = madt_sdth_map(madt_phyaddr);
618 KKASSERT(madt != NULL);
619
620 bzero(&arg, sizeof(arg));
621 arg.cpu = 1;
622 arg.bsp_apic_id = bsp_apic_id;
623
624 error = madt_iterate_entries(madt, madt_lapic_pass2_callback, &arg);
625 if (error)
626 panic("madt_iterate_entries(pass2) failed\n");
627
628 KKASSERT(arg.bsp_found);
629 KKASSERT(arg.cpu > 1);
630 mp_naps = arg.cpu - 1; /* exclude BSP */
631
632 madt_sdth_unmap(&madt->madt_hdr);
633
634 return 0;
635}
636
637struct madt_lapic_probe_cbarg {
638 int cpu_count;
639 vm_offset_t lapic_addr;
640};
641
642static int
643madt_lapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
644{
645 struct madt_lapic_probe_cbarg *arg = xarg;
646
647 if (ent->me_type == MADT_ENT_LAPIC) {
648 const struct acpi_madt_lapic *lapic_ent;
649
650 lapic_ent = (const struct acpi_madt_lapic *)ent;
651 if (lapic_ent->ml_flags & MADT_LAPIC_ENABLED)
652 arg->cpu_count++;
653 } else if (ent->me_type == MADT_ENT_LAPIC_ADDR) {
654 const struct acpi_madt_lapic_addr *lapic_addr_ent;
655
656 if (ent->me_len < sizeof(*lapic_addr_ent)) {
657 kprintf("madt_lapic_probe: "
658 "invalid LAPIC address override length\n");
659 return 0;
660 }
661 lapic_addr_ent = (const struct acpi_madt_lapic_addr *)ent;
662
663 if (lapic_addr_ent->mla_lapic_addr != 0)
664 arg->lapic_addr = lapic_addr_ent->mla_lapic_addr;
665 }
666 return 0;
667}
668
669static int
670madt_lapic_probe(struct lapic_enumerator *e)
671{
672 struct madt_lapic_probe_cbarg arg;
673 struct acpi_madt *madt;
674 int error;
675
676 if (madt_phyaddr == 0)
677 return ENXIO;
678
679 madt = madt_sdth_map(madt_phyaddr);
680 KKASSERT(madt != NULL);
681
682 bzero(&arg, sizeof(arg));
683 arg.lapic_addr = madt->madt_lapic_addr;
684
685 error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
686 if (!error) {
687 if (arg.cpu_count <= 1) {
688 kprintf("madt_lapic_probe: "
689 "less than 2 CPUs is found\n");
690 error = EOPNOTSUPP;
691 }
692 if (arg.lapic_addr == 0) {
693 kprintf("madt_lapic_probe: zero LAPIC address\n");
694 error = EOPNOTSUPP;
695 }
696 }
697
698 madt_sdth_unmap(&madt->madt_hdr);
699 return error;
700}
701
702static void
703madt_lapic_enumerate(struct lapic_enumerator *e)
704{
705 vm_offset_t lapic_addr;
706 int bsp_apic_id;
707
708 KKASSERT(madt_phyaddr != 0);
709
710 lapic_addr = madt_lapic_pass1();
711 if (lapic_addr == 0)
712 panic("madt_lapic_enumerate: no local apic\n");
713
714 lapic_map(lapic_addr);
715
716 bsp_apic_id = APIC_ID(lapic->id);
717 if (madt_lapic_pass2(bsp_apic_id))
718 panic("madt_lapic_enumerate: madt_lapic_pass2 failed\n");
719}
720
721static struct lapic_enumerator madt_lapic_enumerator = {
722 .lapic_prio = LAPIC_ENUM_PRIO_MADT,
723 .lapic_probe = madt_lapic_probe,
724 .lapic_enumerate = madt_lapic_enumerate
725};
726
727static void
728madt_lapic_enum_register(void)
729{
730 int prio;
731
732 prio = LAPIC_ENUM_PRIO_MADT;
733 kgetenv_int("hw.madt_lapic_prio", &prio);
734 madt_lapic_enumerator.lapic_prio = prio;
735
736 lapic_enumerator_register(&madt_lapic_enumerator);
737}
738SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);
739
740struct madt_ioapic_probe_cbarg {
741 int ioapic_cnt;
742 int gsi_base0;
743};
744
745static int
746madt_ioapic_probe_callback(void *xarg, const struct acpi_madt_ent *ent)
747{
748 struct madt_ioapic_probe_cbarg *arg = xarg;
749
750 if (ent->me_type == MADT_ENT_INTSRC) {
751 const struct acpi_madt_intsrc *intsrc_ent;
752 int trig, pola;
753
754 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
755
756 /* XXX magic number */
757 if (intsrc_ent->mint_src >= 16) {
758 kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
759 intsrc_ent->mint_src);
760 return EINVAL;
761 }
762
763 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA) {
764 kprintf("ACPI MADT: warning intsrc irq %d "
765 "bus is not ISA (%d)\n",
766 intsrc_ent->mint_src, intsrc_ent->mint_bus);
767 }
768
769 trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
770 MADT_INT_TRIG_SHIFT;
771 if (trig == MADT_INT_TRIG_RSVD) {
772 kprintf("ACPI MADT: warning invalid intsrc irq %d "
773 "trig (%d)\n", intsrc_ent->mint_src, trig);
774 }
775
776 pola = (intsrc_ent->mint_flags & MADT_INT_POLA_MASK) >>
777 MADT_INT_POLA_SHIFT;
778 if (pola == MADT_INT_POLA_RSVD) {
779 kprintf("ACPI MADT: warning invalid intsrc irq %d "
780 "pola (%d)\n", intsrc_ent->mint_src, pola);
781 }
782 } else if (ent->me_type == MADT_ENT_IOAPIC) {
783 const struct acpi_madt_ioapic *ioapic_ent;
784
785 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
786 if (ioapic_ent->mio_addr == 0) {
787 kprintf("madt_ioapic_probe: zero IOAPIC address\n");
788 return EINVAL;
789 }
790
791 arg->ioapic_cnt++;
792 if (ioapic_ent->mio_gsi_base == 0)
793 arg->gsi_base0 = 1;
794 }
795 return 0;
796}
797
798static int
799madt_ioapic_probe(struct ioapic_enumerator *e)
800{
801 struct madt_ioapic_probe_cbarg arg;
802 struct acpi_madt *madt;
803 int error;
804
805 if (madt_phyaddr == 0)
806 return ENXIO;
807
808 madt = madt_sdth_map(madt_phyaddr);
809 KKASSERT(madt != NULL);
810
811 bzero(&arg, sizeof(arg));
812
813 error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
814 if (!error) {
815 if (arg.ioapic_cnt == 0) {
816 kprintf("madt_ioapic_probe: no IOAPIC\n");
817 error = ENXIO;
818 }
819 if (!arg.gsi_base0) {
820 kprintf("madt_ioapic_probe: no GSI base 0\n");
821 error = EINVAL;
822 }
823 }
824
825 madt_sdth_unmap(&madt->madt_hdr);
826 return error;
827}
828
829static int
830madt_ioapic_enum_callback(void *xarg, const struct acpi_madt_ent *ent)
831{
832 if (ent->me_type == MADT_ENT_INTSRC) {
833 const struct acpi_madt_intsrc *intsrc_ent;
834 enum intr_trigger trig;
835 enum intr_polarity pola;
836 int ent_trig;
837
838 intsrc_ent = (const struct acpi_madt_intsrc *)ent;
839
840 KKASSERT(intsrc_ent->mint_src < 16);
841 if (intsrc_ent->mint_bus != MADT_INT_BUS_ISA)
842 return 0;
843
844 ent_trig = (intsrc_ent->mint_flags & MADT_INT_TRIG_MASK) >>
845 MADT_INT_TRIG_SHIFT;
846 if (ent_trig == MADT_INT_TRIG_RSVD) {
847 return 0;
848#ifdef notyet
849 } else if (ent_trig == MADT_INT_TRIG_LEVEL) {
850 trig = INTR_TRIGGER_LEVEL;
851 pola = INTR_POLARITY_LOW;
852#endif
853 } else {
854 trig = INTR_TRIGGER_EDGE;
855 pola = INTR_POLARITY_HIGH;
856 }
857
858 if (intsrc_ent->mint_src == intsrc_ent->mint_gsi &&
859 trig == INTR_TRIGGER_EDGE) {
860 /* Nothing changed */
861 return 0;
862 }
863
864 MADT_VPRINTF("INTSRC irq %d -> gsi %u %c\n",
865 intsrc_ent->mint_src, intsrc_ent->mint_gsi,
866 trig == INTR_TRIGGER_EDGE ? 'E' : 'L');
867 ioapic_intsrc(intsrc_ent->mint_src, intsrc_ent->mint_gsi,
868 trig, pola);
869 } else if (ent->me_type == MADT_ENT_IOAPIC) {
870 const struct acpi_madt_ioapic *ioapic_ent;
871
872 ioapic_ent = (const struct acpi_madt_ioapic *)ent;
873 MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
874 ioapic_ent->mio_addr, ioapic_ent->mio_apic_id,
875 ioapic_ent->mio_gsi_base);
876
877 if (!ioapic_use_old) {
878 uint32_t ver;
879 void *addr;
880 int npin;
881
882 addr = ioapic_map(ioapic_ent->mio_addr);
883
884 ver = ioapic_read(addr, IOAPIC_VER);
885 npin = ((ver & IOART_VER_MAXREDIR) >>
886 MAXREDIRSHIFT) + 1;
887
888 ioapic_add(addr, ioapic_ent->mio_gsi_base, npin);
889 }
890 }
891 return 0;
892}
893
894static void
895madt_ioapic_enumerate(struct ioapic_enumerator *e)
896{
897 struct acpi_madt *madt;
898 int error;
899
900 KKASSERT(madt_phyaddr != 0);
901
902 madt = madt_sdth_map(madt_phyaddr);
903 KKASSERT(madt != NULL);
904
905 error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
906 if (error)
907 panic("madt_ioapic_enumerate failed\n");
908
909 madt_sdth_unmap(&madt->madt_hdr);
910}
911
912static struct ioapic_enumerator madt_ioapic_enumerator = {
913 .ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
914 .ioapic_probe = madt_ioapic_probe,
915 .ioapic_enumerate = madt_ioapic_enumerate
916};
917
918static void
919madt_ioapic_enum_register(void)
920{
921 int prio;
922
923 prio = IOAPIC_ENUM_PRIO_MADT;
924 kgetenv_int("hw.madt_ioapic_prio", &prio);
925 madt_ioapic_enumerator.ioapic_prio = prio;
926
927 ioapic_enumerator_register(&madt_ioapic_enumerator);
928}
929SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
930 madt_ioapic_enum_register, 0);