apic.c 56 KB

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