apic.c 62 KB

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