apic.c 52 KB

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