2 #include "../port/lib.h"
10 enum { /* Local APIC registers */
11 LapicID = 0x0020, /* ID */
12 LapicVER = 0x0030, /* Version */
13 LapicTPR = 0x0080, /* Task Priority */
14 LapicAPR = 0x0090, /* Arbitration Priority */
15 LapicPPR = 0x00A0, /* Processor Priority */
16 LapicEOI = 0x00B0, /* EOI */
17 LapicLDR = 0x00D0, /* Logical Destination */
18 LapicDFR = 0x00E0, /* Destination Format */
19 LapicSVR = 0x00F0, /* Spurious Interrupt Vector */
20 LapicISR = 0x0100, /* Interrupt Status (8 registers) */
21 LapicTMR = 0x0180, /* Trigger Mode (8 registers) */
22 LapicIRR = 0x0200, /* Interrupt Request (8 registers) */
23 LapicESR = 0x0280, /* Error Status */
24 LapicICRLO = 0x0300, /* Interrupt Command */
25 LapicICRHI = 0x0310, /* Interrupt Command [63:32] */
26 LapicTIMER = 0x0320, /* Local Vector Table 0 (TIMER) */
27 LapicPCINT = 0x0340, /* Performance Counter LVT */
28 LapicLINT0 = 0x0350, /* Local Vector Table 1 (LINT0) */
29 LapicLINT1 = 0x0360, /* Local Vector Table 2 (LINT1) */
30 LapicERROR = 0x0370, /* Local Vector Table 3 (ERROR) */
31 LapicTICR = 0x0380, /* Timer Initial Count */
32 LapicTCCR = 0x0390, /* Timer Current Count */
33 LapicTDCR = 0x03E0, /* Timer Divide Configuration */
37 LapicENABLE = 0x00000100, /* Unit Enable */
38 LapicFOCUS = 0x00000200, /* Focus Processor Checking Disable */
41 enum { /* LapicICRLO */
42 /* [14] IPI Trigger Mode Level (RW) */
43 LapicDEASSERT = 0x00000000, /* Deassert level-sensitive interrupt */
44 LapicASSERT = 0x00004000, /* Assert level-sensitive interrupt */
46 /* [17:16] Remote Read Status */
47 LapicINVALID = 0x00000000, /* Invalid */
48 LapicWAIT = 0x00010000, /* In-Progress */
49 LapicVALID = 0x00020000, /* Valid */
51 /* [19:18] Destination Shorthand */
52 LapicFIELD = 0x00000000, /* No shorthand */
53 LapicSELF = 0x00040000, /* Self is single destination */
54 LapicALLINC = 0x00080000, /* All including self */
55 LapicALLEXC = 0x000C0000, /* All Excluding self */
59 LapicSENDCS = 0x00000001, /* Send CS Error */
60 LapicRCVCS = 0x00000002, /* Receive CS Error */
61 LapicSENDACCEPT = 0x00000004, /* Send Accept Error */
62 LapicRCVACCEPT = 0x00000008, /* Receive Accept Error */
63 LapicSENDVECTOR = 0x00000020, /* Send Illegal Vector */
64 LapicRCVVECTOR = 0x00000040, /* Receive Illegal Vector */
65 LapicREGISTER = 0x00000080, /* Illegal Register Address */
68 enum { /* LapicTIMER */
69 /* [17] Timer Mode (RW) */
70 LapicONESHOT = 0x00000000, /* One-shot */
71 LapicPERIODIC = 0x00020000, /* Periodic */
73 /* [19:18] Timer Base (RW) */
74 LapicCLKIN = 0x00000000, /* use CLKIN as input */
75 LapicTMBASE = 0x00040000, /* use TMBASE */
76 LapicDIVIDER = 0x00080000, /* use output of the divider */
79 enum { /* LapicTDCR */
80 LapicX2 = 0x00000000, /* divide by 2 */
81 LapicX4 = 0x00000001, /* divide by 4 */
82 LapicX8 = 0x00000002, /* divide by 8 */
83 LapicX16 = 0x00000003, /* divide by 16 */
84 LapicX32 = 0x00000008, /* divide by 32 */
85 LapicX64 = 0x00000009, /* divide by 64 */
86 LapicX128 = 0x0000000A, /* divide by 128 */
87 LapicX1 = 0x0000000B, /* divide by 1 */
90 static ulong* lapicbase;
103 return *(lapicbase+(r/sizeof(*lapicbase)));
107 lapicw(int r, ulong data)
109 *(lapicbase+(r/sizeof(*lapicbase))) = data;
110 data = *(lapicbase+(LapicID/sizeof(*lapicbase)));
118 * Reload the timer to de-synchronise the processors,
119 * then lower the task priority to allow interrupts to be
120 * accepted by the APIC.
122 microdelay((TK2MS(1)*1000/conf.nmach) * m->machno);
123 lapicw(LapicTICR, lapictimer.max);
124 lapicw(LapicTIMER, LapicCLKIN|LapicPERIODIC|(VectorPIC+IrqTIMER));
130 * use the i8253 clock to figure out our lapic timer rate.
138 lapicw(LapicTDCR, LapicX1);
139 lapicw(LapicTIMER, ApicIMASK|LapicCLKIN|LapicONESHOT|(VectorPIC+IrqTIMER));
141 if(lapictimer.hz == 0ULL){
144 lapicw(LapicTICR, 0xffffffff);
149 lapictimer.hz = (0xffffffffUL-lapicr(LapicTCCR))*10;
150 lapictimer.max = lapictimer.hz/HZ;
151 lapictimer.min = lapictimer.hz/(100*HZ);
153 if(lapictimer.hz > hz-(hz/10)){
154 if(lapictimer.hz > hz+(hz/10))
155 panic("lapic clock %lld > cpu clock > %lld\n",
159 lapictimer.div = hz/lapictimer.hz;
164 lapicinit(Apic* apic)
169 lapicbase = apic->addr;
172 * These don't really matter in Physical mode;
173 * set the defaults anyway.
175 if(strncmp(m->cpuidid, "AuthenticAMD", 12) == 0)
181 lapicw(LapicDFR, dfr);
182 lapicw(LapicLDR, ldr);
183 lapicw(LapicTPR, 0xff);
184 lapicw(LapicSVR, LapicENABLE|(VectorPIC+IrqSPURIOUS));
189 * Some Pentium revisions have a bug whereby spurious
190 * interrupts are generated in the through-local mode.
192 switch(m->cpuidax & 0xFFF){
193 case 0x526: /* stepping cB1 */
194 case 0x52B: /* stepping E0 */
195 case 0x52C: /* stepping cC0 */
196 wrmsr(0x0E, 1<<14); /* TR12 */
201 * Set the local interrupts. It's likely these should just be
202 * masked off for SMP mode as some Pentium Pros have problems if
203 * LINT[01] are set to ExtINT.
204 * Acknowledge any outstanding interrupts.
205 lapicw(LapicLINT0, apic->lintr[0]);
206 lapicw(LapicLINT1, apic->lintr[1]);
210 lvt = (lapicr(LapicVER)>>16) & 0xFF;
212 lapicw(LapicPCINT, ApicIMASK);
213 lapicw(LapicERROR, VectorPIC+IrqERROR);
218 * Issue an INIT Level De-Assert to synchronise arbitration ID's.
220 lapicw(LapicICRHI, 0);
221 lapicw(LapicICRLO, LapicALLINC|ApicLEVEL|LapicDEASSERT|ApicINIT);
222 while(lapicr(LapicICRLO) & ApicDELIVS)
226 * Do not allow acceptance of interrupts until all initialisation
227 * for this processor is done. For the bootstrap processor this can be
228 * early duing initialisation. For the application processors this should
229 * be after the bootstrap processor has lowered priority and is accepting
236 lapicstartap(Apic* apic, int v)
241 crhi = apic->apicno<<24;
242 lapicw(LapicICRHI, crhi);
243 lapicw(LapicICRLO, LapicFIELD|ApicLEVEL|LapicASSERT|ApicINIT);
245 lapicw(LapicICRLO, LapicFIELD|ApicLEVEL|LapicDEASSERT|ApicINIT);
248 for(i = 0; i < 2; i++){
249 lapicw(LapicICRHI, crhi);
250 lapicw(LapicICRLO, LapicFIELD|ApicEDGE|ApicSTARTUP|(v/BY2PG));
256 lapicerror(Ureg*, void*)
261 esr = lapicr(LapicESR);
262 switch(m->cpuidax & 0xFFF){
263 case 0x526: /* stepping cB1 */
264 case 0x52B: /* stepping E0 */
265 case 0x52C: /* stepping cC0 */
268 print("cpu%d: lapicerror: 0x%8.8luX\n", m->machno, esr);
272 lapicspurious(Ureg*, void*)
274 print("cpu%d: lapicspurious\n", m->machno);
282 isr = lapicr(LapicISR + (v/32));
284 return isr & (1<<(v%32));
296 lapicicrw(ulong hi, ulong lo)
298 lapicw(LapicICRHI, hi);
299 lapicw(LapicICRLO, lo);
303 ioapicrdtr(Apic* apic, int sel, int* hi, int* lo)
307 iowin = apic->addr+(0x10/sizeof(ulong));
308 sel = IoapicRDT + 2*sel;
321 ioapicrdtw(Apic* apic, int sel, int hi, int lo)
325 iowin = apic->addr+(0x10/sizeof(ulong));
326 sel = IoapicRDT + 2*sel;
337 ioapicinit(Apic* apic, int apicno)
343 * Initialise the I/O APIC.
344 * The MultiProcessor Specification says it is the responsibility
345 * of the O/S to set the APIC id.
346 * Make sure interrupts are all masked off for now.
348 iowin = apic->addr+(0x10/sizeof(ulong));
350 *apic->addr = IoapicVER;
351 apic->mre = (*iowin>>16) & 0xFF;
353 *apic->addr = IoapicID;
359 for(v = 0; v <= apic->mre; v++)
360 ioapicrdtw(apic, v, hi, lo);
364 lapictimerset(uvlong next)
370 lock(&m->apictimerlock);
372 period = lapictimer.max;
374 period = next - fastticks(nil);
375 period /= lapictimer.div;
377 if(period < lapictimer.min)
378 period = lapictimer.min;
379 else if(period > lapictimer.max - lapictimer.min)
380 period = lapictimer.max;
382 lapicw(LapicTICR, period);
384 unlock(&m->apictimerlock);
389 lapicclock(Ureg *u, void*)
392 * since the MTRR updates need to be synchronized across processors,
393 * we want to do this within the clock tick.
408 lapicw(LapicTPR, 0xFF);
414 lapicw(LapicPCINT, ApicNMI);
418 lapicnmidisable(void)
420 lapicw(LapicPCINT, ApicIMASK);