apic.c 53 KB

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