apic.c 27 KB

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