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