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/delay.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/kernel_stat.h>
  25. #include <linux/sysdev.h>
  26. #include <linux/module.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. #include <asm/idle.h>
  35. #include <asm/proto.h>
  36. #include <asm/timex.h>
  37. int apic_verbosity;
  38. int apic_runs_main_timer;
  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 = 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. void clear_local_APIC(void)
  62. {
  63. int maxlvt;
  64. unsigned int v;
  65. maxlvt = get_maxlvt();
  66. /*
  67. * Masking an LVT entry on a P6 can trigger a local APIC error
  68. * if the vector is zero. Mask LVTERR first to prevent this.
  69. */
  70. if (maxlvt >= 3) {
  71. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  72. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  73. }
  74. /*
  75. * Careful: we have to set masks only first to deassert
  76. * any level-triggered sources.
  77. */
  78. v = apic_read(APIC_LVTT);
  79. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  80. v = apic_read(APIC_LVT0);
  81. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  82. v = apic_read(APIC_LVT1);
  83. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  84. if (maxlvt >= 4) {
  85. v = apic_read(APIC_LVTPC);
  86. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  87. }
  88. /*
  89. * Clean APIC state for other OSs:
  90. */
  91. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  92. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  93. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  94. if (maxlvt >= 3)
  95. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  96. if (maxlvt >= 4)
  97. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  98. v = GET_APIC_VERSION(apic_read(APIC_LVR));
  99. apic_write(APIC_ESR, 0);
  100. apic_read(APIC_ESR);
  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(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(APIC_LVT0, value);
  149. }
  150. else {
  151. /* Disable LVT0 */
  152. apic_write(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(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(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(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;
  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. /*
  260. * Do not trust the local APIC being empty at bootup.
  261. */
  262. clear_local_APIC();
  263. /*
  264. * Enable APIC.
  265. */
  266. value = apic_read(APIC_SPIV);
  267. value &= ~APIC_VECTOR_MASK;
  268. value |= APIC_SPIV_APIC_ENABLED;
  269. value |= APIC_SPIV_FOCUS_DISABLED;
  270. value |= SPURIOUS_APIC_VECTOR;
  271. apic_write(APIC_SPIV, value);
  272. /*
  273. * Set up the virtual wire mode.
  274. */
  275. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  276. value = APIC_DM_NMI;
  277. apic_write(APIC_LVT1, value);
  278. }
  279. void __cpuinit setup_local_APIC (void)
  280. {
  281. unsigned int value, maxlvt;
  282. value = apic_read(APIC_LVR);
  283. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  284. __error_in_apic_c();
  285. /*
  286. * Double-check whether this APIC is really registered.
  287. * This is meaningless in clustered apic mode, so we skip it.
  288. */
  289. if (!apic_id_registered())
  290. BUG();
  291. /*
  292. * Intel recommends to set DFR, LDR and TPR before enabling
  293. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  294. * document number 292116). So here it goes...
  295. */
  296. init_apic_ldr();
  297. /*
  298. * Set Task Priority to 'accept all'. We never change this
  299. * later on.
  300. */
  301. value = apic_read(APIC_TASKPRI);
  302. value &= ~APIC_TPRI_MASK;
  303. apic_write(APIC_TASKPRI, value);
  304. /*
  305. * Now that we are all set up, enable the APIC
  306. */
  307. value = apic_read(APIC_SPIV);
  308. value &= ~APIC_VECTOR_MASK;
  309. /*
  310. * Enable APIC
  311. */
  312. value |= APIC_SPIV_APIC_ENABLED;
  313. /*
  314. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  315. * certain networking cards. If high frequency interrupts are
  316. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  317. * entry is masked/unmasked at a high rate as well then sooner or
  318. * later IOAPIC line gets 'stuck', no more interrupts are received
  319. * from the device. If focus CPU is disabled then the hang goes
  320. * away, oh well :-(
  321. *
  322. * [ This bug can be reproduced easily with a level-triggered
  323. * PCI Ne2000 networking cards and PII/PIII processors, dual
  324. * BX chipset. ]
  325. */
  326. /*
  327. * Actually disabling the focus CPU check just makes the hang less
  328. * frequent as it makes the interrupt distributon model be more
  329. * like LRU than MRU (the short-term load is more even across CPUs).
  330. * See also the comment in end_level_ioapic_irq(). --macro
  331. */
  332. #if 1
  333. /* Enable focus processor (bit==0) */
  334. value &= ~APIC_SPIV_FOCUS_DISABLED;
  335. #else
  336. /* Disable focus processor (bit==1) */
  337. value |= APIC_SPIV_FOCUS_DISABLED;
  338. #endif
  339. /*
  340. * Set spurious IRQ vector
  341. */
  342. value |= SPURIOUS_APIC_VECTOR;
  343. apic_write(APIC_SPIV, value);
  344. /*
  345. * Set up LVT0, LVT1:
  346. *
  347. * set up through-local-APIC on the BP's LINT0. This is not
  348. * strictly necessary in pure symmetric-IO mode, but sometimes
  349. * we delegate interrupts to the 8259A.
  350. */
  351. /*
  352. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  353. */
  354. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  355. if (!smp_processor_id() && (pic_mode || !value)) {
  356. value = APIC_DM_EXTINT;
  357. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
  358. } else {
  359. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  360. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
  361. }
  362. apic_write(APIC_LVT0, value);
  363. /*
  364. * only the BP should see the LINT1 NMI signal, obviously.
  365. */
  366. if (!smp_processor_id())
  367. value = APIC_DM_NMI;
  368. else
  369. value = APIC_DM_NMI | APIC_LVT_MASKED;
  370. apic_write(APIC_LVT1, value);
  371. {
  372. unsigned oldvalue;
  373. maxlvt = get_maxlvt();
  374. oldvalue = apic_read(APIC_ESR);
  375. value = ERROR_APIC_VECTOR; // enables sending errors
  376. apic_write(APIC_LVTERR, value);
  377. /*
  378. * spec says clear errors after enabling vector.
  379. */
  380. if (maxlvt > 3)
  381. apic_write(APIC_ESR, 0);
  382. value = apic_read(APIC_ESR);
  383. if (value != oldvalue)
  384. apic_printk(APIC_VERBOSE,
  385. "ESR value after enabling vector: %08x, after %08x\n",
  386. oldvalue, value);
  387. }
  388. nmi_watchdog_default();
  389. if (nmi_watchdog == NMI_LOCAL_APIC)
  390. setup_apic_nmi_watchdog();
  391. apic_pm_activate();
  392. }
  393. #ifdef CONFIG_PM
  394. static struct {
  395. /* 'active' is true if the local APIC was enabled by us and
  396. not the BIOS; this signifies that we are also responsible
  397. for disabling it before entering apm/acpi suspend */
  398. int active;
  399. /* r/w apic fields */
  400. unsigned int apic_id;
  401. unsigned int apic_taskpri;
  402. unsigned int apic_ldr;
  403. unsigned int apic_dfr;
  404. unsigned int apic_spiv;
  405. unsigned int apic_lvtt;
  406. unsigned int apic_lvtpc;
  407. unsigned int apic_lvt0;
  408. unsigned int apic_lvt1;
  409. unsigned int apic_lvterr;
  410. unsigned int apic_tmict;
  411. unsigned int apic_tdcr;
  412. unsigned int apic_thmr;
  413. } apic_pm_state;
  414. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  415. {
  416. unsigned long flags;
  417. if (!apic_pm_state.active)
  418. return 0;
  419. apic_pm_state.apic_id = apic_read(APIC_ID);
  420. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  421. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  422. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  423. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  424. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  425. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  426. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  427. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  428. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  429. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  430. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  431. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  432. local_save_flags(flags);
  433. local_irq_disable();
  434. disable_local_APIC();
  435. local_irq_restore(flags);
  436. return 0;
  437. }
  438. static int lapic_resume(struct sys_device *dev)
  439. {
  440. unsigned int l, h;
  441. unsigned long flags;
  442. if (!apic_pm_state.active)
  443. return 0;
  444. local_irq_save(flags);
  445. rdmsr(MSR_IA32_APICBASE, l, h);
  446. l &= ~MSR_IA32_APICBASE_BASE;
  447. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  448. wrmsr(MSR_IA32_APICBASE, l, h);
  449. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  450. apic_write(APIC_ID, apic_pm_state.apic_id);
  451. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  452. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  453. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  454. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  455. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  456. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  457. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  458. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  459. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  460. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  461. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  462. apic_write(APIC_ESR, 0);
  463. apic_read(APIC_ESR);
  464. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  465. apic_write(APIC_ESR, 0);
  466. apic_read(APIC_ESR);
  467. local_irq_restore(flags);
  468. return 0;
  469. }
  470. static struct sysdev_class lapic_sysclass = {
  471. set_kset_name("lapic"),
  472. .resume = lapic_resume,
  473. .suspend = lapic_suspend,
  474. };
  475. static struct sys_device device_lapic = {
  476. .id = 0,
  477. .cls = &lapic_sysclass,
  478. };
  479. static void __cpuinit apic_pm_activate(void)
  480. {
  481. apic_pm_state.active = 1;
  482. }
  483. static int __init init_lapic_sysfs(void)
  484. {
  485. int error;
  486. if (!cpu_has_apic)
  487. return 0;
  488. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  489. error = sysdev_class_register(&lapic_sysclass);
  490. if (!error)
  491. error = sysdev_register(&device_lapic);
  492. return error;
  493. }
  494. device_initcall(init_lapic_sysfs);
  495. #else /* CONFIG_PM */
  496. static void apic_pm_activate(void) { }
  497. #endif /* CONFIG_PM */
  498. static int __init apic_set_verbosity(char *str)
  499. {
  500. if (strcmp("debug", str) == 0)
  501. apic_verbosity = APIC_DEBUG;
  502. else if (strcmp("verbose", str) == 0)
  503. apic_verbosity = APIC_VERBOSE;
  504. else
  505. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  506. " use apic=verbose or apic=debug", str);
  507. return 0;
  508. }
  509. __setup("apic=", apic_set_verbosity);
  510. /*
  511. * Detect and enable local APICs on non-SMP boards.
  512. * Original code written by Keir Fraser.
  513. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  514. * not correctly set up (usually the APIC timer won't work etc.)
  515. */
  516. static int __init detect_init_APIC (void)
  517. {
  518. if (!cpu_has_apic) {
  519. printk(KERN_INFO "No local APIC present\n");
  520. return -1;
  521. }
  522. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  523. boot_cpu_id = 0;
  524. return 0;
  525. }
  526. void __init init_apic_mappings(void)
  527. {
  528. unsigned long apic_phys;
  529. /*
  530. * If no local APIC can be found then set up a fake all
  531. * zeroes page to simulate the local APIC and another
  532. * one for the IO-APIC.
  533. */
  534. if (!smp_found_config && detect_init_APIC()) {
  535. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  536. apic_phys = __pa(apic_phys);
  537. } else
  538. apic_phys = mp_lapic_addr;
  539. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  540. apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
  541. /*
  542. * Fetch the APIC ID of the BSP in case we have a
  543. * default configuration (or the MP table is broken).
  544. */
  545. boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
  546. #ifdef CONFIG_X86_IO_APIC
  547. {
  548. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  549. int i;
  550. for (i = 0; i < nr_ioapics; i++) {
  551. if (smp_found_config) {
  552. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  553. } else {
  554. ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  555. ioapic_phys = __pa(ioapic_phys);
  556. }
  557. set_fixmap_nocache(idx, ioapic_phys);
  558. apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
  559. __fix_to_virt(idx), ioapic_phys);
  560. idx++;
  561. }
  562. }
  563. #endif
  564. }
  565. /*
  566. * This function sets up the local APIC timer, with a timeout of
  567. * 'clocks' APIC bus clock. During calibration we actually call
  568. * this function twice on the boot CPU, once with a bogus timeout
  569. * value, second time for real. The other (noncalibrating) CPUs
  570. * call this function only once, with the real, calibrated value.
  571. *
  572. * We do reads before writes even if unnecessary, to get around the
  573. * P5 APIC double write bug.
  574. */
  575. #define APIC_DIVISOR 16
  576. static void __setup_APIC_LVTT(unsigned int clocks)
  577. {
  578. unsigned int lvtt_value, tmp_value, ver;
  579. int cpu = smp_processor_id();
  580. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  581. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  582. if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
  583. lvtt_value |= APIC_LVT_MASKED;
  584. apic_write(APIC_LVTT, lvtt_value);
  585. /*
  586. * Divide PICLK by 16
  587. */
  588. tmp_value = apic_read(APIC_TDCR);
  589. apic_write(APIC_TDCR, (tmp_value
  590. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  591. | APIC_TDR_DIV_16);
  592. apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
  593. }
  594. static void setup_APIC_timer(unsigned int clocks)
  595. {
  596. unsigned long flags;
  597. local_irq_save(flags);
  598. /* wait for irq slice */
  599. if (vxtime.hpet_address) {
  600. int trigger = hpet_readl(HPET_T0_CMP);
  601. while (hpet_readl(HPET_COUNTER) >= trigger)
  602. /* do nothing */ ;
  603. while (hpet_readl(HPET_COUNTER) < trigger)
  604. /* do nothing */ ;
  605. } else {
  606. int c1, c2;
  607. outb_p(0x00, 0x43);
  608. c2 = inb_p(0x40);
  609. c2 |= inb_p(0x40) << 8;
  610. do {
  611. c1 = c2;
  612. outb_p(0x00, 0x43);
  613. c2 = inb_p(0x40);
  614. c2 |= inb_p(0x40) << 8;
  615. } while (c2 - c1 < 300);
  616. }
  617. __setup_APIC_LVTT(clocks);
  618. /* Turn off PIT interrupt if we use APIC timer as main timer.
  619. Only works with the PM timer right now
  620. TBD fix it for HPET too. */
  621. if (vxtime.mode == VXTIME_PMTMR &&
  622. smp_processor_id() == boot_cpu_id &&
  623. apic_runs_main_timer == 1 &&
  624. !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
  625. stop_timer_interrupt();
  626. apic_runs_main_timer++;
  627. }
  628. local_irq_restore(flags);
  629. }
  630. /*
  631. * In this function we calibrate APIC bus clocks to the external
  632. * timer. Unfortunately we cannot use jiffies and the timer irq
  633. * to calibrate, since some later bootup code depends on getting
  634. * the first irq? Ugh.
  635. *
  636. * We want to do the calibration only once since we
  637. * want to have local timer irqs syncron. CPUs connected
  638. * by the same APIC bus have the very same bus frequency.
  639. * And we want to have irqs off anyways, no accidental
  640. * APIC irq that way.
  641. */
  642. #define TICK_COUNT 100000000
  643. static int __init calibrate_APIC_clock(void)
  644. {
  645. int apic, apic_start, tsc, tsc_start;
  646. int result;
  647. /*
  648. * Put whatever arbitrary (but long enough) timeout
  649. * value into the APIC clock, we just want to get the
  650. * counter running for calibration.
  651. */
  652. __setup_APIC_LVTT(1000000000);
  653. apic_start = apic_read(APIC_TMCCT);
  654. rdtscl(tsc_start);
  655. do {
  656. apic = apic_read(APIC_TMCCT);
  657. rdtscl(tsc);
  658. } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
  659. result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
  660. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  661. result / 1000 / 1000, result / 1000 % 1000);
  662. return result * APIC_DIVISOR / HZ;
  663. }
  664. static unsigned int calibration_result;
  665. void __init setup_boot_APIC_clock (void)
  666. {
  667. if (disable_apic_timer) {
  668. printk(KERN_INFO "Disabling APIC timer\n");
  669. return;
  670. }
  671. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  672. using_apic_timer = 1;
  673. local_irq_disable();
  674. calibration_result = calibrate_APIC_clock();
  675. /*
  676. * Now set up the timer for real.
  677. */
  678. setup_APIC_timer(calibration_result);
  679. local_irq_enable();
  680. }
  681. void __cpuinit setup_secondary_APIC_clock(void)
  682. {
  683. local_irq_disable(); /* FIXME: Do we need this? --RR */
  684. setup_APIC_timer(calibration_result);
  685. local_irq_enable();
  686. }
  687. void disable_APIC_timer(void)
  688. {
  689. if (using_apic_timer) {
  690. unsigned long v;
  691. v = apic_read(APIC_LVTT);
  692. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  693. }
  694. }
  695. void enable_APIC_timer(void)
  696. {
  697. int cpu = smp_processor_id();
  698. if (using_apic_timer &&
  699. !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  700. unsigned long v;
  701. v = apic_read(APIC_LVTT);
  702. apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
  703. }
  704. }
  705. void switch_APIC_timer_to_ipi(void *cpumask)
  706. {
  707. cpumask_t mask = *(cpumask_t *)cpumask;
  708. int cpu = smp_processor_id();
  709. if (cpu_isset(cpu, mask) &&
  710. !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  711. disable_APIC_timer();
  712. cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
  713. }
  714. }
  715. EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
  716. void smp_send_timer_broadcast_ipi(void)
  717. {
  718. cpumask_t mask;
  719. cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
  720. if (!cpus_empty(mask)) {
  721. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  722. }
  723. }
  724. void switch_ipi_to_APIC_timer(void *cpumask)
  725. {
  726. cpumask_t mask = *(cpumask_t *)cpumask;
  727. int cpu = smp_processor_id();
  728. if (cpu_isset(cpu, mask) &&
  729. cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  730. cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
  731. enable_APIC_timer();
  732. }
  733. }
  734. EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
  735. int setup_profiling_timer(unsigned int multiplier)
  736. {
  737. return -EINVAL;
  738. }
  739. #ifdef CONFIG_X86_MCE_AMD
  740. void setup_threshold_lvt(unsigned long lvt_off)
  741. {
  742. unsigned int v = 0;
  743. unsigned long reg = (lvt_off << 4) + 0x500;
  744. v |= THRESHOLD_APIC_VECTOR;
  745. apic_write(reg, v);
  746. }
  747. #endif /* CONFIG_X86_MCE_AMD */
  748. #undef APIC_DIVISOR
  749. /*
  750. * Local timer interrupt handler. It does both profiling and
  751. * process statistics/rescheduling.
  752. *
  753. * We do profiling in every local tick, statistics/rescheduling
  754. * happen only every 'profiling multiplier' ticks. The default
  755. * multiplier is 1 and it can be changed by writing the new multiplier
  756. * value into /proc/profile.
  757. */
  758. void smp_local_timer_interrupt(struct pt_regs *regs)
  759. {
  760. profile_tick(CPU_PROFILING, regs);
  761. #ifdef CONFIG_SMP
  762. update_process_times(user_mode(regs));
  763. #endif
  764. if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
  765. main_timer_handler(regs);
  766. /*
  767. * We take the 'long' return path, and there every subsystem
  768. * grabs the appropriate locks (kernel lock/ irq lock).
  769. *
  770. * we might want to decouple profiling from the 'long path',
  771. * and do the profiling totally in assembly.
  772. *
  773. * Currently this isn't too much of an issue (performance wise),
  774. * we can take more than 100K local irqs per second on a 100 MHz P5.
  775. */
  776. }
  777. /*
  778. * Local APIC timer interrupt. This is the most natural way for doing
  779. * local interrupts, but local timer interrupts can be emulated by
  780. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  781. *
  782. * [ if a single-CPU system runs an SMP kernel then we call the local
  783. * interrupt as well. Thus we cannot inline the local irq ... ]
  784. */
  785. void smp_apic_timer_interrupt(struct pt_regs *regs)
  786. {
  787. /*
  788. * the NMI deadlock-detector uses this.
  789. */
  790. add_pda(apic_timer_irqs, 1);
  791. /*
  792. * NOTE! We'd better ACK the irq immediately,
  793. * because timer handling can be slow.
  794. */
  795. ack_APIC_irq();
  796. /*
  797. * update_process_times() expects us to have done irq_enter().
  798. * Besides, if we don't timer interrupts ignore the global
  799. * interrupt lock, which is the WrongThing (tm) to do.
  800. */
  801. exit_idle();
  802. irq_enter();
  803. smp_local_timer_interrupt(regs);
  804. irq_exit();
  805. }
  806. /*
  807. * oem_force_hpet_timer -- force HPET mode for some boxes.
  808. *
  809. * Thus far, the major user of this is IBM's Summit2 series:
  810. *
  811. * Clustered boxes may have unsynced TSC problems if they are
  812. * multi-chassis. Use available data to take a good guess.
  813. * If in doubt, go HPET.
  814. */
  815. __cpuinit int oem_force_hpet_timer(void)
  816. {
  817. int i, clusters, zeros;
  818. unsigned id;
  819. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  820. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  821. for (i = 0; i < NR_CPUS; i++) {
  822. id = bios_cpu_apicid[i];
  823. if (id != BAD_APICID)
  824. __set_bit(APIC_CLUSTERID(id), clustermap);
  825. }
  826. /* Problem: Partially populated chassis may not have CPUs in some of
  827. * the APIC clusters they have been allocated. Only present CPUs have
  828. * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
  829. * clusters are allocated sequentially, count zeros only if they are
  830. * bounded by ones.
  831. */
  832. clusters = 0;
  833. zeros = 0;
  834. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  835. if (test_bit(i, clustermap)) {
  836. clusters += 1 + zeros;
  837. zeros = 0;
  838. } else
  839. ++zeros;
  840. }
  841. /*
  842. * If clusters > 2, then should be multi-chassis. Return 1 for HPET.
  843. * Else return 0 to use TSC.
  844. * May have to revisit this when multi-core + hyperthreaded CPUs come
  845. * out, but AFAIK this will work even for them.
  846. */
  847. return (clusters > 2);
  848. }
  849. /*
  850. * This interrupt should _never_ happen with our APIC/SMP architecture
  851. */
  852. asmlinkage void smp_spurious_interrupt(void)
  853. {
  854. unsigned int v;
  855. exit_idle();
  856. irq_enter();
  857. /*
  858. * Check if this really is a spurious interrupt and ACK it
  859. * if it is a vectored one. Just in case...
  860. * Spurious interrupts should not be ACKed.
  861. */
  862. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  863. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  864. ack_APIC_irq();
  865. #if 0
  866. static unsigned long last_warning;
  867. static unsigned long skipped;
  868. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  869. if (time_before(last_warning+30*HZ,jiffies)) {
  870. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
  871. smp_processor_id(), skipped);
  872. last_warning = jiffies;
  873. skipped = 0;
  874. } else {
  875. skipped++;
  876. }
  877. #endif
  878. irq_exit();
  879. }
  880. /*
  881. * This interrupt should never happen with our APIC/SMP architecture
  882. */
  883. asmlinkage void smp_error_interrupt(void)
  884. {
  885. unsigned int v, v1;
  886. exit_idle();
  887. irq_enter();
  888. /* First tickle the hardware, only then report what went on. -- REW */
  889. v = apic_read(APIC_ESR);
  890. apic_write(APIC_ESR, 0);
  891. v1 = apic_read(APIC_ESR);
  892. ack_APIC_irq();
  893. atomic_inc(&irq_err_count);
  894. /* Here is what the APIC error bits mean:
  895. 0: Send CS error
  896. 1: Receive CS error
  897. 2: Send accept error
  898. 3: Receive accept error
  899. 4: Reserved
  900. 5: Send illegal vector
  901. 6: Received illegal vector
  902. 7: Illegal register address
  903. */
  904. printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  905. smp_processor_id(), v , v1);
  906. irq_exit();
  907. }
  908. int disable_apic;
  909. /*
  910. * This initializes the IO-APIC and APIC hardware if this is
  911. * a UP kernel.
  912. */
  913. int __init APIC_init_uniprocessor (void)
  914. {
  915. if (disable_apic) {
  916. printk(KERN_INFO "Apic disabled\n");
  917. return -1;
  918. }
  919. if (!cpu_has_apic) {
  920. disable_apic = 1;
  921. printk(KERN_INFO "Apic disabled by BIOS\n");
  922. return -1;
  923. }
  924. verify_local_APIC();
  925. connect_bsp_APIC();
  926. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  927. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
  928. setup_local_APIC();
  929. #ifdef CONFIG_X86_IO_APIC
  930. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  931. setup_IO_APIC();
  932. else
  933. nr_ioapics = 0;
  934. #endif
  935. setup_boot_APIC_clock();
  936. check_nmi_watchdog();
  937. return 0;
  938. }
  939. static __init int setup_disableapic(char *str)
  940. {
  941. disable_apic = 1;
  942. return 0;
  943. }
  944. static __init int setup_nolapic(char *str)
  945. {
  946. disable_apic = 1;
  947. return 0;
  948. }
  949. static __init int setup_noapictimer(char *str)
  950. {
  951. if (str[0] != ' ' && str[0] != 0)
  952. return -1;
  953. disable_apic_timer = 1;
  954. return 0;
  955. }
  956. static __init int setup_apicmaintimer(char *str)
  957. {
  958. apic_runs_main_timer = 1;
  959. nohpet = 1;
  960. return 0;
  961. }
  962. __setup("apicmaintimer", setup_apicmaintimer);
  963. static __init int setup_noapicmaintimer(char *str)
  964. {
  965. apic_runs_main_timer = -1;
  966. return 0;
  967. }
  968. __setup("noapicmaintimer", setup_noapicmaintimer);
  969. /* dummy parsing: see setup.c */
  970. __setup("disableapic", setup_disableapic);
  971. __setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
  972. __setup("noapictimer", setup_noapictimer);
  973. /* no "lapic" flag - we only use the lapic when the BIOS tells us so. */