apic.c 53 KB

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