xref: /xv6-public/lapic.c (revision 673b739d)
1 // The local APIC manages internal (non-I/O) interrupts.
2 // See Chapter 8 & Appendix C of Intel processor manual volume 3.
3 
4 #include "types.h"
5 #include "defs.h"
6 #include "memlayout.h"
7 #include "traps.h"
8 #include "mmu.h"
9 #include "x86.h"
10 
11 // Local APIC registers, divided by 4 for use as uint[] indices.
12 #define ID      (0x0020/4)   // ID
13 #define VER     (0x0030/4)   // Version
14 #define TPR     (0x0080/4)   // Task Priority
15 #define EOI     (0x00B0/4)   // EOI
16 #define SVR     (0x00F0/4)   // Spurious Interrupt Vector
17   #define ENABLE     0x00000100   // Unit Enable
18 #define ESR     (0x0280/4)   // Error Status
19 #define ICRLO   (0x0300/4)   // Interrupt Command
20   #define INIT       0x00000500   // INIT/RESET
21   #define STARTUP    0x00000600   // Startup IPI
22   #define DELIVS     0x00001000   // Delivery status
23   #define ASSERT     0x00004000   // Assert interrupt (vs deassert)
24   #define DEASSERT   0x00000000
25   #define LEVEL      0x00008000   // Level triggered
26   #define BCAST      0x00080000   // Send to all APICs, including self.
27   #define BUSY       0x00001000
28   #define FIXED      0x00000000
29 #define ICRHI   (0x0310/4)   // Interrupt Command [63:32]
30 #define TIMER   (0x0320/4)   // Local Vector Table 0 (TIMER)
31   #define X1         0x0000000B   // divide counts by 1
32   #define PERIODIC   0x00020000   // Periodic
33 #define PCINT   (0x0340/4)   // Performance Counter LVT
34 #define LINT0   (0x0350/4)   // Local Vector Table 1 (LINT0)
35 #define LINT1   (0x0360/4)   // Local Vector Table 2 (LINT1)
36 #define ERROR   (0x0370/4)   // Local Vector Table 3 (ERROR)
37   #define MASKED     0x00010000   // Interrupt masked
38 #define TICR    (0x0380/4)   // Timer Initial Count
39 #define TCCR    (0x0390/4)   // Timer Current Count
40 #define TDCR    (0x03E0/4)   // Timer Divide Configuration
41 
42 volatile uint *lapic;  // Initialized in mp.c
43 
44 static void
45 lapicw(int index, int value)
46 {
47   lapic[index] = value;
48   lapic[ID];  // wait for write to finish, by reading
49 }
50 
51 //PAGEBREAK!
52 void
53 lapicinit(int c)
54 {
55   cprintf("lapicinit: %d 0x%x\n", c, lapic);
56   if(!lapic)
57     return;
58 
59   // Enable local APIC; set spurious interrupt vector.
60   lapicw(SVR, ENABLE | (T_IRQ0 + IRQ_SPURIOUS));
61 
62   // The timer repeatedly counts down at bus frequency
63   // from lapic[TICR] and then issues an interrupt.
64   // If xv6 cared more about precise timekeeping,
65   // TICR would be calibrated using an external time source.
66   lapicw(TDCR, X1);
67   lapicw(TIMER, PERIODIC | (T_IRQ0 + IRQ_TIMER));
68   lapicw(TICR, 10000000);
69 
70   // Disable logical interrupt lines.
71   lapicw(LINT0, MASKED);
72   lapicw(LINT1, MASKED);
73 
74   // Disable performance counter overflow interrupts
75   // on machines that provide that interrupt entry.
76   if(((lapic[VER]>>16) & 0xFF) >= 4)
77     lapicw(PCINT, MASKED);
78 
79   // Map error interrupt to IRQ_ERROR.
80   lapicw(ERROR, T_IRQ0 + IRQ_ERROR);
81 
82   // Clear error status register (requires back-to-back writes).
83   lapicw(ESR, 0);
84   lapicw(ESR, 0);
85 
86   // Ack any outstanding interrupts.
87   lapicw(EOI, 0);
88 
89   // Send an Init Level De-Assert to synchronise arbitration ID's.
90   lapicw(ICRHI, 0);
91   lapicw(ICRLO, BCAST | INIT | LEVEL);
92   while(lapic[ICRLO] & DELIVS)
93     ;
94 
95   // Enable interrupts on the APIC (but not on the processor).
96   lapicw(TPR, 0);
97 }
98 
99 int
100 cpunum(void)
101 {
102   // Cannot call cpu when interrupts are enabled:
103   // result not guaranteed to last long enough to be used!
104   // Would prefer to panic but even printing is chancy here:
105   // almost everything, including cprintf and panic, calls cpu,
106   // often indirectly through acquire and release.
107   if(readeflags()&FL_IF){
108     static int n;
109     if(n++ == 0)
110       cprintf("cpu called from %x with interrupts enabled\n",
111         __builtin_return_address(0));
112   }
113 
114   if(lapic)
115     return lapic[ID]>>24;
116   return 0;
117 }
118 
119 // Acknowledge interrupt.
120 void
121 lapiceoi(void)
122 {
123   if(lapic)
124     lapicw(EOI, 0);
125 }
126 
127 // Spin for a given number of microseconds.
128 // On real hardware would want to tune this dynamically.
129 void
130 microdelay(int us)
131 {
132 }
133 
134 #define IO_RTC  0x70
135 
136 // Start additional processor running bootstrap code at addr.
137 // See Appendix B of MultiProcessor Specification.
138 void
139 
140 lapicstartap(uchar apicid, uint addr)
141 {
142   int i;
143   ushort *wrv;
144 
145   // "The BSP must initialize CMOS shutdown code to 0AH
146   // and the warm reset vector (DWORD based at 40:67) to point at
147   // the AP startup code prior to the [universal startup algorithm]."
148   outb(IO_RTC, 0xF);  // offset 0xF is shutdown code
149   outb(IO_RTC+1, 0x0A);
150   wrv = (ushort*)P2V((0x40<<4 | 0x67));  // Warm reset vector
151   wrv[0] = 0;
152   wrv[1] = addr >> 4;
153 
154   // "Universal startup algorithm."
155   // Send INIT (level-triggered) interrupt to reset other CPU.
156   lapicw(ICRHI, apicid<<24);
157   lapicw(ICRLO, INIT | LEVEL | ASSERT);
158   microdelay(200);
159   lapicw(ICRLO, INIT | LEVEL);
160   microdelay(100);    // should be 10ms, but too slow in Bochs!
161 
162   // Send startup IPI (twice!) to enter bootstrap code.
163   // Regular hardware is supposed to only accept a STARTUP
164   // when it is in the halted state due to an INIT.  So the second
165   // should be ignored, but it is part of the official Intel algorithm.
166   // Bochs complains about the second one.  Too bad for Bochs.
167   for(i = 0; i < 2; i++){
168     lapicw(ICRHI, apicid<<24);
169     lapicw(ICRLO, STARTUP | (addr>>12));
170     microdelay(200);
171   }
172 }
173