apic.c 55 KB

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