apic.c 68 KB

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