apic.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Local APIC handling, local APIC timers
  3. *
  4. * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Fixes
  7. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  8. * thanks to Eric Gilmore
  9. * and Rolf G. Tews
  10. * for testing these extensively.
  11. * Maciej W. Rozycki : Various updates and fixes.
  12. * Mikael Pettersson : Power Management for UP-APIC.
  13. * Pavel Machek and
  14. * Mikael Pettersson : PM converted to driver model.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/init.h>
  18. #include <linux/mm.h>
  19. #include <linux/irq.h>
  20. #include <linux/delay.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/mc146818rtc.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/sysdev.h>
  27. #include <asm/atomic.h>
  28. #include <asm/smp.h>
  29. #include <asm/mtrr.h>
  30. #include <asm/mpspec.h>
  31. #include <asm/desc.h>
  32. #include <asm/arch_hooks.h>
  33. #include <asm/hpet.h>
  34. #include <mach_apic.h>
  35. #include "io_ports.h"
  36. /*
  37. * Debug level
  38. */
  39. int apic_verbosity;
  40. static void apic_pm_activate(void);
  41. /*
  42. * 'what should we do if we get a hw irq event on an illegal vector'.
  43. * each architecture has to answer this themselves.
  44. */
  45. void ack_bad_irq(unsigned int irq)
  46. {
  47. printk("unexpected IRQ trap at vector %02x\n", irq);
  48. /*
  49. * Currently unexpected vectors happen only on SMP and APIC.
  50. * We _must_ ack these because every local APIC has only N
  51. * irq slots per priority level, and a 'hanging, unacked' IRQ
  52. * holds up an irq slot - in excessive cases (when multiple
  53. * unexpected vectors occur) that might lock up the APIC
  54. * completely.
  55. */
  56. ack_APIC_irq();
  57. }
  58. void __init apic_intr_init(void)
  59. {
  60. #ifdef CONFIG_SMP
  61. smp_intr_init();
  62. #endif
  63. /* self generated IPI for local APIC timer */
  64. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  65. /* IPI vectors for APIC spurious and error interrupts */
  66. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  67. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  68. /* thermal monitor LVT interrupt */
  69. #ifdef CONFIG_X86_MCE_P4THERMAL
  70. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  71. #endif
  72. }
  73. /* Using APIC to generate smp_local_timer_interrupt? */
  74. int using_apic_timer = 0;
  75. static DEFINE_PER_CPU(int, prof_multiplier) = 1;
  76. static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
  77. static DEFINE_PER_CPU(int, prof_counter) = 1;
  78. static int enabled_via_apicbase;
  79. void enable_NMI_through_LVT0 (void * dummy)
  80. {
  81. unsigned int v, ver;
  82. ver = apic_read(APIC_LVR);
  83. ver = GET_APIC_VERSION(ver);
  84. v = APIC_DM_NMI; /* unmask and set to NMI */
  85. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  86. v |= APIC_LVT_LEVEL_TRIGGER;
  87. apic_write_around(APIC_LVT0, v);
  88. }
  89. int get_physical_broadcast(void)
  90. {
  91. unsigned int lvr, version;
  92. lvr = apic_read(APIC_LVR);
  93. version = GET_APIC_VERSION(lvr);
  94. if (!APIC_INTEGRATED(version) || version >= 0x14)
  95. return 0xff;
  96. else
  97. return 0xf;
  98. }
  99. int get_maxlvt(void)
  100. {
  101. unsigned int v, ver, maxlvt;
  102. v = apic_read(APIC_LVR);
  103. ver = GET_APIC_VERSION(v);
  104. /* 82489DXs do not report # of LVT entries. */
  105. maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
  106. return maxlvt;
  107. }
  108. void clear_local_APIC(void)
  109. {
  110. int maxlvt;
  111. unsigned long v;
  112. maxlvt = get_maxlvt();
  113. /*
  114. * Masking an LVT entry on a P6 can trigger a local APIC error
  115. * if the vector is zero. Mask LVTERR first to prevent this.
  116. */
  117. if (maxlvt >= 3) {
  118. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  119. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  120. }
  121. /*
  122. * Careful: we have to set masks only first to deassert
  123. * any level-triggered sources.
  124. */
  125. v = apic_read(APIC_LVTT);
  126. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  127. v = apic_read(APIC_LVT0);
  128. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  129. v = apic_read(APIC_LVT1);
  130. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  131. if (maxlvt >= 4) {
  132. v = apic_read(APIC_LVTPC);
  133. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  134. }
  135. /* lets not touch this if we didn't frob it */
  136. #ifdef CONFIG_X86_MCE_P4THERMAL
  137. if (maxlvt >= 5) {
  138. v = apic_read(APIC_LVTTHMR);
  139. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  140. }
  141. #endif
  142. /*
  143. * Clean APIC state for other OSs:
  144. */
  145. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  146. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  147. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  148. if (maxlvt >= 3)
  149. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  150. if (maxlvt >= 4)
  151. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  152. #ifdef CONFIG_X86_MCE_P4THERMAL
  153. if (maxlvt >= 5)
  154. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  155. #endif
  156. v = GET_APIC_VERSION(apic_read(APIC_LVR));
  157. if (APIC_INTEGRATED(v)) { /* !82489DX */
  158. if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
  159. apic_write(APIC_ESR, 0);
  160. apic_read(APIC_ESR);
  161. }
  162. }
  163. void __init connect_bsp_APIC(void)
  164. {
  165. if (pic_mode) {
  166. /*
  167. * Do not trust the local APIC being empty at bootup.
  168. */
  169. clear_local_APIC();
  170. /*
  171. * PIC mode, enable APIC mode in the IMCR, i.e.
  172. * connect BSP's local APIC to INT and NMI lines.
  173. */
  174. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  175. "enabling APIC mode.\n");
  176. outb(0x70, 0x22);
  177. outb(0x01, 0x23);
  178. }
  179. enable_apic_mode();
  180. }
  181. void disconnect_bsp_APIC(void)
  182. {
  183. if (pic_mode) {
  184. /*
  185. * Put the board back into PIC mode (has an effect
  186. * only on certain older boards). Note that APIC
  187. * interrupts, including IPIs, won't work beyond
  188. * this point! The only exception are INIT IPIs.
  189. */
  190. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  191. "entering PIC mode.\n");
  192. outb(0x70, 0x22);
  193. outb(0x00, 0x23);
  194. }
  195. }
  196. void disable_local_APIC(void)
  197. {
  198. unsigned long value;
  199. clear_local_APIC();
  200. /*
  201. * Disable APIC (implies clearing of registers
  202. * for 82489DX!).
  203. */
  204. value = apic_read(APIC_SPIV);
  205. value &= ~APIC_SPIV_APIC_ENABLED;
  206. apic_write_around(APIC_SPIV, value);
  207. if (enabled_via_apicbase) {
  208. unsigned int l, h;
  209. rdmsr(MSR_IA32_APICBASE, l, h);
  210. l &= ~MSR_IA32_APICBASE_ENABLE;
  211. wrmsr(MSR_IA32_APICBASE, l, h);
  212. }
  213. }
  214. /*
  215. * This is to verify that we're looking at a real local APIC.
  216. * Check these against your board if the CPUs aren't getting
  217. * started for no apparent reason.
  218. */
  219. int __init verify_local_APIC(void)
  220. {
  221. unsigned int reg0, reg1;
  222. /*
  223. * The version register is read-only in a real APIC.
  224. */
  225. reg0 = apic_read(APIC_LVR);
  226. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  227. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  228. reg1 = apic_read(APIC_LVR);
  229. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  230. /*
  231. * The two version reads above should print the same
  232. * numbers. If the second one is different, then we
  233. * poke at a non-APIC.
  234. */
  235. if (reg1 != reg0)
  236. return 0;
  237. /*
  238. * Check if the version looks reasonably.
  239. */
  240. reg1 = GET_APIC_VERSION(reg0);
  241. if (reg1 == 0x00 || reg1 == 0xff)
  242. return 0;
  243. reg1 = get_maxlvt();
  244. if (reg1 < 0x02 || reg1 == 0xff)
  245. return 0;
  246. /*
  247. * The ID register is read/write in a real APIC.
  248. */
  249. reg0 = apic_read(APIC_ID);
  250. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  251. /*
  252. * The next two are just to see if we have sane values.
  253. * They're only really relevant if we're in Virtual Wire
  254. * compatibility mode, but most boxes are anymore.
  255. */
  256. reg0 = apic_read(APIC_LVT0);
  257. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  258. reg1 = apic_read(APIC_LVT1);
  259. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  260. return 1;
  261. }
  262. void __init sync_Arb_IDs(void)
  263. {
  264. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  265. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  266. if (ver >= 0x14) /* P4 or higher */
  267. return;
  268. /*
  269. * Wait for idle.
  270. */
  271. apic_wait_icr_idle();
  272. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  273. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  274. | APIC_DM_INIT);
  275. }
  276. extern void __error_in_apic_c (void);
  277. /*
  278. * An initial setup of the virtual wire mode.
  279. */
  280. void __init init_bsp_APIC(void)
  281. {
  282. unsigned long value, ver;
  283. /*
  284. * Don't do the setup now if we have a SMP BIOS as the
  285. * through-I/O-APIC virtual wire mode might be active.
  286. */
  287. if (smp_found_config || !cpu_has_apic)
  288. return;
  289. value = apic_read(APIC_LVR);
  290. ver = GET_APIC_VERSION(value);
  291. /*
  292. * Do not trust the local APIC being empty at bootup.
  293. */
  294. clear_local_APIC();
  295. /*
  296. * Enable APIC.
  297. */
  298. value = apic_read(APIC_SPIV);
  299. value &= ~APIC_VECTOR_MASK;
  300. value |= APIC_SPIV_APIC_ENABLED;
  301. /* This bit is reserved on P4/Xeon and should be cleared */
  302. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
  303. value &= ~APIC_SPIV_FOCUS_DISABLED;
  304. else
  305. value |= APIC_SPIV_FOCUS_DISABLED;
  306. value |= SPURIOUS_APIC_VECTOR;
  307. apic_write_around(APIC_SPIV, value);
  308. /*
  309. * Set up the virtual wire mode.
  310. */
  311. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  312. value = APIC_DM_NMI;
  313. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  314. value |= APIC_LVT_LEVEL_TRIGGER;
  315. apic_write_around(APIC_LVT1, value);
  316. }
  317. void __init setup_local_APIC (void)
  318. {
  319. unsigned long oldvalue, value, ver, maxlvt;
  320. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  321. if (esr_disable) {
  322. apic_write(APIC_ESR, 0);
  323. apic_write(APIC_ESR, 0);
  324. apic_write(APIC_ESR, 0);
  325. apic_write(APIC_ESR, 0);
  326. }
  327. value = apic_read(APIC_LVR);
  328. ver = GET_APIC_VERSION(value);
  329. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  330. __error_in_apic_c();
  331. /*
  332. * Double-check whether this APIC is really registered.
  333. */
  334. if (!apic_id_registered())
  335. BUG();
  336. /*
  337. * Intel recommends to set DFR, LDR and TPR before enabling
  338. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  339. * document number 292116). So here it goes...
  340. */
  341. init_apic_ldr();
  342. /*
  343. * Set Task Priority to 'accept all'. We never change this
  344. * later on.
  345. */
  346. value = apic_read(APIC_TASKPRI);
  347. value &= ~APIC_TPRI_MASK;
  348. apic_write_around(APIC_TASKPRI, value);
  349. /*
  350. * Now that we are all set up, enable the APIC
  351. */
  352. value = apic_read(APIC_SPIV);
  353. value &= ~APIC_VECTOR_MASK;
  354. /*
  355. * Enable APIC
  356. */
  357. value |= APIC_SPIV_APIC_ENABLED;
  358. /*
  359. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  360. * certain networking cards. If high frequency interrupts are
  361. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  362. * entry is masked/unmasked at a high rate as well then sooner or
  363. * later IOAPIC line gets 'stuck', no more interrupts are received
  364. * from the device. If focus CPU is disabled then the hang goes
  365. * away, oh well :-(
  366. *
  367. * [ This bug can be reproduced easily with a level-triggered
  368. * PCI Ne2000 networking cards and PII/PIII processors, dual
  369. * BX chipset. ]
  370. */
  371. /*
  372. * Actually disabling the focus CPU check just makes the hang less
  373. * frequent as it makes the interrupt distributon model be more
  374. * like LRU than MRU (the short-term load is more even across CPUs).
  375. * See also the comment in end_level_ioapic_irq(). --macro
  376. */
  377. #if 1
  378. /* Enable focus processor (bit==0) */
  379. value &= ~APIC_SPIV_FOCUS_DISABLED;
  380. #else
  381. /* Disable focus processor (bit==1) */
  382. value |= APIC_SPIV_FOCUS_DISABLED;
  383. #endif
  384. /*
  385. * Set spurious IRQ vector
  386. */
  387. value |= SPURIOUS_APIC_VECTOR;
  388. apic_write_around(APIC_SPIV, value);
  389. /*
  390. * Set up LVT0, LVT1:
  391. *
  392. * set up through-local-APIC on the BP's LINT0. This is not
  393. * strictly necessery in pure symmetric-IO mode, but sometimes
  394. * we delegate interrupts to the 8259A.
  395. */
  396. /*
  397. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  398. */
  399. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  400. if (!smp_processor_id() && (pic_mode || !value)) {
  401. value = APIC_DM_EXTINT;
  402. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  403. smp_processor_id());
  404. } else {
  405. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  406. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  407. smp_processor_id());
  408. }
  409. apic_write_around(APIC_LVT0, value);
  410. /*
  411. * only the BP should see the LINT1 NMI signal, obviously.
  412. */
  413. if (!smp_processor_id())
  414. value = APIC_DM_NMI;
  415. else
  416. value = APIC_DM_NMI | APIC_LVT_MASKED;
  417. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  418. value |= APIC_LVT_LEVEL_TRIGGER;
  419. apic_write_around(APIC_LVT1, value);
  420. if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
  421. maxlvt = get_maxlvt();
  422. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  423. apic_write(APIC_ESR, 0);
  424. oldvalue = apic_read(APIC_ESR);
  425. value = ERROR_APIC_VECTOR; // enables sending errors
  426. apic_write_around(APIC_LVTERR, value);
  427. /*
  428. * spec says clear errors after enabling vector.
  429. */
  430. if (maxlvt > 3)
  431. apic_write(APIC_ESR, 0);
  432. value = apic_read(APIC_ESR);
  433. if (value != oldvalue)
  434. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  435. "vector: 0x%08lx after: 0x%08lx\n",
  436. oldvalue, value);
  437. } else {
  438. if (esr_disable)
  439. /*
  440. * Something untraceble is creating bad interrupts on
  441. * secondary quads ... for the moment, just leave the
  442. * ESR disabled - we can't do anything useful with the
  443. * errors anyway - mbligh
  444. */
  445. printk("Leaving ESR disabled.\n");
  446. else
  447. printk("No ESR for 82489DX.\n");
  448. }
  449. if (nmi_watchdog == NMI_LOCAL_APIC)
  450. setup_apic_nmi_watchdog();
  451. apic_pm_activate();
  452. }
  453. /*
  454. * If Linux enabled the LAPIC against the BIOS default
  455. * disable it down before re-entering the BIOS on shutdown.
  456. * Otherwise the BIOS may get confused and not power-off.
  457. */
  458. void lapic_shutdown(void)
  459. {
  460. if (!cpu_has_apic || !enabled_via_apicbase)
  461. return;
  462. local_irq_disable();
  463. disable_local_APIC();
  464. local_irq_enable();
  465. }
  466. #ifdef CONFIG_PM
  467. static struct {
  468. int active;
  469. /* r/w apic fields */
  470. unsigned int apic_id;
  471. unsigned int apic_taskpri;
  472. unsigned int apic_ldr;
  473. unsigned int apic_dfr;
  474. unsigned int apic_spiv;
  475. unsigned int apic_lvtt;
  476. unsigned int apic_lvtpc;
  477. unsigned int apic_lvt0;
  478. unsigned int apic_lvt1;
  479. unsigned int apic_lvterr;
  480. unsigned int apic_tmict;
  481. unsigned int apic_tdcr;
  482. unsigned int apic_thmr;
  483. } apic_pm_state;
  484. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  485. {
  486. unsigned long flags;
  487. if (!apic_pm_state.active)
  488. return 0;
  489. apic_pm_state.apic_id = apic_read(APIC_ID);
  490. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  491. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  492. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  493. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  494. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  495. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  496. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  497. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  498. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  499. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  500. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  501. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  502. local_irq_save(flags);
  503. disable_local_APIC();
  504. local_irq_restore(flags);
  505. return 0;
  506. }
  507. static int lapic_resume(struct sys_device *dev)
  508. {
  509. unsigned int l, h;
  510. unsigned long flags;
  511. if (!apic_pm_state.active)
  512. return 0;
  513. local_irq_save(flags);
  514. /*
  515. * Make sure the APICBASE points to the right address
  516. *
  517. * FIXME! This will be wrong if we ever support suspend on
  518. * SMP! We'll need to do this as part of the CPU restore!
  519. */
  520. rdmsr(MSR_IA32_APICBASE, l, h);
  521. l &= ~MSR_IA32_APICBASE_BASE;
  522. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  523. wrmsr(MSR_IA32_APICBASE, l, h);
  524. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  525. apic_write(APIC_ID, apic_pm_state.apic_id);
  526. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  527. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  528. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  529. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  530. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  531. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  532. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  533. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  534. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  535. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  536. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  537. apic_write(APIC_ESR, 0);
  538. apic_read(APIC_ESR);
  539. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  540. apic_write(APIC_ESR, 0);
  541. apic_read(APIC_ESR);
  542. local_irq_restore(flags);
  543. return 0;
  544. }
  545. /*
  546. * This device has no shutdown method - fully functioning local APICs
  547. * are needed on every CPU up until machine_halt/restart/poweroff.
  548. */
  549. static struct sysdev_class lapic_sysclass = {
  550. set_kset_name("lapic"),
  551. .resume = lapic_resume,
  552. .suspend = lapic_suspend,
  553. };
  554. static struct sys_device device_lapic = {
  555. .id = 0,
  556. .cls = &lapic_sysclass,
  557. };
  558. static void __init apic_pm_activate(void)
  559. {
  560. apic_pm_state.active = 1;
  561. }
  562. static int __init init_lapic_sysfs(void)
  563. {
  564. int error;
  565. if (!cpu_has_apic)
  566. return 0;
  567. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  568. error = sysdev_class_register(&lapic_sysclass);
  569. if (!error)
  570. error = sysdev_register(&device_lapic);
  571. return error;
  572. }
  573. device_initcall(init_lapic_sysfs);
  574. #else /* CONFIG_PM */
  575. static void apic_pm_activate(void) { }
  576. #endif /* CONFIG_PM */
  577. /*
  578. * Detect and enable local APICs on non-SMP boards.
  579. * Original code written by Keir Fraser.
  580. */
  581. /*
  582. * Knob to control our willingness to enable the local APIC.
  583. */
  584. int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
  585. static int __init lapic_disable(char *str)
  586. {
  587. enable_local_apic = -1;
  588. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  589. return 0;
  590. }
  591. __setup("nolapic", lapic_disable);
  592. static int __init lapic_enable(char *str)
  593. {
  594. enable_local_apic = 1;
  595. return 0;
  596. }
  597. __setup("lapic", lapic_enable);
  598. static int __init apic_set_verbosity(char *str)
  599. {
  600. if (strcmp("debug", str) == 0)
  601. apic_verbosity = APIC_DEBUG;
  602. else if (strcmp("verbose", str) == 0)
  603. apic_verbosity = APIC_VERBOSE;
  604. else
  605. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  606. " use apic=verbose or apic=debug", str);
  607. return 0;
  608. }
  609. __setup("apic=", apic_set_verbosity);
  610. static int __init detect_init_APIC (void)
  611. {
  612. u32 h, l, features;
  613. extern void get_cpu_vendor(struct cpuinfo_x86*);
  614. /* Disabled by kernel option? */
  615. if (enable_local_apic < 0)
  616. return -1;
  617. /* Workaround for us being called before identify_cpu(). */
  618. get_cpu_vendor(&boot_cpu_data);
  619. switch (boot_cpu_data.x86_vendor) {
  620. case X86_VENDOR_AMD:
  621. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  622. (boot_cpu_data.x86 == 15))
  623. break;
  624. goto no_apic;
  625. case X86_VENDOR_INTEL:
  626. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  627. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  628. break;
  629. goto no_apic;
  630. default:
  631. goto no_apic;
  632. }
  633. if (!cpu_has_apic) {
  634. /*
  635. * Over-ride BIOS and try to enable the local
  636. * APIC only if "lapic" specified.
  637. */
  638. if (enable_local_apic <= 0) {
  639. printk("Local APIC disabled by BIOS -- "
  640. "you can enable it with \"lapic\"\n");
  641. return -1;
  642. }
  643. /*
  644. * Some BIOSes disable the local APIC in the
  645. * APIC_BASE MSR. This can only be done in
  646. * software for Intel P6 or later and AMD K7
  647. * (Model > 1) or later.
  648. */
  649. rdmsr(MSR_IA32_APICBASE, l, h);
  650. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  651. printk("Local APIC disabled by BIOS -- reenabling.\n");
  652. l &= ~MSR_IA32_APICBASE_BASE;
  653. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  654. wrmsr(MSR_IA32_APICBASE, l, h);
  655. enabled_via_apicbase = 1;
  656. }
  657. }
  658. /*
  659. * The APIC feature bit should now be enabled
  660. * in `cpuid'
  661. */
  662. features = cpuid_edx(1);
  663. if (!(features & (1 << X86_FEATURE_APIC))) {
  664. printk("Could not enable APIC!\n");
  665. return -1;
  666. }
  667. set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  668. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  669. /* The BIOS may have set up the APIC at some other address */
  670. rdmsr(MSR_IA32_APICBASE, l, h);
  671. if (l & MSR_IA32_APICBASE_ENABLE)
  672. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  673. if (nmi_watchdog != NMI_NONE)
  674. nmi_watchdog = NMI_LOCAL_APIC;
  675. printk("Found and enabled local APIC!\n");
  676. apic_pm_activate();
  677. return 0;
  678. no_apic:
  679. printk("No local APIC present or hardware disabled\n");
  680. return -1;
  681. }
  682. void __init init_apic_mappings(void)
  683. {
  684. unsigned long apic_phys;
  685. /*
  686. * If no local APIC can be found then set up a fake all
  687. * zeroes page to simulate the local APIC and another
  688. * one for the IO-APIC.
  689. */
  690. if (!smp_found_config && detect_init_APIC()) {
  691. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  692. apic_phys = __pa(apic_phys);
  693. } else
  694. apic_phys = mp_lapic_addr;
  695. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  696. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  697. apic_phys);
  698. /*
  699. * Fetch the APIC ID of the BSP in case we have a
  700. * default configuration (or the MP table is broken).
  701. */
  702. if (boot_cpu_physical_apicid == -1U)
  703. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  704. #ifdef CONFIG_X86_IO_APIC
  705. {
  706. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  707. int i;
  708. for (i = 0; i < nr_ioapics; i++) {
  709. if (smp_found_config) {
  710. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  711. if (!ioapic_phys) {
  712. printk(KERN_ERR
  713. "WARNING: bogus zero IO-APIC "
  714. "address found in MPTABLE, "
  715. "disabling IO/APIC support!\n");
  716. smp_found_config = 0;
  717. skip_ioapic_setup = 1;
  718. goto fake_ioapic_page;
  719. }
  720. } else {
  721. fake_ioapic_page:
  722. ioapic_phys = (unsigned long)
  723. alloc_bootmem_pages(PAGE_SIZE);
  724. ioapic_phys = __pa(ioapic_phys);
  725. }
  726. set_fixmap_nocache(idx, ioapic_phys);
  727. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  728. __fix_to_virt(idx), ioapic_phys);
  729. idx++;
  730. }
  731. }
  732. #endif
  733. }
  734. /*
  735. * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
  736. * per second. We assume that the caller has already set up the local
  737. * APIC.
  738. *
  739. * The APIC timer is not exactly sync with the external timer chip, it
  740. * closely follows bus clocks.
  741. */
  742. /*
  743. * The timer chip is already set up at HZ interrupts per second here,
  744. * but we do not accept timer interrupts yet. We only allow the BP
  745. * to calibrate.
  746. */
  747. static unsigned int __init get_8254_timer_count(void)
  748. {
  749. extern spinlock_t i8253_lock;
  750. unsigned long flags;
  751. unsigned int count;
  752. spin_lock_irqsave(&i8253_lock, flags);
  753. outb_p(0x00, PIT_MODE);
  754. count = inb_p(PIT_CH0);
  755. count |= inb_p(PIT_CH0) << 8;
  756. spin_unlock_irqrestore(&i8253_lock, flags);
  757. return count;
  758. }
  759. /* next tick in 8254 can be caught by catching timer wraparound */
  760. static void __init wait_8254_wraparound(void)
  761. {
  762. unsigned int curr_count, prev_count;
  763. curr_count = get_8254_timer_count();
  764. do {
  765. prev_count = curr_count;
  766. curr_count = get_8254_timer_count();
  767. /* workaround for broken Mercury/Neptune */
  768. if (prev_count >= curr_count + 0x100)
  769. curr_count = get_8254_timer_count();
  770. } while (prev_count >= curr_count);
  771. }
  772. /*
  773. * Default initialization for 8254 timers. If we use other timers like HPET,
  774. * we override this later
  775. */
  776. void (*wait_timer_tick)(void) __initdata = wait_8254_wraparound;
  777. /*
  778. * This function sets up the local APIC timer, with a timeout of
  779. * 'clocks' APIC bus clock. During calibration we actually call
  780. * this function twice on the boot CPU, once with a bogus timeout
  781. * value, second time for real. The other (noncalibrating) CPUs
  782. * call this function only once, with the real, calibrated value.
  783. *
  784. * We do reads before writes even if unnecessary, to get around the
  785. * P5 APIC double write bug.
  786. */
  787. #define APIC_DIVISOR 16
  788. static void __setup_APIC_LVTT(unsigned int clocks)
  789. {
  790. unsigned int lvtt_value, tmp_value, ver;
  791. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  792. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  793. if (!APIC_INTEGRATED(ver))
  794. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  795. apic_write_around(APIC_LVTT, lvtt_value);
  796. /*
  797. * Divide PICLK by 16
  798. */
  799. tmp_value = apic_read(APIC_TDCR);
  800. apic_write_around(APIC_TDCR, (tmp_value
  801. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  802. | APIC_TDR_DIV_16);
  803. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  804. }
  805. static void __init setup_APIC_timer(unsigned int clocks)
  806. {
  807. unsigned long flags;
  808. local_irq_save(flags);
  809. /*
  810. * Wait for IRQ0's slice:
  811. */
  812. wait_timer_tick();
  813. __setup_APIC_LVTT(clocks);
  814. local_irq_restore(flags);
  815. }
  816. /*
  817. * In this function we calibrate APIC bus clocks to the external
  818. * timer. Unfortunately we cannot use jiffies and the timer irq
  819. * to calibrate, since some later bootup code depends on getting
  820. * the first irq? Ugh.
  821. *
  822. * We want to do the calibration only once since we
  823. * want to have local timer irqs syncron. CPUs connected
  824. * by the same APIC bus have the very same bus frequency.
  825. * And we want to have irqs off anyways, no accidental
  826. * APIC irq that way.
  827. */
  828. static int __init calibrate_APIC_clock(void)
  829. {
  830. unsigned long long t1 = 0, t2 = 0;
  831. long tt1, tt2;
  832. long result;
  833. int i;
  834. const int LOOPS = HZ/10;
  835. apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
  836. /*
  837. * Put whatever arbitrary (but long enough) timeout
  838. * value into the APIC clock, we just want to get the
  839. * counter running for calibration.
  840. */
  841. __setup_APIC_LVTT(1000000000);
  842. /*
  843. * The timer chip counts down to zero. Let's wait
  844. * for a wraparound to start exact measurement:
  845. * (the current tick might have been already half done)
  846. */
  847. wait_timer_tick();
  848. /*
  849. * We wrapped around just now. Let's start:
  850. */
  851. if (cpu_has_tsc)
  852. rdtscll(t1);
  853. tt1 = apic_read(APIC_TMCCT);
  854. /*
  855. * Let's wait LOOPS wraprounds:
  856. */
  857. for (i = 0; i < LOOPS; i++)
  858. wait_timer_tick();
  859. tt2 = apic_read(APIC_TMCCT);
  860. if (cpu_has_tsc)
  861. rdtscll(t2);
  862. /*
  863. * The APIC bus clock counter is 32 bits only, it
  864. * might have overflown, but note that we use signed
  865. * longs, thus no extra care needed.
  866. *
  867. * underflown to be exact, as the timer counts down ;)
  868. */
  869. result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
  870. if (cpu_has_tsc)
  871. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  872. "%ld.%04ld MHz.\n",
  873. ((long)(t2-t1)/LOOPS)/(1000000/HZ),
  874. ((long)(t2-t1)/LOOPS)%(1000000/HZ));
  875. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  876. "%ld.%04ld MHz.\n",
  877. result/(1000000/HZ),
  878. result%(1000000/HZ));
  879. return result;
  880. }
  881. static unsigned int calibration_result;
  882. void __init setup_boot_APIC_clock(void)
  883. {
  884. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
  885. using_apic_timer = 1;
  886. local_irq_disable();
  887. calibration_result = calibrate_APIC_clock();
  888. /*
  889. * Now set up the timer for real.
  890. */
  891. setup_APIC_timer(calibration_result);
  892. local_irq_enable();
  893. }
  894. void __init setup_secondary_APIC_clock(void)
  895. {
  896. setup_APIC_timer(calibration_result);
  897. }
  898. void __init disable_APIC_timer(void)
  899. {
  900. if (using_apic_timer) {
  901. unsigned long v;
  902. v = apic_read(APIC_LVTT);
  903. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  904. }
  905. }
  906. void enable_APIC_timer(void)
  907. {
  908. if (using_apic_timer) {
  909. unsigned long v;
  910. v = apic_read(APIC_LVTT);
  911. apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
  912. }
  913. }
  914. /*
  915. * the frequency of the profiling timer can be changed
  916. * by writing a multiplier value into /proc/profile.
  917. */
  918. int setup_profiling_timer(unsigned int multiplier)
  919. {
  920. int i;
  921. /*
  922. * Sanity check. [at least 500 APIC cycles should be
  923. * between APIC interrupts as a rule of thumb, to avoid
  924. * irqs flooding us]
  925. */
  926. if ( (!multiplier) || (calibration_result/multiplier < 500))
  927. return -EINVAL;
  928. /*
  929. * Set the new multiplier for each CPU. CPUs don't start using the
  930. * new values until the next timer interrupt in which they do process
  931. * accounting. At that time they also adjust their APIC timers
  932. * accordingly.
  933. */
  934. for (i = 0; i < NR_CPUS; ++i)
  935. per_cpu(prof_multiplier, i) = multiplier;
  936. return 0;
  937. }
  938. #undef APIC_DIVISOR
  939. /*
  940. * Local timer interrupt handler. It does both profiling and
  941. * process statistics/rescheduling.
  942. *
  943. * We do profiling in every local tick, statistics/rescheduling
  944. * happen only every 'profiling multiplier' ticks. The default
  945. * multiplier is 1 and it can be changed by writing the new multiplier
  946. * value into /proc/profile.
  947. */
  948. inline void smp_local_timer_interrupt(struct pt_regs * regs)
  949. {
  950. int cpu = smp_processor_id();
  951. profile_tick(CPU_PROFILING, regs);
  952. if (--per_cpu(prof_counter, cpu) <= 0) {
  953. /*
  954. * The multiplier may have changed since the last time we got
  955. * to this point as a result of the user writing to
  956. * /proc/profile. In this case we need to adjust the APIC
  957. * timer accordingly.
  958. *
  959. * Interrupts are already masked off at this point.
  960. */
  961. per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
  962. if (per_cpu(prof_counter, cpu) !=
  963. per_cpu(prof_old_multiplier, cpu)) {
  964. __setup_APIC_LVTT(
  965. calibration_result/
  966. per_cpu(prof_counter, cpu));
  967. per_cpu(prof_old_multiplier, cpu) =
  968. per_cpu(prof_counter, cpu);
  969. }
  970. #ifdef CONFIG_SMP
  971. update_process_times(user_mode(regs));
  972. #endif
  973. }
  974. /*
  975. * We take the 'long' return path, and there every subsystem
  976. * grabs the apropriate locks (kernel lock/ irq lock).
  977. *
  978. * we might want to decouple profiling from the 'long path',
  979. * and do the profiling totally in assembly.
  980. *
  981. * Currently this isn't too much of an issue (performance wise),
  982. * we can take more than 100K local irqs per second on a 100 MHz P5.
  983. */
  984. }
  985. /*
  986. * Local APIC timer interrupt. This is the most natural way for doing
  987. * local interrupts, but local timer interrupts can be emulated by
  988. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  989. *
  990. * [ if a single-CPU system runs an SMP kernel then we call the local
  991. * interrupt as well. Thus we cannot inline the local irq ... ]
  992. */
  993. fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
  994. {
  995. int cpu = smp_processor_id();
  996. /*
  997. * the NMI deadlock-detector uses this.
  998. */
  999. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  1000. /*
  1001. * NOTE! We'd better ACK the irq immediately,
  1002. * because timer handling can be slow.
  1003. */
  1004. ack_APIC_irq();
  1005. /*
  1006. * update_process_times() expects us to have done irq_enter().
  1007. * Besides, if we don't timer interrupts ignore the global
  1008. * interrupt lock, which is the WrongThing (tm) to do.
  1009. */
  1010. irq_enter();
  1011. smp_local_timer_interrupt(regs);
  1012. irq_exit();
  1013. }
  1014. /*
  1015. * This interrupt should _never_ happen with our APIC/SMP architecture
  1016. */
  1017. fastcall void smp_spurious_interrupt(struct pt_regs *regs)
  1018. {
  1019. unsigned long v;
  1020. irq_enter();
  1021. /*
  1022. * Check if this really is a spurious interrupt and ACK it
  1023. * if it is a vectored one. Just in case...
  1024. * Spurious interrupts should not be ACKed.
  1025. */
  1026. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1027. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1028. ack_APIC_irq();
  1029. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1030. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
  1031. smp_processor_id());
  1032. irq_exit();
  1033. }
  1034. /*
  1035. * This interrupt should never happen with our APIC/SMP architecture
  1036. */
  1037. fastcall void smp_error_interrupt(struct pt_regs *regs)
  1038. {
  1039. unsigned long v, v1;
  1040. irq_enter();
  1041. /* First tickle the hardware, only then report what went on. -- REW */
  1042. v = apic_read(APIC_ESR);
  1043. apic_write(APIC_ESR, 0);
  1044. v1 = apic_read(APIC_ESR);
  1045. ack_APIC_irq();
  1046. atomic_inc(&irq_err_count);
  1047. /* Here is what the APIC error bits mean:
  1048. 0: Send CS error
  1049. 1: Receive CS error
  1050. 2: Send accept error
  1051. 3: Receive accept error
  1052. 4: Reserved
  1053. 5: Send illegal vector
  1054. 6: Received illegal vector
  1055. 7: Illegal register address
  1056. */
  1057. printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
  1058. smp_processor_id(), v , v1);
  1059. irq_exit();
  1060. }
  1061. /*
  1062. * This initializes the IO-APIC and APIC hardware if this is
  1063. * a UP kernel.
  1064. */
  1065. int __init APIC_init_uniprocessor (void)
  1066. {
  1067. if (enable_local_apic < 0)
  1068. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1069. if (!smp_found_config && !cpu_has_apic)
  1070. return -1;
  1071. /*
  1072. * Complain if the BIOS pretends there is one.
  1073. */
  1074. if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1075. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1076. boot_cpu_physical_apicid);
  1077. return -1;
  1078. }
  1079. verify_local_APIC();
  1080. connect_bsp_APIC();
  1081. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1082. setup_local_APIC();
  1083. #ifdef CONFIG_X86_IO_APIC
  1084. if (smp_found_config)
  1085. if (!skip_ioapic_setup && nr_ioapics)
  1086. setup_IO_APIC();
  1087. #endif
  1088. setup_boot_APIC_clock();
  1089. return 0;
  1090. }