DragonFlyBSD Kernel Audit
sys/platform/pc64/acpica/acpi_madt.c
← back
  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
/*
 * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
 * 
 * This code is derived from software contributed to The DragonFly Project
 * by Sepherosa Ziehau <sepherosa@gmail.com>
 * 
 * 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. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT HOLDERS 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.
 */

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/systm.h>

#include <machine_base/isa/isa_intr.h>
#include <machine_base/apic/lapic.h>
#include <machine_base/apic/ioapic.h>
#include <machine_base/apic/apicvar.h>
#include <machine_base/acpica/acpi_md_cpu.h>
#include <machine/specialreg.h>

#include <contrib/dev/acpica/source/include/acpi.h>

#include "acpi_sdt_var.h"
#include "acpi_sci_var.h"

extern int naps;

#define MADT_VPRINTF(fmt, arg...) \
do { \
	if (bootverbose) \
		kprintf("ACPI MADT: " fmt , ##arg); \
} while (0)

#define MADT_INT_BUS_ISA	0

typedef int			(*madt_iter_t)(void *,
				    const ACPI_SUBTABLE_HEADER *);

static int			madt_check(vm_paddr_t);
static int			madt_iterate_entries(ACPI_TABLE_MADT *,
				    madt_iter_t, void *);

static vm_paddr_t		madt_lapic_pass1(void);
static int			madt_lapic_pass2(int);

static int			madt_lapic_enumerate(struct lapic_enumerator *);
static int			madt_lapic_probe(struct lapic_enumerator *);

static void			madt_ioapic_enumerate(
				    struct ioapic_enumerator *);
static int			madt_ioapic_probe(struct ioapic_enumerator *);

static vm_paddr_t		madt_phyaddr;

static boolean_t		madt_use_x2apic = FALSE;

u_int				cpu_id_to_acpi_id[NAPICID];

static void
madt_probe(void)
{
	vm_paddr_t madt_paddr;
	int i;

	for (i = 0; i < NAPICID; ++i)
		CPUID_TO_ACPIID(i) = (u_int)-1;

	KKASSERT(madt_phyaddr == 0);

	madt_paddr = sdt_search(ACPI_SIG_MADT);
	if (madt_paddr == 0) {
		kprintf("madt_probe: can't locate MADT\n");
		return;
	}

	/* Preliminary checks */
	if (madt_check(madt_paddr)) {
		kprintf("madt_probe: madt_check failed\n");
		return;
	}

	madt_phyaddr = madt_paddr;
}
SYSINIT(madt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, madt_probe, 0);

static int
madt_check(vm_paddr_t madt_paddr)
{
	ACPI_TABLE_MADT *madt;
	int error = 0;

	KKASSERT(madt_paddr != 0);

	madt = sdt_sdth_map(madt_paddr);
	KKASSERT(madt != NULL);

	/*
	 * MADT in ACPI specification 1.0 - 6.4
	 */
	if (madt->Header.Revision < 1 || madt->Header.Revision > 5) {
		kprintf("madt_check: unknown MADT revision %d\n",
			madt->Header.Revision);
	}

	if (madt->Header.Length < sizeof(*madt)) {
		kprintf("madt_check: invalid MADT length %u\n",
			madt->Header.Length);
		error = EINVAL;
		goto back;
	}
back:
	sdt_sdth_unmap(&madt->Header);
	return error;
}

static int
madt_iterate_entries(ACPI_TABLE_MADT *madt, madt_iter_t func, void *arg)
{
	int size, cur, error;

	size = madt->Header.Length - sizeof(*madt);
	cur = 0;
	error = 0;

	while (size - cur > sizeof(ACPI_SUBTABLE_HEADER)) {
		const ACPI_SUBTABLE_HEADER *ent;
		
		ent = (const ACPI_SUBTABLE_HEADER *)
		    ((char *)madt + sizeof(*madt) + cur);
		if (ent->Length < sizeof(*ent)) {
			kprintf("madt_iterate_entries: invalid MADT "
				"entry len %d\n", ent->Length);
			error = EINVAL;
			break;
		}
		if (ent->Length > (size - cur)) {
			kprintf("madt_iterate_entries: invalid MADT "
				"entry len %d, > table length\n", ent->Length);
			error = EINVAL;
			break;
		}

		cur += ent->Length;

		/*
		 * Only Local APIC, I/O APIC and Interrupt Source Override
		 * are defined in ACPI specification 1.0 - 5.0
		 */
		switch (ent->Type) {
		case ACPI_MADT_TYPE_LOCAL_APIC:
			if (ent->Length < sizeof(ACPI_MADT_LOCAL_APIC)) {
				kprintf("madt_iterate_entries: invalid MADT "
					"lapic entry len %d\n", ent->Length);
				error = EINVAL;
			}
			break;

		case ACPI_MADT_TYPE_LOCAL_X2APIC:
			if (ent->Length < sizeof(ACPI_MADT_LOCAL_X2APIC)) {
				kprintf("madt_iterate_entries: invalid MADT "
					"x2apic entry len %d\n", ent->Length);
				error = EINVAL;
			}
			break;

		case ACPI_MADT_TYPE_IO_APIC:
			if (ent->Length < sizeof(ACPI_MADT_IO_APIC)) {
				kprintf("madt_iterate_entries: invalid MADT "
					"ioapic entry len %d\n", ent->Length);
				error = EINVAL;
			}
			break;

		case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
			if (ent->Length < sizeof(ACPI_MADT_INTERRUPT_OVERRIDE)) {
				kprintf("madt_iterate_entries: invalid MADT "
					"intsrc entry len %d\n",
					ent->Length);
				error = EINVAL;
			}
			break;
		}
		if (error)
			break;

		error = func(arg, ent);
		if (error)
			break;

		ent = ACPI_ADD_PTR(ACPI_SUBTABLE_HEADER, ent, ent->Length);
	}
	return error;
}

static int
madt_lapic_pass1_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
{
	const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;
	uint64_t *addr64 = xarg;

	if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE)
		return 0;
	if (ent->Length < sizeof(*lapic_addr_ent)) {
		kprintf("madt_lapic_pass1: "
			"invalid LAPIC address override length\n");
		return 0;
	}
	lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;

	*addr64 = lapic_addr_ent->Address;
	return 0;
}

static vm_paddr_t
madt_lapic_pass1(void)
{
	ACPI_TABLE_MADT *madt;
	vm_paddr_t lapic_addr;
	uint64_t lapic_addr64;
	int error;

	KKASSERT(madt_phyaddr != 0);

	madt = sdt_sdth_map(madt_phyaddr);
	KKASSERT(madt != NULL);

	MADT_VPRINTF("LAPIC address 0x%x, flags %#x\n",
		     madt->Address, madt->Flags);
	lapic_addr = madt->Address;

	lapic_addr64 = 0;
	error = madt_iterate_entries(madt, madt_lapic_pass1_callback,
				     &lapic_addr64);
	if (error)
		panic("madt_iterate_entries(pass1) failed");

	if (lapic_addr64 != 0) {
		kprintf("ACPI MADT: 64bits lapic address 0x%lx\n",
			lapic_addr64);
		lapic_addr = lapic_addr64;
	}

	sdt_sdth_unmap(&madt->Header);

	return lapic_addr;
}

struct madt_lapic_pass2_cbarg {
	int	cpu;
	int	bsp_found;
	int	bsp_apic_id;
};

static int
madt_lapic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
{
	const ACPI_MADT_LOCAL_APIC *lapic_ent;
	struct madt_lapic_pass2_cbarg *arg = xarg;

	if (ent->Type != ACPI_MADT_TYPE_LOCAL_APIC)
		return 0;

	lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
	if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
		int cpu;

		if (lapic_ent->Id == arg->bsp_apic_id) {
			cpu = 0;
			if (arg->bsp_found) {
				kprintf("cpu id %d, duplicate BSP found\n",
					arg->cpu);
			}
			arg->bsp_found = 1;
		} else {
			cpu = arg->cpu;
			arg->cpu++;
		}
		MADT_VPRINTF("cpu id %d, acpi id %d, apic id %d\n",
		    cpu, lapic_ent->ProcessorId, lapic_ent->Id);
		if (lapic_ent->Id >= 255) {
			kprintf("cpu id %d, WARNING acpi id %d\n",
				cpu, lapic_ent->Id);
		}
		if (lapic_ent->ProcessorId >= 255) {
			kprintf("cpu id %d, WARNING acpi processorid %d\n",
				cpu, lapic_ent->ProcessorId);
		}
		lapic_set_cpuid(cpu, lapic_ent->Id);
		CPUID_TO_ACPIID(cpu) = lapic_ent->ProcessorId;
	}
	return 0;
}

static int
madt_x2apic_pass2_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
{
	const ACPI_MADT_LOCAL_X2APIC *x2apic_ent;
	struct madt_lapic_pass2_cbarg *arg = xarg;

	if (ent->Type != ACPI_MADT_TYPE_LOCAL_X2APIC)
		return 0;

	x2apic_ent = (const ACPI_MADT_LOCAL_X2APIC *)ent;
	if (x2apic_ent->LapicFlags & ACPI_MADT_ENABLED) {
		int cpu;

		if (x2apic_ent->LocalApicId == arg->bsp_apic_id) {
			cpu = 0;
			arg->bsp_found = 1;
		} else {
			cpu = arg->cpu;
			arg->cpu++;
		}
		MADT_VPRINTF("cpu id %d, acpi uid %u, apic id %d\n",
		    cpu, x2apic_ent->Uid, x2apic_ent->LocalApicId);
		lapic_set_cpuid(cpu, x2apic_ent->LocalApicId);
		CPUID_TO_ACPIID(cpu) = x2apic_ent->Uid;
	}
	return 0;
}

static int
madt_lapic_pass2(int bsp_apic_id)
{
	ACPI_TABLE_MADT *madt;
	struct madt_lapic_pass2_cbarg arg;
	madt_iter_t func;
	int error;

	MADT_VPRINTF("BSP apic id %d\n", bsp_apic_id);

	KKASSERT(madt_phyaddr != 0);

	madt = sdt_sdth_map(madt_phyaddr);
	KKASSERT(madt != NULL);

	bzero(&arg, sizeof(arg));
	arg.cpu = 1;
	arg.bsp_apic_id = bsp_apic_id;

	if (madt_use_x2apic)
		func = madt_x2apic_pass2_callback;
	else
		func = madt_lapic_pass2_callback;
	error = madt_iterate_entries(madt, func, &arg);
	if (error)
		panic("madt_iterate_entries(pass2) failed");

	KKASSERT(arg.bsp_found);
	naps = arg.cpu - 1; /* exclude BSP */
	kprintf("ACPI CPUS = %d\n", arg.cpu);

	sdt_sdth_unmap(&madt->Header);

	return 0;
}

struct madt_lapic_probe_cbarg {
	int		x2apic_count;
	int		lapic_count;
	vm_paddr_t	lapic_addr;
};

static int
madt_lapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
{
	struct madt_lapic_probe_cbarg *arg = xarg;

	if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC) {
		const ACPI_MADT_LOCAL_APIC *lapic_ent;

		lapic_ent = (const ACPI_MADT_LOCAL_APIC *)ent;
		if (lapic_ent->LapicFlags & ACPI_MADT_ENABLED) {
			arg->lapic_count++;
			if (lapic_ent->Id == APICID_MAX) {
				kprintf("madt_lapic_probe: "
				    "invalid LAPIC apic id %d\n",
				    lapic_ent->Id);
				return EINVAL;
			}
		}
	} else if (ent->Type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
		const ACPI_MADT_LOCAL_X2APIC *x2apic_ent;

		x2apic_ent = (const ACPI_MADT_LOCAL_X2APIC *)ent;
		if (x2apic_ent->LapicFlags & ACPI_MADT_ENABLED) {
			if (x2apic_ent->LocalApicId < APICID_MAX) {
				/*
				 * XXX we only support APIC ID 0~254 at
				 * the moment.
				 */
				arg->x2apic_count++;
			}
		}
	} else if (ent->Type == ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE) {
		const ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_addr_ent;

		if (ent->Length < sizeof(*lapic_addr_ent)) {
			kprintf("madt_lapic_probe: "
				"invalid LAPIC address override length\n");
			return 0;
		}
		lapic_addr_ent = (const ACPI_MADT_LOCAL_APIC_OVERRIDE *)ent;

		if (lapic_addr_ent->Address != 0)
			arg->lapic_addr = lapic_addr_ent->Address;
	}
	return 0;
}

static int
madt_lapic_probe(struct lapic_enumerator *e)
{
	struct madt_lapic_probe_cbarg arg;
	ACPI_TABLE_MADT *madt;
	int error;

	if (madt_phyaddr == 0)
		return ENXIO;

	madt = sdt_sdth_map(madt_phyaddr);
	KKASSERT(madt != NULL);

	bzero(&arg, sizeof(arg));
	arg.lapic_addr = madt->Address;

	error = madt_iterate_entries(madt, madt_lapic_probe_callback, &arg);
	if (!error) {
		kprintf("madt_lapic_probe: lapic_count=%d x2apic_count=%d\n",
			arg.lapic_count, arg.x2apic_count);
		if (arg.lapic_count == 0 && arg.x2apic_count == 0) {
			kprintf("madt_lapic_probe: no CPU is found\n");
			error = EOPNOTSUPP;
		} else if (arg.lapic_count == 0) {
			/*
			 * ACPI 5.1 says that LOCAL_X2APIC entry should
			 * be used only if APIC ID > 255.  While ACPI 6.2
			 * removes that constraint, which means that
			 * LOCAL_X2APIC entry could be used for any APIC
			 * ID.
			 *
			 * XXX
			 * In DragonFlyBSD, we don't support APIC ID >=
			 * 255, so LOCAL_X2APIC entries should be ignored,
			 * if LOCAL_X2APIC entries are mixed with
			 * LOCAL_APIC entries.  LOCAL_X2APIC entries are
			 * used, iff only LOCAL_X2APIC entries exist.
			 */
			madt_use_x2apic = TRUE;
			kprintf("MADT: use X2APIC entries\n");
		}

		if (arg.lapic_addr == 0) {
			/*
			 * The LAPIC address does not matter in X2APIC mode.
			 */
			if ((cpu_feature2 & CPUID2_X2APIC) == 0) {
				kprintf("madt_lapic_probe: zero LAPIC address\n");
				error = EOPNOTSUPP;
			}
		}
	}

	sdt_sdth_unmap(&madt->Header);
	return error;
}

static int
madt_lapic_enumerate(struct lapic_enumerator *e)
{
	vm_paddr_t lapic_addr = 0;
	int bsp_apic_id;

	KKASSERT(madt_phyaddr != 0);

	if (!x2apic_enable) {
		lapic_addr = madt_lapic_pass1();
		if (lapic_addr == 0) {
			/*
			 * No LAPIC address.
			 */
			if (cpu_feature2 & CPUID2_X2APIC) {
				/*
				 * X2APIC mode is not enabled, but the CPU supports
				 * it.  Forcefully enable X2APIC mode, which nullifies
				 * the requirement of the LAPIC address.
				 */
				kprintf("MADT: no LAPIC address, force X2APIC mode\n");
				KKASSERT(!x2apic_enable);
				x2apic_enable = 1;
				lapic_x2apic_enter(TRUE);
			} else {
				/*
				 * We should not reach here, madt_lapic_probe() must
				 * have failed.
				 */
				panic("madt_lapic_enumerate: no local apic");
			}
		}
	}

	if (!x2apic_enable) {
		KASSERT(lapic_addr != 0, ("madt_lapic_enumerate: zero LAPIC address"));
		lapic_map(lapic_addr);
	}

	bsp_apic_id = LAPIC_READID;
	if (bsp_apic_id == APICID_MAX) {
		/*
		 * XXX
		 * Some old brain dead BIOS will set BSP's LAPIC apic id
		 * to 255, though all LAPIC entries in MADT are valid.
		 */
		kprintf("%s invalid BSP LAPIC apic id %d\n", __func__,
		    bsp_apic_id);
		return EINVAL;
	}

	if (madt_lapic_pass2(bsp_apic_id))
		panic("madt_lapic_enumerate: madt_lapic_pass2 failed");

	return 0;
}

static struct lapic_enumerator	madt_lapic_enumerator = {
	.lapic_prio = LAPIC_ENUM_PRIO_MADT,
	.lapic_probe = madt_lapic_probe,
	.lapic_enumerate = madt_lapic_enumerate
};

static void
madt_lapic_enum_register(void)
{
	int prio;

	prio = LAPIC_ENUM_PRIO_MADT;
	kgetenv_int("hw.madt_lapic_prio", &prio);
	madt_lapic_enumerator.lapic_prio = prio;

	lapic_enumerator_register(&madt_lapic_enumerator);
}
SYSINIT(madt_lapic, SI_BOOT2_PRESMP, SI_ORDER_ANY, madt_lapic_enum_register, 0);

struct madt_ioapic_probe_cbarg {
	int	ioapic_cnt;
	int	gsi_base0;
};

static int
madt_ioapic_probe_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
{
	struct madt_ioapic_probe_cbarg *arg = xarg;

	if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
		const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
		int trig, pola;

		intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;

		if (intsrc_ent->SourceIrq >= ISA_IRQ_CNT) {
			kprintf("madt_ioapic_probe: invalid intsrc irq (%d)\n",
				intsrc_ent->SourceIrq);
			return EINVAL;
		}

		if (intsrc_ent->Bus != MADT_INT_BUS_ISA) {
			kprintf("ACPI MADT: warning intsrc irq %d "
				"bus is not ISA (%d)\n",
				intsrc_ent->SourceIrq, intsrc_ent->Bus);
		}

		trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
		if (trig == ACPI_MADT_TRIGGER_RESERVED) {
			kprintf("ACPI MADT: warning invalid intsrc irq %d "
				"trig, reserved\n", intsrc_ent->SourceIrq);
		} else if (trig == ACPI_MADT_TRIGGER_LEVEL) {
			MADT_VPRINTF("warning invalid intsrc irq %d "
			    "trig, level\n", intsrc_ent->SourceIrq);
		}

		pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
		if (pola == ACPI_MADT_POLARITY_RESERVED) {
			kprintf("ACPI MADT: warning invalid intsrc irq %d "
				"pola, reserved\n", intsrc_ent->SourceIrq);
		} else if (pola == ACPI_MADT_POLARITY_ACTIVE_LOW) {
			MADT_VPRINTF("warning invalid intsrc irq %d "
			    "pola, low\n", intsrc_ent->SourceIrq);
		}
	} else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
		const ACPI_MADT_IO_APIC *ioapic_ent;

		ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
		if (ioapic_ent->Address == 0) {
			kprintf("madt_ioapic_probe: zero IOAPIC address\n");
			return EINVAL;
		}
		if (ioapic_ent->Id == APICID_MAX) {
			kprintf("madt_ioapic_probe: "
			    "invalid IOAPIC apic id %d\n",
			    ioapic_ent->Id);
			return EINVAL;
		}

		arg->ioapic_cnt++;
		if (ioapic_ent->GlobalIrqBase == 0)
			arg->gsi_base0 = 1;
	}
	return 0;
}

static int
madt_ioapic_probe(struct ioapic_enumerator *e)
{
	struct madt_ioapic_probe_cbarg arg;
	ACPI_TABLE_MADT *madt;
	int error;

	if (madt_phyaddr == 0)
		return ENXIO;

	madt = sdt_sdth_map(madt_phyaddr);
	KKASSERT(madt != NULL);

	bzero(&arg, sizeof(arg));

	error = madt_iterate_entries(madt, madt_ioapic_probe_callback, &arg);
	if (!error) {
		if (arg.ioapic_cnt == 0) {
			kprintf("madt_ioapic_probe: no IOAPIC\n");
			error = ENXIO;
		}
		if (!arg.gsi_base0) {
			kprintf("madt_ioapic_probe: no GSI base 0\n");
			error = EINVAL;
		}
	}

	sdt_sdth_unmap(&madt->Header);
	return error;
}

static int
madt_ioapic_enum_callback(void *xarg, const ACPI_SUBTABLE_HEADER *ent)
{
	if (ent->Type == ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
		const ACPI_MADT_INTERRUPT_OVERRIDE *intsrc_ent;
		enum intr_trigger trig;
		enum intr_polarity pola;
		int ent_trig, ent_pola;

		intsrc_ent = (const ACPI_MADT_INTERRUPT_OVERRIDE *)ent;

		KKASSERT(intsrc_ent->SourceIrq < ISA_IRQ_CNT);
		if (intsrc_ent->Bus != MADT_INT_BUS_ISA)
			return 0;

		ent_trig = intsrc_ent->IntiFlags & ACPI_MADT_TRIGGER_MASK;
		if (ent_trig == ACPI_MADT_TRIGGER_RESERVED)
			return 0;
		else if (ent_trig == ACPI_MADT_TRIGGER_LEVEL)
			trig = INTR_TRIGGER_LEVEL;
		else
			trig = INTR_TRIGGER_EDGE;

		ent_pola = intsrc_ent->IntiFlags & ACPI_MADT_POLARITY_MASK;
		if (ent_pola == ACPI_MADT_POLARITY_RESERVED)
			return 0;
		else if (ent_pola == ACPI_MADT_POLARITY_ACTIVE_LOW)
			pola = INTR_POLARITY_LOW;
		else
			pola = INTR_POLARITY_HIGH;

		if (intsrc_ent->SourceIrq == acpi_sci_irqno()) {
			acpi_sci_setmode1(trig, pola);
			MADT_VPRINTF("SCI irq %d, first test %s/%s\n",
			    intsrc_ent->SourceIrq,
			    intr_str_trigger(trig), intr_str_polarity(pola));
		}

		/*
		 * We ignore the polarity and trigger changes, since
		 * most of them are wrong or useless at best.
		 */
		if (intsrc_ent->SourceIrq == intsrc_ent->GlobalIrq) {
			/* Nothing changed */
			return 0;
		}
		trig = INTR_TRIGGER_EDGE;
		pola = INTR_POLARITY_HIGH;

		MADT_VPRINTF("INTSRC irq %d -> gsi %u %s/%s\n",
			     intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
			     intr_str_trigger(trig), intr_str_polarity(pola));
		ioapic_intsrc(intsrc_ent->SourceIrq, intsrc_ent->GlobalIrq,
			      trig, pola);
	} else if (ent->Type == ACPI_MADT_TYPE_IO_APIC) {
		const ACPI_MADT_IO_APIC *ioapic_ent;
		uint32_t ver;
		void *addr;
		int npin;

		ioapic_ent = (const ACPI_MADT_IO_APIC *)ent;
		MADT_VPRINTF("IOAPIC addr 0x%08x, apic id %d, gsi base %u\n",
			     ioapic_ent->Address, ioapic_ent->Id,
			     ioapic_ent->GlobalIrqBase);

		addr = ioapic_map(ioapic_ent->Address);

		ver = ioapic_read(addr, IOAPIC_VER);
		npin = ((ver & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;

		ioapic_add(addr, ioapic_ent->GlobalIrqBase, npin);
	}
	return 0;
}

static void
madt_ioapic_enumerate(struct ioapic_enumerator *e)
{
	ACPI_TABLE_MADT *madt;
	int error;

	KKASSERT(madt_phyaddr != 0);

	madt = sdt_sdth_map(madt_phyaddr);
	KKASSERT(madt != NULL);

	error = madt_iterate_entries(madt, madt_ioapic_enum_callback, NULL);
	if (error)
		panic("madt_ioapic_enumerate failed");

	sdt_sdth_unmap(&madt->Header);
}

static struct ioapic_enumerator	madt_ioapic_enumerator = {
	.ioapic_prio = IOAPIC_ENUM_PRIO_MADT,
	.ioapic_probe = madt_ioapic_probe,
	.ioapic_enumerate = madt_ioapic_enumerate
};

static void
madt_ioapic_enum_register(void)
{
	int prio;

	prio = IOAPIC_ENUM_PRIO_MADT;
	kgetenv_int("hw.madt_ioapic_prio", &prio);
	madt_ioapic_enumerator.ioapic_prio = prio;

	ioapic_enumerator_register(&madt_ioapic_enumerator);
}
SYSINIT(madt_ioapic, SI_BOOT2_PRESMP, SI_ORDER_ANY,
	madt_ioapic_enum_register, 0);