apic.c 39 KB

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