apic.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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 DEFINE_PER_CPU(int, prof_multiplier) = 1;
  38. static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
  39. static DEFINE_PER_CPU(int, prof_counter) = 1;
  40. static void apic_pm_activate(void);
  41. void enable_NMI_through_LVT0 (void * dummy)
  42. {
  43. unsigned int v, ver;
  44. ver = apic_read(APIC_LVR);
  45. ver = GET_APIC_VERSION(ver);
  46. v = APIC_DM_NMI; /* unmask and set to NMI */
  47. apic_write_around(APIC_LVT0, v);
  48. }
  49. int get_maxlvt(void)
  50. {
  51. unsigned int v, ver, maxlvt;
  52. v = apic_read(APIC_LVR);
  53. ver = GET_APIC_VERSION(v);
  54. maxlvt = GET_APIC_MAXLVT(v);
  55. return maxlvt;
  56. }
  57. void clear_local_APIC(void)
  58. {
  59. int maxlvt;
  60. unsigned int v;
  61. maxlvt = get_maxlvt();
  62. /*
  63. * Masking an LVT entry on a P6 can trigger a local APIC error
  64. * if the vector is zero. Mask LVTERR first to prevent this.
  65. */
  66. if (maxlvt >= 3) {
  67. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  68. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  69. }
  70. /*
  71. * Careful: we have to set masks only first to deassert
  72. * any level-triggered sources.
  73. */
  74. v = apic_read(APIC_LVTT);
  75. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  76. v = apic_read(APIC_LVT0);
  77. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  78. v = apic_read(APIC_LVT1);
  79. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  80. if (maxlvt >= 4) {
  81. v = apic_read(APIC_LVTPC);
  82. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  83. }
  84. /*
  85. * Clean APIC state for other OSs:
  86. */
  87. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  88. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  89. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  90. if (maxlvt >= 3)
  91. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  92. if (maxlvt >= 4)
  93. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  94. v = GET_APIC_VERSION(apic_read(APIC_LVR));
  95. apic_write(APIC_ESR, 0);
  96. apic_read(APIC_ESR);
  97. }
  98. void __init connect_bsp_APIC(void)
  99. {
  100. if (pic_mode) {
  101. /*
  102. * Do not trust the local APIC being empty at bootup.
  103. */
  104. clear_local_APIC();
  105. /*
  106. * PIC mode, enable APIC mode in the IMCR, i.e.
  107. * connect BSP's local APIC to INT and NMI lines.
  108. */
  109. apic_printk(APIC_VERBOSE, "leaving PIC mode, enabling APIC mode.\n");
  110. outb(0x70, 0x22);
  111. outb(0x01, 0x23);
  112. }
  113. }
  114. void disconnect_bsp_APIC(int virt_wire_setup)
  115. {
  116. if (pic_mode) {
  117. /*
  118. * Put the board back into PIC mode (has an effect
  119. * only on certain older boards). Note that APIC
  120. * interrupts, including IPIs, won't work beyond
  121. * this point! The only exception are INIT IPIs.
  122. */
  123. apic_printk(APIC_QUIET, "disabling APIC mode, entering PIC mode.\n");
  124. outb(0x70, 0x22);
  125. outb(0x00, 0x23);
  126. }
  127. else {
  128. /* Go back to Virtual Wire compatibility mode */
  129. unsigned long value;
  130. /* For the spurious interrupt use vector F, and enable it */
  131. value = apic_read(APIC_SPIV);
  132. value &= ~APIC_VECTOR_MASK;
  133. value |= APIC_SPIV_APIC_ENABLED;
  134. value |= 0xf;
  135. apic_write_around(APIC_SPIV, value);
  136. if (!virt_wire_setup) {
  137. /* For LVT0 make it edge triggered, active high, external and enabled */
  138. value = apic_read(APIC_LVT0);
  139. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  140. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  141. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
  142. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  143. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  144. apic_write_around(APIC_LVT0, value);
  145. }
  146. else {
  147. /* Disable LVT0 */
  148. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  149. }
  150. /* For LVT1 make it edge triggered, active high, nmi and enabled */
  151. value = apic_read(APIC_LVT1);
  152. value &= ~(
  153. APIC_MODE_MASK | APIC_SEND_PENDING |
  154. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  155. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  156. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  157. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  158. apic_write_around(APIC_LVT1, value);
  159. }
  160. }
  161. void disable_local_APIC(void)
  162. {
  163. unsigned int value;
  164. clear_local_APIC();
  165. /*
  166. * Disable APIC (implies clearing of registers
  167. * for 82489DX!).
  168. */
  169. value = apic_read(APIC_SPIV);
  170. value &= ~APIC_SPIV_APIC_ENABLED;
  171. apic_write_around(APIC_SPIV, value);
  172. }
  173. /*
  174. * This is to verify that we're looking at a real local APIC.
  175. * Check these against your board if the CPUs aren't getting
  176. * started for no apparent reason.
  177. */
  178. int __init verify_local_APIC(void)
  179. {
  180. unsigned int reg0, reg1;
  181. /*
  182. * The version register is read-only in a real APIC.
  183. */
  184. reg0 = apic_read(APIC_LVR);
  185. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  186. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  187. reg1 = apic_read(APIC_LVR);
  188. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  189. /*
  190. * The two version reads above should print the same
  191. * numbers. If the second one is different, then we
  192. * poke at a non-APIC.
  193. */
  194. if (reg1 != reg0)
  195. return 0;
  196. /*
  197. * Check if the version looks reasonably.
  198. */
  199. reg1 = GET_APIC_VERSION(reg0);
  200. if (reg1 == 0x00 || reg1 == 0xff)
  201. return 0;
  202. reg1 = get_maxlvt();
  203. if (reg1 < 0x02 || reg1 == 0xff)
  204. return 0;
  205. /*
  206. * The ID register is read/write in a real APIC.
  207. */
  208. reg0 = apic_read(APIC_ID);
  209. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  210. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  211. reg1 = apic_read(APIC_ID);
  212. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  213. apic_write(APIC_ID, reg0);
  214. if (reg1 != (reg0 ^ APIC_ID_MASK))
  215. return 0;
  216. /*
  217. * The next two are just to see if we have sane values.
  218. * They're only really relevant if we're in Virtual Wire
  219. * compatibility mode, but most boxes are anymore.
  220. */
  221. reg0 = apic_read(APIC_LVT0);
  222. apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
  223. reg1 = apic_read(APIC_LVT1);
  224. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  225. return 1;
  226. }
  227. void __init sync_Arb_IDs(void)
  228. {
  229. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  230. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  231. if (ver >= 0x14) /* P4 or higher */
  232. return;
  233. /*
  234. * Wait for idle.
  235. */
  236. apic_wait_icr_idle();
  237. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  238. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  239. | APIC_DM_INIT);
  240. }
  241. extern void __error_in_apic_c (void);
  242. /*
  243. * An initial setup of the virtual wire mode.
  244. */
  245. void __init init_bsp_APIC(void)
  246. {
  247. unsigned int value, ver;
  248. /*
  249. * Don't do the setup now if we have a SMP BIOS as the
  250. * through-I/O-APIC virtual wire mode might be active.
  251. */
  252. if (smp_found_config || !cpu_has_apic)
  253. return;
  254. value = apic_read(APIC_LVR);
  255. ver = GET_APIC_VERSION(value);
  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_around(APIC_SPIV, value);
  269. /*
  270. * Set up the virtual wire mode.
  271. */
  272. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  273. value = APIC_DM_NMI;
  274. apic_write_around(APIC_LVT1, value);
  275. }
  276. void __cpuinit setup_local_APIC (void)
  277. {
  278. unsigned int value, ver, maxlvt;
  279. value = apic_read(APIC_LVR);
  280. ver = GET_APIC_VERSION(value);
  281. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  282. __error_in_apic_c();
  283. /*
  284. * Double-check whether this APIC is really registered.
  285. * This is meaningless in clustered apic mode, so we skip it.
  286. */
  287. if (!apic_id_registered())
  288. BUG();
  289. /*
  290. * Intel recommends to set DFR, LDR and TPR before enabling
  291. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  292. * document number 292116). So here it goes...
  293. */
  294. init_apic_ldr();
  295. /*
  296. * Set Task Priority to 'accept all'. We never change this
  297. * later on.
  298. */
  299. value = apic_read(APIC_TASKPRI);
  300. value &= ~APIC_TPRI_MASK;
  301. apic_write_around(APIC_TASKPRI, value);
  302. /*
  303. * Now that we are all set up, enable the APIC
  304. */
  305. value = apic_read(APIC_SPIV);
  306. value &= ~APIC_VECTOR_MASK;
  307. /*
  308. * Enable APIC
  309. */
  310. value |= APIC_SPIV_APIC_ENABLED;
  311. /*
  312. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  313. * certain networking cards. If high frequency interrupts are
  314. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  315. * entry is masked/unmasked at a high rate as well then sooner or
  316. * later IOAPIC line gets 'stuck', no more interrupts are received
  317. * from the device. If focus CPU is disabled then the hang goes
  318. * away, oh well :-(
  319. *
  320. * [ This bug can be reproduced easily with a level-triggered
  321. * PCI Ne2000 networking cards and PII/PIII processors, dual
  322. * BX chipset. ]
  323. */
  324. /*
  325. * Actually disabling the focus CPU check just makes the hang less
  326. * frequent as it makes the interrupt distributon model be more
  327. * like LRU than MRU (the short-term load is more even across CPUs).
  328. * See also the comment in end_level_ioapic_irq(). --macro
  329. */
  330. #if 1
  331. /* Enable focus processor (bit==0) */
  332. value &= ~APIC_SPIV_FOCUS_DISABLED;
  333. #else
  334. /* Disable focus processor (bit==1) */
  335. value |= APIC_SPIV_FOCUS_DISABLED;
  336. #endif
  337. /*
  338. * Set spurious IRQ vector
  339. */
  340. value |= SPURIOUS_APIC_VECTOR;
  341. apic_write_around(APIC_SPIV, value);
  342. /*
  343. * Set up LVT0, LVT1:
  344. *
  345. * set up through-local-APIC on the BP's LINT0. This is not
  346. * strictly necessary in pure symmetric-IO mode, but sometimes
  347. * we delegate interrupts to the 8259A.
  348. */
  349. /*
  350. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  351. */
  352. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  353. if (!smp_processor_id() && (pic_mode || !value)) {
  354. value = APIC_DM_EXTINT;
  355. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
  356. } else {
  357. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  358. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
  359. }
  360. apic_write_around(APIC_LVT0, value);
  361. /*
  362. * only the BP should see the LINT1 NMI signal, obviously.
  363. */
  364. if (!smp_processor_id())
  365. value = APIC_DM_NMI;
  366. else
  367. value = APIC_DM_NMI | APIC_LVT_MASKED;
  368. apic_write_around(APIC_LVT1, value);
  369. {
  370. unsigned oldvalue;
  371. maxlvt = get_maxlvt();
  372. oldvalue = apic_read(APIC_ESR);
  373. value = ERROR_APIC_VECTOR; // enables sending errors
  374. apic_write_around(APIC_LVTERR, value);
  375. /*
  376. * spec says clear errors after enabling vector.
  377. */
  378. if (maxlvt > 3)
  379. apic_write(APIC_ESR, 0);
  380. value = apic_read(APIC_ESR);
  381. if (value != oldvalue)
  382. apic_printk(APIC_VERBOSE,
  383. "ESR value after enabling vector: %08x, after %08x\n",
  384. oldvalue, value);
  385. }
  386. nmi_watchdog_default();
  387. if (nmi_watchdog == NMI_LOCAL_APIC)
  388. setup_apic_nmi_watchdog();
  389. apic_pm_activate();
  390. }
  391. #ifdef CONFIG_PM
  392. static struct {
  393. /* 'active' is true if the local APIC was enabled by us and
  394. not the BIOS; this signifies that we are also responsible
  395. for disabling it before entering apm/acpi suspend */
  396. int active;
  397. /* r/w apic fields */
  398. unsigned int apic_id;
  399. unsigned int apic_taskpri;
  400. unsigned int apic_ldr;
  401. unsigned int apic_dfr;
  402. unsigned int apic_spiv;
  403. unsigned int apic_lvtt;
  404. unsigned int apic_lvtpc;
  405. unsigned int apic_lvt0;
  406. unsigned int apic_lvt1;
  407. unsigned int apic_lvterr;
  408. unsigned int apic_tmict;
  409. unsigned int apic_tdcr;
  410. unsigned int apic_thmr;
  411. } apic_pm_state;
  412. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  413. {
  414. unsigned long flags;
  415. if (!apic_pm_state.active)
  416. return 0;
  417. apic_pm_state.apic_id = apic_read(APIC_ID);
  418. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  419. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  420. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  421. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  422. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  423. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  424. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  425. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  426. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  427. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  428. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  429. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  430. local_save_flags(flags);
  431. local_irq_disable();
  432. disable_local_APIC();
  433. local_irq_restore(flags);
  434. return 0;
  435. }
  436. static int lapic_resume(struct sys_device *dev)
  437. {
  438. unsigned int l, h;
  439. unsigned long flags;
  440. if (!apic_pm_state.active)
  441. return 0;
  442. /* XXX: Pavel needs this for S3 resume, but can't explain why */
  443. set_fixmap_nocache(FIX_APIC_BASE, APIC_DEFAULT_PHYS_BASE);
  444. local_irq_save(flags);
  445. rdmsr(MSR_IA32_APICBASE, l, h);
  446. l &= ~MSR_IA32_APICBASE_BASE;
  447. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  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. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  580. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  581. apic_write_around(APIC_LVTT, lvtt_value);
  582. /*
  583. * Divide PICLK by 16
  584. */
  585. tmp_value = apic_read(APIC_TDCR);
  586. apic_write_around(APIC_TDCR, (tmp_value
  587. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  588. | APIC_TDR_DIV_16);
  589. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  590. }
  591. static void setup_APIC_timer(unsigned int clocks)
  592. {
  593. unsigned long flags;
  594. local_irq_save(flags);
  595. /* For some reasons this doesn't work on Simics, so fake it for now */
  596. if (!strstr(boot_cpu_data.x86_model_id, "Screwdriver")) {
  597. __setup_APIC_LVTT(clocks);
  598. return;
  599. }
  600. /* wait for irq slice */
  601. if (vxtime.hpet_address) {
  602. int trigger = hpet_readl(HPET_T0_CMP);
  603. while (hpet_readl(HPET_COUNTER) >= trigger)
  604. /* do nothing */ ;
  605. while (hpet_readl(HPET_COUNTER) < trigger)
  606. /* do nothing */ ;
  607. } else {
  608. int c1, c2;
  609. outb_p(0x00, 0x43);
  610. c2 = inb_p(0x40);
  611. c2 |= inb_p(0x40) << 8;
  612. do {
  613. c1 = c2;
  614. outb_p(0x00, 0x43);
  615. c2 = inb_p(0x40);
  616. c2 |= inb_p(0x40) << 8;
  617. } while (c2 - c1 < 300);
  618. }
  619. __setup_APIC_LVTT(clocks);
  620. local_irq_restore(flags);
  621. }
  622. /*
  623. * In this function we calibrate APIC bus clocks to the external
  624. * timer. Unfortunately we cannot use jiffies and the timer irq
  625. * to calibrate, since some later bootup code depends on getting
  626. * the first irq? Ugh.
  627. *
  628. * We want to do the calibration only once since we
  629. * want to have local timer irqs syncron. CPUs connected
  630. * by the same APIC bus have the very same bus frequency.
  631. * And we want to have irqs off anyways, no accidental
  632. * APIC irq that way.
  633. */
  634. #define TICK_COUNT 100000000
  635. static int __init calibrate_APIC_clock(void)
  636. {
  637. int apic, apic_start, tsc, tsc_start;
  638. int result;
  639. /*
  640. * Put whatever arbitrary (but long enough) timeout
  641. * value into the APIC clock, we just want to get the
  642. * counter running for calibration.
  643. */
  644. __setup_APIC_LVTT(1000000000);
  645. apic_start = apic_read(APIC_TMCCT);
  646. rdtscl(tsc_start);
  647. do {
  648. apic = apic_read(APIC_TMCCT);
  649. rdtscl(tsc);
  650. } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
  651. result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
  652. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  653. result / 1000 / 1000, result / 1000 % 1000);
  654. return result * APIC_DIVISOR / HZ;
  655. }
  656. static unsigned int calibration_result;
  657. void __init setup_boot_APIC_clock (void)
  658. {
  659. if (disable_apic_timer) {
  660. printk(KERN_INFO "Disabling APIC timer\n");
  661. return;
  662. }
  663. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  664. using_apic_timer = 1;
  665. local_irq_disable();
  666. calibration_result = calibrate_APIC_clock();
  667. /*
  668. * Now set up the timer for real.
  669. */
  670. setup_APIC_timer(calibration_result);
  671. local_irq_enable();
  672. }
  673. void __cpuinit setup_secondary_APIC_clock(void)
  674. {
  675. local_irq_disable(); /* FIXME: Do we need this? --RR */
  676. setup_APIC_timer(calibration_result);
  677. local_irq_enable();
  678. }
  679. void __cpuinit disable_APIC_timer(void)
  680. {
  681. if (using_apic_timer) {
  682. unsigned long v;
  683. v = apic_read(APIC_LVTT);
  684. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  685. }
  686. }
  687. void enable_APIC_timer(void)
  688. {
  689. if (using_apic_timer) {
  690. unsigned long v;
  691. v = apic_read(APIC_LVTT);
  692. apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
  693. }
  694. }
  695. /*
  696. * the frequency of the profiling timer can be changed
  697. * by writing a multiplier value into /proc/profile.
  698. */
  699. int setup_profiling_timer(unsigned int multiplier)
  700. {
  701. int i;
  702. /*
  703. * Sanity check. [at least 500 APIC cycles should be
  704. * between APIC interrupts as a rule of thumb, to avoid
  705. * irqs flooding us]
  706. */
  707. if ( (!multiplier) || (calibration_result/multiplier < 500))
  708. return -EINVAL;
  709. /*
  710. * Set the new multiplier for each CPU. CPUs don't start using the
  711. * new values until the next timer interrupt in which they do process
  712. * accounting. At that time they also adjust their APIC timers
  713. * accordingly.
  714. */
  715. for (i = 0; i < NR_CPUS; ++i)
  716. per_cpu(prof_multiplier, i) = multiplier;
  717. return 0;
  718. }
  719. #undef APIC_DIVISOR
  720. /*
  721. * Local timer interrupt handler. It does both profiling and
  722. * process statistics/rescheduling.
  723. *
  724. * We do profiling in every local tick, statistics/rescheduling
  725. * happen only every 'profiling multiplier' ticks. The default
  726. * multiplier is 1 and it can be changed by writing the new multiplier
  727. * value into /proc/profile.
  728. */
  729. void smp_local_timer_interrupt(struct pt_regs *regs)
  730. {
  731. int cpu = smp_processor_id();
  732. profile_tick(CPU_PROFILING, regs);
  733. if (--per_cpu(prof_counter, cpu) <= 0) {
  734. /*
  735. * The multiplier may have changed since the last time we got
  736. * to this point as a result of the user writing to
  737. * /proc/profile. In this case we need to adjust the APIC
  738. * timer accordingly.
  739. *
  740. * Interrupts are already masked off at this point.
  741. */
  742. per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
  743. if (per_cpu(prof_counter, cpu) !=
  744. per_cpu(prof_old_multiplier, cpu)) {
  745. __setup_APIC_LVTT(calibration_result/
  746. per_cpu(prof_counter, cpu));
  747. per_cpu(prof_old_multiplier, cpu) =
  748. per_cpu(prof_counter, cpu);
  749. }
  750. #ifdef CONFIG_SMP
  751. update_process_times(user_mode(regs));
  752. #endif
  753. }
  754. /*
  755. * We take the 'long' return path, and there every subsystem
  756. * grabs the appropriate locks (kernel lock/ irq lock).
  757. *
  758. * we might want to decouple profiling from the 'long path',
  759. * and do the profiling totally in assembly.
  760. *
  761. * Currently this isn't too much of an issue (performance wise),
  762. * we can take more than 100K local irqs per second on a 100 MHz P5.
  763. */
  764. }
  765. /*
  766. * Local APIC timer interrupt. This is the most natural way for doing
  767. * local interrupts, but local timer interrupts can be emulated by
  768. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  769. *
  770. * [ if a single-CPU system runs an SMP kernel then we call the local
  771. * interrupt as well. Thus we cannot inline the local irq ... ]
  772. */
  773. void smp_apic_timer_interrupt(struct pt_regs *regs)
  774. {
  775. /*
  776. * the NMI deadlock-detector uses this.
  777. */
  778. add_pda(apic_timer_irqs, 1);
  779. /*
  780. * NOTE! We'd better ACK the irq immediately,
  781. * because timer handling can be slow.
  782. */
  783. ack_APIC_irq();
  784. /*
  785. * update_process_times() expects us to have done irq_enter().
  786. * Besides, if we don't timer interrupts ignore the global
  787. * interrupt lock, which is the WrongThing (tm) to do.
  788. */
  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. irq_enter();
  843. /*
  844. * Check if this really is a spurious interrupt and ACK it
  845. * if it is a vectored one. Just in case...
  846. * Spurious interrupts should not be ACKed.
  847. */
  848. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  849. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  850. ack_APIC_irq();
  851. #if 0
  852. static unsigned long last_warning;
  853. static unsigned long skipped;
  854. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  855. if (time_before(last_warning+30*HZ,jiffies)) {
  856. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
  857. smp_processor_id(), skipped);
  858. last_warning = jiffies;
  859. skipped = 0;
  860. } else {
  861. skipped++;
  862. }
  863. #endif
  864. irq_exit();
  865. }
  866. /*
  867. * This interrupt should never happen with our APIC/SMP architecture
  868. */
  869. asmlinkage void smp_error_interrupt(void)
  870. {
  871. unsigned int v, v1;
  872. irq_enter();
  873. /* First tickle the hardware, only then report what went on. -- REW */
  874. v = apic_read(APIC_ESR);
  875. apic_write(APIC_ESR, 0);
  876. v1 = apic_read(APIC_ESR);
  877. ack_APIC_irq();
  878. atomic_inc(&irq_err_count);
  879. /* Here is what the APIC error bits mean:
  880. 0: Send CS error
  881. 1: Receive CS error
  882. 2: Send accept error
  883. 3: Receive accept error
  884. 4: Reserved
  885. 5: Send illegal vector
  886. 6: Received illegal vector
  887. 7: Illegal register address
  888. */
  889. printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  890. smp_processor_id(), v , v1);
  891. irq_exit();
  892. }
  893. int disable_apic;
  894. /*
  895. * This initializes the IO-APIC and APIC hardware if this is
  896. * a UP kernel.
  897. */
  898. int __init APIC_init_uniprocessor (void)
  899. {
  900. if (disable_apic) {
  901. printk(KERN_INFO "Apic disabled\n");
  902. return -1;
  903. }
  904. if (!cpu_has_apic) {
  905. disable_apic = 1;
  906. printk(KERN_INFO "Apic disabled by BIOS\n");
  907. return -1;
  908. }
  909. verify_local_APIC();
  910. connect_bsp_APIC();
  911. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  912. apic_write_around(APIC_ID, boot_cpu_id);
  913. setup_local_APIC();
  914. #ifdef CONFIG_X86_IO_APIC
  915. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  916. setup_IO_APIC();
  917. else
  918. nr_ioapics = 0;
  919. #endif
  920. setup_boot_APIC_clock();
  921. check_nmi_watchdog();
  922. return 0;
  923. }
  924. static __init int setup_disableapic(char *str)
  925. {
  926. disable_apic = 1;
  927. return 0;
  928. }
  929. static __init int setup_nolapic(char *str)
  930. {
  931. disable_apic = 1;
  932. return 0;
  933. }
  934. static __init int setup_noapictimer(char *str)
  935. {
  936. disable_apic_timer = 1;
  937. return 0;
  938. }
  939. /* dummy parsing: see setup.c */
  940. __setup("disableapic", setup_disableapic);
  941. __setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
  942. __setup("noapictimer", setup_noapictimer);
  943. /* no "lapic" flag - we only use the lapic when the BIOS tells us so. */