apic.c 38 KB

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