apic.c 55 KB

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