x86_64/ioapic: Cleanup header inclusion
[dragonfly.git] / sys / platform / pc64 / apic / ioapic.c
1 /*
2  * Copyright (c) 1996, by Steve Passe
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. The name of the developer may NOT be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
26  */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/bus.h>
32 #include <sys/machintr.h>
33 #include <sys/thread2.h>
34
35 #include <machine/pmap.h>
36 #include <machine_base/icu/icu_var.h>
37 #include <machine_base/apic/lapic.h>
38 #include <machine_base/apic/ioapic.h>
39 #include <machine_base/apic/ioapic_abi.h>
40
41 #define IOAPIC_COUNT_MAX        16
42 #define IOAPIC_ID_MASK          (IOAPIC_COUNT_MAX - 1)
43
44 struct ioapic_info {
45         int             io_idx;
46         int             io_apic_id;
47         void            *io_addr;
48         int             io_npin;
49         int             io_gsi_base;
50
51         TAILQ_ENTRY(ioapic_info) io_link;
52 };
53 TAILQ_HEAD(ioapic_info_list, ioapic_info);
54
55 struct ioapic_intsrc {
56         int             int_gsi;
57         enum intr_trigger int_trig;
58         enum intr_polarity int_pola;
59 };
60
61 struct ioapic_conf {
62         struct ioapic_info_list ioc_list;
63         struct ioapic_intsrc ioc_intsrc[16];    /* XXX magic number */
64 };
65
66 static void     ioapic_setup(const struct ioapic_info *);
67 static int      ioapic_alloc_apic_id(int);
68 static void     ioapic_set_apic_id(const struct ioapic_info *);
69 static void     ioapic_gsi_setup(int);
70 static const struct ioapic_info *
71                 ioapic_gsi_search(int);
72 static void     ioapic_pin_prog(void *, int, int,
73                     enum intr_trigger, enum intr_polarity, uint32_t);
74
75 static struct ioapic_conf       ioapic_conf;
76
77 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
78         TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
79
80 int             ioapic_enable = 1; /* I/O APIC is enabled by default */
81
82 int
83 ioapic_config(void)
84 {
85         struct ioapic_enumerator *e;
86         struct ioapic_info *info;
87         int start_apic_id = 0;
88         int error, i, probe;
89         register_t ef = 0;
90
91         TAILQ_INIT(&ioapic_conf.ioc_list);
92         /* XXX magic number */
93         for (i = 0; i < 16; ++i)
94                 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
95
96         probe = 1;
97         TUNABLE_INT_FETCH("hw.ioapic_probe", &probe);
98         if (!probe) {
99                 kprintf("IOAPIC: warning I/O APIC will not be probed\n");
100                 return ENXIO;
101         }
102
103         TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
104                 error = e->ioapic_probe(e);
105                 if (!error)
106                         break;
107         }
108         if (e == NULL) {
109                 kprintf("IOAPIC: can't find I/O APIC\n");
110                 return ENXIO;
111         }
112
113         crit_enter();
114
115         ef = read_rflags();
116         cpu_disable_intr();
117
118         /*
119          * Switch to I/O APIC MachIntrABI and reconfigure
120          * the default IDT entries.
121          */
122         MachIntrABI = MachIntrABI_IOAPIC;
123         MachIntrABI.setdefault();
124
125         e->ioapic_enumerate(e);
126
127         /*
128          * Setup index
129          */
130         i = 0;
131         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
132                 info->io_idx = i++;
133
134         if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
135                 panic("ioapic_config: more than 16 I/O APIC\n");
136
137         /*
138          * Setup APIC ID
139          */
140         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
141                 int apic_id;
142
143                 apic_id = ioapic_alloc_apic_id(start_apic_id);
144                 if (apic_id == NAPICID) {
145                         kprintf("IOAPIC: can't alloc APIC ID for "
146                                 "%dth I/O APIC\n", info->io_idx);
147                         break;
148                 }
149                 info->io_apic_id = apic_id;
150
151                 start_apic_id = apic_id + 1;
152         }
153         if (info != NULL) {
154                 /*
155                  * xAPIC allows I/O APIC's APIC ID to be same
156                  * as the LAPIC's APIC ID
157                  */
158                 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
159                         "for I/O APIC\n");
160
161                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
162                         info->io_apic_id = info->io_idx;
163         }
164
165         /*
166          * Warning about any GSI holes
167          */
168         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
169                 const struct ioapic_info *prev_info;
170
171                 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
172                 if (prev_info != NULL) {
173                         if (info->io_gsi_base !=
174                         prev_info->io_gsi_base + prev_info->io_npin) {
175                                 kprintf("IOAPIC: warning gsi hole "
176                                         "[%d, %d]\n",
177                                         prev_info->io_gsi_base +
178                                         prev_info->io_npin,
179                                         info->io_gsi_base - 1);
180                         }
181                 }
182         }
183
184         if (bootverbose) {
185                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
186                         kprintf("IOAPIC: idx %d, apic id %d, "
187                                 "gsi base %d, npin %d\n",
188                                 info->io_idx,
189                                 info->io_apic_id,
190                                 info->io_gsi_base,
191                                 info->io_npin);
192                 }
193         }
194
195         /*
196          * Setup all I/O APIC
197          */
198         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
199                 ioapic_setup(info);
200         ioapic_abi_fixup_irqmap();
201
202         write_rflags(ef);
203
204         MachIntrABI.cleanup();
205
206         crit_exit();
207
208         return 0;
209 }
210
211 void
212 ioapic_enumerator_register(struct ioapic_enumerator *ne)
213 {
214         struct ioapic_enumerator *e;
215
216         TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
217                 if (e->ioapic_prio < ne->ioapic_prio) {
218                         TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
219                         return;
220                 }
221         }
222         TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
223 }
224
225 void
226 ioapic_add(void *addr, int gsi_base, int npin)
227 {
228         struct ioapic_info *info, *ninfo;
229         int gsi_end;
230
231         gsi_end = gsi_base + npin - 1;
232         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
233                 if ((gsi_base >= info->io_gsi_base &&
234                      gsi_base < info->io_gsi_base + info->io_npin) ||
235                     (gsi_end >= info->io_gsi_base &&
236                      gsi_end < info->io_gsi_base + info->io_npin)) {
237                         panic("ioapic_add: overlapped gsi, base %d npin %d, "
238                               "hit base %d, npin %d\n", gsi_base, npin,
239                               info->io_gsi_base, info->io_npin);
240                 }
241                 if (info->io_addr == addr)
242                         panic("ioapic_add: duplicated addr %p\n", addr);
243         }
244
245         ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
246         ninfo->io_addr = addr;
247         ninfo->io_npin = npin;
248         ninfo->io_gsi_base = gsi_base;
249         ninfo->io_apic_id = -1;
250
251         /*
252          * Create IOAPIC list in ascending order of GSI base
253          */
254         TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
255             ioapic_info_list, io_link) {
256                 if (ninfo->io_gsi_base > info->io_gsi_base) {
257                         TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
258                             info, ninfo, io_link);
259                         break;
260                 }
261         }
262         if (info == NULL)
263                 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
264 }
265
266 void
267 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
268 {
269         struct ioapic_intsrc *int_src;
270
271         KKASSERT(irq < 16);
272         int_src = &ioapic_conf.ioc_intsrc[irq];
273
274         if (gsi == 0) {
275                 /* Don't allow mixed mode */
276                 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
277                 return;
278         }
279
280         if (int_src->int_gsi != -1) {
281                 if (int_src->int_gsi != gsi) {
282                         kprintf("IOAPIC: warning intsrc irq %d, gsi "
283                                 "%d -> %d\n", irq, int_src->int_gsi, gsi);
284                 }
285                 if (int_src->int_trig != trig) {
286                         kprintf("IOAPIC: warning intsrc irq %d, trig "
287                                 "%s -> %s\n", irq,
288                                 intr_str_trigger(int_src->int_trig),
289                                 intr_str_trigger(trig));
290                 }
291                 if (int_src->int_pola != pola) {
292                         kprintf("IOAPIC: warning intsrc irq %d, pola "
293                                 "%s -> %s\n", irq,
294                                 intr_str_polarity(int_src->int_pola),
295                                 intr_str_polarity(pola));
296                 }
297         }
298         int_src->int_gsi = gsi;
299         int_src->int_trig = trig;
300         int_src->int_pola = pola;
301 }
302
303 static void
304 ioapic_set_apic_id(const struct ioapic_info *info)
305 {
306         uint32_t id;
307         int apic_id;
308
309         id = ioapic_read(info->io_addr, IOAPIC_ID);
310
311         id &= ~APIC_ID_MASK;
312         id |= (info->io_apic_id << 24);
313
314         ioapic_write(info->io_addr, IOAPIC_ID, id);
315
316         /*
317          * Re-read && test
318          */
319         id = ioapic_read(info->io_addr, IOAPIC_ID);
320         apic_id = (id & APIC_ID_MASK) >> 24;
321
322         /*
323          * I/O APIC ID is a 4bits field
324          */
325         if ((apic_id & IOAPIC_ID_MASK) !=
326             (info->io_apic_id & IOAPIC_ID_MASK)) {
327                 panic("ioapic_set_apic_id: can't set apic id to %d, "
328                       "currently set to %d\n", info->io_apic_id, apic_id);
329         }
330 }
331
332 static void
333 ioapic_gsi_setup(int gsi)
334 {
335         enum intr_trigger trig;
336         enum intr_polarity pola;
337         int irq;
338
339         if (gsi == 0) {
340                 /* ExtINT */
341                 imen_lock();
342                 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
343                     ioapic_gsi_pin(gsi), 0);
344                 imen_unlock();
345                 return;
346         }
347
348         for (irq = 0; irq < 16; ++irq) {
349                 const struct ioapic_intsrc *int_src =
350                     &ioapic_conf.ioc_intsrc[irq];
351
352                 if (gsi == int_src->int_gsi) {
353                         trig = int_src->int_trig;
354                         pola = int_src->int_pola;
355                         break;
356                 }
357         }
358
359         if (irq == 16) {
360                 if (gsi < 16) {
361                         trig = INTR_TRIGGER_EDGE;
362                         pola = INTR_POLARITY_HIGH;
363                 } else {
364                         trig = INTR_TRIGGER_LEVEL;
365                         pola = INTR_POLARITY_LOW;
366                 }
367                 irq = gsi;
368         }
369
370         ioapic_abi_set_irqmap(irq, gsi, trig, pola);
371 }
372
373 void *
374 ioapic_gsi_ioaddr(int gsi)
375 {
376         const struct ioapic_info *info;
377
378         info = ioapic_gsi_search(gsi);
379         return info->io_addr;
380 }
381
382 int
383 ioapic_gsi_pin(int gsi)
384 {
385         const struct ioapic_info *info;
386
387         info = ioapic_gsi_search(gsi);
388         return gsi - info->io_gsi_base;
389 }
390
391 static const struct ioapic_info *
392 ioapic_gsi_search(int gsi)
393 {
394         const struct ioapic_info *info;
395
396         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
397                 if (gsi >= info->io_gsi_base &&
398                     gsi < info->io_gsi_base + info->io_npin)
399                         return info;
400         }
401         panic("ioapic_gsi_search: no I/O APIC\n");
402 }
403
404 int
405 ioapic_gsi(int idx, int pin)
406 {
407         const struct ioapic_info *info;
408
409         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
410                 if (info->io_idx == idx)
411                         break;
412         }
413         if (info == NULL)
414                 return -1;
415         if (pin >= info->io_npin)
416                 return -1;
417         return info->io_gsi_base + pin;
418 }
419
420 void
421 ioapic_extpin_setup(void *addr, int pin, int vec)
422 {
423         ioapic_pin_prog(addr, pin, vec,
424             INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
425 }
426
427 int
428 ioapic_extpin_gsi(void)
429 {
430         return 0;
431 }
432
433 void
434 ioapic_pin_setup(void *addr, int pin, int vec,
435     enum intr_trigger trig, enum intr_polarity pola)
436 {
437         /*
438          * Always clear an I/O APIC pin before [re]programming it.  This is
439          * particularly important if the pin is set up for a level interrupt
440          * as the IOART_REM_IRR bit might be set.   When we reprogram the
441          * vector any EOI from pending ints on this pin could be lost and
442          * IRR might never get reset.
443          *
444          * To fix this problem, clear the vector and make sure it is 
445          * programmed as an edge interrupt.  This should theoretically
446          * clear IRR so we can later, safely program it as a level 
447          * interrupt.
448          */
449         ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
450             IOART_DELFIXED);
451         ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
452 }
453
454 static void
455 ioapic_pin_prog(void *addr, int pin, int vec,
456     enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
457 {
458         uint32_t flags, target;
459         int select;
460
461         KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
462
463         select = IOAPIC_REDTBL0 + (2 * pin);
464
465         flags = ioapic_read(addr, select) & IOART_RESV;
466         flags |= IOART_INTMSET | IOART_DESTPHY;
467 #ifdef foo
468         flags |= del_mode;
469 #else
470         /*
471          * We only support limited I/O APIC mixed mode,
472          * so even for ExtINT, we still use "fixed"
473          * delivery mode.
474          */
475         flags |= IOART_DELFIXED;
476 #endif
477
478         if (del_mode == IOART_DELEXINT) {
479                 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
480                          pola == INTR_POLARITY_CONFORM);
481                 flags |= IOART_TRGREDG | IOART_INTAHI;
482         } else {
483                 switch (trig) {
484                 case INTR_TRIGGER_EDGE:
485                         flags |= IOART_TRGREDG;
486                         break;
487
488                 case INTR_TRIGGER_LEVEL:
489                         flags |= IOART_TRGRLVL;
490                         break;
491
492                 case INTR_TRIGGER_CONFORM:
493                         panic("ioapic_pin_prog: trig conform is not "
494                               "supported\n");
495                 }
496                 switch (pola) {
497                 case INTR_POLARITY_HIGH:
498                         flags |= IOART_INTAHI;
499                         break;
500
501                 case INTR_POLARITY_LOW:
502                         flags |= IOART_INTALO;
503                         break;
504
505                 case INTR_POLARITY_CONFORM:
506                         panic("ioapic_pin_prog: pola conform is not "
507                               "supported\n");
508                 }
509         }
510
511         target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
512         target |= (CPUID_TO_APICID(0) << IOART_HI_DEST_SHIFT) &
513                   IOART_HI_DEST_MASK;
514
515         ioapic_write(addr, select, flags | vec);
516         ioapic_write(addr, select + 1, target);
517 }
518
519 static void
520 ioapic_setup(const struct ioapic_info *info)
521 {
522         int i;
523
524         ioapic_set_apic_id(info);
525
526         for (i = 0; i < info->io_npin; ++i)
527                 ioapic_gsi_setup(info->io_gsi_base + i);
528 }
529
530 static int
531 ioapic_alloc_apic_id(int start)
532 {
533         for (;;) {
534                 const struct ioapic_info *info;
535                 int apic_id, apic_id16;
536
537                 apic_id = lapic_unused_apic_id(start);
538                 if (apic_id == NAPICID) {
539                         kprintf("IOAPIC: can't find unused APIC ID\n");
540                         return apic_id;
541                 }
542                 apic_id16 = apic_id & IOAPIC_ID_MASK;
543
544                 /*
545                  * Check against other I/O APIC's APIC ID's lower 4bits.
546                  *
547                  * The new APIC ID will have to be different from others
548                  * in the lower 4bits, no matter whether xAPIC is used
549                  * or not.
550                  */
551                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
552                         if (info->io_apic_id == -1) {
553                                 info = NULL;
554                                 break;
555                         }
556                         if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
557                                 break;
558                 }
559                 if (info == NULL)
560                         return apic_id;
561
562                 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
563                         "%dth I/O APIC, keep searching...\n",
564                         apic_id, info->io_idx);
565
566                 start = apic_id + 1;
567         }
568         panic("ioapic_unused_apic_id: never reached\n");
569 }
570
571 /*
572  * Map a physical memory address representing I/O into KVA.  The I/O
573  * block is assumed not to cross a page boundary.
574  */
575 void *
576 ioapic_map(vm_paddr_t pa)
577 {
578         KKASSERT(pa < 0x100000000LL);
579
580         return pmap_mapdev_uncacheable(pa, PAGE_SIZE);
581 }
582
583 static void
584 ioapic_sysinit(void *dummy __unused)
585 {
586         int error;
587
588         if (!ioapic_enable)
589                 return;
590
591         KASSERT(lapic_enable, ("I/O APIC is enabled, but LAPIC is disabled\n"));
592         error = ioapic_config();
593         if (error) {
594                 ioapic_enable = 0;
595                 icu_reinit_noioapic();
596                 lapic_fixup_noioapic();
597         }
598 }
599 SYSINIT(ioapic, SI_BOOT2_IOAPIC, SI_ORDER_FIRST, ioapic_sysinit, NULL)