apic.c 30 KB

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