apic.c 63 KB

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