apic.c 57 KB

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