apic.c 56 KB

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