apic.c 59 KB

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