apic.c 64 KB

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