apic.c 27 KB

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