apic_32.c 40 KB

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