apic.c 28 KB

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