apic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "mp.h"
  8. enum { /* Local APIC registers */
  9. LapicID = 0x0020, /* ID */
  10. LapicVER = 0x0030, /* Version */
  11. LapicTPR = 0x0080, /* Task Priority */
  12. LapicAPR = 0x0090, /* Arbitration Priority */
  13. LapicPPR = 0x00A0, /* Processor Priority */
  14. LapicEOI = 0x00B0, /* EOI */
  15. LapicLDR = 0x00D0, /* Logical Destination */
  16. LapicDFR = 0x00E0, /* Destination Format */
  17. LapicSVR = 0x00F0, /* Spurious Interrupt Vector */
  18. LapicISR = 0x0100, /* Interrupt Status (8 registers) */
  19. LapicTMR = 0x0180, /* Trigger Mode (8 registers) */
  20. LapicIRR = 0x0200, /* Interrupt Request (8 registers) */
  21. LapicESR = 0x0280, /* Error Status */
  22. LapicICRLO = 0x0300, /* Interrupt Command */
  23. LapicICRHI = 0x0310, /* Interrupt Command [63:32] */
  24. LapicTIMER = 0x0320, /* Local Vector Table 0 (TIMER) */
  25. LapicPCINT = 0x0340, /* Performance Counter LVT */
  26. LapicLINT0 = 0x0350, /* Local Vector Table 1 (LINT0) */
  27. LapicLINT1 = 0x0360, /* Local Vector Table 2 (LINT1) */
  28. LapicERROR = 0x0370, /* Local Vector Table 3 (ERROR) */
  29. LapicTICR = 0x0380, /* Timer Initial Count */
  30. LapicTCCR = 0x0390, /* Timer Current Count */
  31. LapicTDCR = 0x03E0, /* Timer Divide Configuration */
  32. };
  33. enum { /* LapicSVR */
  34. LapicENABLE = 0x00000100, /* Unit Enable */
  35. LapicFOCUS = 0x00000200, /* Focus Processor Checking Disable */
  36. };
  37. enum { /* LapicICRLO */
  38. /* [14] IPI Trigger Mode Level (RW) */
  39. LapicDEASSERT = 0x00000000, /* Deassert level-sensitive interrupt */
  40. LapicASSERT = 0x00004000, /* Assert level-sensitive interrupt */
  41. /* [17:16] Remote Read Status */
  42. LapicINVALID = 0x00000000, /* Invalid */
  43. LapicWAIT = 0x00010000, /* In-Progress */
  44. LapicVALID = 0x00020000, /* Valid */
  45. /* [19:18] Destination Shorthand */
  46. LapicFIELD = 0x00000000, /* No shorthand */
  47. LapicSELF = 0x00040000, /* Self is single destination */
  48. LapicALLINC = 0x00080000, /* All including self */
  49. LapicALLEXC = 0x000C0000, /* All Excluding self */
  50. };
  51. enum { /* LapicESR */
  52. LapicSENDCS = 0x00000001, /* Send CS Error */
  53. LapicRCVCS = 0x00000002, /* Receive CS Error */
  54. LapicSENDACCEPT = 0x00000004, /* Send Accept Error */
  55. LapicRCVACCEPT = 0x00000008, /* Receive Accept Error */
  56. LapicSENDVECTOR = 0x00000020, /* Send Illegal Vector */
  57. LapicRCVVECTOR = 0x00000040, /* Receive Illegal Vector */
  58. LapicREGISTER = 0x00000080, /* Illegal Register Address */
  59. };
  60. enum { /* LapicTIMER */
  61. /* [17] Timer Mode (RW) */
  62. LapicONESHOT = 0x00000000, /* One-shot */
  63. LapicPERIODIC = 0x00020000, /* Periodic */
  64. /* [19:18] Timer Base (RW) */
  65. LapicCLKIN = 0x00000000, /* use CLKIN as input */
  66. LapicTMBASE = 0x00040000, /* use TMBASE */
  67. LapicDIVIDER = 0x00080000, /* use output of the divider */
  68. };
  69. enum { /* LapicTDCR */
  70. LapicX2 = 0x00000000, /* divide by 2 */
  71. LapicX4 = 0x00000001, /* divide by 4 */
  72. LapicX8 = 0x00000002, /* divide by 8 */
  73. LapicX16 = 0x00000003, /* divide by 16 */
  74. LapicX32 = 0x00000008, /* divide by 32 */
  75. LapicX64 = 0x00000009, /* divide by 64 */
  76. LapicX128 = 0x0000000A, /* divide by 128 */
  77. LapicX1 = 0x0000000B, /* divide by 1 */
  78. };
  79. static ulong* lapicbase;
  80. struct
  81. {
  82. uvlong hz;
  83. ulong max;
  84. ulong min;
  85. ulong div;
  86. } lapictimer;
  87. static ulong
  88. lapicr(int r)
  89. {
  90. return *(lapicbase+(r/sizeof(*lapicbase)));
  91. }
  92. static void
  93. lapicw(int r, ulong data)
  94. {
  95. *(lapicbase+(r/sizeof(*lapicbase))) = data;
  96. data = *(lapicbase+(LapicID/sizeof(*lapicbase)));
  97. USED(data);
  98. }
  99. void
  100. lapiconline(void)
  101. {
  102. /*
  103. * Reload the timer to de-synchronise the processors,
  104. * then lower the task priority to allow interrupts to be
  105. * accepted by the APIC.
  106. */
  107. microdelay((TK2MS(1)*1000/conf.nmach) * m->machno);
  108. lapicw(LapicTICR, lapictimer.max);
  109. lapicw(LapicTIMER, LapicCLKIN|LapicPERIODIC|(VectorPIC+IrqTIMER));
  110. lapicw(LapicTPR, 0);
  111. }
  112. /*
  113. * use the i8253 clock to figure out our lapic timer rate.
  114. */
  115. static void
  116. lapictimerinit(void)
  117. {
  118. uvlong x, v, hz;
  119. v = m->cpuhz/1000;
  120. lapicw(LapicTDCR, LapicX1);
  121. lapicw(LapicTIMER, ApicIMASK|LapicCLKIN|LapicONESHOT|(VectorPIC+IrqTIMER));
  122. if(lapictimer.hz == 0ULL){
  123. x = fastticks(&hz);
  124. x += hz/10;
  125. lapicw(LapicTICR, 0xffffffff);
  126. do{
  127. v = fastticks(nil);
  128. }while(v < x);
  129. lapictimer.hz = (0xffffffffUL-lapicr(LapicTCCR))*10;
  130. lapictimer.max = lapictimer.hz/HZ;
  131. lapictimer.min = lapictimer.hz/(100*HZ);
  132. if(lapictimer.hz > hz)
  133. panic("lapic clock faster than cpu clock %lld > %lld",
  134. lapictimer.hz, hz);
  135. lapictimer.div = hz/lapictimer.hz;
  136. }
  137. }
  138. void
  139. lapicinit(Apic* apic)
  140. {
  141. ulong r, lvt;
  142. if(lapicbase == 0)
  143. lapicbase = apic->addr;
  144. lapicw(LapicDFR, 0xFFFFFFFF);
  145. r = (lapicr(LapicID)>>24) & 0xFF;
  146. lapicw(LapicLDR, (1<<r)<<24);
  147. lapicw(LapicTPR, 0xFF);
  148. lapicw(LapicSVR, LapicENABLE|(VectorPIC+IrqSPURIOUS));
  149. lapictimerinit();
  150. /*
  151. * Some Pentium revisions have a bug whereby spurious
  152. * interrupts are generated in the through-local mode.
  153. */
  154. switch(m->cpuidax & 0xFFF){
  155. case 0x526: /* stepping cB1 */
  156. case 0x52B: /* stepping E0 */
  157. case 0x52C: /* stepping cC0 */
  158. wrmsr(0x0E, 1<<14); /* TR12 */
  159. break;
  160. }
  161. /*
  162. * Set the local interrupts. It's likely these should just be
  163. * masked off for SMP mode as some Pentium Pros have problems if
  164. * LINT[01] are set to ExtINT.
  165. * Acknowledge any outstanding interrupts.
  166. lapicw(LapicLINT0, apic->lintr[0]);
  167. lapicw(LapicLINT1, apic->lintr[1]);
  168. */
  169. lapiceoi(0);
  170. lvt = (lapicr(LapicVER)>>16) & 0xFF;
  171. if(lvt >= 4)
  172. lapicw(LapicPCINT, ApicIMASK);
  173. lapicw(LapicERROR, VectorPIC+IrqERROR);
  174. lapicw(LapicESR, 0);
  175. lapicr(LapicESR);
  176. /*
  177. * Issue an INIT Level De-Assert to synchronise arbitration ID's.
  178. */
  179. lapicw(LapicICRHI, 0);
  180. lapicw(LapicICRLO, LapicALLINC|ApicLEVEL|LapicDEASSERT|ApicINIT);
  181. while(lapicr(LapicICRLO) & ApicDELIVS)
  182. ;
  183. /*
  184. * Do not allow acceptance of interrupts until all initialisation
  185. * for this processor is done. For the bootstrap processor this can be
  186. * early duing initialisation. For the application processors this should
  187. * be after the bootstrap processor has lowered priority and is accepting
  188. * interrupts.
  189. lapicw(LapicTPR, 0);
  190. */
  191. }
  192. void
  193. lapicstartap(Apic* apic, int v)
  194. {
  195. int i;
  196. ulong crhi;
  197. crhi = apic->apicno<<24;
  198. lapicw(LapicICRHI, crhi);
  199. lapicw(LapicICRLO, LapicFIELD|ApicLEVEL|LapicASSERT|ApicINIT);
  200. microdelay(200);
  201. lapicw(LapicICRLO, LapicFIELD|ApicLEVEL|LapicDEASSERT|ApicINIT);
  202. delay(10);
  203. for(i = 0; i < 2; i++){
  204. lapicw(LapicICRHI, crhi);
  205. lapicw(LapicICRLO, LapicFIELD|ApicEDGE|ApicSTARTUP|(v/BY2PG));
  206. microdelay(200);
  207. }
  208. }
  209. void
  210. lapicerror(Ureg*, void*)
  211. {
  212. ulong esr;
  213. lapicw(LapicESR, 0);
  214. esr = lapicr(LapicESR);
  215. switch(m->cpuidax & 0xFFF){
  216. case 0x526: /* stepping cB1 */
  217. case 0x52B: /* stepping E0 */
  218. case 0x52C: /* stepping cC0 */
  219. return;
  220. }
  221. print("cpu%d: lapicerror: 0x%8.8luX\n", m->machno, esr);
  222. }
  223. void
  224. lapicspurious(Ureg*, void*)
  225. {
  226. print("cpu%d: lapicspurious\n", m->machno);
  227. }
  228. int
  229. lapicisr(int v)
  230. {
  231. ulong isr;
  232. isr = lapicr(LapicISR + (v/32));
  233. return isr & (1<<(v%32));
  234. }
  235. int
  236. lapiceoi(int v)
  237. {
  238. lapicw(LapicEOI, 0);
  239. return v;
  240. }
  241. void
  242. lapicicrw(ulong hi, ulong lo)
  243. {
  244. lapicw(LapicICRHI, hi);
  245. lapicw(LapicICRLO, lo);
  246. }
  247. void
  248. ioapicrdtr(Apic* apic, int sel, int* hi, int* lo)
  249. {
  250. ulong *iowin;
  251. iowin = apic->addr+(0x10/sizeof(ulong));
  252. sel = IoapicRDT + 2*sel;
  253. lock(apic);
  254. *apic->addr = sel+1;
  255. if(hi)
  256. *hi = *iowin;
  257. *apic->addr = sel;
  258. if(lo)
  259. *lo = *iowin;
  260. unlock(apic);
  261. }
  262. void
  263. ioapicrdtw(Apic* apic, int sel, int hi, int lo)
  264. {
  265. ulong *iowin;
  266. iowin = apic->addr+(0x10/sizeof(ulong));
  267. sel = IoapicRDT + 2*sel;
  268. lock(apic);
  269. *apic->addr = sel+1;
  270. *iowin = hi;
  271. *apic->addr = sel;
  272. *iowin = lo;
  273. unlock(apic);
  274. }
  275. void
  276. ioapicinit(Apic* apic, int apicno)
  277. {
  278. int hi, lo, v;
  279. ulong *iowin;
  280. /*
  281. * Initialise the I/O APIC.
  282. * The MultiProcessor Specification says it is the responsibility
  283. * of the O/S to set the APIC id.
  284. * Make sure interrupts are all masked off for now.
  285. */
  286. iowin = apic->addr+(0x10/sizeof(ulong));
  287. lock(apic);
  288. *apic->addr = IoapicVER;
  289. apic->mre = (*iowin>>16) & 0xFF;
  290. *apic->addr = IoapicID;
  291. *iowin = apicno<<24;
  292. unlock(apic);
  293. hi = 0;
  294. lo = ApicIMASK;
  295. for(v = 0; v <= apic->mre; v++)
  296. ioapicrdtw(apic, v, hi, lo);
  297. }
  298. void
  299. lapictimerset(uvlong next)
  300. {
  301. vlong period;
  302. int x;
  303. x = splhi();
  304. lock(&m->apictimerlock);
  305. period = lapictimer.max;
  306. if(next != 0){
  307. period = next - fastticks(nil);
  308. period /= lapictimer.div;
  309. if(period < lapictimer.min)
  310. period = lapictimer.min;
  311. else if(period > lapictimer.max - lapictimer.min)
  312. period = lapictimer.max;
  313. }
  314. lapicw(LapicTICR, period);
  315. unlock(&m->apictimerlock);
  316. splx(x);
  317. }
  318. void
  319. lapicclock(Ureg *u, void*)
  320. {
  321. timerintr(u, 0);
  322. }
  323. void
  324. lapicintron(void)
  325. {
  326. lapicw(LapicTPR, 0);
  327. }
  328. void
  329. lapicintroff(void)
  330. {
  331. lapicw(LapicTPR, 0xFF);
  332. }
  333. void
  334. lapicnmienable(void)
  335. {
  336. lapicw(LapicPCINT, ApicNMI);
  337. }
  338. void
  339. lapicnmidisable(void)
  340. {
  341. lapicw(LapicPCINT, ApicIMASK);
  342. }