]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/msi.c
Dynamically allocate IRQ ranges on x86.
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / msi.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2006 Yahoo!, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Support for PCI Message Signalled Interrupts (MSI).  MSI interrupts on
35  * x86 are basically APIC messages that the northbridge delivers directly
36  * to the local APICs as if they had come from an I/O APIC.
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_acpi.h"
43
44 #include <sys/param.h>
45 #include <sys/bus.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/sx.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #include <x86/apicreg.h>
54 #include <machine/cputypes.h>
55 #include <machine/md_var.h>
56 #include <machine/frame.h>
57 #include <machine/intr_machdep.h>
58 #include <x86/apicvar.h>
59 #include <x86/iommu/iommu_intrmap.h>
60 #include <machine/specialreg.h>
61 #include <dev/pci/pcivar.h>
62
63 /* Fields in address for Intel MSI messages. */
64 #define MSI_INTEL_ADDR_DEST             0x000ff000
65 #define MSI_INTEL_ADDR_RH               0x00000008
66 # define MSI_INTEL_ADDR_RH_ON           0x00000008
67 # define MSI_INTEL_ADDR_RH_OFF          0x00000000
68 #define MSI_INTEL_ADDR_DM               0x00000004
69 # define MSI_INTEL_ADDR_DM_PHYSICAL     0x00000000
70 # define MSI_INTEL_ADDR_DM_LOGICAL      0x00000004
71
72 /* Fields in data for Intel MSI messages. */
73 #define MSI_INTEL_DATA_TRGRMOD          IOART_TRGRMOD   /* Trigger mode. */
74 # define MSI_INTEL_DATA_TRGREDG         IOART_TRGREDG
75 # define MSI_INTEL_DATA_TRGRLVL         IOART_TRGRLVL
76 #define MSI_INTEL_DATA_LEVEL            0x00004000      /* Polarity. */
77 # define MSI_INTEL_DATA_DEASSERT        0x00000000
78 # define MSI_INTEL_DATA_ASSERT          0x00004000
79 #define MSI_INTEL_DATA_DELMOD           IOART_DELMOD    /* Delivery mode. */
80 # define MSI_INTEL_DATA_DELFIXED        IOART_DELFIXED
81 # define MSI_INTEL_DATA_DELLOPRI        IOART_DELLOPRI
82 # define MSI_INTEL_DATA_DELSMI          IOART_DELSMI
83 # define MSI_INTEL_DATA_DELNMI          IOART_DELNMI
84 # define MSI_INTEL_DATA_DELINIT         IOART_DELINIT
85 # define MSI_INTEL_DATA_DELEXINT        IOART_DELEXINT
86 #define MSI_INTEL_DATA_INTVEC           IOART_INTVEC    /* Interrupt vector. */
87
88 /*
89  * Build Intel MSI message and data values from a source.  AMD64 systems
90  * seem to be compatible, so we use the same function for both.
91  */
92 #define INTEL_ADDR(msi)                                                 \
93         (MSI_INTEL_ADDR_BASE | (msi)->msi_cpu << 12 |                   \
94             MSI_INTEL_ADDR_RH_OFF | MSI_INTEL_ADDR_DM_PHYSICAL)
95 #define INTEL_DATA(msi)                                                 \
96         (MSI_INTEL_DATA_TRGREDG | MSI_INTEL_DATA_DELFIXED | (msi)->msi_vector)
97
98 static MALLOC_DEFINE(M_MSI, "msi", "PCI MSI");
99
100 /*
101  * MSI sources are bunched into groups.  This is because MSI forces
102  * all of the messages to share the address and data registers and
103  * thus certain properties (such as the local APIC ID target on x86).
104  * Each group has a 'first' source that contains information global to
105  * the group.  These fields are marked with (g) below.
106  *
107  * Note that local APIC ID is kind of special.  Each message will be
108  * assigned an ID by the system; however, a group will use the ID from
109  * the first message.
110  *
111  * For MSI-X, each message is isolated.
112  */
113 struct msi_intsrc {
114         struct intsrc msi_intsrc;
115         device_t msi_dev;               /* Owning device. (g) */
116         struct msi_intsrc *msi_first;   /* First source in group. */
117         u_int msi_irq;                  /* IRQ cookie. */
118         u_int msi_msix;                 /* MSI-X message. */
119         u_int msi_vector:8;             /* IDT vector. */
120         u_int msi_cpu;                  /* Local APIC ID. (g) */
121         u_int msi_count:8;              /* Messages in this group. (g) */
122         u_int msi_maxcount:8;           /* Alignment for this group. (g) */
123         u_int *msi_irqs;                /* Group's IRQ list. (g) */
124         u_int msi_remap_cookie;
125 };
126
127 static void     msi_create_source(void);
128 static void     msi_enable_source(struct intsrc *isrc);
129 static void     msi_disable_source(struct intsrc *isrc, int eoi);
130 static void     msi_eoi_source(struct intsrc *isrc);
131 static void     msi_enable_intr(struct intsrc *isrc);
132 static void     msi_disable_intr(struct intsrc *isrc);
133 static int      msi_vector(struct intsrc *isrc);
134 static int      msi_source_pending(struct intsrc *isrc);
135 static int      msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
136                     enum intr_polarity pol);
137 static int      msi_assign_cpu(struct intsrc *isrc, u_int apic_id);
138
139 struct pic msi_pic = {
140         .pic_enable_source = msi_enable_source,
141         .pic_disable_source = msi_disable_source,
142         .pic_eoi_source = msi_eoi_source,
143         .pic_enable_intr = msi_enable_intr,
144         .pic_disable_intr = msi_disable_intr,
145         .pic_vector = msi_vector,
146         .pic_source_pending = msi_source_pending,
147         .pic_suspend = NULL,
148         .pic_resume = NULL,
149         .pic_config_intr = msi_config_intr,
150         .pic_assign_cpu = msi_assign_cpu,
151         .pic_reprogram_pin = NULL,
152 };
153
154 u_int first_msi_irq;
155
156 #ifdef SMP
157 /**
158  * Xen hypervisors prior to 4.6.0 do not properly handle updates to
159  * enabled MSI-X table entries.  Allow migration of MSI-X interrupts
160  * to be disabled via a tunable. Values have the following meaning:
161  *
162  * -1: automatic detection by FreeBSD
163  *  0: enable migration
164  *  1: disable migration
165  */
166 int msix_disable_migration = -1;
167 SYSCTL_INT(_machdep, OID_AUTO, disable_msix_migration, CTLFLAG_RDTUN,
168     &msix_disable_migration, 0,
169     "Disable migration of MSI-X interrupts between CPUs");
170 #endif
171
172 static int msi_enabled;
173 static u_int msi_last_irq;
174 static struct mtx msi_lock;
175
176 static void
177 msi_enable_source(struct intsrc *isrc)
178 {
179 }
180
181 static void
182 msi_disable_source(struct intsrc *isrc, int eoi)
183 {
184
185         if (eoi == PIC_EOI)
186                 lapic_eoi();
187 }
188
189 static void
190 msi_eoi_source(struct intsrc *isrc)
191 {
192
193         lapic_eoi();
194 }
195
196 static void
197 msi_enable_intr(struct intsrc *isrc)
198 {
199         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
200
201         apic_enable_vector(msi->msi_cpu, msi->msi_vector);
202 }
203
204 static void
205 msi_disable_intr(struct intsrc *isrc)
206 {
207         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
208
209         apic_disable_vector(msi->msi_cpu, msi->msi_vector);
210 }
211
212 static int
213 msi_vector(struct intsrc *isrc)
214 {
215         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
216
217         return (msi->msi_irq);
218 }
219
220 static int
221 msi_source_pending(struct intsrc *isrc)
222 {
223
224         return (0);
225 }
226
227 static int
228 msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
229     enum intr_polarity pol)
230 {
231
232         return (ENODEV);
233 }
234
235 static int
236 msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
237 {
238         struct msi_intsrc *sib, *msi = (struct msi_intsrc *)isrc;
239         int old_vector;
240         u_int old_id;
241         int i, vector;
242
243         /*
244          * Only allow CPUs to be assigned to the first message for an
245          * MSI group.
246          */
247         if (msi->msi_first != msi)
248                 return (EINVAL);
249
250 #ifdef SMP
251         if (msix_disable_migration && msi->msi_msix)
252                 return (EINVAL);
253 #endif
254
255         /* Store information to free existing irq. */
256         old_vector = msi->msi_vector;
257         old_id = msi->msi_cpu;
258         if (old_id == apic_id)
259                 return (0);
260
261         /* Allocate IDT vectors on this cpu. */
262         if (msi->msi_count > 1) {
263                 KASSERT(msi->msi_msix == 0, ("MSI-X message group"));
264                 vector = apic_alloc_vectors(apic_id, msi->msi_irqs,
265                     msi->msi_count, msi->msi_maxcount);
266         } else
267                 vector = apic_alloc_vector(apic_id, msi->msi_irq);
268         if (vector == 0)
269                 return (ENOSPC);
270
271         msi->msi_cpu = apic_id;
272         msi->msi_vector = vector;
273         if (msi->msi_intsrc.is_handlers > 0)
274                 apic_enable_vector(msi->msi_cpu, msi->msi_vector);
275         if (bootverbose)
276                 printf("msi: Assigning %s IRQ %d to local APIC %u vector %u\n",
277                     msi->msi_msix ? "MSI-X" : "MSI", msi->msi_irq,
278                     msi->msi_cpu, msi->msi_vector);
279         for (i = 1; i < msi->msi_count; i++) {
280                 sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
281                 sib->msi_cpu = apic_id;
282                 sib->msi_vector = vector + i;
283                 if (sib->msi_intsrc.is_handlers > 0)
284                         apic_enable_vector(sib->msi_cpu, sib->msi_vector);
285                 if (bootverbose)
286                         printf(
287                     "msi: Assigning MSI IRQ %d to local APIC %u vector %u\n",
288                             sib->msi_irq, sib->msi_cpu, sib->msi_vector);
289         }
290         BUS_REMAP_INTR(device_get_parent(msi->msi_dev), msi->msi_dev,
291             msi->msi_irq);
292
293         /*
294          * Free the old vector after the new one is established.  This is done
295          * to prevent races where we could miss an interrupt.
296          */
297         if (msi->msi_intsrc.is_handlers > 0)
298                 apic_disable_vector(old_id, old_vector);
299         apic_free_vector(old_id, old_vector, msi->msi_irq);
300         for (i = 1; i < msi->msi_count; i++) {
301                 sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
302                 if (sib->msi_intsrc.is_handlers > 0)
303                         apic_disable_vector(old_id, old_vector + i);
304                 apic_free_vector(old_id, old_vector + i, msi->msi_irqs[i]);
305         }
306         return (0);
307 }
308
309 void
310 msi_init(void)
311 {
312
313         /* Check if we have a supported CPU. */
314         switch (cpu_vendor_id) {
315         case CPU_VENDOR_INTEL:
316         case CPU_VENDOR_AMD:
317                 break;
318         case CPU_VENDOR_CENTAUR:
319                 if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
320                     CPUID_TO_MODEL(cpu_id) >= 0xf)
321                         break;
322                 /* FALLTHROUGH */
323         default:
324                 return;
325         }
326
327 #ifdef SMP
328         if (msix_disable_migration == -1) {
329                 /* The default is to allow migration of MSI-X interrupts. */
330                 msix_disable_migration = 0;
331         }
332 #endif
333
334         MPASS(num_io_irqs > 0);
335         first_msi_irq = max(MINIMUM_MSI_INT, num_io_irqs);
336         num_io_irqs = first_msi_irq + NUM_MSI_INTS;
337
338         msi_enabled = 1;
339         intr_register_pic(&msi_pic);
340         mtx_init(&msi_lock, "msi", NULL, MTX_DEF);
341 }
342
343 static void
344 msi_create_source(void)
345 {
346         struct msi_intsrc *msi;
347         u_int irq;
348
349         mtx_lock(&msi_lock);
350         if (msi_last_irq >= NUM_MSI_INTS) {
351                 mtx_unlock(&msi_lock);
352                 return;
353         }
354         irq = msi_last_irq + first_msi_irq;
355         msi_last_irq++;
356         mtx_unlock(&msi_lock);
357
358         msi = malloc(sizeof(struct msi_intsrc), M_MSI, M_WAITOK | M_ZERO);
359         msi->msi_intsrc.is_pic = &msi_pic;
360         msi->msi_irq = irq;
361         intr_register_source(&msi->msi_intsrc);
362         nexus_add_irq(irq);
363 }
364
365 /*
366  * Try to allocate 'count' interrupt sources with contiguous IDT values.
367  */
368 int
369 msi_alloc(device_t dev, int count, int maxcount, int *irqs)
370 {
371         struct msi_intsrc *msi, *fsrc;
372         u_int cpu, domain, *mirqs;
373         int cnt, i, vector;
374 #ifdef ACPI_DMAR
375         u_int cookies[count];
376         int error;
377 #endif
378
379         if (!msi_enabled)
380                 return (ENXIO);
381
382         if (bus_get_domain(dev, &domain) != 0)
383                 domain = 0;
384
385         if (count > 1)
386                 mirqs = malloc(count * sizeof(*mirqs), M_MSI, M_WAITOK);
387         else
388                 mirqs = NULL;
389 again:
390         mtx_lock(&msi_lock);
391
392         /* Try to find 'count' free IRQs. */
393         cnt = 0;
394         for (i = first_msi_irq; i < first_msi_irq + NUM_MSI_INTS; i++) {
395                 msi = (struct msi_intsrc *)intr_lookup_source(i);
396
397                 /* End of allocated sources, so break. */
398                 if (msi == NULL)
399                         break;
400
401                 /* If this is a free one, save its IRQ in the array. */
402                 if (msi->msi_dev == NULL) {
403                         irqs[cnt] = i;
404                         cnt++;
405                         if (cnt == count)
406                                 break;
407                 }
408         }
409
410         /* Do we need to create some new sources? */
411         if (cnt < count) {
412                 /* If we would exceed the max, give up. */
413                 if (i + (count - cnt) >= first_msi_irq + NUM_MSI_INTS) {
414                         mtx_unlock(&msi_lock);
415                         free(mirqs, M_MSI);
416                         return (ENXIO);
417                 }
418                 mtx_unlock(&msi_lock);
419
420                 /* We need count - cnt more sources. */
421                 while (cnt < count) {
422                         msi_create_source();
423                         cnt++;
424                 }
425                 goto again;
426         }
427
428         /* Ok, we now have the IRQs allocated. */
429         KASSERT(cnt == count, ("count mismatch"));
430
431         /* Allocate 'count' IDT vectors. */
432         cpu = intr_next_cpu(domain);
433         vector = apic_alloc_vectors(cpu, irqs, count, maxcount);
434         if (vector == 0) {
435                 mtx_unlock(&msi_lock);
436                 free(mirqs, M_MSI);
437                 return (ENOSPC);
438         }
439
440 #ifdef ACPI_DMAR
441         mtx_unlock(&msi_lock);
442         error = iommu_alloc_msi_intr(dev, cookies, count);
443         mtx_lock(&msi_lock);
444         if (error == EOPNOTSUPP)
445                 error = 0;
446         if (error != 0) {
447                 for (i = 0; i < count; i++)
448                         apic_free_vector(cpu, vector + i, irqs[i]);
449                 free(mirqs, M_MSI);
450                 return (error);
451         }
452         for (i = 0; i < count; i++) {
453                 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
454                 msi->msi_remap_cookie = cookies[i];
455         }
456 #endif
457
458         /* Assign IDT vectors and make these messages owned by 'dev'. */
459         fsrc = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
460         for (i = 0; i < count; i++) {
461                 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
462                 msi->msi_cpu = cpu;
463                 msi->msi_dev = dev;
464                 msi->msi_vector = vector + i;
465                 if (bootverbose)
466                         printf(
467                     "msi: routing MSI IRQ %d to local APIC %u vector %u\n",
468                             msi->msi_irq, msi->msi_cpu, msi->msi_vector);
469                 msi->msi_first = fsrc;
470                 KASSERT(msi->msi_intsrc.is_handlers == 0,
471                     ("dead MSI has handlers"));
472         }
473         fsrc->msi_count = count;
474         fsrc->msi_maxcount = maxcount;
475         if (count > 1)
476                 bcopy(irqs, mirqs, count * sizeof(*mirqs));
477         fsrc->msi_irqs = mirqs;
478         mtx_unlock(&msi_lock);
479         return (0);
480 }
481
482 int
483 msi_release(int *irqs, int count)
484 {
485         struct msi_intsrc *msi, *first;
486         int i;
487
488         mtx_lock(&msi_lock);
489         first = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
490         if (first == NULL) {
491                 mtx_unlock(&msi_lock);
492                 return (ENOENT);
493         }
494
495         /* Make sure this isn't an MSI-X message. */
496         if (first->msi_msix) {
497                 mtx_unlock(&msi_lock);
498                 return (EINVAL);
499         }
500
501         /* Make sure this message is allocated to a group. */
502         if (first->msi_first == NULL) {
503                 mtx_unlock(&msi_lock);
504                 return (ENXIO);
505         }
506
507         /*
508          * Make sure this is the start of a group and that we are releasing
509          * the entire group.
510          */
511         if (first->msi_first != first || first->msi_count != count) {
512                 mtx_unlock(&msi_lock);
513                 return (EINVAL);
514         }
515         KASSERT(first->msi_dev != NULL, ("unowned group"));
516
517         /* Clear all the extra messages in the group. */
518         for (i = 1; i < count; i++) {
519                 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
520                 KASSERT(msi->msi_first == first, ("message not in group"));
521                 KASSERT(msi->msi_dev == first->msi_dev, ("owner mismatch"));
522 #ifdef ACPI_DMAR
523                 iommu_unmap_msi_intr(first->msi_dev, msi->msi_remap_cookie);
524 #endif
525                 msi->msi_first = NULL;
526                 msi->msi_dev = NULL;
527                 apic_free_vector(msi->msi_cpu, msi->msi_vector, msi->msi_irq);
528                 msi->msi_vector = 0;
529         }
530
531         /* Clear out the first message. */
532 #ifdef ACPI_DMAR
533         mtx_unlock(&msi_lock);
534         iommu_unmap_msi_intr(first->msi_dev, first->msi_remap_cookie);
535         mtx_lock(&msi_lock);
536 #endif
537         first->msi_first = NULL;
538         first->msi_dev = NULL;
539         apic_free_vector(first->msi_cpu, first->msi_vector, first->msi_irq);
540         first->msi_vector = 0;
541         first->msi_count = 0;
542         first->msi_maxcount = 0;
543         free(first->msi_irqs, M_MSI);
544         first->msi_irqs = NULL;
545
546         mtx_unlock(&msi_lock);
547         return (0);
548 }
549
550 int
551 msi_map(int irq, uint64_t *addr, uint32_t *data)
552 {
553         struct msi_intsrc *msi;
554         int error;
555 #ifdef ACPI_DMAR
556         struct msi_intsrc *msi1;
557         int i, k;
558 #endif
559
560         mtx_lock(&msi_lock);
561         msi = (struct msi_intsrc *)intr_lookup_source(irq);
562         if (msi == NULL) {
563                 mtx_unlock(&msi_lock);
564                 return (ENOENT);
565         }
566
567         /* Make sure this message is allocated to a device. */
568         if (msi->msi_dev == NULL) {
569                 mtx_unlock(&msi_lock);
570                 return (ENXIO);
571         }
572
573         /*
574          * If this message isn't an MSI-X message, make sure it's part
575          * of a group, and switch to the first message in the
576          * group.
577          */
578         if (!msi->msi_msix) {
579                 if (msi->msi_first == NULL) {
580                         mtx_unlock(&msi_lock);
581                         return (ENXIO);
582                 }
583                 msi = msi->msi_first;
584         }
585
586 #ifdef ACPI_DMAR
587         if (!msi->msi_msix) {
588                 for (k = msi->msi_count - 1, i = first_msi_irq; k > 0 &&
589                     i < first_msi_irq + NUM_MSI_INTS; i++) {
590                         if (i == msi->msi_irq)
591                                 continue;
592                         msi1 = (struct msi_intsrc *)intr_lookup_source(i);
593                         if (!msi1->msi_msix && msi1->msi_first == msi) {
594                                 mtx_unlock(&msi_lock);
595                                 iommu_map_msi_intr(msi1->msi_dev,
596                                     msi1->msi_cpu, msi1->msi_vector,
597                                     msi1->msi_remap_cookie, NULL, NULL);
598                                 k--;
599                                 mtx_lock(&msi_lock);
600                         }
601                 }
602         }
603         mtx_unlock(&msi_lock);
604         error = iommu_map_msi_intr(msi->msi_dev, msi->msi_cpu,
605             msi->msi_vector, msi->msi_remap_cookie, addr, data);
606 #else
607         mtx_unlock(&msi_lock);
608         error = EOPNOTSUPP;
609 #endif
610         if (error == EOPNOTSUPP) {
611                 *addr = INTEL_ADDR(msi);
612                 *data = INTEL_DATA(msi);
613                 error = 0;
614         }
615         return (error);
616 }
617
618 int
619 msix_alloc(device_t dev, int *irq)
620 {
621         struct msi_intsrc *msi;
622         u_int cpu, domain;
623         int i, vector;
624 #ifdef ACPI_DMAR
625         u_int cookie;
626         int error;
627 #endif
628
629         if (!msi_enabled)
630                 return (ENXIO);
631
632         if (bus_get_domain(dev, &domain) != 0)
633                 domain = 0;
634
635 again:
636         mtx_lock(&msi_lock);
637
638         /* Find a free IRQ. */
639         for (i = first_msi_irq; i < first_msi_irq + NUM_MSI_INTS; i++) {
640                 msi = (struct msi_intsrc *)intr_lookup_source(i);
641
642                 /* End of allocated sources, so break. */
643                 if (msi == NULL)
644                         break;
645
646                 /* Stop at the first free source. */
647                 if (msi->msi_dev == NULL)
648                         break;
649         }
650
651         /* Do we need to create a new source? */
652         if (msi == NULL) {
653                 /* If we would exceed the max, give up. */
654                 if (i + 1 >= first_msi_irq + NUM_MSI_INTS) {
655                         mtx_unlock(&msi_lock);
656                         return (ENXIO);
657                 }
658                 mtx_unlock(&msi_lock);
659
660                 /* Create a new source. */
661                 msi_create_source();
662                 goto again;
663         }
664
665         /* Allocate an IDT vector. */
666         cpu = intr_next_cpu(domain);
667         vector = apic_alloc_vector(cpu, i);
668         if (vector == 0) {
669                 mtx_unlock(&msi_lock);
670                 return (ENOSPC);
671         }
672
673         msi->msi_dev = dev;
674 #ifdef ACPI_DMAR
675         mtx_unlock(&msi_lock);
676         error = iommu_alloc_msi_intr(dev, &cookie, 1);
677         mtx_lock(&msi_lock);
678         if (error == EOPNOTSUPP)
679                 error = 0;
680         if (error != 0) {
681                 msi->msi_dev = NULL;
682                 apic_free_vector(cpu, vector, i);
683                 return (error);
684         }
685         msi->msi_remap_cookie = cookie;
686 #endif
687
688         if (bootverbose)
689                 printf("msi: routing MSI-X IRQ %d to local APIC %u vector %u\n",
690                     msi->msi_irq, cpu, vector);
691
692         /* Setup source. */
693         msi->msi_cpu = cpu;
694         msi->msi_first = msi;
695         msi->msi_vector = vector;
696         msi->msi_msix = 1;
697         msi->msi_count = 1;
698         msi->msi_maxcount = 1;
699         msi->msi_irqs = NULL;
700
701         KASSERT(msi->msi_intsrc.is_handlers == 0, ("dead MSI-X has handlers"));
702         mtx_unlock(&msi_lock);
703
704         *irq = i;
705         return (0);
706 }
707
708 int
709 msix_release(int irq)
710 {
711         struct msi_intsrc *msi;
712
713         mtx_lock(&msi_lock);
714         msi = (struct msi_intsrc *)intr_lookup_source(irq);
715         if (msi == NULL) {
716                 mtx_unlock(&msi_lock);
717                 return (ENOENT);
718         }
719
720         /* Make sure this is an MSI-X message. */
721         if (!msi->msi_msix) {
722                 mtx_unlock(&msi_lock);
723                 return (EINVAL);
724         }
725
726         KASSERT(msi->msi_dev != NULL, ("unowned message"));
727
728         /* Clear out the message. */
729 #ifdef ACPI_DMAR
730         mtx_unlock(&msi_lock);
731         iommu_unmap_msi_intr(msi->msi_dev, msi->msi_remap_cookie);
732         mtx_lock(&msi_lock);
733 #endif
734         msi->msi_first = NULL;
735         msi->msi_dev = NULL;
736         apic_free_vector(msi->msi_cpu, msi->msi_vector, msi->msi_irq);
737         msi->msi_vector = 0;
738         msi->msi_msix = 0;
739         msi->msi_count = 0;
740         msi->msi_maxcount = 0;
741
742         mtx_unlock(&msi_lock);
743         return (0);
744 }