apic.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * Local APIC handling, local APIC timers
  3. *
  4. * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Fixes
  7. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  8. * thanks to Eric Gilmore
  9. * and Rolf G. Tews
  10. * for testing these extensively.
  11. * Maciej W. Rozycki : Various updates and fixes.
  12. * Mikael Pettersson : Power Management for UP-APIC.
  13. * Pavel Machek and
  14. * Mikael Pettersson : PM converted to driver model.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/delay.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mc146818rtc.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/module.h>
  26. #include <asm/atomic.h>
  27. #include <asm/smp.h>
  28. #include <asm/mtrr.h>
  29. #include <asm/mpspec.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/mach_apic.h>
  32. #include <asm/nmi.h>
  33. #include <asm/idle.h>
  34. #include <asm/proto.h>
  35. #include <asm/timex.h>
  36. int apic_verbosity;
  37. int apic_runs_main_timer;
  38. int apic_calibrate_pmtmr __initdata;
  39. int disable_apic_timer __initdata;
  40. /*
  41. * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
  42. * IPIs in place of local APIC timers
  43. */
  44. static cpumask_t timer_interrupt_broadcast_ipi_mask;
  45. /* Using APIC to generate smp_local_timer_interrupt? */
  46. int using_apic_timer __read_mostly = 0;
  47. static void apic_pm_activate(void);
  48. void enable_NMI_through_LVT0 (void * dummy)
  49. {
  50. unsigned int v;
  51. v = APIC_DM_NMI; /* unmask and set to NMI */
  52. apic_write(APIC_LVT0, v);
  53. }
  54. int get_maxlvt(void)
  55. {
  56. unsigned int v, maxlvt;
  57. v = apic_read(APIC_LVR);
  58. maxlvt = GET_APIC_MAXLVT(v);
  59. return maxlvt;
  60. }
  61. /*
  62. * 'what should we do if we get a hw irq event on an illegal vector'.
  63. * each architecture has to answer this themselves.
  64. */
  65. void ack_bad_irq(unsigned int irq)
  66. {
  67. printk("unexpected IRQ trap at vector %02x\n", irq);
  68. /*
  69. * Currently unexpected vectors happen only on SMP and APIC.
  70. * We _must_ ack these because every local APIC has only N
  71. * irq slots per priority level, and a 'hanging, unacked' IRQ
  72. * holds up an irq slot - in excessive cases (when multiple
  73. * unexpected vectors occur) that might lock up the APIC
  74. * completely.
  75. * But don't ack when the APIC is disabled. -AK
  76. */
  77. if (!disable_apic)
  78. ack_APIC_irq();
  79. }
  80. void clear_local_APIC(void)
  81. {
  82. int maxlvt;
  83. unsigned int v;
  84. maxlvt = get_maxlvt();
  85. /*
  86. * Masking an LVT entry can trigger a local APIC error
  87. * if the vector is zero. Mask LVTERR first to prevent this.
  88. */
  89. if (maxlvt >= 3) {
  90. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  91. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  92. }
  93. /*
  94. * Careful: we have to set masks only first to deassert
  95. * any level-triggered sources.
  96. */
  97. v = apic_read(APIC_LVTT);
  98. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  99. v = apic_read(APIC_LVT0);
  100. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  101. v = apic_read(APIC_LVT1);
  102. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  103. if (maxlvt >= 4) {
  104. v = apic_read(APIC_LVTPC);
  105. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  106. }
  107. /*
  108. * Clean APIC state for other OSs:
  109. */
  110. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  111. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  112. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  113. if (maxlvt >= 3)
  114. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  115. if (maxlvt >= 4)
  116. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  117. v = GET_APIC_VERSION(apic_read(APIC_LVR));
  118. apic_write(APIC_ESR, 0);
  119. apic_read(APIC_ESR);
  120. }
  121. void __init connect_bsp_APIC(void)
  122. {
  123. if (pic_mode) {
  124. /*
  125. * Do not trust the local APIC being empty at bootup.
  126. */
  127. clear_local_APIC();
  128. /*
  129. * PIC mode, enable APIC mode in the IMCR, i.e.
  130. * connect BSP's local APIC to INT and NMI lines.
  131. */
  132. apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
  133. outb(0x70, 0x22);
  134. outb(0x01, 0x23);
  135. }
  136. }
  137. void disconnect_bsp_APIC(int virt_wire_setup)
  138. {
  139. if (pic_mode) {
  140. /*
  141. * Put the board back into PIC mode (has an effect
  142. * only on certain older boards). Note that APIC
  143. * interrupts, including IPIs, won't work beyond
  144. * this point! The only exception are INIT IPIs.
  145. */
  146. apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
  147. outb(0x70, 0x22);
  148. outb(0x00, 0x23);
  149. }
  150. else {
  151. /* Go back to Virtual Wire compatibility mode */
  152. unsigned long value;
  153. /* For the spurious interrupt use vector F, and enable it */
  154. value = apic_read(APIC_SPIV);
  155. value &= ~APIC_VECTOR_MASK;
  156. value |= APIC_SPIV_APIC_ENABLED;
  157. value |= 0xf;
  158. apic_write(APIC_SPIV, value);
  159. if (!virt_wire_setup) {
  160. /* For LVT0 make it edge triggered, active high, external and enabled */
  161. value = apic_read(APIC_LVT0);
  162. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  163. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  164. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
  165. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  166. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  167. apic_write(APIC_LVT0, value);
  168. }
  169. else {
  170. /* Disable LVT0 */
  171. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  172. }
  173. /* For LVT1 make it edge triggered, active high, nmi and enabled */
  174. value = apic_read(APIC_LVT1);
  175. value &= ~(
  176. APIC_MODE_MASK | APIC_SEND_PENDING |
  177. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  178. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  179. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  180. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  181. apic_write(APIC_LVT1, value);
  182. }
  183. }
  184. void disable_local_APIC(void)
  185. {
  186. unsigned int value;
  187. clear_local_APIC();
  188. /*
  189. * Disable APIC (implies clearing of registers
  190. * for 82489DX!).
  191. */
  192. value = apic_read(APIC_SPIV);
  193. value &= ~APIC_SPIV_APIC_ENABLED;
  194. apic_write(APIC_SPIV, value);
  195. }
  196. /*
  197. * This is to verify that we're looking at a real local APIC.
  198. * Check these against your board if the CPUs aren't getting
  199. * started for no apparent reason.
  200. */
  201. int __init verify_local_APIC(void)
  202. {
  203. unsigned int reg0, reg1;
  204. /*
  205. * The version register is read-only in a real APIC.
  206. */
  207. reg0 = apic_read(APIC_LVR);
  208. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  209. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  210. reg1 = apic_read(APIC_LVR);
  211. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  212. /*
  213. * The two version reads above should print the same
  214. * numbers. If the second one is different, then we
  215. * poke at a non-APIC.
  216. */
  217. if (reg1 != reg0)
  218. return 0;
  219. /*
  220. * Check if the version looks reasonably.
  221. */
  222. reg1 = GET_APIC_VERSION(reg0);
  223. if (reg1 == 0x00 || reg1 == 0xff)
  224. return 0;
  225. reg1 = get_maxlvt();
  226. if (reg1 < 0x02 || reg1 == 0xff)
  227. return 0;
  228. /*
  229. * The ID register is read/write in a real APIC.
  230. */
  231. reg0 = apic_read(APIC_ID);
  232. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  233. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  234. reg1 = apic_read(APIC_ID);
  235. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  236. apic_write(APIC_ID, reg0);
  237. if (reg1 != (reg0 ^ APIC_ID_MASK))
  238. return 0;
  239. /*
  240. * The next two are just to see if we have sane values.
  241. * They're only really relevant if we're in Virtual Wire
  242. * compatibility mode, but most boxes are anymore.
  243. */
  244. reg0 = apic_read(APIC_LVT0);
  245. apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
  246. reg1 = apic_read(APIC_LVT1);
  247. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  248. return 1;
  249. }
  250. void __init sync_Arb_IDs(void)
  251. {
  252. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  253. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  254. if (ver >= 0x14) /* P4 or higher */
  255. return;
  256. /*
  257. * Wait for idle.
  258. */
  259. apic_wait_icr_idle();
  260. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  261. apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  262. | APIC_DM_INIT);
  263. }
  264. extern void __error_in_apic_c (void);
  265. /*
  266. * An initial setup of the virtual wire mode.
  267. */
  268. void __init init_bsp_APIC(void)
  269. {
  270. unsigned int value;
  271. /*
  272. * Don't do the setup now if we have a SMP BIOS as the
  273. * through-I/O-APIC virtual wire mode might be active.
  274. */
  275. if (smp_found_config || !cpu_has_apic)
  276. return;
  277. value = apic_read(APIC_LVR);
  278. /*
  279. * Do not trust the local APIC being empty at bootup.
  280. */
  281. clear_local_APIC();
  282. /*
  283. * Enable APIC.
  284. */
  285. value = apic_read(APIC_SPIV);
  286. value &= ~APIC_VECTOR_MASK;
  287. value |= APIC_SPIV_APIC_ENABLED;
  288. value |= APIC_SPIV_FOCUS_DISABLED;
  289. value |= SPURIOUS_APIC_VECTOR;
  290. apic_write(APIC_SPIV, value);
  291. /*
  292. * Set up the virtual wire mode.
  293. */
  294. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  295. value = APIC_DM_NMI;
  296. apic_write(APIC_LVT1, value);
  297. }
  298. void __cpuinit setup_local_APIC (void)
  299. {
  300. unsigned int value, maxlvt;
  301. int i, j;
  302. value = apic_read(APIC_LVR);
  303. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  304. __error_in_apic_c();
  305. /*
  306. * Double-check whether this APIC is really registered.
  307. * This is meaningless in clustered apic mode, so we skip it.
  308. */
  309. if (!apic_id_registered())
  310. BUG();
  311. /*
  312. * Intel recommends to set DFR, LDR and TPR before enabling
  313. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  314. * document number 292116). So here it goes...
  315. */
  316. init_apic_ldr();
  317. /*
  318. * Set Task Priority to 'accept all'. We never change this
  319. * later on.
  320. */
  321. value = apic_read(APIC_TASKPRI);
  322. value &= ~APIC_TPRI_MASK;
  323. apic_write(APIC_TASKPRI, value);
  324. /*
  325. * After a crash, we no longer service the interrupts and a pending
  326. * interrupt from previous kernel might still have ISR bit set.
  327. *
  328. * Most probably by now CPU has serviced that pending interrupt and
  329. * it might not have done the ack_APIC_irq() because it thought,
  330. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  331. * does not clear the ISR bit and cpu thinks it has already serivced
  332. * the interrupt. Hence a vector might get locked. It was noticed
  333. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  334. */
  335. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  336. value = apic_read(APIC_ISR + i*0x10);
  337. for (j = 31; j >= 0; j--) {
  338. if (value & (1<<j))
  339. ack_APIC_irq();
  340. }
  341. }
  342. /*
  343. * Now that we are all set up, enable the APIC
  344. */
  345. value = apic_read(APIC_SPIV);
  346. value &= ~APIC_VECTOR_MASK;
  347. /*
  348. * Enable APIC
  349. */
  350. value |= APIC_SPIV_APIC_ENABLED;
  351. /*
  352. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  353. * certain networking cards. If high frequency interrupts are
  354. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  355. * entry is masked/unmasked at a high rate as well then sooner or
  356. * later IOAPIC line gets 'stuck', no more interrupts are received
  357. * from the device. If focus CPU is disabled then the hang goes
  358. * away, oh well :-(
  359. *
  360. * [ This bug can be reproduced easily with a level-triggered
  361. * PCI Ne2000 networking cards and PII/PIII processors, dual
  362. * BX chipset. ]
  363. */
  364. /*
  365. * Actually disabling the focus CPU check just makes the hang less
  366. * frequent as it makes the interrupt distributon model be more
  367. * like LRU than MRU (the short-term load is more even across CPUs).
  368. * See also the comment in end_level_ioapic_irq(). --macro
  369. */
  370. #if 1
  371. /* Enable focus processor (bit==0) */
  372. value &= ~APIC_SPIV_FOCUS_DISABLED;
  373. #else
  374. /* Disable focus processor (bit==1) */
  375. value |= APIC_SPIV_FOCUS_DISABLED;
  376. #endif
  377. /*
  378. * Set spurious IRQ vector
  379. */
  380. value |= SPURIOUS_APIC_VECTOR;
  381. apic_write(APIC_SPIV, value);
  382. /*
  383. * Set up LVT0, LVT1:
  384. *
  385. * set up through-local-APIC on the BP's LINT0. This is not
  386. * strictly necessary in pure symmetric-IO mode, but sometimes
  387. * we delegate interrupts to the 8259A.
  388. */
  389. /*
  390. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  391. */
  392. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  393. if (!smp_processor_id() && (pic_mode || !value)) {
  394. value = APIC_DM_EXTINT;
  395. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
  396. } else {
  397. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  398. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
  399. }
  400. apic_write(APIC_LVT0, value);
  401. /*
  402. * only the BP should see the LINT1 NMI signal, obviously.
  403. */
  404. if (!smp_processor_id())
  405. value = APIC_DM_NMI;
  406. else
  407. value = APIC_DM_NMI | APIC_LVT_MASKED;
  408. apic_write(APIC_LVT1, value);
  409. {
  410. unsigned oldvalue;
  411. maxlvt = get_maxlvt();
  412. oldvalue = apic_read(APIC_ESR);
  413. value = ERROR_APIC_VECTOR; // enables sending errors
  414. apic_write(APIC_LVTERR, value);
  415. /*
  416. * spec says clear errors after enabling vector.
  417. */
  418. if (maxlvt > 3)
  419. apic_write(APIC_ESR, 0);
  420. value = apic_read(APIC_ESR);
  421. if (value != oldvalue)
  422. apic_printk(APIC_VERBOSE,
  423. "ESR value after enabling vector: %08x, after %08x\n",
  424. oldvalue, value);
  425. }
  426. nmi_watchdog_default();
  427. if (nmi_watchdog == NMI_LOCAL_APIC)
  428. setup_apic_nmi_watchdog();
  429. apic_pm_activate();
  430. }
  431. #ifdef CONFIG_PM
  432. static struct {
  433. /* 'active' is true if the local APIC was enabled by us and
  434. not the BIOS; this signifies that we are also responsible
  435. for disabling it before entering apm/acpi suspend */
  436. int active;
  437. /* r/w apic fields */
  438. unsigned int apic_id;
  439. unsigned int apic_taskpri;
  440. unsigned int apic_ldr;
  441. unsigned int apic_dfr;
  442. unsigned int apic_spiv;
  443. unsigned int apic_lvtt;
  444. unsigned int apic_lvtpc;
  445. unsigned int apic_lvt0;
  446. unsigned int apic_lvt1;
  447. unsigned int apic_lvterr;
  448. unsigned int apic_tmict;
  449. unsigned int apic_tdcr;
  450. unsigned int apic_thmr;
  451. } apic_pm_state;
  452. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  453. {
  454. unsigned long flags;
  455. if (!apic_pm_state.active)
  456. return 0;
  457. apic_pm_state.apic_id = apic_read(APIC_ID);
  458. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  459. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  460. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  461. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  462. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  463. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  464. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  465. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  466. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  467. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  468. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  469. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  470. local_save_flags(flags);
  471. local_irq_disable();
  472. disable_local_APIC();
  473. local_irq_restore(flags);
  474. return 0;
  475. }
  476. static int lapic_resume(struct sys_device *dev)
  477. {
  478. unsigned int l, h;
  479. unsigned long flags;
  480. if (!apic_pm_state.active)
  481. return 0;
  482. local_irq_save(flags);
  483. rdmsr(MSR_IA32_APICBASE, l, h);
  484. l &= ~MSR_IA32_APICBASE_BASE;
  485. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  486. wrmsr(MSR_IA32_APICBASE, l, h);
  487. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  488. apic_write(APIC_ID, apic_pm_state.apic_id);
  489. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  490. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  491. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  492. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  493. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  494. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  495. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  496. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  497. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  498. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  499. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  500. apic_write(APIC_ESR, 0);
  501. apic_read(APIC_ESR);
  502. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  503. apic_write(APIC_ESR, 0);
  504. apic_read(APIC_ESR);
  505. local_irq_restore(flags);
  506. return 0;
  507. }
  508. static struct sysdev_class lapic_sysclass = {
  509. set_kset_name("lapic"),
  510. .resume = lapic_resume,
  511. .suspend = lapic_suspend,
  512. };
  513. static struct sys_device device_lapic = {
  514. .id = 0,
  515. .cls = &lapic_sysclass,
  516. };
  517. static void __cpuinit apic_pm_activate(void)
  518. {
  519. apic_pm_state.active = 1;
  520. }
  521. static int __init init_lapic_sysfs(void)
  522. {
  523. int error;
  524. if (!cpu_has_apic)
  525. return 0;
  526. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  527. error = sysdev_class_register(&lapic_sysclass);
  528. if (!error)
  529. error = sysdev_register(&device_lapic);
  530. return error;
  531. }
  532. device_initcall(init_lapic_sysfs);
  533. #else /* CONFIG_PM */
  534. static void apic_pm_activate(void) { }
  535. #endif /* CONFIG_PM */
  536. static int __init apic_set_verbosity(char *str)
  537. {
  538. if (strcmp("debug", str) == 0)
  539. apic_verbosity = APIC_DEBUG;
  540. else if (strcmp("verbose", str) == 0)
  541. apic_verbosity = APIC_VERBOSE;
  542. else
  543. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  544. " use apic=verbose or apic=debug", str);
  545. return 1;
  546. }
  547. __setup("apic=", apic_set_verbosity);
  548. /*
  549. * Detect and enable local APICs on non-SMP boards.
  550. * Original code written by Keir Fraser.
  551. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  552. * not correctly set up (usually the APIC timer won't work etc.)
  553. */
  554. static int __init detect_init_APIC (void)
  555. {
  556. if (!cpu_has_apic) {
  557. printk(KERN_INFO "No local APIC present\n");
  558. return -1;
  559. }
  560. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  561. boot_cpu_id = 0;
  562. return 0;
  563. }
  564. void __init init_apic_mappings(void)
  565. {
  566. unsigned long apic_phys;
  567. /*
  568. * If no local APIC can be found then set up a fake all
  569. * zeroes page to simulate the local APIC and another
  570. * one for the IO-APIC.
  571. */
  572. if (!smp_found_config && detect_init_APIC()) {
  573. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  574. apic_phys = __pa(apic_phys);
  575. } else
  576. apic_phys = mp_lapic_addr;
  577. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  578. apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
  579. /*
  580. * Fetch the APIC ID of the BSP in case we have a
  581. * default configuration (or the MP table is broken).
  582. */
  583. boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
  584. #ifdef CONFIG_X86_IO_APIC
  585. {
  586. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  587. int i;
  588. for (i = 0; i < nr_ioapics; i++) {
  589. if (smp_found_config) {
  590. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  591. } else {
  592. ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  593. ioapic_phys = __pa(ioapic_phys);
  594. }
  595. set_fixmap_nocache(idx, ioapic_phys);
  596. apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
  597. __fix_to_virt(idx), ioapic_phys);
  598. idx++;
  599. }
  600. }
  601. #endif
  602. }
  603. /*
  604. * This function sets up the local APIC timer, with a timeout of
  605. * 'clocks' APIC bus clock. During calibration we actually call
  606. * this function twice on the boot CPU, once with a bogus timeout
  607. * value, second time for real. The other (noncalibrating) CPUs
  608. * call this function only once, with the real, calibrated value.
  609. *
  610. * We do reads before writes even if unnecessary, to get around the
  611. * P5 APIC double write bug.
  612. */
  613. #define APIC_DIVISOR 16
  614. static void __setup_APIC_LVTT(unsigned int clocks)
  615. {
  616. unsigned int lvtt_value, tmp_value, ver;
  617. int cpu = smp_processor_id();
  618. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  619. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  620. if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
  621. lvtt_value |= APIC_LVT_MASKED;
  622. apic_write(APIC_LVTT, lvtt_value);
  623. /*
  624. * Divide PICLK by 16
  625. */
  626. tmp_value = apic_read(APIC_TDCR);
  627. apic_write(APIC_TDCR, (tmp_value
  628. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  629. | APIC_TDR_DIV_16);
  630. apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
  631. }
  632. static void setup_APIC_timer(unsigned int clocks)
  633. {
  634. unsigned long flags;
  635. local_irq_save(flags);
  636. /* wait for irq slice */
  637. if (vxtime.hpet_address && hpet_use_timer) {
  638. int trigger = hpet_readl(HPET_T0_CMP);
  639. while (hpet_readl(HPET_COUNTER) >= trigger)
  640. /* do nothing */ ;
  641. while (hpet_readl(HPET_COUNTER) < trigger)
  642. /* do nothing */ ;
  643. } else {
  644. int c1, c2;
  645. outb_p(0x00, 0x43);
  646. c2 = inb_p(0x40);
  647. c2 |= inb_p(0x40) << 8;
  648. do {
  649. c1 = c2;
  650. outb_p(0x00, 0x43);
  651. c2 = inb_p(0x40);
  652. c2 |= inb_p(0x40) << 8;
  653. } while (c2 - c1 < 300);
  654. }
  655. __setup_APIC_LVTT(clocks);
  656. /* Turn off PIT interrupt if we use APIC timer as main timer.
  657. Only works with the PM timer right now
  658. TBD fix it for HPET too. */
  659. if (vxtime.mode == VXTIME_PMTMR &&
  660. smp_processor_id() == boot_cpu_id &&
  661. apic_runs_main_timer == 1 &&
  662. !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
  663. stop_timer_interrupt();
  664. apic_runs_main_timer++;
  665. }
  666. local_irq_restore(flags);
  667. }
  668. /*
  669. * In this function we calibrate APIC bus clocks to the external
  670. * timer. Unfortunately we cannot use jiffies and the timer irq
  671. * to calibrate, since some later bootup code depends on getting
  672. * the first irq? Ugh.
  673. *
  674. * We want to do the calibration only once since we
  675. * want to have local timer irqs syncron. CPUs connected
  676. * by the same APIC bus have the very same bus frequency.
  677. * And we want to have irqs off anyways, no accidental
  678. * APIC irq that way.
  679. */
  680. #define TICK_COUNT 100000000
  681. static int __init calibrate_APIC_clock(void)
  682. {
  683. int apic, apic_start, tsc, tsc_start;
  684. int result;
  685. /*
  686. * Put whatever arbitrary (but long enough) timeout
  687. * value into the APIC clock, we just want to get the
  688. * counter running for calibration.
  689. */
  690. __setup_APIC_LVTT(1000000000);
  691. apic_start = apic_read(APIC_TMCCT);
  692. #ifdef CONFIG_X86_PM_TIMER
  693. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  694. pmtimer_wait(5000); /* 5ms wait */
  695. apic = apic_read(APIC_TMCCT);
  696. result = (apic_start - apic) * 1000L / 5;
  697. } else
  698. #endif
  699. {
  700. rdtscl(tsc_start);
  701. do {
  702. apic = apic_read(APIC_TMCCT);
  703. rdtscl(tsc);
  704. } while ((tsc - tsc_start) < TICK_COUNT &&
  705. (apic - apic_start) < TICK_COUNT);
  706. result = (apic_start - apic) * 1000L * cpu_khz /
  707. (tsc - tsc_start);
  708. }
  709. printk("result %d\n", result);
  710. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  711. result / 1000 / 1000, result / 1000 % 1000);
  712. return result * APIC_DIVISOR / HZ;
  713. }
  714. static unsigned int calibration_result;
  715. void __init setup_boot_APIC_clock (void)
  716. {
  717. if (disable_apic_timer) {
  718. printk(KERN_INFO "Disabling APIC timer\n");
  719. return;
  720. }
  721. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  722. using_apic_timer = 1;
  723. local_irq_disable();
  724. calibration_result = calibrate_APIC_clock();
  725. /*
  726. * Now set up the timer for real.
  727. */
  728. setup_APIC_timer(calibration_result);
  729. local_irq_enable();
  730. }
  731. void __cpuinit setup_secondary_APIC_clock(void)
  732. {
  733. local_irq_disable(); /* FIXME: Do we need this? --RR */
  734. setup_APIC_timer(calibration_result);
  735. local_irq_enable();
  736. }
  737. void disable_APIC_timer(void)
  738. {
  739. if (using_apic_timer) {
  740. unsigned long v;
  741. v = apic_read(APIC_LVTT);
  742. /*
  743. * When an illegal vector value (0-15) is written to an LVT
  744. * entry and delivery mode is Fixed, the APIC may signal an
  745. * illegal vector error, with out regard to whether the mask
  746. * bit is set or whether an interrupt is actually seen on input.
  747. *
  748. * Boot sequence might call this function when the LVTT has
  749. * '0' vector value. So make sure vector field is set to
  750. * valid value.
  751. */
  752. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  753. apic_write(APIC_LVTT, v);
  754. }
  755. }
  756. void enable_APIC_timer(void)
  757. {
  758. int cpu = smp_processor_id();
  759. if (using_apic_timer &&
  760. !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  761. unsigned long v;
  762. v = apic_read(APIC_LVTT);
  763. apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
  764. }
  765. }
  766. void switch_APIC_timer_to_ipi(void *cpumask)
  767. {
  768. cpumask_t mask = *(cpumask_t *)cpumask;
  769. int cpu = smp_processor_id();
  770. if (cpu_isset(cpu, mask) &&
  771. !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  772. disable_APIC_timer();
  773. cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
  774. }
  775. }
  776. EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
  777. void smp_send_timer_broadcast_ipi(void)
  778. {
  779. cpumask_t mask;
  780. cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
  781. if (!cpus_empty(mask)) {
  782. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  783. }
  784. }
  785. void switch_ipi_to_APIC_timer(void *cpumask)
  786. {
  787. cpumask_t mask = *(cpumask_t *)cpumask;
  788. int cpu = smp_processor_id();
  789. if (cpu_isset(cpu, mask) &&
  790. cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  791. cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
  792. enable_APIC_timer();
  793. }
  794. }
  795. EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
  796. int setup_profiling_timer(unsigned int multiplier)
  797. {
  798. return -EINVAL;
  799. }
  800. void setup_APIC_extened_lvt(unsigned char lvt_off, unsigned char vector,
  801. unsigned char msg_type, unsigned char mask)
  802. {
  803. unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
  804. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  805. apic_write(reg, v);
  806. }
  807. #undef APIC_DIVISOR
  808. /*
  809. * Local timer interrupt handler. It does both profiling and
  810. * process statistics/rescheduling.
  811. *
  812. * We do profiling in every local tick, statistics/rescheduling
  813. * happen only every 'profiling multiplier' ticks. The default
  814. * multiplier is 1 and it can be changed by writing the new multiplier
  815. * value into /proc/profile.
  816. */
  817. void smp_local_timer_interrupt(struct pt_regs *regs)
  818. {
  819. profile_tick(CPU_PROFILING, regs);
  820. #ifdef CONFIG_SMP
  821. update_process_times(user_mode(regs));
  822. #endif
  823. if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
  824. main_timer_handler(regs);
  825. /*
  826. * We take the 'long' return path, and there every subsystem
  827. * grabs the appropriate locks (kernel lock/ irq lock).
  828. *
  829. * we might want to decouple profiling from the 'long path',
  830. * and do the profiling totally in assembly.
  831. *
  832. * Currently this isn't too much of an issue (performance wise),
  833. * we can take more than 100K local irqs per second on a 100 MHz P5.
  834. */
  835. }
  836. /*
  837. * Local APIC timer interrupt. This is the most natural way for doing
  838. * local interrupts, but local timer interrupts can be emulated by
  839. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  840. *
  841. * [ if a single-CPU system runs an SMP kernel then we call the local
  842. * interrupt as well. Thus we cannot inline the local irq ... ]
  843. */
  844. void smp_apic_timer_interrupt(struct pt_regs *regs)
  845. {
  846. /*
  847. * the NMI deadlock-detector uses this.
  848. */
  849. add_pda(apic_timer_irqs, 1);
  850. /*
  851. * NOTE! We'd better ACK the irq immediately,
  852. * because timer handling can be slow.
  853. */
  854. ack_APIC_irq();
  855. /*
  856. * update_process_times() expects us to have done irq_enter().
  857. * Besides, if we don't timer interrupts ignore the global
  858. * interrupt lock, which is the WrongThing (tm) to do.
  859. */
  860. exit_idle();
  861. irq_enter();
  862. smp_local_timer_interrupt(regs);
  863. irq_exit();
  864. }
  865. /*
  866. * apic_is_clustered_box() -- Check if we can expect good TSC
  867. *
  868. * Thus far, the major user of this is IBM's Summit2 series:
  869. *
  870. * Clustered boxes may have unsynced TSC problems if they are
  871. * multi-chassis. Use available data to take a good guess.
  872. * If in doubt, go HPET.
  873. */
  874. __cpuinit int apic_is_clustered_box(void)
  875. {
  876. int i, clusters, zeros;
  877. unsigned id;
  878. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  879. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  880. for (i = 0; i < NR_CPUS; i++) {
  881. id = bios_cpu_apicid[i];
  882. if (id != BAD_APICID)
  883. __set_bit(APIC_CLUSTERID(id), clustermap);
  884. }
  885. /* Problem: Partially populated chassis may not have CPUs in some of
  886. * the APIC clusters they have been allocated. Only present CPUs have
  887. * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
  888. * clusters are allocated sequentially, count zeros only if they are
  889. * bounded by ones.
  890. */
  891. clusters = 0;
  892. zeros = 0;
  893. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  894. if (test_bit(i, clustermap)) {
  895. clusters += 1 + zeros;
  896. zeros = 0;
  897. } else
  898. ++zeros;
  899. }
  900. /*
  901. * If clusters > 2, then should be multi-chassis.
  902. * May have to revisit this when multi-core + hyperthreaded CPUs come
  903. * out, but AFAIK this will work even for them.
  904. */
  905. return (clusters > 2);
  906. }
  907. /*
  908. * This interrupt should _never_ happen with our APIC/SMP architecture
  909. */
  910. asmlinkage void smp_spurious_interrupt(void)
  911. {
  912. unsigned int v;
  913. exit_idle();
  914. irq_enter();
  915. /*
  916. * Check if this really is a spurious interrupt and ACK it
  917. * if it is a vectored one. Just in case...
  918. * Spurious interrupts should not be ACKed.
  919. */
  920. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  921. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  922. ack_APIC_irq();
  923. #if 0
  924. static unsigned long last_warning;
  925. static unsigned long skipped;
  926. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  927. if (time_before(last_warning+30*HZ,jiffies)) {
  928. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
  929. smp_processor_id(), skipped);
  930. last_warning = jiffies;
  931. skipped = 0;
  932. } else {
  933. skipped++;
  934. }
  935. #endif
  936. irq_exit();
  937. }
  938. /*
  939. * This interrupt should never happen with our APIC/SMP architecture
  940. */
  941. asmlinkage void smp_error_interrupt(void)
  942. {
  943. unsigned int v, v1;
  944. exit_idle();
  945. irq_enter();
  946. /* First tickle the hardware, only then report what went on. -- REW */
  947. v = apic_read(APIC_ESR);
  948. apic_write(APIC_ESR, 0);
  949. v1 = apic_read(APIC_ESR);
  950. ack_APIC_irq();
  951. atomic_inc(&irq_err_count);
  952. /* Here is what the APIC error bits mean:
  953. 0: Send CS error
  954. 1: Receive CS error
  955. 2: Send accept error
  956. 3: Receive accept error
  957. 4: Reserved
  958. 5: Send illegal vector
  959. 6: Received illegal vector
  960. 7: Illegal register address
  961. */
  962. printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  963. smp_processor_id(), v , v1);
  964. irq_exit();
  965. }
  966. int disable_apic;
  967. /*
  968. * This initializes the IO-APIC and APIC hardware if this is
  969. * a UP kernel.
  970. */
  971. int __init APIC_init_uniprocessor (void)
  972. {
  973. if (disable_apic) {
  974. printk(KERN_INFO "Apic disabled\n");
  975. return -1;
  976. }
  977. if (!cpu_has_apic) {
  978. disable_apic = 1;
  979. printk(KERN_INFO "Apic disabled by BIOS\n");
  980. return -1;
  981. }
  982. verify_local_APIC();
  983. connect_bsp_APIC();
  984. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  985. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
  986. setup_local_APIC();
  987. #ifdef CONFIG_X86_IO_APIC
  988. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  989. setup_IO_APIC();
  990. else
  991. nr_ioapics = 0;
  992. #endif
  993. setup_boot_APIC_clock();
  994. check_nmi_watchdog();
  995. return 0;
  996. }
  997. static __init int setup_disableapic(char *str)
  998. {
  999. disable_apic = 1;
  1000. return 1;
  1001. }
  1002. static __init int setup_nolapic(char *str)
  1003. {
  1004. disable_apic = 1;
  1005. return 1;
  1006. }
  1007. static __init int setup_noapictimer(char *str)
  1008. {
  1009. if (str[0] != ' ' && str[0] != 0)
  1010. return 0;
  1011. disable_apic_timer = 1;
  1012. return 1;
  1013. }
  1014. static __init int setup_apicmaintimer(char *str)
  1015. {
  1016. apic_runs_main_timer = 1;
  1017. nohpet = 1;
  1018. return 1;
  1019. }
  1020. __setup("apicmaintimer", setup_apicmaintimer);
  1021. static __init int setup_noapicmaintimer(char *str)
  1022. {
  1023. apic_runs_main_timer = -1;
  1024. return 1;
  1025. }
  1026. __setup("noapicmaintimer", setup_noapicmaintimer);
  1027. static __init int setup_apicpmtimer(char *s)
  1028. {
  1029. apic_calibrate_pmtmr = 1;
  1030. notsc_setup(NULL);
  1031. return setup_apicmaintimer(NULL);
  1032. }
  1033. __setup("apicpmtimer", setup_apicpmtimer);
  1034. /* dummy parsing: see setup.c */
  1035. __setup("disableapic", setup_disableapic);
  1036. __setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
  1037. __setup("noapictimer", setup_noapictimer);
  1038. /* no "lapic" flag - we only use the lapic when the BIOS tells us so. */