apic_32.c 39 KB

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