apic.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. value = apic_read(APIC_LVR);
  303. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  304. __error_in_apic_c();
  305. /*
  306. * Double-check whether this APIC is really registered.
  307. * This is meaningless in clustered apic mode, so we skip it.
  308. */
  309. if (!apic_id_registered())
  310. BUG();
  311. /*
  312. * Intel recommends to set DFR, LDR and TPR before enabling
  313. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  314. * document number 292116). So here it goes...
  315. */
  316. init_apic_ldr();
  317. /*
  318. * Set Task Priority to 'accept all'. We never change this
  319. * later on.
  320. */
  321. value = apic_read(APIC_TASKPRI);
  322. value &= ~APIC_TPRI_MASK;
  323. apic_write(APIC_TASKPRI, value);
  324. /*
  325. * Now that we are all set up, enable the APIC
  326. */
  327. value = apic_read(APIC_SPIV);
  328. value &= ~APIC_VECTOR_MASK;
  329. /*
  330. * Enable APIC
  331. */
  332. value |= APIC_SPIV_APIC_ENABLED;
  333. /*
  334. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  335. * certain networking cards. If high frequency interrupts are
  336. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  337. * entry is masked/unmasked at a high rate as well then sooner or
  338. * later IOAPIC line gets 'stuck', no more interrupts are received
  339. * from the device. If focus CPU is disabled then the hang goes
  340. * away, oh well :-(
  341. *
  342. * [ This bug can be reproduced easily with a level-triggered
  343. * PCI Ne2000 networking cards and PII/PIII processors, dual
  344. * BX chipset. ]
  345. */
  346. /*
  347. * Actually disabling the focus CPU check just makes the hang less
  348. * frequent as it makes the interrupt distributon model be more
  349. * like LRU than MRU (the short-term load is more even across CPUs).
  350. * See also the comment in end_level_ioapic_irq(). --macro
  351. */
  352. #if 1
  353. /* Enable focus processor (bit==0) */
  354. value &= ~APIC_SPIV_FOCUS_DISABLED;
  355. #else
  356. /* Disable focus processor (bit==1) */
  357. value |= APIC_SPIV_FOCUS_DISABLED;
  358. #endif
  359. /*
  360. * Set spurious IRQ vector
  361. */
  362. value |= SPURIOUS_APIC_VECTOR;
  363. apic_write(APIC_SPIV, value);
  364. /*
  365. * Set up LVT0, LVT1:
  366. *
  367. * set up through-local-APIC on the BP's LINT0. This is not
  368. * strictly necessary in pure symmetric-IO mode, but sometimes
  369. * we delegate interrupts to the 8259A.
  370. */
  371. /*
  372. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  373. */
  374. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  375. if (!smp_processor_id() && (pic_mode || !value)) {
  376. value = APIC_DM_EXTINT;
  377. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
  378. } else {
  379. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  380. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
  381. }
  382. apic_write(APIC_LVT0, value);
  383. /*
  384. * only the BP should see the LINT1 NMI signal, obviously.
  385. */
  386. if (!smp_processor_id())
  387. value = APIC_DM_NMI;
  388. else
  389. value = APIC_DM_NMI | APIC_LVT_MASKED;
  390. apic_write(APIC_LVT1, value);
  391. {
  392. unsigned oldvalue;
  393. maxlvt = get_maxlvt();
  394. oldvalue = apic_read(APIC_ESR);
  395. value = ERROR_APIC_VECTOR; // enables sending errors
  396. apic_write(APIC_LVTERR, value);
  397. /*
  398. * spec says clear errors after enabling vector.
  399. */
  400. if (maxlvt > 3)
  401. apic_write(APIC_ESR, 0);
  402. value = apic_read(APIC_ESR);
  403. if (value != oldvalue)
  404. apic_printk(APIC_VERBOSE,
  405. "ESR value after enabling vector: %08x, after %08x\n",
  406. oldvalue, value);
  407. }
  408. nmi_watchdog_default();
  409. if (nmi_watchdog == NMI_LOCAL_APIC)
  410. setup_apic_nmi_watchdog();
  411. apic_pm_activate();
  412. }
  413. #ifdef CONFIG_PM
  414. static struct {
  415. /* 'active' is true if the local APIC was enabled by us and
  416. not the BIOS; this signifies that we are also responsible
  417. for disabling it before entering apm/acpi suspend */
  418. int active;
  419. /* r/w apic fields */
  420. unsigned int apic_id;
  421. unsigned int apic_taskpri;
  422. unsigned int apic_ldr;
  423. unsigned int apic_dfr;
  424. unsigned int apic_spiv;
  425. unsigned int apic_lvtt;
  426. unsigned int apic_lvtpc;
  427. unsigned int apic_lvt0;
  428. unsigned int apic_lvt1;
  429. unsigned int apic_lvterr;
  430. unsigned int apic_tmict;
  431. unsigned int apic_tdcr;
  432. unsigned int apic_thmr;
  433. } apic_pm_state;
  434. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  435. {
  436. unsigned long flags;
  437. if (!apic_pm_state.active)
  438. return 0;
  439. apic_pm_state.apic_id = apic_read(APIC_ID);
  440. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  441. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  442. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  443. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  444. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  445. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  446. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  447. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  448. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  449. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  450. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  451. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  452. local_save_flags(flags);
  453. local_irq_disable();
  454. disable_local_APIC();
  455. local_irq_restore(flags);
  456. return 0;
  457. }
  458. static int lapic_resume(struct sys_device *dev)
  459. {
  460. unsigned int l, h;
  461. unsigned long flags;
  462. if (!apic_pm_state.active)
  463. return 0;
  464. local_irq_save(flags);
  465. rdmsr(MSR_IA32_APICBASE, l, h);
  466. l &= ~MSR_IA32_APICBASE_BASE;
  467. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  468. wrmsr(MSR_IA32_APICBASE, l, h);
  469. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  470. apic_write(APIC_ID, apic_pm_state.apic_id);
  471. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  472. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  473. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  474. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  475. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  476. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  477. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  478. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  479. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  480. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  481. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  482. apic_write(APIC_ESR, 0);
  483. apic_read(APIC_ESR);
  484. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  485. apic_write(APIC_ESR, 0);
  486. apic_read(APIC_ESR);
  487. local_irq_restore(flags);
  488. return 0;
  489. }
  490. static struct sysdev_class lapic_sysclass = {
  491. set_kset_name("lapic"),
  492. .resume = lapic_resume,
  493. .suspend = lapic_suspend,
  494. };
  495. static struct sys_device device_lapic = {
  496. .id = 0,
  497. .cls = &lapic_sysclass,
  498. };
  499. static void __cpuinit apic_pm_activate(void)
  500. {
  501. apic_pm_state.active = 1;
  502. }
  503. static int __init init_lapic_sysfs(void)
  504. {
  505. int error;
  506. if (!cpu_has_apic)
  507. return 0;
  508. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  509. error = sysdev_class_register(&lapic_sysclass);
  510. if (!error)
  511. error = sysdev_register(&device_lapic);
  512. return error;
  513. }
  514. device_initcall(init_lapic_sysfs);
  515. #else /* CONFIG_PM */
  516. static void apic_pm_activate(void) { }
  517. #endif /* CONFIG_PM */
  518. static int __init apic_set_verbosity(char *str)
  519. {
  520. if (strcmp("debug", str) == 0)
  521. apic_verbosity = APIC_DEBUG;
  522. else if (strcmp("verbose", str) == 0)
  523. apic_verbosity = APIC_VERBOSE;
  524. else
  525. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  526. " use apic=verbose or apic=debug", str);
  527. return 0;
  528. }
  529. __setup("apic=", apic_set_verbosity);
  530. /*
  531. * Detect and enable local APICs on non-SMP boards.
  532. * Original code written by Keir Fraser.
  533. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  534. * not correctly set up (usually the APIC timer won't work etc.)
  535. */
  536. static int __init detect_init_APIC (void)
  537. {
  538. if (!cpu_has_apic) {
  539. printk(KERN_INFO "No local APIC present\n");
  540. return -1;
  541. }
  542. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  543. boot_cpu_id = 0;
  544. return 0;
  545. }
  546. void __init init_apic_mappings(void)
  547. {
  548. unsigned long apic_phys;
  549. /*
  550. * If no local APIC can be found then set up a fake all
  551. * zeroes page to simulate the local APIC and another
  552. * one for the IO-APIC.
  553. */
  554. if (!smp_found_config && detect_init_APIC()) {
  555. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  556. apic_phys = __pa(apic_phys);
  557. } else
  558. apic_phys = mp_lapic_addr;
  559. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  560. apic_printk(APIC_VERBOSE,"mapped APIC to %16lx (%16lx)\n", APIC_BASE, apic_phys);
  561. /*
  562. * Fetch the APIC ID of the BSP in case we have a
  563. * default configuration (or the MP table is broken).
  564. */
  565. boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
  566. #ifdef CONFIG_X86_IO_APIC
  567. {
  568. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  569. int i;
  570. for (i = 0; i < nr_ioapics; i++) {
  571. if (smp_found_config) {
  572. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  573. } else {
  574. ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  575. ioapic_phys = __pa(ioapic_phys);
  576. }
  577. set_fixmap_nocache(idx, ioapic_phys);
  578. apic_printk(APIC_VERBOSE,"mapped IOAPIC to %016lx (%016lx)\n",
  579. __fix_to_virt(idx), ioapic_phys);
  580. idx++;
  581. }
  582. }
  583. #endif
  584. }
  585. /*
  586. * This function sets up the local APIC timer, with a timeout of
  587. * 'clocks' APIC bus clock. During calibration we actually call
  588. * this function twice on the boot CPU, once with a bogus timeout
  589. * value, second time for real. The other (noncalibrating) CPUs
  590. * call this function only once, with the real, calibrated value.
  591. *
  592. * We do reads before writes even if unnecessary, to get around the
  593. * P5 APIC double write bug.
  594. */
  595. #define APIC_DIVISOR 16
  596. static void __setup_APIC_LVTT(unsigned int clocks)
  597. {
  598. unsigned int lvtt_value, tmp_value, ver;
  599. int cpu = smp_processor_id();
  600. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  601. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  602. if (cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask))
  603. lvtt_value |= APIC_LVT_MASKED;
  604. apic_write(APIC_LVTT, lvtt_value);
  605. /*
  606. * Divide PICLK by 16
  607. */
  608. tmp_value = apic_read(APIC_TDCR);
  609. apic_write(APIC_TDCR, (tmp_value
  610. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  611. | APIC_TDR_DIV_16);
  612. apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
  613. }
  614. static void setup_APIC_timer(unsigned int clocks)
  615. {
  616. unsigned long flags;
  617. local_irq_save(flags);
  618. /* wait for irq slice */
  619. if (vxtime.hpet_address) {
  620. int trigger = hpet_readl(HPET_T0_CMP);
  621. while (hpet_readl(HPET_COUNTER) >= trigger)
  622. /* do nothing */ ;
  623. while (hpet_readl(HPET_COUNTER) < trigger)
  624. /* do nothing */ ;
  625. } else {
  626. int c1, c2;
  627. outb_p(0x00, 0x43);
  628. c2 = inb_p(0x40);
  629. c2 |= inb_p(0x40) << 8;
  630. do {
  631. c1 = c2;
  632. outb_p(0x00, 0x43);
  633. c2 = inb_p(0x40);
  634. c2 |= inb_p(0x40) << 8;
  635. } while (c2 - c1 < 300);
  636. }
  637. __setup_APIC_LVTT(clocks);
  638. /* Turn off PIT interrupt if we use APIC timer as main timer.
  639. Only works with the PM timer right now
  640. TBD fix it for HPET too. */
  641. if (vxtime.mode == VXTIME_PMTMR &&
  642. smp_processor_id() == boot_cpu_id &&
  643. apic_runs_main_timer == 1 &&
  644. !cpu_isset(boot_cpu_id, timer_interrupt_broadcast_ipi_mask)) {
  645. stop_timer_interrupt();
  646. apic_runs_main_timer++;
  647. }
  648. local_irq_restore(flags);
  649. }
  650. /*
  651. * In this function we calibrate APIC bus clocks to the external
  652. * timer. Unfortunately we cannot use jiffies and the timer irq
  653. * to calibrate, since some later bootup code depends on getting
  654. * the first irq? Ugh.
  655. *
  656. * We want to do the calibration only once since we
  657. * want to have local timer irqs syncron. CPUs connected
  658. * by the same APIC bus have the very same bus frequency.
  659. * And we want to have irqs off anyways, no accidental
  660. * APIC irq that way.
  661. */
  662. #define TICK_COUNT 100000000
  663. static int __init calibrate_APIC_clock(void)
  664. {
  665. int apic, apic_start, tsc, tsc_start;
  666. int result;
  667. /*
  668. * Put whatever arbitrary (but long enough) timeout
  669. * value into the APIC clock, we just want to get the
  670. * counter running for calibration.
  671. */
  672. __setup_APIC_LVTT(1000000000);
  673. apic_start = apic_read(APIC_TMCCT);
  674. #ifdef CONFIG_X86_PM_TIMER
  675. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  676. pmtimer_wait(5000); /* 5ms wait */
  677. apic = apic_read(APIC_TMCCT);
  678. result = (apic_start - apic) * 1000L / 5;
  679. } else
  680. #endif
  681. {
  682. rdtscl(tsc_start);
  683. do {
  684. apic = apic_read(APIC_TMCCT);
  685. rdtscl(tsc);
  686. } while ((tsc - tsc_start) < TICK_COUNT &&
  687. (apic - apic_start) < TICK_COUNT);
  688. result = (apic_start - apic) * 1000L * cpu_khz /
  689. (tsc - tsc_start);
  690. }
  691. printk("result %d\n", result);
  692. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  693. result / 1000 / 1000, result / 1000 % 1000);
  694. return result * APIC_DIVISOR / HZ;
  695. }
  696. static unsigned int calibration_result;
  697. void __init setup_boot_APIC_clock (void)
  698. {
  699. if (disable_apic_timer) {
  700. printk(KERN_INFO "Disabling APIC timer\n");
  701. return;
  702. }
  703. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  704. using_apic_timer = 1;
  705. local_irq_disable();
  706. calibration_result = calibrate_APIC_clock();
  707. /*
  708. * Now set up the timer for real.
  709. */
  710. setup_APIC_timer(calibration_result);
  711. local_irq_enable();
  712. }
  713. void __cpuinit setup_secondary_APIC_clock(void)
  714. {
  715. local_irq_disable(); /* FIXME: Do we need this? --RR */
  716. setup_APIC_timer(calibration_result);
  717. local_irq_enable();
  718. }
  719. void disable_APIC_timer(void)
  720. {
  721. if (using_apic_timer) {
  722. unsigned long v;
  723. v = apic_read(APIC_LVTT);
  724. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  725. }
  726. }
  727. void enable_APIC_timer(void)
  728. {
  729. int cpu = smp_processor_id();
  730. if (using_apic_timer &&
  731. !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  732. unsigned long v;
  733. v = apic_read(APIC_LVTT);
  734. apic_write(APIC_LVTT, v & ~APIC_LVT_MASKED);
  735. }
  736. }
  737. void switch_APIC_timer_to_ipi(void *cpumask)
  738. {
  739. cpumask_t mask = *(cpumask_t *)cpumask;
  740. int cpu = smp_processor_id();
  741. if (cpu_isset(cpu, mask) &&
  742. !cpu_isset(cpu, timer_interrupt_broadcast_ipi_mask)) {
  743. disable_APIC_timer();
  744. cpu_set(cpu, timer_interrupt_broadcast_ipi_mask);
  745. }
  746. }
  747. EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
  748. void smp_send_timer_broadcast_ipi(void)
  749. {
  750. cpumask_t mask;
  751. cpus_and(mask, cpu_online_map, timer_interrupt_broadcast_ipi_mask);
  752. if (!cpus_empty(mask)) {
  753. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  754. }
  755. }
  756. void switch_ipi_to_APIC_timer(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. cpu_clear(cpu, timer_interrupt_broadcast_ipi_mask);
  763. enable_APIC_timer();
  764. }
  765. }
  766. EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
  767. int setup_profiling_timer(unsigned int multiplier)
  768. {
  769. return -EINVAL;
  770. }
  771. #ifdef CONFIG_X86_MCE_AMD
  772. void setup_threshold_lvt(unsigned long lvt_off)
  773. {
  774. unsigned int v = 0;
  775. unsigned long reg = (lvt_off << 4) + 0x500;
  776. v |= THRESHOLD_APIC_VECTOR;
  777. apic_write(reg, v);
  778. }
  779. #endif /* CONFIG_X86_MCE_AMD */
  780. #undef APIC_DIVISOR
  781. /*
  782. * Local timer interrupt handler. It does both profiling and
  783. * process statistics/rescheduling.
  784. *
  785. * We do profiling in every local tick, statistics/rescheduling
  786. * happen only every 'profiling multiplier' ticks. The default
  787. * multiplier is 1 and it can be changed by writing the new multiplier
  788. * value into /proc/profile.
  789. */
  790. void smp_local_timer_interrupt(struct pt_regs *regs)
  791. {
  792. profile_tick(CPU_PROFILING, regs);
  793. #ifdef CONFIG_SMP
  794. update_process_times(user_mode(regs));
  795. #endif
  796. if (apic_runs_main_timer > 1 && smp_processor_id() == boot_cpu_id)
  797. main_timer_handler(regs);
  798. /*
  799. * We take the 'long' return path, and there every subsystem
  800. * grabs the appropriate locks (kernel lock/ irq lock).
  801. *
  802. * we might want to decouple profiling from the 'long path',
  803. * and do the profiling totally in assembly.
  804. *
  805. * Currently this isn't too much of an issue (performance wise),
  806. * we can take more than 100K local irqs per second on a 100 MHz P5.
  807. */
  808. }
  809. /*
  810. * Local APIC timer interrupt. This is the most natural way for doing
  811. * local interrupts, but local timer interrupts can be emulated by
  812. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  813. *
  814. * [ if a single-CPU system runs an SMP kernel then we call the local
  815. * interrupt as well. Thus we cannot inline the local irq ... ]
  816. */
  817. void smp_apic_timer_interrupt(struct pt_regs *regs)
  818. {
  819. /*
  820. * the NMI deadlock-detector uses this.
  821. */
  822. add_pda(apic_timer_irqs, 1);
  823. /*
  824. * NOTE! We'd better ACK the irq immediately,
  825. * because timer handling can be slow.
  826. */
  827. ack_APIC_irq();
  828. /*
  829. * update_process_times() expects us to have done irq_enter().
  830. * Besides, if we don't timer interrupts ignore the global
  831. * interrupt lock, which is the WrongThing (tm) to do.
  832. */
  833. exit_idle();
  834. irq_enter();
  835. smp_local_timer_interrupt(regs);
  836. irq_exit();
  837. }
  838. /*
  839. * oem_force_hpet_timer -- force HPET mode for some boxes.
  840. *
  841. * Thus far, the major user of this is IBM's Summit2 series:
  842. *
  843. * Clustered boxes may have unsynced TSC problems if they are
  844. * multi-chassis. Use available data to take a good guess.
  845. * If in doubt, go HPET.
  846. */
  847. __cpuinit int oem_force_hpet_timer(void)
  848. {
  849. int i, clusters, zeros;
  850. unsigned id;
  851. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  852. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  853. for (i = 0; i < NR_CPUS; i++) {
  854. id = bios_cpu_apicid[i];
  855. if (id != BAD_APICID)
  856. __set_bit(APIC_CLUSTERID(id), clustermap);
  857. }
  858. /* Problem: Partially populated chassis may not have CPUs in some of
  859. * the APIC clusters they have been allocated. Only present CPUs have
  860. * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
  861. * clusters are allocated sequentially, count zeros only if they are
  862. * bounded by ones.
  863. */
  864. clusters = 0;
  865. zeros = 0;
  866. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  867. if (test_bit(i, clustermap)) {
  868. clusters += 1 + zeros;
  869. zeros = 0;
  870. } else
  871. ++zeros;
  872. }
  873. /*
  874. * If clusters > 2, then should be multi-chassis. Return 1 for HPET.
  875. * Else return 0 to use TSC.
  876. * May have to revisit this when multi-core + hyperthreaded CPUs come
  877. * out, but AFAIK this will work even for them.
  878. */
  879. return (clusters > 2);
  880. }
  881. /*
  882. * This interrupt should _never_ happen with our APIC/SMP architecture
  883. */
  884. asmlinkage void smp_spurious_interrupt(void)
  885. {
  886. unsigned int v;
  887. exit_idle();
  888. irq_enter();
  889. /*
  890. * Check if this really is a spurious interrupt and ACK it
  891. * if it is a vectored one. Just in case...
  892. * Spurious interrupts should not be ACKed.
  893. */
  894. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  895. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  896. ack_APIC_irq();
  897. #if 0
  898. static unsigned long last_warning;
  899. static unsigned long skipped;
  900. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  901. if (time_before(last_warning+30*HZ,jiffies)) {
  902. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
  903. smp_processor_id(), skipped);
  904. last_warning = jiffies;
  905. skipped = 0;
  906. } else {
  907. skipped++;
  908. }
  909. #endif
  910. irq_exit();
  911. }
  912. /*
  913. * This interrupt should never happen with our APIC/SMP architecture
  914. */
  915. asmlinkage void smp_error_interrupt(void)
  916. {
  917. unsigned int v, v1;
  918. exit_idle();
  919. irq_enter();
  920. /* First tickle the hardware, only then report what went on. -- REW */
  921. v = apic_read(APIC_ESR);
  922. apic_write(APIC_ESR, 0);
  923. v1 = apic_read(APIC_ESR);
  924. ack_APIC_irq();
  925. atomic_inc(&irq_err_count);
  926. /* Here is what the APIC error bits mean:
  927. 0: Send CS error
  928. 1: Receive CS error
  929. 2: Send accept error
  930. 3: Receive accept error
  931. 4: Reserved
  932. 5: Send illegal vector
  933. 6: Received illegal vector
  934. 7: Illegal register address
  935. */
  936. printk (KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  937. smp_processor_id(), v , v1);
  938. irq_exit();
  939. }
  940. int disable_apic;
  941. /*
  942. * This initializes the IO-APIC and APIC hardware if this is
  943. * a UP kernel.
  944. */
  945. int __init APIC_init_uniprocessor (void)
  946. {
  947. if (disable_apic) {
  948. printk(KERN_INFO "Apic disabled\n");
  949. return -1;
  950. }
  951. if (!cpu_has_apic) {
  952. disable_apic = 1;
  953. printk(KERN_INFO "Apic disabled by BIOS\n");
  954. return -1;
  955. }
  956. verify_local_APIC();
  957. connect_bsp_APIC();
  958. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  959. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
  960. setup_local_APIC();
  961. #ifdef CONFIG_X86_IO_APIC
  962. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  963. setup_IO_APIC();
  964. else
  965. nr_ioapics = 0;
  966. #endif
  967. setup_boot_APIC_clock();
  968. check_nmi_watchdog();
  969. return 0;
  970. }
  971. static __init int setup_disableapic(char *str)
  972. {
  973. disable_apic = 1;
  974. return 0;
  975. }
  976. static __init int setup_nolapic(char *str)
  977. {
  978. disable_apic = 1;
  979. return 0;
  980. }
  981. static __init int setup_noapictimer(char *str)
  982. {
  983. if (str[0] != ' ' && str[0] != 0)
  984. return -1;
  985. disable_apic_timer = 1;
  986. return 0;
  987. }
  988. static __init int setup_apicmaintimer(char *str)
  989. {
  990. apic_runs_main_timer = 1;
  991. nohpet = 1;
  992. return 0;
  993. }
  994. __setup("apicmaintimer", setup_apicmaintimer);
  995. static __init int setup_noapicmaintimer(char *str)
  996. {
  997. apic_runs_main_timer = -1;
  998. return 0;
  999. }
  1000. __setup("noapicmaintimer", setup_noapicmaintimer);
  1001. static __init int setup_apicpmtimer(char *s)
  1002. {
  1003. apic_calibrate_pmtmr = 1;
  1004. return setup_apicmaintimer(NULL);
  1005. }
  1006. __setup("apicpmtimer", setup_apicpmtimer);
  1007. /* dummy parsing: see setup.c */
  1008. __setup("disableapic", setup_disableapic);
  1009. __setup("nolapic", setup_nolapic); /* same as disableapic, for compatibility */
  1010. __setup("noapictimer", setup_noapictimer);
  1011. /* no "lapic" flag - we only use the lapic when the BIOS tells us so. */