apic.c 53 KB

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