apic.c 30 KB

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