apic.c 54 KB

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