apic_64.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. 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 = 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 = 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 = 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 = 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. #ifdef CONFIG_X86_IO_APIC
  620. static struct resource * __init ioapic_setup_resources(void)
  621. {
  622. #define IOAPIC_RESOURCE_NAME_SIZE 11
  623. unsigned long n;
  624. struct resource *res;
  625. char *mem;
  626. int i;
  627. if (nr_ioapics <= 0)
  628. return NULL;
  629. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  630. n *= nr_ioapics;
  631. mem = alloc_bootmem(n);
  632. res = (void *)mem;
  633. if (mem != NULL) {
  634. memset(mem, 0, n);
  635. mem += sizeof(struct resource) * nr_ioapics;
  636. for (i = 0; i < nr_ioapics; i++) {
  637. res[i].name = mem;
  638. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  639. sprintf(mem, "IOAPIC %u", i);
  640. mem += IOAPIC_RESOURCE_NAME_SIZE;
  641. }
  642. }
  643. ioapic_resources = res;
  644. return res;
  645. }
  646. static int __init ioapic_insert_resources(void)
  647. {
  648. int i;
  649. struct resource *r = ioapic_resources;
  650. if (!r) {
  651. printk("IO APIC resources could be not be allocated.\n");
  652. return -1;
  653. }
  654. for (i = 0; i < nr_ioapics; i++) {
  655. insert_resource(&iomem_resource, r);
  656. r++;
  657. }
  658. return 0;
  659. }
  660. /* Insert the IO APIC resources after PCI initialization has occured to handle
  661. * IO APICS that are mapped in on a BAR in PCI space. */
  662. late_initcall(ioapic_insert_resources);
  663. #endif
  664. void __init init_apic_mappings(void)
  665. {
  666. unsigned long apic_phys;
  667. /*
  668. * If no local APIC can be found then set up a fake all
  669. * zeroes page to simulate the local APIC and another
  670. * one for the IO-APIC.
  671. */
  672. if (!smp_found_config && detect_init_APIC()) {
  673. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  674. apic_phys = __pa(apic_phys);
  675. } else
  676. apic_phys = mp_lapic_addr;
  677. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  678. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  679. APIC_BASE, apic_phys);
  680. /* Put local APIC into the resource map. */
  681. lapic_resource.start = apic_phys;
  682. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  683. insert_resource(&iomem_resource, &lapic_resource);
  684. /*
  685. * Fetch the APIC ID of the BSP in case we have a
  686. * default configuration (or the MP table is broken).
  687. */
  688. boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
  689. {
  690. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  691. int i;
  692. struct resource *ioapic_res;
  693. ioapic_res = ioapic_setup_resources();
  694. for (i = 0; i < nr_ioapics; i++) {
  695. if (smp_found_config) {
  696. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  697. } else {
  698. ioapic_phys = (unsigned long)
  699. alloc_bootmem_pages(PAGE_SIZE);
  700. ioapic_phys = __pa(ioapic_phys);
  701. }
  702. set_fixmap_nocache(idx, ioapic_phys);
  703. apic_printk(APIC_VERBOSE,
  704. "mapped IOAPIC to %016lx (%016lx)\n",
  705. __fix_to_virt(idx), ioapic_phys);
  706. idx++;
  707. if (ioapic_res != NULL) {
  708. ioapic_res->start = ioapic_phys;
  709. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  710. ioapic_res++;
  711. }
  712. }
  713. }
  714. }
  715. /*
  716. * This function sets up the local APIC timer, with a timeout of
  717. * 'clocks' APIC bus clock. During calibration we actually call
  718. * this function twice on the boot CPU, once with a bogus timeout
  719. * value, second time for real. The other (noncalibrating) CPUs
  720. * call this function only once, with the real, calibrated value.
  721. *
  722. * We do reads before writes even if unnecessary, to get around the
  723. * P5 APIC double write bug.
  724. */
  725. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  726. {
  727. unsigned int lvtt_value, tmp_value;
  728. lvtt_value = LOCAL_TIMER_VECTOR;
  729. if (!oneshot)
  730. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  731. if (!irqen)
  732. lvtt_value |= APIC_LVT_MASKED;
  733. apic_write(APIC_LVTT, lvtt_value);
  734. /*
  735. * Divide PICLK by 16
  736. */
  737. tmp_value = apic_read(APIC_TDCR);
  738. apic_write(APIC_TDCR, (tmp_value
  739. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  740. | APIC_TDR_DIV_16);
  741. if (!oneshot)
  742. apic_write(APIC_TMICT, clocks);
  743. }
  744. static void setup_APIC_timer(void)
  745. {
  746. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  747. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  748. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  749. clockevents_register_device(levt);
  750. }
  751. /*
  752. * In this function we calibrate APIC bus clocks to the external
  753. * timer. Unfortunately we cannot use jiffies and the timer irq
  754. * to calibrate, since some later bootup code depends on getting
  755. * the first irq? Ugh.
  756. *
  757. * We want to do the calibration only once since we
  758. * want to have local timer irqs syncron. CPUs connected
  759. * by the same APIC bus have the very same bus frequency.
  760. * And we want to have irqs off anyways, no accidental
  761. * APIC irq that way.
  762. */
  763. #define TICK_COUNT 100000000
  764. static void __init calibrate_APIC_clock(void)
  765. {
  766. unsigned apic, apic_start;
  767. unsigned long tsc, tsc_start;
  768. int result;
  769. local_irq_disable();
  770. /*
  771. * Put whatever arbitrary (but long enough) timeout
  772. * value into the APIC clock, we just want to get the
  773. * counter running for calibration.
  774. *
  775. * No interrupt enable !
  776. */
  777. __setup_APIC_LVTT(250000000, 0, 0);
  778. apic_start = apic_read(APIC_TMCCT);
  779. #ifdef CONFIG_X86_PM_TIMER
  780. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  781. pmtimer_wait(5000); /* 5ms wait */
  782. apic = apic_read(APIC_TMCCT);
  783. result = (apic_start - apic) * 1000L / 5;
  784. } else
  785. #endif
  786. {
  787. rdtscll(tsc_start);
  788. do {
  789. apic = apic_read(APIC_TMCCT);
  790. rdtscll(tsc);
  791. } while ((tsc - tsc_start) < TICK_COUNT &&
  792. (apic_start - apic) < TICK_COUNT);
  793. result = (apic_start - apic) * 1000L * tsc_khz /
  794. (tsc - tsc_start);
  795. }
  796. local_irq_enable();
  797. printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
  798. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  799. result / 1000 / 1000, result / 1000 % 1000);
  800. /* Calculate the scaled math multiplication factor */
  801. lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC, 32);
  802. lapic_clockevent.max_delta_ns =
  803. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  804. lapic_clockevent.min_delta_ns =
  805. clockevent_delta2ns(0xF, &lapic_clockevent);
  806. calibration_result = result / HZ;
  807. }
  808. void __init setup_boot_APIC_clock (void)
  809. {
  810. /*
  811. * The local apic timer can be disabled via the kernel commandline.
  812. * Register the lapic timer as a dummy clock event source on SMP
  813. * systems, so the broadcast mechanism is used. On UP systems simply
  814. * ignore it.
  815. */
  816. if (disable_apic_timer) {
  817. printk(KERN_INFO "Disabling APIC timer\n");
  818. /* No broadcast on UP ! */
  819. if (num_possible_cpus() > 1)
  820. setup_APIC_timer();
  821. return;
  822. }
  823. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  824. calibrate_APIC_clock();
  825. /*
  826. * If nmi_watchdog is set to IO_APIC, we need the
  827. * PIT/HPET going. Otherwise register lapic as a dummy
  828. * device.
  829. */
  830. if (nmi_watchdog != NMI_IO_APIC)
  831. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  832. else
  833. printk(KERN_WARNING "APIC timer registered as dummy,"
  834. " due to nmi_watchdog=1!\n");
  835. setup_APIC_timer();
  836. }
  837. /*
  838. * AMD C1E enabled CPUs have a real nasty problem: Some BIOSes set the
  839. * C1E flag only in the secondary CPU, so when we detect the wreckage
  840. * we already have enabled the boot CPU local apic timer. Check, if
  841. * disable_apic_timer is set and the DUMMY flag is cleared. If yes,
  842. * set the DUMMY flag again and force the broadcast mode in the
  843. * clockevents layer.
  844. */
  845. void __cpuinit check_boot_apic_timer_broadcast(void)
  846. {
  847. if (!disable_apic_timer ||
  848. (lapic_clockevent.features & CLOCK_EVT_FEAT_DUMMY))
  849. return;
  850. printk(KERN_INFO "AMD C1E detected late. Force timer broadcast.\n");
  851. lapic_clockevent.features |= CLOCK_EVT_FEAT_DUMMY;
  852. local_irq_enable();
  853. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE, &boot_cpu_id);
  854. local_irq_disable();
  855. }
  856. void __cpuinit setup_secondary_APIC_clock(void)
  857. {
  858. check_boot_apic_timer_broadcast();
  859. setup_APIC_timer();
  860. }
  861. int setup_profiling_timer(unsigned int multiplier)
  862. {
  863. return -EINVAL;
  864. }
  865. void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
  866. unsigned char msg_type, unsigned char mask)
  867. {
  868. unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
  869. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  870. apic_write(reg, v);
  871. }
  872. /*
  873. * Local timer interrupt handler. It does both profiling and
  874. * process statistics/rescheduling.
  875. *
  876. * We do profiling in every local tick, statistics/rescheduling
  877. * happen only every 'profiling multiplier' ticks. The default
  878. * multiplier is 1 and it can be changed by writing the new multiplier
  879. * value into /proc/profile.
  880. */
  881. void smp_local_timer_interrupt(void)
  882. {
  883. int cpu = smp_processor_id();
  884. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  885. /*
  886. * Normally we should not be here till LAPIC has been initialized but
  887. * in some cases like kdump, its possible that there is a pending LAPIC
  888. * timer interrupt from previous kernel's context and is delivered in
  889. * new kernel the moment interrupts are enabled.
  890. *
  891. * Interrupts are enabled early and LAPIC is setup much later, hence
  892. * its possible that when we get here evt->event_handler is NULL.
  893. * Check for event_handler being NULL and discard the interrupt as
  894. * spurious.
  895. */
  896. if (!evt->event_handler) {
  897. printk(KERN_WARNING
  898. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  899. /* Switch it off */
  900. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  901. return;
  902. }
  903. /*
  904. * the NMI deadlock-detector uses this.
  905. */
  906. add_pda(apic_timer_irqs, 1);
  907. evt->event_handler(evt);
  908. }
  909. /*
  910. * Local APIC timer interrupt. This is the most natural way for doing
  911. * local interrupts, but local timer interrupts can be emulated by
  912. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  913. *
  914. * [ if a single-CPU system runs an SMP kernel then we call the local
  915. * interrupt as well. Thus we cannot inline the local irq ... ]
  916. */
  917. void smp_apic_timer_interrupt(struct pt_regs *regs)
  918. {
  919. struct pt_regs *old_regs = set_irq_regs(regs);
  920. /*
  921. * NOTE! We'd better ACK the irq immediately,
  922. * because timer handling can be slow.
  923. */
  924. ack_APIC_irq();
  925. /*
  926. * update_process_times() expects us to have done irq_enter().
  927. * Besides, if we don't timer interrupts ignore the global
  928. * interrupt lock, which is the WrongThing (tm) to do.
  929. */
  930. exit_idle();
  931. irq_enter();
  932. smp_local_timer_interrupt();
  933. irq_exit();
  934. set_irq_regs(old_regs);
  935. }
  936. /*
  937. * apic_is_clustered_box() -- Check if we can expect good TSC
  938. *
  939. * Thus far, the major user of this is IBM's Summit2 series:
  940. *
  941. * Clustered boxes may have unsynced TSC problems if they are
  942. * multi-chassis. Use available data to take a good guess.
  943. * If in doubt, go HPET.
  944. */
  945. __cpuinit int apic_is_clustered_box(void)
  946. {
  947. int i, clusters, zeros;
  948. unsigned id;
  949. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  950. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  951. for (i = 0; i < NR_CPUS; i++) {
  952. id = bios_cpu_apicid[i];
  953. if (id != BAD_APICID)
  954. __set_bit(APIC_CLUSTERID(id), clustermap);
  955. }
  956. /* Problem: Partially populated chassis may not have CPUs in some of
  957. * the APIC clusters they have been allocated. Only present CPUs have
  958. * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
  959. * clusters are allocated sequentially, count zeros only if they are
  960. * bounded by ones.
  961. */
  962. clusters = 0;
  963. zeros = 0;
  964. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  965. if (test_bit(i, clustermap)) {
  966. clusters += 1 + zeros;
  967. zeros = 0;
  968. } else
  969. ++zeros;
  970. }
  971. /*
  972. * If clusters > 2, then should be multi-chassis.
  973. * May have to revisit this when multi-core + hyperthreaded CPUs come
  974. * out, but AFAIK this will work even for them.
  975. */
  976. return (clusters > 2);
  977. }
  978. /*
  979. * This interrupt should _never_ happen with our APIC/SMP architecture
  980. */
  981. asmlinkage void smp_spurious_interrupt(void)
  982. {
  983. unsigned int v;
  984. exit_idle();
  985. irq_enter();
  986. /*
  987. * Check if this really is a spurious interrupt and ACK it
  988. * if it is a vectored one. Just in case...
  989. * Spurious interrupts should not be ACKed.
  990. */
  991. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  992. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  993. ack_APIC_irq();
  994. add_pda(irq_spurious_count, 1);
  995. irq_exit();
  996. }
  997. /*
  998. * This interrupt should never happen with our APIC/SMP architecture
  999. */
  1000. asmlinkage void smp_error_interrupt(void)
  1001. {
  1002. unsigned int v, v1;
  1003. exit_idle();
  1004. irq_enter();
  1005. /* First tickle the hardware, only then report what went on. -- REW */
  1006. v = apic_read(APIC_ESR);
  1007. apic_write(APIC_ESR, 0);
  1008. v1 = apic_read(APIC_ESR);
  1009. ack_APIC_irq();
  1010. atomic_inc(&irq_err_count);
  1011. /* Here is what the APIC error bits mean:
  1012. 0: Send CS error
  1013. 1: Receive CS error
  1014. 2: Send accept error
  1015. 3: Receive accept error
  1016. 4: Reserved
  1017. 5: Send illegal vector
  1018. 6: Received illegal vector
  1019. 7: Illegal register address
  1020. */
  1021. printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  1022. smp_processor_id(), v , v1);
  1023. irq_exit();
  1024. }
  1025. int disable_apic;
  1026. /*
  1027. * This initializes the IO-APIC and APIC hardware if this is
  1028. * a UP kernel.
  1029. */
  1030. int __init APIC_init_uniprocessor (void)
  1031. {
  1032. if (disable_apic) {
  1033. printk(KERN_INFO "Apic disabled\n");
  1034. return -1;
  1035. }
  1036. if (!cpu_has_apic) {
  1037. disable_apic = 1;
  1038. printk(KERN_INFO "Apic disabled by BIOS\n");
  1039. return -1;
  1040. }
  1041. verify_local_APIC();
  1042. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  1043. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
  1044. setup_local_APIC();
  1045. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1046. setup_IO_APIC();
  1047. else
  1048. nr_ioapics = 0;
  1049. setup_boot_APIC_clock();
  1050. check_nmi_watchdog();
  1051. return 0;
  1052. }
  1053. static __init int setup_disableapic(char *str)
  1054. {
  1055. disable_apic = 1;
  1056. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1057. return 0;
  1058. }
  1059. early_param("disableapic", setup_disableapic);
  1060. /* same as disableapic, for compatibility */
  1061. static __init int setup_nolapic(char *str)
  1062. {
  1063. return setup_disableapic(str);
  1064. }
  1065. early_param("nolapic", setup_nolapic);
  1066. static int __init parse_lapic_timer_c2_ok(char *arg)
  1067. {
  1068. local_apic_timer_c2_ok = 1;
  1069. return 0;
  1070. }
  1071. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1072. static __init int setup_noapictimer(char *str)
  1073. {
  1074. if (str[0] != ' ' && str[0] != 0)
  1075. return 0;
  1076. disable_apic_timer = 1;
  1077. return 1;
  1078. }
  1079. __setup("noapictimer", setup_noapictimer);
  1080. static __init int setup_apicpmtimer(char *s)
  1081. {
  1082. apic_calibrate_pmtmr = 1;
  1083. notsc_setup(NULL);
  1084. return 0;
  1085. }
  1086. __setup("apicpmtimer", setup_apicpmtimer);