apic.c 68 KB

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