apic_64.c 31 KB

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