apic.c 64 KB

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