2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Support for PCI Message Signalled Interrupts (MSI). MSI interrupts on
32 * x86 are basically APIC messages that the northbridge delivers directly
33 * to the local APICs as if they had come from an I/O APIC.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/systm.h>
47 #include <machine/apicreg.h>
48 #include <machine/md_var.h>
49 #include <machine/frame.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/apicvar.h>
52 #include <dev/pci/pcivar.h>
54 /* Fields in address for Intel MSI messages. */
55 #define MSI_INTEL_ADDR_DEST 0x000ff000
56 #define MSI_INTEL_ADDR_RH 0x00000008
57 # define MSI_INTEL_ADDR_RH_ON 0x00000008
58 # define MSI_INTEL_ADDR_RH_OFF 0x00000000
59 #define MSI_INTEL_ADDR_DM 0x00000004
60 # define MSI_INTEL_ADDR_DM_PHYSICAL 0x00000000
61 # define MSI_INTEL_ADDR_DM_LOGICAL 0x00000004
63 /* Fields in data for Intel MSI messages. */
64 #define MSI_INTEL_DATA_TRGRMOD IOART_TRGRMOD /* Trigger mode. */
65 # define MSI_INTEL_DATA_TRGREDG IOART_TRGREDG
66 # define MSI_INTEL_DATA_TRGRLVL IOART_TRGRLVL
67 #define MSI_INTEL_DATA_LEVEL 0x00004000 /* Polarity. */
68 # define MSI_INTEL_DATA_DEASSERT 0x00000000
69 # define MSI_INTEL_DATA_ASSERT 0x00004000
70 #define MSI_INTEL_DATA_DELMOD IOART_DELMOD /* Delivery mode. */
71 # define MSI_INTEL_DATA_DELFIXED IOART_DELFIXED
72 # define MSI_INTEL_DATA_DELLOPRI IOART_DELLOPRI
73 # define MSI_INTEL_DATA_DELSMI IOART_DELSMI
74 # define MSI_INTEL_DATA_DELNMI IOART_DELNMI
75 # define MSI_INTEL_DATA_DELINIT IOART_DELINIT
76 # define MSI_INTEL_DATA_DELEXINT IOART_DELEXINT
77 #define MSI_INTEL_DATA_INTVEC IOART_INTVEC /* Interrupt vector. */
80 * Build Intel MSI message and data values from a source. AMD64 systems
81 * seem to be compatible, so we use the same function for both.
83 #define INTEL_ADDR(msi) \
84 (MSI_INTEL_ADDR_BASE | (msi)->msi_cpu << 12 | \
85 MSI_INTEL_ADDR_RH_OFF | MSI_INTEL_ADDR_DM_PHYSICAL)
86 #define INTEL_DATA(msi) \
87 (MSI_INTEL_DATA_TRGREDG | MSI_INTEL_DATA_DELFIXED | (msi)->msi_vector)
89 static MALLOC_DEFINE(M_MSI, "msi", "PCI MSI");
92 * MSI sources are bunched into groups. This is because MSI forces
93 * all of the messages to share the address and data registers and
94 * thus certain properties (such as the local APIC ID target on x86).
95 * Each group has a 'first' source that contains information global to
96 * the group. These fields are marked with (g) below.
98 * Note that local APIC ID is kind of special. Each message will be
99 * assigned an ID by the system; however, a group will use the ID from
102 * For MSI-X, each message is isolated.
105 struct intsrc msi_intsrc;
106 device_t msi_dev; /* Owning device. (g) */
107 struct msi_intsrc *msi_first; /* First source in group. */
108 u_int msi_irq; /* IRQ cookie. */
109 u_int msi_msix; /* MSI-X message. */
110 u_int msi_vector:8; /* IDT vector. */
111 u_int msi_cpu:8; /* Local APIC ID. (g) */
112 u_int msi_count:8; /* Messages in this group. (g) */
115 static void msi_create_source(void);
116 static void msi_enable_source(struct intsrc *isrc);
117 static void msi_disable_source(struct intsrc *isrc, int eoi);
118 static void msi_eoi_source(struct intsrc *isrc);
119 static void msi_enable_intr(struct intsrc *isrc);
120 static void msi_disable_intr(struct intsrc *isrc);
121 static int msi_vector(struct intsrc *isrc);
122 static int msi_source_pending(struct intsrc *isrc);
123 static int msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
124 enum intr_polarity pol);
125 static void msi_assign_cpu(struct intsrc *isrc, u_int apic_id);
127 struct pic msi_pic = { msi_enable_source, msi_disable_source, msi_eoi_source,
128 msi_enable_intr, msi_disable_intr, msi_vector,
129 msi_source_pending, NULL, NULL, msi_config_intr,
132 static int msi_enabled;
133 static int msi_last_irq;
134 static struct mtx msi_lock;
137 msi_enable_source(struct intsrc *isrc)
142 msi_disable_source(struct intsrc *isrc, int eoi)
150 msi_eoi_source(struct intsrc *isrc)
157 msi_enable_intr(struct intsrc *isrc)
159 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
161 apic_enable_vector(msi->msi_vector);
165 msi_disable_intr(struct intsrc *isrc)
167 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
169 apic_disable_vector(msi->msi_vector);
173 msi_vector(struct intsrc *isrc)
175 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
177 return (msi->msi_irq);
181 msi_source_pending(struct intsrc *isrc)
188 msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
189 enum intr_polarity pol)
196 msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
198 struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
200 msi->msi_cpu = apic_id;
202 printf("msi: Assigning %s IRQ %d to local APIC %u\n",
203 msi->msi_msix ? "MSI-X" : "MSI", msi->msi_irq,
205 pci_remap_msi_irq(msi->msi_dev, msi->msi_irq);
212 /* Check if we have a supported CPU. */
213 if (!(strcmp(cpu_vendor, "GenuineIntel") == 0 ||
214 strcmp(cpu_vendor, "AuthenticAMD") == 0))
218 intr_register_pic(&msi_pic);
219 mtx_init(&msi_lock, "msi", NULL, MTX_DEF);
223 msi_create_source(void)
225 struct msi_intsrc *msi;
229 if (msi_last_irq >= NUM_MSI_INTS) {
230 mtx_unlock(&msi_lock);
233 irq = msi_last_irq + FIRST_MSI_INT;
235 mtx_unlock(&msi_lock);
237 msi = malloc(sizeof(struct msi_intsrc), M_MSI, M_WAITOK | M_ZERO);
238 msi->msi_intsrc.is_pic = &msi_pic;
240 intr_register_source(&msi->msi_intsrc);
245 * Try to allocate 'count' interrupt sources with contiguous IDT values. If
246 * we allocate any new sources, then their IRQ values will be at the end of
247 * the irqs[] array, with *newirq being the index of the first new IRQ value
248 * and *newcount being the number of new IRQ values added.
251 msi_alloc(device_t dev, int count, int maxcount, int *irqs)
253 struct msi_intsrc *msi, *fsrc;
262 /* Try to find 'count' free IRQs. */
264 for (i = FIRST_MSI_INT; i < FIRST_MSI_INT + NUM_MSI_INTS; i++) {
265 msi = (struct msi_intsrc *)intr_lookup_source(i);
267 /* End of allocated sources, so break. */
271 /* If this is a free one, save its IRQ in the array. */
272 if (msi->msi_dev == NULL) {
280 /* Do we need to create some new sources? */
282 /* If we would exceed the max, give up. */
283 if (i + (count - cnt) > FIRST_MSI_INT + NUM_MSI_INTS) {
284 mtx_unlock(&msi_lock);
287 mtx_unlock(&msi_lock);
289 /* We need count - cnt more sources. */
290 while (cnt < count) {
297 /* Ok, we now have the IRQs allocated. */
298 KASSERT(cnt == count, ("count mismatch"));
300 /* Allocate 'count' IDT vectors. */
301 vector = apic_alloc_vectors(irqs, count, maxcount);
303 mtx_unlock(&msi_lock);
307 /* Assign IDT vectors and make these messages owned by 'dev'. */
308 fsrc = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
309 for (i = 0; i < count; i++) {
310 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
312 msi->msi_vector = vector + i;
314 printf("msi: routing MSI IRQ %d to vector %u\n",
315 msi->msi_irq, msi->msi_vector);
316 msi->msi_first = fsrc;
317 KASSERT(msi->msi_intsrc.is_handlers == 0,
318 ("dead MSI has handlers"));
320 fsrc->msi_count = count;
321 mtx_unlock(&msi_lock);
327 msi_release(int *irqs, int count)
329 struct msi_intsrc *msi, *first;
333 first = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
335 mtx_unlock(&msi_lock);
339 /* Make sure this isn't an MSI-X message. */
340 if (first->msi_msix) {
341 mtx_unlock(&msi_lock);
345 /* Make sure this message is allocated to a group. */
346 if (first->msi_first == NULL) {
347 mtx_unlock(&msi_lock);
352 * Make sure this is the start of a group and that we are releasing
355 if (first->msi_first != first || first->msi_count != count) {
356 mtx_unlock(&msi_lock);
359 KASSERT(first->msi_dev != NULL, ("unowned group"));
361 /* Clear all the extra messages in the group. */
362 for (i = 1; i < count; i++) {
363 msi = (struct msi_intsrc *)intr_lookup_source(irqs[i]);
364 KASSERT(msi->msi_first == first, ("message not in group"));
365 KASSERT(msi->msi_dev == first->msi_dev, ("owner mismatch"));
366 msi->msi_first = NULL;
368 apic_free_vector(msi->msi_vector, msi->msi_irq);
372 /* Clear out the first message. */
373 first->msi_first = NULL;
374 first->msi_dev = NULL;
375 apic_free_vector(first->msi_vector, first->msi_irq);
376 first->msi_vector = 0;
377 first->msi_count = 0;
379 mtx_unlock(&msi_lock);
384 msi_map(int irq, uint64_t *addr, uint32_t *data)
386 struct msi_intsrc *msi;
389 msi = (struct msi_intsrc *)intr_lookup_source(irq);
391 mtx_unlock(&msi_lock);
395 /* Make sure this message is allocated to a device. */
396 if (msi->msi_dev == NULL) {
397 mtx_unlock(&msi_lock);
402 * If this message isn't an MSI-X message, make sure it's part
403 * of a group, and switch to the first message in the
406 if (!msi->msi_msix) {
407 if (msi->msi_first == NULL) {
408 mtx_unlock(&msi_lock);
411 msi = msi->msi_first;
414 *addr = INTEL_ADDR(msi);
415 *data = INTEL_DATA(msi);
416 mtx_unlock(&msi_lock);
421 msix_alloc(device_t dev, int *irq)
423 struct msi_intsrc *msi;
432 /* Find a free IRQ. */
433 for (i = FIRST_MSI_INT; i < FIRST_MSI_INT + NUM_MSI_INTS; i++) {
434 msi = (struct msi_intsrc *)intr_lookup_source(i);
436 /* End of allocated sources, so break. */
440 /* Stop at the first free source. */
441 if (msi->msi_dev == NULL)
445 /* Do we need to create a new source? */
447 /* If we would exceed the max, give up. */
448 if (i + 1 > FIRST_MSI_INT + NUM_MSI_INTS) {
449 mtx_unlock(&msi_lock);
452 mtx_unlock(&msi_lock);
454 /* Create a new source. */
459 /* Allocate an IDT vector. */
460 vector = apic_alloc_vector(i);
462 printf("msi: routing MSI-X IRQ %d to vector %u\n", msi->msi_irq,
467 msi->msi_vector = vector;
470 KASSERT(msi->msi_intsrc.is_handlers == 0, ("dead MSI-X has handlers"));
471 mtx_unlock(&msi_lock);
478 msix_release(int irq)
480 struct msi_intsrc *msi;
483 msi = (struct msi_intsrc *)intr_lookup_source(irq);
485 mtx_unlock(&msi_lock);
489 /* Make sure this is an MSI-X message. */
490 if (!msi->msi_msix) {
491 mtx_unlock(&msi_lock);
495 KASSERT(msi->msi_dev != NULL, ("unowned message"));
497 /* Clear out the message. */
499 apic_free_vector(msi->msi_vector, msi->msi_irq);
503 mtx_unlock(&msi_lock);