apic_64.c 29 KB

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