apic.c 55 KB

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