apic.c 30 KB

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