apic.c 28 KB

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