2 * Copyright (c) 2006 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Support for PCI Message Signalled Interrupts (MSI). MSI interrupts on
33 * x86 are basically APIC messages that the northbridge delivers directly
34 * to the local APICs as if they had come from an I/O APIC.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <x86/apicreg.h>
50 #include <machine/cputypes.h>
51 #include <machine/md_var.h>
52 #include <machine/frame.h>
53 #include <machine/intr_machdep.h>
54 #include <machine/apicvar.h>
55 #include <machine/specialreg.h>
56 #include <dev/pci/pcivar.h>
58 /* Fields in address for Intel MSI messages. */
59 #define MSI_INTEL_ADDR_DEST 0x000ff000
60 #define MSI_INTEL_ADDR_RH 0x00000008
61 # define MSI_INTEL_ADDR_RH_ON 0x00000008
62 # define MSI_INTEL_ADDR_RH_OFF 0x00000000
63 #define MSI_INTEL_ADDR_DM 0x00000004
64 # define MSI_INTEL_ADDR_DM_PHYSICAL 0x00000000
65 # define MSI_INTEL_ADDR_DM_LOGICAL 0x00000004
67 /* Fields in data for Intel MSI messages. */
68 #define MSI_INTEL_DATA_TRGRMOD IOART_TRGRMOD /* Trigger mode. */
69 # define MSI_INTEL_DATA_TRGREDG IOART_TRGREDG
70 # define MSI_INTEL_DATA_TRGRLVL IOART_TRGRLVL
71 #define MSI_INTEL_DATA_LEVEL 0x00004000 /* Polarity. */
72 # define MSI_INTEL_DATA_DEASSERT 0x00000000
73 # define MSI_INTEL_DATA_ASSERT 0x00004000
74 #define MSI_INTEL_DATA_DELMOD IOART_DELMOD /* Delivery mode. */
75 # define MSI_INTEL_DATA_DELFIXED IOART_DELFIXED
76 # define MSI_INTEL_DATA_DELLOPRI IOART_DELLOPRI
77 # define MSI_INTEL_DATA_DELSMI IOART_DELSMI
78 # define MSI_INTEL_DATA_DELNMI IOART_DELNMI
79 # define MSI_INTEL_DATA_DELINIT IOART_DELINIT
80 # define MSI_INTEL_DATA_DELEXINT IOART_DELEXINT
81 #define MSI_INTEL_DATA_INTVEC IOART_INTVEC /* Interrupt vector. */
84 * Build Intel MSI message and data values from a source. AMD64 systems
85 * seem to be compatible, so we use the same function for both.
87 #define INTEL_ADDR(msi) \
88 (MSI_INTEL_ADDR_BASE | (msi)->msi_cpu << 12 | \
89 MSI_INTEL_ADDR_RH_OFF | MSI_INTEL_ADDR_DM_PHYSICAL)
90 #define INTEL_DATA(msi) \
91 (MSI_INTEL_DATA_TRGREDG | MSI_INTEL_DATA_DELFIXED | (msi)->msi_vector)
93 static MALLOC_DEFINE(M_MSI, "msi", "PCI MSI");
96 * MSI sources are bunched into groups. This is because MSI forces
97 * all of the messages to share the address and data registers and
98 * thus certain properties (such as the local APIC ID target on x86).
99 * Each group has a 'first' source that contains information global to
100 * the group. These fields are marked with (g) below.
102 * Note that local APIC ID is kind of special. Each message will be
103 * assigned an ID by the system; however, a group will use the ID from
106 * For MSI-X, each message is isolated.
109 struct intsrc msi_intsrc;
110 device_t msi_dev; /* Owning device. (g) */
111 struct msi_intsrc *msi_first; /* First source in group. */
112 u_int msi_irq; /* IRQ cookie. */
113 u_int msi_msix; /* MSI-X message. */
114 u_int msi_vector:8; /* IDT vector. */
115 u_int msi_cpu:8; /* Local APIC ID. (g) */
116 u_int msi_count:8; /* Messages in this group. (g) */
117 u_int msi_maxcount:8; /* Alignment for this group. (g) */
118 int *msi_irqs; /* Group's IRQ list. (g) */
121 static void msi_create_source(void);
122 static void msi_enable_source(struct intsrc *isrc);
123 static void msi_disable_source(struct intsrc *isrc, int eoi);
124 static void msi_eoi_source(struct intsrc *isrc);
125 static void msi_enable_intr(struct intsrc *isrc);
126 static void msi_disable_intr(struct intsrc *isrc);
127 static int msi_vector(struct intsrc *isrc);
128 static int msi_source_pending(struct intsrc *isrc);
129 static int msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
130 enum intr_polarity pol);
131 static int msi_assign_cpu(struct intsrc *isrc, u_int apic_id);
133 struct pic msi_pic = { msi_enable_source, msi_disable_source, msi_eoi_source,
134 msi_enable_intr, msi_disable_intr, msi_vector,
135 msi_source_pending, NULL, NULL, msi_config_intr,
140 * Xen hypervisors prior to 4.6.0 do not properly handle updates to
141 * enabled MSI-X table entries. Allow migration of MSI-X interrupts
142 * to be disabled via a tunable. Values have the following meaning:
144 * -1: automatic detection by FreeBSD
145 * 0: enable migration
146 * 1: disable migration
148 int msix_disable_migration = -1;
149 SYSCTL_INT(_machdep, OID_AUTO, disable_msix_migration, CTLFLAG_RDTUN,
150 &msix_disable_migration, 0,
151 "Disable migration of MSI-X interrupts between CPUs");
154 static int msi_enabled;
155 static int msi_last_irq;
156 static struct mtx msi_lock;
159 msi_enable_source(struct intsrc *isrc)
164 msi_disable_source(struct intsrc *isrc, int eoi)
172 msi_eoi_source(struct intsrc *isrc)
179 msi_enable_intr(struct intsrc *isrc)
181 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
183 apic_enable_vector(msi->msi_cpu, msi->msi_vector);
187 msi_disable_intr(struct intsrc *isrc)
189 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
191 apic_disable_vector(msi->msi_cpu, msi->msi_vector);
195 msi_vector(struct intsrc *isrc)
197 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
199 return (msi->msi_irq);
203 msi_source_pending(struct intsrc *isrc)
210 msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
211 enum intr_polarity pol)
218 msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
220 struct msi_intsrc *sib, *msi = (struct msi_intsrc *)isrc;
226 * Only allow CPUs to be assigned to the first message for an
229 if (msi->msi_first != msi)
233 if (msix_disable_migration && msi->msi_msix)
237 /* Store information to free existing irq. */
238 old_vector = msi->msi_vector;
239 old_id = msi->msi_cpu;
240 if (old_id == apic_id)
243 /* Allocate IDT vectors on this cpu. */
244 if (msi->msi_count > 1) {
245 KASSERT(msi->msi_msix == 0, ("MSI-X message group"));
246 vector = apic_alloc_vectors(apic_id, msi->msi_irqs,
247 msi->msi_count, msi->msi_maxcount);
249 vector = apic_alloc_vector(apic_id, msi->msi_irq);
253 msi->msi_cpu = apic_id;
254 msi->msi_vector = vector;
255 if (msi->msi_intsrc.is_handlers > 0)
256 apic_enable_vector(msi->msi_cpu, msi->msi_vector);
258 printf("msi: Assigning %s IRQ %d to local APIC %u vector %u\n",
259 msi->msi_msix ? "MSI-X" : "MSI", msi->msi_irq,
260 msi->msi_cpu, msi->msi_vector);
261 for (i = 1; i < msi->msi_count; i++) {
262 sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
263 sib->msi_cpu = apic_id;
264 sib->msi_vector = vector + i;
265 if (sib->msi_intsrc.is_handlers > 0)
266 apic_enable_vector(sib->msi_cpu, sib->msi_vector);
269 "msi: Assigning MSI IRQ %d to local APIC %u vector %u\n",
270 sib->msi_irq, sib->msi_cpu, sib->msi_vector);
272 BUS_REMAP_INTR(device_get_parent(msi->msi_dev), msi->msi_dev,
276 * Free the old vector after the new one is established. This is done
277 * to prevent races where we could miss an interrupt.
279 if (msi->msi_intsrc.is_handlers > 0)
280 apic_disable_vector(old_id, old_vector);
281 apic_free_vector(old_id, old_vector, msi->msi_irq);
282 for (i = 1; i < msi->msi_count; i++) {
283 sib = (struct msi_intsrc *)intr_lookup_source(msi->msi_irqs[i]);
284 if (sib->msi_intsrc.is_handlers > 0)
285 apic_disable_vector(old_id, old_vector + i);
286 apic_free_vector(old_id, old_vector + i, msi->msi_irqs[i]);
295 /* Check if we have a supported CPU. */
296 switch (cpu_vendor_id) {
297 case CPU_VENDOR_INTEL:
300 case CPU_VENDOR_CENTAUR:
301 if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
302 CPUID_TO_MODEL(cpu_id) >= 0xf)
310 if (msix_disable_migration == -1) {
311 /* The default is to allow migration of MSI-X interrupts. */
312 msix_disable_migration = 0;
317 intr_register_pic(&msi_pic);
318 mtx_init(&msi_lock, "msi", NULL, MTX_DEF);
322 msi_create_source(void)
324 struct msi_intsrc *msi;
328 if (msi_last_irq >= NUM_MSI_INTS) {
329 mtx_unlock(&msi_lock);
332 irq = msi_last_irq + FIRST_MSI_INT;
334 mtx_unlock(&msi_lock);
336 msi = malloc(sizeof(struct msi_intsrc), M_MSI, M_WAITOK | M_ZERO);
337 msi->msi_intsrc.is_pic = &msi_pic;
339 intr_register_source(&msi->msi_intsrc);
344 * Try to allocate 'count' interrupt sources with contiguous IDT values.
347 msi_alloc(device_t dev, int count, int maxcount, int *irqs)
349 struct msi_intsrc *msi, *fsrc;
351 int cnt, i, *mirqs, vector;
357 mirqs = malloc(count * sizeof(*mirqs), M_MSI, M_WAITOK);
363 /* Try to find 'count' free IRQs. */
365 for (i = FIRST_MSI_INT; i < FIRST_MSI_INT + NUM_MSI_INTS; i++) {
366 msi = (struct msi_intsrc *)intr_lookup_source(i);
368 /* End of allocated sources, so break. */
372 /* If this is a free one, save its IRQ in the array. */
373 if (msi->msi_dev == NULL) {
381 /* Do we need to create some new sources? */
383 /* If we would exceed the max, give up. */
384 if (i + (count - cnt) > FIRST_MSI_INT + NUM_MSI_INTS) {
385 mtx_unlock(&msi_lock);
389 mtx_unlock(&msi_lock);
391 /* We need count - cnt more sources. */
392 while (cnt < count) {
399 /* Ok, we now have the IRQs allocated. */
400 KASSERT(cnt == count, ("count mismatch"));
402 /* Allocate 'count' IDT vectors. */
403 cpu = intr_next_cpu();
404 vector = apic_alloc_vectors(cpu, irqs, count, maxcount);
406 mtx_unlock(&msi_lock);
411 /* Assign IDT vectors and make these messages owned by 'dev'. */
412 fsrc = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
413 for (i = 0; i < count; i++) {
414 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
417 msi->msi_vector = vector + i;
420 "msi: routing MSI IRQ %d to local APIC %u vector %u\n",
421 msi->msi_irq, msi->msi_cpu, msi->msi_vector);
422 msi->msi_first = fsrc;
423 KASSERT(msi->msi_intsrc.is_handlers == 0,
424 ("dead MSI has handlers"));
426 fsrc->msi_count = count;
427 fsrc->msi_maxcount = maxcount;
429 bcopy(irqs, mirqs, count * sizeof(*mirqs));
430 fsrc->msi_irqs = mirqs;
431 mtx_unlock(&msi_lock);
437 msi_release(int *irqs, int count)
439 struct msi_intsrc *msi, *first;
443 first = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
445 mtx_unlock(&msi_lock);
449 /* Make sure this isn't an MSI-X message. */
450 if (first->msi_msix) {
451 mtx_unlock(&msi_lock);
455 /* Make sure this message is allocated to a group. */
456 if (first->msi_first == NULL) {
457 mtx_unlock(&msi_lock);
462 * Make sure this is the start of a group and that we are releasing
465 if (first->msi_first != first || first->msi_count != count) {
466 mtx_unlock(&msi_lock);
469 KASSERT(first->msi_dev != NULL, ("unowned group"));
471 /* Clear all the extra messages in the group. */
472 for (i = 1; i < count; i++) {
473 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
474 KASSERT(msi->msi_first == first, ("message not in group"));
475 KASSERT(msi->msi_dev == first->msi_dev, ("owner mismatch"));
476 msi->msi_first = NULL;
478 apic_free_vector(msi->msi_cpu, msi->msi_vector, msi->msi_irq);
482 /* Clear out the first message. */
483 first->msi_first = NULL;
484 first->msi_dev = NULL;
485 apic_free_vector(first->msi_cpu, first->msi_vector, first->msi_irq);
486 first->msi_vector = 0;
487 first->msi_count = 0;
488 first->msi_maxcount = 0;
489 free(first->msi_irqs, M_MSI);
490 first->msi_irqs = NULL;
492 mtx_unlock(&msi_lock);
497 msi_map(int irq, uint64_t *addr, uint32_t *data)
499 struct msi_intsrc *msi;
502 msi = (struct msi_intsrc *)intr_lookup_source(irq);
504 mtx_unlock(&msi_lock);
508 /* Make sure this message is allocated to a device. */
509 if (msi->msi_dev == NULL) {
510 mtx_unlock(&msi_lock);
515 * If this message isn't an MSI-X message, make sure it's part
516 * of a group, and switch to the first message in the
519 if (!msi->msi_msix) {
520 if (msi->msi_first == NULL) {
521 mtx_unlock(&msi_lock);
524 msi = msi->msi_first;
527 *addr = INTEL_ADDR(msi);
528 *data = INTEL_DATA(msi);
529 mtx_unlock(&msi_lock);
534 msix_alloc(device_t dev, int *irq)
536 struct msi_intsrc *msi;
546 /* Find a free IRQ. */
547 for (i = FIRST_MSI_INT; i < FIRST_MSI_INT + NUM_MSI_INTS; i++) {
548 msi = (struct msi_intsrc *)intr_lookup_source(i);
550 /* End of allocated sources, so break. */
554 /* Stop at the first free source. */
555 if (msi->msi_dev == NULL)
559 /* Do we need to create a new source? */
561 /* If we would exceed the max, give up. */
562 if (i + 1 > FIRST_MSI_INT + NUM_MSI_INTS) {
563 mtx_unlock(&msi_lock);
566 mtx_unlock(&msi_lock);
568 /* Create a new source. */
573 /* Allocate an IDT vector. */
574 cpu = intr_next_cpu();
575 vector = apic_alloc_vector(cpu, i);
577 mtx_unlock(&msi_lock);
581 printf("msi: routing MSI-X IRQ %d to local APIC %u vector %u\n",
582 msi->msi_irq, cpu, vector);
587 msi->msi_first = msi;
588 msi->msi_vector = vector;
591 msi->msi_maxcount = 1;
592 msi->msi_irqs = NULL;
594 KASSERT(msi->msi_intsrc.is_handlers == 0, ("dead MSI-X has handlers"));
595 mtx_unlock(&msi_lock);
602 msix_release(int irq)
604 struct msi_intsrc *msi;
607 msi = (struct msi_intsrc *)intr_lookup_source(irq);
609 mtx_unlock(&msi_lock);
613 /* Make sure this is an MSI-X message. */
614 if (!msi->msi_msix) {
615 mtx_unlock(&msi_lock);
619 KASSERT(msi->msi_dev != NULL, ("unowned message"));
621 /* Clear out the message. */
622 msi->msi_first = NULL;
624 apic_free_vector(msi->msi_cpu, msi->msi_vector, msi->msi_irq);
628 msi->msi_maxcount = 0;
630 mtx_unlock(&msi_lock);