apic.c 27 KB

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