pci_msi_machdep.c revision 1.8 1 /* $NetBSD: pci_msi_machdep.c,v 1.8 2015/08/13 04:39:33 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 /*
30 * TODO
31 *
32 * - PBA (Pending Bit Array) support
33 * - HyperTransport mapping support
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pci_msi_machdep.c,v 1.8 2015/08/13 04:39:33 msaitoh Exp $");
38
39 #include "opt_intrdebug.h"
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/systm.h>
45 #include <sys/cpu.h>
46 #include <sys/errno.h>
47 #include <sys/device.h>
48 #include <sys/intr.h>
49 #include <sys/kmem.h>
50 #include <sys/malloc.h>
51
52 #include <dev/pci/pcivar.h>
53
54 #include <machine/i82093var.h>
55 #include <machine/pic.h>
56
57 #include <x86/pci/msipic.h>
58 #include <x86/pci/pci_msi_machdep.h>
59
60 #ifdef INTRDEBUG
61 #define MSIDEBUG
62 #endif
63
64 #ifdef MSIDEBUG
65 #define DPRINTF(msg) printf msg
66 #else
67 #define DPRINTF(msg)
68 #endif
69
70 static pci_intr_handle_t
71 pci_msi_calculate_handle(struct pic *msi_pic, int vector)
72 {
73 pci_intr_handle_t pih;
74
75 KASSERT(msipic_is_msi_pic(msi_pic));
76
77 pih = __SHIFTIN((uint64_t)msipic_get_devid(msi_pic), MSI_INT_DEV_MASK)
78 | __SHIFTIN((uint64_t)vector, MSI_INT_VEC_MASK)
79 | APIC_INT_VIA_MSI;
80 if (msi_pic->pic_type == PIC_MSI)
81 MSI_INT_MAKE_MSI(pih);
82 else if (msi_pic->pic_type == PIC_MSIX)
83 MSI_INT_MAKE_MSIX(pih);
84 else
85 panic("%s: Unexpected pic_type: %d\n", __func__,
86 msi_pic->pic_type);
87
88 return pih;
89 }
90
91 static pci_intr_handle_t *
92 pci_msi_alloc_vectors(struct pic *msi_pic, uint *table_indexes, int *count)
93 {
94 struct intrsource *isp;
95 pci_intr_handle_t *vectors, pih;
96 int i;
97 const char *intrstr;
98 char intrstr_buf[INTRIDBUF];
99
100 vectors = kmem_zalloc(sizeof(vectors[0]) * (*count), KM_SLEEP);
101 if (vectors == NULL) {
102 DPRINTF(("cannot allocate vectors\n"));
103 return NULL;
104 }
105
106 mutex_enter(&cpu_lock);
107 for (i = 0; i < *count; i++) {
108 u_int table_index;
109
110 if (table_indexes == NULL)
111 table_index = i;
112 else
113 table_index = table_indexes[i];
114
115 pih = pci_msi_calculate_handle(msi_pic, table_index);
116
117 intrstr = x86_pci_msi_string(NULL, pih, intrstr_buf,
118 sizeof(intrstr_buf));
119 isp = intr_allocate_io_intrsource(intrstr);
120 if (isp == NULL) {
121 mutex_exit(&cpu_lock);
122 DPRINTF(("can't allocate io_intersource\n"));
123 kmem_free(vectors, sizeof(vectors[0]) * (*count));
124 return NULL;
125 }
126
127 vectors[i] = pih;
128 }
129 mutex_exit(&cpu_lock);
130
131 return vectors;
132 }
133
134 static void
135 pci_msi_free_vectors(struct pic *msi_pic, pci_intr_handle_t *pihs, int count)
136 {
137 pci_intr_handle_t pih;
138 int i;
139 const char *intrstr;
140 char intrstr_buf[INTRIDBUF];
141
142 mutex_enter(&cpu_lock);
143 for (i = 0; i < count; i++) {
144 pih = pci_msi_calculate_handle(msi_pic, i);
145 intrstr = x86_pci_msi_string(NULL, pih, intrstr_buf,
146 sizeof(intrstr_buf));
147 intr_free_io_intrsource(intrstr);
148 }
149 mutex_exit(&cpu_lock);
150
151 kmem_free(pihs, sizeof(pihs[0]) * count);
152 }
153
154 static int
155 pci_msi_alloc_common(pci_intr_handle_t **ihps, int *count,
156 const struct pci_attach_args *pa, bool exact)
157 {
158 struct pic *msi_pic;
159 pci_intr_handle_t *vectors;
160 int error, i;
161
162 if ((pa->pa_flags & PCI_FLAGS_MSI_OKAY) == 0) {
163 DPRINTF(("PCI host bridge does not support MSI.\n"));
164 return ENODEV;
165 }
166
167 msi_pic = msipic_construct_msi_pic(pa);
168 if (msi_pic == NULL) {
169 DPRINTF(("cannot allocate MSI pic.\n"));
170 return EINVAL;
171 }
172
173 vectors = NULL;
174 while (*count > 0) {
175 vectors = pci_msi_alloc_vectors(msi_pic, NULL, count);
176 if (vectors != NULL)
177 break;
178
179 if (exact) {
180 DPRINTF(("cannot allocate MSI vectors.\n"));
181 msipic_destruct_msi_pic(msi_pic);
182 return ENOMEM;
183 } else {
184 (*count) >>= 1; /* must be power of 2. */
185 continue;
186 }
187 }
188 if (vectors == NULL) {
189 DPRINTF(("cannot allocate MSI vectors.\n"));
190 msipic_destruct_msi_pic(msi_pic);
191 return ENOMEM;
192 }
193
194 for (i = 0; i < *count; i++) {
195 MSI_INT_MAKE_MSI(vectors[i]);
196 }
197
198 error = msipic_set_msi_vectors(msi_pic, NULL, *count);
199 if (error) {
200 pci_msi_free_vectors(msi_pic, vectors, *count);
201 msipic_destruct_msi_pic(msi_pic);
202 return error;
203 }
204
205 *ihps = vectors;
206 return 0;
207 }
208
209 static void *
210 pci_msi_common_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
211 int level, int (*func)(void *), void *arg, struct pic *pic)
212 {
213 int irq, pin;
214 bool mpsafe;
215
216 KASSERT(INT_VIA_MSI(ih));
217
218 irq = -1;
219 pin = MSI_INT_VEC(ih);
220 mpsafe = ((ih & MPSAFE_MASK) != 0);
221
222 return intr_establish(irq, pic, pin, IST_EDGE, level, func, arg,
223 mpsafe);
224 }
225
226 static void
227 pci_msi_common_disestablish(pci_chipset_tag_t pc, void *cookie)
228 {
229
230 intr_disestablish(cookie);
231 }
232
233 static int
234 pci_msix_alloc_common(pci_intr_handle_t **ihps, u_int *table_indexes,
235 int *count, const struct pci_attach_args *pa, bool exact)
236 {
237 struct pic *msix_pic;
238 pci_intr_handle_t *vectors;
239 int error, i;
240
241 if ((pa->pa_flags & PCI_FLAGS_MSIX_OKAY) == 0) {
242 DPRINTF(("PCI host bridge does not support MSI-X.\n"));
243 return ENODEV;
244 }
245
246 msix_pic = msipic_construct_msix_pic(pa);
247 if (msix_pic == NULL)
248 return EINVAL;
249
250 vectors = NULL;
251 while (*count > 0) {
252 vectors = pci_msi_alloc_vectors(msix_pic, table_indexes, count);
253 if (vectors != NULL)
254 break;
255
256 if (exact) {
257 DPRINTF(("cannot allocate MSI-X vectors.\n"));
258 msipic_destruct_msix_pic(msix_pic);
259 return ENOMEM;
260 } else {
261 (*count)--;
262 continue;
263 }
264 }
265 if (vectors == NULL) {
266 DPRINTF(("cannot allocate MSI-X vectors.\n"));
267 msipic_destruct_msix_pic(msix_pic);
268 return ENOMEM;
269 }
270
271 for (i = 0; i < *count; i++) {
272 MSI_INT_MAKE_MSIX(vectors[i]);
273 }
274
275 error = msipic_set_msi_vectors(msix_pic, vectors, *count);
276 if (error) {
277 pci_msi_free_vectors(msix_pic, vectors, *count);
278 msipic_destruct_msix_pic(msix_pic);
279 return error;
280 }
281
282 *ihps = vectors;
283 return 0;
284 }
285
286 static int
287 x86_pci_msi_alloc(pci_intr_handle_t **ihps, int *count,
288 const struct pci_attach_args *pa)
289 {
290
291 return pci_msi_alloc_common(ihps, count, pa, false);
292 }
293
294 static int
295 x86_pci_msi_alloc_exact(pci_intr_handle_t **ihps, int count,
296 const struct pci_attach_args *pa)
297 {
298
299 return pci_msi_alloc_common(ihps, &count, pa, true);
300 }
301
302 static void
303 x86_pci_msi_release_internal(pci_intr_handle_t *pihs, int count)
304 {
305 struct pic *pic;
306
307 pic = msipic_find_msi_pic(MSI_INT_DEV(pihs[0]));
308 if (pic == NULL)
309 return;
310
311 pci_msi_free_vectors(pic, pihs, count);
312 msipic_destruct_msi_pic(pic);
313 }
314
315 static int
316 x86_pci_msix_alloc(pci_intr_handle_t **ihps, int *count,
317 const struct pci_attach_args *pa)
318 {
319
320 return pci_msix_alloc_common(ihps, NULL, count, pa, false);
321 }
322
323 static int
324 x86_pci_msix_alloc_exact(pci_intr_handle_t **ihps, int count,
325 const struct pci_attach_args *pa)
326 {
327
328 return pci_msix_alloc_common(ihps, NULL, &count, pa, true);
329 }
330
331 static int
332 x86_pci_msix_alloc_map(pci_intr_handle_t **ihps, u_int *table_indexes,
333 int count, const struct pci_attach_args *pa)
334 {
335
336 return pci_msix_alloc_common(ihps, table_indexes, &count, pa, true);
337 }
338
339 static void
340 x86_pci_msix_release_internal(pci_intr_handle_t *pihs, int count)
341 {
342 struct pic *pic;
343
344 pic = msipic_find_msi_pic(MSI_INT_DEV(pihs[0]));
345 if (pic == NULL)
346 return;
347
348 pci_msi_free_vectors(pic, pihs, count);
349 msipic_destruct_msix_pic(pic);
350 }
351
352 /*****************************************************************************/
353 /*
354 * extern for MD code.
355 */
356
357 /*
358 * Return intrid for a MSI/MSI-X device.
359 * "buf" must be allocated by caller.
360 */
361 const char *
362 x86_pci_msi_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
363 size_t len)
364 {
365 int dev, vec;
366
367 KASSERT(INT_VIA_MSI(ih));
368
369 dev = MSI_INT_DEV(ih);
370 vec = MSI_INT_VEC(ih);
371 if (MSI_INT_IS_MSIX(ih))
372 snprintf(buf, len, "msix%d vec %d", dev, vec);
373 else
374 snprintf(buf, len, "msi%d vec %d", dev, vec);
375
376 return buf;
377 }
378
379 /*
380 * Release MSI handles.
381 */
382 void
383 x86_pci_msi_release(pci_chipset_tag_t pc, pci_intr_handle_t *pihs, int count)
384 {
385
386 if (count < 1)
387 return;
388
389 return x86_pci_msi_release_internal(pihs, count);
390 }
391
392 /*
393 * Establish a MSI handle.
394 * If multiple MSI handle is requied to establish, device driver must call
395 * this function for each handle.
396 */
397 void *
398 x86_pci_msi_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
399 int level, int (*func)(void *), void *arg)
400 {
401 struct pic *pic;
402
403 pic = msipic_find_msi_pic(MSI_INT_DEV(ih));
404 if (pic == NULL) {
405 DPRINTF(("pci_intr_handler has no msi_pic\n"));
406 return NULL;
407 }
408
409 return pci_msi_common_establish(pc, ih, level, func, arg, pic);
410 }
411
412 /*
413 * Disestablish a MSI handle.
414 * If multiple MSI handle is requied to disestablish, device driver must call
415 * this function for each handle.
416 */
417 void
418 x86_pci_msi_disestablish(pci_chipset_tag_t pc, void *cookie)
419 {
420
421 pci_msi_common_disestablish(pc, cookie);
422 }
423
424 /*
425 * Release MSI-X handles.
426 */
427 void
428 x86_pci_msix_release(pci_chipset_tag_t pc, pci_intr_handle_t *pihs, int count)
429 {
430
431 if (count < 1)
432 return;
433
434 return x86_pci_msix_release_internal(pihs, count);
435 }
436
437 /*
438 * Establish a MSI-X handle.
439 * If multiple MSI-X handle is requied to establish, device driver must call
440 * this function for each handle.
441 */
442 void *
443 x86_pci_msix_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
444 int level, int (*func)(void *), void *arg)
445 {
446 struct pic *pic;
447
448 pic = msipic_find_msi_pic(MSI_INT_DEV(ih));
449 if (pic == NULL) {
450 DPRINTF(("pci_intr_handler has no msi_pic\n"));
451 return NULL;
452 }
453
454 return pci_msi_common_establish(pc, ih, level, func, arg, pic);
455 }
456
457 /*
458 * Disestablish a MSI-X handle.
459 * If multiple MSI-X handle is requied to disestablish, device driver must call
460 * this function for each handle.
461 */
462 void
463 x86_pci_msix_disestablish(pci_chipset_tag_t pc, void *cookie)
464 {
465
466 pci_msi_common_disestablish(pc, cookie);
467 }
468
469 /*****************************************************************************/
470 /*
471 * extern for MI code.
472 */
473
474 /*
475 * This function is used by device drivers like pci_intr_map().
476 *
477 * "ihps" is the array of vector numbers which MSI used instead of IRQ number.
478 * "count" must be power of 2.
479 * "count" can decrease if struct intrsource cannot be allocated.
480 * if count == 0, return non-zero value.
481 */
482 int
483 pci_msi_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
484 int *count)
485 {
486 int hw_max;
487
488 /* MSI vector count must be power of 2. */
489 KASSERT(*count > 0);
490 KASSERT(((*count - 1) & *count) == 0);
491
492 hw_max = pci_msi_count(pa->pa_pc, pa->pa_tag);
493 if (hw_max == 0)
494 return ENODEV;
495
496 if (*count > hw_max) {
497 DPRINTF(("cut off MSI count to %d\n", hw_max));
498 *count = hw_max; /* cut off hw_max */
499 }
500
501 return x86_pci_msi_alloc(ihps, count, pa);
502 }
503
504 /*
505 * This function is used by device drivers like pci_intr_map().
506 *
507 * "ihps" is the array of vector numbers which MSI used instead of IRQ number.
508 * "count" must be power of 2.
509 * "count" can not decrease.
510 * If "count" struct intrsources cannot be allocated, return non-zero value.
511 */
512 int
513 pci_msi_alloc_exact(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
514 int count)
515 {
516 int hw_max;
517
518 /* MSI vector count must be power of 2. */
519 KASSERT(count > 0);
520 KASSERT(((count - 1) & count) == 0);
521
522 hw_max = pci_msi_count(pa->pa_pc, pa->pa_tag);
523 if (hw_max == 0)
524 return ENODEV;
525
526 if (count > hw_max) {
527 DPRINTF(("over hardware max MSI count %d\n", hw_max));
528 return EINVAL;
529 }
530
531 return x86_pci_msi_alloc_exact(ihps, count, pa);
532 }
533
534 /*
535 * This function is used by device drivers like pci_intr_map().
536 *
537 * "ihps" is the array of vector numbers which MSI-X used instead of IRQ number.
538 * "count" can decrease if enough struct intrsources cannot be allocated.
539 * if count == 0, return non-zero value.
540 */
541 int
542 pci_msix_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
543 int *count)
544 {
545 int hw_max;
546
547 KASSERT(*count > 0);
548
549 hw_max = pci_msix_count(pa->pa_pc, pa->pa_tag);
550 if (hw_max == 0)
551 return ENODEV;
552
553 if (*count > hw_max) {
554 DPRINTF(("cut off MSI-X count to %d\n", hw_max));
555 *count = hw_max; /* cut off hw_max */
556 }
557
558 return x86_pci_msix_alloc(ihps, count, pa);
559 }
560
561 /*
562 * This function is used by device drivers like pci_intr_map().
563 *
564 * "ihps" is the array of vector numbers which MSI-X used instead of IRQ number.
565 * "count" can not decrease.
566 * If "count" struct intrsource cannot be allocated, return non-zero value.
567 */
568 int
569 pci_msix_alloc_exact(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
570 int count)
571 {
572 int hw_max;
573
574 KASSERT(count > 0);
575
576 hw_max = pci_msix_count(pa->pa_pc, pa->pa_tag);
577 if (hw_max == 0)
578 return ENODEV;
579
580 if (count > hw_max) {
581 DPRINTF(("over hardware max MSI-X count %d\n", hw_max));
582 return EINVAL;
583 }
584
585 return x86_pci_msix_alloc_exact(ihps, count, pa);
586 }
587
588 /*
589 * This function is used by device drivers like pci_intr_map().
590 * Futhermore, this function can map each handle to a MSI-X table index.
591 *
592 * "ihps" is the array of vector numbers which MSI-X used instead of IRQ number.
593 * "count" can not decrease.
594 * "map" size must be equal to "count".
595 * If "count" struct intrsource cannot be allocated, return non-zero value.
596 * e.g.
597 * If "map" = { 1, 4, 0 },
598 * 1st handle is bound to MSI-X index 1
599 * 2nd handle is bound to MSI-X index 4
600 * 3rd handle is bound to MSI-X index 0
601 */
602 int
603 pci_msix_alloc_map(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
604 u_int *table_indexes, int count)
605 {
606 int hw_max, i, j;
607
608 KASSERT(count > 0);
609
610 hw_max = pci_msix_count(pa->pa_pc, pa->pa_tag);
611 if (hw_max == 0)
612 return ENODEV;
613
614 if (count > hw_max) {
615 DPRINTF(("over hardware max MSI-X count %d\n", hw_max));
616 return EINVAL;
617 }
618
619 /* check not to duplicate table_index */
620 for (i = 0; i < count; i++) {
621 u_int basing = table_indexes[i];
622
623 KASSERT(table_indexes[i] < PCI_MSIX_MAX_VECTORS);
624 if (basing >= hw_max) {
625 DPRINTF(("table index is over hardware max MSI-X index %d\n",
626 hw_max - 1));
627 return EINVAL;
628 }
629
630 for (j = i + 1; j < count; j++) {
631 if (basing == table_indexes[j]) {
632 DPRINTF(("MSI-X table index duplicated\n"));
633 return EINVAL;
634 }
635 }
636 }
637
638 return x86_pci_msix_alloc_map(ihps, table_indexes, count, pa);
639 }
640