apic.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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(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. if (disable_apic) {
  967. disable_ioapic_setup();
  968. return;
  969. }
  970. #ifdef CONFIG_X86_32
  971. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  972. if (lapic_is_integrated() && esr_disable) {
  973. apic_write(APIC_ESR, 0);
  974. apic_write(APIC_ESR, 0);
  975. apic_write(APIC_ESR, 0);
  976. apic_write(APIC_ESR, 0);
  977. }
  978. #endif
  979. preempt_disable();
  980. /*
  981. * Double-check whether this APIC is really registered.
  982. * This is meaningless in clustered apic mode, so we skip it.
  983. */
  984. if (!apic_id_registered())
  985. BUG();
  986. /*
  987. * Intel recommends to set DFR, LDR and TPR before enabling
  988. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  989. * document number 292116). So here it goes...
  990. */
  991. init_apic_ldr();
  992. /*
  993. * Set Task Priority to 'accept all'. We never change this
  994. * later on.
  995. */
  996. value = apic_read(APIC_TASKPRI);
  997. value &= ~APIC_TPRI_MASK;
  998. apic_write(APIC_TASKPRI, value);
  999. /*
  1000. * After a crash, we no longer service the interrupts and a pending
  1001. * interrupt from previous kernel might still have ISR bit set.
  1002. *
  1003. * Most probably by now CPU has serviced that pending interrupt and
  1004. * it might not have done the ack_APIC_irq() because it thought,
  1005. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  1006. * does not clear the ISR bit and cpu thinks it has already serivced
  1007. * the interrupt. Hence a vector might get locked. It was noticed
  1008. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  1009. */
  1010. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  1011. value = apic_read(APIC_ISR + i*0x10);
  1012. for (j = 31; j >= 0; j--) {
  1013. if (value & (1<<j))
  1014. ack_APIC_irq();
  1015. }
  1016. }
  1017. /*
  1018. * Now that we are all set up, enable the APIC
  1019. */
  1020. value = apic_read(APIC_SPIV);
  1021. value &= ~APIC_VECTOR_MASK;
  1022. /*
  1023. * Enable APIC
  1024. */
  1025. value |= APIC_SPIV_APIC_ENABLED;
  1026. #ifdef CONFIG_X86_32
  1027. /*
  1028. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  1029. * certain networking cards. If high frequency interrupts are
  1030. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  1031. * entry is masked/unmasked at a high rate as well then sooner or
  1032. * later IOAPIC line gets 'stuck', no more interrupts are received
  1033. * from the device. If focus CPU is disabled then the hang goes
  1034. * away, oh well :-(
  1035. *
  1036. * [ This bug can be reproduced easily with a level-triggered
  1037. * PCI Ne2000 networking cards and PII/PIII processors, dual
  1038. * BX chipset. ]
  1039. */
  1040. /*
  1041. * Actually disabling the focus CPU check just makes the hang less
  1042. * frequent as it makes the interrupt distributon model be more
  1043. * like LRU than MRU (the short-term load is more even across CPUs).
  1044. * See also the comment in end_level_ioapic_irq(). --macro
  1045. */
  1046. /*
  1047. * - enable focus processor (bit==0)
  1048. * - 64bit mode always use processor focus
  1049. * so no need to set it
  1050. */
  1051. value &= ~APIC_SPIV_FOCUS_DISABLED;
  1052. #endif
  1053. /*
  1054. * Set spurious IRQ vector
  1055. */
  1056. value |= SPURIOUS_APIC_VECTOR;
  1057. apic_write(APIC_SPIV, value);
  1058. /*
  1059. * Set up LVT0, LVT1:
  1060. *
  1061. * set up through-local-APIC on the BP's LINT0. This is not
  1062. * strictly necessary in pure symmetric-IO mode, but sometimes
  1063. * we delegate interrupts to the 8259A.
  1064. */
  1065. /*
  1066. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  1067. */
  1068. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  1069. if (!smp_processor_id() && (pic_mode || !value)) {
  1070. value = APIC_DM_EXTINT;
  1071. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  1072. smp_processor_id());
  1073. } else {
  1074. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  1075. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  1076. smp_processor_id());
  1077. }
  1078. apic_write(APIC_LVT0, value);
  1079. /*
  1080. * only the BP should see the LINT1 NMI signal, obviously.
  1081. */
  1082. if (!smp_processor_id())
  1083. value = APIC_DM_NMI;
  1084. else
  1085. value = APIC_DM_NMI | APIC_LVT_MASKED;
  1086. if (!lapic_is_integrated()) /* 82489DX */
  1087. value |= APIC_LVT_LEVEL_TRIGGER;
  1088. apic_write(APIC_LVT1, value);
  1089. preempt_enable();
  1090. }
  1091. void __cpuinit end_local_APIC_setup(void)
  1092. {
  1093. lapic_setup_esr();
  1094. #ifdef CONFIG_X86_32
  1095. {
  1096. unsigned int value;
  1097. /* Disable the local apic timer */
  1098. value = apic_read(APIC_LVTT);
  1099. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  1100. apic_write(APIC_LVTT, value);
  1101. }
  1102. #endif
  1103. setup_apic_nmi_watchdog(NULL);
  1104. apic_pm_activate();
  1105. }
  1106. #ifdef HAVE_X2APIC
  1107. void check_x2apic(void)
  1108. {
  1109. int msr, msr2;
  1110. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1111. if (msr & X2APIC_ENABLE) {
  1112. pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
  1113. x2apic_preenabled = x2apic = 1;
  1114. apic_ops = &x2apic_ops;
  1115. }
  1116. }
  1117. void enable_x2apic(void)
  1118. {
  1119. int msr, msr2;
  1120. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1121. if (!(msr & X2APIC_ENABLE)) {
  1122. pr_info("Enabling x2apic\n");
  1123. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  1124. }
  1125. }
  1126. void __init enable_IR_x2apic(void)
  1127. {
  1128. #ifdef CONFIG_INTR_REMAP
  1129. int ret;
  1130. unsigned long flags;
  1131. if (!cpu_has_x2apic)
  1132. return;
  1133. if (!x2apic_preenabled && disable_x2apic) {
  1134. pr_info("Skipped enabling x2apic and Interrupt-remapping "
  1135. "because of nox2apic\n");
  1136. return;
  1137. }
  1138. if (x2apic_preenabled && disable_x2apic)
  1139. panic("Bios already enabled x2apic, can't enforce nox2apic");
  1140. if (!x2apic_preenabled && skip_ioapic_setup) {
  1141. pr_info("Skipped enabling x2apic and Interrupt-remapping "
  1142. "because of skipping io-apic setup\n");
  1143. return;
  1144. }
  1145. ret = dmar_table_init();
  1146. if (ret) {
  1147. pr_info("dmar_table_init() failed with %d:\n", ret);
  1148. if (x2apic_preenabled)
  1149. panic("x2apic enabled by bios. But IR enabling failed");
  1150. else
  1151. pr_info("Not enabling x2apic,Intr-remapping\n");
  1152. return;
  1153. }
  1154. local_irq_save(flags);
  1155. mask_8259A();
  1156. ret = save_mask_IO_APIC_setup();
  1157. if (ret) {
  1158. pr_info("Saving IO-APIC state failed: %d\n", ret);
  1159. goto end;
  1160. }
  1161. ret = enable_intr_remapping(1);
  1162. if (ret && x2apic_preenabled) {
  1163. local_irq_restore(flags);
  1164. panic("x2apic enabled by bios. But IR enabling failed");
  1165. }
  1166. if (ret)
  1167. goto end_restore;
  1168. if (!x2apic) {
  1169. x2apic = 1;
  1170. apic_ops = &x2apic_ops;
  1171. enable_x2apic();
  1172. }
  1173. end_restore:
  1174. if (ret)
  1175. /*
  1176. * IR enabling failed
  1177. */
  1178. restore_IO_APIC_setup();
  1179. else
  1180. reinit_intr_remapped_IO_APIC(x2apic_preenabled);
  1181. end:
  1182. unmask_8259A();
  1183. local_irq_restore(flags);
  1184. if (!ret) {
  1185. if (!x2apic_preenabled)
  1186. pr_info("Enabled x2apic and interrupt-remapping\n");
  1187. else
  1188. pr_info("Enabled Interrupt-remapping\n");
  1189. } else
  1190. pr_err("Failed to enable Interrupt-remapping and x2apic\n");
  1191. #else
  1192. if (!cpu_has_x2apic)
  1193. return;
  1194. if (x2apic_preenabled)
  1195. panic("x2apic enabled prior OS handover,"
  1196. " enable CONFIG_INTR_REMAP");
  1197. pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
  1198. " and x2apic\n");
  1199. #endif
  1200. return;
  1201. }
  1202. #endif /* HAVE_X2APIC */
  1203. #ifdef CONFIG_X86_64
  1204. /*
  1205. * Detect and enable local APICs on non-SMP boards.
  1206. * Original code written by Keir Fraser.
  1207. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  1208. * not correctly set up (usually the APIC timer won't work etc.)
  1209. */
  1210. static int __init detect_init_APIC(void)
  1211. {
  1212. if (!cpu_has_apic) {
  1213. pr_info("No local APIC present\n");
  1214. return -1;
  1215. }
  1216. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1217. boot_cpu_physical_apicid = 0;
  1218. return 0;
  1219. }
  1220. #else
  1221. /*
  1222. * Detect and initialize APIC
  1223. */
  1224. static int __init detect_init_APIC(void)
  1225. {
  1226. u32 h, l, features;
  1227. /* Disabled by kernel option? */
  1228. if (disable_apic)
  1229. return -1;
  1230. switch (boot_cpu_data.x86_vendor) {
  1231. case X86_VENDOR_AMD:
  1232. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  1233. (boot_cpu_data.x86 == 15))
  1234. break;
  1235. goto no_apic;
  1236. case X86_VENDOR_INTEL:
  1237. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  1238. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  1239. break;
  1240. goto no_apic;
  1241. default:
  1242. goto no_apic;
  1243. }
  1244. if (!cpu_has_apic) {
  1245. /*
  1246. * Over-ride BIOS and try to enable the local APIC only if
  1247. * "lapic" specified.
  1248. */
  1249. if (!force_enable_local_apic) {
  1250. pr_info("Local APIC disabled by BIOS -- "
  1251. "you can enable it with \"lapic\"\n");
  1252. return -1;
  1253. }
  1254. /*
  1255. * Some BIOSes disable the local APIC in the APIC_BASE
  1256. * MSR. This can only be done in software for Intel P6 or later
  1257. * and AMD K7 (Model > 1) or later.
  1258. */
  1259. rdmsr(MSR_IA32_APICBASE, l, h);
  1260. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  1261. pr_info("Local APIC disabled by BIOS -- reenabling.\n");
  1262. l &= ~MSR_IA32_APICBASE_BASE;
  1263. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  1264. wrmsr(MSR_IA32_APICBASE, l, h);
  1265. enabled_via_apicbase = 1;
  1266. }
  1267. }
  1268. /*
  1269. * The APIC feature bit should now be enabled
  1270. * in `cpuid'
  1271. */
  1272. features = cpuid_edx(1);
  1273. if (!(features & (1 << X86_FEATURE_APIC))) {
  1274. pr_warning("Could not enable APIC!\n");
  1275. return -1;
  1276. }
  1277. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1278. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1279. /* The BIOS may have set up the APIC at some other address */
  1280. rdmsr(MSR_IA32_APICBASE, l, h);
  1281. if (l & MSR_IA32_APICBASE_ENABLE)
  1282. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1283. pr_info("Found and enabled local APIC!\n");
  1284. apic_pm_activate();
  1285. return 0;
  1286. no_apic:
  1287. pr_info("No local APIC present or hardware disabled\n");
  1288. return -1;
  1289. }
  1290. #endif
  1291. #ifdef CONFIG_X86_64
  1292. void __init early_init_lapic_mapping(void)
  1293. {
  1294. unsigned long phys_addr;
  1295. /*
  1296. * If no local APIC can be found then go out
  1297. * : it means there is no mpatable and MADT
  1298. */
  1299. if (!smp_found_config)
  1300. return;
  1301. phys_addr = mp_lapic_addr;
  1302. set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
  1303. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1304. APIC_BASE, phys_addr);
  1305. /*
  1306. * Fetch the APIC ID of the BSP in case we have a
  1307. * default configuration (or the MP table is broken).
  1308. */
  1309. boot_cpu_physical_apicid = read_apic_id();
  1310. }
  1311. #endif
  1312. /**
  1313. * init_apic_mappings - initialize APIC mappings
  1314. */
  1315. void __init init_apic_mappings(void)
  1316. {
  1317. #ifdef HAVE_X2APIC
  1318. if (x2apic) {
  1319. boot_cpu_physical_apicid = read_apic_id();
  1320. return;
  1321. }
  1322. #endif
  1323. /*
  1324. * If no local APIC can be found then set up a fake all
  1325. * zeroes page to simulate the local APIC and another
  1326. * one for the IO-APIC.
  1327. */
  1328. if (!smp_found_config && detect_init_APIC()) {
  1329. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  1330. apic_phys = __pa(apic_phys);
  1331. } else
  1332. apic_phys = mp_lapic_addr;
  1333. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  1334. apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
  1335. APIC_BASE, apic_phys);
  1336. /*
  1337. * Fetch the APIC ID of the BSP in case we have a
  1338. * default configuration (or the MP table is broken).
  1339. */
  1340. if (boot_cpu_physical_apicid == -1U)
  1341. boot_cpu_physical_apicid = read_apic_id();
  1342. }
  1343. /*
  1344. * This initializes the IO-APIC and APIC hardware if this is
  1345. * a UP kernel.
  1346. */
  1347. int apic_version[MAX_APICS];
  1348. int __init APIC_init_uniprocessor(void)
  1349. {
  1350. if (disable_apic) {
  1351. pr_info("Apic disabled\n");
  1352. return -1;
  1353. }
  1354. #ifdef CONFIG_X86_64
  1355. if (!cpu_has_apic) {
  1356. disable_apic = 1;
  1357. pr_info("Apic disabled by BIOS\n");
  1358. return -1;
  1359. }
  1360. #else
  1361. if (!smp_found_config && !cpu_has_apic)
  1362. return -1;
  1363. /*
  1364. * Complain if the BIOS pretends there is one.
  1365. */
  1366. if (!cpu_has_apic &&
  1367. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1368. pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
  1369. boot_cpu_physical_apicid);
  1370. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1371. return -1;
  1372. }
  1373. #endif
  1374. #ifdef HAVE_X2APIC
  1375. enable_IR_x2apic();
  1376. #endif
  1377. #ifdef CONFIG_X86_64
  1378. setup_apic_routing();
  1379. #endif
  1380. verify_local_APIC();
  1381. connect_bsp_APIC();
  1382. #ifdef CONFIG_X86_64
  1383. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  1384. #else
  1385. /*
  1386. * Hack: In case of kdump, after a crash, kernel might be booting
  1387. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1388. * might be zero if read from MP tables. Get it from LAPIC.
  1389. */
  1390. # ifdef CONFIG_CRASH_DUMP
  1391. boot_cpu_physical_apicid = read_apic_id();
  1392. # endif
  1393. #endif
  1394. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  1395. setup_local_APIC();
  1396. #ifdef CONFIG_X86_64
  1397. /*
  1398. * Now enable IO-APICs, actually call clear_IO_APIC
  1399. * We need clear_IO_APIC before enabling vector on BP
  1400. */
  1401. if (!skip_ioapic_setup && nr_ioapics)
  1402. enable_IO_APIC();
  1403. #endif
  1404. #ifdef CONFIG_X86_IO_APIC
  1405. if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
  1406. #endif
  1407. localise_nmi_watchdog();
  1408. end_local_APIC_setup();
  1409. #ifdef CONFIG_X86_IO_APIC
  1410. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1411. setup_IO_APIC();
  1412. # ifdef CONFIG_X86_64
  1413. else
  1414. nr_ioapics = 0;
  1415. # endif
  1416. #endif
  1417. #ifdef CONFIG_X86_64
  1418. setup_boot_APIC_clock();
  1419. check_nmi_watchdog();
  1420. #else
  1421. setup_boot_clock();
  1422. #endif
  1423. return 0;
  1424. }
  1425. /*
  1426. * Local APIC interrupts
  1427. */
  1428. /*
  1429. * This interrupt should _never_ happen with our APIC/SMP architecture
  1430. */
  1431. void smp_spurious_interrupt(struct pt_regs *regs)
  1432. {
  1433. u32 v;
  1434. exit_idle();
  1435. irq_enter();
  1436. /*
  1437. * Check if this really is a spurious interrupt and ACK it
  1438. * if it is a vectored one. Just in case...
  1439. * Spurious interrupts should not be ACKed.
  1440. */
  1441. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1442. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1443. ack_APIC_irq();
  1444. inc_irq_stat(irq_spurious_count);
  1445. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1446. pr_info("spurious APIC interrupt on CPU#%d, "
  1447. "should never happen.\n", smp_processor_id());
  1448. irq_exit();
  1449. }
  1450. /*
  1451. * This interrupt should never happen with our APIC/SMP architecture
  1452. */
  1453. void smp_error_interrupt(struct pt_regs *regs)
  1454. {
  1455. u32 v, v1;
  1456. exit_idle();
  1457. irq_enter();
  1458. /* First tickle the hardware, only then report what went on. -- REW */
  1459. v = apic_read(APIC_ESR);
  1460. apic_write(APIC_ESR, 0);
  1461. v1 = apic_read(APIC_ESR);
  1462. ack_APIC_irq();
  1463. atomic_inc(&irq_err_count);
  1464. /*
  1465. * Here is what the APIC error bits mean:
  1466. * 0: Send CS error
  1467. * 1: Receive CS error
  1468. * 2: Send accept error
  1469. * 3: Receive accept error
  1470. * 4: Reserved
  1471. * 5: Send illegal vector
  1472. * 6: Received illegal vector
  1473. * 7: Illegal register address
  1474. */
  1475. pr_debug("APIC error on CPU%d: %02x(%02x)\n",
  1476. smp_processor_id(), v , v1);
  1477. irq_exit();
  1478. }
  1479. /**
  1480. * connect_bsp_APIC - attach the APIC to the interrupt system
  1481. */
  1482. void __init connect_bsp_APIC(void)
  1483. {
  1484. #ifdef CONFIG_X86_32
  1485. if (pic_mode) {
  1486. /*
  1487. * Do not trust the local APIC being empty at bootup.
  1488. */
  1489. clear_local_APIC();
  1490. /*
  1491. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1492. * local APIC to INT and NMI lines.
  1493. */
  1494. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1495. "enabling APIC mode.\n");
  1496. outb(0x70, 0x22);
  1497. outb(0x01, 0x23);
  1498. }
  1499. #endif
  1500. enable_apic_mode();
  1501. }
  1502. /**
  1503. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1504. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1505. *
  1506. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1507. * APIC is disabled.
  1508. */
  1509. void disconnect_bsp_APIC(int virt_wire_setup)
  1510. {
  1511. unsigned int value;
  1512. #ifdef CONFIG_X86_32
  1513. if (pic_mode) {
  1514. /*
  1515. * Put the board back into PIC mode (has an effect only on
  1516. * certain older boards). Note that APIC interrupts, including
  1517. * IPIs, won't work beyond this point! The only exception are
  1518. * INIT IPIs.
  1519. */
  1520. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1521. "entering PIC mode.\n");
  1522. outb(0x70, 0x22);
  1523. outb(0x00, 0x23);
  1524. return;
  1525. }
  1526. #endif
  1527. /* Go back to Virtual Wire compatibility mode */
  1528. /* For the spurious interrupt use vector F, and enable it */
  1529. value = apic_read(APIC_SPIV);
  1530. value &= ~APIC_VECTOR_MASK;
  1531. value |= APIC_SPIV_APIC_ENABLED;
  1532. value |= 0xf;
  1533. apic_write(APIC_SPIV, value);
  1534. if (!virt_wire_setup) {
  1535. /*
  1536. * For LVT0 make it edge triggered, active high,
  1537. * external and enabled
  1538. */
  1539. value = apic_read(APIC_LVT0);
  1540. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1541. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1542. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1543. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1544. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1545. apic_write(APIC_LVT0, value);
  1546. } else {
  1547. /* Disable LVT0 */
  1548. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  1549. }
  1550. /*
  1551. * For LVT1 make it edge triggered, active high,
  1552. * nmi and enabled
  1553. */
  1554. value = apic_read(APIC_LVT1);
  1555. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1556. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1557. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1558. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1559. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1560. apic_write(APIC_LVT1, value);
  1561. }
  1562. void __cpuinit generic_processor_info(int apicid, int version)
  1563. {
  1564. int cpu;
  1565. /*
  1566. * Validate version
  1567. */
  1568. if (version == 0x0) {
  1569. pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
  1570. "fixing up to 0x10. (tell your hw vendor)\n",
  1571. version);
  1572. version = 0x10;
  1573. }
  1574. apic_version[apicid] = version;
  1575. if (num_processors >= nr_cpu_ids) {
  1576. int max = nr_cpu_ids;
  1577. int thiscpu = max + disabled_cpus;
  1578. pr_warning(
  1579. "ACPI: NR_CPUS/possible_cpus limit of %i reached."
  1580. " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
  1581. disabled_cpus++;
  1582. return;
  1583. }
  1584. num_processors++;
  1585. cpu = cpumask_next_zero(-1, cpu_present_mask);
  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. early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  1620. early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  1621. #endif
  1622. set_cpu_possible(cpu, true);
  1623. set_cpu_present(cpu, true);
  1624. }
  1625. #ifdef CONFIG_X86_64
  1626. int hard_smp_processor_id(void)
  1627. {
  1628. return read_apic_id();
  1629. }
  1630. #endif
  1631. /*
  1632. * Power management
  1633. */
  1634. #ifdef CONFIG_PM
  1635. static struct {
  1636. /*
  1637. * 'active' is true if the local APIC was enabled by us and
  1638. * not the BIOS; this signifies that we are also responsible
  1639. * for disabling it before entering apm/acpi suspend
  1640. */
  1641. int active;
  1642. /* r/w apic fields */
  1643. unsigned int apic_id;
  1644. unsigned int apic_taskpri;
  1645. unsigned int apic_ldr;
  1646. unsigned int apic_dfr;
  1647. unsigned int apic_spiv;
  1648. unsigned int apic_lvtt;
  1649. unsigned int apic_lvtpc;
  1650. unsigned int apic_lvt0;
  1651. unsigned int apic_lvt1;
  1652. unsigned int apic_lvterr;
  1653. unsigned int apic_tmict;
  1654. unsigned int apic_tdcr;
  1655. unsigned int apic_thmr;
  1656. } apic_pm_state;
  1657. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1658. {
  1659. unsigned long flags;
  1660. int maxlvt;
  1661. if (!apic_pm_state.active)
  1662. return 0;
  1663. maxlvt = lapic_get_maxlvt();
  1664. apic_pm_state.apic_id = apic_read(APIC_ID);
  1665. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1666. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1667. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1668. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1669. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1670. if (maxlvt >= 4)
  1671. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1672. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1673. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1674. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1675. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1676. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1677. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1678. if (maxlvt >= 5)
  1679. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1680. #endif
  1681. local_irq_save(flags);
  1682. disable_local_APIC();
  1683. local_irq_restore(flags);
  1684. return 0;
  1685. }
  1686. static int lapic_resume(struct sys_device *dev)
  1687. {
  1688. unsigned int l, h;
  1689. unsigned long flags;
  1690. int maxlvt;
  1691. if (!apic_pm_state.active)
  1692. return 0;
  1693. maxlvt = lapic_get_maxlvt();
  1694. local_irq_save(flags);
  1695. #ifdef HAVE_X2APIC
  1696. if (x2apic)
  1697. enable_x2apic();
  1698. else
  1699. #endif
  1700. {
  1701. /*
  1702. * Make sure the APICBASE points to the right address
  1703. *
  1704. * FIXME! This will be wrong if we ever support suspend on
  1705. * SMP! We'll need to do this as part of the CPU restore!
  1706. */
  1707. rdmsr(MSR_IA32_APICBASE, l, h);
  1708. l &= ~MSR_IA32_APICBASE_BASE;
  1709. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1710. wrmsr(MSR_IA32_APICBASE, l, h);
  1711. }
  1712. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1713. apic_write(APIC_ID, apic_pm_state.apic_id);
  1714. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1715. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1716. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1717. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1718. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1719. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1720. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1721. if (maxlvt >= 5)
  1722. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1723. #endif
  1724. if (maxlvt >= 4)
  1725. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1726. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1727. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1728. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1729. apic_write(APIC_ESR, 0);
  1730. apic_read(APIC_ESR);
  1731. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1732. apic_write(APIC_ESR, 0);
  1733. apic_read(APIC_ESR);
  1734. local_irq_restore(flags);
  1735. return 0;
  1736. }
  1737. /*
  1738. * This device has no shutdown method - fully functioning local APICs
  1739. * are needed on every CPU up until machine_halt/restart/poweroff.
  1740. */
  1741. static struct sysdev_class lapic_sysclass = {
  1742. .name = "lapic",
  1743. .resume = lapic_resume,
  1744. .suspend = lapic_suspend,
  1745. };
  1746. static struct sys_device device_lapic = {
  1747. .id = 0,
  1748. .cls = &lapic_sysclass,
  1749. };
  1750. static void __cpuinit apic_pm_activate(void)
  1751. {
  1752. apic_pm_state.active = 1;
  1753. }
  1754. static int __init init_lapic_sysfs(void)
  1755. {
  1756. int error;
  1757. if (!cpu_has_apic)
  1758. return 0;
  1759. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1760. error = sysdev_class_register(&lapic_sysclass);
  1761. if (!error)
  1762. error = sysdev_register(&device_lapic);
  1763. return error;
  1764. }
  1765. device_initcall(init_lapic_sysfs);
  1766. #else /* CONFIG_PM */
  1767. static void apic_pm_activate(void) { }
  1768. #endif /* CONFIG_PM */
  1769. #ifdef CONFIG_X86_64
  1770. /*
  1771. * apic_is_clustered_box() -- Check if we can expect good TSC
  1772. *
  1773. * Thus far, the major user of this is IBM's Summit2 series:
  1774. *
  1775. * Clustered boxes may have unsynced TSC problems if they are
  1776. * multi-chassis. Use available data to take a good guess.
  1777. * If in doubt, go HPET.
  1778. */
  1779. __cpuinit int apic_is_clustered_box(void)
  1780. {
  1781. int i, clusters, zeros;
  1782. unsigned id;
  1783. u16 *bios_cpu_apicid;
  1784. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1785. /*
  1786. * there is not this kind of box with AMD CPU yet.
  1787. * Some AMD box with quadcore cpu and 8 sockets apicid
  1788. * will be [4, 0x23] or [8, 0x27] could be thought to
  1789. * vsmp box still need checking...
  1790. */
  1791. if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
  1792. return 0;
  1793. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1794. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1795. for (i = 0; i < nr_cpu_ids; i++) {
  1796. /* are we being called early in kernel startup? */
  1797. if (bios_cpu_apicid) {
  1798. id = bios_cpu_apicid[i];
  1799. } else if (i < nr_cpu_ids) {
  1800. if (cpu_present(i))
  1801. id = per_cpu(x86_bios_cpu_apicid, i);
  1802. else
  1803. continue;
  1804. } else
  1805. break;
  1806. if (id != BAD_APICID)
  1807. __set_bit(APIC_CLUSTERID(id), clustermap);
  1808. }
  1809. /* Problem: Partially populated chassis may not have CPUs in some of
  1810. * the APIC clusters they have been allocated. Only present CPUs have
  1811. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1812. * Since clusters are allocated sequentially, count zeros only if
  1813. * they are bounded by ones.
  1814. */
  1815. clusters = 0;
  1816. zeros = 0;
  1817. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1818. if (test_bit(i, clustermap)) {
  1819. clusters += 1 + zeros;
  1820. zeros = 0;
  1821. } else
  1822. ++zeros;
  1823. }
  1824. /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1825. * not guaranteed to be synced between boards
  1826. */
  1827. if (is_vsmp_box() && clusters > 1)
  1828. return 1;
  1829. /*
  1830. * If clusters > 2, then should be multi-chassis.
  1831. * May have to revisit this when multi-core + hyperthreaded CPUs come
  1832. * out, but AFAIK this will work even for them.
  1833. */
  1834. return (clusters > 2);
  1835. }
  1836. #endif
  1837. /*
  1838. * APIC command line parameters
  1839. */
  1840. static int __init setup_disableapic(char *arg)
  1841. {
  1842. disable_apic = 1;
  1843. setup_clear_cpu_cap(X86_FEATURE_APIC);
  1844. return 0;
  1845. }
  1846. early_param("disableapic", setup_disableapic);
  1847. /* same as disableapic, for compatibility */
  1848. static int __init setup_nolapic(char *arg)
  1849. {
  1850. return setup_disableapic(arg);
  1851. }
  1852. early_param("nolapic", setup_nolapic);
  1853. static int __init parse_lapic_timer_c2_ok(char *arg)
  1854. {
  1855. local_apic_timer_c2_ok = 1;
  1856. return 0;
  1857. }
  1858. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1859. static int __init parse_disable_apic_timer(char *arg)
  1860. {
  1861. disable_apic_timer = 1;
  1862. return 0;
  1863. }
  1864. early_param("noapictimer", parse_disable_apic_timer);
  1865. static int __init parse_nolapic_timer(char *arg)
  1866. {
  1867. disable_apic_timer = 1;
  1868. return 0;
  1869. }
  1870. early_param("nolapic_timer", parse_nolapic_timer);
  1871. static int __init apic_set_verbosity(char *arg)
  1872. {
  1873. if (!arg) {
  1874. #ifdef CONFIG_X86_64
  1875. skip_ioapic_setup = 0;
  1876. return 0;
  1877. #endif
  1878. return -EINVAL;
  1879. }
  1880. if (strcmp("debug", arg) == 0)
  1881. apic_verbosity = APIC_DEBUG;
  1882. else if (strcmp("verbose", arg) == 0)
  1883. apic_verbosity = APIC_VERBOSE;
  1884. else {
  1885. pr_warning("APIC Verbosity level %s not recognised"
  1886. " use apic=verbose or apic=debug\n", arg);
  1887. return -EINVAL;
  1888. }
  1889. return 0;
  1890. }
  1891. early_param("apic", apic_set_verbosity);
  1892. static int __init lapic_insert_resource(void)
  1893. {
  1894. if (!apic_phys)
  1895. return -1;
  1896. /* Put local APIC into the resource map. */
  1897. lapic_resource.start = apic_phys;
  1898. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  1899. insert_resource(&iomem_resource, &lapic_resource);
  1900. return 0;
  1901. }
  1902. /*
  1903. * need call insert after e820_reserve_resources()
  1904. * that is using request_resource
  1905. */
  1906. late_initcall(lapic_insert_resource);