apic.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  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/ioport.h>
  25. #include <linux/cpu.h>
  26. #include <linux/clockchips.h>
  27. #include <linux/acpi_pmtmr.h>
  28. #include <linux/module.h>
  29. #include <linux/dmi.h>
  30. #include <linux/dmar.h>
  31. #include <linux/ftrace.h>
  32. #include <linux/smp.h>
  33. #include <linux/nmi.h>
  34. #include <linux/timex.h>
  35. #include <asm/atomic.h>
  36. #include <asm/mtrr.h>
  37. #include <asm/mpspec.h>
  38. #include <asm/desc.h>
  39. #include <asm/arch_hooks.h>
  40. #include <asm/hpet.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/i8253.h>
  43. #include <asm/idle.h>
  44. #include <asm/proto.h>
  45. #include <asm/apic.h>
  46. #include <asm/i8259.h>
  47. #include <asm/smp.h>
  48. #include <mach_apic.h>
  49. #include <mach_apicdef.h>
  50. #include <mach_ipi.h>
  51. /*
  52. * Sanity check
  53. */
  54. #if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
  55. # error SPURIOUS_APIC_VECTOR definition error
  56. #endif
  57. #ifdef CONFIG_X86_32
  58. /*
  59. * Knob to control our willingness to enable the local APIC.
  60. *
  61. * +1=force-enable
  62. */
  63. static int force_enable_local_apic;
  64. /*
  65. * APIC command line parameters
  66. */
  67. static int __init parse_lapic(char *arg)
  68. {
  69. force_enable_local_apic = 1;
  70. return 0;
  71. }
  72. early_param("lapic", parse_lapic);
  73. /* Local APIC was disabled by the BIOS and enabled by the kernel */
  74. static int enabled_via_apicbase;
  75. #endif
  76. #ifdef CONFIG_X86_64
  77. static int apic_calibrate_pmtmr __initdata;
  78. static __init int setup_apicpmtimer(char *s)
  79. {
  80. apic_calibrate_pmtmr = 1;
  81. notsc_setup(NULL);
  82. return 0;
  83. }
  84. __setup("apicpmtimer", setup_apicpmtimer);
  85. #endif
  86. #ifdef CONFIG_X86_64
  87. #define HAVE_X2APIC
  88. #endif
  89. #ifdef HAVE_X2APIC
  90. int x2apic;
  91. /* x2apic enabled before OS handover */
  92. static int x2apic_preenabled;
  93. static int disable_x2apic;
  94. static __init int setup_nox2apic(char *str)
  95. {
  96. disable_x2apic = 1;
  97. setup_clear_cpu_cap(X86_FEATURE_X2APIC);
  98. return 0;
  99. }
  100. early_param("nox2apic", setup_nox2apic);
  101. #endif
  102. unsigned long mp_lapic_addr;
  103. int disable_apic;
  104. /* Disable local APIC timer from the kernel commandline or via dmi quirk */
  105. static int disable_apic_timer __cpuinitdata;
  106. /* Local APIC timer works in C2 */
  107. int local_apic_timer_c2_ok;
  108. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  109. int first_system_vector = 0xfe;
  110. /*
  111. * Debug level, exported for io_apic.c
  112. */
  113. unsigned int apic_verbosity;
  114. int pic_mode;
  115. /* Have we found an MP table */
  116. int smp_found_config;
  117. static struct resource lapic_resource = {
  118. .name = "Local APIC",
  119. .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
  120. };
  121. static unsigned int calibration_result;
  122. static int lapic_next_event(unsigned long delta,
  123. struct clock_event_device *evt);
  124. static void lapic_timer_setup(enum clock_event_mode mode,
  125. struct clock_event_device *evt);
  126. static void lapic_timer_broadcast(const struct cpumask *mask);
  127. static void apic_pm_activate(void);
  128. /*
  129. * The local apic timer can be used for any function which is CPU local.
  130. */
  131. static struct clock_event_device lapic_clockevent = {
  132. .name = "lapic",
  133. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  134. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  135. .shift = 32,
  136. .set_mode = lapic_timer_setup,
  137. .set_next_event = lapic_next_event,
  138. .broadcast = lapic_timer_broadcast,
  139. .rating = 100,
  140. .irq = -1,
  141. };
  142. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  143. static unsigned long apic_phys;
  144. /*
  145. * Get the LAPIC version
  146. */
  147. static inline int lapic_get_version(void)
  148. {
  149. return GET_APIC_VERSION(apic_read(APIC_LVR));
  150. }
  151. /*
  152. * Check, if the APIC is integrated or a separate chip
  153. */
  154. static inline int lapic_is_integrated(void)
  155. {
  156. #ifdef CONFIG_X86_64
  157. return 1;
  158. #else
  159. return APIC_INTEGRATED(lapic_get_version());
  160. #endif
  161. }
  162. /*
  163. * Check, whether this is a modern or a first generation APIC
  164. */
  165. static int modern_apic(void)
  166. {
  167. /* AMD systems use old APIC versions, so check the CPU */
  168. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  169. boot_cpu_data.x86 >= 0xf)
  170. return 1;
  171. return lapic_get_version() >= 0x14;
  172. }
  173. /*
  174. * Paravirt kernels also might be using these below ops. So we still
  175. * use generic apic_read()/apic_write(), which might be pointing to different
  176. * ops in PARAVIRT case.
  177. */
  178. void xapic_wait_icr_idle(void)
  179. {
  180. while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
  181. cpu_relax();
  182. }
  183. u32 safe_xapic_wait_icr_idle(void)
  184. {
  185. u32 send_status;
  186. int timeout;
  187. timeout = 0;
  188. do {
  189. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  190. if (!send_status)
  191. break;
  192. udelay(100);
  193. } while (timeout++ < 1000);
  194. return send_status;
  195. }
  196. void xapic_icr_write(u32 low, u32 id)
  197. {
  198. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
  199. apic_write(APIC_ICR, low);
  200. }
  201. static u64 xapic_icr_read(void)
  202. {
  203. u32 icr1, icr2;
  204. icr2 = apic_read(APIC_ICR2);
  205. icr1 = apic_read(APIC_ICR);
  206. return icr1 | ((u64)icr2 << 32);
  207. }
  208. static struct apic_ops xapic_ops = {
  209. .read = native_apic_mem_read,
  210. .write = native_apic_mem_write,
  211. .icr_read = xapic_icr_read,
  212. .icr_write = xapic_icr_write,
  213. .wait_icr_idle = xapic_wait_icr_idle,
  214. .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
  215. };
  216. struct apic_ops __read_mostly *apic_ops = &xapic_ops;
  217. EXPORT_SYMBOL_GPL(apic_ops);
  218. #ifdef HAVE_X2APIC
  219. static void x2apic_wait_icr_idle(void)
  220. {
  221. /* no need to wait for icr idle in x2apic */
  222. return;
  223. }
  224. static u32 safe_x2apic_wait_icr_idle(void)
  225. {
  226. /* no need to wait for icr idle in x2apic */
  227. return 0;
  228. }
  229. void x2apic_icr_write(u32 low, u32 id)
  230. {
  231. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  232. }
  233. static u64 x2apic_icr_read(void)
  234. {
  235. unsigned long val;
  236. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  237. return val;
  238. }
  239. static struct apic_ops x2apic_ops = {
  240. .read = native_apic_msr_read,
  241. .write = native_apic_msr_write,
  242. .icr_read = x2apic_icr_read,
  243. .icr_write = x2apic_icr_write,
  244. .wait_icr_idle = x2apic_wait_icr_idle,
  245. .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
  246. };
  247. #endif
  248. /**
  249. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  250. */
  251. void __cpuinit enable_NMI_through_LVT0(void)
  252. {
  253. unsigned int v;
  254. /* unmask and set to NMI */
  255. v = APIC_DM_NMI;
  256. /* Level triggered for 82489DX (32bit mode) */
  257. if (!lapic_is_integrated())
  258. v |= APIC_LVT_LEVEL_TRIGGER;
  259. apic_write(APIC_LVT0, v);
  260. }
  261. #ifdef CONFIG_X86_32
  262. /**
  263. * get_physical_broadcast - Get number of physical broadcast IDs
  264. */
  265. int get_physical_broadcast(void)
  266. {
  267. return modern_apic() ? 0xff : 0xf;
  268. }
  269. #endif
  270. /**
  271. * lapic_get_maxlvt - get the maximum number of local vector table entries
  272. */
  273. int lapic_get_maxlvt(void)
  274. {
  275. unsigned int v;
  276. v = apic_read(APIC_LVR);
  277. /*
  278. * - we always have APIC integrated on 64bit mode
  279. * - 82489DXs do not report # of LVT entries
  280. */
  281. return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
  282. }
  283. /*
  284. * Local APIC timer
  285. */
  286. /* Clock divisor */
  287. #define APIC_DIVISOR 16
  288. /*
  289. * This function sets up the local APIC timer, with a timeout of
  290. * 'clocks' APIC bus clock. During calibration we actually call
  291. * this function twice on the boot CPU, once with a bogus timeout
  292. * value, second time for real. The other (noncalibrating) CPUs
  293. * call this function only once, with the real, calibrated value.
  294. *
  295. * We do reads before writes even if unnecessary, to get around the
  296. * P5 APIC double write bug.
  297. */
  298. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  299. {
  300. unsigned int lvtt_value, tmp_value;
  301. lvtt_value = LOCAL_TIMER_VECTOR;
  302. if (!oneshot)
  303. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  304. if (!lapic_is_integrated())
  305. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  306. if (!irqen)
  307. lvtt_value |= APIC_LVT_MASKED;
  308. apic_write(APIC_LVTT, lvtt_value);
  309. /*
  310. * Divide PICLK by 16
  311. */
  312. tmp_value = apic_read(APIC_TDCR);
  313. apic_write(APIC_TDCR,
  314. (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
  315. APIC_TDR_DIV_16);
  316. if (!oneshot)
  317. apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
  318. }
  319. /*
  320. * Setup extended LVT, AMD specific (K8, family 10h)
  321. *
  322. * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
  323. * MCE interrupts are supported. Thus MCE offset must be set to 0.
  324. *
  325. * If mask=1, the LVT entry does not generate interrupts while mask=0
  326. * enables the vector. See also the BKDGs.
  327. */
  328. #define APIC_EILVT_LVTOFF_MCE 0
  329. #define APIC_EILVT_LVTOFF_IBS 1
  330. static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
  331. {
  332. unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
  333. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  334. apic_write(reg, v);
  335. }
  336. u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
  337. {
  338. setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
  339. return APIC_EILVT_LVTOFF_MCE;
  340. }
  341. u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
  342. {
  343. setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
  344. return APIC_EILVT_LVTOFF_IBS;
  345. }
  346. EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
  347. /*
  348. * Program the next event, relative to now
  349. */
  350. static int lapic_next_event(unsigned long delta,
  351. struct clock_event_device *evt)
  352. {
  353. apic_write(APIC_TMICT, delta);
  354. return 0;
  355. }
  356. /*
  357. * Setup the lapic timer in periodic or oneshot mode
  358. */
  359. static void lapic_timer_setup(enum clock_event_mode mode,
  360. struct clock_event_device *evt)
  361. {
  362. unsigned long flags;
  363. unsigned int v;
  364. /* Lapic used as dummy for broadcast ? */
  365. if (evt->features & CLOCK_EVT_FEAT_DUMMY)
  366. return;
  367. local_irq_save(flags);
  368. switch (mode) {
  369. case CLOCK_EVT_MODE_PERIODIC:
  370. case CLOCK_EVT_MODE_ONESHOT:
  371. __setup_APIC_LVTT(calibration_result,
  372. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  373. break;
  374. case CLOCK_EVT_MODE_UNUSED:
  375. case CLOCK_EVT_MODE_SHUTDOWN:
  376. v = apic_read(APIC_LVTT);
  377. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  378. apic_write(APIC_LVTT, v);
  379. apic_write(APIC_TMICT, 0xffffffff);
  380. break;
  381. case CLOCK_EVT_MODE_RESUME:
  382. /* Nothing to do here */
  383. break;
  384. }
  385. local_irq_restore(flags);
  386. }
  387. /*
  388. * Local APIC timer broadcast function
  389. */
  390. static void lapic_timer_broadcast(const struct cpumask *mask)
  391. {
  392. #ifdef CONFIG_SMP
  393. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  394. #endif
  395. }
  396. /*
  397. * Setup the local APIC timer for this CPU. Copy the initilized values
  398. * of the boot CPU and register the clock event in the framework.
  399. */
  400. static void __cpuinit setup_APIC_timer(void)
  401. {
  402. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  403. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  404. levt->cpumask = cpumask_of(smp_processor_id());
  405. clockevents_register_device(levt);
  406. }
  407. /*
  408. * In this functions we calibrate APIC bus clocks to the external timer.
  409. *
  410. * We want to do the calibration only once since we want to have local timer
  411. * irqs syncron. CPUs connected by the same APIC bus have the very same bus
  412. * frequency.
  413. *
  414. * This was previously done by reading the PIT/HPET and waiting for a wrap
  415. * around to find out, that a tick has elapsed. I have a box, where the PIT
  416. * readout is broken, so it never gets out of the wait loop again. This was
  417. * also reported by others.
  418. *
  419. * Monitoring the jiffies value is inaccurate and the clockevents
  420. * infrastructure allows us to do a simple substitution of the interrupt
  421. * handler.
  422. *
  423. * The calibration routine also uses the pm_timer when possible, as the PIT
  424. * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
  425. * back to normal later in the boot process).
  426. */
  427. #define LAPIC_CAL_LOOPS (HZ/10)
  428. static __initdata int lapic_cal_loops = -1;
  429. static __initdata long lapic_cal_t1, lapic_cal_t2;
  430. static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
  431. static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
  432. static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
  433. /*
  434. * Temporary interrupt handler.
  435. */
  436. static void __init lapic_cal_handler(struct clock_event_device *dev)
  437. {
  438. unsigned long long tsc = 0;
  439. long tapic = apic_read(APIC_TMCCT);
  440. unsigned long pm = acpi_pm_read_early();
  441. if (cpu_has_tsc)
  442. rdtscll(tsc);
  443. switch (lapic_cal_loops++) {
  444. case 0:
  445. lapic_cal_t1 = tapic;
  446. lapic_cal_tsc1 = tsc;
  447. lapic_cal_pm1 = pm;
  448. lapic_cal_j1 = jiffies;
  449. break;
  450. case LAPIC_CAL_LOOPS:
  451. lapic_cal_t2 = tapic;
  452. lapic_cal_tsc2 = tsc;
  453. if (pm < lapic_cal_pm1)
  454. pm += ACPI_PM_OVRRUN;
  455. lapic_cal_pm2 = pm;
  456. lapic_cal_j2 = jiffies;
  457. break;
  458. }
  459. }
  460. static int __init calibrate_by_pmtimer(long deltapm, long *delta)
  461. {
  462. const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
  463. const long pm_thresh = pm_100ms / 100;
  464. unsigned long mult;
  465. u64 res;
  466. #ifndef CONFIG_X86_PM_TIMER
  467. return -1;
  468. #endif
  469. apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
  470. /* Check, if the PM timer is available */
  471. if (!deltapm)
  472. return -1;
  473. mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
  474. if (deltapm > (pm_100ms - pm_thresh) &&
  475. deltapm < (pm_100ms + pm_thresh)) {
  476. apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
  477. } else {
  478. res = (((u64)deltapm) * mult) >> 22;
  479. do_div(res, 1000000);
  480. pr_warning("APIC calibration not consistent "
  481. "with PM Timer: %ldms instead of 100ms\n",
  482. (long)res);
  483. /* Correct the lapic counter value */
  484. res = (((u64)(*delta)) * pm_100ms);
  485. do_div(res, deltapm);
  486. pr_info("APIC delta adjusted to PM-Timer: "
  487. "%lu (%ld)\n", (unsigned long)res, *delta);
  488. *delta = (long)res;
  489. }
  490. return 0;
  491. }
  492. static int __init calibrate_APIC_clock(void)
  493. {
  494. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  495. void (*real_handler)(struct clock_event_device *dev);
  496. unsigned long deltaj;
  497. long delta;
  498. int pm_referenced = 0;
  499. local_irq_disable();
  500. /* Replace the global interrupt handler */
  501. real_handler = global_clock_event->event_handler;
  502. global_clock_event->event_handler = lapic_cal_handler;
  503. /*
  504. * Setup the APIC counter to maximum. There is no way the lapic
  505. * can underflow in the 100ms detection time frame
  506. */
  507. __setup_APIC_LVTT(0xffffffff, 0, 0);
  508. /* Let the interrupts run */
  509. local_irq_enable();
  510. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  511. cpu_relax();
  512. local_irq_disable();
  513. /* Restore the real event handler */
  514. global_clock_event->event_handler = real_handler;
  515. /* Build delta t1-t2 as apic timer counts down */
  516. delta = lapic_cal_t1 - lapic_cal_t2;
  517. apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
  518. /* we trust the PM based calibration if possible */
  519. pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
  520. &delta);
  521. /* Calculate the scaled math multiplication factor */
  522. lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
  523. lapic_clockevent.shift);
  524. lapic_clockevent.max_delta_ns =
  525. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  526. lapic_clockevent.min_delta_ns =
  527. clockevent_delta2ns(0xF, &lapic_clockevent);
  528. calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
  529. apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
  530. apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
  531. apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
  532. calibration_result);
  533. if (cpu_has_tsc) {
  534. delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
  535. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  536. "%ld.%04ld MHz.\n",
  537. (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
  538. (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
  539. }
  540. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  541. "%u.%04u MHz.\n",
  542. calibration_result / (1000000 / HZ),
  543. calibration_result % (1000000 / HZ));
  544. /*
  545. * Do a sanity check on the APIC calibration result
  546. */
  547. if (calibration_result < (1000000 / HZ)) {
  548. local_irq_enable();
  549. pr_warning("APIC frequency too slow, disabling apic timer\n");
  550. return -1;
  551. }
  552. levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
  553. /*
  554. * PM timer calibration failed or not turned on
  555. * so lets try APIC timer based calibration
  556. */
  557. if (!pm_referenced) {
  558. apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
  559. /*
  560. * Setup the apic timer manually
  561. */
  562. levt->event_handler = lapic_cal_handler;
  563. lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
  564. lapic_cal_loops = -1;
  565. /* Let the interrupts run */
  566. local_irq_enable();
  567. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  568. cpu_relax();
  569. /* Stop the lapic timer */
  570. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
  571. /* Jiffies delta */
  572. deltaj = lapic_cal_j2 - lapic_cal_j1;
  573. apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
  574. /* Check, if the jiffies result is consistent */
  575. if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
  576. apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
  577. else
  578. levt->features |= CLOCK_EVT_FEAT_DUMMY;
  579. } else
  580. local_irq_enable();
  581. if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
  582. pr_warning("APIC timer disabled due to verification failure\n");
  583. return -1;
  584. }
  585. return 0;
  586. }
  587. /*
  588. * Setup the boot APIC
  589. *
  590. * Calibrate and verify the result.
  591. */
  592. void __init setup_boot_APIC_clock(void)
  593. {
  594. /*
  595. * The local apic timer can be disabled via the kernel
  596. * commandline or from the CPU detection code. Register the lapic
  597. * timer as a dummy clock event source on SMP systems, so the
  598. * broadcast mechanism is used. On UP systems simply ignore it.
  599. */
  600. if (disable_apic_timer) {
  601. pr_info("Disabling APIC timer\n");
  602. /* No broadcast on UP ! */
  603. if (num_possible_cpus() > 1) {
  604. lapic_clockevent.mult = 1;
  605. setup_APIC_timer();
  606. }
  607. return;
  608. }
  609. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
  610. "calibrating APIC timer ...\n");
  611. if (calibrate_APIC_clock()) {
  612. /* No broadcast on UP ! */
  613. if (num_possible_cpus() > 1)
  614. setup_APIC_timer();
  615. return;
  616. }
  617. /*
  618. * If nmi_watchdog is set to IO_APIC, we need the
  619. * PIT/HPET going. Otherwise register lapic as a dummy
  620. * device.
  621. */
  622. if (nmi_watchdog != NMI_IO_APIC)
  623. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  624. else
  625. pr_warning("APIC timer registered as dummy,"
  626. " due to nmi_watchdog=%d!\n", nmi_watchdog);
  627. /* Setup the lapic or request the broadcast */
  628. setup_APIC_timer();
  629. }
  630. void __cpuinit setup_secondary_APIC_clock(void)
  631. {
  632. setup_APIC_timer();
  633. }
  634. /*
  635. * The guts of the apic timer interrupt
  636. */
  637. static void local_apic_timer_interrupt(void)
  638. {
  639. int cpu = smp_processor_id();
  640. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  641. /*
  642. * Normally we should not be here till LAPIC has been initialized but
  643. * in some cases like kdump, its possible that there is a pending LAPIC
  644. * timer interrupt from previous kernel's context and is delivered in
  645. * new kernel the moment interrupts are enabled.
  646. *
  647. * Interrupts are enabled early and LAPIC is setup much later, hence
  648. * its possible that when we get here evt->event_handler is NULL.
  649. * Check for event_handler being NULL and discard the interrupt as
  650. * spurious.
  651. */
  652. if (!evt->event_handler) {
  653. pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  654. /* Switch it off */
  655. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  656. return;
  657. }
  658. /*
  659. * the NMI deadlock-detector uses this.
  660. */
  661. inc_irq_stat(apic_timer_irqs);
  662. evt->event_handler(evt);
  663. }
  664. /*
  665. * Local APIC timer interrupt. This is the most natural way for doing
  666. * local interrupts, but local timer interrupts can be emulated by
  667. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  668. *
  669. * [ if a single-CPU system runs an SMP kernel then we call the local
  670. * interrupt as well. Thus we cannot inline the local irq ... ]
  671. */
  672. void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
  673. {
  674. struct pt_regs *old_regs = set_irq_regs(regs);
  675. /*
  676. * NOTE! We'd better ACK the irq immediately,
  677. * because timer handling can be slow.
  678. */
  679. ack_APIC_irq();
  680. /*
  681. * update_process_times() expects us to have done irq_enter().
  682. * Besides, if we don't timer interrupts ignore the global
  683. * interrupt lock, which is the WrongThing (tm) to do.
  684. */
  685. exit_idle();
  686. irq_enter();
  687. local_apic_timer_interrupt();
  688. irq_exit();
  689. set_irq_regs(old_regs);
  690. }
  691. int setup_profiling_timer(unsigned int multiplier)
  692. {
  693. return -EINVAL;
  694. }
  695. /*
  696. * Local APIC start and shutdown
  697. */
  698. /**
  699. * clear_local_APIC - shutdown the local APIC
  700. *
  701. * This is called, when a CPU is disabled and before rebooting, so the state of
  702. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  703. * leftovers during boot.
  704. */
  705. void clear_local_APIC(void)
  706. {
  707. int maxlvt;
  708. u32 v;
  709. /* APIC hasn't been mapped yet */
  710. if (!apic_phys)
  711. return;
  712. maxlvt = lapic_get_maxlvt();
  713. /*
  714. * Masking an LVT entry can trigger a local APIC error
  715. * if the vector is zero. Mask LVTERR first to prevent this.
  716. */
  717. if (maxlvt >= 3) {
  718. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  719. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  720. }
  721. /*
  722. * Careful: we have to set masks only first to deassert
  723. * any level-triggered sources.
  724. */
  725. v = apic_read(APIC_LVTT);
  726. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  727. v = apic_read(APIC_LVT0);
  728. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  729. v = apic_read(APIC_LVT1);
  730. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  731. if (maxlvt >= 4) {
  732. v = apic_read(APIC_LVTPC);
  733. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  734. }
  735. /* lets not touch this if we didn't frob it */
  736. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  737. if (maxlvt >= 5) {
  738. v = apic_read(APIC_LVTTHMR);
  739. apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  740. }
  741. #endif
  742. /*
  743. * Clean APIC state for other OSs:
  744. */
  745. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  746. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  747. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  748. if (maxlvt >= 3)
  749. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  750. if (maxlvt >= 4)
  751. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  752. /* Integrated APIC (!82489DX) ? */
  753. if (lapic_is_integrated()) {
  754. if (maxlvt > 3)
  755. /* Clear ESR due to Pentium errata 3AP and 11AP */
  756. apic_write(APIC_ESR, 0);
  757. apic_read(APIC_ESR);
  758. }
  759. }
  760. /**
  761. * disable_local_APIC - clear and disable the local APIC
  762. */
  763. void disable_local_APIC(void)
  764. {
  765. unsigned int value;
  766. /* APIC hasn't been mapped yet */
  767. if (!apic_phys)
  768. return;
  769. clear_local_APIC();
  770. /*
  771. * Disable APIC (implies clearing of registers
  772. * for 82489DX!).
  773. */
  774. value = apic_read(APIC_SPIV);
  775. value &= ~APIC_SPIV_APIC_ENABLED;
  776. apic_write(APIC_SPIV, value);
  777. #ifdef CONFIG_X86_32
  778. /*
  779. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  780. * restore the disabled state.
  781. */
  782. if (enabled_via_apicbase) {
  783. unsigned int l, h;
  784. rdmsr(MSR_IA32_APICBASE, l, h);
  785. l &= ~MSR_IA32_APICBASE_ENABLE;
  786. wrmsr(MSR_IA32_APICBASE, l, h);
  787. }
  788. #endif
  789. }
  790. /*
  791. * If Linux enabled the LAPIC against the BIOS default disable it down before
  792. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  793. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  794. * for the case where Linux didn't enable the LAPIC.
  795. */
  796. void lapic_shutdown(void)
  797. {
  798. unsigned long flags;
  799. if (!cpu_has_apic)
  800. return;
  801. local_irq_save(flags);
  802. #ifdef CONFIG_X86_32
  803. if (!enabled_via_apicbase)
  804. clear_local_APIC();
  805. else
  806. #endif
  807. disable_local_APIC();
  808. local_irq_restore(flags);
  809. }
  810. /*
  811. * This is to verify that we're looking at a real local APIC.
  812. * Check these against your board if the CPUs aren't getting
  813. * started for no apparent reason.
  814. */
  815. int __init verify_local_APIC(void)
  816. {
  817. unsigned int reg0, reg1;
  818. /*
  819. * The version register is read-only in a real APIC.
  820. */
  821. reg0 = apic_read(APIC_LVR);
  822. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  823. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  824. reg1 = apic_read(APIC_LVR);
  825. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  826. /*
  827. * The two version reads above should print the same
  828. * numbers. If the second one is different, then we
  829. * poke at a non-APIC.
  830. */
  831. if (reg1 != reg0)
  832. return 0;
  833. /*
  834. * Check if the version looks reasonably.
  835. */
  836. reg1 = GET_APIC_VERSION(reg0);
  837. if (reg1 == 0x00 || reg1 == 0xff)
  838. return 0;
  839. reg1 = lapic_get_maxlvt();
  840. if (reg1 < 0x02 || reg1 == 0xff)
  841. return 0;
  842. /*
  843. * The ID register is read/write in a real APIC.
  844. */
  845. reg0 = apic_read(APIC_ID);
  846. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  847. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  848. reg1 = apic_read(APIC_ID);
  849. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  850. apic_write(APIC_ID, reg0);
  851. if (reg1 != (reg0 ^ APIC_ID_MASK))
  852. return 0;
  853. /*
  854. * The next two are just to see if we have sane values.
  855. * They're only really relevant if we're in Virtual Wire
  856. * compatibility mode, but most boxes are anymore.
  857. */
  858. reg0 = apic_read(APIC_LVT0);
  859. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  860. reg1 = apic_read(APIC_LVT1);
  861. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  862. return 1;
  863. }
  864. /**
  865. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  866. */
  867. void __init sync_Arb_IDs(void)
  868. {
  869. /*
  870. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  871. * needed on AMD.
  872. */
  873. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  874. return;
  875. /*
  876. * Wait for idle.
  877. */
  878. apic_wait_icr_idle();
  879. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  880. apic_write(APIC_ICR, APIC_DEST_ALLINC |
  881. APIC_INT_LEVELTRIG | APIC_DM_INIT);
  882. }
  883. /*
  884. * An initial setup of the virtual wire mode.
  885. */
  886. void __init init_bsp_APIC(void)
  887. {
  888. unsigned int value;
  889. /*
  890. * Don't do the setup now if we have a SMP BIOS as the
  891. * through-I/O-APIC virtual wire mode might be active.
  892. */
  893. if (smp_found_config || !cpu_has_apic)
  894. return;
  895. /*
  896. * Do not trust the local APIC being empty at bootup.
  897. */
  898. clear_local_APIC();
  899. /*
  900. * Enable APIC.
  901. */
  902. value = apic_read(APIC_SPIV);
  903. value &= ~APIC_VECTOR_MASK;
  904. value |= APIC_SPIV_APIC_ENABLED;
  905. #ifdef CONFIG_X86_32
  906. /* This bit is reserved on P4/Xeon and should be cleared */
  907. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  908. (boot_cpu_data.x86 == 15))
  909. value &= ~APIC_SPIV_FOCUS_DISABLED;
  910. else
  911. #endif
  912. value |= APIC_SPIV_FOCUS_DISABLED;
  913. value |= SPURIOUS_APIC_VECTOR;
  914. apic_write(APIC_SPIV, value);
  915. /*
  916. * Set up the virtual wire mode.
  917. */
  918. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  919. value = APIC_DM_NMI;
  920. if (!lapic_is_integrated()) /* 82489DX */
  921. value |= APIC_LVT_LEVEL_TRIGGER;
  922. apic_write(APIC_LVT1, value);
  923. }
  924. static void __cpuinit lapic_setup_esr(void)
  925. {
  926. unsigned int oldvalue, value, maxlvt;
  927. if (!lapic_is_integrated()) {
  928. pr_info("No ESR for 82489DX.\n");
  929. return;
  930. }
  931. if (esr_disable) {
  932. /*
  933. * Something untraceable is creating bad interrupts on
  934. * secondary quads ... for the moment, just leave the
  935. * ESR disabled - we can't do anything useful with the
  936. * errors anyway - mbligh
  937. */
  938. pr_info("Leaving ESR disabled.\n");
  939. return;
  940. }
  941. maxlvt = lapic_get_maxlvt();
  942. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  943. apic_write(APIC_ESR, 0);
  944. oldvalue = apic_read(APIC_ESR);
  945. /* enables sending errors */
  946. value = ERROR_APIC_VECTOR;
  947. apic_write(APIC_LVTERR, value);
  948. /*
  949. * spec says clear errors after enabling vector.
  950. */
  951. if (maxlvt > 3)
  952. apic_write(APIC_ESR, 0);
  953. value = apic_read(APIC_ESR);
  954. if (value != oldvalue)
  955. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  956. "vector: 0x%08x after: 0x%08x\n",
  957. oldvalue, value);
  958. }
  959. /**
  960. * setup_local_APIC - setup the local APIC
  961. */
  962. void __cpuinit setup_local_APIC(void)
  963. {
  964. unsigned int value;
  965. int i, j;
  966. #ifdef CONFIG_X86_32
  967. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  968. if (lapic_is_integrated() && esr_disable) {
  969. apic_write(APIC_ESR, 0);
  970. apic_write(APIC_ESR, 0);
  971. apic_write(APIC_ESR, 0);
  972. apic_write(APIC_ESR, 0);
  973. }
  974. #endif
  975. preempt_disable();
  976. /*
  977. * Double-check whether this APIC is really registered.
  978. * This is meaningless in clustered apic mode, so we skip it.
  979. */
  980. if (!apic_id_registered())
  981. BUG();
  982. /*
  983. * Intel recommends to set DFR, LDR and TPR before enabling
  984. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  985. * document number 292116). So here it goes...
  986. */
  987. init_apic_ldr();
  988. /*
  989. * Set Task Priority to 'accept all'. We never change this
  990. * later on.
  991. */
  992. value = apic_read(APIC_TASKPRI);
  993. value &= ~APIC_TPRI_MASK;
  994. apic_write(APIC_TASKPRI, value);
  995. /*
  996. * After a crash, we no longer service the interrupts and a pending
  997. * interrupt from previous kernel might still have ISR bit set.
  998. *
  999. * Most probably by now CPU has serviced that pending interrupt and
  1000. * it might not have done the ack_APIC_irq() because it thought,
  1001. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  1002. * does not clear the ISR bit and cpu thinks it has already serivced
  1003. * the interrupt. Hence a vector might get locked. It was noticed
  1004. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  1005. */
  1006. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  1007. value = apic_read(APIC_ISR + i*0x10);
  1008. for (j = 31; j >= 0; j--) {
  1009. if (value & (1<<j))
  1010. ack_APIC_irq();
  1011. }
  1012. }
  1013. /*
  1014. * Now that we are all set up, enable the APIC
  1015. */
  1016. value = apic_read(APIC_SPIV);
  1017. value &= ~APIC_VECTOR_MASK;
  1018. /*
  1019. * Enable APIC
  1020. */
  1021. value |= APIC_SPIV_APIC_ENABLED;
  1022. #ifdef CONFIG_X86_32
  1023. /*
  1024. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  1025. * certain networking cards. If high frequency interrupts are
  1026. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  1027. * entry is masked/unmasked at a high rate as well then sooner or
  1028. * later IOAPIC line gets 'stuck', no more interrupts are received
  1029. * from the device. If focus CPU is disabled then the hang goes
  1030. * away, oh well :-(
  1031. *
  1032. * [ This bug can be reproduced easily with a level-triggered
  1033. * PCI Ne2000 networking cards and PII/PIII processors, dual
  1034. * BX chipset. ]
  1035. */
  1036. /*
  1037. * Actually disabling the focus CPU check just makes the hang less
  1038. * frequent as it makes the interrupt distributon model be more
  1039. * like LRU than MRU (the short-term load is more even across CPUs).
  1040. * See also the comment in end_level_ioapic_irq(). --macro
  1041. */
  1042. /*
  1043. * - enable focus processor (bit==0)
  1044. * - 64bit mode always use processor focus
  1045. * so no need to set it
  1046. */
  1047. value &= ~APIC_SPIV_FOCUS_DISABLED;
  1048. #endif
  1049. /*
  1050. * Set spurious IRQ vector
  1051. */
  1052. value |= SPURIOUS_APIC_VECTOR;
  1053. apic_write(APIC_SPIV, value);
  1054. /*
  1055. * Set up LVT0, LVT1:
  1056. *
  1057. * set up through-local-APIC on the BP's LINT0. This is not
  1058. * strictly necessary in pure symmetric-IO mode, but sometimes
  1059. * we delegate interrupts to the 8259A.
  1060. */
  1061. /*
  1062. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  1063. */
  1064. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  1065. if (!smp_processor_id() && (pic_mode || !value)) {
  1066. value = APIC_DM_EXTINT;
  1067. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  1068. smp_processor_id());
  1069. } else {
  1070. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  1071. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  1072. smp_processor_id());
  1073. }
  1074. apic_write(APIC_LVT0, value);
  1075. /*
  1076. * only the BP should see the LINT1 NMI signal, obviously.
  1077. */
  1078. if (!smp_processor_id())
  1079. value = APIC_DM_NMI;
  1080. else
  1081. value = APIC_DM_NMI | APIC_LVT_MASKED;
  1082. if (!lapic_is_integrated()) /* 82489DX */
  1083. value |= APIC_LVT_LEVEL_TRIGGER;
  1084. apic_write(APIC_LVT1, value);
  1085. preempt_enable();
  1086. }
  1087. void __cpuinit end_local_APIC_setup(void)
  1088. {
  1089. lapic_setup_esr();
  1090. #ifdef CONFIG_X86_32
  1091. {
  1092. unsigned int value;
  1093. /* Disable the local apic timer */
  1094. value = apic_read(APIC_LVTT);
  1095. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  1096. apic_write(APIC_LVTT, value);
  1097. }
  1098. #endif
  1099. setup_apic_nmi_watchdog(NULL);
  1100. apic_pm_activate();
  1101. }
  1102. #ifdef HAVE_X2APIC
  1103. void check_x2apic(void)
  1104. {
  1105. int msr, msr2;
  1106. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1107. if (msr & X2APIC_ENABLE) {
  1108. pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
  1109. x2apic_preenabled = x2apic = 1;
  1110. apic_ops = &x2apic_ops;
  1111. }
  1112. }
  1113. void enable_x2apic(void)
  1114. {
  1115. int msr, msr2;
  1116. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1117. if (!(msr & X2APIC_ENABLE)) {
  1118. pr_info("Enabling x2apic\n");
  1119. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  1120. }
  1121. }
  1122. void __init enable_IR_x2apic(void)
  1123. {
  1124. #ifdef CONFIG_INTR_REMAP
  1125. int ret;
  1126. unsigned long flags;
  1127. if (!cpu_has_x2apic)
  1128. return;
  1129. if (!x2apic_preenabled && disable_x2apic) {
  1130. pr_info("Skipped enabling x2apic and Interrupt-remapping "
  1131. "because of nox2apic\n");
  1132. return;
  1133. }
  1134. if (x2apic_preenabled && disable_x2apic)
  1135. panic("Bios already enabled x2apic, can't enforce nox2apic");
  1136. if (!x2apic_preenabled && skip_ioapic_setup) {
  1137. pr_info("Skipped enabling x2apic and Interrupt-remapping "
  1138. "because of skipping io-apic setup\n");
  1139. return;
  1140. }
  1141. ret = dmar_table_init();
  1142. if (ret) {
  1143. pr_info("dmar_table_init() failed with %d:\n", ret);
  1144. if (x2apic_preenabled)
  1145. panic("x2apic enabled by bios. But IR enabling failed");
  1146. else
  1147. pr_info("Not enabling x2apic,Intr-remapping\n");
  1148. return;
  1149. }
  1150. local_irq_save(flags);
  1151. mask_8259A();
  1152. ret = save_mask_IO_APIC_setup();
  1153. if (ret) {
  1154. pr_info("Saving IO-APIC state failed: %d\n", ret);
  1155. goto end;
  1156. }
  1157. ret = enable_intr_remapping(1);
  1158. if (ret && x2apic_preenabled) {
  1159. local_irq_restore(flags);
  1160. panic("x2apic enabled by bios. But IR enabling failed");
  1161. }
  1162. if (ret)
  1163. goto end_restore;
  1164. if (!x2apic) {
  1165. x2apic = 1;
  1166. apic_ops = &x2apic_ops;
  1167. enable_x2apic();
  1168. }
  1169. end_restore:
  1170. if (ret)
  1171. /*
  1172. * IR enabling failed
  1173. */
  1174. restore_IO_APIC_setup();
  1175. else
  1176. reinit_intr_remapped_IO_APIC(x2apic_preenabled);
  1177. end:
  1178. unmask_8259A();
  1179. local_irq_restore(flags);
  1180. if (!ret) {
  1181. if (!x2apic_preenabled)
  1182. pr_info("Enabled x2apic and interrupt-remapping\n");
  1183. else
  1184. pr_info("Enabled Interrupt-remapping\n");
  1185. } else
  1186. pr_err("Failed to enable Interrupt-remapping and x2apic\n");
  1187. #else
  1188. if (!cpu_has_x2apic)
  1189. return;
  1190. if (x2apic_preenabled)
  1191. panic("x2apic enabled prior OS handover,"
  1192. " enable CONFIG_INTR_REMAP");
  1193. pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
  1194. " and x2apic\n");
  1195. #endif
  1196. return;
  1197. }
  1198. #endif /* HAVE_X2APIC */
  1199. #ifdef CONFIG_X86_64
  1200. /*
  1201. * Detect and enable local APICs on non-SMP boards.
  1202. * Original code written by Keir Fraser.
  1203. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  1204. * not correctly set up (usually the APIC timer won't work etc.)
  1205. */
  1206. static int __init detect_init_APIC(void)
  1207. {
  1208. if (!cpu_has_apic) {
  1209. pr_info("No local APIC present\n");
  1210. return -1;
  1211. }
  1212. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1213. boot_cpu_physical_apicid = 0;
  1214. return 0;
  1215. }
  1216. #else
  1217. /*
  1218. * Detect and initialize APIC
  1219. */
  1220. static int __init detect_init_APIC(void)
  1221. {
  1222. u32 h, l, features;
  1223. /* Disabled by kernel option? */
  1224. if (disable_apic)
  1225. return -1;
  1226. switch (boot_cpu_data.x86_vendor) {
  1227. case X86_VENDOR_AMD:
  1228. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  1229. (boot_cpu_data.x86 >= 15))
  1230. break;
  1231. goto no_apic;
  1232. case X86_VENDOR_INTEL:
  1233. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  1234. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  1235. break;
  1236. goto no_apic;
  1237. default:
  1238. goto no_apic;
  1239. }
  1240. if (!cpu_has_apic) {
  1241. /*
  1242. * Over-ride BIOS and try to enable the local APIC only if
  1243. * "lapic" specified.
  1244. */
  1245. if (!force_enable_local_apic) {
  1246. pr_info("Local APIC disabled by BIOS -- "
  1247. "you can enable it with \"lapic\"\n");
  1248. return -1;
  1249. }
  1250. /*
  1251. * Some BIOSes disable the local APIC in the APIC_BASE
  1252. * MSR. This can only be done in software for Intel P6 or later
  1253. * and AMD K7 (Model > 1) or later.
  1254. */
  1255. rdmsr(MSR_IA32_APICBASE, l, h);
  1256. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  1257. pr_info("Local APIC disabled by BIOS -- reenabling.\n");
  1258. l &= ~MSR_IA32_APICBASE_BASE;
  1259. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  1260. wrmsr(MSR_IA32_APICBASE, l, h);
  1261. enabled_via_apicbase = 1;
  1262. }
  1263. }
  1264. /*
  1265. * The APIC feature bit should now be enabled
  1266. * in `cpuid'
  1267. */
  1268. features = cpuid_edx(1);
  1269. if (!(features & (1 << X86_FEATURE_APIC))) {
  1270. pr_warning("Could not enable APIC!\n");
  1271. return -1;
  1272. }
  1273. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1274. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1275. /* The BIOS may have set up the APIC at some other address */
  1276. rdmsr(MSR_IA32_APICBASE, l, h);
  1277. if (l & MSR_IA32_APICBASE_ENABLE)
  1278. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1279. pr_info("Found and enabled local APIC!\n");
  1280. apic_pm_activate();
  1281. return 0;
  1282. no_apic:
  1283. pr_info("No local APIC present or hardware disabled\n");
  1284. return -1;
  1285. }
  1286. #endif
  1287. #ifdef CONFIG_X86_64
  1288. void __init early_init_lapic_mapping(void)
  1289. {
  1290. unsigned long phys_addr;
  1291. /*
  1292. * If no local APIC can be found then go out
  1293. * : it means there is no mpatable and MADT
  1294. */
  1295. if (!smp_found_config)
  1296. return;
  1297. phys_addr = mp_lapic_addr;
  1298. set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
  1299. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1300. APIC_BASE, phys_addr);
  1301. /*
  1302. * Fetch the APIC ID of the BSP in case we have a
  1303. * default configuration (or the MP table is broken).
  1304. */
  1305. boot_cpu_physical_apicid = read_apic_id();
  1306. }
  1307. #endif
  1308. /**
  1309. * init_apic_mappings - initialize APIC mappings
  1310. */
  1311. void __init init_apic_mappings(void)
  1312. {
  1313. #ifdef HAVE_X2APIC
  1314. if (x2apic) {
  1315. boot_cpu_physical_apicid = read_apic_id();
  1316. return;
  1317. }
  1318. #endif
  1319. /*
  1320. * If no local APIC can be found then set up a fake all
  1321. * zeroes page to simulate the local APIC and another
  1322. * one for the IO-APIC.
  1323. */
  1324. if (!smp_found_config && detect_init_APIC()) {
  1325. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  1326. apic_phys = __pa(apic_phys);
  1327. } else
  1328. apic_phys = mp_lapic_addr;
  1329. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  1330. apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
  1331. APIC_BASE, apic_phys);
  1332. /*
  1333. * Fetch the APIC ID of the BSP in case we have a
  1334. * default configuration (or the MP table is broken).
  1335. */
  1336. if (boot_cpu_physical_apicid == -1U)
  1337. boot_cpu_physical_apicid = read_apic_id();
  1338. }
  1339. /*
  1340. * This initializes the IO-APIC and APIC hardware if this is
  1341. * a UP kernel.
  1342. */
  1343. int apic_version[MAX_APICS];
  1344. int __init APIC_init_uniprocessor(void)
  1345. {
  1346. #ifdef CONFIG_X86_64
  1347. if (disable_apic) {
  1348. pr_info("Apic disabled\n");
  1349. return -1;
  1350. }
  1351. if (!cpu_has_apic) {
  1352. disable_apic = 1;
  1353. pr_info("Apic disabled by BIOS\n");
  1354. return -1;
  1355. }
  1356. #else
  1357. if (!smp_found_config && !cpu_has_apic)
  1358. return -1;
  1359. /*
  1360. * Complain if the BIOS pretends there is one.
  1361. */
  1362. if (!cpu_has_apic &&
  1363. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1364. pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
  1365. boot_cpu_physical_apicid);
  1366. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1367. return -1;
  1368. }
  1369. #endif
  1370. #ifdef HAVE_X2APIC
  1371. enable_IR_x2apic();
  1372. #endif
  1373. #ifdef CONFIG_X86_64
  1374. setup_apic_routing();
  1375. #endif
  1376. verify_local_APIC();
  1377. connect_bsp_APIC();
  1378. #ifdef CONFIG_X86_64
  1379. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  1380. #else
  1381. /*
  1382. * Hack: In case of kdump, after a crash, kernel might be booting
  1383. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1384. * might be zero if read from MP tables. Get it from LAPIC.
  1385. */
  1386. # ifdef CONFIG_CRASH_DUMP
  1387. boot_cpu_physical_apicid = read_apic_id();
  1388. # endif
  1389. #endif
  1390. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  1391. setup_local_APIC();
  1392. #ifdef CONFIG_X86_64
  1393. /*
  1394. * Now enable IO-APICs, actually call clear_IO_APIC
  1395. * We need clear_IO_APIC before enabling vector on BP
  1396. */
  1397. if (!skip_ioapic_setup && nr_ioapics)
  1398. enable_IO_APIC();
  1399. #endif
  1400. #ifdef CONFIG_X86_IO_APIC
  1401. if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
  1402. #endif
  1403. localise_nmi_watchdog();
  1404. end_local_APIC_setup();
  1405. #ifdef CONFIG_X86_IO_APIC
  1406. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1407. setup_IO_APIC();
  1408. # ifdef CONFIG_X86_64
  1409. else
  1410. nr_ioapics = 0;
  1411. # endif
  1412. #endif
  1413. #ifdef CONFIG_X86_64
  1414. setup_boot_APIC_clock();
  1415. check_nmi_watchdog();
  1416. #else
  1417. setup_boot_clock();
  1418. #endif
  1419. return 0;
  1420. }
  1421. /*
  1422. * Local APIC interrupts
  1423. */
  1424. /*
  1425. * This interrupt should _never_ happen with our APIC/SMP architecture
  1426. */
  1427. void smp_spurious_interrupt(struct pt_regs *regs)
  1428. {
  1429. u32 v;
  1430. exit_idle();
  1431. irq_enter();
  1432. /*
  1433. * Check if this really is a spurious interrupt and ACK it
  1434. * if it is a vectored one. Just in case...
  1435. * Spurious interrupts should not be ACKed.
  1436. */
  1437. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1438. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1439. ack_APIC_irq();
  1440. inc_irq_stat(irq_spurious_count);
  1441. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1442. pr_info("spurious APIC interrupt on CPU#%d, "
  1443. "should never happen.\n", smp_processor_id());
  1444. irq_exit();
  1445. }
  1446. /*
  1447. * This interrupt should never happen with our APIC/SMP architecture
  1448. */
  1449. void smp_error_interrupt(struct pt_regs *regs)
  1450. {
  1451. u32 v, v1;
  1452. exit_idle();
  1453. irq_enter();
  1454. /* First tickle the hardware, only then report what went on. -- REW */
  1455. v = apic_read(APIC_ESR);
  1456. apic_write(APIC_ESR, 0);
  1457. v1 = apic_read(APIC_ESR);
  1458. ack_APIC_irq();
  1459. atomic_inc(&irq_err_count);
  1460. /*
  1461. * Here is what the APIC error bits mean:
  1462. * 0: Send CS error
  1463. * 1: Receive CS error
  1464. * 2: Send accept error
  1465. * 3: Receive accept error
  1466. * 4: Reserved
  1467. * 5: Send illegal vector
  1468. * 6: Received illegal vector
  1469. * 7: Illegal register address
  1470. */
  1471. pr_debug("APIC error on CPU%d: %02x(%02x)\n",
  1472. smp_processor_id(), v , v1);
  1473. irq_exit();
  1474. }
  1475. /**
  1476. * connect_bsp_APIC - attach the APIC to the interrupt system
  1477. */
  1478. void __init connect_bsp_APIC(void)
  1479. {
  1480. #ifdef CONFIG_X86_32
  1481. if (pic_mode) {
  1482. /*
  1483. * Do not trust the local APIC being empty at bootup.
  1484. */
  1485. clear_local_APIC();
  1486. /*
  1487. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1488. * local APIC to INT and NMI lines.
  1489. */
  1490. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1491. "enabling APIC mode.\n");
  1492. outb(0x70, 0x22);
  1493. outb(0x01, 0x23);
  1494. }
  1495. #endif
  1496. enable_apic_mode();
  1497. }
  1498. /**
  1499. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1500. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1501. *
  1502. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1503. * APIC is disabled.
  1504. */
  1505. void disconnect_bsp_APIC(int virt_wire_setup)
  1506. {
  1507. unsigned int value;
  1508. #ifdef CONFIG_X86_32
  1509. if (pic_mode) {
  1510. /*
  1511. * Put the board back into PIC mode (has an effect only on
  1512. * certain older boards). Note that APIC interrupts, including
  1513. * IPIs, won't work beyond this point! The only exception are
  1514. * INIT IPIs.
  1515. */
  1516. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1517. "entering PIC mode.\n");
  1518. outb(0x70, 0x22);
  1519. outb(0x00, 0x23);
  1520. return;
  1521. }
  1522. #endif
  1523. /* Go back to Virtual Wire compatibility mode */
  1524. /* For the spurious interrupt use vector F, and enable it */
  1525. value = apic_read(APIC_SPIV);
  1526. value &= ~APIC_VECTOR_MASK;
  1527. value |= APIC_SPIV_APIC_ENABLED;
  1528. value |= 0xf;
  1529. apic_write(APIC_SPIV, value);
  1530. if (!virt_wire_setup) {
  1531. /*
  1532. * For LVT0 make it edge triggered, active high,
  1533. * external and enabled
  1534. */
  1535. value = apic_read(APIC_LVT0);
  1536. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1537. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1538. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1539. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1540. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1541. apic_write(APIC_LVT0, value);
  1542. } else {
  1543. /* Disable LVT0 */
  1544. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  1545. }
  1546. /*
  1547. * For LVT1 make it edge triggered, active high,
  1548. * nmi and enabled
  1549. */
  1550. value = apic_read(APIC_LVT1);
  1551. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1552. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1553. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1554. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1555. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1556. apic_write(APIC_LVT1, value);
  1557. }
  1558. void __cpuinit generic_processor_info(int apicid, int version)
  1559. {
  1560. int cpu;
  1561. /*
  1562. * Validate version
  1563. */
  1564. if (version == 0x0) {
  1565. pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
  1566. "fixing up to 0x10. (tell your hw vendor)\n",
  1567. version);
  1568. version = 0x10;
  1569. }
  1570. apic_version[apicid] = version;
  1571. if (num_processors >= nr_cpu_ids) {
  1572. int max = nr_cpu_ids;
  1573. int thiscpu = max + disabled_cpus;
  1574. pr_warning(
  1575. "ACPI: NR_CPUS/possible_cpus limit of %i reached."
  1576. " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
  1577. disabled_cpus++;
  1578. return;
  1579. }
  1580. num_processors++;
  1581. cpu = cpumask_next_zero(-1, cpu_present_mask);
  1582. if (version != apic_version[boot_cpu_physical_apicid])
  1583. WARN_ONCE(1,
  1584. "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
  1585. apic_version[boot_cpu_physical_apicid], cpu, version);
  1586. physid_set(apicid, phys_cpu_present_map);
  1587. if (apicid == boot_cpu_physical_apicid) {
  1588. /*
  1589. * x86_bios_cpu_apicid is required to have processors listed
  1590. * in same order as logical cpu numbers. Hence the first
  1591. * entry is BSP, and so on.
  1592. */
  1593. cpu = 0;
  1594. }
  1595. if (apicid > max_physical_apicid)
  1596. max_physical_apicid = apicid;
  1597. #ifdef CONFIG_X86_32
  1598. /*
  1599. * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
  1600. * but we need to work other dependencies like SMP_SUSPEND etc
  1601. * before this can be done without some confusion.
  1602. * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
  1603. * - Ashok Raj <ashok.raj@intel.com>
  1604. */
  1605. if (max_physical_apicid >= 8) {
  1606. switch (boot_cpu_data.x86_vendor) {
  1607. case X86_VENDOR_INTEL:
  1608. if (!APIC_XAPIC(version)) {
  1609. def_to_bigsmp = 0;
  1610. break;
  1611. }
  1612. /* If P4 and above fall through */
  1613. case X86_VENDOR_AMD:
  1614. def_to_bigsmp = 1;
  1615. }
  1616. }
  1617. #endif
  1618. #if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
  1619. /* are we being called early in kernel startup? */
  1620. if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
  1621. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  1622. u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1623. cpu_to_apicid[cpu] = apicid;
  1624. bios_cpu_apicid[cpu] = apicid;
  1625. } else {
  1626. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  1627. per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  1628. }
  1629. #endif
  1630. set_cpu_possible(cpu, true);
  1631. set_cpu_present(cpu, true);
  1632. }
  1633. #ifdef CONFIG_X86_64
  1634. int hard_smp_processor_id(void)
  1635. {
  1636. return read_apic_id();
  1637. }
  1638. #endif
  1639. /*
  1640. * Power management
  1641. */
  1642. #ifdef CONFIG_PM
  1643. static struct {
  1644. /*
  1645. * 'active' is true if the local APIC was enabled by us and
  1646. * not the BIOS; this signifies that we are also responsible
  1647. * for disabling it before entering apm/acpi suspend
  1648. */
  1649. int active;
  1650. /* r/w apic fields */
  1651. unsigned int apic_id;
  1652. unsigned int apic_taskpri;
  1653. unsigned int apic_ldr;
  1654. unsigned int apic_dfr;
  1655. unsigned int apic_spiv;
  1656. unsigned int apic_lvtt;
  1657. unsigned int apic_lvtpc;
  1658. unsigned int apic_lvt0;
  1659. unsigned int apic_lvt1;
  1660. unsigned int apic_lvterr;
  1661. unsigned int apic_tmict;
  1662. unsigned int apic_tdcr;
  1663. unsigned int apic_thmr;
  1664. } apic_pm_state;
  1665. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1666. {
  1667. unsigned long flags;
  1668. int maxlvt;
  1669. if (!apic_pm_state.active)
  1670. return 0;
  1671. maxlvt = lapic_get_maxlvt();
  1672. apic_pm_state.apic_id = apic_read(APIC_ID);
  1673. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1674. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1675. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1676. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1677. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1678. if (maxlvt >= 4)
  1679. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1680. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1681. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1682. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1683. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1684. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1685. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1686. if (maxlvt >= 5)
  1687. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1688. #endif
  1689. local_irq_save(flags);
  1690. disable_local_APIC();
  1691. local_irq_restore(flags);
  1692. return 0;
  1693. }
  1694. static int lapic_resume(struct sys_device *dev)
  1695. {
  1696. unsigned int l, h;
  1697. unsigned long flags;
  1698. int maxlvt;
  1699. if (!apic_pm_state.active)
  1700. return 0;
  1701. maxlvt = lapic_get_maxlvt();
  1702. local_irq_save(flags);
  1703. #ifdef HAVE_X2APIC
  1704. if (x2apic)
  1705. enable_x2apic();
  1706. else
  1707. #endif
  1708. {
  1709. /*
  1710. * Make sure the APICBASE points to the right address
  1711. *
  1712. * FIXME! This will be wrong if we ever support suspend on
  1713. * SMP! We'll need to do this as part of the CPU restore!
  1714. */
  1715. rdmsr(MSR_IA32_APICBASE, l, h);
  1716. l &= ~MSR_IA32_APICBASE_BASE;
  1717. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1718. wrmsr(MSR_IA32_APICBASE, l, h);
  1719. }
  1720. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1721. apic_write(APIC_ID, apic_pm_state.apic_id);
  1722. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1723. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1724. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1725. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1726. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1727. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1728. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1729. if (maxlvt >= 5)
  1730. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1731. #endif
  1732. if (maxlvt >= 4)
  1733. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1734. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1735. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1736. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1737. apic_write(APIC_ESR, 0);
  1738. apic_read(APIC_ESR);
  1739. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1740. apic_write(APIC_ESR, 0);
  1741. apic_read(APIC_ESR);
  1742. local_irq_restore(flags);
  1743. return 0;
  1744. }
  1745. /*
  1746. * This device has no shutdown method - fully functioning local APICs
  1747. * are needed on every CPU up until machine_halt/restart/poweroff.
  1748. */
  1749. static struct sysdev_class lapic_sysclass = {
  1750. .name = "lapic",
  1751. .resume = lapic_resume,
  1752. .suspend = lapic_suspend,
  1753. };
  1754. static struct sys_device device_lapic = {
  1755. .id = 0,
  1756. .cls = &lapic_sysclass,
  1757. };
  1758. static void __cpuinit apic_pm_activate(void)
  1759. {
  1760. apic_pm_state.active = 1;
  1761. }
  1762. static int __init init_lapic_sysfs(void)
  1763. {
  1764. int error;
  1765. if (!cpu_has_apic)
  1766. return 0;
  1767. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1768. error = sysdev_class_register(&lapic_sysclass);
  1769. if (!error)
  1770. error = sysdev_register(&device_lapic);
  1771. return error;
  1772. }
  1773. device_initcall(init_lapic_sysfs);
  1774. #else /* CONFIG_PM */
  1775. static void apic_pm_activate(void) { }
  1776. #endif /* CONFIG_PM */
  1777. #ifdef CONFIG_X86_64
  1778. /*
  1779. * apic_is_clustered_box() -- Check if we can expect good TSC
  1780. *
  1781. * Thus far, the major user of this is IBM's Summit2 series:
  1782. *
  1783. * Clustered boxes may have unsynced TSC problems if they are
  1784. * multi-chassis. Use available data to take a good guess.
  1785. * If in doubt, go HPET.
  1786. */
  1787. __cpuinit int apic_is_clustered_box(void)
  1788. {
  1789. int i, clusters, zeros;
  1790. unsigned id;
  1791. u16 *bios_cpu_apicid;
  1792. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1793. /*
  1794. * there is not this kind of box with AMD CPU yet.
  1795. * Some AMD box with quadcore cpu and 8 sockets apicid
  1796. * will be [4, 0x23] or [8, 0x27] could be thought to
  1797. * vsmp box still need checking...
  1798. */
  1799. if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
  1800. return 0;
  1801. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1802. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1803. for (i = 0; i < nr_cpu_ids; i++) {
  1804. /* are we being called early in kernel startup? */
  1805. if (bios_cpu_apicid) {
  1806. id = bios_cpu_apicid[i];
  1807. } else if (i < nr_cpu_ids) {
  1808. if (cpu_present(i))
  1809. id = per_cpu(x86_bios_cpu_apicid, i);
  1810. else
  1811. continue;
  1812. } else
  1813. break;
  1814. if (id != BAD_APICID)
  1815. __set_bit(APIC_CLUSTERID(id), clustermap);
  1816. }
  1817. /* Problem: Partially populated chassis may not have CPUs in some of
  1818. * the APIC clusters they have been allocated. Only present CPUs have
  1819. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1820. * Since clusters are allocated sequentially, count zeros only if
  1821. * they are bounded by ones.
  1822. */
  1823. clusters = 0;
  1824. zeros = 0;
  1825. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1826. if (test_bit(i, clustermap)) {
  1827. clusters += 1 + zeros;
  1828. zeros = 0;
  1829. } else
  1830. ++zeros;
  1831. }
  1832. /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1833. * not guaranteed to be synced between boards
  1834. */
  1835. if (is_vsmp_box() && clusters > 1)
  1836. return 1;
  1837. /*
  1838. * If clusters > 2, then should be multi-chassis.
  1839. * May have to revisit this when multi-core + hyperthreaded CPUs come
  1840. * out, but AFAIK this will work even for them.
  1841. */
  1842. return (clusters > 2);
  1843. }
  1844. #endif
  1845. /*
  1846. * APIC command line parameters
  1847. */
  1848. static int __init setup_disableapic(char *arg)
  1849. {
  1850. disable_apic = 1;
  1851. setup_clear_cpu_cap(X86_FEATURE_APIC);
  1852. return 0;
  1853. }
  1854. early_param("disableapic", setup_disableapic);
  1855. /* same as disableapic, for compatibility */
  1856. static int __init setup_nolapic(char *arg)
  1857. {
  1858. return setup_disableapic(arg);
  1859. }
  1860. early_param("nolapic", setup_nolapic);
  1861. static int __init parse_lapic_timer_c2_ok(char *arg)
  1862. {
  1863. local_apic_timer_c2_ok = 1;
  1864. return 0;
  1865. }
  1866. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1867. static int __init parse_disable_apic_timer(char *arg)
  1868. {
  1869. disable_apic_timer = 1;
  1870. return 0;
  1871. }
  1872. early_param("noapictimer", parse_disable_apic_timer);
  1873. static int __init parse_nolapic_timer(char *arg)
  1874. {
  1875. disable_apic_timer = 1;
  1876. return 0;
  1877. }
  1878. early_param("nolapic_timer", parse_nolapic_timer);
  1879. static int __init apic_set_verbosity(char *arg)
  1880. {
  1881. if (!arg) {
  1882. #ifdef CONFIG_X86_64
  1883. skip_ioapic_setup = 0;
  1884. return 0;
  1885. #endif
  1886. return -EINVAL;
  1887. }
  1888. if (strcmp("debug", arg) == 0)
  1889. apic_verbosity = APIC_DEBUG;
  1890. else if (strcmp("verbose", arg) == 0)
  1891. apic_verbosity = APIC_VERBOSE;
  1892. else {
  1893. pr_warning("APIC Verbosity level %s not recognised"
  1894. " use apic=verbose or apic=debug\n", arg);
  1895. return -EINVAL;
  1896. }
  1897. return 0;
  1898. }
  1899. early_param("apic", apic_set_verbosity);
  1900. static int __init lapic_insert_resource(void)
  1901. {
  1902. if (!apic_phys)
  1903. return -1;
  1904. /* Put local APIC into the resource map. */
  1905. lapic_resource.start = apic_phys;
  1906. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  1907. insert_resource(&iomem_resource, &lapic_resource);
  1908. return 0;
  1909. }
  1910. /*
  1911. * need call insert after e820_reserve_resources()
  1912. * that is using request_resource
  1913. */
  1914. late_initcall(lapic_insert_resource);