apic.c 66 KB

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