apic.c 26 KB

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