apic.c 67 KB

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