apic_32.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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/cpu.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/acpi_pmtmr.h>
  27. #include <linux/module.h>
  28. #include <linux/dmi.h>
  29. #include <asm/atomic.h>
  30. #include <asm/smp.h>
  31. #include <asm/mtrr.h>
  32. #include <asm/mpspec.h>
  33. #include <asm/desc.h>
  34. #include <asm/arch_hooks.h>
  35. #include <asm/hpet.h>
  36. #include <asm/i8253.h>
  37. #include <asm/nmi.h>
  38. #include <mach_apic.h>
  39. #include <mach_apicdef.h>
  40. #include <mach_ipi.h>
  41. /*
  42. * Sanity check
  43. */
  44. #if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
  45. # error SPURIOUS_APIC_VECTOR definition error
  46. #endif
  47. /*
  48. * Knob to control our willingness to enable the local APIC.
  49. *
  50. * -1=force-disable, +1=force-enable
  51. */
  52. static int enable_local_apic __initdata;
  53. /* Local APIC timer verification ok */
  54. static int local_apic_timer_verify_ok;
  55. /* Disable local APIC timer from the kernel commandline or via dmi quirk
  56. or using CPU MSR check */
  57. int local_apic_timer_disabled;
  58. /* Local APIC timer works in C2 */
  59. int local_apic_timer_c2_ok;
  60. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  61. /*
  62. * Debug level, exported for io_apic.c
  63. */
  64. int apic_verbosity;
  65. static unsigned int calibration_result;
  66. static int lapic_next_event(unsigned long delta,
  67. struct clock_event_device *evt);
  68. static void lapic_timer_setup(enum clock_event_mode mode,
  69. struct clock_event_device *evt);
  70. static void lapic_timer_broadcast(cpumask_t mask);
  71. static void apic_pm_activate(void);
  72. /*
  73. * The local apic timer can be used for any function which is CPU local.
  74. */
  75. static struct clock_event_device lapic_clockevent = {
  76. .name = "lapic",
  77. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  78. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  79. .shift = 32,
  80. .set_mode = lapic_timer_setup,
  81. .set_next_event = lapic_next_event,
  82. .broadcast = lapic_timer_broadcast,
  83. .rating = 100,
  84. .irq = -1,
  85. };
  86. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  87. /* Local APIC was disabled by the BIOS and enabled by the kernel */
  88. static int enabled_via_apicbase;
  89. /*
  90. * Get the LAPIC version
  91. */
  92. static inline int lapic_get_version(void)
  93. {
  94. return GET_APIC_VERSION(apic_read(APIC_LVR));
  95. }
  96. /*
  97. * Check, if the APIC is integrated or a separate chip
  98. */
  99. static inline int lapic_is_integrated(void)
  100. {
  101. return APIC_INTEGRATED(lapic_get_version());
  102. }
  103. /*
  104. * Check, whether this is a modern or a first generation APIC
  105. */
  106. static int modern_apic(void)
  107. {
  108. /* AMD systems use old APIC versions, so check the CPU */
  109. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  110. boot_cpu_data.x86 >= 0xf)
  111. return 1;
  112. return lapic_get_version() >= 0x14;
  113. }
  114. void apic_wait_icr_idle(void)
  115. {
  116. while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
  117. cpu_relax();
  118. }
  119. u32 safe_apic_wait_icr_idle(void)
  120. {
  121. u32 send_status;
  122. int timeout;
  123. timeout = 0;
  124. do {
  125. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  126. if (!send_status)
  127. break;
  128. udelay(100);
  129. } while (timeout++ < 1000);
  130. return send_status;
  131. }
  132. /**
  133. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  134. */
  135. void __cpuinit enable_NMI_through_LVT0(void)
  136. {
  137. unsigned int v = APIC_DM_NMI;
  138. /* Level triggered for 82489DX */
  139. if (!lapic_is_integrated())
  140. v |= APIC_LVT_LEVEL_TRIGGER;
  141. apic_write_around(APIC_LVT0, v);
  142. }
  143. /**
  144. * get_physical_broadcast - Get number of physical broadcast IDs
  145. */
  146. int get_physical_broadcast(void)
  147. {
  148. return modern_apic() ? 0xff : 0xf;
  149. }
  150. /**
  151. * lapic_get_maxlvt - get the maximum number of local vector table entries
  152. */
  153. int lapic_get_maxlvt(void)
  154. {
  155. unsigned int v = apic_read(APIC_LVR);
  156. /* 82489DXs do not report # of LVT entries. */
  157. return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
  158. }
  159. /*
  160. * Local APIC timer
  161. */
  162. /* Clock divisor is set to 16 */
  163. #define APIC_DIVISOR 16
  164. /*
  165. * This function sets up the local APIC timer, with a timeout of
  166. * 'clocks' APIC bus clock. During calibration we actually call
  167. * this function twice on the boot CPU, once with a bogus timeout
  168. * value, second time for real. The other (noncalibrating) CPUs
  169. * call this function only once, with the real, calibrated value.
  170. *
  171. * We do reads before writes even if unnecessary, to get around the
  172. * P5 APIC double write bug.
  173. */
  174. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  175. {
  176. unsigned int lvtt_value, tmp_value;
  177. lvtt_value = LOCAL_TIMER_VECTOR;
  178. if (!oneshot)
  179. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  180. if (!lapic_is_integrated())
  181. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  182. if (!irqen)
  183. lvtt_value |= APIC_LVT_MASKED;
  184. apic_write_around(APIC_LVTT, lvtt_value);
  185. /*
  186. * Divide PICLK by 16
  187. */
  188. tmp_value = apic_read(APIC_TDCR);
  189. apic_write_around(APIC_TDCR, (tmp_value
  190. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  191. | APIC_TDR_DIV_16);
  192. if (!oneshot)
  193. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  194. }
  195. /*
  196. * Program the next event, relative to now
  197. */
  198. static int lapic_next_event(unsigned long delta,
  199. struct clock_event_device *evt)
  200. {
  201. apic_write_around(APIC_TMICT, delta);
  202. return 0;
  203. }
  204. /*
  205. * Setup the lapic timer in periodic or oneshot mode
  206. */
  207. static void lapic_timer_setup(enum clock_event_mode mode,
  208. struct clock_event_device *evt)
  209. {
  210. unsigned long flags;
  211. unsigned int v;
  212. /* Lapic used for broadcast ? */
  213. if (!local_apic_timer_verify_ok)
  214. return;
  215. local_irq_save(flags);
  216. switch (mode) {
  217. case CLOCK_EVT_MODE_PERIODIC:
  218. case CLOCK_EVT_MODE_ONESHOT:
  219. __setup_APIC_LVTT(calibration_result,
  220. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  221. break;
  222. case CLOCK_EVT_MODE_UNUSED:
  223. case CLOCK_EVT_MODE_SHUTDOWN:
  224. v = apic_read(APIC_LVTT);
  225. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  226. apic_write_around(APIC_LVTT, v);
  227. break;
  228. case CLOCK_EVT_MODE_RESUME:
  229. /* Nothing to do here */
  230. break;
  231. }
  232. local_irq_restore(flags);
  233. }
  234. /*
  235. * Local APIC timer broadcast function
  236. */
  237. static void lapic_timer_broadcast(cpumask_t mask)
  238. {
  239. #ifdef CONFIG_SMP
  240. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  241. #endif
  242. }
  243. /*
  244. * Setup the local APIC timer for this CPU. Copy the initilized values
  245. * of the boot CPU and register the clock event in the framework.
  246. */
  247. static void __devinit setup_APIC_timer(void)
  248. {
  249. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  250. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  251. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  252. clockevents_register_device(levt);
  253. }
  254. /*
  255. * In this functions we calibrate APIC bus clocks to the external timer.
  256. *
  257. * We want to do the calibration only once since we want to have local timer
  258. * irqs syncron. CPUs connected by the same APIC bus have the very same bus
  259. * frequency.
  260. *
  261. * This was previously done by reading the PIT/HPET and waiting for a wrap
  262. * around to find out, that a tick has elapsed. I have a box, where the PIT
  263. * readout is broken, so it never gets out of the wait loop again. This was
  264. * also reported by others.
  265. *
  266. * Monitoring the jiffies value is inaccurate and the clockevents
  267. * infrastructure allows us to do a simple substitution of the interrupt
  268. * handler.
  269. *
  270. * The calibration routine also uses the pm_timer when possible, as the PIT
  271. * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
  272. * back to normal later in the boot process).
  273. */
  274. #define LAPIC_CAL_LOOPS (HZ/10)
  275. static __initdata int lapic_cal_loops = -1;
  276. static __initdata long lapic_cal_t1, lapic_cal_t2;
  277. static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
  278. static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
  279. static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
  280. /*
  281. * Temporary interrupt handler.
  282. */
  283. static void __init lapic_cal_handler(struct clock_event_device *dev)
  284. {
  285. unsigned long long tsc = 0;
  286. long tapic = apic_read(APIC_TMCCT);
  287. unsigned long pm = acpi_pm_read_early();
  288. if (cpu_has_tsc)
  289. rdtscll(tsc);
  290. switch (lapic_cal_loops++) {
  291. case 0:
  292. lapic_cal_t1 = tapic;
  293. lapic_cal_tsc1 = tsc;
  294. lapic_cal_pm1 = pm;
  295. lapic_cal_j1 = jiffies;
  296. break;
  297. case LAPIC_CAL_LOOPS:
  298. lapic_cal_t2 = tapic;
  299. lapic_cal_tsc2 = tsc;
  300. if (pm < lapic_cal_pm1)
  301. pm += ACPI_PM_OVRRUN;
  302. lapic_cal_pm2 = pm;
  303. lapic_cal_j2 = jiffies;
  304. break;
  305. }
  306. }
  307. /*
  308. * Setup the boot APIC
  309. *
  310. * Calibrate and verify the result.
  311. */
  312. void __init setup_boot_APIC_clock(void)
  313. {
  314. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  315. const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
  316. const long pm_thresh = pm_100ms/100;
  317. void (*real_handler)(struct clock_event_device *dev);
  318. unsigned long deltaj;
  319. long delta, deltapm;
  320. int pm_referenced = 0;
  321. /*
  322. * The local apic timer can be disabled via the kernel
  323. * commandline or from the CPU detection code. Register the lapic
  324. * timer as a dummy clock event source on SMP systems, so the
  325. * broadcast mechanism is used. On UP systems simply ignore it.
  326. */
  327. if (local_apic_timer_disabled) {
  328. /* No broadcast on UP ! */
  329. if (num_possible_cpus() > 1)
  330. setup_APIC_timer();
  331. return;
  332. }
  333. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
  334. "calibrating APIC timer ...\n");
  335. local_irq_disable();
  336. /* Replace the global interrupt handler */
  337. real_handler = global_clock_event->event_handler;
  338. global_clock_event->event_handler = lapic_cal_handler;
  339. /*
  340. * Setup the APIC counter to 1e9. There is no way the lapic
  341. * can underflow in the 100ms detection time frame
  342. */
  343. __setup_APIC_LVTT(1000000000, 0, 0);
  344. /* Let the interrupts run */
  345. local_irq_enable();
  346. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  347. cpu_relax();
  348. local_irq_disable();
  349. /* Restore the real event handler */
  350. global_clock_event->event_handler = real_handler;
  351. /* Build delta t1-t2 as apic timer counts down */
  352. delta = lapic_cal_t1 - lapic_cal_t2;
  353. apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
  354. /* Check, if the PM timer is available */
  355. deltapm = lapic_cal_pm2 - lapic_cal_pm1;
  356. apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
  357. if (deltapm) {
  358. unsigned long mult;
  359. u64 res;
  360. mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
  361. if (deltapm > (pm_100ms - pm_thresh) &&
  362. deltapm < (pm_100ms + pm_thresh)) {
  363. apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
  364. } else {
  365. res = (((u64) deltapm) * mult) >> 22;
  366. do_div(res, 1000000);
  367. printk(KERN_WARNING "APIC calibration not consistent "
  368. "with PM Timer: %ldms instead of 100ms\n",
  369. (long)res);
  370. /* Correct the lapic counter value */
  371. res = (((u64) delta) * pm_100ms);
  372. do_div(res, deltapm);
  373. printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
  374. "%lu (%ld)\n", (unsigned long) res, delta);
  375. delta = (long) res;
  376. }
  377. pm_referenced = 1;
  378. }
  379. /* Calculate the scaled math multiplication factor */
  380. lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
  381. lapic_clockevent.max_delta_ns =
  382. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  383. lapic_clockevent.min_delta_ns =
  384. clockevent_delta2ns(0xF, &lapic_clockevent);
  385. calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
  386. apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
  387. apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
  388. apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
  389. calibration_result);
  390. if (cpu_has_tsc) {
  391. delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
  392. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  393. "%ld.%04ld MHz.\n",
  394. (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
  395. (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
  396. }
  397. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  398. "%u.%04u MHz.\n",
  399. calibration_result / (1000000 / HZ),
  400. calibration_result % (1000000 / HZ));
  401. local_apic_timer_verify_ok = 1;
  402. /*
  403. * Do a sanity check on the APIC calibration result
  404. */
  405. if (calibration_result < (1000000 / HZ)) {
  406. local_irq_enable();
  407. printk(KERN_WARNING
  408. "APIC frequency too slow, disabling apic timer\n");
  409. /* No broadcast on UP ! */
  410. if (num_possible_cpus() > 1)
  411. setup_APIC_timer();
  412. return;
  413. }
  414. /* We trust the pm timer based calibration */
  415. if (!pm_referenced) {
  416. apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
  417. /*
  418. * Setup the apic timer manually
  419. */
  420. levt->event_handler = lapic_cal_handler;
  421. lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
  422. lapic_cal_loops = -1;
  423. /* Let the interrupts run */
  424. local_irq_enable();
  425. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  426. cpu_relax();
  427. local_irq_disable();
  428. /* Stop the lapic timer */
  429. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
  430. local_irq_enable();
  431. /* Jiffies delta */
  432. deltaj = lapic_cal_j2 - lapic_cal_j1;
  433. apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
  434. /* Check, if the jiffies result is consistent */
  435. if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
  436. apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
  437. else
  438. local_apic_timer_verify_ok = 0;
  439. } else
  440. local_irq_enable();
  441. if (!local_apic_timer_verify_ok) {
  442. printk(KERN_WARNING
  443. "APIC timer disabled due to verification failure.\n");
  444. /* No broadcast on UP ! */
  445. if (num_possible_cpus() == 1)
  446. return;
  447. } else {
  448. /*
  449. * If nmi_watchdog is set to IO_APIC, we need the
  450. * PIT/HPET going. Otherwise register lapic as a dummy
  451. * device.
  452. */
  453. if (nmi_watchdog != NMI_IO_APIC)
  454. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  455. else
  456. printk(KERN_WARNING "APIC timer registered as dummy,"
  457. " due to nmi_watchdog=1!\n");
  458. }
  459. /* Setup the lapic or request the broadcast */
  460. setup_APIC_timer();
  461. }
  462. void __devinit setup_secondary_APIC_clock(void)
  463. {
  464. setup_APIC_timer();
  465. }
  466. /*
  467. * The guts of the apic timer interrupt
  468. */
  469. static void local_apic_timer_interrupt(void)
  470. {
  471. int cpu = smp_processor_id();
  472. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  473. /*
  474. * Normally we should not be here till LAPIC has been initialized but
  475. * in some cases like kdump, its possible that there is a pending LAPIC
  476. * timer interrupt from previous kernel's context and is delivered in
  477. * new kernel the moment interrupts are enabled.
  478. *
  479. * Interrupts are enabled early and LAPIC is setup much later, hence
  480. * its possible that when we get here evt->event_handler is NULL.
  481. * Check for event_handler being NULL and discard the interrupt as
  482. * spurious.
  483. */
  484. if (!evt->event_handler) {
  485. printk(KERN_WARNING
  486. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  487. /* Switch it off */
  488. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  489. return;
  490. }
  491. /*
  492. * the NMI deadlock-detector uses this.
  493. */
  494. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  495. evt->event_handler(evt);
  496. }
  497. /*
  498. * Local APIC timer interrupt. This is the most natural way for doing
  499. * local interrupts, but local timer interrupts can be emulated by
  500. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  501. *
  502. * [ if a single-CPU system runs an SMP kernel then we call the local
  503. * interrupt as well. Thus we cannot inline the local irq ... ]
  504. */
  505. void smp_apic_timer_interrupt(struct pt_regs *regs)
  506. {
  507. struct pt_regs *old_regs = set_irq_regs(regs);
  508. /*
  509. * NOTE! We'd better ACK the irq immediately,
  510. * because timer handling can be slow.
  511. */
  512. ack_APIC_irq();
  513. /*
  514. * update_process_times() expects us to have done irq_enter().
  515. * Besides, if we don't timer interrupts ignore the global
  516. * interrupt lock, which is the WrongThing (tm) to do.
  517. */
  518. irq_enter();
  519. local_apic_timer_interrupt();
  520. irq_exit();
  521. set_irq_regs(old_regs);
  522. }
  523. int setup_profiling_timer(unsigned int multiplier)
  524. {
  525. return -EINVAL;
  526. }
  527. /*
  528. * Local APIC start and shutdown
  529. */
  530. /**
  531. * clear_local_APIC - shutdown the local APIC
  532. *
  533. * This is called, when a CPU is disabled and before rebooting, so the state of
  534. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  535. * leftovers during boot.
  536. */
  537. void clear_local_APIC(void)
  538. {
  539. int maxlvt = lapic_get_maxlvt();
  540. u32 v;
  541. /*
  542. * Masking an LVT entry can trigger a local APIC error
  543. * if the vector is zero. Mask LVTERR first to prevent this.
  544. */
  545. if (maxlvt >= 3) {
  546. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  547. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  548. }
  549. /*
  550. * Careful: we have to set masks only first to deassert
  551. * any level-triggered sources.
  552. */
  553. v = apic_read(APIC_LVTT);
  554. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  555. v = apic_read(APIC_LVT0);
  556. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  557. v = apic_read(APIC_LVT1);
  558. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  559. if (maxlvt >= 4) {
  560. v = apic_read(APIC_LVTPC);
  561. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  562. }
  563. /* lets not touch this if we didn't frob it */
  564. #ifdef CONFIG_X86_MCE_P4THERMAL
  565. if (maxlvt >= 5) {
  566. v = apic_read(APIC_LVTTHMR);
  567. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  568. }
  569. #endif
  570. /*
  571. * Clean APIC state for other OSs:
  572. */
  573. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  574. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  575. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  576. if (maxlvt >= 3)
  577. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  578. if (maxlvt >= 4)
  579. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  580. #ifdef CONFIG_X86_MCE_P4THERMAL
  581. if (maxlvt >= 5)
  582. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  583. #endif
  584. /* Integrated APIC (!82489DX) ? */
  585. if (lapic_is_integrated()) {
  586. if (maxlvt > 3)
  587. /* Clear ESR due to Pentium errata 3AP and 11AP */
  588. apic_write(APIC_ESR, 0);
  589. apic_read(APIC_ESR);
  590. }
  591. }
  592. /**
  593. * disable_local_APIC - clear and disable the local APIC
  594. */
  595. void disable_local_APIC(void)
  596. {
  597. unsigned long value;
  598. clear_local_APIC();
  599. /*
  600. * Disable APIC (implies clearing of registers
  601. * for 82489DX!).
  602. */
  603. value = apic_read(APIC_SPIV);
  604. value &= ~APIC_SPIV_APIC_ENABLED;
  605. apic_write_around(APIC_SPIV, value);
  606. /*
  607. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  608. * restore the disabled state.
  609. */
  610. if (enabled_via_apicbase) {
  611. unsigned int l, h;
  612. rdmsr(MSR_IA32_APICBASE, l, h);
  613. l &= ~MSR_IA32_APICBASE_ENABLE;
  614. wrmsr(MSR_IA32_APICBASE, l, h);
  615. }
  616. }
  617. /*
  618. * If Linux enabled the LAPIC against the BIOS default disable it down before
  619. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  620. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  621. * for the case where Linux didn't enable the LAPIC.
  622. */
  623. void lapic_shutdown(void)
  624. {
  625. unsigned long flags;
  626. if (!cpu_has_apic)
  627. return;
  628. local_irq_save(flags);
  629. clear_local_APIC();
  630. if (enabled_via_apicbase)
  631. disable_local_APIC();
  632. local_irq_restore(flags);
  633. }
  634. /*
  635. * This is to verify that we're looking at a real local APIC.
  636. * Check these against your board if the CPUs aren't getting
  637. * started for no apparent reason.
  638. */
  639. int __init verify_local_APIC(void)
  640. {
  641. unsigned int reg0, reg1;
  642. /*
  643. * The version register is read-only in a real APIC.
  644. */
  645. reg0 = apic_read(APIC_LVR);
  646. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  647. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  648. reg1 = apic_read(APIC_LVR);
  649. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  650. /*
  651. * The two version reads above should print the same
  652. * numbers. If the second one is different, then we
  653. * poke at a non-APIC.
  654. */
  655. if (reg1 != reg0)
  656. return 0;
  657. /*
  658. * Check if the version looks reasonably.
  659. */
  660. reg1 = GET_APIC_VERSION(reg0);
  661. if (reg1 == 0x00 || reg1 == 0xff)
  662. return 0;
  663. reg1 = lapic_get_maxlvt();
  664. if (reg1 < 0x02 || reg1 == 0xff)
  665. return 0;
  666. /*
  667. * The ID register is read/write in a real APIC.
  668. */
  669. reg0 = apic_read(APIC_ID);
  670. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  671. /*
  672. * The next two are just to see if we have sane values.
  673. * They're only really relevant if we're in Virtual Wire
  674. * compatibility mode, but most boxes are anymore.
  675. */
  676. reg0 = apic_read(APIC_LVT0);
  677. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  678. reg1 = apic_read(APIC_LVT1);
  679. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  680. return 1;
  681. }
  682. /**
  683. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  684. */
  685. void __init sync_Arb_IDs(void)
  686. {
  687. /*
  688. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  689. * needed on AMD.
  690. */
  691. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  692. return;
  693. /*
  694. * Wait for idle.
  695. */
  696. apic_wait_icr_idle();
  697. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  698. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  699. | APIC_DM_INIT);
  700. }
  701. /*
  702. * An initial setup of the virtual wire mode.
  703. */
  704. void __init init_bsp_APIC(void)
  705. {
  706. unsigned long value;
  707. /*
  708. * Don't do the setup now if we have a SMP BIOS as the
  709. * through-I/O-APIC virtual wire mode might be active.
  710. */
  711. if (smp_found_config || !cpu_has_apic)
  712. return;
  713. /*
  714. * Do not trust the local APIC being empty at bootup.
  715. */
  716. clear_local_APIC();
  717. /*
  718. * Enable APIC.
  719. */
  720. value = apic_read(APIC_SPIV);
  721. value &= ~APIC_VECTOR_MASK;
  722. value |= APIC_SPIV_APIC_ENABLED;
  723. /* This bit is reserved on P4/Xeon and should be cleared */
  724. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  725. (boot_cpu_data.x86 == 15))
  726. value &= ~APIC_SPIV_FOCUS_DISABLED;
  727. else
  728. value |= APIC_SPIV_FOCUS_DISABLED;
  729. value |= SPURIOUS_APIC_VECTOR;
  730. apic_write_around(APIC_SPIV, value);
  731. /*
  732. * Set up the virtual wire mode.
  733. */
  734. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  735. value = APIC_DM_NMI;
  736. if (!lapic_is_integrated()) /* 82489DX */
  737. value |= APIC_LVT_LEVEL_TRIGGER;
  738. apic_write_around(APIC_LVT1, value);
  739. }
  740. /**
  741. * setup_local_APIC - setup the local APIC
  742. */
  743. void __cpuinit setup_local_APIC(void)
  744. {
  745. unsigned long oldvalue, value, maxlvt, integrated;
  746. int i, j;
  747. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  748. if (esr_disable) {
  749. apic_write(APIC_ESR, 0);
  750. apic_write(APIC_ESR, 0);
  751. apic_write(APIC_ESR, 0);
  752. apic_write(APIC_ESR, 0);
  753. }
  754. integrated = lapic_is_integrated();
  755. /*
  756. * Double-check whether this APIC is really registered.
  757. */
  758. if (!apic_id_registered())
  759. BUG();
  760. /*
  761. * Intel recommends to set DFR, LDR and TPR before enabling
  762. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  763. * document number 292116). So here it goes...
  764. */
  765. init_apic_ldr();
  766. /*
  767. * Set Task Priority to 'accept all'. We never change this
  768. * later on.
  769. */
  770. value = apic_read(APIC_TASKPRI);
  771. value &= ~APIC_TPRI_MASK;
  772. apic_write_around(APIC_TASKPRI, value);
  773. /*
  774. * After a crash, we no longer service the interrupts and a pending
  775. * interrupt from previous kernel might still have ISR bit set.
  776. *
  777. * Most probably by now CPU has serviced that pending interrupt and
  778. * it might not have done the ack_APIC_irq() because it thought,
  779. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  780. * does not clear the ISR bit and cpu thinks it has already serivced
  781. * the interrupt. Hence a vector might get locked. It was noticed
  782. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  783. */
  784. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  785. value = apic_read(APIC_ISR + i*0x10);
  786. for (j = 31; j >= 0; j--) {
  787. if (value & (1<<j))
  788. ack_APIC_irq();
  789. }
  790. }
  791. /*
  792. * Now that we are all set up, enable the APIC
  793. */
  794. value = apic_read(APIC_SPIV);
  795. value &= ~APIC_VECTOR_MASK;
  796. /*
  797. * Enable APIC
  798. */
  799. value |= APIC_SPIV_APIC_ENABLED;
  800. /*
  801. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  802. * certain networking cards. If high frequency interrupts are
  803. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  804. * entry is masked/unmasked at a high rate as well then sooner or
  805. * later IOAPIC line gets 'stuck', no more interrupts are received
  806. * from the device. If focus CPU is disabled then the hang goes
  807. * away, oh well :-(
  808. *
  809. * [ This bug can be reproduced easily with a level-triggered
  810. * PCI Ne2000 networking cards and PII/PIII processors, dual
  811. * BX chipset. ]
  812. */
  813. /*
  814. * Actually disabling the focus CPU check just makes the hang less
  815. * frequent as it makes the interrupt distributon model be more
  816. * like LRU than MRU (the short-term load is more even across CPUs).
  817. * See also the comment in end_level_ioapic_irq(). --macro
  818. */
  819. /* Enable focus processor (bit==0) */
  820. value &= ~APIC_SPIV_FOCUS_DISABLED;
  821. /*
  822. * Set spurious IRQ vector
  823. */
  824. value |= SPURIOUS_APIC_VECTOR;
  825. apic_write_around(APIC_SPIV, value);
  826. /*
  827. * Set up LVT0, LVT1:
  828. *
  829. * set up through-local-APIC on the BP's LINT0. This is not
  830. * strictly necessary in pure symmetric-IO mode, but sometimes
  831. * we delegate interrupts to the 8259A.
  832. */
  833. /*
  834. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  835. */
  836. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  837. if (!smp_processor_id() && (pic_mode || !value)) {
  838. value = APIC_DM_EXTINT;
  839. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  840. smp_processor_id());
  841. } else {
  842. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  843. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  844. smp_processor_id());
  845. }
  846. apic_write_around(APIC_LVT0, value);
  847. /*
  848. * only the BP should see the LINT1 NMI signal, obviously.
  849. */
  850. if (!smp_processor_id())
  851. value = APIC_DM_NMI;
  852. else
  853. value = APIC_DM_NMI | APIC_LVT_MASKED;
  854. if (!integrated) /* 82489DX */
  855. value |= APIC_LVT_LEVEL_TRIGGER;
  856. apic_write_around(APIC_LVT1, value);
  857. if (integrated && !esr_disable) {
  858. /* !82489DX */
  859. maxlvt = lapic_get_maxlvt();
  860. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  861. apic_write(APIC_ESR, 0);
  862. oldvalue = apic_read(APIC_ESR);
  863. /* enables sending errors */
  864. value = ERROR_APIC_VECTOR;
  865. apic_write_around(APIC_LVTERR, value);
  866. /*
  867. * spec says clear errors after enabling vector.
  868. */
  869. if (maxlvt > 3)
  870. apic_write(APIC_ESR, 0);
  871. value = apic_read(APIC_ESR);
  872. if (value != oldvalue)
  873. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  874. "vector: 0x%08lx after: 0x%08lx\n",
  875. oldvalue, value);
  876. } else {
  877. if (esr_disable)
  878. /*
  879. * Something untraceable is creating bad interrupts on
  880. * secondary quads ... for the moment, just leave the
  881. * ESR disabled - we can't do anything useful with the
  882. * errors anyway - mbligh
  883. */
  884. printk(KERN_INFO "Leaving ESR disabled.\n");
  885. else
  886. printk(KERN_INFO "No ESR for 82489DX.\n");
  887. }
  888. /* Disable the local apic timer */
  889. value = apic_read(APIC_LVTT);
  890. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  891. apic_write_around(APIC_LVTT, value);
  892. setup_apic_nmi_watchdog(NULL);
  893. apic_pm_activate();
  894. }
  895. /*
  896. * Detect and initialize APIC
  897. */
  898. static int __init detect_init_APIC(void)
  899. {
  900. u32 h, l, features;
  901. /* Disabled by kernel option? */
  902. if (enable_local_apic < 0)
  903. return -1;
  904. switch (boot_cpu_data.x86_vendor) {
  905. case X86_VENDOR_AMD:
  906. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  907. (boot_cpu_data.x86 == 15))
  908. break;
  909. goto no_apic;
  910. case X86_VENDOR_INTEL:
  911. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  912. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  913. break;
  914. goto no_apic;
  915. default:
  916. goto no_apic;
  917. }
  918. if (!cpu_has_apic) {
  919. /*
  920. * Over-ride BIOS and try to enable the local APIC only if
  921. * "lapic" specified.
  922. */
  923. if (enable_local_apic <= 0) {
  924. printk(KERN_INFO "Local APIC disabled by BIOS -- "
  925. "you can enable it with \"lapic\"\n");
  926. return -1;
  927. }
  928. /*
  929. * Some BIOSes disable the local APIC in the APIC_BASE
  930. * MSR. This can only be done in software for Intel P6 or later
  931. * and AMD K7 (Model > 1) or later.
  932. */
  933. rdmsr(MSR_IA32_APICBASE, l, h);
  934. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  935. printk(KERN_INFO
  936. "Local APIC disabled by BIOS -- reenabling.\n");
  937. l &= ~MSR_IA32_APICBASE_BASE;
  938. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  939. wrmsr(MSR_IA32_APICBASE, l, h);
  940. enabled_via_apicbase = 1;
  941. }
  942. }
  943. /*
  944. * The APIC feature bit should now be enabled
  945. * in `cpuid'
  946. */
  947. features = cpuid_edx(1);
  948. if (!(features & (1 << X86_FEATURE_APIC))) {
  949. printk(KERN_WARNING "Could not enable APIC!\n");
  950. return -1;
  951. }
  952. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  953. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  954. /* The BIOS may have set up the APIC at some other address */
  955. rdmsr(MSR_IA32_APICBASE, l, h);
  956. if (l & MSR_IA32_APICBASE_ENABLE)
  957. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  958. if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
  959. nmi_watchdog = NMI_LOCAL_APIC;
  960. printk(KERN_INFO "Found and enabled local APIC!\n");
  961. apic_pm_activate();
  962. return 0;
  963. no_apic:
  964. printk(KERN_INFO "No local APIC present or hardware disabled\n");
  965. return -1;
  966. }
  967. /**
  968. * init_apic_mappings - initialize APIC mappings
  969. */
  970. void __init init_apic_mappings(void)
  971. {
  972. unsigned long apic_phys;
  973. /*
  974. * If no local APIC can be found then set up a fake all
  975. * zeroes page to simulate the local APIC and another
  976. * one for the IO-APIC.
  977. */
  978. if (!smp_found_config && detect_init_APIC()) {
  979. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  980. apic_phys = __pa(apic_phys);
  981. } else
  982. apic_phys = mp_lapic_addr;
  983. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  984. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  985. apic_phys);
  986. /*
  987. * Fetch the APIC ID of the BSP in case we have a
  988. * default configuration (or the MP table is broken).
  989. */
  990. if (boot_cpu_physical_apicid == -1U)
  991. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  992. #ifdef CONFIG_X86_IO_APIC
  993. {
  994. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  995. int i;
  996. for (i = 0; i < nr_ioapics; i++) {
  997. if (smp_found_config) {
  998. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  999. if (!ioapic_phys) {
  1000. printk(KERN_ERR
  1001. "WARNING: bogus zero IO-APIC "
  1002. "address found in MPTABLE, "
  1003. "disabling IO/APIC support!\n");
  1004. smp_found_config = 0;
  1005. skip_ioapic_setup = 1;
  1006. goto fake_ioapic_page;
  1007. }
  1008. } else {
  1009. fake_ioapic_page:
  1010. ioapic_phys = (unsigned long)
  1011. alloc_bootmem_pages(PAGE_SIZE);
  1012. ioapic_phys = __pa(ioapic_phys);
  1013. }
  1014. set_fixmap_nocache(idx, ioapic_phys);
  1015. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  1016. __fix_to_virt(idx), ioapic_phys);
  1017. idx++;
  1018. }
  1019. }
  1020. #endif
  1021. }
  1022. /*
  1023. * This initializes the IO-APIC and APIC hardware if this is
  1024. * a UP kernel.
  1025. */
  1026. int __init APIC_init_uniprocessor(void)
  1027. {
  1028. if (enable_local_apic < 0)
  1029. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1030. if (!smp_found_config && !cpu_has_apic)
  1031. return -1;
  1032. /*
  1033. * Complain if the BIOS pretends there is one.
  1034. */
  1035. if (!cpu_has_apic &&
  1036. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1037. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1038. boot_cpu_physical_apicid);
  1039. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1040. return -1;
  1041. }
  1042. verify_local_APIC();
  1043. connect_bsp_APIC();
  1044. /*
  1045. * Hack: In case of kdump, after a crash, kernel might be booting
  1046. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1047. * might be zero if read from MP tables. Get it from LAPIC.
  1048. */
  1049. #ifdef CONFIG_CRASH_DUMP
  1050. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  1051. #endif
  1052. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1053. setup_local_APIC();
  1054. #ifdef CONFIG_X86_IO_APIC
  1055. if (smp_found_config)
  1056. if (!skip_ioapic_setup && nr_ioapics)
  1057. setup_IO_APIC();
  1058. #endif
  1059. setup_boot_clock();
  1060. return 0;
  1061. }
  1062. /*
  1063. * Local APIC interrupts
  1064. */
  1065. /*
  1066. * This interrupt should _never_ happen with our APIC/SMP architecture
  1067. */
  1068. void smp_spurious_interrupt(struct pt_regs *regs)
  1069. {
  1070. unsigned long v;
  1071. irq_enter();
  1072. /*
  1073. * Check if this really is a spurious interrupt and ACK it
  1074. * if it is a vectored one. Just in case...
  1075. * Spurious interrupts should not be ACKed.
  1076. */
  1077. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1078. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1079. ack_APIC_irq();
  1080. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1081. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
  1082. "should never happen.\n", smp_processor_id());
  1083. __get_cpu_var(irq_stat).irq_spurious_count++;
  1084. irq_exit();
  1085. }
  1086. /*
  1087. * This interrupt should never happen with our APIC/SMP architecture
  1088. */
  1089. void smp_error_interrupt(struct pt_regs *regs)
  1090. {
  1091. unsigned long v, v1;
  1092. irq_enter();
  1093. /* First tickle the hardware, only then report what went on. -- REW */
  1094. v = apic_read(APIC_ESR);
  1095. apic_write(APIC_ESR, 0);
  1096. v1 = apic_read(APIC_ESR);
  1097. ack_APIC_irq();
  1098. atomic_inc(&irq_err_count);
  1099. /* Here is what the APIC error bits mean:
  1100. 0: Send CS error
  1101. 1: Receive CS error
  1102. 2: Send accept error
  1103. 3: Receive accept error
  1104. 4: Reserved
  1105. 5: Send illegal vector
  1106. 6: Received illegal vector
  1107. 7: Illegal register address
  1108. */
  1109. printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
  1110. smp_processor_id(), v , v1);
  1111. irq_exit();
  1112. }
  1113. /*
  1114. * Initialize APIC interrupts
  1115. */
  1116. void __init apic_intr_init(void)
  1117. {
  1118. #ifdef CONFIG_SMP
  1119. smp_intr_init();
  1120. #endif
  1121. /* self generated IPI for local APIC timer */
  1122. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  1123. /* IPI vectors for APIC spurious and error interrupts */
  1124. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  1125. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  1126. /* thermal monitor LVT interrupt */
  1127. #ifdef CONFIG_X86_MCE_P4THERMAL
  1128. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  1129. #endif
  1130. }
  1131. /**
  1132. * connect_bsp_APIC - attach the APIC to the interrupt system
  1133. */
  1134. void __init connect_bsp_APIC(void)
  1135. {
  1136. if (pic_mode) {
  1137. /*
  1138. * Do not trust the local APIC being empty at bootup.
  1139. */
  1140. clear_local_APIC();
  1141. /*
  1142. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1143. * local APIC to INT and NMI lines.
  1144. */
  1145. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1146. "enabling APIC mode.\n");
  1147. outb(0x70, 0x22);
  1148. outb(0x01, 0x23);
  1149. }
  1150. enable_apic_mode();
  1151. }
  1152. /**
  1153. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1154. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1155. *
  1156. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1157. * APIC is disabled.
  1158. */
  1159. void disconnect_bsp_APIC(int virt_wire_setup)
  1160. {
  1161. if (pic_mode) {
  1162. /*
  1163. * Put the board back into PIC mode (has an effect only on
  1164. * certain older boards). Note that APIC interrupts, including
  1165. * IPIs, won't work beyond this point! The only exception are
  1166. * INIT IPIs.
  1167. */
  1168. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1169. "entering PIC mode.\n");
  1170. outb(0x70, 0x22);
  1171. outb(0x00, 0x23);
  1172. } else {
  1173. /* Go back to Virtual Wire compatibility mode */
  1174. unsigned long value;
  1175. /* For the spurious interrupt use vector F, and enable it */
  1176. value = apic_read(APIC_SPIV);
  1177. value &= ~APIC_VECTOR_MASK;
  1178. value |= APIC_SPIV_APIC_ENABLED;
  1179. value |= 0xf;
  1180. apic_write_around(APIC_SPIV, value);
  1181. if (!virt_wire_setup) {
  1182. /*
  1183. * For LVT0 make it edge triggered, active high,
  1184. * external and enabled
  1185. */
  1186. value = apic_read(APIC_LVT0);
  1187. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1188. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1189. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1190. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1191. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1192. apic_write_around(APIC_LVT0, value);
  1193. } else {
  1194. /* Disable LVT0 */
  1195. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  1196. }
  1197. /*
  1198. * For LVT1 make it edge triggered, active high, nmi and
  1199. * enabled
  1200. */
  1201. value = apic_read(APIC_LVT1);
  1202. value &= ~(
  1203. APIC_MODE_MASK | APIC_SEND_PENDING |
  1204. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1205. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1206. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1207. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1208. apic_write_around(APIC_LVT1, value);
  1209. }
  1210. }
  1211. /*
  1212. * Power management
  1213. */
  1214. #ifdef CONFIG_PM
  1215. static struct {
  1216. int active;
  1217. /* r/w apic fields */
  1218. unsigned int apic_id;
  1219. unsigned int apic_taskpri;
  1220. unsigned int apic_ldr;
  1221. unsigned int apic_dfr;
  1222. unsigned int apic_spiv;
  1223. unsigned int apic_lvtt;
  1224. unsigned int apic_lvtpc;
  1225. unsigned int apic_lvt0;
  1226. unsigned int apic_lvt1;
  1227. unsigned int apic_lvterr;
  1228. unsigned int apic_tmict;
  1229. unsigned int apic_tdcr;
  1230. unsigned int apic_thmr;
  1231. } apic_pm_state;
  1232. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1233. {
  1234. unsigned long flags;
  1235. int maxlvt;
  1236. if (!apic_pm_state.active)
  1237. return 0;
  1238. maxlvt = lapic_get_maxlvt();
  1239. apic_pm_state.apic_id = apic_read(APIC_ID);
  1240. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1241. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1242. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1243. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1244. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1245. if (maxlvt >= 4)
  1246. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1247. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1248. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1249. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1250. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1251. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1252. #ifdef CONFIG_X86_MCE_P4THERMAL
  1253. if (maxlvt >= 5)
  1254. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1255. #endif
  1256. local_irq_save(flags);
  1257. disable_local_APIC();
  1258. local_irq_restore(flags);
  1259. return 0;
  1260. }
  1261. static int lapic_resume(struct sys_device *dev)
  1262. {
  1263. unsigned int l, h;
  1264. unsigned long flags;
  1265. int maxlvt;
  1266. if (!apic_pm_state.active)
  1267. return 0;
  1268. maxlvt = lapic_get_maxlvt();
  1269. local_irq_save(flags);
  1270. /*
  1271. * Make sure the APICBASE points to the right address
  1272. *
  1273. * FIXME! This will be wrong if we ever support suspend on
  1274. * SMP! We'll need to do this as part of the CPU restore!
  1275. */
  1276. rdmsr(MSR_IA32_APICBASE, l, h);
  1277. l &= ~MSR_IA32_APICBASE_BASE;
  1278. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1279. wrmsr(MSR_IA32_APICBASE, l, h);
  1280. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1281. apic_write(APIC_ID, apic_pm_state.apic_id);
  1282. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1283. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1284. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1285. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1286. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1287. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1288. #ifdef CONFIG_X86_MCE_P4THERMAL
  1289. if (maxlvt >= 5)
  1290. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1291. #endif
  1292. if (maxlvt >= 4)
  1293. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1294. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1295. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1296. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1297. apic_write(APIC_ESR, 0);
  1298. apic_read(APIC_ESR);
  1299. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1300. apic_write(APIC_ESR, 0);
  1301. apic_read(APIC_ESR);
  1302. local_irq_restore(flags);
  1303. return 0;
  1304. }
  1305. /*
  1306. * This device has no shutdown method - fully functioning local APICs
  1307. * are needed on every CPU up until machine_halt/restart/poweroff.
  1308. */
  1309. static struct sysdev_class lapic_sysclass = {
  1310. .name = "lapic",
  1311. .resume = lapic_resume,
  1312. .suspend = lapic_suspend,
  1313. };
  1314. static struct sys_device device_lapic = {
  1315. .id = 0,
  1316. .cls = &lapic_sysclass,
  1317. };
  1318. static void __devinit apic_pm_activate(void)
  1319. {
  1320. apic_pm_state.active = 1;
  1321. }
  1322. static int __init init_lapic_sysfs(void)
  1323. {
  1324. int error;
  1325. if (!cpu_has_apic)
  1326. return 0;
  1327. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1328. error = sysdev_class_register(&lapic_sysclass);
  1329. if (!error)
  1330. error = sysdev_register(&device_lapic);
  1331. return error;
  1332. }
  1333. device_initcall(init_lapic_sysfs);
  1334. #else /* CONFIG_PM */
  1335. static void apic_pm_activate(void) { }
  1336. #endif /* CONFIG_PM */
  1337. /*
  1338. * APIC command line parameters
  1339. */
  1340. static int __init parse_lapic(char *arg)
  1341. {
  1342. enable_local_apic = 1;
  1343. return 0;
  1344. }
  1345. early_param("lapic", parse_lapic);
  1346. static int __init parse_nolapic(char *arg)
  1347. {
  1348. enable_local_apic = -1;
  1349. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1350. return 0;
  1351. }
  1352. early_param("nolapic", parse_nolapic);
  1353. static int __init parse_disable_lapic_timer(char *arg)
  1354. {
  1355. local_apic_timer_disabled = 1;
  1356. return 0;
  1357. }
  1358. early_param("nolapic_timer", parse_disable_lapic_timer);
  1359. static int __init parse_lapic_timer_c2_ok(char *arg)
  1360. {
  1361. local_apic_timer_c2_ok = 1;
  1362. return 0;
  1363. }
  1364. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1365. static int __init apic_set_verbosity(char *str)
  1366. {
  1367. if (strcmp("debug", str) == 0)
  1368. apic_verbosity = APIC_DEBUG;
  1369. else if (strcmp("verbose", str) == 0)
  1370. apic_verbosity = APIC_VERBOSE;
  1371. return 1;
  1372. }
  1373. __setup("apic=", apic_set_verbosity);