apic_32.c 44 KB

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