apic.c 31 KB

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