Home | History | Annotate | Line # | Download | only in pci
msipic.c revision 1.15
      1 /*	$NetBSD: msipic.c,v 1.15 2019/06/17 06:38:29 msaitoh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2015 Internet Initiative Japan Inc.
      5  * All rights reserved.
      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  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: msipic.c,v 1.15 2019/06/17 06:38:29 msaitoh Exp $");
     31 
     32 #include "opt_intrdebug.h"
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/errno.h>
     38 #include <sys/kmem.h>
     39 #include <sys/mutex.h>
     40 
     41 #include <dev/pci/pcivar.h>
     42 
     43 #include <machine/i82489reg.h>
     44 #include <machine/i82489var.h>
     45 #include <machine/i82093reg.h>
     46 #include <machine/i82093var.h>
     47 #include <machine/pic.h>
     48 #include <machine/lock.h>
     49 
     50 #include <x86/pci/msipic.h>
     51 
     52 #ifdef INTRDEBUG
     53 #define MSIPICDEBUG
     54 #endif
     55 
     56 #ifdef MSIPICDEBUG
     57 #define DPRINTF(msg) printf msg
     58 #else
     59 #define DPRINTF(msg)
     60 #endif
     61 
     62 #define BUS_SPACE_WRITE_FLUSH(pc, tag) (void)bus_space_read_4(pc, tag, 0)
     63 
     64 #define MSIPICNAMEBUF 16
     65 
     66 /*
     67  * A Pseudo pic for single MSI/MSI-X device.
     68  * The pic and MSI/MSI-X device are distinbuished by "devid". The "devid"
     69  * is managed by below "dev_seqs".
     70  */
     71 struct msipic {
     72 	int mp_bus;
     73 	int mp_dev;
     74 	int mp_fun;
     75 
     76 	int mp_devid; /* The device id for the MSI/MSI-X device. */
     77 	int mp_veccnt; /* The number of MSI/MSI-X vectors. */
     78 
     79 	char mp_pic_name[MSIPICNAMEBUF]; /* The MSI/MSI-X device's name. */
     80 
     81 	struct pci_attach_args mp_pa;
     82 	bus_space_tag_t mp_bstag;
     83 	bus_space_handle_t mp_bshandle;
     84 	bus_size_t mp_bssize;
     85 	struct pic *mp_pic;
     86 
     87 	LIST_ENTRY(msipic) mp_list;
     88 };
     89 
     90 static kmutex_t msipic_list_lock;
     91 
     92 static LIST_HEAD(, msipic) msipic_list =
     93 	LIST_HEAD_INITIALIZER(msipic_list);
     94 
     95 /*
     96  * This struct managements "devid" to use the same "devid" for the device
     97  * re-attached. If the device's bus number and device number and function
     98  * number are equal, it is assumed re-attached.
     99  */
    100 struct dev_last_used_seq {
    101 	bool ds_using;
    102 	int ds_bus;
    103 	int ds_dev;
    104 	int ds_fun;
    105 };
    106 /* The number of MSI/MSI-X devices supported by system. */
    107 #define NUM_MSI_DEVS 256
    108 /* Record devids to use the same devid when the device is re-attached. */
    109 static struct dev_last_used_seq dev_seqs[NUM_MSI_DEVS];
    110 
    111 static int msipic_allocate_common_msi_devid(const struct pci_attach_args *);
    112 static void msipic_release_common_msi_devid(int);
    113 
    114 static struct pic *msipic_find_msi_pic_locked(int);
    115 static struct pic *msipic_construct_common_msi_pic(const struct pci_attach_args *,
    116 						   struct pic *);
    117 static void msipic_destruct_common_msi_pic(struct pic *);
    118 
    119 static void msi_set_msictl_enablebit(struct pic *, int, int);
    120 static void msi_hwmask(struct pic *, int);
    121 static void msi_hwunmask(struct pic *, int);
    122 static void msi_addroute(struct pic *, struct cpu_info *, int, int, int);
    123 static void msi_delroute(struct pic *, struct cpu_info *, int, int, int);
    124 
    125 static void msix_set_vecctl_mask(struct pic *, int, int);
    126 static void msix_hwmask(struct pic *, int);
    127 static void msix_hwunmask(struct pic *, int);
    128 static void msix_addroute(struct pic *, struct cpu_info *, int, int, int);
    129 static void msix_delroute(struct pic *, struct cpu_info *, int, int, int);
    130 
    131 /*
    132  * Return new "devid" for the device attached first.
    133  * Return the same "devid" for the device re-attached after dettached once.
    134  * Return -1 if the number of attached MSI/MSI-X devices is over NUM_MSI_DEVS.
    135  */
    136 static int
    137 msipic_allocate_common_msi_devid(const struct pci_attach_args *pa)
    138 {
    139 	pci_chipset_tag_t pc;
    140 	pcitag_t tag;
    141 	int bus, dev, fun, i;
    142 
    143 	KASSERT(mutex_owned(&msipic_list_lock));
    144 
    145 	pc = pa->pa_pc;
    146 	tag = pa->pa_tag;
    147 	pci_decompose_tag(pc, tag, &bus, &dev, &fun);
    148 
    149 	/* if the device was once attached, use same devid */
    150 	for (i = 0; i < NUM_MSI_DEVS; i++) {
    151 		/* skip host bridge */
    152 		if (dev_seqs[i].ds_bus == 0
    153 		    && dev_seqs[i].ds_dev == 0
    154 		    && dev_seqs[i].ds_fun == 0)
    155 			break;
    156 
    157 		if (dev_seqs[i].ds_bus == bus
    158 		    && dev_seqs[i].ds_dev == dev
    159 		    && dev_seqs[i].ds_fun == fun) {
    160 			dev_seqs[i].ds_using = true;
    161 			return i;
    162 		}
    163 	}
    164 
    165 	for (i = 0; i < NUM_MSI_DEVS; i++) {
    166 		if (dev_seqs[i].ds_using == 0) {
    167 			dev_seqs[i].ds_using = true;
    168 			dev_seqs[i].ds_bus = bus;
    169 			dev_seqs[i].ds_dev = dev;
    170 			dev_seqs[i].ds_fun = fun;
    171 			return i;
    172 		}
    173 	}
    174 
    175 	DPRINTF(("too many MSI devices.\n"));
    176 	return -1;
    177 }
    178 
    179 /*
    180  * Set the "devid" unused, but keep reserving the "devid" to reuse when
    181  * the device is re-attached.
    182  */
    183 static void
    184 msipic_release_common_msi_devid(int devid)
    185 {
    186 
    187 	KASSERT(mutex_owned(&msipic_list_lock));
    188 
    189 	if (devid < 0 || NUM_MSI_DEVS <= devid) {
    190 		DPRINTF(("%s: invalid devid.\n", __func__));
    191 		return;
    192 	}
    193 
    194 	dev_seqs[devid].ds_using = false;
    195 	/* Keep ds_* to reuse the same devid for the same device. */
    196 }
    197 
    198 static struct pic *
    199 msipic_find_msi_pic_locked(int devid)
    200 {
    201 	struct msipic *mpp;
    202 
    203 	KASSERT(mutex_owned(&msipic_list_lock));
    204 
    205 	LIST_FOREACH(mpp, &msipic_list, mp_list) {
    206 		if (mpp->mp_devid == devid)
    207 			return mpp->mp_pic;
    208 	}
    209 	return NULL;
    210 }
    211 
    212 /*
    213  * Return the msi_pic whose device is already registered.
    214  * If the device is not registered yet, return NULL.
    215  */
    216 struct pic *
    217 msipic_find_msi_pic(int devid)
    218 {
    219 	struct pic *msipic;
    220 
    221 	mutex_enter(&msipic_list_lock);
    222 	msipic = msipic_find_msi_pic_locked(devid);
    223 	mutex_exit(&msipic_list_lock);
    224 
    225 	return msipic;
    226 }
    227 
    228 /*
    229  * A common construct process of MSI and MSI-X.
    230  */
    231 static struct pic *
    232 msipic_construct_common_msi_pic(const struct pci_attach_args *pa,
    233     struct pic *pic_tmpl)
    234 {
    235 	struct pic *pic;
    236 	struct msipic *msipic;
    237 	int devid;
    238 
    239 	pic = kmem_alloc(sizeof(*pic), KM_SLEEP);
    240 	msipic = kmem_zalloc(sizeof(*msipic), KM_SLEEP);
    241 
    242 	mutex_enter(&msipic_list_lock);
    243 
    244 	devid = msipic_allocate_common_msi_devid(pa);
    245 	if (devid == -1) {
    246 		mutex_exit(&msipic_list_lock);
    247 		kmem_free(pic, sizeof(*pic));
    248 		kmem_free(msipic, sizeof(*msipic));
    249 		return NULL;
    250 	}
    251 
    252 	memcpy(pic, pic_tmpl, sizeof(*pic));
    253 	pic->pic_edge_stubs
    254 	    = x2apic_mode ? x2apic_edge_stubs : ioapic_edge_stubs;
    255 	pic->pic_msipic = msipic;
    256 	msipic->mp_pic = pic;
    257 	pci_decompose_tag(pa->pa_pc, pa->pa_tag,
    258 	    &msipic->mp_bus, &msipic->mp_dev, &msipic->mp_fun);
    259 	memcpy(&msipic->mp_pa, pa, sizeof(msipic->mp_pa));
    260 	msipic->mp_devid = devid;
    261 	/*
    262 	 * pci_msi{,x}_alloc() must be called only once in the device driver.
    263 	 */
    264 	KASSERT(msipic_find_msi_pic_locked(msipic->mp_devid) == NULL);
    265 
    266 	LIST_INSERT_HEAD(&msipic_list, msipic, mp_list);
    267 
    268 	mutex_exit(&msipic_list_lock);
    269 
    270 	return pic;
    271 }
    272 
    273 static void
    274 msipic_destruct_common_msi_pic(struct pic *msi_pic)
    275 {
    276 	struct msipic *msipic;
    277 
    278 	if (msi_pic == NULL)
    279 		return;
    280 
    281 	msipic = msi_pic->pic_msipic;
    282 	mutex_enter(&msipic_list_lock);
    283 	LIST_REMOVE(msipic, mp_list);
    284 	msipic_release_common_msi_devid(msipic->mp_devid);
    285 	mutex_exit(&msipic_list_lock);
    286 
    287 	kmem_free(msipic, sizeof(*msipic));
    288 	kmem_free(msi_pic, sizeof(*msi_pic));
    289 }
    290 
    291 /*
    292  * The pic is MSI/MSI-X pic or not.
    293  */
    294 bool
    295 msipic_is_msi_pic(struct pic *pic)
    296 {
    297 
    298 	return (pic->pic_msipic != NULL);
    299 }
    300 
    301 /*
    302  * Return the MSI/MSI-X devid which is unique for each devices.
    303  */
    304 int
    305 msipic_get_devid(struct pic *pic)
    306 {
    307 
    308 	KASSERT(msipic_is_msi_pic(pic));
    309 
    310 	return pic->pic_msipic->mp_devid;
    311 }
    312 
    313 #define MSI_MSICTL_ENABLE 1
    314 #define MSI_MSICTL_DISABLE 0
    315 static void
    316 msi_set_msictl_enablebit(struct pic *pic, int msi_vec, int flag)
    317 {
    318 	pci_chipset_tag_t pc;
    319 	struct pci_attach_args *pa;
    320 	pcitag_t tag;
    321 	pcireg_t ctl;
    322 	int off, err __diagused;
    323 
    324 	pc = NULL;
    325 	pa = &pic->pic_msipic->mp_pa;
    326 	tag = pa->pa_tag;
    327 	err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
    328 	KASSERT(err != 0);
    329 
    330 	/*
    331 	 * MSI can establish only one vector at once.
    332 	 * So, use whole device mask bit instead of a vector mask bit.
    333 	 */
    334 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    335 	if (flag == MSI_MSICTL_ENABLE)
    336 		ctl |= PCI_MSI_CTL_MSI_ENABLE;
    337 	else
    338 		ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    339 
    340 	pci_conf_write(pc, tag, off, ctl);
    341 }
    342 
    343 static void
    344 msi_hwmask(struct pic *pic, int msi_vec)
    345 {
    346 
    347 	msi_set_msictl_enablebit(pic, msi_vec, MSI_MSICTL_DISABLE);
    348 }
    349 
    350 /*
    351  * Do not use pic->hwunmask() immediately after pic->delroute().
    352  * It is required to use pic->addroute() before pic->hwunmask().
    353  */
    354 static void
    355 msi_hwunmask(struct pic *pic, int msi_vec)
    356 {
    357 
    358 	msi_set_msictl_enablebit(pic, msi_vec, MSI_MSICTL_ENABLE);
    359 }
    360 
    361 static void
    362 msi_addroute(struct pic *pic, struct cpu_info *ci,
    363 	     int unused, int idt_vec, int type)
    364 {
    365 	pci_chipset_tag_t pc;
    366 	struct pci_attach_args *pa;
    367 	pcitag_t tag;
    368 	pcireg_t addr, data, ctl;
    369 	int off, err __diagused;
    370 
    371 	pc = NULL;
    372 	pa = &pic->pic_msipic->mp_pa;
    373 	tag = pa->pa_tag;
    374 	err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
    375 	KASSERT(err != 0);
    376 
    377 	/*
    378 	 * See Intel 64 and IA-32 Architectures Software Developer's Manual
    379 	 * Volume 3 10.11 Message Signalled Interrupts.
    380 	 */
    381 	/*
    382 	 * "cpuid" for MSI address is local APIC ID. In NetBSD, the ID is
    383 	 * the same as ci->ci_cpuid.
    384 	 */
    385 	addr = LAPIC_MSIADDR_BASE | __SHIFTIN(ci->ci_cpuid,
    386 	    LAPIC_MSIADDR_DSTID_MASK);
    387 	/* If trigger mode is edge, it don't care level for trigger mode. */
    388 	data = __SHIFTIN(idt_vec, LAPIC_VECTOR_MASK)
    389 		| LAPIC_TRIGMODE_EDGE | LAPIC_DLMODE_FIXED;
    390 
    391 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    392 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
    393 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO, addr);
    394 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI, 0);
    395 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, data);
    396 	} else {
    397 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR, addr);
    398 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA, data);
    399 	}
    400 	ctl |= PCI_MSI_CTL_MSI_ENABLE;
    401 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    402 }
    403 
    404 /*
    405  * Do not use pic->hwunmask() immediately after pic->delroute().
    406  * It is required to use pic->addroute() before pic->hwunmask().
    407  */
    408 static void
    409 msi_delroute(struct pic *pic, struct cpu_info *ci,
    410 	     int msi_vec, int idt_vec, int type)
    411 {
    412 
    413 	msi_hwmask(pic, msi_vec);
    414 }
    415 
    416 /*
    417  * Template for MSI pic.
    418  * .pic_msipic is set later in construct_msi_pic().
    419  */
    420 static struct pic msi_pic_tmpl = {
    421 	.pic_type = PIC_MSI,
    422 	.pic_vecbase = 0,
    423 	.pic_apicid = 0,
    424 	.pic_lock = __SIMPLELOCK_UNLOCKED, /* not used for msi_pic */
    425 	.pic_hwmask = msi_hwmask,
    426 	.pic_hwunmask = msi_hwunmask,
    427 	.pic_addroute = msi_addroute,
    428 	.pic_delroute = msi_delroute,
    429 };
    430 
    431 /*
    432  * Create pseudo pic for a MSI device.
    433  */
    434 struct pic *
    435 msipic_construct_msi_pic(const struct pci_attach_args *pa)
    436 {
    437 	struct pic *msi_pic;
    438 	char pic_name_buf[MSIPICNAMEBUF];
    439 
    440 	msi_pic = msipic_construct_common_msi_pic(pa, &msi_pic_tmpl);
    441 	if (msi_pic == NULL) {
    442 		DPRINTF(("cannot allocate MSI pic.\n"));
    443 		return NULL;
    444 	}
    445 
    446 	memset(pic_name_buf, 0, MSIPICNAMEBUF);
    447 	snprintf(pic_name_buf, MSIPICNAMEBUF, "msi%d",
    448 	    msi_pic->pic_msipic->mp_devid);
    449 	strncpy(msi_pic->pic_msipic->mp_pic_name, pic_name_buf,
    450 	    MSIPICNAMEBUF - 1);
    451 	msi_pic->pic_name = msi_pic->pic_msipic->mp_pic_name;
    452 
    453 	return msi_pic;
    454 }
    455 
    456 /*
    457  * Delete pseudo pic for a MSI device.
    458  */
    459 void
    460 msipic_destruct_msi_pic(struct pic *msi_pic)
    461 {
    462 
    463 	msipic_destruct_common_msi_pic(msi_pic);
    464 }
    465 
    466 #define MSIX_VECCTL_HWMASK 1
    467 #define MSIX_VECCTL_HWUNMASK 0
    468 static void
    469 msix_set_vecctl_mask(struct pic *pic, int msix_vec, int flag)
    470 {
    471 	bus_space_tag_t bstag;
    472 	bus_space_handle_t bshandle;
    473 	uint64_t entry_base;
    474 	uint32_t vecctl;
    475 
    476 	if (msix_vec < 0) {
    477 		DPRINTF(("%s: invalid MSI-X table index, devid=%d vecid=%d",
    478 			__func__, msipic_get_devid(pic), msix_vec));
    479 		return;
    480 	}
    481 
    482 	entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
    483 
    484 	bstag = pic->pic_msipic->mp_bstag;
    485 	bshandle = pic->pic_msipic->mp_bshandle;
    486 	vecctl = bus_space_read_4(bstag, bshandle,
    487 	    entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL);
    488 	if (flag == MSIX_VECCTL_HWMASK)
    489 		vecctl |= PCI_MSIX_VECTCTL_MASK;
    490 	else
    491 		vecctl &= ~PCI_MSIX_VECTCTL_MASK;
    492 
    493 	bus_space_write_4(bstag, bshandle,
    494 	    entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, vecctl);
    495 	BUS_SPACE_WRITE_FLUSH(bstag, bshandle);
    496 }
    497 
    498 static void
    499 msix_hwmask(struct pic *pic, int msix_vec)
    500 {
    501 
    502 	msix_set_vecctl_mask(pic, msix_vec, MSIX_VECCTL_HWMASK);
    503 }
    504 
    505 /*
    506  * Do not use pic->hwunmask() immediately after pic->delroute().
    507  * It is required to use pic->addroute() before pic->hwunmask().
    508  */
    509 static void
    510 msix_hwunmask(struct pic *pic, int msix_vec)
    511 {
    512 
    513 	msix_set_vecctl_mask(pic, msix_vec, MSIX_VECCTL_HWUNMASK);
    514 }
    515 
    516 static void
    517 msix_addroute(struct pic *pic, struct cpu_info *ci,
    518 	     int msix_vec, int idt_vec, int type)
    519 {
    520 	pci_chipset_tag_t pc;
    521 	struct pci_attach_args *pa;
    522 	pcitag_t tag;
    523 	bus_space_tag_t bstag;
    524 	bus_space_handle_t bshandle;
    525 	uint64_t entry_base;
    526 	pcireg_t addr, data, ctl;
    527 	int off, err __diagused;
    528 
    529 	if (msix_vec < 0) {
    530 		DPRINTF(("%s: invalid MSI-X table index, devid=%d vecid=%d",
    531 			__func__, msipic_get_devid(pic), msix_vec));
    532 		return;
    533 	}
    534 
    535 	pa = &pic->pic_msipic->mp_pa;
    536 	pc = pa->pa_pc;
    537 	tag = pa->pa_tag;
    538 	err = pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL);
    539 	KASSERT(err != 0);
    540 
    541 	entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
    542 
    543 	/*
    544 	 * See Intel 64 and IA-32 Architectures Software Developer's Manual
    545 	 * Volume 3 10.11 Message Signalled Interrupts.
    546 	 */
    547 	/*
    548 	 * "cpuid" for MSI-X address is local APIC ID. In NetBSD, the ID is
    549 	 * the same as ci->ci_cpuid.
    550 	 */
    551 	addr = LAPIC_MSIADDR_BASE | __SHIFTIN(ci->ci_cpuid,
    552 	    LAPIC_MSIADDR_DSTID_MASK);
    553 	/* If trigger mode is edge, it don't care level for trigger mode. */
    554 	data = __SHIFTIN(idt_vec, LAPIC_VECTOR_MASK)
    555 		| LAPIC_TRIGMODE_EDGE | LAPIC_DLMODE_FIXED;
    556 
    557 	bstag = pic->pic_msipic->mp_bstag;
    558 	bshandle = pic->pic_msipic->mp_bshandle;
    559 	bus_space_write_4(bstag, bshandle,
    560 	    entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, addr);
    561 	bus_space_write_4(bstag, bshandle,
    562 	    entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, 0);
    563 	bus_space_write_4(bstag, bshandle,
    564 	    entry_base + PCI_MSIX_TABLE_ENTRY_DATA, data);
    565 	bus_space_write_4(bstag, bshandle,
    566 	    entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
    567 	BUS_SPACE_WRITE_FLUSH(bstag, bshandle);
    568 
    569 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    570 	ctl |= PCI_MSIX_CTL_ENABLE;
    571 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    572 }
    573 
    574 /*
    575  * Do not use pic->hwunmask() immediately after pic->delroute().
    576  * It is required to use pic->addroute() before pic->hwunmask().
    577  */
    578 static void
    579 msix_delroute(struct pic *pic, struct cpu_info *ci,
    580 	     int msix_vec, int vec, int type)
    581 {
    582 
    583 	msix_hwmask(pic, msix_vec);
    584 }
    585 
    586 /*
    587  * Template for MSI-X pic.
    588  * .pic_msipic is set later in construct_msix_pic().
    589  */
    590 static struct pic msix_pic_tmpl = {
    591 	.pic_type = PIC_MSIX,
    592 	.pic_vecbase = 0,
    593 	.pic_apicid = 0,
    594 	.pic_lock = __SIMPLELOCK_UNLOCKED, /* not used for msix_pic */
    595 	.pic_hwmask = msix_hwmask,
    596 	.pic_hwunmask = msix_hwunmask,
    597 	.pic_addroute = msix_addroute,
    598 	.pic_delroute = msix_delroute,
    599 };
    600 
    601 struct pic *
    602 msipic_construct_msix_pic(const struct pci_attach_args *pa)
    603 {
    604 	struct pic *msix_pic;
    605 	pci_chipset_tag_t pc;
    606 	pcitag_t tag;
    607 	pcireg_t tbl;
    608 	bus_space_tag_t bstag;
    609 	bus_space_handle_t bshandle;
    610 	bus_size_t bssize;
    611 	size_t table_size;
    612 	uint32_t table_offset;
    613 	u_int memtype;
    614 	bus_addr_t memaddr;
    615 	int flags;
    616 	int bir, bar, err, off, table_nentry;
    617 	char pic_name_buf[MSIPICNAMEBUF];
    618 
    619 	table_nentry = pci_msix_count(pa->pa_pc, pa->pa_tag);
    620 	if (table_nentry == 0) {
    621 		DPRINTF(("MSI-X table entry is 0.\n"));
    622 		return NULL;
    623 	}
    624 
    625 	pc = pa->pa_pc;
    626 	tag = pa->pa_tag;
    627 	if (pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL) == 0) {
    628 		DPRINTF(("%s: no msix capability", __func__));
    629 		return NULL;
    630 	}
    631 
    632 	msix_pic = msipic_construct_common_msi_pic(pa, &msix_pic_tmpl);
    633 	if (msix_pic == NULL) {
    634 		DPRINTF(("cannot allocate MSI-X pic.\n"));
    635 		return NULL;
    636 	}
    637 
    638 	memset(pic_name_buf, 0, MSIPICNAMEBUF);
    639 	snprintf(pic_name_buf, MSIPICNAMEBUF, "msix%d",
    640 	    msix_pic->pic_msipic->mp_devid);
    641 	strncpy(msix_pic->pic_msipic->mp_pic_name, pic_name_buf,
    642 	    MSIPICNAMEBUF - 1);
    643 	msix_pic->pic_name = msix_pic->pic_msipic->mp_pic_name;
    644 
    645 	tbl = pci_conf_read(pc, tag, off + PCI_MSIX_TBLOFFSET);
    646 	table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
    647 	bir = tbl & PCI_MSIX_PBABIR_MASK;
    648 	switch (bir) {
    649 	case 0:
    650 		bar = PCI_BAR0;
    651 		break;
    652 	case 1:
    653 		bar = PCI_BAR1;
    654 		break;
    655 	case 2:
    656 		bar = PCI_BAR2;
    657 		break;
    658 	case 3:
    659 		bar = PCI_BAR3;
    660 		break;
    661 	case 4:
    662 		bar = PCI_BAR4;
    663 		break;
    664 	case 5:
    665 		bar = PCI_BAR5;
    666 		break;
    667 	default:
    668 		aprint_error("detect an illegal device! "
    669 		    "The device use reserved BIR values.\n");
    670 		msipic_destruct_common_msi_pic(msix_pic);
    671 		return NULL;
    672 	}
    673 	memtype = pci_mapreg_type(pc, tag, bar);
    674 	/*
    675 	 * PCI_MSIX_TABLE_ENTRY_SIZE consists below
    676 	 *     - Vector Control (32bit)
    677 	 *     - Message Data (32bit)
    678 	 *     - Message Upper Address (32bit)
    679 	 *     - Message Lower Address (32bit)
    680 	 */
    681 	table_size = table_nentry * PCI_MSIX_TABLE_ENTRY_SIZE;
    682 #if 0
    683 	err = pci_mapreg_submap(pa, bar, memtype, BUS_SPACE_MAP_LINEAR,
    684 	    roundup(table_size, PAGE_SIZE), table_offset,
    685 	    &bstag, &bshandle, NULL, &bssize);
    686 #else
    687 	/*
    688 	 * Workaround for PCI prefetchable bit. Some chips (e.g. Intel 82599)
    689 	 * report SERR and MSI-X doesn't work. This problem might not be the
    690 	 * driver's bug but our PCI common part or VMs' bug. Until we find a
    691 	 * real reason, we ignore the prefetchable bit.
    692 	 */
    693 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, memtype,
    694 		&memaddr, NULL, &flags) != 0) {
    695 		DPRINTF(("cannot get a map info.\n"));
    696 		msipic_destruct_common_msi_pic(msix_pic);
    697 		return NULL;
    698 	}
    699 	if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
    700 		DPRINTF(( "clear prefetchable bit\n"));
    701 		flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
    702 	}
    703 	bssize = roundup(table_size, PAGE_SIZE);
    704 	err = bus_space_map(pa->pa_memt, memaddr + table_offset, bssize, flags,
    705 	    &bshandle);
    706 	bstag = pa->pa_memt;
    707 #endif
    708 	if (err) {
    709 		DPRINTF(("cannot map msix table.\n"));
    710 		msipic_destruct_common_msi_pic(msix_pic);
    711 		return NULL;
    712 	}
    713 	msix_pic->pic_msipic->mp_bstag = bstag;
    714 	msix_pic->pic_msipic->mp_bshandle = bshandle;
    715 	msix_pic->pic_msipic->mp_bssize = bssize;
    716 
    717 	return msix_pic;
    718 }
    719 
    720 /*
    721  * Delete pseudo pic for a MSI-X device.
    722  */
    723 void
    724 msipic_destruct_msix_pic(struct pic *msix_pic)
    725 {
    726 	struct msipic *msipic;
    727 
    728 	KASSERT(msipic_is_msi_pic(msix_pic));
    729 	KASSERT(msix_pic->pic_type == PIC_MSIX);
    730 
    731 	msipic = msix_pic->pic_msipic;
    732 	bus_space_unmap(msipic->mp_bstag, msipic->mp_bshandle,
    733 	    msipic->mp_bssize);
    734 
    735 	msipic_destruct_common_msi_pic(msix_pic);
    736 }
    737 
    738 /*
    739  * Set the number of MSI vectors for pseudo MSI pic.
    740  */
    741 int
    742 msipic_set_msi_vectors(struct pic *msi_pic, pci_intr_handle_t *pihs,
    743     int count)
    744 {
    745 
    746 	KASSERT(msipic_is_msi_pic(msi_pic));
    747 
    748 	msi_pic->pic_msipic->mp_veccnt = count;
    749 	return 0;
    750 }
    751 
    752 /*
    753  * Initialize the system to use MSI/MSI-X.
    754  */
    755 void
    756 msipic_init(void)
    757 {
    758 
    759 	mutex_init(&msipic_list_lock, MUTEX_DEFAULT, IPL_NONE);
    760 }
    761