apic.c 55 KB

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