apic.c 56 KB

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