apic.c 67 KB

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