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