apic.c 30 KB

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