msipic.c revision 1.19 1 /* $NetBSD: msipic.c,v 1.19 2019/11/13 02:54:59 hikaru 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.19 2019/11/13 02:54:59 hikaru 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 #include <sys/bitops.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 number 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 msipic = kmem_zalloc(sizeof(*msipic), KM_SLEEP);
242
243 mutex_enter(&msipic_list_lock);
244
245 devid = msipic_allocate_common_msi_devid(pa);
246 if (devid == -1) {
247 mutex_exit(&msipic_list_lock);
248 kmem_free(pic, sizeof(*pic));
249 kmem_free(msipic, sizeof(*msipic));
250 return NULL;
251 }
252
253 memcpy(pic, pic_tmpl, sizeof(*pic));
254 pic->pic_edge_stubs
255 = x2apic_mode ? x2apic_edge_stubs : ioapic_edge_stubs;
256 pic->pic_msipic = msipic;
257 msipic->mp_pic = pic;
258 pci_decompose_tag(pa->pa_pc, pa->pa_tag,
259 &msipic->mp_bus, &msipic->mp_dev, &msipic->mp_fun);
260 memcpy(&msipic->mp_pa, pa, sizeof(msipic->mp_pa));
261 msipic->mp_devid = devid;
262 /*
263 * pci_msi{,x}_alloc() must be called only once in the device driver.
264 */
265 KASSERT(msipic_find_msi_pic_locked(msipic->mp_devid) == NULL);
266
267 LIST_INSERT_HEAD(&msipic_list, msipic, mp_list);
268
269 mutex_exit(&msipic_list_lock);
270
271 return pic;
272 }
273
274 static void
275 msipic_destruct_common_msi_pic(struct pic *msi_pic)
276 {
277 struct msipic *msipic;
278
279 if (msi_pic == NULL)
280 return;
281
282 msipic = msi_pic->pic_msipic;
283 mutex_enter(&msipic_list_lock);
284 LIST_REMOVE(msipic, mp_list);
285 msipic_release_common_msi_devid(msipic->mp_devid);
286 mutex_exit(&msipic_list_lock);
287
288 kmem_free(msipic, sizeof(*msipic));
289 kmem_free(msi_pic, sizeof(*msi_pic));
290 }
291
292 /*
293 * The pic is MSI/MSI-X pic or not.
294 */
295 bool
296 msipic_is_msi_pic(struct pic *pic)
297 {
298
299 return (pic->pic_msipic != NULL);
300 }
301
302 /*
303 * Return the MSI/MSI-X devid which is unique for each devices.
304 */
305 int
306 msipic_get_devid(struct pic *pic)
307 {
308
309 KASSERT(msipic_is_msi_pic(pic));
310
311 return pic->pic_msipic->mp_devid;
312 }
313
314 #define MSI_MSICTL_ENABLE 1
315 #define MSI_MSICTL_DISABLE 0
316 static void
317 msi_set_msictl_enablebit(struct pic *pic, int msi_vec, int flag)
318 {
319 pci_chipset_tag_t pc;
320 struct pci_attach_args *pa;
321 pcitag_t tag;
322 pcireg_t ctl;
323 int off, err __diagused;
324
325 pc = NULL;
326 pa = &pic->pic_msipic->mp_pa;
327 tag = pa->pa_tag;
328 err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
329 KASSERT(err != 0);
330
331 /*
332 * MSI can establish only one vector at once.
333 * So, use whole device mask bit instead of a vector mask bit.
334 */
335 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
336 if (flag == MSI_MSICTL_ENABLE)
337 ctl |= PCI_MSI_CTL_MSI_ENABLE;
338 else
339 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
340
341 pci_conf_write(pc, tag, off, ctl);
342 }
343
344 static void
345 msi_hwmask(struct pic *pic, int msi_vec)
346 {
347
348 msi_set_msictl_enablebit(pic, msi_vec, MSI_MSICTL_DISABLE);
349 }
350
351 /*
352 * Do not use pic->hwunmask() immediately after pic->delroute().
353 * It is required to use pic->addroute() before pic->hwunmask().
354 */
355 static void
356 msi_hwunmask(struct pic *pic, int msi_vec)
357 {
358
359 msi_set_msictl_enablebit(pic, msi_vec, MSI_MSICTL_ENABLE);
360 }
361
362 static void
363 msi_addroute(struct pic *pic, struct cpu_info *ci,
364 int unused, int idt_vec, int type)
365 {
366 pci_chipset_tag_t pc;
367 struct pci_attach_args *pa;
368 pcitag_t tag;
369 pcireg_t addr, data, ctl;
370 int off, err __diagused;
371
372 pc = NULL;
373 pa = &pic->pic_msipic->mp_pa;
374 tag = pa->pa_tag;
375 err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
376 KASSERT(err != 0);
377
378 /*
379 * See Intel 64 and IA-32 Architectures Software Developer's Manual
380 * Volume 3 10.11 Message Signalled Interrupts.
381 */
382 /*
383 * "cpuid" for MSI address is local APIC ID. In NetBSD, the ID is
384 * the same as ci->ci_cpuid.
385 */
386 addr = LAPIC_MSIADDR_BASE | __SHIFTIN(ci->ci_cpuid,
387 LAPIC_MSIADDR_DSTID_MASK);
388 /* If trigger mode is edge, it don't care level for trigger mode. */
389 data = __SHIFTIN(idt_vec, LAPIC_VECTOR_MASK)
390 | LAPIC_TRIGMODE_EDGE | LAPIC_DLMODE_FIXED;
391
392 /*
393 * The size of the message data register is 16bit if the extended
394 * message data is not implemented. If it's 16bit and the per-vector
395 * masking is not capable, the location of the upper 16bit is out of
396 * the MSI capability structure's range. The PCI spec says the upper
397 * 16bit is driven to 0 if the message data register is 16bit. It's the
398 * spec, so it's OK just to write it regardless of the value of the
399 * upper 16bit.
400 */
401 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
402 if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
403 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO, addr);
404 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI, 0);
405 pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, data);
406 } else {
407 pci_conf_write(pc, tag, off + PCI_MSI_MADDR, addr);
408 pci_conf_write(pc, tag, off + PCI_MSI_MDATA, data);
409 }
410 ctl |= PCI_MSI_CTL_MSI_ENABLE;
411 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
412 }
413
414 /*
415 * Do not use pic->hwunmask() immediately after pic->delroute().
416 * It is required to use pic->addroute() before pic->hwunmask().
417 */
418 static void
419 msi_delroute(struct pic *pic, struct cpu_info *ci,
420 int msi_vec, int idt_vec, int type)
421 {
422
423 msi_hwmask(pic, msi_vec);
424 }
425
426 /*
427 * Template for MSI pic.
428 * .pic_msipic is set later in construct_msi_pic().
429 */
430 static struct pic msi_pic_tmpl = {
431 .pic_type = PIC_MSI,
432 .pic_vecbase = 0,
433 .pic_apicid = 0,
434 .pic_lock = __SIMPLELOCK_UNLOCKED, /* not used for msi_pic */
435 .pic_hwmask = msi_hwmask,
436 .pic_hwunmask = msi_hwunmask,
437 .pic_addroute = msi_addroute,
438 .pic_delroute = msi_delroute,
439 };
440
441 /*
442 * Create pseudo pic for a MSI device.
443 */
444 struct pic *
445 msipic_construct_msi_pic(const struct pci_attach_args *pa)
446 {
447 struct pic *msi_pic;
448 char pic_name_buf[MSIPICNAMEBUF];
449
450 msi_pic = msipic_construct_common_msi_pic(pa, &msi_pic_tmpl);
451 if (msi_pic == NULL) {
452 DPRINTF(("cannot allocate MSI pic.\n"));
453 return NULL;
454 }
455
456 memset(pic_name_buf, 0, MSIPICNAMEBUF);
457 snprintf(pic_name_buf, MSIPICNAMEBUF, "msi%d",
458 msi_pic->pic_msipic->mp_devid);
459 strncpy(msi_pic->pic_msipic->mp_pic_name, pic_name_buf,
460 MSIPICNAMEBUF - 1);
461 msi_pic->pic_name = msi_pic->pic_msipic->mp_pic_name;
462
463 return msi_pic;
464 }
465
466 /*
467 * Delete pseudo pic for a MSI device.
468 */
469 void
470 msipic_destruct_msi_pic(struct pic *msi_pic)
471 {
472
473 msipic_destruct_common_msi_pic(msi_pic);
474 }
475
476 #define MSIX_VECCTL_HWMASK 1
477 #define MSIX_VECCTL_HWUNMASK 0
478 static void
479 msix_set_vecctl_mask(struct pic *pic, int msix_vec, int flag)
480 {
481 bus_space_tag_t bstag;
482 bus_space_handle_t bshandle;
483 uint64_t entry_base;
484 uint32_t vecctl;
485
486 if (msix_vec < 0) {
487 DPRINTF(("%s: invalid MSI-X table index, devid=%d vecid=%d",
488 __func__, msipic_get_devid(pic), msix_vec));
489 return;
490 }
491
492 entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
493
494 bstag = pic->pic_msipic->mp_bstag;
495 bshandle = pic->pic_msipic->mp_bshandle;
496 vecctl = bus_space_read_4(bstag, bshandle,
497 entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL);
498 if (flag == MSIX_VECCTL_HWMASK)
499 vecctl |= PCI_MSIX_VECTCTL_MASK;
500 else
501 vecctl &= ~PCI_MSIX_VECTCTL_MASK;
502
503 bus_space_write_4(bstag, bshandle,
504 entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, vecctl);
505 BUS_SPACE_WRITE_FLUSH(bstag, bshandle);
506 }
507
508 static void
509 msix_hwmask(struct pic *pic, int msix_vec)
510 {
511
512 msix_set_vecctl_mask(pic, msix_vec, MSIX_VECCTL_HWMASK);
513 }
514
515 /*
516 * Do not use pic->hwunmask() immediately after pic->delroute().
517 * It is required to use pic->addroute() before pic->hwunmask().
518 */
519 static void
520 msix_hwunmask(struct pic *pic, int msix_vec)
521 {
522
523 msix_set_vecctl_mask(pic, msix_vec, MSIX_VECCTL_HWUNMASK);
524 }
525
526 static void
527 msix_addroute(struct pic *pic, struct cpu_info *ci,
528 int msix_vec, int idt_vec, int type)
529 {
530 pci_chipset_tag_t pc;
531 struct pci_attach_args *pa;
532 pcitag_t tag;
533 bus_space_tag_t bstag;
534 bus_space_handle_t bshandle;
535 uint64_t entry_base;
536 pcireg_t addr, data, ctl;
537 int off, err __diagused;
538
539 if (msix_vec < 0) {
540 DPRINTF(("%s: invalid MSI-X table index, devid=%d vecid=%d",
541 __func__, msipic_get_devid(pic), msix_vec));
542 return;
543 }
544
545 pa = &pic->pic_msipic->mp_pa;
546 pc = pa->pa_pc;
547 tag = pa->pa_tag;
548 err = pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL);
549 KASSERT(err != 0);
550
551 /* Disable MSI-X before writing MSI-X table */
552 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
553 ctl &= ~PCI_MSIX_CTL_ENABLE;
554 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
555
556 entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
557
558 /*
559 * See Intel 64 and IA-32 Architectures Software Developer's Manual
560 * Volume 3 10.11 Message Signalled Interrupts.
561 */
562 /*
563 * "cpuid" for MSI-X address is local APIC ID. In NetBSD, the ID is
564 * the same as ci->ci_cpuid.
565 */
566 addr = LAPIC_MSIADDR_BASE | __SHIFTIN(ci->ci_cpuid,
567 LAPIC_MSIADDR_DSTID_MASK);
568 /* If trigger mode is edge, it don't care level for trigger mode. */
569 data = __SHIFTIN(idt_vec, LAPIC_VECTOR_MASK)
570 | LAPIC_TRIGMODE_EDGE | LAPIC_DLMODE_FIXED;
571
572 bstag = pic->pic_msipic->mp_bstag;
573 bshandle = pic->pic_msipic->mp_bshandle;
574 bus_space_write_4(bstag, bshandle,
575 entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, addr);
576 bus_space_write_4(bstag, bshandle,
577 entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, 0);
578 bus_space_write_4(bstag, bshandle,
579 entry_base + PCI_MSIX_TABLE_ENTRY_DATA, data);
580 bus_space_write_4(bstag, bshandle,
581 entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
582 BUS_SPACE_WRITE_FLUSH(bstag, bshandle);
583
584 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
585 ctl |= PCI_MSIX_CTL_ENABLE;
586 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
587 }
588
589 /*
590 * Do not use pic->hwunmask() immediately after pic->delroute().
591 * It is required to use pic->addroute() before pic->hwunmask().
592 */
593 static void
594 msix_delroute(struct pic *pic, struct cpu_info *ci,
595 int msix_vec, int vec, int type)
596 {
597
598 msix_hwmask(pic, msix_vec);
599 }
600
601 /*
602 * Template for MSI-X pic.
603 * .pic_msipic is set later in construct_msix_pic().
604 */
605 static struct pic msix_pic_tmpl = {
606 .pic_type = PIC_MSIX,
607 .pic_vecbase = 0,
608 .pic_apicid = 0,
609 .pic_lock = __SIMPLELOCK_UNLOCKED, /* not used for msix_pic */
610 .pic_hwmask = msix_hwmask,
611 .pic_hwunmask = msix_hwunmask,
612 .pic_addroute = msix_addroute,
613 .pic_delroute = msix_delroute,
614 };
615
616 struct pic *
617 msipic_construct_msix_pic(const struct pci_attach_args *pa)
618 {
619 struct pic *msix_pic;
620 pci_chipset_tag_t pc;
621 pcitag_t tag;
622 pcireg_t tbl;
623 bus_space_tag_t bstag;
624 bus_space_handle_t bshandle;
625 bus_size_t bssize;
626 size_t table_size;
627 uint32_t table_offset;
628 u_int memtype;
629 bus_addr_t memaddr;
630 int flags;
631 int bir, bar, err, off, table_nentry;
632 char pic_name_buf[MSIPICNAMEBUF];
633
634 table_nentry = pci_msix_count(pa->pa_pc, pa->pa_tag);
635 if (table_nentry == 0) {
636 DPRINTF(("MSI-X table entry is 0.\n"));
637 return NULL;
638 }
639
640 pc = pa->pa_pc;
641 tag = pa->pa_tag;
642 if (pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL) == 0) {
643 DPRINTF(("%s: no msix capability", __func__));
644 return NULL;
645 }
646
647 msix_pic = msipic_construct_common_msi_pic(pa, &msix_pic_tmpl);
648 if (msix_pic == NULL) {
649 DPRINTF(("cannot allocate MSI-X pic.\n"));
650 return NULL;
651 }
652
653 memset(pic_name_buf, 0, MSIPICNAMEBUF);
654 snprintf(pic_name_buf, MSIPICNAMEBUF, "msix%d",
655 msix_pic->pic_msipic->mp_devid);
656 strncpy(msix_pic->pic_msipic->mp_pic_name, pic_name_buf,
657 MSIPICNAMEBUF - 1);
658 msix_pic->pic_name = msix_pic->pic_msipic->mp_pic_name;
659
660 tbl = pci_conf_read(pc, tag, off + PCI_MSIX_TBLOFFSET);
661 table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
662 bir = tbl & PCI_MSIX_PBABIR_MASK;
663 switch (bir) {
664 case 0:
665 bar = PCI_BAR0;
666 break;
667 case 1:
668 bar = PCI_BAR1;
669 break;
670 case 2:
671 bar = PCI_BAR2;
672 break;
673 case 3:
674 bar = PCI_BAR3;
675 break;
676 case 4:
677 bar = PCI_BAR4;
678 break;
679 case 5:
680 bar = PCI_BAR5;
681 break;
682 default:
683 aprint_error("detect an illegal device! "
684 "The device use reserved BIR values.\n");
685 msipic_destruct_common_msi_pic(msix_pic);
686 return NULL;
687 }
688 memtype = pci_mapreg_type(pc, tag, bar);
689 /*
690 * PCI_MSIX_TABLE_ENTRY_SIZE consists below
691 * - Vector Control (32bit)
692 * - Message Data (32bit)
693 * - Message Upper Address (32bit)
694 * - Message Lower Address (32bit)
695 */
696 table_size = table_nentry * PCI_MSIX_TABLE_ENTRY_SIZE;
697 #if 0
698 err = pci_mapreg_submap(pa, bar, memtype, BUS_SPACE_MAP_LINEAR,
699 roundup(table_size, PAGE_SIZE), table_offset,
700 &bstag, &bshandle, NULL, &bssize);
701 #else
702 /*
703 * Workaround for PCI prefetchable bit. Some chips (e.g. Intel 82599)
704 * report SERR and MSI-X doesn't work. This problem might not be the
705 * driver's bug but our PCI common part or VMs' bug. Until we find a
706 * real reason, we ignore the prefetchable bit.
707 */
708 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, memtype,
709 &memaddr, NULL, &flags) != 0) {
710 DPRINTF(("cannot get a map info.\n"));
711 msipic_destruct_common_msi_pic(msix_pic);
712 return NULL;
713 }
714 if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
715 DPRINTF(( "clear prefetchable bit\n"));
716 flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
717 }
718 bssize = roundup(table_size, PAGE_SIZE);
719 err = _x86_memio_map(pa->pa_memt, memaddr + table_offset, bssize, flags,
720 &bshandle);
721 bstag = pa->pa_memt;
722 #endif
723 if (err) {
724 DPRINTF(("cannot map msix table.\n"));
725 msipic_destruct_common_msi_pic(msix_pic);
726 return NULL;
727 }
728 msix_pic->pic_msipic->mp_bstag = bstag;
729 msix_pic->pic_msipic->mp_bshandle = bshandle;
730 msix_pic->pic_msipic->mp_bssize = bssize;
731
732 return msix_pic;
733 }
734
735 /*
736 * Delete pseudo pic for a MSI-X device.
737 */
738 void
739 msipic_destruct_msix_pic(struct pic *msix_pic)
740 {
741 struct msipic *msipic;
742
743 KASSERT(msipic_is_msi_pic(msix_pic));
744 KASSERT(msix_pic->pic_type == PIC_MSIX);
745
746 msipic = msix_pic->pic_msipic;
747 _x86_memio_unmap(msipic->mp_bstag, msipic->mp_bshandle,
748 msipic->mp_bssize, NULL);
749
750 msipic_destruct_common_msi_pic(msix_pic);
751 }
752
753 /*
754 * Set the number of MSI vectors for pseudo MSI pic.
755 */
756 int
757 msipic_set_msi_vectors(struct pic *msi_pic, pci_intr_handle_t *pihs,
758 int count)
759 {
760
761 KASSERT(msipic_is_msi_pic(msi_pic));
762
763 if (msi_pic->pic_type == PIC_MSI) {
764 pci_chipset_tag_t pc;
765 struct pci_attach_args *pa;
766 pcitag_t tag;
767 int off, err __diagused;
768 pcireg_t ctl;
769
770 pc = NULL;
771 pa = &msi_pic->pic_msipic->mp_pa;
772 tag = pa->pa_tag;
773 err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
774 KASSERT(err != 0);
775
776 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
777 ctl &= ~PCI_MSI_CTL_MME_MASK;
778 ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
779 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
780 }
781
782 msi_pic->pic_msipic->mp_veccnt = count;
783 return 0;
784 }
785
786 /*
787 * Initialize the system to use MSI/MSI-X.
788 */
789 void
790 msipic_init(void)
791 {
792
793 mutex_init(&msipic_list_lock, MUTEX_DEFAULT, IPL_NONE);
794 }
795