apic.c 57 KB

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