apic.c 55 KB

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