apic.c 29 KB

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