apic_32.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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 = 0;
  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 seperate 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 enable_NMI_through_LVT0 (void * dummy)
  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. /* We trust the pm timer based calibration */
  403. if (!pm_referenced) {
  404. apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
  405. /*
  406. * Setup the apic timer manually
  407. */
  408. levt->event_handler = lapic_cal_handler;
  409. lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
  410. lapic_cal_loops = -1;
  411. /* Let the interrupts run */
  412. local_irq_enable();
  413. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  414. cpu_relax();
  415. local_irq_disable();
  416. /* Stop the lapic timer */
  417. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
  418. local_irq_enable();
  419. /* Jiffies delta */
  420. deltaj = lapic_cal_j2 - lapic_cal_j1;
  421. apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
  422. /* Check, if the jiffies result is consistent */
  423. if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
  424. apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
  425. else
  426. local_apic_timer_verify_ok = 0;
  427. } else
  428. local_irq_enable();
  429. if (!local_apic_timer_verify_ok) {
  430. printk(KERN_WARNING
  431. "APIC timer disabled due to verification failure.\n");
  432. /* No broadcast on UP ! */
  433. if (num_possible_cpus() == 1)
  434. return;
  435. } else {
  436. /*
  437. * If nmi_watchdog is set to IO_APIC, we need the
  438. * PIT/HPET going. Otherwise register lapic as a dummy
  439. * device.
  440. */
  441. if (nmi_watchdog != NMI_IO_APIC)
  442. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  443. else
  444. printk(KERN_WARNING "APIC timer registered as dummy,"
  445. " due to nmi_watchdog=1!\n");
  446. }
  447. /* Setup the lapic or request the broadcast */
  448. setup_APIC_timer();
  449. }
  450. void __devinit setup_secondary_APIC_clock(void)
  451. {
  452. setup_APIC_timer();
  453. }
  454. /*
  455. * The guts of the apic timer interrupt
  456. */
  457. static void local_apic_timer_interrupt(void)
  458. {
  459. int cpu = smp_processor_id();
  460. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  461. /*
  462. * Normally we should not be here till LAPIC has been initialized but
  463. * in some cases like kdump, its possible that there is a pending LAPIC
  464. * timer interrupt from previous kernel's context and is delivered in
  465. * new kernel the moment interrupts are enabled.
  466. *
  467. * Interrupts are enabled early and LAPIC is setup much later, hence
  468. * its possible that when we get here evt->event_handler is NULL.
  469. * Check for event_handler being NULL and discard the interrupt as
  470. * spurious.
  471. */
  472. if (!evt->event_handler) {
  473. printk(KERN_WARNING
  474. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  475. /* Switch it off */
  476. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  477. return;
  478. }
  479. /*
  480. * the NMI deadlock-detector uses this.
  481. */
  482. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  483. evt->event_handler(evt);
  484. }
  485. /*
  486. * Local APIC timer interrupt. This is the most natural way for doing
  487. * local interrupts, but local timer interrupts can be emulated by
  488. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  489. *
  490. * [ if a single-CPU system runs an SMP kernel then we call the local
  491. * interrupt as well. Thus we cannot inline the local irq ... ]
  492. */
  493. void smp_apic_timer_interrupt(struct pt_regs *regs)
  494. {
  495. struct pt_regs *old_regs = set_irq_regs(regs);
  496. /*
  497. * NOTE! We'd better ACK the irq immediately,
  498. * because timer handling can be slow.
  499. */
  500. ack_APIC_irq();
  501. /*
  502. * update_process_times() expects us to have done irq_enter().
  503. * Besides, if we don't timer interrupts ignore the global
  504. * interrupt lock, which is the WrongThing (tm) to do.
  505. */
  506. irq_enter();
  507. local_apic_timer_interrupt();
  508. irq_exit();
  509. set_irq_regs(old_regs);
  510. }
  511. int setup_profiling_timer(unsigned int multiplier)
  512. {
  513. return -EINVAL;
  514. }
  515. /*
  516. * Local APIC start and shutdown
  517. */
  518. /**
  519. * clear_local_APIC - shutdown the local APIC
  520. *
  521. * This is called, when a CPU is disabled and before rebooting, so the state of
  522. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  523. * leftovers during boot.
  524. */
  525. void clear_local_APIC(void)
  526. {
  527. int maxlvt = lapic_get_maxlvt();
  528. u32 v;
  529. /*
  530. * Masking an LVT entry can trigger a local APIC error
  531. * if the vector is zero. Mask LVTERR first to prevent this.
  532. */
  533. if (maxlvt >= 3) {
  534. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  535. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  536. }
  537. /*
  538. * Careful: we have to set masks only first to deassert
  539. * any level-triggered sources.
  540. */
  541. v = apic_read(APIC_LVTT);
  542. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  543. v = apic_read(APIC_LVT0);
  544. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  545. v = apic_read(APIC_LVT1);
  546. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  547. if (maxlvt >= 4) {
  548. v = apic_read(APIC_LVTPC);
  549. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  550. }
  551. /* lets not touch this if we didn't frob it */
  552. #ifdef CONFIG_X86_MCE_P4THERMAL
  553. if (maxlvt >= 5) {
  554. v = apic_read(APIC_LVTTHMR);
  555. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  556. }
  557. #endif
  558. /*
  559. * Clean APIC state for other OSs:
  560. */
  561. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  562. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  563. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  564. if (maxlvt >= 3)
  565. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  566. if (maxlvt >= 4)
  567. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  568. #ifdef CONFIG_X86_MCE_P4THERMAL
  569. if (maxlvt >= 5)
  570. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  571. #endif
  572. /* Integrated APIC (!82489DX) ? */
  573. if (lapic_is_integrated()) {
  574. if (maxlvt > 3)
  575. /* Clear ESR due to Pentium errata 3AP and 11AP */
  576. apic_write(APIC_ESR, 0);
  577. apic_read(APIC_ESR);
  578. }
  579. }
  580. /**
  581. * disable_local_APIC - clear and disable the local APIC
  582. */
  583. void disable_local_APIC(void)
  584. {
  585. unsigned long value;
  586. clear_local_APIC();
  587. /*
  588. * Disable APIC (implies clearing of registers
  589. * for 82489DX!).
  590. */
  591. value = apic_read(APIC_SPIV);
  592. value &= ~APIC_SPIV_APIC_ENABLED;
  593. apic_write_around(APIC_SPIV, value);
  594. /*
  595. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  596. * restore the disabled state.
  597. */
  598. if (enabled_via_apicbase) {
  599. unsigned int l, h;
  600. rdmsr(MSR_IA32_APICBASE, l, h);
  601. l &= ~MSR_IA32_APICBASE_ENABLE;
  602. wrmsr(MSR_IA32_APICBASE, l, h);
  603. }
  604. }
  605. /*
  606. * If Linux enabled the LAPIC against the BIOS default disable it down before
  607. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  608. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  609. * for the case where Linux didn't enable the LAPIC.
  610. */
  611. void lapic_shutdown(void)
  612. {
  613. unsigned long flags;
  614. if (!cpu_has_apic)
  615. return;
  616. local_irq_save(flags);
  617. clear_local_APIC();
  618. if (enabled_via_apicbase)
  619. disable_local_APIC();
  620. local_irq_restore(flags);
  621. }
  622. /*
  623. * This is to verify that we're looking at a real local APIC.
  624. * Check these against your board if the CPUs aren't getting
  625. * started for no apparent reason.
  626. */
  627. int __init verify_local_APIC(void)
  628. {
  629. unsigned int reg0, reg1;
  630. /*
  631. * The version register is read-only in a real APIC.
  632. */
  633. reg0 = apic_read(APIC_LVR);
  634. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  635. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  636. reg1 = apic_read(APIC_LVR);
  637. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  638. /*
  639. * The two version reads above should print the same
  640. * numbers. If the second one is different, then we
  641. * poke at a non-APIC.
  642. */
  643. if (reg1 != reg0)
  644. return 0;
  645. /*
  646. * Check if the version looks reasonably.
  647. */
  648. reg1 = GET_APIC_VERSION(reg0);
  649. if (reg1 == 0x00 || reg1 == 0xff)
  650. return 0;
  651. reg1 = lapic_get_maxlvt();
  652. if (reg1 < 0x02 || reg1 == 0xff)
  653. return 0;
  654. /*
  655. * The ID register is read/write in a real APIC.
  656. */
  657. reg0 = apic_read(APIC_ID);
  658. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  659. /*
  660. * The next two are just to see if we have sane values.
  661. * They're only really relevant if we're in Virtual Wire
  662. * compatibility mode, but most boxes are anymore.
  663. */
  664. reg0 = apic_read(APIC_LVT0);
  665. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  666. reg1 = apic_read(APIC_LVT1);
  667. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  668. return 1;
  669. }
  670. /**
  671. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  672. */
  673. void __init sync_Arb_IDs(void)
  674. {
  675. /*
  676. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  677. * needed on AMD.
  678. */
  679. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  680. return;
  681. /*
  682. * Wait for idle.
  683. */
  684. apic_wait_icr_idle();
  685. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  686. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  687. | APIC_DM_INIT);
  688. }
  689. /*
  690. * An initial setup of the virtual wire mode.
  691. */
  692. void __init init_bsp_APIC(void)
  693. {
  694. unsigned long value;
  695. /*
  696. * Don't do the setup now if we have a SMP BIOS as the
  697. * through-I/O-APIC virtual wire mode might be active.
  698. */
  699. if (smp_found_config || !cpu_has_apic)
  700. return;
  701. /*
  702. * Do not trust the local APIC being empty at bootup.
  703. */
  704. clear_local_APIC();
  705. /*
  706. * Enable APIC.
  707. */
  708. value = apic_read(APIC_SPIV);
  709. value &= ~APIC_VECTOR_MASK;
  710. value |= APIC_SPIV_APIC_ENABLED;
  711. /* This bit is reserved on P4/Xeon and should be cleared */
  712. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  713. (boot_cpu_data.x86 == 15))
  714. value &= ~APIC_SPIV_FOCUS_DISABLED;
  715. else
  716. value |= APIC_SPIV_FOCUS_DISABLED;
  717. value |= SPURIOUS_APIC_VECTOR;
  718. apic_write_around(APIC_SPIV, value);
  719. /*
  720. * Set up the virtual wire mode.
  721. */
  722. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  723. value = APIC_DM_NMI;
  724. if (!lapic_is_integrated()) /* 82489DX */
  725. value |= APIC_LVT_LEVEL_TRIGGER;
  726. apic_write_around(APIC_LVT1, value);
  727. }
  728. /**
  729. * setup_local_APIC - setup the local APIC
  730. */
  731. void __cpuinit setup_local_APIC(void)
  732. {
  733. unsigned long oldvalue, value, maxlvt, integrated;
  734. int i, j;
  735. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  736. if (esr_disable) {
  737. apic_write(APIC_ESR, 0);
  738. apic_write(APIC_ESR, 0);
  739. apic_write(APIC_ESR, 0);
  740. apic_write(APIC_ESR, 0);
  741. }
  742. integrated = lapic_is_integrated();
  743. /*
  744. * Double-check whether this APIC is really registered.
  745. */
  746. if (!apic_id_registered())
  747. BUG();
  748. /*
  749. * Intel recommends to set DFR, LDR and TPR before enabling
  750. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  751. * document number 292116). So here it goes...
  752. */
  753. init_apic_ldr();
  754. /*
  755. * Set Task Priority to 'accept all'. We never change this
  756. * later on.
  757. */
  758. value = apic_read(APIC_TASKPRI);
  759. value &= ~APIC_TPRI_MASK;
  760. apic_write_around(APIC_TASKPRI, value);
  761. /*
  762. * After a crash, we no longer service the interrupts and a pending
  763. * interrupt from previous kernel might still have ISR bit set.
  764. *
  765. * Most probably by now CPU has serviced that pending interrupt and
  766. * it might not have done the ack_APIC_irq() because it thought,
  767. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  768. * does not clear the ISR bit and cpu thinks it has already serivced
  769. * the interrupt. Hence a vector might get locked. It was noticed
  770. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  771. */
  772. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  773. value = apic_read(APIC_ISR + i*0x10);
  774. for (j = 31; j >= 0; j--) {
  775. if (value & (1<<j))
  776. ack_APIC_irq();
  777. }
  778. }
  779. /*
  780. * Now that we are all set up, enable the APIC
  781. */
  782. value = apic_read(APIC_SPIV);
  783. value &= ~APIC_VECTOR_MASK;
  784. /*
  785. * Enable APIC
  786. */
  787. value |= APIC_SPIV_APIC_ENABLED;
  788. /*
  789. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  790. * certain networking cards. If high frequency interrupts are
  791. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  792. * entry is masked/unmasked at a high rate as well then sooner or
  793. * later IOAPIC line gets 'stuck', no more interrupts are received
  794. * from the device. If focus CPU is disabled then the hang goes
  795. * away, oh well :-(
  796. *
  797. * [ This bug can be reproduced easily with a level-triggered
  798. * PCI Ne2000 networking cards and PII/PIII processors, dual
  799. * BX chipset. ]
  800. */
  801. /*
  802. * Actually disabling the focus CPU check just makes the hang less
  803. * frequent as it makes the interrupt distributon model be more
  804. * like LRU than MRU (the short-term load is more even across CPUs).
  805. * See also the comment in end_level_ioapic_irq(). --macro
  806. */
  807. /* Enable focus processor (bit==0) */
  808. value &= ~APIC_SPIV_FOCUS_DISABLED;
  809. /*
  810. * Set spurious IRQ vector
  811. */
  812. value |= SPURIOUS_APIC_VECTOR;
  813. apic_write_around(APIC_SPIV, value);
  814. /*
  815. * Set up LVT0, LVT1:
  816. *
  817. * set up through-local-APIC on the BP's LINT0. This is not
  818. * strictly necessary in pure symmetric-IO mode, but sometimes
  819. * we delegate interrupts to the 8259A.
  820. */
  821. /*
  822. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  823. */
  824. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  825. if (!smp_processor_id() && (pic_mode || !value)) {
  826. value = APIC_DM_EXTINT;
  827. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  828. smp_processor_id());
  829. } else {
  830. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  831. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  832. smp_processor_id());
  833. }
  834. apic_write_around(APIC_LVT0, value);
  835. /*
  836. * only the BP should see the LINT1 NMI signal, obviously.
  837. */
  838. if (!smp_processor_id())
  839. value = APIC_DM_NMI;
  840. else
  841. value = APIC_DM_NMI | APIC_LVT_MASKED;
  842. if (!integrated) /* 82489DX */
  843. value |= APIC_LVT_LEVEL_TRIGGER;
  844. apic_write_around(APIC_LVT1, value);
  845. if (integrated && !esr_disable) { /* !82489DX */
  846. maxlvt = lapic_get_maxlvt();
  847. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  848. apic_write(APIC_ESR, 0);
  849. oldvalue = apic_read(APIC_ESR);
  850. /* enables sending errors */
  851. value = ERROR_APIC_VECTOR;
  852. apic_write_around(APIC_LVTERR, value);
  853. /*
  854. * spec says clear errors after enabling vector.
  855. */
  856. if (maxlvt > 3)
  857. apic_write(APIC_ESR, 0);
  858. value = apic_read(APIC_ESR);
  859. if (value != oldvalue)
  860. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  861. "vector: 0x%08lx after: 0x%08lx\n",
  862. oldvalue, value);
  863. } else {
  864. if (esr_disable)
  865. /*
  866. * Something untraceable is creating bad interrupts on
  867. * secondary quads ... for the moment, just leave the
  868. * ESR disabled - we can't do anything useful with the
  869. * errors anyway - mbligh
  870. */
  871. printk(KERN_INFO "Leaving ESR disabled.\n");
  872. else
  873. printk(KERN_INFO "No ESR for 82489DX.\n");
  874. }
  875. /* Disable the local apic timer */
  876. value = apic_read(APIC_LVTT);
  877. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  878. apic_write_around(APIC_LVTT, value);
  879. setup_apic_nmi_watchdog(NULL);
  880. apic_pm_activate();
  881. }
  882. /*
  883. * Detect and initialize APIC
  884. */
  885. static int __init detect_init_APIC (void)
  886. {
  887. u32 h, l, features;
  888. /* Disabled by kernel option? */
  889. if (enable_local_apic < 0)
  890. return -1;
  891. switch (boot_cpu_data.x86_vendor) {
  892. case X86_VENDOR_AMD:
  893. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  894. (boot_cpu_data.x86 == 15))
  895. break;
  896. goto no_apic;
  897. case X86_VENDOR_INTEL:
  898. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  899. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  900. break;
  901. goto no_apic;
  902. default:
  903. goto no_apic;
  904. }
  905. if (!cpu_has_apic) {
  906. /*
  907. * Over-ride BIOS and try to enable the local APIC only if
  908. * "lapic" specified.
  909. */
  910. if (enable_local_apic <= 0) {
  911. printk(KERN_INFO "Local APIC disabled by BIOS -- "
  912. "you can enable it with \"lapic\"\n");
  913. return -1;
  914. }
  915. /*
  916. * Some BIOSes disable the local APIC in the APIC_BASE
  917. * MSR. This can only be done in software for Intel P6 or later
  918. * and AMD K7 (Model > 1) or later.
  919. */
  920. rdmsr(MSR_IA32_APICBASE, l, h);
  921. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  922. printk(KERN_INFO
  923. "Local APIC disabled by BIOS -- reenabling.\n");
  924. l &= ~MSR_IA32_APICBASE_BASE;
  925. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  926. wrmsr(MSR_IA32_APICBASE, l, h);
  927. enabled_via_apicbase = 1;
  928. }
  929. }
  930. /*
  931. * The APIC feature bit should now be enabled
  932. * in `cpuid'
  933. */
  934. features = cpuid_edx(1);
  935. if (!(features & (1 << X86_FEATURE_APIC))) {
  936. printk(KERN_WARNING "Could not enable APIC!\n");
  937. return -1;
  938. }
  939. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  940. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  941. /* The BIOS may have set up the APIC at some other address */
  942. rdmsr(MSR_IA32_APICBASE, l, h);
  943. if (l & MSR_IA32_APICBASE_ENABLE)
  944. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  945. if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
  946. nmi_watchdog = NMI_LOCAL_APIC;
  947. printk(KERN_INFO "Found and enabled local APIC!\n");
  948. apic_pm_activate();
  949. return 0;
  950. no_apic:
  951. printk(KERN_INFO "No local APIC present or hardware disabled\n");
  952. return -1;
  953. }
  954. /**
  955. * init_apic_mappings - initialize APIC mappings
  956. */
  957. void __init init_apic_mappings(void)
  958. {
  959. unsigned long apic_phys;
  960. /*
  961. * If no local APIC can be found then set up a fake all
  962. * zeroes page to simulate the local APIC and another
  963. * one for the IO-APIC.
  964. */
  965. if (!smp_found_config && detect_init_APIC()) {
  966. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  967. apic_phys = __pa(apic_phys);
  968. } else
  969. apic_phys = mp_lapic_addr;
  970. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  971. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  972. apic_phys);
  973. /*
  974. * Fetch the APIC ID of the BSP in case we have a
  975. * default configuration (or the MP table is broken).
  976. */
  977. if (boot_cpu_physical_apicid == -1U)
  978. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  979. #ifdef CONFIG_X86_IO_APIC
  980. {
  981. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  982. int i;
  983. for (i = 0; i < nr_ioapics; i++) {
  984. if (smp_found_config) {
  985. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  986. if (!ioapic_phys) {
  987. printk(KERN_ERR
  988. "WARNING: bogus zero IO-APIC "
  989. "address found in MPTABLE, "
  990. "disabling IO/APIC support!\n");
  991. smp_found_config = 0;
  992. skip_ioapic_setup = 1;
  993. goto fake_ioapic_page;
  994. }
  995. } else {
  996. fake_ioapic_page:
  997. ioapic_phys = (unsigned long)
  998. alloc_bootmem_pages(PAGE_SIZE);
  999. ioapic_phys = __pa(ioapic_phys);
  1000. }
  1001. set_fixmap_nocache(idx, ioapic_phys);
  1002. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  1003. __fix_to_virt(idx), ioapic_phys);
  1004. idx++;
  1005. }
  1006. }
  1007. #endif
  1008. }
  1009. /*
  1010. * This initializes the IO-APIC and APIC hardware if this is
  1011. * a UP kernel.
  1012. */
  1013. int __init APIC_init_uniprocessor (void)
  1014. {
  1015. if (enable_local_apic < 0)
  1016. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1017. if (!smp_found_config && !cpu_has_apic)
  1018. return -1;
  1019. /*
  1020. * Complain if the BIOS pretends there is one.
  1021. */
  1022. if (!cpu_has_apic &&
  1023. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1024. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1025. boot_cpu_physical_apicid);
  1026. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1027. return -1;
  1028. }
  1029. verify_local_APIC();
  1030. connect_bsp_APIC();
  1031. /*
  1032. * Hack: In case of kdump, after a crash, kernel might be booting
  1033. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1034. * might be zero if read from MP tables. Get it from LAPIC.
  1035. */
  1036. #ifdef CONFIG_CRASH_DUMP
  1037. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  1038. #endif
  1039. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1040. setup_local_APIC();
  1041. #ifdef CONFIG_X86_IO_APIC
  1042. if (smp_found_config)
  1043. if (!skip_ioapic_setup && nr_ioapics)
  1044. setup_IO_APIC();
  1045. #endif
  1046. setup_boot_clock();
  1047. return 0;
  1048. }
  1049. /*
  1050. * Local APIC interrupts
  1051. */
  1052. /*
  1053. * This interrupt should _never_ happen with our APIC/SMP architecture
  1054. */
  1055. void smp_spurious_interrupt(struct pt_regs *regs)
  1056. {
  1057. unsigned long v;
  1058. irq_enter();
  1059. /*
  1060. * Check if this really is a spurious interrupt and ACK it
  1061. * if it is a vectored one. Just in case...
  1062. * Spurious interrupts should not be ACKed.
  1063. */
  1064. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1065. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1066. ack_APIC_irq();
  1067. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1068. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
  1069. "should never happen.\n", smp_processor_id());
  1070. __get_cpu_var(irq_stat).irq_spurious_count++;
  1071. irq_exit();
  1072. }
  1073. /*
  1074. * This interrupt should never happen with our APIC/SMP architecture
  1075. */
  1076. void smp_error_interrupt(struct pt_regs *regs)
  1077. {
  1078. unsigned long v, v1;
  1079. irq_enter();
  1080. /* First tickle the hardware, only then report what went on. -- REW */
  1081. v = apic_read(APIC_ESR);
  1082. apic_write(APIC_ESR, 0);
  1083. v1 = apic_read(APIC_ESR);
  1084. ack_APIC_irq();
  1085. atomic_inc(&irq_err_count);
  1086. /* Here is what the APIC error bits mean:
  1087. 0: Send CS error
  1088. 1: Receive CS error
  1089. 2: Send accept error
  1090. 3: Receive accept error
  1091. 4: Reserved
  1092. 5: Send illegal vector
  1093. 6: Received illegal vector
  1094. 7: Illegal register address
  1095. */
  1096. printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
  1097. smp_processor_id(), v , v1);
  1098. irq_exit();
  1099. }
  1100. /*
  1101. * Initialize APIC interrupts
  1102. */
  1103. void __init apic_intr_init(void)
  1104. {
  1105. #ifdef CONFIG_SMP
  1106. smp_intr_init();
  1107. #endif
  1108. /* self generated IPI for local APIC timer */
  1109. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  1110. /* IPI vectors for APIC spurious and error interrupts */
  1111. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  1112. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  1113. /* thermal monitor LVT interrupt */
  1114. #ifdef CONFIG_X86_MCE_P4THERMAL
  1115. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  1116. #endif
  1117. }
  1118. /**
  1119. * connect_bsp_APIC - attach the APIC to the interrupt system
  1120. */
  1121. void __init connect_bsp_APIC(void)
  1122. {
  1123. if (pic_mode) {
  1124. /*
  1125. * Do not trust the local APIC being empty at bootup.
  1126. */
  1127. clear_local_APIC();
  1128. /*
  1129. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1130. * local APIC to INT and NMI lines.
  1131. */
  1132. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1133. "enabling APIC mode.\n");
  1134. outb(0x70, 0x22);
  1135. outb(0x01, 0x23);
  1136. }
  1137. enable_apic_mode();
  1138. }
  1139. /**
  1140. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1141. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1142. *
  1143. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1144. * APIC is disabled.
  1145. */
  1146. void disconnect_bsp_APIC(int virt_wire_setup)
  1147. {
  1148. if (pic_mode) {
  1149. /*
  1150. * Put the board back into PIC mode (has an effect only on
  1151. * certain older boards). Note that APIC interrupts, including
  1152. * IPIs, won't work beyond this point! The only exception are
  1153. * INIT IPIs.
  1154. */
  1155. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1156. "entering PIC mode.\n");
  1157. outb(0x70, 0x22);
  1158. outb(0x00, 0x23);
  1159. } else {
  1160. /* Go back to Virtual Wire compatibility mode */
  1161. unsigned long value;
  1162. /* For the spurious interrupt use vector F, and enable it */
  1163. value = apic_read(APIC_SPIV);
  1164. value &= ~APIC_VECTOR_MASK;
  1165. value |= APIC_SPIV_APIC_ENABLED;
  1166. value |= 0xf;
  1167. apic_write_around(APIC_SPIV, value);
  1168. if (!virt_wire_setup) {
  1169. /*
  1170. * For LVT0 make it edge triggered, active high,
  1171. * external and enabled
  1172. */
  1173. value = apic_read(APIC_LVT0);
  1174. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1175. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1176. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
  1177. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1178. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1179. apic_write_around(APIC_LVT0, value);
  1180. } else {
  1181. /* Disable LVT0 */
  1182. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  1183. }
  1184. /*
  1185. * For LVT1 make it edge triggered, active high, nmi and
  1186. * enabled
  1187. */
  1188. value = apic_read(APIC_LVT1);
  1189. value &= ~(
  1190. APIC_MODE_MASK | APIC_SEND_PENDING |
  1191. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1192. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1193. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1194. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1195. apic_write_around(APIC_LVT1, value);
  1196. }
  1197. }
  1198. /*
  1199. * Power management
  1200. */
  1201. #ifdef CONFIG_PM
  1202. static struct {
  1203. int active;
  1204. /* r/w apic fields */
  1205. unsigned int apic_id;
  1206. unsigned int apic_taskpri;
  1207. unsigned int apic_ldr;
  1208. unsigned int apic_dfr;
  1209. unsigned int apic_spiv;
  1210. unsigned int apic_lvtt;
  1211. unsigned int apic_lvtpc;
  1212. unsigned int apic_lvt0;
  1213. unsigned int apic_lvt1;
  1214. unsigned int apic_lvterr;
  1215. unsigned int apic_tmict;
  1216. unsigned int apic_tdcr;
  1217. unsigned int apic_thmr;
  1218. } apic_pm_state;
  1219. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1220. {
  1221. unsigned long flags;
  1222. int maxlvt;
  1223. if (!apic_pm_state.active)
  1224. return 0;
  1225. maxlvt = lapic_get_maxlvt();
  1226. apic_pm_state.apic_id = apic_read(APIC_ID);
  1227. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1228. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1229. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1230. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1231. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1232. if (maxlvt >= 4)
  1233. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1234. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1235. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1236. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1237. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1238. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1239. #ifdef CONFIG_X86_MCE_P4THERMAL
  1240. if (maxlvt >= 5)
  1241. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1242. #endif
  1243. local_irq_save(flags);
  1244. disable_local_APIC();
  1245. local_irq_restore(flags);
  1246. return 0;
  1247. }
  1248. static int lapic_resume(struct sys_device *dev)
  1249. {
  1250. unsigned int l, h;
  1251. unsigned long flags;
  1252. int maxlvt;
  1253. if (!apic_pm_state.active)
  1254. return 0;
  1255. maxlvt = lapic_get_maxlvt();
  1256. local_irq_save(flags);
  1257. /*
  1258. * Make sure the APICBASE points to the right address
  1259. *
  1260. * FIXME! This will be wrong if we ever support suspend on
  1261. * SMP! We'll need to do this as part of the CPU restore!
  1262. */
  1263. rdmsr(MSR_IA32_APICBASE, l, h);
  1264. l &= ~MSR_IA32_APICBASE_BASE;
  1265. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1266. wrmsr(MSR_IA32_APICBASE, l, h);
  1267. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1268. apic_write(APIC_ID, apic_pm_state.apic_id);
  1269. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1270. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1271. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1272. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1273. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1274. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1275. #ifdef CONFIG_X86_MCE_P4THERMAL
  1276. if (maxlvt >= 5)
  1277. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1278. #endif
  1279. if (maxlvt >= 4)
  1280. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1281. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1282. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1283. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1284. apic_write(APIC_ESR, 0);
  1285. apic_read(APIC_ESR);
  1286. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1287. apic_write(APIC_ESR, 0);
  1288. apic_read(APIC_ESR);
  1289. local_irq_restore(flags);
  1290. return 0;
  1291. }
  1292. /*
  1293. * This device has no shutdown method - fully functioning local APICs
  1294. * are needed on every CPU up until machine_halt/restart/poweroff.
  1295. */
  1296. static struct sysdev_class lapic_sysclass = {
  1297. .name = "lapic",
  1298. .resume = lapic_resume,
  1299. .suspend = lapic_suspend,
  1300. };
  1301. static struct sys_device device_lapic = {
  1302. .id = 0,
  1303. .cls = &lapic_sysclass,
  1304. };
  1305. static void __devinit apic_pm_activate(void)
  1306. {
  1307. apic_pm_state.active = 1;
  1308. }
  1309. static int __init init_lapic_sysfs(void)
  1310. {
  1311. int error;
  1312. if (!cpu_has_apic)
  1313. return 0;
  1314. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1315. error = sysdev_class_register(&lapic_sysclass);
  1316. if (!error)
  1317. error = sysdev_register(&device_lapic);
  1318. return error;
  1319. }
  1320. device_initcall(init_lapic_sysfs);
  1321. #else /* CONFIG_PM */
  1322. static void apic_pm_activate(void) { }
  1323. #endif /* CONFIG_PM */
  1324. /*
  1325. * APIC command line parameters
  1326. */
  1327. static int __init parse_lapic(char *arg)
  1328. {
  1329. enable_local_apic = 1;
  1330. return 0;
  1331. }
  1332. early_param("lapic", parse_lapic);
  1333. static int __init parse_nolapic(char *arg)
  1334. {
  1335. enable_local_apic = -1;
  1336. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1337. return 0;
  1338. }
  1339. early_param("nolapic", parse_nolapic);
  1340. static int __init parse_disable_lapic_timer(char *arg)
  1341. {
  1342. local_apic_timer_disabled = 1;
  1343. return 0;
  1344. }
  1345. early_param("nolapic_timer", parse_disable_lapic_timer);
  1346. static int __init parse_lapic_timer_c2_ok(char *arg)
  1347. {
  1348. local_apic_timer_c2_ok = 1;
  1349. return 0;
  1350. }
  1351. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1352. static int __init apic_set_verbosity(char *str)
  1353. {
  1354. if (strcmp("debug", str) == 0)
  1355. apic_verbosity = APIC_DEBUG;
  1356. else if (strcmp("verbose", str) == 0)
  1357. apic_verbosity = APIC_VERBOSE;
  1358. return 1;
  1359. }
  1360. __setup("apic=", apic_set_verbosity);