apic.c 75 KB

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