apic.c 61 KB

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