apic.c 54 KB

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