apic.c 29 KB

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