apic.c 29 KB

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