apic.c 63 KB

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