apic.c 66 KB

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