apic_32.c 43 KB

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