Merge branch 'vendor/GREP'
[dragonfly.git] / sys / platform / pc32 / apic / mpapic.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 <machine/globaldata.h>
34 #include <machine/smp.h>
35 #include <machine/cputypes.h>
36 #include <machine/md_var.h>
37 #include <machine/pmap.h>
38 #include <machine_base/apic/mpapic.h>
39 #include <machine_base/apic/ioapic_abi.h>
40 #include <machine/segments.h>
41 #include <sys/thread2.h>
42
43 #include <machine/intr_machdep.h>
44
45 #define IOAPIC_COUNT_MAX        16
46 #define IOAPIC_ID_MASK          (IOAPIC_COUNT_MAX - 1)
47
48 /* XXX */
49 extern pt_entry_t *SMPpt;
50
51 /* EISA Edge/Level trigger control registers */
52 #define ELCR0   0x4d0                   /* eisa irq 0-7 */
53 #define ELCR1   0x4d1                   /* eisa irq 8-15 */
54
55 struct ioapic_info {
56         int             io_idx;
57         int             io_apic_id;
58         void            *io_addr;
59         int             io_npin;
60         int             io_gsi_base;
61
62         TAILQ_ENTRY(ioapic_info) io_link;
63 };
64 TAILQ_HEAD(ioapic_info_list, ioapic_info);
65
66 struct ioapic_intsrc {
67         int             int_gsi;
68         enum intr_trigger int_trig;
69         enum intr_polarity int_pola;
70 };
71
72 struct ioapic_conf {
73         struct ioapic_info_list ioc_list;
74         struct ioapic_intsrc ioc_intsrc[16];    /* XXX magic number */
75 };
76
77 static void     lapic_timer_calibrate(void);
78 static void     lapic_timer_set_divisor(int);
79 static void     lapic_timer_fixup_handler(void *);
80 static void     lapic_timer_restart_handler(void *);
81
82 void            lapic_timer_process(void);
83 void            lapic_timer_process_frame(struct intrframe *);
84
85 static int      lapic_timer_enable = 1;
86 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
87
88 static void     lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
89 static void     lapic_timer_intr_enable(struct cputimer_intr *);
90 static void     lapic_timer_intr_restart(struct cputimer_intr *);
91 static void     lapic_timer_intr_pmfixup(struct cputimer_intr *);
92
93 static int      lapic_unused_apic_id(int);
94
95 static void     ioapic_setup(const struct ioapic_info *);
96 static int      ioapic_alloc_apic_id(int);
97 static void     ioapic_set_apic_id(const struct ioapic_info *);
98 static void     ioapic_gsi_setup(int);
99 static const struct ioapic_info *
100                 ioapic_gsi_search(int);
101 static void     ioapic_pin_prog(void *, int, int,
102                     enum intr_trigger, enum intr_polarity, uint32_t);
103
104 static struct cputimer_intr lapic_cputimer_intr = {
105         .freq = 0,
106         .reload = lapic_timer_intr_reload,
107         .enable = lapic_timer_intr_enable,
108         .config = cputimer_intr_default_config,
109         .restart = lapic_timer_intr_restart,
110         .pmfixup = lapic_timer_intr_pmfixup,
111         .initclock = cputimer_intr_default_initclock,
112         .next = SLIST_ENTRY_INITIALIZER,
113         .name = "lapic",
114         .type = CPUTIMER_INTR_LAPIC,
115         .prio = CPUTIMER_INTR_PRIO_LAPIC,
116         .caps = CPUTIMER_INTR_CAP_NONE
117 };
118
119 /*
120  * pointers to pmapped apic hardware.
121  */
122
123 volatile ioapic_t       **ioapic;
124
125 static int              lapic_timer_divisor_idx = -1;
126 static const uint32_t   lapic_timer_divisors[] = {
127         APIC_TDCR_2,    APIC_TDCR_4,    APIC_TDCR_8,    APIC_TDCR_16,
128         APIC_TDCR_32,   APIC_TDCR_64,   APIC_TDCR_128,  APIC_TDCR_1
129 };
130 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
131
132 int                     lapic_id_max;
133
134 static struct ioapic_conf       ioapic_conf;
135
136 /*
137  * Enable LAPIC, configure interrupts.
138  */
139 void
140 lapic_init(boolean_t bsp)
141 {
142         uint32_t timer;
143         u_int   temp;
144
145         /*
146          * Install vectors
147          *
148          * Since IDT is shared between BSP and APs, these vectors
149          * only need to be installed once; we do it on BSP.
150          */
151         if (bsp) {
152                 /* Install a 'Spurious INTerrupt' vector */
153                 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
154                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
155
156                 /* Install an inter-CPU IPI for TLB invalidation */
157                 setidt(XINVLTLB_OFFSET, Xinvltlb,
158                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
159
160                 /* Install an inter-CPU IPI for IPIQ messaging */
161                 setidt(XIPIQ_OFFSET, Xipiq,
162                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
163
164                 /* Install a timer vector */
165                 setidt(XTIMER_OFFSET, Xtimer,
166                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
167                 
168                 /* Install an inter-CPU IPI for CPU stop/restart */
169                 setidt(XCPUSTOP_OFFSET, Xcpustop,
170                     SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
171         }
172
173         /*
174          * Setup LINT0 as ExtINT on the BSP.  This is theoretically an
175          * aggregate interrupt input from the 8259.  The INTA cycle
176          * will be routed to the external controller (the 8259) which
177          * is expected to supply the vector.
178          *
179          * Must be setup edge triggered, active high.
180          *
181          * Disable LINT0 on BSP, if I/O APIC is enabled.
182          *
183          * Disable LINT0 on the APs.  It doesn't matter what delivery
184          * mode we use because we leave it masked.
185          */
186         temp = lapic.lvt_lint0;
187         temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK | 
188                   APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
189         if (bsp) {
190                 temp |= APIC_LVT_DM_EXTINT;
191                 if (apic_io_enable)
192                         temp |= APIC_LVT_MASKED;
193         } else {
194                 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
195         }
196         lapic.lvt_lint0 = temp;
197
198         /*
199          * Setup LINT1 as NMI.
200          *
201          * Must be setup edge trigger, active high.
202          *
203          * Enable LINT1 on BSP, if I/O APIC is enabled.
204          *
205          * Disable LINT1 on the APs.
206          */
207         temp = lapic.lvt_lint1;
208         temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK | 
209                   APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
210         temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
211         if (bsp && apic_io_enable)
212                 temp &= ~APIC_LVT_MASKED;
213         lapic.lvt_lint1 = temp;
214
215         /*
216          * Mask the LAPIC error interrupt, LAPIC performance counter
217          * interrupt.
218          */
219         lapic.lvt_error = lapic.lvt_error | APIC_LVT_MASKED;
220         lapic.lvt_pcint = lapic.lvt_pcint | APIC_LVT_MASKED;
221
222         /*
223          * Set LAPIC timer vector and mask the LAPIC timer interrupt.
224          */
225         timer = lapic.lvt_timer;
226         timer &= ~APIC_LVTT_VECTOR;
227         timer |= XTIMER_OFFSET;
228         timer |= APIC_LVTT_MASKED;
229         lapic.lvt_timer = timer;
230
231         /*
232          * Set the Task Priority Register as needed.   At the moment allow
233          * interrupts on all cpus (the APs will remain CLId until they are
234          * ready to deal).  We could disable all but IPIs by setting
235          * temp |= TPR_IPI for cpu != 0.
236          */
237         temp = lapic.tpr;
238         temp &= ~APIC_TPR_PRIO;         /* clear priority field */
239 #ifdef SMP /* APIC-IO */
240 if (!apic_io_enable) {
241 #endif
242         /*
243          * If we are NOT running the IO APICs, the LAPIC will only be used
244          * for IPIs.  Set the TPR to prevent any unintentional interrupts.
245          */
246         temp |= TPR_IPI;
247 #ifdef SMP /* APIC-IO */
248 }
249 #endif
250
251         lapic.tpr = temp;
252
253         /* 
254          * Enable the LAPIC 
255          */
256         temp = lapic.svr;
257         temp |= APIC_SVR_ENABLE;        /* enable the LAPIC */
258         temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
259
260         /*
261          * Set the spurious interrupt vector.  The low 4 bits of the vector
262          * must be 1111.
263          */
264         if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
265                 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
266         temp &= ~APIC_SVR_VECTOR;
267         temp |= XSPURIOUSINT_OFFSET;
268
269         lapic.svr = temp;
270
271         /*
272          * Pump out a few EOIs to clean out interrupts that got through
273          * before we were able to set the TPR.
274          */
275         lapic.eoi = 0;
276         lapic.eoi = 0;
277         lapic.eoi = 0;
278
279         if (bsp) {
280                 lapic_timer_calibrate();
281                 if (lapic_timer_enable) {
282                         cputimer_intr_register(&lapic_cputimer_intr);
283                         cputimer_intr_select(&lapic_cputimer_intr, 0);
284                 }
285         } else {
286                 lapic_timer_set_divisor(lapic_timer_divisor_idx);
287         }
288
289         if (bootverbose)
290                 apic_dump("apic_initialize()");
291 }
292
293 static void
294 lapic_timer_set_divisor(int divisor_idx)
295 {
296         KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
297         lapic.dcr_timer = lapic_timer_divisors[divisor_idx];
298 }
299
300 static void
301 lapic_timer_oneshot(u_int count)
302 {
303         uint32_t value;
304
305         value = lapic.lvt_timer;
306         value &= ~APIC_LVTT_PERIODIC;
307         lapic.lvt_timer = value;
308         lapic.icr_timer = count;
309 }
310
311 static void
312 lapic_timer_oneshot_quick(u_int count)
313 {
314         lapic.icr_timer = count;
315 }
316
317 static void
318 lapic_timer_calibrate(void)
319 {
320         sysclock_t value;
321
322         /* Try to calibrate the local APIC timer. */
323         for (lapic_timer_divisor_idx = 0;
324              lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
325              lapic_timer_divisor_idx++) {
326                 lapic_timer_set_divisor(lapic_timer_divisor_idx);
327                 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
328                 DELAY(2000000);
329                 value = APIC_TIMER_MAX_COUNT - lapic.ccr_timer;
330                 if (value != APIC_TIMER_MAX_COUNT)
331                         break;
332         }
333         if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
334                 panic("lapic: no proper timer divisor?!\n");
335         lapic_cputimer_intr.freq = value / 2;
336
337         kprintf("lapic: divisor index %d, frequency %u Hz\n",
338                 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
339 }
340
341 static void
342 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
343 {
344         sysclock_t count;
345
346         gd->gd_timer_running = 0;
347
348         count = sys_cputimer->count();
349         if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
350                 systimer_intr(&count, 0, frame);
351 }
352
353 void
354 lapic_timer_process(void)
355 {
356         lapic_timer_process_oncpu(mycpu, NULL);
357 }
358
359 void
360 lapic_timer_process_frame(struct intrframe *frame)
361 {
362         lapic_timer_process_oncpu(mycpu, frame);
363 }
364
365 static void
366 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
367 {
368         struct globaldata *gd = mycpu;
369
370         reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
371         if (reload < 2)
372                 reload = 2;
373
374         if (gd->gd_timer_running) {
375                 if (reload < lapic.ccr_timer)
376                         lapic_timer_oneshot_quick(reload);
377         } else {
378                 gd->gd_timer_running = 1;
379                 lapic_timer_oneshot_quick(reload);
380         }
381 }
382
383 static void
384 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
385 {
386         uint32_t timer;
387
388         timer = lapic.lvt_timer;
389         timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
390         lapic.lvt_timer = timer;
391
392         lapic_timer_fixup_handler(NULL);
393 }
394
395 static void
396 lapic_timer_fixup_handler(void *arg)
397 {
398         int *started = arg;
399
400         if (started != NULL)
401                 *started = 0;
402
403         if (cpu_vendor_id == CPU_VENDOR_AMD) {
404                 /*
405                  * Detect the presence of C1E capability mostly on latest
406                  * dual-cores (or future) k8 family.  This feature renders
407                  * the local APIC timer dead, so we disable it by reading
408                  * the Interrupt Pending Message register and clearing both
409                  * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
410                  * 
411                  * Reference:
412                  *   "BIOS and Kernel Developer's Guide for AMD NPT
413                  *    Family 0Fh Processors"
414                  *   #32559 revision 3.00
415                  */
416                 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
417                     (cpu_id & 0x0fff0000) >= 0x00040000) {
418                         uint64_t msr;
419
420                         msr = rdmsr(0xc0010055);
421                         if (msr & 0x18000000) {
422                                 struct globaldata *gd = mycpu;
423
424                                 kprintf("cpu%d: AMD C1E detected\n",
425                                         gd->gd_cpuid);
426                                 wrmsr(0xc0010055, msr & ~0x18000000ULL);
427
428                                 /*
429                                  * We are kinda stalled;
430                                  * kick start again.
431                                  */
432                                 gd->gd_timer_running = 1;
433                                 lapic_timer_oneshot_quick(2);
434
435                                 if (started != NULL)
436                                         *started = 1;
437                         }
438                 }
439         }
440 }
441
442 static void
443 lapic_timer_restart_handler(void *dummy __unused)
444 {
445         int started;
446
447         lapic_timer_fixup_handler(&started);
448         if (!started) {
449                 struct globaldata *gd = mycpu;
450
451                 gd->gd_timer_running = 1;
452                 lapic_timer_oneshot_quick(2);
453         }
454 }
455
456 /*
457  * This function is called only by ACPI-CA code currently:
458  * - AMD C1E fixup.  AMD C1E only seems to happen after ACPI
459  *   module controls PM.  So once ACPI-CA is attached, we try
460  *   to apply the fixup to prevent LAPIC timer from hanging.
461  */
462 static void
463 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
464 {
465         lwkt_send_ipiq_mask(smp_active_mask,
466                             lapic_timer_fixup_handler, NULL);
467 }
468
469 static void
470 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
471 {
472         lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
473 }
474
475
476 /*
477  * dump contents of local APIC registers
478  */
479 void
480 apic_dump(char* str)
481 {
482         kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
483         kprintf("     lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
484                 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
485 }
486
487
488 #ifdef SMP /* APIC-IO */
489
490 /*
491  * IO APIC code,
492  */
493
494 #define IOAPIC_ISA_INTS         16
495 #define REDIRCNT_IOAPIC(A) \
496             ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
497
498 static int trigger (int apic, int pin, u_int32_t * flags);
499 static void polarity (int apic, int pin, u_int32_t * flags, int level);
500
501 #define DEFAULT_FLAGS           \
502         ((u_int32_t)            \
503          (IOART_INTMSET |       \
504           IOART_DESTPHY |       \
505           IOART_DELLOPRI))
506
507 #define DEFAULT_ISA_FLAGS       \
508         ((u_int32_t)            \
509          (IOART_INTMSET |       \
510           IOART_TRGREDG |       \
511           IOART_INTAHI |        \
512           IOART_DESTPHY |       \
513           IOART_DELLOPRI))
514
515 void
516 io_apic_set_id(int apic, int id)
517 {
518         u_int32_t ux;
519         
520         ux = ioapic_read(ioapic[apic], IOAPIC_ID);      /* get current contents */
521         if (((ux & APIC_ID_MASK) >> 24) != id) {
522                 kprintf("Changing APIC ID for IO APIC #%d"
523                        " from %d to %d on chip\n",
524                        apic, ((ux & APIC_ID_MASK) >> 24), id);
525                 ux &= ~APIC_ID_MASK;    /* clear the ID field */
526                 ux |= (id << 24);
527                 ioapic_write(ioapic[apic], IOAPIC_ID, ux);      /* write new value */
528                 ux = ioapic_read(ioapic[apic], IOAPIC_ID);      /* re-read && test */
529                 if (((ux & APIC_ID_MASK) >> 24) != id)
530                         panic("can't control IO APIC #%d ID, reg: 0x%08x",
531                               apic, ux);
532         }
533 }
534
535
536 int
537 io_apic_get_id(int apic)
538 {
539   return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
540 }
541   
542
543
544 /*
545  * Setup the IO APIC.
546  */
547 void
548 io_apic_setup_intpin(int apic, int pin)
549 {
550         int bus, bustype, irq;
551         u_char          select;         /* the select register is 8 bits */
552         u_int32_t       flags;          /* the window register is 32 bits */
553         u_int32_t       target;         /* the window register is 32 bits */
554         u_int32_t       vector;         /* the window register is 32 bits */
555         int             level;
556         int             cpuid;
557         char            envpath[32];
558
559         select = pin * 2 + IOAPIC_REDTBL0;      /* register */
560
561         /*
562          * Always clear an IO APIC pin before [re]programming it.  This is
563          * particularly important if the pin is set up for a level interrupt
564          * as the IOART_REM_IRR bit might be set.   When we reprogram the
565          * vector any EOI from pending ints on this pin could be lost and
566          * IRR might never get reset.
567          *
568          * To fix this problem, clear the vector and make sure it is 
569          * programmed as an edge interrupt.  This should theoretically
570          * clear IRR so we can later, safely program it as a level 
571          * interrupt.
572          */
573         imen_lock();
574
575         flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
576         flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
577         flags |= IOART_DESTPHY | IOART_DELFIXED;
578
579         target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
580         target |= 0;    /* fixed mode cpu mask of 0 - don't deliver anywhere */
581
582         vector = 0;
583
584         ioapic_write(ioapic[apic], select, flags | vector);
585         ioapic_write(ioapic[apic], select + 1, target);
586
587         imen_unlock();
588
589         /*
590          * We only deal with vectored interrupts here.  ? documentation is
591          * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
592          * vs ExTINT, etc.
593          *
594          * This test also catches unconfigured pins.
595          */
596         if (apic_int_type(apic, pin) != 0)
597                 return;
598
599         /*
600          * Leave the pin unprogrammed if it does not correspond to
601          * an IRQ.
602          */
603         irq = apic_irq(apic, pin);
604         if (irq < 0)
605                 return;
606         
607         /* determine the bus type for this pin */
608         bus = apic_src_bus_id(apic, pin);
609         if (bus < 0)
610                 return;
611         bustype = apic_bus_type(bus);
612         
613         if ((bustype == ISA) &&
614             (pin < IOAPIC_ISA_INTS) && 
615             (irq == pin) &&
616             (apic_polarity(apic, pin) == 0x1) &&
617             (apic_trigger(apic, pin) == 0x3)) {
618                 /* 
619                  * A broken BIOS might describe some ISA 
620                  * interrupts as active-high level-triggered.
621                  * Use default ISA flags for those interrupts.
622                  */
623                 flags = DEFAULT_ISA_FLAGS;
624         } else {
625                 /* 
626                  * Program polarity and trigger mode according to 
627                  * interrupt entry.
628                  */
629                 flags = DEFAULT_FLAGS;
630                 level = trigger(apic, pin, &flags);
631                 if (level == 1)
632                         int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
633                 polarity(apic, pin, &flags, level);
634         }
635
636         cpuid = 0;
637         ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
638         kgetenv_int(envpath, &cpuid);
639
640         /* ncpus may not be available yet */
641         if (cpuid > mp_naps)
642                 cpuid = 0;
643
644         if (bootverbose) {
645                 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
646                        apic, pin, irq, cpuid);
647         }
648
649         /*
650          * Program the appropriate registers.  This routing may be 
651          * overridden when an interrupt handler for a device is
652          * actually added (see register_int(), which calls through
653          * the MACHINTR ABI to set up an interrupt handler/vector).
654          *
655          * The order in which we must program the two registers for
656          * safety is unclear! XXX
657          */
658         imen_lock();
659
660         vector = IDT_OFFSET + irq;                      /* IDT vec */
661         target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
662         /* Deliver all interrupts to CPU0 (BSP) */
663         target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
664                   IOART_HI_DEST_MASK;
665         flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
666         ioapic_write(ioapic[apic], select, flags | vector);
667         ioapic_write(ioapic[apic], select + 1, target);
668
669         imen_unlock();
670 }
671
672 int
673 io_apic_setup(int apic)
674 {
675         int             maxpin;
676         int             pin;
677
678         maxpin = REDIRCNT_IOAPIC(apic);         /* pins in APIC */
679         kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
680         
681         for (pin = 0; pin < maxpin; ++pin) {
682                 io_apic_setup_intpin(apic, pin);
683         }
684         while (pin < 32) {
685                 if (apic_int_type(apic, pin) >= 0) {
686                         kprintf("Warning: IOAPIC #%d pin %d does not exist,"
687                                 " cannot program!\n", apic, pin);
688                 }
689                 ++pin;
690         }
691
692         /* return GOOD status */
693         return 0;
694 }
695 #undef DEFAULT_ISA_FLAGS
696 #undef DEFAULT_FLAGS
697
698
699 #define DEFAULT_EXTINT_FLAGS    \
700         ((u_int32_t)            \
701          (IOART_INTMSET |       \
702           IOART_TRGREDG |       \
703           IOART_INTAHI |        \
704           IOART_DESTPHY |       \
705           IOART_DELLOPRI))
706
707 /*
708  * XXX this function is only used by 8254 setup
709  * Setup the source of External INTerrupts.
710  */
711 int
712 ext_int_setup(int apic, int intr)
713 {
714         u_char  select;         /* the select register is 8 bits */
715         u_int32_t flags;        /* the window register is 32 bits */
716         u_int32_t target;       /* the window register is 32 bits */
717         u_int32_t vector;       /* the window register is 32 bits */
718         int cpuid;
719         char envpath[32];
720
721         if (apic_int_type(apic, intr) != 3)
722                 return -1;
723
724         cpuid = 0;
725         ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
726         kgetenv_int(envpath, &cpuid);
727
728         /* ncpus may not be available yet */
729         if (cpuid > mp_naps)
730                 cpuid = 0;
731
732         /* Deliver interrupts to CPU0 (BSP) */
733         target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
734                  IOART_HI_DEST_MASK;
735         select = IOAPIC_REDTBL0 + (2 * intr);
736         vector = IDT_OFFSET + intr;
737         flags = DEFAULT_EXTINT_FLAGS;
738
739         ioapic_write(ioapic[apic], select, flags | vector);
740         ioapic_write(ioapic[apic], select + 1, target);
741
742         return 0;
743 }
744 #undef DEFAULT_EXTINT_FLAGS
745
746
747 /*
748  * Set the trigger level for an IO APIC pin.
749  */
750 static int
751 trigger(int apic, int pin, u_int32_t * flags)
752 {
753         int     id;
754         int     eirq;
755         int     level;
756         static int intcontrol = -1;
757
758         switch (apic_trigger(apic, pin)) {
759
760         case 0x00:
761                 break;
762
763         case 0x01:
764                 *flags &= ~IOART_TRGRLVL;       /* *flags |= IOART_TRGREDG */
765                 return 0;
766
767         case 0x03:
768                 *flags |= IOART_TRGRLVL;
769                 return 1;
770
771         case -1:
772         default:
773                 goto bad;
774         }
775
776         if ((id = apic_src_bus_id(apic, pin)) == -1)
777                 goto bad;
778
779         switch (apic_bus_type(id)) {
780         case ISA:
781                 *flags &= ~IOART_TRGRLVL;       /* *flags |= IOART_TRGREDG; */
782                 return 0;
783
784         case EISA:
785                 eirq = apic_src_bus_irq(apic, pin);
786
787                 if (eirq < 0 || eirq > 15) {
788                         kprintf("EISA IRQ %d?!?!\n", eirq);
789                         goto bad;
790                 }
791
792                 if (intcontrol == -1) {
793                         intcontrol = inb(ELCR1) << 8;
794                         intcontrol |= inb(ELCR0);
795                         kprintf("EISA INTCONTROL = %08x\n", intcontrol);
796                 }
797
798                 /* Use ELCR settings to determine level or edge mode */
799                 level = (intcontrol >> eirq) & 1;
800
801                 /*
802                  * Note that on older Neptune chipset based systems, any
803                  * pci interrupts often show up here and in the ELCR as well
804                  * as level sensitive interrupts attributed to the EISA bus.
805                  */
806
807                 if (level)
808                         *flags |= IOART_TRGRLVL;
809                 else
810                         *flags &= ~IOART_TRGRLVL;
811
812                 return level;
813
814         case PCI:
815                 *flags |= IOART_TRGRLVL;
816                 return 1;
817
818         case -1:
819         default:
820                 goto bad;
821         }
822
823 bad:
824         panic("bad APIC IO INT flags");
825 }
826
827
828 /*
829  * Set the polarity value for an IO APIC pin.
830  */
831 static void
832 polarity(int apic, int pin, u_int32_t * flags, int level)
833 {
834         int     id;
835
836         switch (apic_polarity(apic, pin)) {
837
838         case 0x00:
839                 break;
840
841         case 0x01:
842                 *flags &= ~IOART_INTALO;        /* *flags |= IOART_INTAHI */
843                 return;
844
845         case 0x03:
846                 *flags |= IOART_INTALO;
847                 return;
848
849         case -1:
850         default:
851                 goto bad;
852         }
853
854         if ((id = apic_src_bus_id(apic, pin)) == -1)
855                 goto bad;
856
857         switch (apic_bus_type(id)) {
858         case ISA:
859                 *flags &= ~IOART_INTALO;        /* *flags |= IOART_INTAHI */
860                 return;
861
862         case EISA:
863                 /* polarity converter always gives active high */
864                 *flags &= ~IOART_INTALO;
865                 return;
866
867         case PCI:
868                 *flags |= IOART_INTALO;
869                 return;
870
871         case -1:
872         default:
873                 goto bad;
874         }
875
876 bad:
877         panic("bad APIC IO INT flags");
878 }
879
880
881 /*
882  * Print contents of unmasked IRQs.
883  */
884 void
885 imen_dump(void)
886 {
887         int x;
888
889         kprintf("SMP: enabled INTs: ");
890         for (x = 0; x < APIC_INTMAPSIZE; ++x) {
891                 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
892                         kprintf("%d ", x);
893         }
894         kprintf("\n");
895 }
896
897
898 /*
899  * Inter Processor Interrupt functions.
900  */
901
902 #endif  /* SMP APIC-IO */
903
904 /*
905  * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
906  *
907  *  destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
908  *  vector is any valid SYSTEM INT vector
909  *  delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
910  *
911  * A backlog of requests can create a deadlock between cpus.  To avoid this
912  * we have to be able to accept IPIs at the same time we are trying to send
913  * them.  The critical section prevents us from attempting to send additional
914  * IPIs reentrantly, but also prevents IPIQ processing so we have to call
915  * lwkt_process_ipiq() manually.  It's rather messy and expensive for this
916  * to occur but fortunately it does not happen too often.
917  */
918 int
919 apic_ipi(int dest_type, int vector, int delivery_mode)
920 {
921         u_long  icr_lo;
922
923         crit_enter();
924         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
925             unsigned int eflags = read_eflags();
926             cpu_enable_intr();
927             DEBUG_PUSH_INFO("apic_ipi");
928             while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
929                 lwkt_process_ipiq();
930             }
931             DEBUG_POP_INFO();
932             write_eflags(eflags);
933         }
934
935         icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK) | dest_type | 
936                 delivery_mode | vector;
937         lapic.icr_lo = icr_lo;
938         crit_exit();
939         return 0;
940 }
941
942 void
943 single_apic_ipi(int cpu, int vector, int delivery_mode)
944 {
945         u_long  icr_lo;
946         u_long  icr_hi;
947
948         crit_enter();
949         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
950             unsigned int eflags = read_eflags();
951             cpu_enable_intr();
952             DEBUG_PUSH_INFO("single_apic_ipi");
953             while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
954                 lwkt_process_ipiq();
955             }
956             DEBUG_POP_INFO();
957             write_eflags(eflags);
958         }
959         icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
960         icr_hi |= (CPU_TO_ID(cpu) << 24);
961         lapic.icr_hi = icr_hi;
962
963         /* build ICR_LOW */
964         icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK)
965             | APIC_DEST_DESTFLD | delivery_mode | vector;
966
967         /* write APIC ICR */
968         lapic.icr_lo = icr_lo;
969         crit_exit();
970 }
971
972 #if 0   
973
974 /*
975  * Returns 0 if the apic is busy, 1 if we were able to queue the request.
976  *
977  * NOT WORKING YET!  The code as-is may end up not queueing an IPI at all
978  * to the target, and the scheduler does not 'poll' for IPI messages.
979  */
980 int
981 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
982 {
983         u_long  icr_lo;
984         u_long  icr_hi;
985
986         crit_enter();
987         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
988             crit_exit();
989             return(0);
990         }
991         icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
992         icr_hi |= (CPU_TO_ID(cpu) << 24);
993         lapic.icr_hi = icr_hi;
994
995         /* build IRC_LOW */
996         icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
997             | APIC_DEST_DESTFLD | delivery_mode | vector;
998
999         /* write APIC ICR */
1000         lapic.icr_lo = icr_lo;
1001         crit_exit();
1002         return(1);
1003 }
1004
1005 #endif
1006
1007 /*
1008  * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
1009  *
1010  * target is a bitmask of destination cpus.  Vector is any
1011  * valid system INT vector.  Delivery mode may be either
1012  * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
1013  */
1014 void
1015 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
1016 {
1017         crit_enter();
1018         while (target) {
1019                 int n = BSFCPUMASK(target);
1020                 target &= ~CPUMASK(n);
1021                 single_apic_ipi(n, vector, delivery_mode);
1022         }
1023         crit_exit();
1024 }
1025
1026 /*
1027  * Timer code, in development...
1028  *  - suggested by rgrimes@gndrsh.aac.dev.com
1029  */
1030 int
1031 get_apic_timer_frequency(void)
1032 {
1033         return(lapic_cputimer_intr.freq);
1034 }
1035
1036 /*
1037  * Load a 'downcount time' in uSeconds.
1038  */
1039 void
1040 set_apic_timer(int us)
1041 {
1042         u_int count;
1043
1044         /*
1045          * When we reach here, lapic timer's frequency
1046          * must have been calculated as well as the
1047          * divisor (lapic.dcr_timer is setup during the
1048          * divisor calculation).
1049          */
1050         KKASSERT(lapic_cputimer_intr.freq != 0 &&
1051                  lapic_timer_divisor_idx >= 0);
1052
1053         count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1054         lapic_timer_oneshot(count);
1055 }
1056
1057
1058 /*
1059  * Read remaining time in timer.
1060  */
1061 int
1062 read_apic_timer(void)
1063 {
1064 #if 0
1065         /** XXX FIXME: we need to return the actual remaining time,
1066          *         for now we just return the remaining count.
1067          */
1068 #else
1069         return lapic.ccr_timer;
1070 #endif
1071 }
1072
1073
1074 /*
1075  * Spin-style delay, set delay time in uS, spin till it drains.
1076  */
1077 void
1078 u_sleep(int count)
1079 {
1080         set_apic_timer(count);
1081         while (read_apic_timer())
1082                  /* spin */ ;
1083 }
1084
1085 static int
1086 lapic_unused_apic_id(int start)
1087 {
1088         int i;
1089
1090         for (i = start; i < NAPICID; ++i) {
1091                 if (ID_TO_CPU(i) == -1)
1092                         return i;
1093         }
1094         return NAPICID;
1095 }
1096
1097 void
1098 lapic_map(vm_offset_t lapic_addr)
1099 {
1100         /* Local apic is mapped on last page */
1101         SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
1102             pmap_get_pgeflag() | (lapic_addr & PG_FRAME));
1103
1104         kprintf("lapic: at %p\n", (void *)lapic_addr);
1105 }
1106
1107 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1108         TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1109
1110 void
1111 lapic_config(void)
1112 {
1113         struct lapic_enumerator *e;
1114         int error, i;
1115
1116         for (i = 0; i < NAPICID; ++i)
1117                 ID_TO_CPU(i) = -1;
1118
1119         TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1120                 error = e->lapic_probe(e);
1121                 if (!error)
1122                         break;
1123         }
1124         if (e == NULL)
1125                 panic("can't config lapic\n");
1126
1127         e->lapic_enumerate(e);
1128 }
1129
1130 void
1131 lapic_enumerator_register(struct lapic_enumerator *ne)
1132 {
1133         struct lapic_enumerator *e;
1134
1135         TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1136                 if (e->lapic_prio < ne->lapic_prio) {
1137                         TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1138                         return;
1139                 }
1140         }
1141         TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1142 }
1143
1144 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1145         TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1146
1147 void
1148 ioapic_config(void)
1149 {
1150         struct ioapic_enumerator *e;
1151         int error, i;
1152         u_long ef = 0;
1153
1154         TAILQ_INIT(&ioapic_conf.ioc_list);
1155         /* XXX magic number */
1156         for (i = 0; i < 16; ++i)
1157                 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
1158
1159         TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1160                 error = e->ioapic_probe(e);
1161                 if (!error)
1162                         break;
1163         }
1164         if (e == NULL) {
1165 #ifdef notyet
1166                 panic("can't config I/O APIC\n");
1167 #else
1168                 kprintf("no I/O APIC\n");
1169                 return;
1170 #endif
1171         }
1172
1173         if (!ioapic_use_old) {
1174                 crit_enter();
1175
1176                 ef = read_eflags();
1177                 cpu_disable_intr();
1178
1179                 /*
1180                  * Switch to I/O APIC MachIntrABI and reconfigure
1181                  * the default IDT entries.
1182                  */
1183                 MachIntrABI = MachIntrABI_IOAPIC;
1184                 MachIntrABI.setdefault();
1185         }
1186
1187         e->ioapic_enumerate(e);
1188
1189         if (!ioapic_use_old) {
1190                 struct ioapic_info *info;
1191                 int start_apic_id = 0;
1192
1193                 /*
1194                  * Setup index
1195                  */
1196                 i = 0;
1197                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1198                         info->io_idx = i++;
1199
1200                 if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
1201                         panic("ioapic_config: more than 16 I/O APIC\n");
1202
1203                 /*
1204                  * Setup APIC ID
1205                  */
1206                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1207                         int apic_id;
1208
1209                         apic_id = ioapic_alloc_apic_id(start_apic_id);
1210                         if (apic_id == NAPICID) {
1211                                 kprintf("IOAPIC: can't alloc APIC ID for "
1212                                         "%dth I/O APIC\n", info->io_idx);
1213                                 break;
1214                         }
1215                         info->io_apic_id = apic_id;
1216
1217                         start_apic_id = apic_id + 1;
1218                 }
1219                 if (info != NULL) {
1220                         /*
1221                          * xAPIC allows I/O APIC's APIC ID to be same
1222                          * as the LAPIC's APIC ID
1223                          */
1224                         kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
1225                                 "for I/O APIC\n");
1226
1227                         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1228                                 info->io_apic_id = info->io_idx;
1229                 }
1230
1231                 /*
1232                  * Warning about any GSI holes
1233                  */
1234                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1235                         const struct ioapic_info *prev_info;
1236
1237                         prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1238                         if (prev_info != NULL) {
1239                                 if (info->io_gsi_base !=
1240                                 prev_info->io_gsi_base + prev_info->io_npin) {
1241                                         kprintf("IOAPIC: warning gsi hole "
1242                                                 "[%d, %d]\n",
1243                                                 prev_info->io_gsi_base +
1244                                                 prev_info->io_npin,
1245                                                 info->io_gsi_base - 1);
1246                                 }
1247                         }
1248                 }
1249
1250                 if (bootverbose) {
1251                         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1252                                 kprintf("IOAPIC: idx %d, apic id %d, "
1253                                         "gsi base %d, npin %d\n",
1254                                         info->io_idx,
1255                                         info->io_apic_id,
1256                                         info->io_gsi_base,
1257                                         info->io_npin);
1258                         }
1259                 }
1260
1261                 /*
1262                  * Setup all I/O APIC
1263                  */
1264                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1265                         ioapic_setup(info);
1266                 ioapic_abi_fixup_irqmap();
1267
1268                 write_eflags(ef);
1269
1270                 MachIntrABI.cleanup();
1271
1272                 crit_exit();
1273         }
1274 }
1275
1276 void
1277 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1278 {
1279         struct ioapic_enumerator *e;
1280
1281         TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1282                 if (e->ioapic_prio < ne->ioapic_prio) {
1283                         TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1284                         return;
1285                 }
1286         }
1287         TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1288 }
1289
1290 void
1291 ioapic_add(void *addr, int gsi_base, int npin)
1292 {
1293         struct ioapic_info *info, *ninfo;
1294         int gsi_end;
1295
1296         gsi_end = gsi_base + npin - 1;
1297         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1298                 if ((gsi_base >= info->io_gsi_base &&
1299                      gsi_base < info->io_gsi_base + info->io_npin) ||
1300                     (gsi_end >= info->io_gsi_base &&
1301                      gsi_end < info->io_gsi_base + info->io_npin)) {
1302                         panic("ioapic_add: overlapped gsi, base %d npin %d, "
1303                               "hit base %d, npin %d\n", gsi_base, npin,
1304                               info->io_gsi_base, info->io_npin);
1305                 }
1306                 if (info->io_addr == addr)
1307                         panic("ioapic_add: duplicated addr %p\n", addr);
1308         }
1309
1310         ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1311         ninfo->io_addr = addr;
1312         ninfo->io_npin = npin;
1313         ninfo->io_gsi_base = gsi_base;
1314         ninfo->io_apic_id = -1;
1315
1316         /*
1317          * Create IOAPIC list in ascending order of GSI base
1318          */
1319         TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1320             ioapic_info_list, io_link) {
1321                 if (ninfo->io_gsi_base > info->io_gsi_base) {
1322                         TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1323                             info, ninfo, io_link);
1324                         break;
1325                 }
1326         }
1327         if (info == NULL)
1328                 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1329 }
1330
1331 void
1332 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
1333 {
1334         struct ioapic_intsrc *int_src;
1335
1336         KKASSERT(irq < 16);
1337         int_src = &ioapic_conf.ioc_intsrc[irq];
1338
1339         if (gsi == 0) {
1340                 /* Don't allow mixed mode */
1341                 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
1342                 return;
1343         }
1344
1345         if (int_src->int_gsi != -1) {
1346                 if (int_src->int_gsi != gsi) {
1347                         kprintf("IOAPIC: warning intsrc irq %d, gsi "
1348                                 "%d -> %d\n", irq, int_src->int_gsi, gsi);
1349                 }
1350                 if (int_src->int_trig != trig) {
1351                         kprintf("IOAPIC: warning intsrc irq %d, trig "
1352                                 "%s -> %s\n", irq,
1353                                 intr_str_trigger(int_src->int_trig),
1354                                 intr_str_trigger(trig));
1355                 }
1356                 if (int_src->int_pola != pola) {
1357                         kprintf("IOAPIC: warning intsrc irq %d, pola "
1358                                 "%s -> %s\n", irq,
1359                                 intr_str_polarity(int_src->int_pola),
1360                                 intr_str_polarity(pola));
1361                 }
1362         }
1363         int_src->int_gsi = gsi;
1364         int_src->int_trig = trig;
1365         int_src->int_pola = pola;
1366 }
1367
1368 static void
1369 ioapic_set_apic_id(const struct ioapic_info *info)
1370 {
1371         uint32_t id;
1372         int apic_id;
1373
1374         id = ioapic_read(info->io_addr, IOAPIC_ID);
1375
1376         id &= ~APIC_ID_MASK;
1377         id |= (info->io_apic_id << 24);
1378
1379         ioapic_write(info->io_addr, IOAPIC_ID, id);
1380
1381         /*
1382          * Re-read && test
1383          */
1384         id = ioapic_read(info->io_addr, IOAPIC_ID);
1385         apic_id = (id & APIC_ID_MASK) >> 24;
1386
1387         /*
1388          * I/O APIC ID is a 4bits field
1389          */
1390         if ((apic_id & IOAPIC_ID_MASK) !=
1391             (info->io_apic_id & IOAPIC_ID_MASK)) {
1392                 panic("ioapic_set_apic_id: can't set apic id to %d, "
1393                       "currently set to %d\n", info->io_apic_id, apic_id);
1394         }
1395 }
1396
1397 static void
1398 ioapic_gsi_setup(int gsi)
1399 {
1400         enum intr_trigger trig;
1401         enum intr_polarity pola;
1402         int irq;
1403
1404         if (gsi == 0) {
1405                 /* ExtINT */
1406                 imen_lock();
1407                 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
1408                     ioapic_gsi_pin(gsi), 0);
1409                 imen_unlock();
1410                 return;
1411         }
1412
1413         trig = 0;       /* silence older gcc's */
1414         pola = 0;       /* silence older gcc's */
1415
1416         for (irq = 0; irq < 16; ++irq) {
1417                 const struct ioapic_intsrc *int_src =
1418                     &ioapic_conf.ioc_intsrc[irq];
1419
1420                 if (gsi == int_src->int_gsi) {
1421                         trig = int_src->int_trig;
1422                         pola = int_src->int_pola;
1423                         break;
1424                 }
1425         }
1426
1427         if (irq == 16) {
1428                 if (gsi < 16) {
1429                         trig = INTR_TRIGGER_EDGE;
1430                         pola = INTR_POLARITY_HIGH;
1431                 } else {
1432                         trig = INTR_TRIGGER_LEVEL;
1433                         pola = INTR_POLARITY_LOW;
1434                 }
1435                 irq = gsi;
1436         }
1437
1438         ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1439 }
1440
1441 void *
1442 ioapic_gsi_ioaddr(int gsi)
1443 {
1444         const struct ioapic_info *info;
1445
1446         info = ioapic_gsi_search(gsi);
1447         return info->io_addr;
1448 }
1449
1450 int
1451 ioapic_gsi_pin(int gsi)
1452 {
1453         const struct ioapic_info *info;
1454
1455         info = ioapic_gsi_search(gsi);
1456         return gsi - info->io_gsi_base;
1457 }
1458
1459 static const struct ioapic_info *
1460 ioapic_gsi_search(int gsi)
1461 {
1462         const struct ioapic_info *info;
1463
1464         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1465                 if (gsi >= info->io_gsi_base &&
1466                     gsi < info->io_gsi_base + info->io_npin)
1467                         return info;
1468         }
1469         panic("ioapic_gsi_search: no I/O APIC\n");
1470 }
1471
1472 int
1473 ioapic_gsi(int idx, int pin)
1474 {
1475         const struct ioapic_info *info;
1476
1477         TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1478                 if (info->io_idx == idx)
1479                         break;
1480         }
1481         if (info == NULL)
1482                 return -1;
1483         if (pin >= info->io_npin)
1484                 return -1;
1485         return info->io_gsi_base + pin;
1486 }
1487
1488 void
1489 ioapic_extpin_setup(void *addr, int pin, int vec)
1490 {
1491         ioapic_pin_prog(addr, pin, vec,
1492             INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1493 }
1494
1495 int
1496 ioapic_extpin_gsi(void)
1497 {
1498         return 0;
1499 }
1500
1501 void
1502 ioapic_pin_setup(void *addr, int pin, int vec,
1503     enum intr_trigger trig, enum intr_polarity pola)
1504 {
1505         /*
1506          * Always clear an I/O APIC pin before [re]programming it.  This is
1507          * particularly important if the pin is set up for a level interrupt
1508          * as the IOART_REM_IRR bit might be set.   When we reprogram the
1509          * vector any EOI from pending ints on this pin could be lost and
1510          * IRR might never get reset.
1511          *
1512          * To fix this problem, clear the vector and make sure it is 
1513          * programmed as an edge interrupt.  This should theoretically
1514          * clear IRR so we can later, safely program it as a level 
1515          * interrupt.
1516          */
1517         ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1518             IOART_DELFIXED);
1519         ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1520 }
1521
1522 static void
1523 ioapic_pin_prog(void *addr, int pin, int vec,
1524     enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1525 {
1526         uint32_t flags, target;
1527         int select;
1528
1529         KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1530
1531         select = IOAPIC_REDTBL0 + (2 * pin);
1532
1533         flags = ioapic_read(addr, select) & IOART_RESV;
1534         flags |= IOART_INTMSET | IOART_DESTPHY;
1535 #ifdef foo
1536         flags |= del_mode;
1537 #else
1538         /*
1539          * We only support limited I/O APIC mixed mode,
1540          * so even for ExtINT, we still use "fixed"
1541          * delivery mode.
1542          */
1543         flags |= IOART_DELFIXED;
1544 #endif
1545
1546         if (del_mode == IOART_DELEXINT) {
1547                 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1548                          pola == INTR_POLARITY_CONFORM);
1549                 flags |= IOART_TRGREDG | IOART_INTAHI;
1550         } else {
1551                 switch (trig) {
1552                 case INTR_TRIGGER_EDGE:
1553                         flags |= IOART_TRGREDG;
1554                         break;
1555
1556                 case INTR_TRIGGER_LEVEL:
1557                         flags |= IOART_TRGRLVL;
1558                         break;
1559
1560                 case INTR_TRIGGER_CONFORM:
1561                         panic("ioapic_pin_prog: trig conform is not "
1562                               "supported\n");
1563                 }
1564                 switch (pola) {
1565                 case INTR_POLARITY_HIGH:
1566                         flags |= IOART_INTAHI;
1567                         break;
1568
1569                 case INTR_POLARITY_LOW:
1570                         flags |= IOART_INTALO;
1571                         break;
1572
1573                 case INTR_POLARITY_CONFORM:
1574                         panic("ioapic_pin_prog: pola conform is not "
1575                               "supported\n");
1576                 }
1577         }
1578
1579         target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1580         target |= (CPU_TO_ID(0) << IOART_HI_DEST_SHIFT) &
1581                   IOART_HI_DEST_MASK;
1582
1583         ioapic_write(addr, select, flags | vec);
1584         ioapic_write(addr, select + 1, target);
1585 }
1586
1587 static void
1588 ioapic_setup(const struct ioapic_info *info)
1589 {
1590         int i;
1591
1592         ioapic_set_apic_id(info);
1593
1594         for (i = 0; i < info->io_npin; ++i)
1595                 ioapic_gsi_setup(info->io_gsi_base + i);
1596 }
1597
1598 static int
1599 ioapic_alloc_apic_id(int start)
1600 {
1601         for (;;) {
1602                 const struct ioapic_info *info;
1603                 int apic_id, apic_id16;
1604
1605                 apic_id = lapic_unused_apic_id(start);
1606                 if (apic_id == NAPICID) {
1607                         kprintf("IOAPIC: can't find unused APIC ID\n");
1608                         return apic_id;
1609                 }
1610                 apic_id16 = apic_id & IOAPIC_ID_MASK;
1611
1612                 /*
1613                  * Check against other I/O APIC's APIC ID's lower 4bits.
1614                  *
1615                  * The new APIC ID will have to be different from others
1616                  * in the lower 4bits, no matter whether xAPIC is used
1617                  * or not.
1618                  */
1619                 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1620                         if (info->io_apic_id == -1) {
1621                                 info = NULL;
1622                                 break;
1623                         }
1624                         if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
1625                                 break;
1626                 }
1627                 if (info == NULL)
1628                         return apic_id;
1629
1630                 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
1631                         "%dth I/O APIC, keep searching...\n",
1632                         apic_id, info->io_idx);
1633
1634                 start = apic_id + 1;
1635         }
1636         panic("ioapic_unused_apic_id: never reached\n");
1637 }