⬢ DragonFlyBSD Kernel Audit
sys/platform/pc64/icu/icu_abi.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
/*
 * Copyright (c) 1991 The Regents of the University of California.
 * Copyright (c) 2005,2008 The DragonFly Project.
 * All rights reserved.
 * 
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz.
 * 
 * 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/systm.h>
#include <sys/kernel.h>
#include <sys/machintr.h>
#include <sys/interrupt.h>
#include <sys/rman.h>
#include <sys/bus.h>

#include <machine/segments.h>
#include <machine/md_var.h>
#include <machine/intr_machdep.h>
#include <machine/globaldata.h>
#include <machine/smp.h>
#include <machine/msi_var.h>

#include <machine_base/icu/elcr_var.h>

#include <machine_base/icu/icu.h>
#include <machine_base/icu/icu_abi.h>
#include <machine_base/icu/icu_ipl.h>
#include <machine_base/apic/ioapic.h>

extern inthand_t
	IDTVEC(icu_intr0),	IDTVEC(icu_intr1),
	IDTVEC(icu_intr2),	IDTVEC(icu_intr3),
	IDTVEC(icu_intr4),	IDTVEC(icu_intr5),
	IDTVEC(icu_intr6),	IDTVEC(icu_intr7),
	IDTVEC(icu_intr8),	IDTVEC(icu_intr9),
	IDTVEC(icu_intr10),	IDTVEC(icu_intr11),
	IDTVEC(icu_intr12),	IDTVEC(icu_intr13),
	IDTVEC(icu_intr14),	IDTVEC(icu_intr15);

static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
	&IDTVEC(icu_intr0),	&IDTVEC(icu_intr1),
	&IDTVEC(icu_intr2),	&IDTVEC(icu_intr3),
	&IDTVEC(icu_intr4),	&IDTVEC(icu_intr5),
	&IDTVEC(icu_intr6),	&IDTVEC(icu_intr7),
	&IDTVEC(icu_intr8),	&IDTVEC(icu_intr9),
	&IDTVEC(icu_intr10),	&IDTVEC(icu_intr11),
	&IDTVEC(icu_intr12),	&IDTVEC(icu_intr13),
	&IDTVEC(icu_intr14),	&IDTVEC(icu_intr15)
};

static struct icu_irqmap {
	int			im_type;	/* ICU_IMT_ */
	enum intr_trigger	im_trig;
	int			im_msi_base;
	uint32_t		im_flags;	/* ICU_IMF_ */
} icu_irqmaps[MAXCPU][IDT_HWI_VECTORS];

static struct lwkt_token icu_irqmap_tok =
	LWKT_TOKEN_INITIALIZER(icu_irqmap_token);

#define ICU_IMT_UNUSED		0	/* KEEP THIS */
#define ICU_IMT_RESERVED	1
#define ICU_IMT_LEGACY		2
#define ICU_IMT_SYSCALL		3
#define ICU_IMT_MSI		4
#define ICU_IMT_MSIX		5

#define ICU_IMT_ISHWI(map)	((map)->im_type != ICU_IMT_RESERVED && \
				 (map)->im_type != ICU_IMT_SYSCALL)

#define ICU_IMF_CONF		0x1

extern void	ICU_INTREN(int);
extern void	ICU_INTRDIS(int);

extern int	imcr_present;

static void	icu_abi_intr_enable(int);
static void	icu_abi_intr_disable(int);
static void	icu_abi_intr_setup(int, int);
static void	icu_abi_intr_teardown(int);

static void	icu_abi_legacy_intr_config(int, enum intr_trigger,
		    enum intr_polarity);
static int	icu_abi_legacy_intr_cpuid(int);
static int	icu_abi_legacy_intr_find(int, enum intr_trigger,
		    enum intr_polarity);
static int	icu_abi_legacy_intr_find_bygsi(int, enum intr_trigger,
		    enum intr_polarity);

static int	icu_abi_msi_alloc(int [], int, int);
static void	icu_abi_msi_release(const int [], int, int);
static void	icu_abi_msi_map(int, uint64_t *, uint32_t *, int);
static int	icu_abi_msix_alloc(int *, int);
static void	icu_abi_msix_release(int, int);

static int	icu_abi_msi_alloc_intern(int, const char *,
		    int [], int, int);
static void	icu_abi_msi_release_intern(int, const char *,
		    const int [], int, int);

static void	icu_abi_finalize(void);
static void	icu_abi_cleanup(void);
static void	icu_abi_setdefault(void);
static void	icu_abi_stabilize(void);
static void	icu_abi_initmap(void);
static void	icu_abi_rman_setup(struct rman *);

struct machintr_abi MachIntrABI_ICU = {
	MACHINTR_ICU,
	.intr_disable	= icu_abi_intr_disable,
	.intr_enable	= icu_abi_intr_enable,
	.intr_setup	= icu_abi_intr_setup,
	.intr_teardown	= icu_abi_intr_teardown,

	.legacy_intr_config = icu_abi_legacy_intr_config,
	.legacy_intr_cpuid = icu_abi_legacy_intr_cpuid,
	.legacy_intr_find = icu_abi_legacy_intr_find,
	.legacy_intr_find_bygsi = icu_abi_legacy_intr_find_bygsi,

	.msi_alloc	= icu_abi_msi_alloc,
	.msi_release	= icu_abi_msi_release,
	.msi_map	= icu_abi_msi_map,
	.msix_alloc	= icu_abi_msix_alloc,
	.msix_release	= icu_abi_msix_release,

	.finalize	= icu_abi_finalize,
	.cleanup	= icu_abi_cleanup,
	.setdefault	= icu_abi_setdefault,
	.stabilize	= icu_abi_stabilize,
	.initmap	= icu_abi_initmap,
	.rman_setup	= icu_abi_rman_setup
};

static int	icu_abi_msi_start;	/* NOTE: for testing only */

/*
 * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
 */

static void
icu_abi_intr_enable(int irq)
{
	const struct icu_irqmap *map;

	KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
	    ("icu enable, invalid irq %d", irq));

	map = &icu_irqmaps[mycpuid][irq];
	KASSERT(ICU_IMT_ISHWI(map),
	    ("icu enable, not hwi irq %d, type %d, cpu%d",
	     irq, map->im_type, mycpuid));
	if (map->im_type != ICU_IMT_LEGACY)
		return;

	ICU_INTREN(irq);
}

static void
icu_abi_intr_disable(int irq)
{
	const struct icu_irqmap *map;

	KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
	    ("icu disable, invalid irq %d", irq));

	map = &icu_irqmaps[mycpuid][irq];
	KASSERT(ICU_IMT_ISHWI(map),
	    ("icu disable, not hwi irq %d, type %d, cpu%d",
	     irq, map->im_type, mycpuid));
	if (map->im_type != ICU_IMT_LEGACY)
		return;

	ICU_INTRDIS(irq);
}

/*
 * Called before interrupts are physically enabled
 */
static void
icu_abi_stabilize(void)
{
	int intr;

	for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
		ICU_INTRDIS(intr);
	ICU_INTREN(ICU_IRQ_SLAVE);
}

/*
 * Called after interrupts physically enabled but before the
 * critical section is released.
 */
static void
icu_abi_cleanup(void)
{
	bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
}

/*
 * Called after stablize and cleanup; critical section is not
 * held and interrupts are not physically disabled.
 */
static void
icu_abi_finalize(void)
{
	KKASSERT(MachIntrABI.type == MACHINTR_ICU);
	KKASSERT(!ioapic_enable);

	/*
	 * If an IMCR is present, programming bit 0 disconnects the 8259
	 * from the BSP.  The 8259 may still be connected to LINT0 on the
	 * BSP's LAPIC.
	 *
	 * If we are running SMP the LAPIC is active, try to use virtual
	 * wire mode so we can use other interrupt sources within the LAPIC
	 * in addition to the 8259.
	 */
	if (imcr_present) {
		outb(0x22, 0x70);
		outb(0x23, 0x01);
	}
}

static void
icu_abi_intr_setup(int intr, int flags)
{
	const struct icu_irqmap *map;
	register_t ef;

	KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
	    ("icu setup, invalid irq %d", intr));

	map = &icu_irqmaps[mycpuid][intr];
	KASSERT(ICU_IMT_ISHWI(map),
	    ("icu setup, not hwi irq %d, type %d, cpu%d",
	     intr, map->im_type, mycpuid));
	if (map->im_type != ICU_IMT_LEGACY)
		return;

	ef = read_rflags();
	cpu_disable_intr();

	ICU_INTREN(intr);

	write_rflags(ef);
}

static void
icu_abi_intr_teardown(int intr)
{
	const struct icu_irqmap *map;
	register_t ef;

	KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
	    ("icu teardown, invalid irq %d", intr));

	map = &icu_irqmaps[mycpuid][intr];
	KASSERT(ICU_IMT_ISHWI(map),
	    ("icu teardown, not hwi irq %d, type %d, cpu%d",
	     intr, map->im_type, mycpuid));
	if (map->im_type != ICU_IMT_LEGACY)
		return;

	ef = read_rflags();
	cpu_disable_intr();

	ICU_INTRDIS(intr);

	write_rflags(ef);
}

static void
icu_abi_setdefault(void)
{
	int intr;

	for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
		if (intr == ICU_IRQ_SLAVE)
			continue;
		setidt_global(IDT_OFFSET + intr, icu_intr[intr],
		    SDT_SYSIGT, SEL_KPL, 0);
	}
}

static void
icu_abi_initmap(void)
{
	int cpu;

	kgetenv_int("hw.icu.msi_start", &icu_abi_msi_start);
	icu_abi_msi_start &= ~0x1f;	/* MUST be 32 aligned */

	/*
	 * NOTE: ncpus is not ready yet
	 */
	for (cpu = 0; cpu < MAXCPU; ++cpu) {
		int i;

		if (cpu != 0) {
			for (i = 0; i < ICU_HWI_VECTORS; ++i)
				icu_irqmaps[cpu][i].im_type = ICU_IMT_RESERVED;
		} else {
			for (i = 0; i < ICU_HWI_VECTORS; ++i)
				icu_irqmaps[cpu][i].im_type = ICU_IMT_LEGACY;
			icu_irqmaps[cpu][ICU_IRQ_SLAVE].im_type =
			    ICU_IMT_RESERVED;

			if (elcr_found) {
				for (i = 0; i < ICU_HWI_VECTORS; ++i) {
					icu_irqmaps[cpu][i].im_trig =
					    elcr_read_trigger(i);
				}
			} else {
				/*
				 * NOTE: Trigger mode does not matter at all
				 */
				for (i = 0; i < ICU_HWI_VECTORS; ++i) {
					icu_irqmaps[cpu][i].im_trig =
					    INTR_TRIGGER_EDGE;
				}
			}
		}

		for (i = 0; i < IDT_HWI_VECTORS; ++i)
			icu_irqmaps[cpu][i].im_msi_base = -1;

		icu_irqmaps[cpu][IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type =
		    ICU_IMT_SYSCALL;
	}
}

static void
icu_abi_legacy_intr_config(int irq, enum intr_trigger trig,
    enum intr_polarity pola __unused)
{
	struct icu_irqmap *map;

	KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);

	KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
	map = &icu_irqmaps[0][irq];

	KKASSERT(map->im_type == ICU_IMT_LEGACY);

	/* TODO: Check whether it is configured or not */
	map->im_flags |= ICU_IMF_CONF;

	if (trig == map->im_trig)
		return;

	if (bootverbose) {
		kprintf("ICU: irq %d, %s -> %s\n", irq,
			intr_str_trigger(map->im_trig),
			intr_str_trigger(trig));
	}
	map->im_trig = trig;

	if (!elcr_found) {
		if (bootverbose)
			kprintf("ICU: no ELCR, skip irq %d config\n", irq);
		return;
	}
	elcr_write_trigger(irq, map->im_trig);
}

static int
icu_abi_legacy_intr_cpuid(int irq __unused)
{
	return 0;
}

static void
icu_abi_rman_setup(struct rman *rm)
{
	int start, end, i;

	KASSERT(rm->rm_cpuid >= 0 && rm->rm_cpuid < MAXCPU,
	    ("invalid rman cpuid %d", rm->rm_cpuid));

	start = end = -1;
	for (i = 0; i < IDT_HWI_VECTORS; ++i) {
		const struct icu_irqmap *map = &icu_irqmaps[rm->rm_cpuid][i];

		if (start < 0) {
			if (ICU_IMT_ISHWI(map))
				start = end = i;
		} else {
			if (ICU_IMT_ISHWI(map)) {
				end = i;
			} else {
				KKASSERT(end >= 0);
				if (bootverbose) {
					kprintf("ICU: rman cpu%d %d - %d\n",
					    rm->rm_cpuid, start, end);
				}
				if (rman_manage_region(rm, start, end)) {
					panic("rman_manage_region"
					    "(cpu%d %d - %d)", rm->rm_cpuid,
					    start, end);
				}
				start = end = -1;
			}
		}
	}
	if (start >= 0) {
		KKASSERT(end >= 0);
		if (bootverbose) {
			kprintf("ICU: rman cpu%d %d - %d\n",
			    rm->rm_cpuid, start, end);
		}
		if (rman_manage_region(rm, start, end)) {
			panic("rman_manage_region(cpu%d %d - %d)",
			    rm->rm_cpuid, start, end);
		}
	}
}

static int
icu_abi_msi_alloc_intern(int type, const char *desc,
    int intrs[], int count, int cpuid)
{
	int i, error;

	KASSERT(cpuid >= 0 && cpuid < ncpus,
	    ("invalid cpuid %d", cpuid));

	KASSERT(count > 0 && count <= 32, ("invalid count %d", count));
	KASSERT(powerof2(count), ("count %d is not power of 2", count));

	lwkt_gettoken(&icu_irqmap_tok);

	/*
	 * NOTE:
	 * Since IDT_OFFSET is 32, which is the maximum valid 'count',
	 * we do not need to find out the first properly aligned
	 * interrupt vector.
	 */

	error = EMSGSIZE;
	for (i = icu_abi_msi_start; i < IDT_HWI_VECTORS; i += count) {
		int j;

		if (icu_irqmaps[cpuid][i].im_type != ICU_IMT_UNUSED)
			continue;

		for (j = 1; j < count; ++j) {
			if (icu_irqmaps[cpuid][i + j].im_type != ICU_IMT_UNUSED)
				break;
		}
		if (j != count)
			continue;

		for (j = 0; j < count; ++j) {
			struct icu_irqmap *map;
			int intr = i + j;

			map = &icu_irqmaps[cpuid][intr];
			KASSERT(map->im_msi_base < 0,
			    ("intr %d, stale %s-base %d",
			     intr, desc, map->im_msi_base));

			map->im_type = type;
			map->im_msi_base = i;

			intrs[j] = intr;
			msi_setup(intr, cpuid);

			if (bootverbose) {
				kprintf("alloc %s intr %d on cpu%d\n",
				    desc, intr, cpuid);
			}
		}
		error = 0;
		break;
	}

	lwkt_reltoken(&icu_irqmap_tok);

	return error;
}

static void
icu_abi_msi_release_intern(int type, const char *desc,
    const int intrs[], int count, int cpuid)
{
	int i, msi_base = -1, intr_next = -1, mask;

	KASSERT(cpuid >= 0 && cpuid < ncpus,
	    ("invalid cpuid %d", cpuid));

	KASSERT(count > 0 && count <= 32, ("invalid count %d", count));

	mask = count - 1;
	KASSERT((count & mask) == 0, ("count %d is not power of 2", count));

	lwkt_gettoken(&icu_irqmap_tok);

	for (i = 0; i < count; ++i) {
		struct icu_irqmap *map;
		int intr = intrs[i];

		KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
		    ("invalid intr %d", intr));

		map = &icu_irqmaps[cpuid][intr];
		KASSERT(map->im_type == type,
		    ("trying to release non-%s intr %d, type %d", desc,
		     intr, map->im_type));
		KASSERT(map->im_msi_base >= 0 && map->im_msi_base <= intr,
		    ("intr %d, invalid %s-base %d", intr, desc,
		     map->im_msi_base));
		KASSERT((map->im_msi_base & mask) == 0,
		    ("intr %d, %s-base %d is not properly aligned %d",
		     intr, desc, map->im_msi_base, count));

		if (msi_base < 0) {
			msi_base = map->im_msi_base;
		} else {
			KASSERT(map->im_msi_base == msi_base,
			    ("intr %d, inconsistent %s-base, "
			     "was %d, now %d",
			     intr, desc, msi_base, map->im_msi_base));
		}

		if (intr_next < intr)
			intr_next = intr;

		map->im_type = ICU_IMT_UNUSED;
		map->im_msi_base = -1;

		if (bootverbose) {
			kprintf("release %s intr %d on cpu%d\n",
			    desc, intr, cpuid);
		}
	}

	KKASSERT(intr_next > 0);
	KKASSERT(msi_base >= 0);

	++intr_next;
	if (intr_next < IDT_HWI_VECTORS) {
		const struct icu_irqmap *map = &icu_irqmaps[cpuid][intr_next];

		if (map->im_type == type) {
			KASSERT(map->im_msi_base != msi_base,
			    ("more than %d %s was allocated", count, desc));
		}
	}

	lwkt_reltoken(&icu_irqmap_tok);
}

static int
icu_abi_msi_alloc(int intrs[], int count, int cpuid)
{
	return icu_abi_msi_alloc_intern(ICU_IMT_MSI, "MSI",
	    intrs, count, cpuid);
}

static void
icu_abi_msi_release(const int intrs[], int count, int cpuid)
{
	icu_abi_msi_release_intern(ICU_IMT_MSI, "MSI",
	    intrs, count, cpuid);
}

static int
icu_abi_msix_alloc(int *intr, int cpuid)
{
	return icu_abi_msi_alloc_intern(ICU_IMT_MSIX, "MSI-X",
	    intr, 1, cpuid);
}

static void
icu_abi_msix_release(int intr, int cpuid)
{
	icu_abi_msi_release_intern(ICU_IMT_MSIX, "MSI-X",
	    &intr, 1, cpuid);
}

static void
icu_abi_msi_map(int intr, uint64_t *addr, uint32_t *data, int cpuid)
{
	const struct icu_irqmap *map;

	KASSERT(cpuid >= 0 && cpuid < ncpus,
	    ("invalid cpuid %d", cpuid));

	KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
	    ("invalid intr %d", intr));

	lwkt_gettoken(&icu_irqmap_tok);

	map = &icu_irqmaps[cpuid][intr];
	KASSERT(map->im_type == ICU_IMT_MSI ||
	    map->im_type == ICU_IMT_MSIX,
	    ("trying to map non-MSI/MSI-X intr %d, type %d", intr, map->im_type));
	KASSERT(map->im_msi_base >= 0 && map->im_msi_base <= intr,
	    ("intr %d, invalid %s-base %d", intr,
	     map->im_type == ICU_IMT_MSI ? "MSI" : "MSI-X",
	     map->im_msi_base));

	msi_map(map->im_msi_base, addr, data, cpuid);

	if (bootverbose) {
		kprintf("map %s intr %d on cpu%d\n",
		    map->im_type == ICU_IMT_MSI ? "MSI" : "MSI-X",
		    intr, cpuid);
	}

	lwkt_reltoken(&icu_irqmap_tok);
}

static int
icu_abi_legacy_intr_find(int irq, enum intr_trigger trig,
    enum intr_polarity pola __unused)
{
	const struct icu_irqmap *map;

#ifdef INVARIANTS
	if (trig == INTR_TRIGGER_CONFORM) {
		KKASSERT(pola == INTR_POLARITY_CONFORM);
	} else {
		KKASSERT(trig == INTR_TRIGGER_EDGE ||
		    trig == INTR_TRIGGER_LEVEL);
		KKASSERT(pola == INTR_POLARITY_HIGH ||
		    pola == INTR_POLARITY_LOW);
	}
#endif

	if (irq < 0 || irq >= ICU_HWI_VECTORS)
		return -1;

	map = &icu_irqmaps[0][irq];
	if (map->im_type == ICU_IMT_LEGACY) {
		if ((map->im_flags & ICU_IMF_CONF) &&
		    trig != INTR_TRIGGER_CONFORM) {
			if (map->im_trig != trig)
				return -1;
		}
		return irq;
	}
	return -1;
}

static int
icu_abi_legacy_intr_find_bygsi(int gsi, enum intr_trigger trig,
    enum intr_polarity pola)
{
	/* GSI and IRQ has 1:1 mapping */
	return icu_abi_legacy_intr_find(gsi, trig, pola);
}