apic.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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/cpu.h>
  26. #include <linux/module.h>
  27. #include <asm/atomic.h>
  28. #include <asm/smp.h>
  29. #include <asm/mtrr.h>
  30. #include <asm/mpspec.h>
  31. #include <asm/desc.h>
  32. #include <asm/arch_hooks.h>
  33. #include <asm/hpet.h>
  34. #include <asm/i8253.h>
  35. #include <asm/nmi.h>
  36. #include <mach_apic.h>
  37. #include <mach_apicdef.h>
  38. #include <mach_ipi.h>
  39. #include "io_ports.h"
  40. /*
  41. * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
  42. * IPIs in place of local APIC timers
  43. */
  44. static cpumask_t timer_bcast_ipi;
  45. /*
  46. * Knob to control our willingness to enable the local APIC.
  47. */
  48. static int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
  49. static inline void lapic_disable(void)
  50. {
  51. enable_local_apic = -1;
  52. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  53. }
  54. static inline void lapic_enable(void)
  55. {
  56. enable_local_apic = 1;
  57. }
  58. /*
  59. * Debug level
  60. */
  61. int apic_verbosity;
  62. static void apic_pm_activate(void);
  63. static int modern_apic(void)
  64. {
  65. unsigned int lvr, version;
  66. /* AMD systems use old APIC versions, so check the CPU */
  67. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  68. boot_cpu_data.x86 >= 0xf)
  69. return 1;
  70. lvr = apic_read(APIC_LVR);
  71. version = GET_APIC_VERSION(lvr);
  72. return version >= 0x14;
  73. }
  74. /*
  75. * 'what should we do if we get a hw irq event on an illegal vector'.
  76. * each architecture has to answer this themselves.
  77. */
  78. void ack_bad_irq(unsigned int irq)
  79. {
  80. printk("unexpected IRQ trap at vector %02x\n", irq);
  81. /*
  82. * Currently unexpected vectors happen only on SMP and APIC.
  83. * We _must_ ack these because every local APIC has only N
  84. * irq slots per priority level, and a 'hanging, unacked' IRQ
  85. * holds up an irq slot - in excessive cases (when multiple
  86. * unexpected vectors occur) that might lock up the APIC
  87. * completely.
  88. * But only ack when the APIC is enabled -AK
  89. */
  90. if (cpu_has_apic)
  91. ack_APIC_irq();
  92. }
  93. void __init apic_intr_init(void)
  94. {
  95. #ifdef CONFIG_SMP
  96. smp_intr_init();
  97. #endif
  98. /* self generated IPI for local APIC timer */
  99. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  100. /* IPI vectors for APIC spurious and error interrupts */
  101. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  102. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  103. /* thermal monitor LVT interrupt */
  104. #ifdef CONFIG_X86_MCE_P4THERMAL
  105. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  106. #endif
  107. }
  108. /* Using APIC to generate smp_local_timer_interrupt? */
  109. int using_apic_timer __read_mostly = 0;
  110. static int enabled_via_apicbase;
  111. void enable_NMI_through_LVT0 (void * dummy)
  112. {
  113. unsigned int v, ver;
  114. ver = apic_read(APIC_LVR);
  115. ver = GET_APIC_VERSION(ver);
  116. v = APIC_DM_NMI; /* unmask and set to NMI */
  117. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  118. v |= APIC_LVT_LEVEL_TRIGGER;
  119. apic_write_around(APIC_LVT0, v);
  120. }
  121. int get_physical_broadcast(void)
  122. {
  123. if (modern_apic())
  124. return 0xff;
  125. else
  126. return 0xf;
  127. }
  128. int get_maxlvt(void)
  129. {
  130. unsigned int v, ver, maxlvt;
  131. v = apic_read(APIC_LVR);
  132. ver = GET_APIC_VERSION(v);
  133. /* 82489DXs do not report # of LVT entries. */
  134. maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
  135. return maxlvt;
  136. }
  137. void clear_local_APIC(void)
  138. {
  139. int maxlvt;
  140. unsigned long v;
  141. maxlvt = get_maxlvt();
  142. /*
  143. * Masking an LVT entry can trigger a local APIC error
  144. * if the vector is zero. Mask LVTERR first to prevent this.
  145. */
  146. if (maxlvt >= 3) {
  147. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  148. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  149. }
  150. /*
  151. * Careful: we have to set masks only first to deassert
  152. * any level-triggered sources.
  153. */
  154. v = apic_read(APIC_LVTT);
  155. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  156. v = apic_read(APIC_LVT0);
  157. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  158. v = apic_read(APIC_LVT1);
  159. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  160. if (maxlvt >= 4) {
  161. v = apic_read(APIC_LVTPC);
  162. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  163. }
  164. /* lets not touch this if we didn't frob it */
  165. #ifdef CONFIG_X86_MCE_P4THERMAL
  166. if (maxlvt >= 5) {
  167. v = apic_read(APIC_LVTTHMR);
  168. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  169. }
  170. #endif
  171. /*
  172. * Clean APIC state for other OSs:
  173. */
  174. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  175. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  176. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  177. if (maxlvt >= 3)
  178. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  179. if (maxlvt >= 4)
  180. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  181. #ifdef CONFIG_X86_MCE_P4THERMAL
  182. if (maxlvt >= 5)
  183. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  184. #endif
  185. v = GET_APIC_VERSION(apic_read(APIC_LVR));
  186. if (APIC_INTEGRATED(v)) { /* !82489DX */
  187. if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
  188. apic_write(APIC_ESR, 0);
  189. apic_read(APIC_ESR);
  190. }
  191. }
  192. void __init connect_bsp_APIC(void)
  193. {
  194. if (pic_mode) {
  195. /*
  196. * Do not trust the local APIC being empty at bootup.
  197. */
  198. clear_local_APIC();
  199. /*
  200. * PIC mode, enable APIC mode in the IMCR, i.e.
  201. * connect BSP's local APIC to INT and NMI lines.
  202. */
  203. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  204. "enabling APIC mode.\n");
  205. outb(0x70, 0x22);
  206. outb(0x01, 0x23);
  207. }
  208. enable_apic_mode();
  209. }
  210. void disconnect_bsp_APIC(int virt_wire_setup)
  211. {
  212. if (pic_mode) {
  213. /*
  214. * Put the board back into PIC mode (has an effect
  215. * only on certain older boards). Note that APIC
  216. * interrupts, including IPIs, won't work beyond
  217. * this point! The only exception are INIT IPIs.
  218. */
  219. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  220. "entering PIC mode.\n");
  221. outb(0x70, 0x22);
  222. outb(0x00, 0x23);
  223. }
  224. else {
  225. /* Go back to Virtual Wire compatibility mode */
  226. unsigned long value;
  227. /* For the spurious interrupt use vector F, and enable it */
  228. value = apic_read(APIC_SPIV);
  229. value &= ~APIC_VECTOR_MASK;
  230. value |= APIC_SPIV_APIC_ENABLED;
  231. value |= 0xf;
  232. apic_write_around(APIC_SPIV, value);
  233. if (!virt_wire_setup) {
  234. /* For LVT0 make it edge triggered, active high, external and enabled */
  235. value = apic_read(APIC_LVT0);
  236. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  237. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  238. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
  239. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  240. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  241. apic_write_around(APIC_LVT0, value);
  242. }
  243. else {
  244. /* Disable LVT0 */
  245. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  246. }
  247. /* For LVT1 make it edge triggered, active high, nmi and enabled */
  248. value = apic_read(APIC_LVT1);
  249. value &= ~(
  250. APIC_MODE_MASK | APIC_SEND_PENDING |
  251. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  252. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  253. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  254. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  255. apic_write_around(APIC_LVT1, value);
  256. }
  257. }
  258. void disable_local_APIC(void)
  259. {
  260. unsigned long value;
  261. clear_local_APIC();
  262. /*
  263. * Disable APIC (implies clearing of registers
  264. * for 82489DX!).
  265. */
  266. value = apic_read(APIC_SPIV);
  267. value &= ~APIC_SPIV_APIC_ENABLED;
  268. apic_write_around(APIC_SPIV, value);
  269. if (enabled_via_apicbase) {
  270. unsigned int l, h;
  271. rdmsr(MSR_IA32_APICBASE, l, h);
  272. l &= ~MSR_IA32_APICBASE_ENABLE;
  273. wrmsr(MSR_IA32_APICBASE, l, h);
  274. }
  275. }
  276. /*
  277. * This is to verify that we're looking at a real local APIC.
  278. * Check these against your board if the CPUs aren't getting
  279. * started for no apparent reason.
  280. */
  281. int __init verify_local_APIC(void)
  282. {
  283. unsigned int reg0, reg1;
  284. /*
  285. * The version register is read-only in a real APIC.
  286. */
  287. reg0 = apic_read(APIC_LVR);
  288. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  289. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  290. reg1 = apic_read(APIC_LVR);
  291. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  292. /*
  293. * The two version reads above should print the same
  294. * numbers. If the second one is different, then we
  295. * poke at a non-APIC.
  296. */
  297. if (reg1 != reg0)
  298. return 0;
  299. /*
  300. * Check if the version looks reasonably.
  301. */
  302. reg1 = GET_APIC_VERSION(reg0);
  303. if (reg1 == 0x00 || reg1 == 0xff)
  304. return 0;
  305. reg1 = get_maxlvt();
  306. if (reg1 < 0x02 || reg1 == 0xff)
  307. return 0;
  308. /*
  309. * The ID register is read/write in a real APIC.
  310. */
  311. reg0 = apic_read(APIC_ID);
  312. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  313. /*
  314. * The next two are just to see if we have sane values.
  315. * They're only really relevant if we're in Virtual Wire
  316. * compatibility mode, but most boxes are anymore.
  317. */
  318. reg0 = apic_read(APIC_LVT0);
  319. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  320. reg1 = apic_read(APIC_LVT1);
  321. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  322. return 1;
  323. }
  324. void __init sync_Arb_IDs(void)
  325. {
  326. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1
  327. And not needed on AMD */
  328. if (modern_apic())
  329. return;
  330. /*
  331. * Wait for idle.
  332. */
  333. apic_wait_icr_idle();
  334. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  335. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  336. | APIC_DM_INIT);
  337. }
  338. extern void __error_in_apic_c (void);
  339. /*
  340. * An initial setup of the virtual wire mode.
  341. */
  342. void __init init_bsp_APIC(void)
  343. {
  344. unsigned long value, ver;
  345. /*
  346. * Don't do the setup now if we have a SMP BIOS as the
  347. * through-I/O-APIC virtual wire mode might be active.
  348. */
  349. if (smp_found_config || !cpu_has_apic)
  350. return;
  351. value = apic_read(APIC_LVR);
  352. ver = GET_APIC_VERSION(value);
  353. /*
  354. * Do not trust the local APIC being empty at bootup.
  355. */
  356. clear_local_APIC();
  357. /*
  358. * Enable APIC.
  359. */
  360. value = apic_read(APIC_SPIV);
  361. value &= ~APIC_VECTOR_MASK;
  362. value |= APIC_SPIV_APIC_ENABLED;
  363. /* This bit is reserved on P4/Xeon and should be cleared */
  364. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
  365. value &= ~APIC_SPIV_FOCUS_DISABLED;
  366. else
  367. value |= APIC_SPIV_FOCUS_DISABLED;
  368. value |= SPURIOUS_APIC_VECTOR;
  369. apic_write_around(APIC_SPIV, value);
  370. /*
  371. * Set up the virtual wire mode.
  372. */
  373. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  374. value = APIC_DM_NMI;
  375. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  376. value |= APIC_LVT_LEVEL_TRIGGER;
  377. apic_write_around(APIC_LVT1, value);
  378. }
  379. void __devinit setup_local_APIC(void)
  380. {
  381. unsigned long oldvalue, value, ver, maxlvt;
  382. int i, j;
  383. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  384. if (esr_disable) {
  385. apic_write(APIC_ESR, 0);
  386. apic_write(APIC_ESR, 0);
  387. apic_write(APIC_ESR, 0);
  388. apic_write(APIC_ESR, 0);
  389. }
  390. value = apic_read(APIC_LVR);
  391. ver = GET_APIC_VERSION(value);
  392. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  393. __error_in_apic_c();
  394. /*
  395. * Double-check whether this APIC is really registered.
  396. */
  397. if (!apic_id_registered())
  398. BUG();
  399. /*
  400. * Intel recommends to set DFR, LDR and TPR before enabling
  401. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  402. * document number 292116). So here it goes...
  403. */
  404. init_apic_ldr();
  405. /*
  406. * Set Task Priority to 'accept all'. We never change this
  407. * later on.
  408. */
  409. value = apic_read(APIC_TASKPRI);
  410. value &= ~APIC_TPRI_MASK;
  411. apic_write_around(APIC_TASKPRI, value);
  412. /*
  413. * After a crash, we no longer service the interrupts and a pending
  414. * interrupt from previous kernel might still have ISR bit set.
  415. *
  416. * Most probably by now CPU has serviced that pending interrupt and
  417. * it might not have done the ack_APIC_irq() because it thought,
  418. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  419. * does not clear the ISR bit and cpu thinks it has already serivced
  420. * the interrupt. Hence a vector might get locked. It was noticed
  421. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  422. */
  423. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  424. value = apic_read(APIC_ISR + i*0x10);
  425. for (j = 31; j >= 0; j--) {
  426. if (value & (1<<j))
  427. ack_APIC_irq();
  428. }
  429. }
  430. /*
  431. * Now that we are all set up, enable the APIC
  432. */
  433. value = apic_read(APIC_SPIV);
  434. value &= ~APIC_VECTOR_MASK;
  435. /*
  436. * Enable APIC
  437. */
  438. value |= APIC_SPIV_APIC_ENABLED;
  439. /*
  440. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  441. * certain networking cards. If high frequency interrupts are
  442. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  443. * entry is masked/unmasked at a high rate as well then sooner or
  444. * later IOAPIC line gets 'stuck', no more interrupts are received
  445. * from the device. If focus CPU is disabled then the hang goes
  446. * away, oh well :-(
  447. *
  448. * [ This bug can be reproduced easily with a level-triggered
  449. * PCI Ne2000 networking cards and PII/PIII processors, dual
  450. * BX chipset. ]
  451. */
  452. /*
  453. * Actually disabling the focus CPU check just makes the hang less
  454. * frequent as it makes the interrupt distributon model be more
  455. * like LRU than MRU (the short-term load is more even across CPUs).
  456. * See also the comment in end_level_ioapic_irq(). --macro
  457. */
  458. #if 1
  459. /* Enable focus processor (bit==0) */
  460. value &= ~APIC_SPIV_FOCUS_DISABLED;
  461. #else
  462. /* Disable focus processor (bit==1) */
  463. value |= APIC_SPIV_FOCUS_DISABLED;
  464. #endif
  465. /*
  466. * Set spurious IRQ vector
  467. */
  468. value |= SPURIOUS_APIC_VECTOR;
  469. apic_write_around(APIC_SPIV, value);
  470. /*
  471. * Set up LVT0, LVT1:
  472. *
  473. * set up through-local-APIC on the BP's LINT0. This is not
  474. * strictly necessery in pure symmetric-IO mode, but sometimes
  475. * we delegate interrupts to the 8259A.
  476. */
  477. /*
  478. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  479. */
  480. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  481. if (!smp_processor_id() && (pic_mode || !value)) {
  482. value = APIC_DM_EXTINT;
  483. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  484. smp_processor_id());
  485. } else {
  486. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  487. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  488. smp_processor_id());
  489. }
  490. apic_write_around(APIC_LVT0, value);
  491. /*
  492. * only the BP should see the LINT1 NMI signal, obviously.
  493. */
  494. if (!smp_processor_id())
  495. value = APIC_DM_NMI;
  496. else
  497. value = APIC_DM_NMI | APIC_LVT_MASKED;
  498. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  499. value |= APIC_LVT_LEVEL_TRIGGER;
  500. apic_write_around(APIC_LVT1, value);
  501. if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
  502. maxlvt = get_maxlvt();
  503. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  504. apic_write(APIC_ESR, 0);
  505. oldvalue = apic_read(APIC_ESR);
  506. value = ERROR_APIC_VECTOR; // enables sending errors
  507. apic_write_around(APIC_LVTERR, value);
  508. /*
  509. * spec says clear errors after enabling vector.
  510. */
  511. if (maxlvt > 3)
  512. apic_write(APIC_ESR, 0);
  513. value = apic_read(APIC_ESR);
  514. if (value != oldvalue)
  515. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  516. "vector: 0x%08lx after: 0x%08lx\n",
  517. oldvalue, value);
  518. } else {
  519. if (esr_disable)
  520. /*
  521. * Something untraceble is creating bad interrupts on
  522. * secondary quads ... for the moment, just leave the
  523. * ESR disabled - we can't do anything useful with the
  524. * errors anyway - mbligh
  525. */
  526. printk("Leaving ESR disabled.\n");
  527. else
  528. printk("No ESR for 82489DX.\n");
  529. }
  530. setup_apic_nmi_watchdog(NULL);
  531. apic_pm_activate();
  532. }
  533. /*
  534. * If Linux enabled the LAPIC against the BIOS default
  535. * disable it down before re-entering the BIOS on shutdown.
  536. * Otherwise the BIOS may get confused and not power-off.
  537. * Additionally clear all LVT entries before disable_local_APIC
  538. * for the case where Linux didn't enable the LAPIC.
  539. */
  540. void lapic_shutdown(void)
  541. {
  542. unsigned long flags;
  543. if (!cpu_has_apic)
  544. return;
  545. local_irq_save(flags);
  546. clear_local_APIC();
  547. if (enabled_via_apicbase)
  548. disable_local_APIC();
  549. local_irq_restore(flags);
  550. }
  551. #ifdef CONFIG_PM
  552. static struct {
  553. int active;
  554. /* r/w apic fields */
  555. unsigned int apic_id;
  556. unsigned int apic_taskpri;
  557. unsigned int apic_ldr;
  558. unsigned int apic_dfr;
  559. unsigned int apic_spiv;
  560. unsigned int apic_lvtt;
  561. unsigned int apic_lvtpc;
  562. unsigned int apic_lvt0;
  563. unsigned int apic_lvt1;
  564. unsigned int apic_lvterr;
  565. unsigned int apic_tmict;
  566. unsigned int apic_tdcr;
  567. unsigned int apic_thmr;
  568. } apic_pm_state;
  569. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  570. {
  571. unsigned long flags;
  572. int maxlvt;
  573. if (!apic_pm_state.active)
  574. return 0;
  575. maxlvt = get_maxlvt();
  576. apic_pm_state.apic_id = apic_read(APIC_ID);
  577. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  578. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  579. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  580. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  581. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  582. if (maxlvt >= 4)
  583. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  584. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  585. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  586. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  587. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  588. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  589. #ifdef CONFIG_X86_MCE_P4THERMAL
  590. if (maxlvt >= 5)
  591. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  592. #endif
  593. local_irq_save(flags);
  594. disable_local_APIC();
  595. local_irq_restore(flags);
  596. return 0;
  597. }
  598. static int lapic_resume(struct sys_device *dev)
  599. {
  600. unsigned int l, h;
  601. unsigned long flags;
  602. int maxlvt;
  603. if (!apic_pm_state.active)
  604. return 0;
  605. maxlvt = get_maxlvt();
  606. local_irq_save(flags);
  607. /*
  608. * Make sure the APICBASE points to the right address
  609. *
  610. * FIXME! This will be wrong if we ever support suspend on
  611. * SMP! We'll need to do this as part of the CPU restore!
  612. */
  613. rdmsr(MSR_IA32_APICBASE, l, h);
  614. l &= ~MSR_IA32_APICBASE_BASE;
  615. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  616. wrmsr(MSR_IA32_APICBASE, l, h);
  617. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  618. apic_write(APIC_ID, apic_pm_state.apic_id);
  619. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  620. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  621. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  622. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  623. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  624. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  625. #ifdef CONFIG_X86_MCE_P4THERMAL
  626. if (maxlvt >= 5)
  627. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  628. #endif
  629. if (maxlvt >= 4)
  630. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  631. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  632. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  633. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  634. apic_write(APIC_ESR, 0);
  635. apic_read(APIC_ESR);
  636. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  637. apic_write(APIC_ESR, 0);
  638. apic_read(APIC_ESR);
  639. local_irq_restore(flags);
  640. return 0;
  641. }
  642. /*
  643. * This device has no shutdown method - fully functioning local APICs
  644. * are needed on every CPU up until machine_halt/restart/poweroff.
  645. */
  646. static struct sysdev_class lapic_sysclass = {
  647. set_kset_name("lapic"),
  648. .resume = lapic_resume,
  649. .suspend = lapic_suspend,
  650. };
  651. static struct sys_device device_lapic = {
  652. .id = 0,
  653. .cls = &lapic_sysclass,
  654. };
  655. static void __devinit apic_pm_activate(void)
  656. {
  657. apic_pm_state.active = 1;
  658. }
  659. static int __init init_lapic_sysfs(void)
  660. {
  661. int error;
  662. if (!cpu_has_apic)
  663. return 0;
  664. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  665. error = sysdev_class_register(&lapic_sysclass);
  666. if (!error)
  667. error = sysdev_register(&device_lapic);
  668. return error;
  669. }
  670. device_initcall(init_lapic_sysfs);
  671. #else /* CONFIG_PM */
  672. static void apic_pm_activate(void) { }
  673. #endif /* CONFIG_PM */
  674. /*
  675. * Detect and enable local APICs on non-SMP boards.
  676. * Original code written by Keir Fraser.
  677. */
  678. static int __init apic_set_verbosity(char *str)
  679. {
  680. if (strcmp("debug", str) == 0)
  681. apic_verbosity = APIC_DEBUG;
  682. else if (strcmp("verbose", str) == 0)
  683. apic_verbosity = APIC_VERBOSE;
  684. return 1;
  685. }
  686. __setup("apic=", apic_set_verbosity);
  687. static int __init detect_init_APIC (void)
  688. {
  689. u32 h, l, features;
  690. /* Disabled by kernel option? */
  691. if (enable_local_apic < 0)
  692. return -1;
  693. switch (boot_cpu_data.x86_vendor) {
  694. case X86_VENDOR_AMD:
  695. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  696. (boot_cpu_data.x86 == 15))
  697. break;
  698. goto no_apic;
  699. case X86_VENDOR_INTEL:
  700. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  701. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  702. break;
  703. goto no_apic;
  704. default:
  705. goto no_apic;
  706. }
  707. if (!cpu_has_apic) {
  708. /*
  709. * Over-ride BIOS and try to enable the local
  710. * APIC only if "lapic" specified.
  711. */
  712. if (enable_local_apic <= 0) {
  713. printk("Local APIC disabled by BIOS -- "
  714. "you can enable it with \"lapic\"\n");
  715. return -1;
  716. }
  717. /*
  718. * Some BIOSes disable the local APIC in the
  719. * APIC_BASE MSR. This can only be done in
  720. * software for Intel P6 or later and AMD K7
  721. * (Model > 1) or later.
  722. */
  723. rdmsr(MSR_IA32_APICBASE, l, h);
  724. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  725. printk("Local APIC disabled by BIOS -- reenabling.\n");
  726. l &= ~MSR_IA32_APICBASE_BASE;
  727. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  728. wrmsr(MSR_IA32_APICBASE, l, h);
  729. enabled_via_apicbase = 1;
  730. }
  731. }
  732. /*
  733. * The APIC feature bit should now be enabled
  734. * in `cpuid'
  735. */
  736. features = cpuid_edx(1);
  737. if (!(features & (1 << X86_FEATURE_APIC))) {
  738. printk("Could not enable APIC!\n");
  739. return -1;
  740. }
  741. set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  742. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  743. /* The BIOS may have set up the APIC at some other address */
  744. rdmsr(MSR_IA32_APICBASE, l, h);
  745. if (l & MSR_IA32_APICBASE_ENABLE)
  746. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  747. if (nmi_watchdog != NMI_NONE)
  748. nmi_watchdog = NMI_LOCAL_APIC;
  749. printk("Found and enabled local APIC!\n");
  750. apic_pm_activate();
  751. return 0;
  752. no_apic:
  753. printk("No local APIC present or hardware disabled\n");
  754. return -1;
  755. }
  756. void __init init_apic_mappings(void)
  757. {
  758. unsigned long apic_phys;
  759. /*
  760. * If no local APIC can be found then set up a fake all
  761. * zeroes page to simulate the local APIC and another
  762. * one for the IO-APIC.
  763. */
  764. if (!smp_found_config && detect_init_APIC()) {
  765. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  766. apic_phys = __pa(apic_phys);
  767. } else
  768. apic_phys = mp_lapic_addr;
  769. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  770. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  771. apic_phys);
  772. /*
  773. * Fetch the APIC ID of the BSP in case we have a
  774. * default configuration (or the MP table is broken).
  775. */
  776. if (boot_cpu_physical_apicid == -1U)
  777. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  778. #ifdef CONFIG_X86_IO_APIC
  779. {
  780. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  781. int i;
  782. for (i = 0; i < nr_ioapics; i++) {
  783. if (smp_found_config) {
  784. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  785. if (!ioapic_phys) {
  786. printk(KERN_ERR
  787. "WARNING: bogus zero IO-APIC "
  788. "address found in MPTABLE, "
  789. "disabling IO/APIC support!\n");
  790. smp_found_config = 0;
  791. skip_ioapic_setup = 1;
  792. goto fake_ioapic_page;
  793. }
  794. } else {
  795. fake_ioapic_page:
  796. ioapic_phys = (unsigned long)
  797. alloc_bootmem_pages(PAGE_SIZE);
  798. ioapic_phys = __pa(ioapic_phys);
  799. }
  800. set_fixmap_nocache(idx, ioapic_phys);
  801. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  802. __fix_to_virt(idx), ioapic_phys);
  803. idx++;
  804. }
  805. }
  806. #endif
  807. }
  808. /*
  809. * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
  810. * per second. We assume that the caller has already set up the local
  811. * APIC.
  812. *
  813. * The APIC timer is not exactly sync with the external timer chip, it
  814. * closely follows bus clocks.
  815. */
  816. /*
  817. * The timer chip is already set up at HZ interrupts per second here,
  818. * but we do not accept timer interrupts yet. We only allow the BP
  819. * to calibrate.
  820. */
  821. static unsigned int __devinit get_8254_timer_count(void)
  822. {
  823. unsigned long flags;
  824. unsigned int count;
  825. spin_lock_irqsave(&i8253_lock, flags);
  826. outb_p(0x00, PIT_MODE);
  827. count = inb_p(PIT_CH0);
  828. count |= inb_p(PIT_CH0) << 8;
  829. spin_unlock_irqrestore(&i8253_lock, flags);
  830. return count;
  831. }
  832. /* next tick in 8254 can be caught by catching timer wraparound */
  833. static void __devinit wait_8254_wraparound(void)
  834. {
  835. unsigned int curr_count, prev_count;
  836. curr_count = get_8254_timer_count();
  837. do {
  838. prev_count = curr_count;
  839. curr_count = get_8254_timer_count();
  840. /* workaround for broken Mercury/Neptune */
  841. if (prev_count >= curr_count + 0x100)
  842. curr_count = get_8254_timer_count();
  843. } while (prev_count >= curr_count);
  844. }
  845. /*
  846. * Default initialization for 8254 timers. If we use other timers like HPET,
  847. * we override this later
  848. */
  849. void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
  850. /*
  851. * This function sets up the local APIC timer, with a timeout of
  852. * 'clocks' APIC bus clock. During calibration we actually call
  853. * this function twice on the boot CPU, once with a bogus timeout
  854. * value, second time for real. The other (noncalibrating) CPUs
  855. * call this function only once, with the real, calibrated value.
  856. *
  857. * We do reads before writes even if unnecessary, to get around the
  858. * P5 APIC double write bug.
  859. */
  860. #define APIC_DIVISOR 16
  861. static void __setup_APIC_LVTT(unsigned int clocks)
  862. {
  863. unsigned int lvtt_value, tmp_value, ver;
  864. int cpu = smp_processor_id();
  865. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  866. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  867. if (!APIC_INTEGRATED(ver))
  868. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  869. if (cpu_isset(cpu, timer_bcast_ipi))
  870. lvtt_value |= APIC_LVT_MASKED;
  871. apic_write_around(APIC_LVTT, lvtt_value);
  872. /*
  873. * Divide PICLK by 16
  874. */
  875. tmp_value = apic_read(APIC_TDCR);
  876. apic_write_around(APIC_TDCR, (tmp_value
  877. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  878. | APIC_TDR_DIV_16);
  879. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  880. }
  881. static void __devinit setup_APIC_timer(unsigned int clocks)
  882. {
  883. unsigned long flags;
  884. local_irq_save(flags);
  885. /*
  886. * Wait for IRQ0's slice:
  887. */
  888. wait_timer_tick();
  889. __setup_APIC_LVTT(clocks);
  890. local_irq_restore(flags);
  891. }
  892. /*
  893. * In this function we calibrate APIC bus clocks to the external
  894. * timer. Unfortunately we cannot use jiffies and the timer irq
  895. * to calibrate, since some later bootup code depends on getting
  896. * the first irq? Ugh.
  897. *
  898. * We want to do the calibration only once since we
  899. * want to have local timer irqs syncron. CPUs connected
  900. * by the same APIC bus have the very same bus frequency.
  901. * And we want to have irqs off anyways, no accidental
  902. * APIC irq that way.
  903. */
  904. static int __init calibrate_APIC_clock(void)
  905. {
  906. unsigned long long t1 = 0, t2 = 0;
  907. long tt1, tt2;
  908. long result;
  909. int i;
  910. const int LOOPS = HZ/10;
  911. apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
  912. /*
  913. * Put whatever arbitrary (but long enough) timeout
  914. * value into the APIC clock, we just want to get the
  915. * counter running for calibration.
  916. */
  917. __setup_APIC_LVTT(1000000000);
  918. /*
  919. * The timer chip counts down to zero. Let's wait
  920. * for a wraparound to start exact measurement:
  921. * (the current tick might have been already half done)
  922. */
  923. wait_timer_tick();
  924. /*
  925. * We wrapped around just now. Let's start:
  926. */
  927. if (cpu_has_tsc)
  928. rdtscll(t1);
  929. tt1 = apic_read(APIC_TMCCT);
  930. /*
  931. * Let's wait LOOPS wraprounds:
  932. */
  933. for (i = 0; i < LOOPS; i++)
  934. wait_timer_tick();
  935. tt2 = apic_read(APIC_TMCCT);
  936. if (cpu_has_tsc)
  937. rdtscll(t2);
  938. /*
  939. * The APIC bus clock counter is 32 bits only, it
  940. * might have overflown, but note that we use signed
  941. * longs, thus no extra care needed.
  942. *
  943. * underflown to be exact, as the timer counts down ;)
  944. */
  945. result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
  946. if (cpu_has_tsc)
  947. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  948. "%ld.%04ld MHz.\n",
  949. ((long)(t2-t1)/LOOPS)/(1000000/HZ),
  950. ((long)(t2-t1)/LOOPS)%(1000000/HZ));
  951. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  952. "%ld.%04ld MHz.\n",
  953. result/(1000000/HZ),
  954. result%(1000000/HZ));
  955. return result;
  956. }
  957. static unsigned int calibration_result;
  958. void __init setup_boot_APIC_clock(void)
  959. {
  960. unsigned long flags;
  961. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
  962. using_apic_timer = 1;
  963. local_irq_save(flags);
  964. calibration_result = calibrate_APIC_clock();
  965. /*
  966. * Now set up the timer for real.
  967. */
  968. setup_APIC_timer(calibration_result);
  969. local_irq_restore(flags);
  970. }
  971. void __devinit setup_secondary_APIC_clock(void)
  972. {
  973. setup_APIC_timer(calibration_result);
  974. }
  975. void disable_APIC_timer(void)
  976. {
  977. if (using_apic_timer) {
  978. unsigned long v;
  979. v = apic_read(APIC_LVTT);
  980. /*
  981. * When an illegal vector value (0-15) is written to an LVT
  982. * entry and delivery mode is Fixed, the APIC may signal an
  983. * illegal vector error, with out regard to whether the mask
  984. * bit is set or whether an interrupt is actually seen on input.
  985. *
  986. * Boot sequence might call this function when the LVTT has
  987. * '0' vector value. So make sure vector field is set to
  988. * valid value.
  989. */
  990. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  991. apic_write_around(APIC_LVTT, v);
  992. }
  993. }
  994. void enable_APIC_timer(void)
  995. {
  996. int cpu = smp_processor_id();
  997. if (using_apic_timer &&
  998. !cpu_isset(cpu, timer_bcast_ipi)) {
  999. unsigned long v;
  1000. v = apic_read(APIC_LVTT);
  1001. apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
  1002. }
  1003. }
  1004. void switch_APIC_timer_to_ipi(void *cpumask)
  1005. {
  1006. cpumask_t mask = *(cpumask_t *)cpumask;
  1007. int cpu = smp_processor_id();
  1008. if (cpu_isset(cpu, mask) &&
  1009. !cpu_isset(cpu, timer_bcast_ipi)) {
  1010. disable_APIC_timer();
  1011. cpu_set(cpu, timer_bcast_ipi);
  1012. }
  1013. }
  1014. EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
  1015. void switch_ipi_to_APIC_timer(void *cpumask)
  1016. {
  1017. cpumask_t mask = *(cpumask_t *)cpumask;
  1018. int cpu = smp_processor_id();
  1019. if (cpu_isset(cpu, mask) &&
  1020. cpu_isset(cpu, timer_bcast_ipi)) {
  1021. cpu_clear(cpu, timer_bcast_ipi);
  1022. enable_APIC_timer();
  1023. }
  1024. }
  1025. EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
  1026. #undef APIC_DIVISOR
  1027. /*
  1028. * Local timer interrupt handler. It does both profiling and
  1029. * process statistics/rescheduling.
  1030. *
  1031. * We do profiling in every local tick, statistics/rescheduling
  1032. * happen only every 'profiling multiplier' ticks. The default
  1033. * multiplier is 1 and it can be changed by writing the new multiplier
  1034. * value into /proc/profile.
  1035. */
  1036. inline void smp_local_timer_interrupt(void)
  1037. {
  1038. profile_tick(CPU_PROFILING);
  1039. #ifdef CONFIG_SMP
  1040. update_process_times(user_mode_vm(get_irq_regs()));
  1041. #endif
  1042. /*
  1043. * We take the 'long' return path, and there every subsystem
  1044. * grabs the apropriate locks (kernel lock/ irq lock).
  1045. *
  1046. * we might want to decouple profiling from the 'long path',
  1047. * and do the profiling totally in assembly.
  1048. *
  1049. * Currently this isn't too much of an issue (performance wise),
  1050. * we can take more than 100K local irqs per second on a 100 MHz P5.
  1051. */
  1052. }
  1053. /*
  1054. * Local APIC timer interrupt. This is the most natural way for doing
  1055. * local interrupts, but local timer interrupts can be emulated by
  1056. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  1057. *
  1058. * [ if a single-CPU system runs an SMP kernel then we call the local
  1059. * interrupt as well. Thus we cannot inline the local irq ... ]
  1060. */
  1061. fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
  1062. {
  1063. struct pt_regs *old_regs = set_irq_regs(regs);
  1064. int cpu = smp_processor_id();
  1065. /*
  1066. * the NMI deadlock-detector uses this.
  1067. */
  1068. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  1069. /*
  1070. * NOTE! We'd better ACK the irq immediately,
  1071. * because timer handling can be slow.
  1072. */
  1073. ack_APIC_irq();
  1074. /*
  1075. * update_process_times() expects us to have done irq_enter().
  1076. * Besides, if we don't timer interrupts ignore the global
  1077. * interrupt lock, which is the WrongThing (tm) to do.
  1078. */
  1079. irq_enter();
  1080. smp_local_timer_interrupt();
  1081. irq_exit();
  1082. set_irq_regs(old_regs);
  1083. }
  1084. #ifndef CONFIG_SMP
  1085. static void up_apic_timer_interrupt_call(void)
  1086. {
  1087. int cpu = smp_processor_id();
  1088. /*
  1089. * the NMI deadlock-detector uses this.
  1090. */
  1091. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  1092. smp_local_timer_interrupt();
  1093. }
  1094. #endif
  1095. void smp_send_timer_broadcast_ipi(void)
  1096. {
  1097. cpumask_t mask;
  1098. cpus_and(mask, cpu_online_map, timer_bcast_ipi);
  1099. if (!cpus_empty(mask)) {
  1100. #ifdef CONFIG_SMP
  1101. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  1102. #else
  1103. /*
  1104. * We can directly call the apic timer interrupt handler
  1105. * in UP case. Minus all irq related functions
  1106. */
  1107. up_apic_timer_interrupt_call();
  1108. #endif
  1109. }
  1110. }
  1111. int setup_profiling_timer(unsigned int multiplier)
  1112. {
  1113. return -EINVAL;
  1114. }
  1115. /*
  1116. * This interrupt should _never_ happen with our APIC/SMP architecture
  1117. */
  1118. fastcall void smp_spurious_interrupt(struct pt_regs *regs)
  1119. {
  1120. unsigned long v;
  1121. irq_enter();
  1122. /*
  1123. * Check if this really is a spurious interrupt and ACK it
  1124. * if it is a vectored one. Just in case...
  1125. * Spurious interrupts should not be ACKed.
  1126. */
  1127. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1128. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1129. ack_APIC_irq();
  1130. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1131. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
  1132. smp_processor_id());
  1133. irq_exit();
  1134. }
  1135. /*
  1136. * This interrupt should never happen with our APIC/SMP architecture
  1137. */
  1138. fastcall void smp_error_interrupt(struct pt_regs *regs)
  1139. {
  1140. unsigned long v, v1;
  1141. irq_enter();
  1142. /* First tickle the hardware, only then report what went on. -- REW */
  1143. v = apic_read(APIC_ESR);
  1144. apic_write(APIC_ESR, 0);
  1145. v1 = apic_read(APIC_ESR);
  1146. ack_APIC_irq();
  1147. atomic_inc(&irq_err_count);
  1148. /* Here is what the APIC error bits mean:
  1149. 0: Send CS error
  1150. 1: Receive CS error
  1151. 2: Send accept error
  1152. 3: Receive accept error
  1153. 4: Reserved
  1154. 5: Send illegal vector
  1155. 6: Received illegal vector
  1156. 7: Illegal register address
  1157. */
  1158. printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
  1159. smp_processor_id(), v , v1);
  1160. irq_exit();
  1161. }
  1162. /*
  1163. * This initializes the IO-APIC and APIC hardware if this is
  1164. * a UP kernel.
  1165. */
  1166. int __init APIC_init_uniprocessor (void)
  1167. {
  1168. if (enable_local_apic < 0)
  1169. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1170. if (!smp_found_config && !cpu_has_apic)
  1171. return -1;
  1172. /*
  1173. * Complain if the BIOS pretends there is one.
  1174. */
  1175. if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1176. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1177. boot_cpu_physical_apicid);
  1178. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1179. return -1;
  1180. }
  1181. verify_local_APIC();
  1182. connect_bsp_APIC();
  1183. /*
  1184. * Hack: In case of kdump, after a crash, kernel might be booting
  1185. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1186. * might be zero if read from MP tables. Get it from LAPIC.
  1187. */
  1188. #ifdef CONFIG_CRASH_DUMP
  1189. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  1190. #endif
  1191. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1192. setup_local_APIC();
  1193. #ifdef CONFIG_X86_IO_APIC
  1194. if (smp_found_config)
  1195. if (!skip_ioapic_setup && nr_ioapics)
  1196. setup_IO_APIC();
  1197. #endif
  1198. setup_boot_APIC_clock();
  1199. return 0;
  1200. }
  1201. static int __init parse_lapic(char *arg)
  1202. {
  1203. lapic_enable();
  1204. return 0;
  1205. }
  1206. early_param("lapic", parse_lapic);
  1207. static int __init parse_nolapic(char *arg)
  1208. {
  1209. lapic_disable();
  1210. return 0;
  1211. }
  1212. early_param("nolapic", parse_nolapic);