apic.c 62 KB

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