apic.c 65 KB

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