apic.c 65 KB

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