apic.c 67 KB

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