]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/msi.c
This commit was generated by cvs2svn to compensate for changes in r169695,
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / msi.c
1 /*-
2  * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 /*
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.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/sx.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>
53
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
62
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. */
78
79 /*
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.
82  */
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)
88
89 static MALLOC_DEFINE(M_MSI, "msi", "PCI MSI");
90
91 /*
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.
97  *
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
100  * the first message.
101  *
102  * For MSI-X, each message is isolated.
103  */
104 struct msi_intsrc {
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) */
113 };
114
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);
126
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,
130                        msi_assign_cpu };
131
132 static int msi_enabled;
133 static int msi_last_irq;
134 static struct mtx msi_lock;
135
136 static void
137 msi_enable_source(struct intsrc *isrc)
138 {
139 }
140
141 static void
142 msi_disable_source(struct intsrc *isrc, int eoi)
143 {
144
145         if (eoi == PIC_EOI)
146                 lapic_eoi();
147 }
148
149 static void
150 msi_eoi_source(struct intsrc *isrc)
151 {
152
153         lapic_eoi();
154 }
155
156 static void
157 msi_enable_intr(struct intsrc *isrc)
158 {
159         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
160
161         apic_enable_vector(msi->msi_vector);
162 }
163
164 static void
165 msi_disable_intr(struct intsrc *isrc)
166 {
167         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
168
169         apic_disable_vector(msi->msi_vector);
170 }
171
172 static int
173 msi_vector(struct intsrc *isrc)
174 {
175         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
176
177         return (msi->msi_irq);
178 }
179
180 static int
181 msi_source_pending(struct intsrc *isrc)
182 {
183
184         return (0);
185 }
186
187 static int
188 msi_config_intr(struct intsrc *isrc, enum intr_trigger trig,
189     enum intr_polarity pol)
190 {
191
192         return (ENODEV);
193 }
194
195 static void
196 msi_assign_cpu(struct intsrc *isrc, u_int apic_id)
197 {
198         struct msi_intsrc *msi = (struct msi_intsrc *)isrc;
199
200         msi->msi_cpu = apic_id;
201         if (bootverbose)
202                 printf("msi: Assigning %s IRQ %d to local APIC %u\n",
203                     msi->msi_msix ? "MSI-X" : "MSI", msi->msi_irq,
204                     msi->msi_cpu);      
205         pci_remap_msi_irq(msi->msi_dev, msi->msi_irq);
206 }
207
208 void
209 msi_init(void)
210 {
211
212         /* Check if we have a supported CPU. */
213         if (!(strcmp(cpu_vendor, "GenuineIntel") == 0 ||
214               strcmp(cpu_vendor, "AuthenticAMD") == 0))
215                 return;
216
217         msi_enabled = 1;
218         intr_register_pic(&msi_pic);
219         mtx_init(&msi_lock, "msi", NULL, MTX_DEF);
220 }
221
222 void
223 msi_create_source(void)
224 {
225         struct msi_intsrc *msi;
226         u_int irq;
227
228         mtx_lock(&msi_lock);
229         if (msi_last_irq >= NUM_MSI_INTS) {
230                 mtx_unlock(&msi_lock);
231                 return;
232         }
233         irq = msi_last_irq + FIRST_MSI_INT;
234         msi_last_irq++;
235         mtx_unlock(&msi_lock);
236
237         msi = malloc(sizeof(struct msi_intsrc), M_MSI, M_WAITOK | M_ZERO);      
238         msi->msi_intsrc.is_pic = &msi_pic;
239         msi->msi_irq = irq;
240         intr_register_source(&msi->msi_intsrc);
241         nexus_add_irq(irq);
242 }
243
244 /*
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.
249  */
250 int
251 msi_alloc(device_t dev, int count, int maxcount, int *irqs)
252 {
253         struct msi_intsrc *msi, *fsrc;
254         int cnt, i, vector;
255
256         if (!msi_enabled)
257                 return (ENXIO);
258
259 again:
260         mtx_lock(&msi_lock);
261
262         /* Try to find 'count' free IRQs. */
263         cnt = 0;
264         for (i = FIRST_MSI_INT; i < FIRST_MSI_INT + NUM_MSI_INTS; i++) {
265                 msi = (struct msi_intsrc *)intr_lookup_source(i);
266
267                 /* End of allocated sources, so break. */
268                 if (msi == NULL)
269                         break;
270
271                 /* If this is a free one, save its IRQ in the array. */
272                 if (msi->msi_dev == NULL) {
273                         irqs[cnt] = i;
274                         cnt++;
275                         if (cnt == count)
276                                 break;
277                 }
278         }
279
280         /* Do we need to create some new sources? */
281         if (cnt < count) {
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);
285                         return (ENXIO);
286                 }
287                 mtx_unlock(&msi_lock);
288
289                 /* We need count - cnt more sources. */
290                 while (cnt < count) {
291                         msi_create_source();
292                         cnt++;
293                 }
294                 goto again;
295         }
296
297         /* Ok, we now have the IRQs allocated. */
298         KASSERT(cnt == count, ("count mismatch"));
299
300         /* Allocate 'count' IDT vectors. */
301         vector = apic_alloc_vectors(irqs, count, maxcount);
302         if (vector == 0) {
303                 mtx_unlock(&msi_lock);
304                 return (ENOSPC);
305         }
306
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]);
311                 msi->msi_dev = dev;
312                 msi->msi_vector = vector + i;
313                 if (bootverbose)
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"));
319         }
320         fsrc->msi_count = count;
321         mtx_unlock(&msi_lock);
322
323         return (0);
324 }
325
326 int
327 msi_release(int *irqs, int count)
328 {
329         struct msi_intsrc *msi, *first;
330         int i;
331
332         mtx_lock(&msi_lock);
333         first = (struct msi_intsrc *)intr_lookup_source(irqs[0]);
334         if (first == NULL) {
335                 mtx_unlock(&msi_lock);
336                 return (ENOENT);
337         }
338
339         /* Make sure this isn't an MSI-X message. */
340         if (first->msi_msix) {
341                 mtx_unlock(&msi_lock);
342                 return (EINVAL);
343         }
344
345         /* Make sure this message is allocated to a group. */
346         if (first->msi_first == NULL) {
347                 mtx_unlock(&msi_lock);
348                 return (ENXIO);
349         }
350
351         /*
352          * Make sure this is the start of a group and that we are releasing
353          * the entire group.
354          */
355         if (first->msi_first != first || first->msi_count != count) {
356                 mtx_unlock(&msi_lock);
357                 return (EINVAL);
358         }
359         KASSERT(first->msi_dev != NULL, ("unowned group"));
360
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;
367                 msi->msi_dev = NULL;
368                 apic_free_vector(msi->msi_vector, msi->msi_irq);
369                 msi->msi_vector = 0;
370         }
371
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;
378
379         mtx_unlock(&msi_lock);
380         return (0);
381 }
382
383 int
384 msi_map(int irq, uint64_t *addr, uint32_t *data)
385 {
386         struct msi_intsrc *msi;
387
388         mtx_lock(&msi_lock);
389         msi = (struct msi_intsrc *)intr_lookup_source(irq);
390         if (msi == NULL) {
391                 mtx_unlock(&msi_lock);
392                 return (ENOENT);
393         }
394
395         /* Make sure this message is allocated to a device. */
396         if (msi->msi_dev == NULL) {
397                 mtx_unlock(&msi_lock);
398                 return (ENXIO);
399         }
400
401         /*
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
404          * group.
405          */
406         if (!msi->msi_msix) {
407                 if (msi->msi_first == NULL) {
408                         mtx_unlock(&msi_lock);
409                         return (ENXIO);
410                 }
411                 msi = msi->msi_first;
412         }
413
414         *addr = INTEL_ADDR(msi);
415         *data = INTEL_DATA(msi);
416         mtx_unlock(&msi_lock);
417         return (0);
418 }
419
420 int
421 msix_alloc(device_t dev, int *irq)
422 {
423         struct msi_intsrc *msi;
424         int i, vector;
425
426         if (!msi_enabled)
427                 return (ENXIO);
428
429 again:
430         mtx_lock(&msi_lock);
431
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);
435
436                 /* End of allocated sources, so break. */
437                 if (msi == NULL)
438                         break;
439
440                 /* Stop at the first free source. */
441                 if (msi->msi_dev == NULL)
442                         break;
443         }
444
445         /* Do we need to create a new source? */
446         if (msi == NULL) {
447                 /* If we would exceed the max, give up. */
448                 if (i + 1 > FIRST_MSI_INT + NUM_MSI_INTS) {
449                         mtx_unlock(&msi_lock);
450                         return (ENXIO);
451                 }
452                 mtx_unlock(&msi_lock);
453
454                 /* Create a new source. */
455                 msi_create_source();
456                 goto again;
457         }
458
459         /* Allocate an IDT vector. */
460         vector = apic_alloc_vector(i);
461         if (bootverbose)
462                 printf("msi: routing MSI-X IRQ %d to vector %u\n", msi->msi_irq,
463                     vector);
464
465         /* Setup source. */
466         msi->msi_dev = dev;
467         msi->msi_vector = vector;
468         msi->msi_msix = 1;
469
470         KASSERT(msi->msi_intsrc.is_handlers == 0, ("dead MSI-X has handlers"));
471         mtx_unlock(&msi_lock);
472
473         *irq = i;
474         return (0);
475 }
476
477 int
478 msix_release(int irq)
479 {
480         struct msi_intsrc *msi;
481
482         mtx_lock(&msi_lock);
483         msi = (struct msi_intsrc *)intr_lookup_source(irq);
484         if (msi == NULL) {
485                 mtx_unlock(&msi_lock);
486                 return (ENOENT);
487         }
488
489         /* Make sure this is an MSI-X message. */
490         if (!msi->msi_msix) {
491                 mtx_unlock(&msi_lock);
492                 return (EINVAL);
493         }
494
495         KASSERT(msi->msi_dev != NULL, ("unowned message"));
496
497         /* Clear out the message. */
498         msi->msi_dev = NULL;
499         apic_free_vector(msi->msi_vector, msi->msi_irq);
500         msi->msi_vector = 0;
501         msi->msi_msix = 0;
502
503         mtx_unlock(&msi_lock);
504         return (0);
505 }