apic.c 53 KB

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