apic.c 54 KB

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