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