apic.c 27 KB

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