apic.c 66 KB

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