apic.c 70 KB

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