apic.c 28 KB

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