apic.c 70 KB

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