apic.c 28 KB

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