apic.c 57 KB

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