apic.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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 <linux/cpu.h>
  28. #include <asm/atomic.h>
  29. #include <asm/smp.h>
  30. #include <asm/mtrr.h>
  31. #include <asm/mpspec.h>
  32. #include <asm/desc.h>
  33. #include <asm/arch_hooks.h>
  34. #include <asm/hpet.h>
  35. #include <mach_apic.h>
  36. #include "io_ports.h"
  37. /*
  38. * Debug level
  39. */
  40. int apic_verbosity;
  41. static void apic_pm_activate(void);
  42. /*
  43. * 'what should we do if we get a hw irq event on an illegal vector'.
  44. * each architecture has to answer this themselves.
  45. */
  46. void ack_bad_irq(unsigned int irq)
  47. {
  48. printk("unexpected IRQ trap at vector %02x\n", irq);
  49. /*
  50. * Currently unexpected vectors happen only on SMP and APIC.
  51. * We _must_ ack these because every local APIC has only N
  52. * irq slots per priority level, and a 'hanging, unacked' IRQ
  53. * holds up an irq slot - in excessive cases (when multiple
  54. * unexpected vectors occur) that might lock up the APIC
  55. * completely.
  56. */
  57. ack_APIC_irq();
  58. }
  59. void __init apic_intr_init(void)
  60. {
  61. #ifdef CONFIG_SMP
  62. smp_intr_init();
  63. #endif
  64. /* self generated IPI for local APIC timer */
  65. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  66. /* IPI vectors for APIC spurious and error interrupts */
  67. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  68. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  69. /* thermal monitor LVT interrupt */
  70. #ifdef CONFIG_X86_MCE_P4THERMAL
  71. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  72. #endif
  73. }
  74. /* Using APIC to generate smp_local_timer_interrupt? */
  75. int using_apic_timer = 0;
  76. static DEFINE_PER_CPU(int, prof_multiplier) = 1;
  77. static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
  78. static DEFINE_PER_CPU(int, prof_counter) = 1;
  79. static int enabled_via_apicbase;
  80. void enable_NMI_through_LVT0 (void * dummy)
  81. {
  82. unsigned int v, ver;
  83. ver = apic_read(APIC_LVR);
  84. ver = GET_APIC_VERSION(ver);
  85. v = APIC_DM_NMI; /* unmask and set to NMI */
  86. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  87. v |= APIC_LVT_LEVEL_TRIGGER;
  88. apic_write_around(APIC_LVT0, v);
  89. }
  90. int get_physical_broadcast(void)
  91. {
  92. unsigned int lvr, version;
  93. lvr = apic_read(APIC_LVR);
  94. version = GET_APIC_VERSION(lvr);
  95. if (!APIC_INTEGRATED(version) || version >= 0x14)
  96. return 0xff;
  97. else
  98. return 0xf;
  99. }
  100. int get_maxlvt(void)
  101. {
  102. unsigned int v, ver, maxlvt;
  103. v = apic_read(APIC_LVR);
  104. ver = GET_APIC_VERSION(v);
  105. /* 82489DXs do not report # of LVT entries. */
  106. maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
  107. return maxlvt;
  108. }
  109. void clear_local_APIC(void)
  110. {
  111. int maxlvt;
  112. unsigned long v;
  113. maxlvt = get_maxlvt();
  114. /*
  115. * Masking an LVT entry on a P6 can trigger a local APIC error
  116. * if the vector is zero. Mask LVTERR first to prevent this.
  117. */
  118. if (maxlvt >= 3) {
  119. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  120. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  121. }
  122. /*
  123. * Careful: we have to set masks only first to deassert
  124. * any level-triggered sources.
  125. */
  126. v = apic_read(APIC_LVTT);
  127. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  128. v = apic_read(APIC_LVT0);
  129. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  130. v = apic_read(APIC_LVT1);
  131. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  132. if (maxlvt >= 4) {
  133. v = apic_read(APIC_LVTPC);
  134. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  135. }
  136. /* lets not touch this if we didn't frob it */
  137. #ifdef CONFIG_X86_MCE_P4THERMAL
  138. if (maxlvt >= 5) {
  139. v = apic_read(APIC_LVTTHMR);
  140. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  141. }
  142. #endif
  143. /*
  144. * Clean APIC state for other OSs:
  145. */
  146. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  147. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  148. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  149. if (maxlvt >= 3)
  150. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  151. if (maxlvt >= 4)
  152. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  153. #ifdef CONFIG_X86_MCE_P4THERMAL
  154. if (maxlvt >= 5)
  155. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  156. #endif
  157. v = GET_APIC_VERSION(apic_read(APIC_LVR));
  158. if (APIC_INTEGRATED(v)) { /* !82489DX */
  159. if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
  160. apic_write(APIC_ESR, 0);
  161. apic_read(APIC_ESR);
  162. }
  163. }
  164. void __init connect_bsp_APIC(void)
  165. {
  166. if (pic_mode) {
  167. /*
  168. * Do not trust the local APIC being empty at bootup.
  169. */
  170. clear_local_APIC();
  171. /*
  172. * PIC mode, enable APIC mode in the IMCR, i.e.
  173. * connect BSP's local APIC to INT and NMI lines.
  174. */
  175. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  176. "enabling APIC mode.\n");
  177. outb(0x70, 0x22);
  178. outb(0x01, 0x23);
  179. }
  180. enable_apic_mode();
  181. }
  182. void disconnect_bsp_APIC(void)
  183. {
  184. if (pic_mode) {
  185. /*
  186. * Put the board back into PIC mode (has an effect
  187. * only on certain older boards). Note that APIC
  188. * interrupts, including IPIs, won't work beyond
  189. * this point! The only exception are INIT IPIs.
  190. */
  191. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  192. "entering PIC mode.\n");
  193. outb(0x70, 0x22);
  194. outb(0x00, 0x23);
  195. }
  196. }
  197. void disable_local_APIC(void)
  198. {
  199. unsigned long value;
  200. clear_local_APIC();
  201. /*
  202. * Disable APIC (implies clearing of registers
  203. * for 82489DX!).
  204. */
  205. value = apic_read(APIC_SPIV);
  206. value &= ~APIC_SPIV_APIC_ENABLED;
  207. apic_write_around(APIC_SPIV, value);
  208. if (enabled_via_apicbase) {
  209. unsigned int l, h;
  210. rdmsr(MSR_IA32_APICBASE, l, h);
  211. l &= ~MSR_IA32_APICBASE_ENABLE;
  212. wrmsr(MSR_IA32_APICBASE, l, h);
  213. }
  214. }
  215. /*
  216. * This is to verify that we're looking at a real local APIC.
  217. * Check these against your board if the CPUs aren't getting
  218. * started for no apparent reason.
  219. */
  220. int __init verify_local_APIC(void)
  221. {
  222. unsigned int reg0, reg1;
  223. /*
  224. * The version register is read-only in a real APIC.
  225. */
  226. reg0 = apic_read(APIC_LVR);
  227. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  228. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  229. reg1 = apic_read(APIC_LVR);
  230. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  231. /*
  232. * The two version reads above should print the same
  233. * numbers. If the second one is different, then we
  234. * poke at a non-APIC.
  235. */
  236. if (reg1 != reg0)
  237. return 0;
  238. /*
  239. * Check if the version looks reasonably.
  240. */
  241. reg1 = GET_APIC_VERSION(reg0);
  242. if (reg1 == 0x00 || reg1 == 0xff)
  243. return 0;
  244. reg1 = get_maxlvt();
  245. if (reg1 < 0x02 || reg1 == 0xff)
  246. return 0;
  247. /*
  248. * The ID register is read/write in a real APIC.
  249. */
  250. reg0 = apic_read(APIC_ID);
  251. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  252. /*
  253. * The next two are just to see if we have sane values.
  254. * They're only really relevant if we're in Virtual Wire
  255. * compatibility mode, but most boxes are anymore.
  256. */
  257. reg0 = apic_read(APIC_LVT0);
  258. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  259. reg1 = apic_read(APIC_LVT1);
  260. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  261. return 1;
  262. }
  263. void __init sync_Arb_IDs(void)
  264. {
  265. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  266. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  267. if (ver >= 0x14) /* P4 or higher */
  268. return;
  269. /*
  270. * Wait for idle.
  271. */
  272. apic_wait_icr_idle();
  273. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  274. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  275. | APIC_DM_INIT);
  276. }
  277. extern void __error_in_apic_c (void);
  278. /*
  279. * An initial setup of the virtual wire mode.
  280. */
  281. void __init init_bsp_APIC(void)
  282. {
  283. unsigned long value, ver;
  284. /*
  285. * Don't do the setup now if we have a SMP BIOS as the
  286. * through-I/O-APIC virtual wire mode might be active.
  287. */
  288. if (smp_found_config || !cpu_has_apic)
  289. return;
  290. value = apic_read(APIC_LVR);
  291. ver = GET_APIC_VERSION(value);
  292. /*
  293. * Do not trust the local APIC being empty at bootup.
  294. */
  295. clear_local_APIC();
  296. /*
  297. * Enable APIC.
  298. */
  299. value = apic_read(APIC_SPIV);
  300. value &= ~APIC_VECTOR_MASK;
  301. value |= APIC_SPIV_APIC_ENABLED;
  302. /* This bit is reserved on P4/Xeon and should be cleared */
  303. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
  304. value &= ~APIC_SPIV_FOCUS_DISABLED;
  305. else
  306. value |= APIC_SPIV_FOCUS_DISABLED;
  307. value |= SPURIOUS_APIC_VECTOR;
  308. apic_write_around(APIC_SPIV, value);
  309. /*
  310. * Set up the virtual wire mode.
  311. */
  312. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  313. value = APIC_DM_NMI;
  314. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  315. value |= APIC_LVT_LEVEL_TRIGGER;
  316. apic_write_around(APIC_LVT1, value);
  317. }
  318. void __init setup_local_APIC (void)
  319. {
  320. unsigned long oldvalue, value, ver, maxlvt;
  321. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  322. if (esr_disable) {
  323. apic_write(APIC_ESR, 0);
  324. apic_write(APIC_ESR, 0);
  325. apic_write(APIC_ESR, 0);
  326. apic_write(APIC_ESR, 0);
  327. }
  328. value = apic_read(APIC_LVR);
  329. ver = GET_APIC_VERSION(value);
  330. if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
  331. __error_in_apic_c();
  332. /*
  333. * Double-check whether this APIC is really registered.
  334. */
  335. if (!apic_id_registered())
  336. BUG();
  337. /*
  338. * Intel recommends to set DFR, LDR and TPR before enabling
  339. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  340. * document number 292116). So here it goes...
  341. */
  342. init_apic_ldr();
  343. /*
  344. * Set Task Priority to 'accept all'. We never change this
  345. * later on.
  346. */
  347. value = apic_read(APIC_TASKPRI);
  348. value &= ~APIC_TPRI_MASK;
  349. apic_write_around(APIC_TASKPRI, value);
  350. /*
  351. * Now that we are all set up, enable the APIC
  352. */
  353. value = apic_read(APIC_SPIV);
  354. value &= ~APIC_VECTOR_MASK;
  355. /*
  356. * Enable APIC
  357. */
  358. value |= APIC_SPIV_APIC_ENABLED;
  359. /*
  360. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  361. * certain networking cards. If high frequency interrupts are
  362. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  363. * entry is masked/unmasked at a high rate as well then sooner or
  364. * later IOAPIC line gets 'stuck', no more interrupts are received
  365. * from the device. If focus CPU is disabled then the hang goes
  366. * away, oh well :-(
  367. *
  368. * [ This bug can be reproduced easily with a level-triggered
  369. * PCI Ne2000 networking cards and PII/PIII processors, dual
  370. * BX chipset. ]
  371. */
  372. /*
  373. * Actually disabling the focus CPU check just makes the hang less
  374. * frequent as it makes the interrupt distributon model be more
  375. * like LRU than MRU (the short-term load is more even across CPUs).
  376. * See also the comment in end_level_ioapic_irq(). --macro
  377. */
  378. #if 1
  379. /* Enable focus processor (bit==0) */
  380. value &= ~APIC_SPIV_FOCUS_DISABLED;
  381. #else
  382. /* Disable focus processor (bit==1) */
  383. value |= APIC_SPIV_FOCUS_DISABLED;
  384. #endif
  385. /*
  386. * Set spurious IRQ vector
  387. */
  388. value |= SPURIOUS_APIC_VECTOR;
  389. apic_write_around(APIC_SPIV, value);
  390. /*
  391. * Set up LVT0, LVT1:
  392. *
  393. * set up through-local-APIC on the BP's LINT0. This is not
  394. * strictly necessery in pure symmetric-IO mode, but sometimes
  395. * we delegate interrupts to the 8259A.
  396. */
  397. /*
  398. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  399. */
  400. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  401. if (!smp_processor_id() && (pic_mode || !value)) {
  402. value = APIC_DM_EXTINT;
  403. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  404. smp_processor_id());
  405. } else {
  406. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  407. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  408. smp_processor_id());
  409. }
  410. apic_write_around(APIC_LVT0, value);
  411. /*
  412. * only the BP should see the LINT1 NMI signal, obviously.
  413. */
  414. if (!smp_processor_id())
  415. value = APIC_DM_NMI;
  416. else
  417. value = APIC_DM_NMI | APIC_LVT_MASKED;
  418. if (!APIC_INTEGRATED(ver)) /* 82489DX */
  419. value |= APIC_LVT_LEVEL_TRIGGER;
  420. apic_write_around(APIC_LVT1, value);
  421. if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
  422. maxlvt = get_maxlvt();
  423. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  424. apic_write(APIC_ESR, 0);
  425. oldvalue = apic_read(APIC_ESR);
  426. value = ERROR_APIC_VECTOR; // enables sending errors
  427. apic_write_around(APIC_LVTERR, value);
  428. /*
  429. * spec says clear errors after enabling vector.
  430. */
  431. if (maxlvt > 3)
  432. apic_write(APIC_ESR, 0);
  433. value = apic_read(APIC_ESR);
  434. if (value != oldvalue)
  435. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  436. "vector: 0x%08lx after: 0x%08lx\n",
  437. oldvalue, value);
  438. } else {
  439. if (esr_disable)
  440. /*
  441. * Something untraceble is creating bad interrupts on
  442. * secondary quads ... for the moment, just leave the
  443. * ESR disabled - we can't do anything useful with the
  444. * errors anyway - mbligh
  445. */
  446. printk("Leaving ESR disabled.\n");
  447. else
  448. printk("No ESR for 82489DX.\n");
  449. }
  450. if (nmi_watchdog == NMI_LOCAL_APIC)
  451. setup_apic_nmi_watchdog();
  452. apic_pm_activate();
  453. }
  454. /*
  455. * If Linux enabled the LAPIC against the BIOS default
  456. * disable it down before re-entering the BIOS on shutdown.
  457. * Otherwise the BIOS may get confused and not power-off.
  458. */
  459. void lapic_shutdown(void)
  460. {
  461. if (!cpu_has_apic || !enabled_via_apicbase)
  462. return;
  463. local_irq_disable();
  464. disable_local_APIC();
  465. local_irq_enable();
  466. }
  467. #ifdef CONFIG_PM
  468. static struct {
  469. int active;
  470. /* r/w apic fields */
  471. unsigned int apic_id;
  472. unsigned int apic_taskpri;
  473. unsigned int apic_ldr;
  474. unsigned int apic_dfr;
  475. unsigned int apic_spiv;
  476. unsigned int apic_lvtt;
  477. unsigned int apic_lvtpc;
  478. unsigned int apic_lvt0;
  479. unsigned int apic_lvt1;
  480. unsigned int apic_lvterr;
  481. unsigned int apic_tmict;
  482. unsigned int apic_tdcr;
  483. unsigned int apic_thmr;
  484. } apic_pm_state;
  485. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  486. {
  487. unsigned long flags;
  488. if (!apic_pm_state.active)
  489. return 0;
  490. apic_pm_state.apic_id = apic_read(APIC_ID);
  491. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  492. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  493. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  494. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  495. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  496. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  497. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  498. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  499. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  500. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  501. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  502. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  503. local_irq_save(flags);
  504. disable_local_APIC();
  505. local_irq_restore(flags);
  506. return 0;
  507. }
  508. static int lapic_resume(struct sys_device *dev)
  509. {
  510. unsigned int l, h;
  511. unsigned long flags;
  512. if (!apic_pm_state.active)
  513. return 0;
  514. local_irq_save(flags);
  515. /*
  516. * Make sure the APICBASE points to the right address
  517. *
  518. * FIXME! This will be wrong if we ever support suspend on
  519. * SMP! We'll need to do this as part of the CPU restore!
  520. */
  521. rdmsr(MSR_IA32_APICBASE, l, h);
  522. l &= ~MSR_IA32_APICBASE_BASE;
  523. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  524. wrmsr(MSR_IA32_APICBASE, l, h);
  525. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  526. apic_write(APIC_ID, apic_pm_state.apic_id);
  527. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  528. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  529. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  530. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  531. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  532. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  533. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  534. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  535. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  536. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  537. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  538. apic_write(APIC_ESR, 0);
  539. apic_read(APIC_ESR);
  540. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  541. apic_write(APIC_ESR, 0);
  542. apic_read(APIC_ESR);
  543. local_irq_restore(flags);
  544. return 0;
  545. }
  546. /*
  547. * This device has no shutdown method - fully functioning local APICs
  548. * are needed on every CPU up until machine_halt/restart/poweroff.
  549. */
  550. static struct sysdev_class lapic_sysclass = {
  551. set_kset_name("lapic"),
  552. .resume = lapic_resume,
  553. .suspend = lapic_suspend,
  554. };
  555. static struct sys_device device_lapic = {
  556. .id = 0,
  557. .cls = &lapic_sysclass,
  558. };
  559. static void __init apic_pm_activate(void)
  560. {
  561. apic_pm_state.active = 1;
  562. }
  563. static int __init init_lapic_sysfs(void)
  564. {
  565. int error;
  566. if (!cpu_has_apic)
  567. return 0;
  568. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  569. error = sysdev_class_register(&lapic_sysclass);
  570. if (!error)
  571. error = sysdev_register(&device_lapic);
  572. return error;
  573. }
  574. device_initcall(init_lapic_sysfs);
  575. #else /* CONFIG_PM */
  576. static void apic_pm_activate(void) { }
  577. #endif /* CONFIG_PM */
  578. /*
  579. * Detect and enable local APICs on non-SMP boards.
  580. * Original code written by Keir Fraser.
  581. */
  582. /*
  583. * Knob to control our willingness to enable the local APIC.
  584. */
  585. int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
  586. static int __init lapic_disable(char *str)
  587. {
  588. enable_local_apic = -1;
  589. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  590. return 0;
  591. }
  592. __setup("nolapic", lapic_disable);
  593. static int __init lapic_enable(char *str)
  594. {
  595. enable_local_apic = 1;
  596. return 0;
  597. }
  598. __setup("lapic", lapic_enable);
  599. static int __init apic_set_verbosity(char *str)
  600. {
  601. if (strcmp("debug", str) == 0)
  602. apic_verbosity = APIC_DEBUG;
  603. else if (strcmp("verbose", str) == 0)
  604. apic_verbosity = APIC_VERBOSE;
  605. else
  606. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  607. " use apic=verbose or apic=debug", str);
  608. return 0;
  609. }
  610. __setup("apic=", apic_set_verbosity);
  611. static int __init detect_init_APIC (void)
  612. {
  613. u32 h, l, features;
  614. extern void get_cpu_vendor(struct cpuinfo_x86*);
  615. /* Disabled by kernel option? */
  616. if (enable_local_apic < 0)
  617. return -1;
  618. /* Workaround for us being called before identify_cpu(). */
  619. get_cpu_vendor(&boot_cpu_data);
  620. switch (boot_cpu_data.x86_vendor) {
  621. case X86_VENDOR_AMD:
  622. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  623. (boot_cpu_data.x86 == 15))
  624. break;
  625. goto no_apic;
  626. case X86_VENDOR_INTEL:
  627. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  628. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  629. break;
  630. goto no_apic;
  631. default:
  632. goto no_apic;
  633. }
  634. if (!cpu_has_apic) {
  635. /*
  636. * Over-ride BIOS and try to enable the local
  637. * APIC only if "lapic" specified.
  638. */
  639. if (enable_local_apic <= 0) {
  640. printk("Local APIC disabled by BIOS -- "
  641. "you can enable it with \"lapic\"\n");
  642. return -1;
  643. }
  644. /*
  645. * Some BIOSes disable the local APIC in the
  646. * APIC_BASE MSR. This can only be done in
  647. * software for Intel P6 or later and AMD K7
  648. * (Model > 1) or later.
  649. */
  650. rdmsr(MSR_IA32_APICBASE, l, h);
  651. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  652. printk("Local APIC disabled by BIOS -- reenabling.\n");
  653. l &= ~MSR_IA32_APICBASE_BASE;
  654. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  655. wrmsr(MSR_IA32_APICBASE, l, h);
  656. enabled_via_apicbase = 1;
  657. }
  658. }
  659. /*
  660. * The APIC feature bit should now be enabled
  661. * in `cpuid'
  662. */
  663. features = cpuid_edx(1);
  664. if (!(features & (1 << X86_FEATURE_APIC))) {
  665. printk("Could not enable APIC!\n");
  666. return -1;
  667. }
  668. set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  669. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  670. /* The BIOS may have set up the APIC at some other address */
  671. rdmsr(MSR_IA32_APICBASE, l, h);
  672. if (l & MSR_IA32_APICBASE_ENABLE)
  673. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  674. if (nmi_watchdog != NMI_NONE)
  675. nmi_watchdog = NMI_LOCAL_APIC;
  676. printk("Found and enabled local APIC!\n");
  677. apic_pm_activate();
  678. return 0;
  679. no_apic:
  680. printk("No local APIC present or hardware disabled\n");
  681. return -1;
  682. }
  683. void __init init_apic_mappings(void)
  684. {
  685. unsigned long apic_phys;
  686. /*
  687. * If no local APIC can be found then set up a fake all
  688. * zeroes page to simulate the local APIC and another
  689. * one for the IO-APIC.
  690. */
  691. if (!smp_found_config && detect_init_APIC()) {
  692. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  693. apic_phys = __pa(apic_phys);
  694. } else
  695. apic_phys = mp_lapic_addr;
  696. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  697. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  698. apic_phys);
  699. /*
  700. * Fetch the APIC ID of the BSP in case we have a
  701. * default configuration (or the MP table is broken).
  702. */
  703. if (boot_cpu_physical_apicid == -1U)
  704. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  705. #ifdef CONFIG_X86_IO_APIC
  706. {
  707. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  708. int i;
  709. for (i = 0; i < nr_ioapics; i++) {
  710. if (smp_found_config) {
  711. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  712. if (!ioapic_phys) {
  713. printk(KERN_ERR
  714. "WARNING: bogus zero IO-APIC "
  715. "address found in MPTABLE, "
  716. "disabling IO/APIC support!\n");
  717. smp_found_config = 0;
  718. skip_ioapic_setup = 1;
  719. goto fake_ioapic_page;
  720. }
  721. } else {
  722. fake_ioapic_page:
  723. ioapic_phys = (unsigned long)
  724. alloc_bootmem_pages(PAGE_SIZE);
  725. ioapic_phys = __pa(ioapic_phys);
  726. }
  727. set_fixmap_nocache(idx, ioapic_phys);
  728. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  729. __fix_to_virt(idx), ioapic_phys);
  730. idx++;
  731. }
  732. }
  733. #endif
  734. }
  735. /*
  736. * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
  737. * per second. We assume that the caller has already set up the local
  738. * APIC.
  739. *
  740. * The APIC timer is not exactly sync with the external timer chip, it
  741. * closely follows bus clocks.
  742. */
  743. /*
  744. * The timer chip is already set up at HZ interrupts per second here,
  745. * but we do not accept timer interrupts yet. We only allow the BP
  746. * to calibrate.
  747. */
  748. static unsigned int __init get_8254_timer_count(void)
  749. {
  750. extern spinlock_t i8253_lock;
  751. unsigned long flags;
  752. unsigned int count;
  753. spin_lock_irqsave(&i8253_lock, flags);
  754. outb_p(0x00, PIT_MODE);
  755. count = inb_p(PIT_CH0);
  756. count |= inb_p(PIT_CH0) << 8;
  757. spin_unlock_irqrestore(&i8253_lock, flags);
  758. return count;
  759. }
  760. /* next tick in 8254 can be caught by catching timer wraparound */
  761. static void __init wait_8254_wraparound(void)
  762. {
  763. unsigned int curr_count, prev_count;
  764. curr_count = get_8254_timer_count();
  765. do {
  766. prev_count = curr_count;
  767. curr_count = get_8254_timer_count();
  768. /* workaround for broken Mercury/Neptune */
  769. if (prev_count >= curr_count + 0x100)
  770. curr_count = get_8254_timer_count();
  771. } while (prev_count >= curr_count);
  772. }
  773. /*
  774. * Default initialization for 8254 timers. If we use other timers like HPET,
  775. * we override this later
  776. */
  777. void (*wait_timer_tick)(void) __initdata = wait_8254_wraparound;
  778. /*
  779. * This function sets up the local APIC timer, with a timeout of
  780. * 'clocks' APIC bus clock. During calibration we actually call
  781. * this function twice on the boot CPU, once with a bogus timeout
  782. * value, second time for real. The other (noncalibrating) CPUs
  783. * call this function only once, with the real, calibrated value.
  784. *
  785. * We do reads before writes even if unnecessary, to get around the
  786. * P5 APIC double write bug.
  787. */
  788. #define APIC_DIVISOR 16
  789. static void __setup_APIC_LVTT(unsigned int clocks)
  790. {
  791. unsigned int lvtt_value, tmp_value, ver;
  792. ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  793. lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
  794. if (!APIC_INTEGRATED(ver))
  795. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  796. apic_write_around(APIC_LVTT, lvtt_value);
  797. /*
  798. * Divide PICLK by 16
  799. */
  800. tmp_value = apic_read(APIC_TDCR);
  801. apic_write_around(APIC_TDCR, (tmp_value
  802. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  803. | APIC_TDR_DIV_16);
  804. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  805. }
  806. static void __init setup_APIC_timer(unsigned int clocks)
  807. {
  808. unsigned long flags;
  809. local_irq_save(flags);
  810. /*
  811. * Wait for IRQ0's slice:
  812. */
  813. wait_timer_tick();
  814. __setup_APIC_LVTT(clocks);
  815. local_irq_restore(flags);
  816. }
  817. /*
  818. * In this function we calibrate APIC bus clocks to the external
  819. * timer. Unfortunately we cannot use jiffies and the timer irq
  820. * to calibrate, since some later bootup code depends on getting
  821. * the first irq? Ugh.
  822. *
  823. * We want to do the calibration only once since we
  824. * want to have local timer irqs syncron. CPUs connected
  825. * by the same APIC bus have the very same bus frequency.
  826. * And we want to have irqs off anyways, no accidental
  827. * APIC irq that way.
  828. */
  829. static int __init calibrate_APIC_clock(void)
  830. {
  831. unsigned long long t1 = 0, t2 = 0;
  832. long tt1, tt2;
  833. long result;
  834. int i;
  835. const int LOOPS = HZ/10;
  836. apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
  837. /*
  838. * Put whatever arbitrary (but long enough) timeout
  839. * value into the APIC clock, we just want to get the
  840. * counter running for calibration.
  841. */
  842. __setup_APIC_LVTT(1000000000);
  843. /*
  844. * The timer chip counts down to zero. Let's wait
  845. * for a wraparound to start exact measurement:
  846. * (the current tick might have been already half done)
  847. */
  848. wait_timer_tick();
  849. /*
  850. * We wrapped around just now. Let's start:
  851. */
  852. if (cpu_has_tsc)
  853. rdtscll(t1);
  854. tt1 = apic_read(APIC_TMCCT);
  855. /*
  856. * Let's wait LOOPS wraprounds:
  857. */
  858. for (i = 0; i < LOOPS; i++)
  859. wait_timer_tick();
  860. tt2 = apic_read(APIC_TMCCT);
  861. if (cpu_has_tsc)
  862. rdtscll(t2);
  863. /*
  864. * The APIC bus clock counter is 32 bits only, it
  865. * might have overflown, but note that we use signed
  866. * longs, thus no extra care needed.
  867. *
  868. * underflown to be exact, as the timer counts down ;)
  869. */
  870. result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
  871. if (cpu_has_tsc)
  872. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  873. "%ld.%04ld MHz.\n",
  874. ((long)(t2-t1)/LOOPS)/(1000000/HZ),
  875. ((long)(t2-t1)/LOOPS)%(1000000/HZ));
  876. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  877. "%ld.%04ld MHz.\n",
  878. result/(1000000/HZ),
  879. result%(1000000/HZ));
  880. return result;
  881. }
  882. static unsigned int calibration_result;
  883. void __init setup_boot_APIC_clock(void)
  884. {
  885. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
  886. using_apic_timer = 1;
  887. local_irq_disable();
  888. calibration_result = calibrate_APIC_clock();
  889. /*
  890. * Now set up the timer for real.
  891. */
  892. setup_APIC_timer(calibration_result);
  893. local_irq_enable();
  894. }
  895. void __init setup_secondary_APIC_clock(void)
  896. {
  897. setup_APIC_timer(calibration_result);
  898. }
  899. void __devinit disable_APIC_timer(void)
  900. {
  901. if (using_apic_timer) {
  902. unsigned long v;
  903. v = apic_read(APIC_LVTT);
  904. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  905. }
  906. }
  907. void enable_APIC_timer(void)
  908. {
  909. if (using_apic_timer) {
  910. unsigned long v;
  911. v = apic_read(APIC_LVTT);
  912. apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
  913. }
  914. }
  915. /*
  916. * the frequency of the profiling timer can be changed
  917. * by writing a multiplier value into /proc/profile.
  918. */
  919. int setup_profiling_timer(unsigned int multiplier)
  920. {
  921. int i;
  922. /*
  923. * Sanity check. [at least 500 APIC cycles should be
  924. * between APIC interrupts as a rule of thumb, to avoid
  925. * irqs flooding us]
  926. */
  927. if ( (!multiplier) || (calibration_result/multiplier < 500))
  928. return -EINVAL;
  929. /*
  930. * Set the new multiplier for each CPU. CPUs don't start using the
  931. * new values until the next timer interrupt in which they do process
  932. * accounting. At that time they also adjust their APIC timers
  933. * accordingly.
  934. */
  935. for (i = 0; i < NR_CPUS; ++i)
  936. per_cpu(prof_multiplier, i) = multiplier;
  937. return 0;
  938. }
  939. #undef APIC_DIVISOR
  940. /*
  941. * Local timer interrupt handler. It does both profiling and
  942. * process statistics/rescheduling.
  943. *
  944. * We do profiling in every local tick, statistics/rescheduling
  945. * happen only every 'profiling multiplier' ticks. The default
  946. * multiplier is 1 and it can be changed by writing the new multiplier
  947. * value into /proc/profile.
  948. */
  949. inline void smp_local_timer_interrupt(struct pt_regs * regs)
  950. {
  951. int cpu = smp_processor_id();
  952. profile_tick(CPU_PROFILING, regs);
  953. if (--per_cpu(prof_counter, cpu) <= 0) {
  954. /*
  955. * The multiplier may have changed since the last time we got
  956. * to this point as a result of the user writing to
  957. * /proc/profile. In this case we need to adjust the APIC
  958. * timer accordingly.
  959. *
  960. * Interrupts are already masked off at this point.
  961. */
  962. per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
  963. if (per_cpu(prof_counter, cpu) !=
  964. per_cpu(prof_old_multiplier, cpu)) {
  965. __setup_APIC_LVTT(
  966. calibration_result/
  967. per_cpu(prof_counter, cpu));
  968. per_cpu(prof_old_multiplier, cpu) =
  969. per_cpu(prof_counter, cpu);
  970. }
  971. #ifdef CONFIG_SMP
  972. update_process_times(user_mode_vm(regs));
  973. #endif
  974. }
  975. /*
  976. * We take the 'long' return path, and there every subsystem
  977. * grabs the apropriate locks (kernel lock/ irq lock).
  978. *
  979. * we might want to decouple profiling from the 'long path',
  980. * and do the profiling totally in assembly.
  981. *
  982. * Currently this isn't too much of an issue (performance wise),
  983. * we can take more than 100K local irqs per second on a 100 MHz P5.
  984. */
  985. }
  986. /*
  987. * Local APIC timer interrupt. This is the most natural way for doing
  988. * local interrupts, but local timer interrupts can be emulated by
  989. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  990. *
  991. * [ if a single-CPU system runs an SMP kernel then we call the local
  992. * interrupt as well. Thus we cannot inline the local irq ... ]
  993. */
  994. fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
  995. {
  996. int cpu = smp_processor_id();
  997. /*
  998. * the NMI deadlock-detector uses this.
  999. */
  1000. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  1001. /*
  1002. * NOTE! We'd better ACK the irq immediately,
  1003. * because timer handling can be slow.
  1004. */
  1005. ack_APIC_irq();
  1006. /*
  1007. * update_process_times() expects us to have done irq_enter().
  1008. * Besides, if we don't timer interrupts ignore the global
  1009. * interrupt lock, which is the WrongThing (tm) to do.
  1010. */
  1011. irq_enter();
  1012. smp_local_timer_interrupt(regs);
  1013. irq_exit();
  1014. }
  1015. /*
  1016. * This interrupt should _never_ happen with our APIC/SMP architecture
  1017. */
  1018. fastcall void smp_spurious_interrupt(struct pt_regs *regs)
  1019. {
  1020. unsigned long v;
  1021. irq_enter();
  1022. /*
  1023. * Check if this really is a spurious interrupt and ACK it
  1024. * if it is a vectored one. Just in case...
  1025. * Spurious interrupts should not be ACKed.
  1026. */
  1027. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1028. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1029. ack_APIC_irq();
  1030. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1031. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
  1032. smp_processor_id());
  1033. irq_exit();
  1034. }
  1035. /*
  1036. * This interrupt should never happen with our APIC/SMP architecture
  1037. */
  1038. fastcall void smp_error_interrupt(struct pt_regs *regs)
  1039. {
  1040. unsigned long v, v1;
  1041. irq_enter();
  1042. /* First tickle the hardware, only then report what went on. -- REW */
  1043. v = apic_read(APIC_ESR);
  1044. apic_write(APIC_ESR, 0);
  1045. v1 = apic_read(APIC_ESR);
  1046. ack_APIC_irq();
  1047. atomic_inc(&irq_err_count);
  1048. /* Here is what the APIC error bits mean:
  1049. 0: Send CS error
  1050. 1: Receive CS error
  1051. 2: Send accept error
  1052. 3: Receive accept error
  1053. 4: Reserved
  1054. 5: Send illegal vector
  1055. 6: Received illegal vector
  1056. 7: Illegal register address
  1057. */
  1058. printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
  1059. smp_processor_id(), v , v1);
  1060. irq_exit();
  1061. }
  1062. /*
  1063. * This initializes the IO-APIC and APIC hardware if this is
  1064. * a UP kernel.
  1065. */
  1066. int __init APIC_init_uniprocessor (void)
  1067. {
  1068. if (enable_local_apic < 0)
  1069. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1070. if (!smp_found_config && !cpu_has_apic)
  1071. return -1;
  1072. /*
  1073. * Complain if the BIOS pretends there is one.
  1074. */
  1075. if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1076. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1077. boot_cpu_physical_apicid);
  1078. return -1;
  1079. }
  1080. verify_local_APIC();
  1081. connect_bsp_APIC();
  1082. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1083. setup_local_APIC();
  1084. #ifdef CONFIG_X86_IO_APIC
  1085. if (smp_found_config)
  1086. if (!skip_ioapic_setup && nr_ioapics)
  1087. setup_IO_APIC();
  1088. #endif
  1089. setup_boot_APIC_clock();
  1090. return 0;
  1091. }