apic.c 56 KB

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