apic_32.c 41 KB

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