2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2006 Yahoo!, Inc.
6 * Written by: John Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <x86/apicreg.h>
55 #include <machine/cputypes.h>
56 #include <machine/md_var.h>
57 #include <machine/frame.h>
58 #include <machine/intr_machdep.h>
59 #include <x86/apicvar.h>
60 #include <x86/iommu/iommu_intrmap.h>
61 #include <machine/specialreg.h>
62 #include <dev/pci/pcivar.h>
64 /* Fields in address for Intel MSI messages. */
65 #define MSI_INTEL_ADDR_DEST 0x000ff000
66 #define MSI_INTEL_ADDR_RH 0x00000008
67 # define MSI_INTEL_ADDR_RH_ON 0x00000008
68 # define MSI_INTEL_ADDR_RH_OFF 0x00000000
69 #define MSI_INTEL_ADDR_DM 0x00000004
70 # define MSI_INTEL_ADDR_DM_PHYSICAL 0x00000000
71 # define MSI_INTEL_ADDR_DM_LOGICAL 0x00000004
73 /* Fields in data for Intel MSI messages. */
74 #define MSI_INTEL_DATA_TRGRMOD IOART_TRGRMOD /* Trigger mode. */
75 # define MSI_INTEL_DATA_TRGREDG IOART_TRGREDG
76 # define MSI_INTEL_DATA_TRGRLVL IOART_TRGRLVL
77 #define MSI_INTEL_DATA_LEVEL 0x00004000 /* Polarity. */
78 # define MSI_INTEL_DATA_DEASSERT 0x00000000
79 # define MSI_INTEL_DATA_ASSERT 0x00004000
80 #define MSI_INTEL_DATA_DELMOD IOART_DELMOD /* Delivery mode. */
81 # define MSI_INTEL_DATA_DELFIXED IOART_DELFIXED
82 # define MSI_INTEL_DATA_DELLOPRI IOART_DELLOPRI
83 # define MSI_INTEL_DATA_DELSMI IOART_DELSMI
84 # define MSI_INTEL_DATA_DELNMI IOART_DELNMI
85 # define MSI_INTEL_DATA_DELINIT IOART_DELINIT
86 # define MSI_INTEL_DATA_DELEXINT IOART_DELEXINT
87 #define MSI_INTEL_DATA_INTVEC IOART_INTVEC /* Interrupt vector. */
90 * Build Intel MSI message and data values from a source. AMD64 systems
91 * seem to be compatible, so we use the same function for both.
93 #define INTEL_ADDR(msi) \
94 (MSI_INTEL_ADDR_BASE | (msi)->msi_cpu << 12 | \
95 MSI_INTEL_ADDR_RH_OFF | MSI_INTEL_ADDR_DM_PHYSICAL)
96 #define INTEL_DATA(msi) \
97 (MSI_INTEL_DATA_TRGREDG | MSI_INTEL_DATA_DELFIXED | (msi)->msi_vector)
99 static MALLOC_DEFINE(M_MSI, "msi", "PCI MSI");
102 * MSI sources are bunched into groups. This is because MSI forces
103 * all of the messages to share the address and data registers and
104 * thus certain properties (such as the local APIC ID target on x86).
105 * Each group has a 'first' source that contains information global to
106 * the group. These fields are marked with (g) below.
108 * Note that local APIC ID is kind of special. Each message will be
109 * assigned an ID by the system; however, a group will use the ID from
112 * For MSI-X, each message is isolated.
115 struct intsrc msi_intsrc;
116 device_t msi_dev; /* Owning device. (g) */
117 struct msi_intsrc *msi_first; /* First source in group. */
118 u_int msi_irq; /* IRQ cookie. */
119 u_int msi_msix; /* MSI-X message. */
120 u_int msi_vector:8; /* IDT vector. */
121 u_int msi_cpu; /* Local APIC ID. (g) */
122 u_int msi_count:8; /* Messages in this group. (g) */
123 u_int msi_maxcount:8; /* Alignment for this group. (g) */
124 u_int *msi_irqs; /* Group's IRQ list. (g) */
125 u_int msi_remap_cookie;
128 static void msi_create_source(void);
129 static void msi_enable_source(struct intsrc *isrc);
130 static void msi_disable_source(struct intsrc *isrc, int eoi);
131 static void msi_eoi_source(struct intsrc *isrc);
132 static void msi_enable_intr(struct intsrc *isrc);
133 static void msi_disable_intr(struct intsrc *isrc);
134 static int msi_vector(struct intsrc *isrc);
135 static int msi_source_pending(struct intsrc *isrc);
136 static int msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
137 enum intr_polarity pol);
138 static int msi_assign_cpu(struct intsrc *isrc, u_int apic_id);
140 struct pic msi_pic = {
141 .pic_enable_source = msi_enable_source,
142 .pic_disable_source = msi_disable_source,
143 .pic_eoi_source = msi_eoi_source,
144 .pic_enable_intr = msi_enable_intr,
145 .pic_disable_intr = msi_disable_intr,
146 .pic_vector = msi_vector,
147 .pic_source_pending = msi_source_pending,
150 .pic_config_intr = msi_config_intr,
151 .pic_assign_cpu = msi_assign_cpu,
152 .pic_reprogram_pin = NULL,
156 SYSCTL_UINT(_machdep, OID_AUTO, first_msi_irq, CTLFLAG_RD, &first_msi_irq, 0,
157 "Number of first IRQ reserved for MSI and MSI-X interrupts");
159 u_int num_msi_irqs = 2048;
160 SYSCTL_UINT(_machdep, OID_AUTO, num_msi_irqs, CTLFLAG_RDTUN, &num_msi_irqs, 0,
161 "Number of IRQs reserved for MSI and MSI-X interrupts");
165 * Xen hypervisors prior to 4.6.0 do not properly handle updates to
166 * enabled MSI-X table entries. Allow migration of MSI-X interrupts
167 * to be disabled via a tunable. Values have the following meaning:
169 * -1: automatic detection by FreeBSD
170 * 0: enable migration
171 * 1: disable migration
173 int msix_disable_migration = -1;
174 SYSCTL_INT(_machdep, OID_AUTO, disable_msix_migration, CTLFLAG_RDTUN,
175 &msix_disable_migration, 0,
176 "Disable migration of MSI-X interrupts between CPUs");
179 static int msi_enabled;
180 static u_int msi_last_irq;
181 static struct mtx msi_lock;
184 msi_enable_source(struct intsrc *isrc)
189 msi_disable_source(struct intsrc *isrc, int eoi)
197 msi_eoi_source(struct intsrc *isrc)
204 msi_enable_intr(struct intsrc *isrc)
206 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
208 apic_enable_vector(msi->msi_cpu, msi->msi_vector);
212 msi_disable_intr(struct intsrc *isrc)
214 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
216 apic_disable_vector(msi->msi_cpu, msi->msi_vector);
220 msi_vector(struct intsrc *isrc)
222 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
224 return (msi->msi_irq);
228 msi_source_pending(struct intsrc *isrc)
235 msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
236 enum intr_polarity pol)
243 msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
245 struct msi_intsrc *sib, *msi = (struct msi_intsrc *)isrc;
251 * Only allow CPUs to be assigned to the first message for an
254 if (msi->msi_first != msi)
258 if (msix_disable_migration && msi->msi_msix)
262 /* Store information to free existing irq. */
263 old_vector = msi->msi_vector;
264 old_id = msi->msi_cpu;
265 if (old_id == apic_id)
268 /* Allocate IDT vectors on this cpu. */
269 if (msi->msi_count > 1) {
270 KASSERT(msi->msi_msix == 0, ("MSI-X message group"));
271 vector = apic_alloc_vectors(apic_id, msi->msi_irqs,
272 msi->msi_count, msi->msi_maxcount);
274 vector = apic_alloc_vector(apic_id, msi->msi_irq);
278 msi->msi_cpu = apic_id;
279 msi->msi_vector = vector;
280 if (msi->msi_intsrc.is_handlers > 0)
281 apic_enable_vector(msi->msi_cpu, msi->msi_vector);
283 printf("msi: Assigning %s IRQ %d to local APIC %u vector %u\n",
284 msi->msi_msix ? "MSI-X" : "MSI", msi->msi_irq,
285 msi->msi_cpu, msi->msi_vector);
286 for (i = 1; i < msi->msi_count; i++) {
287 sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
288 sib->msi_cpu = apic_id;
289 sib->msi_vector = vector + i;
290 if (sib->msi_intsrc.is_handlers > 0)
291 apic_enable_vector(sib->msi_cpu, sib->msi_vector);
294 "msi: Assigning MSI IRQ %d to local APIC %u vector %u\n",
295 sib->msi_irq, sib->msi_cpu, sib->msi_vector);
297 BUS_REMAP_INTR(device_get_parent(msi->msi_dev), msi->msi_dev,
301 * Free the old vector after the new one is established. This is done
302 * to prevent races where we could miss an interrupt.
304 if (msi->msi_intsrc.is_handlers > 0)
305 apic_disable_vector(old_id, old_vector);
306 apic_free_vector(old_id, old_vector, msi->msi_irq);
307 for (i = 1; i < msi->msi_count; i++) {
308 sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
309 if (sib->msi_intsrc.is_handlers > 0)
310 apic_disable_vector(old_id, old_vector + i);
311 apic_free_vector(old_id, old_vector + i, msi->msi_irqs[i]);
320 /* Check if we have a supported CPU. */
321 switch (cpu_vendor_id) {
322 case CPU_VENDOR_INTEL:
324 case CPU_VENDOR_HYGON:
326 case CPU_VENDOR_CENTAUR:
327 if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
328 CPUID_TO_MODEL(cpu_id) >= 0xf)
336 if (msix_disable_migration == -1) {
337 /* The default is to allow migration of MSI-X interrupts. */
338 msix_disable_migration = 0;
342 if (num_msi_irqs == 0)
345 first_msi_irq = num_io_irqs;
346 if (num_msi_irqs > UINT_MAX - first_msi_irq)
347 panic("num_msi_irqs too high");
348 num_io_irqs = first_msi_irq + num_msi_irqs;
351 intr_register_pic(&msi_pic);
352 mtx_init(&msi_lock, "msi", NULL, MTX_DEF);
356 msi_create_source(void)
358 struct msi_intsrc *msi;
362 if (msi_last_irq >= num_msi_irqs) {
363 mtx_unlock(&msi_lock);
366 irq = msi_last_irq + first_msi_irq;
368 mtx_unlock(&msi_lock);
370 msi = malloc(sizeof(struct msi_intsrc), M_MSI, M_WAITOK | M_ZERO);
371 msi->msi_intsrc.is_pic = &msi_pic;
373 intr_register_source(&msi->msi_intsrc);
378 * Try to allocate 'count' interrupt sources with contiguous IDT values.
381 msi_alloc(device_t dev, int count, int maxcount, int *irqs)
383 struct msi_intsrc *msi, *fsrc;
384 u_int cpu, domain, *mirqs;
387 u_int cookies[count];
394 if (bus_get_domain(dev, &domain) != 0)
398 mirqs = malloc(count * sizeof(*mirqs), M_MSI, M_WAITOK);
404 /* Try to find 'count' free IRQs. */
406 for (i = first_msi_irq; i < first_msi_irq + num_msi_irqs; i++) {
407 msi = (struct msi_intsrc *)intr_lookup_source(i);
409 /* End of allocated sources, so break. */
413 /* If this is a free one, save its IRQ in the array. */
414 if (msi->msi_dev == NULL) {
422 /* Do we need to create some new sources? */
424 /* If we would exceed the max, give up. */
425 if (i + (count - cnt) > first_msi_irq + num_msi_irqs) {
426 mtx_unlock(&msi_lock);
430 mtx_unlock(&msi_lock);
432 /* We need count - cnt more sources. */
433 while (cnt < count) {
440 /* Ok, we now have the IRQs allocated. */
441 KASSERT(cnt == count, ("count mismatch"));
443 /* Allocate 'count' IDT vectors. */
444 cpu = intr_next_cpu(domain);
445 vector = apic_alloc_vectors(cpu, irqs, count, maxcount);
447 mtx_unlock(&msi_lock);
453 mtx_unlock(&msi_lock);
454 error = iommu_alloc_msi_intr(dev, cookies, count);
456 if (error == EOPNOTSUPP)
459 for (i = 0; i < count; i++)
460 apic_free_vector(cpu, vector + i, irqs[i]);
464 for (i = 0; i < count; i++) {
465 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
466 msi->msi_remap_cookie = cookies[i];
470 /* Assign IDT vectors and make these messages owned by 'dev'. */
471 fsrc = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
472 for (i = 0; i < count; i++) {
473 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
476 msi->msi_vector = vector + i;
479 "msi: routing MSI IRQ %d to local APIC %u vector %u\n",
480 msi->msi_irq, msi->msi_cpu, msi->msi_vector);
481 msi->msi_first = fsrc;
482 KASSERT(msi->msi_intsrc.is_handlers == 0,
483 ("dead MSI has handlers"));
485 fsrc->msi_count = count;
486 fsrc->msi_maxcount = maxcount;
488 bcopy(irqs, mirqs, count * sizeof(*mirqs));
489 fsrc->msi_irqs = mirqs;
490 mtx_unlock(&msi_lock);
495 msi_release(int *irqs, int count)
497 struct msi_intsrc *msi, *first;
501 first = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
503 mtx_unlock(&msi_lock);
507 /* Make sure this isn't an MSI-X message. */
508 if (first->msi_msix) {
509 mtx_unlock(&msi_lock);
513 /* Make sure this message is allocated to a group. */
514 if (first->msi_first == NULL) {
515 mtx_unlock(&msi_lock);
520 * Make sure this is the start of a group and that we are releasing
523 if (first->msi_first != first || first->msi_count != count) {
524 mtx_unlock(&msi_lock);
527 KASSERT(first->msi_dev != NULL, ("unowned group"));
529 /* Clear all the extra messages in the group. */
530 for (i = 1; i < count; i++) {
531 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
532 KASSERT(msi->msi_first == first, ("message not in group"));
533 KASSERT(msi->msi_dev == first->msi_dev, ("owner mismatch"));
535 iommu_unmap_msi_intr(first->msi_dev, msi->msi_remap_cookie);
537 msi->msi_first = NULL;
539 apic_free_vector(msi->msi_cpu, msi->msi_vector, msi->msi_irq);
543 /* Clear out the first message. */
545 mtx_unlock(&msi_lock);
546 iommu_unmap_msi_intr(first->msi_dev, first->msi_remap_cookie);
549 first->msi_first = NULL;
550 first->msi_dev = NULL;
551 apic_free_vector(first->msi_cpu, first->msi_vector, first->msi_irq);
552 first->msi_vector = 0;
553 first->msi_count = 0;
554 first->msi_maxcount = 0;
555 free(first->msi_irqs, M_MSI);
556 first->msi_irqs = NULL;
558 mtx_unlock(&msi_lock);
563 msi_map(int irq, uint64_t *addr, uint32_t *data)
565 struct msi_intsrc *msi;
568 struct msi_intsrc *msi1;
573 msi = (struct msi_intsrc *)intr_lookup_source(irq);
575 mtx_unlock(&msi_lock);
579 /* Make sure this message is allocated to a device. */
580 if (msi->msi_dev == NULL) {
581 mtx_unlock(&msi_lock);
586 * If this message isn't an MSI-X message, make sure it's part
587 * of a group, and switch to the first message in the
590 if (!msi->msi_msix) {
591 if (msi->msi_first == NULL) {
592 mtx_unlock(&msi_lock);
595 msi = msi->msi_first;
599 if (!msi->msi_msix) {
600 for (k = msi->msi_count - 1, i = first_msi_irq; k > 0 &&
601 i < first_msi_irq + num_msi_irqs; i++) {
602 if (i == msi->msi_irq)
604 msi1 = (struct msi_intsrc *)intr_lookup_source(i);
605 if (!msi1->msi_msix && msi1->msi_first == msi) {
606 mtx_unlock(&msi_lock);
607 iommu_map_msi_intr(msi1->msi_dev,
608 msi1->msi_cpu, msi1->msi_vector,
609 msi1->msi_remap_cookie, NULL, NULL);
615 mtx_unlock(&msi_lock);
616 error = iommu_map_msi_intr(msi->msi_dev, msi->msi_cpu,
617 msi->msi_vector, msi->msi_remap_cookie, addr, data);
619 mtx_unlock(&msi_lock);
622 if (error == EOPNOTSUPP) {
623 *addr = INTEL_ADDR(msi);
624 *data = INTEL_DATA(msi);
631 msix_alloc(device_t dev, int *irq)
633 struct msi_intsrc *msi;
644 if (bus_get_domain(dev, &domain) != 0)
650 /* Find a free IRQ. */
651 for (i = first_msi_irq; i < first_msi_irq + num_msi_irqs; i++) {
652 msi = (struct msi_intsrc *)intr_lookup_source(i);
654 /* End of allocated sources, so break. */
658 /* Stop at the first free source. */
659 if (msi->msi_dev == NULL)
663 /* Are all IRQs in use? */
664 if (i == first_msi_irq + num_msi_irqs) {
665 mtx_unlock(&msi_lock);
669 /* Do we need to create a new source? */
671 mtx_unlock(&msi_lock);
673 /* Create a new source. */
678 /* Allocate an IDT vector. */
679 cpu = intr_next_cpu(domain);
680 vector = apic_alloc_vector(cpu, i);
682 mtx_unlock(&msi_lock);
688 mtx_unlock(&msi_lock);
689 error = iommu_alloc_msi_intr(dev, &cookie, 1);
691 if (error == EOPNOTSUPP)
695 apic_free_vector(cpu, vector, i);
698 msi->msi_remap_cookie = cookie;
702 printf("msi: routing MSI-X IRQ %d to local APIC %u vector %u\n",
703 msi->msi_irq, cpu, vector);
707 msi->msi_first = msi;
708 msi->msi_vector = vector;
711 msi->msi_maxcount = 1;
712 msi->msi_irqs = NULL;
714 KASSERT(msi->msi_intsrc.is_handlers == 0, ("dead MSI-X has handlers"));
715 mtx_unlock(&msi_lock);
722 msix_release(int irq)
724 struct msi_intsrc *msi;
727 msi = (struct msi_intsrc *)intr_lookup_source(irq);
729 mtx_unlock(&msi_lock);
733 /* Make sure this is an MSI-X message. */
734 if (!msi->msi_msix) {
735 mtx_unlock(&msi_lock);
739 KASSERT(msi->msi_dev != NULL, ("unowned message"));
741 /* Clear out the message. */
743 mtx_unlock(&msi_lock);
744 iommu_unmap_msi_intr(msi->msi_dev, msi->msi_remap_cookie);
747 msi->msi_first = NULL;
749 apic_free_vector(msi->msi_cpu, msi->msi_vector, msi->msi_irq);
753 msi->msi_maxcount = 0;
755 mtx_unlock(&msi_lock);