apic.c 65 KB

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