apic.c 30 KB

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