apic.c 63 KB

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