2 * Copyright (c) 1996, by Steve Passe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
25 * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
26 * $DragonFly: src/sys/platform/pc32/i386/Attic/mpapic.c,v 1.5 2003/07/08 06:27:26 dillon Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <machine/smptests.h> /** TEST_TEST1, GRAB_LOPRIO */
32 #include <machine/globaldata.h>
33 #include <machine/smp.h>
34 #include <machine/mpapic.h>
35 #include <machine/segments.h>
36 #include <sys/thread2.h>
38 #include <i386/isa/intr_machdep.h> /* Xspuriousint() */
40 /* EISA Edge/Level trigger control registers */
41 #define ELCR0 0x4d0 /* eisa irq 0-7 */
42 #define ELCR1 0x4d1 /* eisa irq 8-15 */
45 * pointers to pmapped apic hardware.
49 volatile ioapic_t **ioapic;
53 * Enable APIC, configure interrupts.
60 /* setup LVT1 as ExtINT */
61 temp = lapic.lvt_lint0;
62 temp &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM);
63 if (mycpu->gd_cpuid == 0)
64 temp |= 0x00000700; /* process ExtInts */
66 temp |= 0x00010700; /* mask ExtInts */
67 lapic.lvt_lint0 = temp;
69 /* setup LVT2 as NMI, masked till later... */
70 temp = lapic.lvt_lint1;
71 temp &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM);
72 temp |= 0x00010400; /* masked, edge trigger, active hi */
73 lapic.lvt_lint1 = temp;
75 /* set the Task Priority Register as needed */
77 temp &= ~APIC_TPR_PRIO; /* clear priority field */
80 * Leave the BSP and TPR 0 during boot so it gets all the interrupts,
81 * set APs at TPR 0xF0 at boot so they get no ints.
84 if (mycpu->gd_cpuid != 0)
85 temp |= TPR_IPI_ONLY; /* disable INTs on this cpu */
89 /* enable the local APIC */
91 temp |= APIC_SVR_SWEN; /* software enable APIC */
92 temp &= ~APIC_SVR_FOCUS; /* enable 'focus processor' */
94 /* set the 'spurious INT' vector */
95 if ((XSPURIOUSINT_OFFSET & APIC_SVR_VEC_FIX) != APIC_SVR_VEC_FIX)
96 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
97 temp &= ~APIC_SVR_VEC_PROG; /* clear (programmable) vector field */
98 temp |= (XSPURIOUSINT_OFFSET & APIC_SVR_VEC_PROG);
100 #if defined(TEST_TEST1)
101 if (cpuid == GUARD_CPU) {
102 temp &= ~APIC_SVR_SWEN; /* software DISABLE APIC */
104 #endif /** TEST_TEST1 */
109 apic_dump("apic_initialize()");
114 * dump contents of local APIC registers
119 printf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
120 printf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
121 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
131 #define IOAPIC_ISA_INTS 16
132 #define REDIRCNT_IOAPIC(A) \
133 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
135 static int trigger __P((int apic, int pin, u_int32_t * flags));
136 static void polarity __P((int apic, int pin, u_int32_t * flags, int level));
138 #define DEFAULT_FLAGS \
144 #define DEFAULT_ISA_FLAGS \
153 io_apic_set_id(int apic, int id)
157 ux = io_apic_read(apic, IOAPIC_ID); /* get current contents */
158 if (((ux & APIC_ID_MASK) >> 24) != id) {
159 printf("Changing APIC ID for IO APIC #%d"
160 " from %d to %d on chip\n",
161 apic, ((ux & APIC_ID_MASK) >> 24), id);
162 ux &= ~APIC_ID_MASK; /* clear the ID field */
164 io_apic_write(apic, IOAPIC_ID, ux); /* write new value */
165 ux = io_apic_read(apic, IOAPIC_ID); /* re-read && test */
166 if (((ux & APIC_ID_MASK) >> 24) != id)
167 panic("can't control IO APIC #%d ID, reg: 0x%08x",
174 io_apic_get_id(int apic)
176 return (io_apic_read(apic, IOAPIC_ID) & APIC_ID_MASK) >> 24;
185 extern int apic_pin_trigger; /* 'opaque' */
188 io_apic_setup_intpin(int apic, int pin)
190 int bus, bustype, irq;
191 u_char select; /* the select register is 8 bits */
192 u_int32_t flags; /* the window register is 32 bits */
193 u_int32_t target; /* the window register is 32 bits */
194 u_int32_t vector; /* the window register is 32 bits */
199 select = pin * 2 + IOAPIC_REDTBL0; /* register */
201 * Always disable interrupts, and by default map
202 * pin X to IRQX because the disable doesn't stick
203 * and the uninitialize vector will get translated
206 * This is correct for IRQs 1 and 3-15. In the other cases,
207 * any robust driver will handle the spurious interrupt, and
208 * the effective NOP beats a panic.
210 * A dedicated "bogus interrupt" entry in the IDT would
211 * be a nicer hack, although some one should find out
212 * why some systems are generating interrupts when they
213 * shouldn't and stop the carnage.
215 vector = NRSVIDT + pin; /* IDT vec */
217 io_apic_write(apic, select,
218 (io_apic_read(apic, select) & ~IOART_INTMASK
219 & ~0xff)|IOART_INTMSET|vector);
222 /* we only deal with vectored INTs here */
223 if (apic_int_type(apic, pin) != 0)
226 irq = apic_irq(apic, pin);
230 /* determine the bus type for this pin */
231 bus = apic_src_bus_id(apic, pin);
234 bustype = apic_bus_type(bus);
236 if ((bustype == ISA) &&
237 (pin < IOAPIC_ISA_INTS) &&
239 (apic_polarity(apic, pin) == 0x1) &&
240 (apic_trigger(apic, pin) == 0x3)) {
242 * A broken BIOS might describe some ISA
243 * interrupts as active-high level-triggered.
244 * Use default ISA flags for those interrupts.
246 flags = DEFAULT_ISA_FLAGS;
249 * Program polarity and trigger mode according to
252 flags = DEFAULT_FLAGS;
253 level = trigger(apic, pin, &flags);
255 apic_pin_trigger |= (1 << irq);
256 polarity(apic, pin, &flags, level);
259 /* program the appropriate registers */
260 if (apic != 0 || pin != irq)
261 printf("IOAPIC #%d intpin %d -> irq %d\n",
263 vector = NRSVIDT + irq; /* IDT vec */
265 io_apic_write(apic, select, flags | vector);
266 io_apic_write(apic, select + 1, target);
271 io_apic_setup(int apic)
277 apic_pin_trigger = 0; /* default to edge-triggered */
279 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
280 printf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
282 for (pin = 0; pin < maxpin; ++pin) {
283 io_apic_setup_intpin(apic, pin);
286 /* return GOOD status */
289 #undef DEFAULT_ISA_FLAGS
293 #define DEFAULT_EXTINT_FLAGS \
302 * Setup the source of External INTerrupts.
305 ext_int_setup(int apic, int intr)
307 u_char select; /* the select register is 8 bits */
308 u_int32_t flags; /* the window register is 32 bits */
309 u_int32_t target; /* the window register is 32 bits */
310 u_int32_t vector; /* the window register is 32 bits */
312 if (apic_int_type(apic, intr) != 3)
316 select = IOAPIC_REDTBL0 + (2 * intr);
317 vector = NRSVIDT + intr;
318 flags = DEFAULT_EXTINT_FLAGS;
320 io_apic_write(apic, select, flags | vector);
321 io_apic_write(apic, select + 1, target);
325 #undef DEFAULT_EXTINT_FLAGS
329 * Set the trigger level for an IO APIC pin.
332 trigger(int apic, int pin, u_int32_t * flags)
337 static int intcontrol = -1;
339 switch (apic_trigger(apic, pin)) {
345 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
349 *flags |= IOART_TRGRLVL;
357 if ((id = apic_src_bus_id(apic, pin)) == -1)
360 switch (apic_bus_type(id)) {
362 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
366 eirq = apic_src_bus_irq(apic, pin);
368 if (eirq < 0 || eirq > 15) {
369 printf("EISA IRQ %d?!?!\n", eirq);
373 if (intcontrol == -1) {
374 intcontrol = inb(ELCR1) << 8;
375 intcontrol |= inb(ELCR0);
376 printf("EISA INTCONTROL = %08x\n", intcontrol);
379 /* Use ELCR settings to determine level or edge mode */
380 level = (intcontrol >> eirq) & 1;
383 * Note that on older Neptune chipset based systems, any
384 * pci interrupts often show up here and in the ELCR as well
385 * as level sensitive interrupts attributed to the EISA bus.
389 *flags |= IOART_TRGRLVL;
391 *flags &= ~IOART_TRGRLVL;
396 *flags |= IOART_TRGRLVL;
405 panic("bad APIC IO INT flags");
410 * Set the polarity value for an IO APIC pin.
413 polarity(int apic, int pin, u_int32_t * flags, int level)
417 switch (apic_polarity(apic, pin)) {
423 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
427 *flags |= IOART_INTALO;
435 if ((id = apic_src_bus_id(apic, pin)) == -1)
438 switch (apic_bus_type(id)) {
440 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
444 /* polarity converter always gives active high */
445 *flags &= ~IOART_INTALO;
449 *flags |= IOART_INTALO;
458 panic("bad APIC IO INT flags");
463 * Print contents of apic_imen.
465 extern u_int apic_imen; /* keep apic_imen 'opaque' */
471 printf("SMP: enabled INTs: ");
472 for (x = 0; x < 24; ++x)
473 if ((apic_imen & (1 << x)) == 0)
475 printf("apic_imen: 0x%08x\n", apic_imen);
480 * Inter Processor Interrupt functions.
485 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
487 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
488 * vector is any valid SYSTEM INT vector
489 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
491 * A backlog of requests can create a deadlock between cpus. To avoid this
492 * we have to be able to accept IPIs at the same time we are trying to send
493 * them. The critical section prevents us from attempting to send additional
494 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
495 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
496 * to occur but fortunately it does not happen too often.
499 apic_ipi(int dest_type, int vector, int delivery_mode)
504 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
505 unsigned int eflags = read_eflags();
507 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
510 write_eflags(eflags);
513 icr_lo = (lapic.icr_lo & APIC_RESV2_MASK) | dest_type |
514 delivery_mode | vector;
515 lapic.icr_lo = icr_lo;
521 apic_ipi_singledest(int cpu, int vector, int delivery_mode)
526 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
527 unsigned int eflags = read_eflags();
529 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
532 write_eflags(eflags);
534 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
535 icr_hi |= (CPU_TO_ID(cpu) << 24);
536 lapic.icr_hi = icr_hi;
539 icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
540 | APIC_DEST_DESTFLD | delivery_mode | vector;
543 lapic.icr_lo = icr_lo;
548 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
550 * target is a bitmask of destination cpus. Vector is any
551 * valid system INT vector. Delivery mode may be either
552 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
555 selected_apic_ipi(u_int target, int vector, int delivery_mode)
561 int n = bsfl(target);
563 if (apic_ipi_singledest(n, vector, delivery_mode) < 0)
573 * Timer code, in development...
574 * - suggested by rgrimes@gndrsh.aac.dev.com
577 /** XXX FIXME: temp hack till we can determin bus clock */
579 #define BUS_CLOCK 66000000
580 #define bus_clock() 66000000
584 int acquire_apic_timer __P((void));
585 int release_apic_timer __P((void));
588 * Acquire the APIC timer for exclusive use.
591 acquire_apic_timer(void)
596 /** XXX FIXME: make this really do something */
597 panic("APIC timer in use when attempting to aquire");
603 * Return the APIC timer.
606 release_apic_timer(void)
611 /** XXX FIXME: make this really do something */
612 panic("APIC timer was already released");
619 * Load a 'downcount time' in uSeconds.
622 set_apic_timer(int value)
625 long ticks_per_microsec;
628 * Calculate divisor and count from value:
630 * timeBase == CPU bus clock divisor == [1,2,4,8,16,32,64,128]
631 * value == time in uS
633 lapic.dcr_timer = APIC_TDCR_1;
634 ticks_per_microsec = bus_clock() / 1000000;
636 /* configure timer as one-shot */
637 lvtt = lapic.lvt_timer;
638 lvtt &= ~(APIC_LVTT_VECTOR | APIC_LVTT_DS | APIC_LVTT_M | APIC_LVTT_TM);
639 lvtt |= APIC_LVTT_M; /* no INT, one-shot */
640 lapic.lvt_timer = lvtt;
643 lapic.icr_timer = value * ticks_per_microsec;
648 * Read remaining time in timer.
651 read_apic_timer(void)
654 /** XXX FIXME: we need to return the actual remaining time,
655 * for now we just return the remaining count.
658 return lapic.ccr_timer;
664 * Spin-style delay, set delay time in uS, spin till it drains.
669 set_apic_timer(count);
670 while (read_apic_timer())