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