apic.c 38 KB

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