apic_64.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /*
  2. * Local APIC handling, local APIC timers
  3. *
  4. * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Fixes
  7. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  8. * thanks to Eric Gilmore
  9. * and Rolf G. Tews
  10. * for testing these extensively.
  11. * Maciej W. Rozycki : Various updates and fixes.
  12. * Mikael Pettersson : Power Management for UP-APIC.
  13. * Pavel Machek and
  14. * Mikael Pettersson : PM converted to driver model.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/delay.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/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 <asm/atomic.h>
  28. #include <asm/smp.h>
  29. #include <asm/mtrr.h>
  30. #include <asm/mpspec.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/mach_apic.h>
  33. #include <asm/nmi.h>
  34. #include <asm/idle.h>
  35. #include <asm/proto.h>
  36. #include <asm/timex.h>
  37. #include <asm/hpet.h>
  38. #include <asm/apic.h>
  39. int apic_verbosity;
  40. int disable_apic_timer __cpuinitdata;
  41. static int apic_calibrate_pmtmr __initdata;
  42. /* Local APIC timer works in C2? */
  43. int local_apic_timer_c2_ok;
  44. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  45. static struct resource *ioapic_resources;
  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. unsigned int safe_apic_wait_icr_idle(void)
  118. {
  119. unsigned int 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 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 = 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. /*
  251. * This is to verify that we're looking at a real local APIC.
  252. * Check these against your board if the CPUs aren't getting
  253. * started for no apparent reason.
  254. */
  255. int __init verify_local_APIC(void)
  256. {
  257. unsigned int reg0, reg1;
  258. /*
  259. * The version register is read-only in a real APIC.
  260. */
  261. reg0 = apic_read(APIC_LVR);
  262. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  263. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  264. reg1 = apic_read(APIC_LVR);
  265. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  266. /*
  267. * The two version reads above should print the same
  268. * numbers. If the second one is different, then we
  269. * poke at a non-APIC.
  270. */
  271. if (reg1 != reg0)
  272. return 0;
  273. /*
  274. * Check if the version looks reasonably.
  275. */
  276. reg1 = GET_APIC_VERSION(reg0);
  277. if (reg1 == 0x00 || reg1 == 0xff)
  278. return 0;
  279. reg1 = get_maxlvt();
  280. if (reg1 < 0x02 || reg1 == 0xff)
  281. return 0;
  282. /*
  283. * The ID register is read/write in a real APIC.
  284. */
  285. reg0 = apic_read(APIC_ID);
  286. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  287. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  288. reg1 = apic_read(APIC_ID);
  289. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  290. apic_write(APIC_ID, reg0);
  291. if (reg1 != (reg0 ^ APIC_ID_MASK))
  292. return 0;
  293. /*
  294. * The next two are just to see if we have sane values.
  295. * They're only really relevant if we're in Virtual Wire
  296. * compatibility mode, but most boxes are anymore.
  297. */
  298. reg0 = apic_read(APIC_LVT0);
  299. apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
  300. reg1 = apic_read(APIC_LVT1);
  301. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  302. return 1;
  303. }
  304. void __init sync_Arb_IDs(void)
  305. {
  306. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  307. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  308. if (ver >= 0x14) /* P4 or higher */
  309. return;
  310. /*
  311. * Wait for idle.
  312. */
  313. apic_wait_icr_idle();
  314. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  315. apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  316. | APIC_DM_INIT);
  317. }
  318. /*
  319. * An initial setup of the virtual wire mode.
  320. */
  321. void __init init_bsp_APIC(void)
  322. {
  323. unsigned int value;
  324. /*
  325. * Don't do the setup now if we have a SMP BIOS as the
  326. * through-I/O-APIC virtual wire mode might be active.
  327. */
  328. if (smp_found_config || !cpu_has_apic)
  329. return;
  330. value = apic_read(APIC_LVR);
  331. /*
  332. * Do not trust the local APIC being empty at bootup.
  333. */
  334. clear_local_APIC();
  335. /*
  336. * Enable APIC.
  337. */
  338. value = apic_read(APIC_SPIV);
  339. value &= ~APIC_VECTOR_MASK;
  340. value |= APIC_SPIV_APIC_ENABLED;
  341. value |= APIC_SPIV_FOCUS_DISABLED;
  342. value |= SPURIOUS_APIC_VECTOR;
  343. apic_write(APIC_SPIV, value);
  344. /*
  345. * Set up the virtual wire mode.
  346. */
  347. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  348. value = APIC_DM_NMI;
  349. apic_write(APIC_LVT1, value);
  350. }
  351. void __cpuinit setup_local_APIC (void)
  352. {
  353. unsigned int value, maxlvt;
  354. int i, j;
  355. value = apic_read(APIC_LVR);
  356. BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
  357. /*
  358. * Double-check whether this APIC is really registered.
  359. * This is meaningless in clustered apic mode, so we skip it.
  360. */
  361. if (!apic_id_registered())
  362. BUG();
  363. /*
  364. * Intel recommends to set DFR, LDR and TPR before enabling
  365. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  366. * document number 292116). So here it goes...
  367. */
  368. init_apic_ldr();
  369. /*
  370. * Set Task Priority to 'accept all'. We never change this
  371. * later on.
  372. */
  373. value = apic_read(APIC_TASKPRI);
  374. value &= ~APIC_TPRI_MASK;
  375. apic_write(APIC_TASKPRI, value);
  376. /*
  377. * After a crash, we no longer service the interrupts and a pending
  378. * interrupt from previous kernel might still have ISR bit set.
  379. *
  380. * Most probably by now CPU has serviced that pending interrupt and
  381. * it might not have done the ack_APIC_irq() because it thought,
  382. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  383. * does not clear the ISR bit and cpu thinks it has already serivced
  384. * the interrupt. Hence a vector might get locked. It was noticed
  385. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  386. */
  387. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  388. value = apic_read(APIC_ISR + i*0x10);
  389. for (j = 31; j >= 0; j--) {
  390. if (value & (1<<j))
  391. ack_APIC_irq();
  392. }
  393. }
  394. /*
  395. * Now that we are all set up, enable the APIC
  396. */
  397. value = apic_read(APIC_SPIV);
  398. value &= ~APIC_VECTOR_MASK;
  399. /*
  400. * Enable APIC
  401. */
  402. value |= APIC_SPIV_APIC_ENABLED;
  403. /* We always use processor focus */
  404. /*
  405. * Set spurious IRQ vector
  406. */
  407. value |= SPURIOUS_APIC_VECTOR;
  408. apic_write(APIC_SPIV, value);
  409. /*
  410. * Set up LVT0, LVT1:
  411. *
  412. * set up through-local-APIC on the BP's LINT0. This is not
  413. * strictly necessary in pure symmetric-IO mode, but sometimes
  414. * we delegate interrupts to the 8259A.
  415. */
  416. /*
  417. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  418. */
  419. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  420. if (!smp_processor_id() && !value) {
  421. value = APIC_DM_EXTINT;
  422. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  423. smp_processor_id());
  424. } else {
  425. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  426. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  427. smp_processor_id());
  428. }
  429. apic_write(APIC_LVT0, value);
  430. /*
  431. * only the BP should see the LINT1 NMI signal, obviously.
  432. */
  433. if (!smp_processor_id())
  434. value = APIC_DM_NMI;
  435. else
  436. value = APIC_DM_NMI | APIC_LVT_MASKED;
  437. apic_write(APIC_LVT1, value);
  438. {
  439. unsigned oldvalue;
  440. maxlvt = get_maxlvt();
  441. oldvalue = apic_read(APIC_ESR);
  442. value = ERROR_APIC_VECTOR; // enables sending errors
  443. apic_write(APIC_LVTERR, value);
  444. /*
  445. * spec says clear errors after enabling vector.
  446. */
  447. if (maxlvt > 3)
  448. apic_write(APIC_ESR, 0);
  449. value = apic_read(APIC_ESR);
  450. if (value != oldvalue)
  451. apic_printk(APIC_VERBOSE,
  452. "ESR value after enabling vector: %08x, after %08x\n",
  453. oldvalue, value);
  454. }
  455. nmi_watchdog_default();
  456. setup_apic_nmi_watchdog(NULL);
  457. apic_pm_activate();
  458. }
  459. #ifdef CONFIG_PM
  460. static struct {
  461. /* 'active' is true if the local APIC was enabled by us and
  462. not the BIOS; this signifies that we are also responsible
  463. for disabling it before entering apm/acpi suspend */
  464. int active;
  465. /* r/w apic fields */
  466. unsigned int apic_id;
  467. unsigned int apic_taskpri;
  468. unsigned int apic_ldr;
  469. unsigned int apic_dfr;
  470. unsigned int apic_spiv;
  471. unsigned int apic_lvtt;
  472. unsigned int apic_lvtpc;
  473. unsigned int apic_lvt0;
  474. unsigned int apic_lvt1;
  475. unsigned int apic_lvterr;
  476. unsigned int apic_tmict;
  477. unsigned int apic_tdcr;
  478. unsigned int apic_thmr;
  479. } apic_pm_state;
  480. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  481. {
  482. unsigned long flags;
  483. int maxlvt;
  484. if (!apic_pm_state.active)
  485. return 0;
  486. maxlvt = get_maxlvt();
  487. apic_pm_state.apic_id = apic_read(APIC_ID);
  488. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  489. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  490. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  491. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  492. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  493. if (maxlvt >= 4)
  494. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  495. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  496. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  497. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  498. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  499. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  500. #ifdef CONFIG_X86_MCE_INTEL
  501. if (maxlvt >= 5)
  502. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  503. #endif
  504. local_irq_save(flags);
  505. disable_local_APIC();
  506. local_irq_restore(flags);
  507. return 0;
  508. }
  509. static int lapic_resume(struct sys_device *dev)
  510. {
  511. unsigned int l, h;
  512. unsigned long flags;
  513. int maxlvt;
  514. if (!apic_pm_state.active)
  515. return 0;
  516. maxlvt = get_maxlvt();
  517. local_irq_save(flags);
  518. rdmsr(MSR_IA32_APICBASE, l, h);
  519. l &= ~MSR_IA32_APICBASE_BASE;
  520. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  521. wrmsr(MSR_IA32_APICBASE, l, h);
  522. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  523. apic_write(APIC_ID, apic_pm_state.apic_id);
  524. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  525. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  526. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  527. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  528. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  529. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  530. #ifdef CONFIG_X86_MCE_INTEL
  531. if (maxlvt >= 5)
  532. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  533. #endif
  534. if (maxlvt >= 4)
  535. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  536. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  537. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  538. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  539. apic_write(APIC_ESR, 0);
  540. apic_read(APIC_ESR);
  541. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  542. apic_write(APIC_ESR, 0);
  543. apic_read(APIC_ESR);
  544. local_irq_restore(flags);
  545. return 0;
  546. }
  547. static struct sysdev_class lapic_sysclass = {
  548. set_kset_name("lapic"),
  549. .resume = lapic_resume,
  550. .suspend = lapic_suspend,
  551. };
  552. static struct sys_device device_lapic = {
  553. .id = 0,
  554. .cls = &lapic_sysclass,
  555. };
  556. static void __cpuinit apic_pm_activate(void)
  557. {
  558. apic_pm_state.active = 1;
  559. }
  560. static int __init init_lapic_sysfs(void)
  561. {
  562. int error;
  563. if (!cpu_has_apic)
  564. return 0;
  565. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  566. error = sysdev_class_register(&lapic_sysclass);
  567. if (!error)
  568. error = sysdev_register(&device_lapic);
  569. return error;
  570. }
  571. device_initcall(init_lapic_sysfs);
  572. #else /* CONFIG_PM */
  573. static void apic_pm_activate(void) { }
  574. #endif /* CONFIG_PM */
  575. static int __init apic_set_verbosity(char *str)
  576. {
  577. if (str == NULL) {
  578. skip_ioapic_setup = 0;
  579. ioapic_force = 1;
  580. return 0;
  581. }
  582. if (strcmp("debug", str) == 0)
  583. apic_verbosity = APIC_DEBUG;
  584. else if (strcmp("verbose", str) == 0)
  585. apic_verbosity = APIC_VERBOSE;
  586. else {
  587. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  588. " use apic=verbose or apic=debug\n", str);
  589. return -EINVAL;
  590. }
  591. return 0;
  592. }
  593. early_param("apic", apic_set_verbosity);
  594. /*
  595. * Detect and enable local APICs on non-SMP boards.
  596. * Original code written by Keir Fraser.
  597. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  598. * not correctly set up (usually the APIC timer won't work etc.)
  599. */
  600. static int __init detect_init_APIC (void)
  601. {
  602. if (!cpu_has_apic) {
  603. printk(KERN_INFO "No local APIC present\n");
  604. return -1;
  605. }
  606. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  607. boot_cpu_id = 0;
  608. return 0;
  609. }
  610. #ifdef CONFIG_X86_IO_APIC
  611. static struct resource * __init ioapic_setup_resources(void)
  612. {
  613. #define IOAPIC_RESOURCE_NAME_SIZE 11
  614. unsigned long n;
  615. struct resource *res;
  616. char *mem;
  617. int i;
  618. if (nr_ioapics <= 0)
  619. return NULL;
  620. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  621. n *= nr_ioapics;
  622. mem = alloc_bootmem(n);
  623. res = (void *)mem;
  624. if (mem != NULL) {
  625. memset(mem, 0, n);
  626. mem += sizeof(struct resource) * nr_ioapics;
  627. for (i = 0; i < nr_ioapics; i++) {
  628. res[i].name = mem;
  629. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  630. sprintf(mem, "IOAPIC %u", i);
  631. mem += IOAPIC_RESOURCE_NAME_SIZE;
  632. }
  633. }
  634. ioapic_resources = res;
  635. return res;
  636. }
  637. static int __init ioapic_insert_resources(void)
  638. {
  639. int i;
  640. struct resource *r = ioapic_resources;
  641. if (!r) {
  642. printk("IO APIC resources could be not be allocated.\n");
  643. return -1;
  644. }
  645. for (i = 0; i < nr_ioapics; i++) {
  646. insert_resource(&iomem_resource, r);
  647. r++;
  648. }
  649. return 0;
  650. }
  651. /* Insert the IO APIC resources after PCI initialization has occured to handle
  652. * IO APICS that are mapped in on a BAR in PCI space. */
  653. late_initcall(ioapic_insert_resources);
  654. #endif
  655. void __init init_apic_mappings(void)
  656. {
  657. unsigned long apic_phys;
  658. /*
  659. * If no local APIC can be found then set up a fake all
  660. * zeroes page to simulate the local APIC and another
  661. * one for the IO-APIC.
  662. */
  663. if (!smp_found_config && detect_init_APIC()) {
  664. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  665. apic_phys = __pa(apic_phys);
  666. } else
  667. apic_phys = mp_lapic_addr;
  668. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  669. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  670. APIC_BASE, apic_phys);
  671. /* Put local APIC into the resource map. */
  672. lapic_resource.start = apic_phys;
  673. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  674. insert_resource(&iomem_resource, &lapic_resource);
  675. /*
  676. * Fetch the APIC ID of the BSP in case we have a
  677. * default configuration (or the MP table is broken).
  678. */
  679. boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
  680. {
  681. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  682. int i;
  683. struct resource *ioapic_res;
  684. ioapic_res = ioapic_setup_resources();
  685. for (i = 0; i < nr_ioapics; i++) {
  686. if (smp_found_config) {
  687. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  688. } else {
  689. ioapic_phys = (unsigned long)
  690. alloc_bootmem_pages(PAGE_SIZE);
  691. ioapic_phys = __pa(ioapic_phys);
  692. }
  693. set_fixmap_nocache(idx, ioapic_phys);
  694. apic_printk(APIC_VERBOSE,
  695. "mapped IOAPIC to %016lx (%016lx)\n",
  696. __fix_to_virt(idx), ioapic_phys);
  697. idx++;
  698. if (ioapic_res != NULL) {
  699. ioapic_res->start = ioapic_phys;
  700. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  701. ioapic_res++;
  702. }
  703. }
  704. }
  705. }
  706. /*
  707. * This function sets up the local APIC timer, with a timeout of
  708. * 'clocks' APIC bus clock. During calibration we actually call
  709. * this function twice on the boot CPU, once with a bogus timeout
  710. * value, second time for real. The other (noncalibrating) CPUs
  711. * call this function only once, with the real, calibrated value.
  712. *
  713. * We do reads before writes even if unnecessary, to get around the
  714. * P5 APIC double write bug.
  715. */
  716. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  717. {
  718. unsigned int lvtt_value, tmp_value;
  719. lvtt_value = LOCAL_TIMER_VECTOR;
  720. if (!oneshot)
  721. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  722. if (!irqen)
  723. lvtt_value |= APIC_LVT_MASKED;
  724. apic_write(APIC_LVTT, lvtt_value);
  725. /*
  726. * Divide PICLK by 16
  727. */
  728. tmp_value = apic_read(APIC_TDCR);
  729. apic_write(APIC_TDCR, (tmp_value
  730. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  731. | APIC_TDR_DIV_16);
  732. if (!oneshot)
  733. apic_write(APIC_TMICT, clocks);
  734. }
  735. static void setup_APIC_timer(void)
  736. {
  737. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  738. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  739. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  740. clockevents_register_device(levt);
  741. }
  742. /*
  743. * In this function we calibrate APIC bus clocks to the external
  744. * timer. Unfortunately we cannot use jiffies and the timer irq
  745. * to calibrate, since some later bootup code depends on getting
  746. * the first irq? Ugh.
  747. *
  748. * We want to do the calibration only once since we
  749. * want to have local timer irqs syncron. CPUs connected
  750. * by the same APIC bus have the very same bus frequency.
  751. * And we want to have irqs off anyways, no accidental
  752. * APIC irq that way.
  753. */
  754. #define TICK_COUNT 100000000
  755. static void __init calibrate_APIC_clock(void)
  756. {
  757. unsigned apic, apic_start;
  758. unsigned long tsc, tsc_start;
  759. int result;
  760. local_irq_disable();
  761. /*
  762. * Put whatever arbitrary (but long enough) timeout
  763. * value into the APIC clock, we just want to get the
  764. * counter running for calibration.
  765. *
  766. * No interrupt enable !
  767. */
  768. __setup_APIC_LVTT(250000000, 0, 0);
  769. apic_start = apic_read(APIC_TMCCT);
  770. #ifdef CONFIG_X86_PM_TIMER
  771. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  772. pmtimer_wait(5000); /* 5ms wait */
  773. apic = apic_read(APIC_TMCCT);
  774. result = (apic_start - apic) * 1000L / 5;
  775. } else
  776. #endif
  777. {
  778. rdtscll(tsc_start);
  779. do {
  780. apic = apic_read(APIC_TMCCT);
  781. rdtscll(tsc);
  782. } while ((tsc - tsc_start) < TICK_COUNT &&
  783. (apic_start - apic) < TICK_COUNT);
  784. result = (apic_start - apic) * 1000L * tsc_khz /
  785. (tsc - tsc_start);
  786. }
  787. local_irq_enable();
  788. printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
  789. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  790. result / 1000 / 1000, result / 1000 % 1000);
  791. /* Calculate the scaled math multiplication factor */
  792. lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC, 32);
  793. lapic_clockevent.max_delta_ns =
  794. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  795. lapic_clockevent.min_delta_ns =
  796. clockevent_delta2ns(0xF, &lapic_clockevent);
  797. calibration_result = result / HZ;
  798. }
  799. void __init setup_boot_APIC_clock (void)
  800. {
  801. /*
  802. * The local apic timer can be disabled via the kernel commandline.
  803. * Register the lapic timer as a dummy clock event source on SMP
  804. * systems, so the broadcast mechanism is used. On UP systems simply
  805. * ignore it.
  806. */
  807. if (disable_apic_timer) {
  808. printk(KERN_INFO "Disabling APIC timer\n");
  809. /* No broadcast on UP ! */
  810. if (num_possible_cpus() > 1)
  811. setup_APIC_timer();
  812. return;
  813. }
  814. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  815. calibrate_APIC_clock();
  816. /*
  817. * If nmi_watchdog is set to IO_APIC, we need the
  818. * PIT/HPET going. Otherwise register lapic as a dummy
  819. * device.
  820. */
  821. if (nmi_watchdog != NMI_IO_APIC)
  822. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  823. else
  824. printk(KERN_WARNING "APIC timer registered as dummy,"
  825. " due to nmi_watchdog=1!\n");
  826. setup_APIC_timer();
  827. }
  828. void __cpuinit setup_secondary_APIC_clock(void)
  829. {
  830. setup_APIC_timer();
  831. }
  832. int setup_profiling_timer(unsigned int multiplier)
  833. {
  834. return -EINVAL;
  835. }
  836. void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
  837. unsigned char msg_type, unsigned char mask)
  838. {
  839. unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
  840. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  841. apic_write(reg, v);
  842. }
  843. /*
  844. * Local timer interrupt handler. It does both profiling and
  845. * process statistics/rescheduling.
  846. *
  847. * We do profiling in every local tick, statistics/rescheduling
  848. * happen only every 'profiling multiplier' ticks. The default
  849. * multiplier is 1 and it can be changed by writing the new multiplier
  850. * value into /proc/profile.
  851. */
  852. void smp_local_timer_interrupt(void)
  853. {
  854. int cpu = smp_processor_id();
  855. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  856. /*
  857. * Normally we should not be here till LAPIC has been initialized but
  858. * in some cases like kdump, its possible that there is a pending LAPIC
  859. * timer interrupt from previous kernel's context and is delivered in
  860. * new kernel the moment interrupts are enabled.
  861. *
  862. * Interrupts are enabled early and LAPIC is setup much later, hence
  863. * its possible that when we get here evt->event_handler is NULL.
  864. * Check for event_handler being NULL and discard the interrupt as
  865. * spurious.
  866. */
  867. if (!evt->event_handler) {
  868. printk(KERN_WARNING
  869. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  870. /* Switch it off */
  871. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  872. return;
  873. }
  874. /*
  875. * the NMI deadlock-detector uses this.
  876. */
  877. add_pda(apic_timer_irqs, 1);
  878. evt->event_handler(evt);
  879. }
  880. /*
  881. * Local APIC timer interrupt. This is the most natural way for doing
  882. * local interrupts, but local timer interrupts can be emulated by
  883. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  884. *
  885. * [ if a single-CPU system runs an SMP kernel then we call the local
  886. * interrupt as well. Thus we cannot inline the local irq ... ]
  887. */
  888. void smp_apic_timer_interrupt(struct pt_regs *regs)
  889. {
  890. struct pt_regs *old_regs = set_irq_regs(regs);
  891. /*
  892. * NOTE! We'd better ACK the irq immediately,
  893. * because timer handling can be slow.
  894. */
  895. ack_APIC_irq();
  896. /*
  897. * update_process_times() expects us to have done irq_enter().
  898. * Besides, if we don't timer interrupts ignore the global
  899. * interrupt lock, which is the WrongThing (tm) to do.
  900. */
  901. exit_idle();
  902. irq_enter();
  903. smp_local_timer_interrupt();
  904. irq_exit();
  905. set_irq_regs(old_regs);
  906. }
  907. /*
  908. * apic_is_clustered_box() -- Check if we can expect good TSC
  909. *
  910. * Thus far, the major user of this is IBM's Summit2 series:
  911. *
  912. * Clustered boxes may have unsynced TSC problems if they are
  913. * multi-chassis. Use available data to take a good guess.
  914. * If in doubt, go HPET.
  915. */
  916. __cpuinit int apic_is_clustered_box(void)
  917. {
  918. int i, clusters, zeros;
  919. unsigned id;
  920. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  921. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  922. for (i = 0; i < NR_CPUS; i++) {
  923. id = bios_cpu_apicid[i];
  924. if (id != BAD_APICID)
  925. __set_bit(APIC_CLUSTERID(id), clustermap);
  926. }
  927. /* Problem: Partially populated chassis may not have CPUs in some of
  928. * the APIC clusters they have been allocated. Only present CPUs have
  929. * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
  930. * clusters are allocated sequentially, count zeros only if they are
  931. * bounded by ones.
  932. */
  933. clusters = 0;
  934. zeros = 0;
  935. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  936. if (test_bit(i, clustermap)) {
  937. clusters += 1 + zeros;
  938. zeros = 0;
  939. } else
  940. ++zeros;
  941. }
  942. /*
  943. * If clusters > 2, then should be multi-chassis.
  944. * May have to revisit this when multi-core + hyperthreaded CPUs come
  945. * out, but AFAIK this will work even for them.
  946. */
  947. return (clusters > 2);
  948. }
  949. /*
  950. * This interrupt should _never_ happen with our APIC/SMP architecture
  951. */
  952. asmlinkage void smp_spurious_interrupt(void)
  953. {
  954. unsigned int v;
  955. exit_idle();
  956. irq_enter();
  957. /*
  958. * Check if this really is a spurious interrupt and ACK it
  959. * if it is a vectored one. Just in case...
  960. * Spurious interrupts should not be ACKed.
  961. */
  962. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  963. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  964. ack_APIC_irq();
  965. irq_exit();
  966. }
  967. /*
  968. * This interrupt should never happen with our APIC/SMP architecture
  969. */
  970. asmlinkage void smp_error_interrupt(void)
  971. {
  972. unsigned int v, v1;
  973. exit_idle();
  974. irq_enter();
  975. /* First tickle the hardware, only then report what went on. -- REW */
  976. v = apic_read(APIC_ESR);
  977. apic_write(APIC_ESR, 0);
  978. v1 = apic_read(APIC_ESR);
  979. ack_APIC_irq();
  980. atomic_inc(&irq_err_count);
  981. /* Here is what the APIC error bits mean:
  982. 0: Send CS error
  983. 1: Receive CS error
  984. 2: Send accept error
  985. 3: Receive accept error
  986. 4: Reserved
  987. 5: Send illegal vector
  988. 6: Received illegal vector
  989. 7: Illegal register address
  990. */
  991. printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  992. smp_processor_id(), v , v1);
  993. irq_exit();
  994. }
  995. int disable_apic;
  996. /*
  997. * This initializes the IO-APIC and APIC hardware if this is
  998. * a UP kernel.
  999. */
  1000. int __init APIC_init_uniprocessor (void)
  1001. {
  1002. if (disable_apic) {
  1003. printk(KERN_INFO "Apic disabled\n");
  1004. return -1;
  1005. }
  1006. if (!cpu_has_apic) {
  1007. disable_apic = 1;
  1008. printk(KERN_INFO "Apic disabled by BIOS\n");
  1009. return -1;
  1010. }
  1011. verify_local_APIC();
  1012. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  1013. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
  1014. setup_local_APIC();
  1015. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1016. setup_IO_APIC();
  1017. else
  1018. nr_ioapics = 0;
  1019. setup_boot_APIC_clock();
  1020. check_nmi_watchdog();
  1021. return 0;
  1022. }
  1023. static __init int setup_disableapic(char *str)
  1024. {
  1025. disable_apic = 1;
  1026. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1027. return 0;
  1028. }
  1029. early_param("disableapic", setup_disableapic);
  1030. /* same as disableapic, for compatibility */
  1031. static __init int setup_nolapic(char *str)
  1032. {
  1033. return setup_disableapic(str);
  1034. }
  1035. early_param("nolapic", setup_nolapic);
  1036. static int __init parse_lapic_timer_c2_ok(char *arg)
  1037. {
  1038. local_apic_timer_c2_ok = 1;
  1039. return 0;
  1040. }
  1041. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1042. static __init int setup_noapictimer(char *str)
  1043. {
  1044. if (str[0] != ' ' && str[0] != 0)
  1045. return 0;
  1046. disable_apic_timer = 1;
  1047. return 1;
  1048. }
  1049. __setup("noapictimer", setup_noapictimer);
  1050. static __init int setup_apicpmtimer(char *s)
  1051. {
  1052. apic_calibrate_pmtmr = 1;
  1053. notsc_setup(NULL);
  1054. return 0;
  1055. }
  1056. __setup("apicpmtimer", setup_apicpmtimer);