apic_64.c 30 KB

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