xref: /xv6-public/lapic.c (revision ed396c06)
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 "param.h"
5 #include "types.h"
6 #include "defs.h"
7 #include "date.h"
8 #include "memlayout.h"
9 #include "traps.h"
10 #include "mmu.h"
11 #include "x86.h"
12 #include "proc.h"  // ncpu
13 
14 // Local APIC registers, divided by 4 for use as uint[] indices.
15 #define ID      (0x0020/4)   // ID
16 #define VER     (0x0030/4)   // Version
17 #define TPR     (0x0080/4)   // Task Priority
18 #define EOI     (0x00B0/4)   // EOI
19 #define SVR     (0x00F0/4)   // Spurious Interrupt Vector
20   #define ENABLE     0x00000100   // Unit Enable
21 #define ESR     (0x0280/4)   // Error Status
22 #define ICRLO   (0x0300/4)   // Interrupt Command
23   #define INIT       0x00000500   // INIT/RESET
24   #define STARTUP    0x00000600   // Startup IPI
25   #define DELIVS     0x00001000   // Delivery status
26   #define ASSERT     0x00004000   // Assert interrupt (vs deassert)
27   #define DEASSERT   0x00000000
28   #define LEVEL      0x00008000   // Level triggered
29   #define BCAST      0x00080000   // Send to all APICs, including self.
30   #define BUSY       0x00001000
31   #define FIXED      0x00000000
32 #define ICRHI   (0x0310/4)   // Interrupt Command [63:32]
33 #define TIMER   (0x0320/4)   // Local Vector Table 0 (TIMER)
34   #define X1         0x0000000B   // divide counts by 1
35   #define PERIODIC   0x00020000   // Periodic
36 #define PCINT   (0x0340/4)   // Performance Counter LVT
37 #define LINT0   (0x0350/4)   // Local Vector Table 1 (LINT0)
38 #define LINT1   (0x0360/4)   // Local Vector Table 2 (LINT1)
39 #define ERROR   (0x0370/4)   // Local Vector Table 3 (ERROR)
40   #define MASKED     0x00010000   // Interrupt masked
41 #define TICR    (0x0380/4)   // Timer Initial Count
42 #define TCCR    (0x0390/4)   // Timer Current Count
43 #define TDCR    (0x03E0/4)   // Timer Divide Configuration
44 
45 volatile uint *lapic;  // Initialized in mp.c
46 
47 static void
48 lapicw(int index, int value)
49 {
50   lapic[index] = value;
51   lapic[ID];  // wait for write to finish, by reading
52 }
53 //PAGEBREAK!
54 
55 void
56 lapicinit(void)
57 {
58   if(!lapic)
59     return;
60 
61   // Enable local APIC; set spurious interrupt vector.
62   lapicw(SVR, ENABLE | (T_IRQ0 + IRQ_SPURIOUS));
63 
64   // The timer repeatedly counts down at bus frequency
65   // from lapic[TICR] and then issues an interrupt.
66   // If xv6 cared more about precise timekeeping,
67   // TICR would be calibrated using an external time source.
68   lapicw(TDCR, X1);
69   lapicw(TIMER, PERIODIC | (T_IRQ0 + IRQ_TIMER));
70   lapicw(TICR, 10000000);
71 
72   // Disable logical interrupt lines.
73   lapicw(LINT0, MASKED);
74   lapicw(LINT1, MASKED);
75 
76   // Disable performance counter overflow interrupts
77   // on machines that provide that interrupt entry.
78   if(((lapic[VER]>>16) & 0xFF) >= 4)
79     lapicw(PCINT, MASKED);
80 
81   // Map error interrupt to IRQ_ERROR.
82   lapicw(ERROR, T_IRQ0 + IRQ_ERROR);
83 
84   // Clear error status register (requires back-to-back writes).
85   lapicw(ESR, 0);
86   lapicw(ESR, 0);
87 
88   // Ack any outstanding interrupts.
89   lapicw(EOI, 0);
90 
91   // Send an Init Level De-Assert to synchronise arbitration ID's.
92   lapicw(ICRHI, 0);
93   lapicw(ICRLO, BCAST | INIT | LEVEL);
94   while(lapic[ICRLO] & DELIVS)
95     ;
96 
97   // Enable interrupts on the APIC (but not on the processor).
98   lapicw(TPR, 0);
99 }
100 
101 // Should be called with interrupts disabled: the calling thread shouldn't be
102 // rescheduled between reading lapic[ID] and checking against cpu array.
103 int
104 lapiccpunum(void)
105 {
106   int apicid, i;
107 
108   if (!lapic)
109     return 0;
110 
111   apicid = lapic[ID] >> 24;
112   for (i = 0; i < ncpu; ++i) {
113     if (cpus[i].apicid == apicid)
114       return i;
115   }
116   panic("unknown apicid\n");
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 CMOS_PORT    0x70
135 #define CMOS_RETURN  0x71
136 
137 // Start additional processor running entry code at addr.
138 // See Appendix B of MultiProcessor Specification.
139 void
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(CMOS_PORT, 0xF);  // offset 0xF is shutdown code
149   outb(CMOS_PORT+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 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 
174 #define CMOS_STATA   0x0a
175 #define CMOS_STATB   0x0b
176 #define CMOS_UIP    (1 << 7)        // RTC update in progress
177 
178 #define SECS    0x00
179 #define MINS    0x02
180 #define HOURS   0x04
181 #define DAY     0x07
182 #define MONTH   0x08
183 #define YEAR    0x09
184 
185 static uint cmos_read(uint reg)
186 {
187   outb(CMOS_PORT,  reg);
188   microdelay(200);
189 
190   return inb(CMOS_RETURN);
191 }
192 
193 static void fill_rtcdate(struct rtcdate *r)
194 {
195   r->second = cmos_read(SECS);
196   r->minute = cmos_read(MINS);
197   r->hour   = cmos_read(HOURS);
198   r->day    = cmos_read(DAY);
199   r->month  = cmos_read(MONTH);
200   r->year   = cmos_read(YEAR);
201 }
202 
203 // qemu seems to use 24-hour GWT and the values are BCD encoded
204 void cmostime(struct rtcdate *r)
205 {
206   struct rtcdate t1, t2;
207   int sb, bcd;
208 
209   sb = cmos_read(CMOS_STATB);
210 
211   bcd = (sb & (1 << 2)) == 0;
212 
213   // make sure CMOS doesn't modify time while we read it
214   for(;;) {
215     fill_rtcdate(&t1);
216     if(cmos_read(CMOS_STATA) & CMOS_UIP)
217         continue;
218     fill_rtcdate(&t2);
219     if(memcmp(&t1, &t2, sizeof(t1)) == 0)
220       break;
221   }
222 
223   // convert
224   if(bcd) {
225 #define    CONV(x)     (t1.x = ((t1.x >> 4) * 10) + (t1.x & 0xf))
226     CONV(second);
227     CONV(minute);
228     CONV(hour  );
229     CONV(day   );
230     CONV(month );
231     CONV(year  );
232 #undef     CONV
233   }
234 
235   *r = t1;
236   r->year += 2000;
237 }
238