apic.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  1. /*
  2. * Local APIC handling, local APIC timers
  3. *
  4. * (c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Fixes
  7. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  8. * thanks to Eric Gilmore
  9. * and Rolf G. Tews
  10. * for testing these extensively.
  11. * Maciej W. Rozycki : Various updates and fixes.
  12. * Mikael Pettersson : Power Management for UP-APIC.
  13. * Pavel Machek and
  14. * Mikael Pettersson : PM converted to driver model.
  15. */
  16. #include <linux/perf_event.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/mc146818rtc.h>
  19. #include <linux/acpi_pmtmr.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/ioport.h>
  25. #include <linux/module.h>
  26. #include <linux/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/io_apic.h>
  45. #include <asm/desc.h>
  46. #include <asm/hpet.h>
  47. #include <asm/idle.h>
  48. #include <asm/mtrr.h>
  49. #include <asm/smp.h>
  50. #include <asm/mce.h>
  51. #include <asm/tsc.h>
  52. #include <asm/hypervisor.h>
  53. unsigned int num_processors;
  54. unsigned disabled_cpus __cpuinitdata;
  55. /* Processor that is doing the boot up */
  56. unsigned int boot_cpu_physical_apicid = -1U;
  57. /*
  58. * The highest APIC ID seen during enumeration.
  59. */
  60. unsigned int max_physical_apicid;
  61. /*
  62. * Bitmask of physically existing CPUs:
  63. */
  64. physid_mask_t phys_cpu_present_map;
  65. /*
  66. * Map cpu index to physical APIC ID
  67. */
  68. DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
  69. DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
  70. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  71. EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
  72. #ifdef CONFIG_X86_32
  73. /*
  74. * Knob to control our willingness to enable the local APIC.
  75. *
  76. * +1=force-enable
  77. */
  78. static int force_enable_local_apic;
  79. /*
  80. * APIC command line parameters
  81. */
  82. static int __init parse_lapic(char *arg)
  83. {
  84. force_enable_local_apic = 1;
  85. return 0;
  86. }
  87. early_param("lapic", parse_lapic);
  88. /* Local APIC was disabled by the BIOS and enabled by the kernel */
  89. static int enabled_via_apicbase;
  90. /*
  91. * Handle interrupt mode configuration register (IMCR).
  92. * This register controls whether the interrupt signals
  93. * that reach the BSP come from the master PIC or from the
  94. * local APIC. Before entering Symmetric I/O Mode, either
  95. * the BIOS or the operating system must switch out of
  96. * PIC Mode by changing the IMCR.
  97. */
  98. static inline void imcr_pic_to_apic(void)
  99. {
  100. /* select IMCR register */
  101. outb(0x70, 0x22);
  102. /* NMI and 8259 INTR go through APIC */
  103. outb(0x01, 0x23);
  104. }
  105. static inline void imcr_apic_to_pic(void)
  106. {
  107. /* select IMCR register */
  108. outb(0x70, 0x22);
  109. /* NMI and 8259 INTR go directly to BSP */
  110. outb(0x00, 0x23);
  111. }
  112. #endif
  113. #ifdef CONFIG_X86_64
  114. static int apic_calibrate_pmtmr __initdata;
  115. static __init int setup_apicpmtimer(char *s)
  116. {
  117. apic_calibrate_pmtmr = 1;
  118. notsc_setup(NULL);
  119. return 0;
  120. }
  121. __setup("apicpmtimer", setup_apicpmtimer);
  122. #endif
  123. int x2apic_mode;
  124. #ifdef CONFIG_X86_X2APIC
  125. /* x2apic enabled before OS handover */
  126. static int x2apic_preenabled;
  127. static __init int setup_nox2apic(char *str)
  128. {
  129. if (x2apic_enabled()) {
  130. pr_warning("Bios already enabled x2apic, "
  131. "can't enforce nox2apic");
  132. return 0;
  133. }
  134. setup_clear_cpu_cap(X86_FEATURE_X2APIC);
  135. return 0;
  136. }
  137. early_param("nox2apic", setup_nox2apic);
  138. #endif
  139. unsigned long mp_lapic_addr;
  140. int disable_apic;
  141. /* Disable local APIC timer from the kernel commandline or via dmi quirk */
  142. static int disable_apic_timer __cpuinitdata;
  143. /* Local APIC timer works in C2 */
  144. int local_apic_timer_c2_ok;
  145. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  146. int first_system_vector = 0xfe;
  147. /*
  148. * Debug level, exported for io_apic.c
  149. */
  150. unsigned int apic_verbosity;
  151. int pic_mode;
  152. /* Have we found an MP table */
  153. int smp_found_config;
  154. static struct resource lapic_resource = {
  155. .name = "Local APIC",
  156. .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
  157. };
  158. static unsigned int calibration_result;
  159. static int lapic_next_event(unsigned long delta,
  160. struct clock_event_device *evt);
  161. static void lapic_timer_setup(enum clock_event_mode mode,
  162. struct clock_event_device *evt);
  163. static void lapic_timer_broadcast(const struct cpumask *mask);
  164. static void apic_pm_activate(void);
  165. /*
  166. * The local apic timer can be used for any function which is CPU local.
  167. */
  168. static struct clock_event_device lapic_clockevent = {
  169. .name = "lapic",
  170. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  171. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  172. .shift = 32,
  173. .set_mode = lapic_timer_setup,
  174. .set_next_event = lapic_next_event,
  175. .broadcast = lapic_timer_broadcast,
  176. .rating = 100,
  177. .irq = -1,
  178. };
  179. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  180. static unsigned long apic_phys;
  181. /*
  182. * Get the LAPIC version
  183. */
  184. static inline int lapic_get_version(void)
  185. {
  186. return GET_APIC_VERSION(apic_read(APIC_LVR));
  187. }
  188. /*
  189. * Check, if the APIC is integrated or a separate chip
  190. */
  191. static inline int lapic_is_integrated(void)
  192. {
  193. #ifdef CONFIG_X86_64
  194. return 1;
  195. #else
  196. return APIC_INTEGRATED(lapic_get_version());
  197. #endif
  198. }
  199. /*
  200. * Check, whether this is a modern or a first generation APIC
  201. */
  202. static int modern_apic(void)
  203. {
  204. /* AMD systems use old APIC versions, so check the CPU */
  205. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  206. boot_cpu_data.x86 >= 0xf)
  207. return 1;
  208. return lapic_get_version() >= 0x14;
  209. }
  210. /*
  211. * right after this call apic become NOOP driven
  212. * so apic->write/read doesn't do anything
  213. */
  214. void apic_disable(void)
  215. {
  216. pr_info("APIC: switched to apic NOOP\n");
  217. apic = &apic_noop;
  218. }
  219. void native_apic_wait_icr_idle(void)
  220. {
  221. while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
  222. cpu_relax();
  223. }
  224. u32 native_safe_apic_wait_icr_idle(void)
  225. {
  226. u32 send_status;
  227. int timeout;
  228. timeout = 0;
  229. do {
  230. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  231. if (!send_status)
  232. break;
  233. udelay(100);
  234. } while (timeout++ < 1000);
  235. return send_status;
  236. }
  237. void native_apic_icr_write(u32 low, u32 id)
  238. {
  239. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
  240. apic_write(APIC_ICR, low);
  241. }
  242. u64 native_apic_icr_read(void)
  243. {
  244. u32 icr1, icr2;
  245. icr2 = apic_read(APIC_ICR2);
  246. icr1 = apic_read(APIC_ICR);
  247. return icr1 | ((u64)icr2 << 32);
  248. }
  249. /**
  250. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  251. */
  252. void __cpuinit enable_NMI_through_LVT0(void)
  253. {
  254. unsigned int v;
  255. /* unmask and set to NMI */
  256. v = APIC_DM_NMI;
  257. /* Level triggered for 82489DX (32bit mode) */
  258. if (!lapic_is_integrated())
  259. v |= APIC_LVT_LEVEL_TRIGGER;
  260. apic_write(APIC_LVT0, v);
  261. }
  262. #ifdef CONFIG_X86_32
  263. /**
  264. * get_physical_broadcast - Get number of physical broadcast IDs
  265. */
  266. int get_physical_broadcast(void)
  267. {
  268. return modern_apic() ? 0xff : 0xf;
  269. }
  270. #endif
  271. /**
  272. * lapic_get_maxlvt - get the maximum number of local vector table entries
  273. */
  274. int lapic_get_maxlvt(void)
  275. {
  276. unsigned int v;
  277. v = apic_read(APIC_LVR);
  278. /*
  279. * - we always have APIC integrated on 64bit mode
  280. * - 82489DXs do not report # of LVT entries
  281. */
  282. return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
  283. }
  284. /*
  285. * Local APIC timer
  286. */
  287. /* Clock divisor */
  288. #define APIC_DIVISOR 16
  289. /*
  290. * This function sets up the local APIC timer, with a timeout of
  291. * 'clocks' APIC bus clock. During calibration we actually call
  292. * this function twice on the boot CPU, once with a bogus timeout
  293. * value, second time for real. The other (noncalibrating) CPUs
  294. * call this function only once, with the real, calibrated value.
  295. *
  296. * We do reads before writes even if unnecessary, to get around the
  297. * P5 APIC double write bug.
  298. */
  299. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  300. {
  301. unsigned int lvtt_value, tmp_value;
  302. lvtt_value = LOCAL_TIMER_VECTOR;
  303. if (!oneshot)
  304. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  305. if (!lapic_is_integrated())
  306. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  307. if (!irqen)
  308. lvtt_value |= APIC_LVT_MASKED;
  309. apic_write(APIC_LVTT, lvtt_value);
  310. /*
  311. * Divide PICLK by 16
  312. */
  313. tmp_value = apic_read(APIC_TDCR);
  314. apic_write(APIC_TDCR,
  315. (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
  316. APIC_TDR_DIV_16);
  317. if (!oneshot)
  318. apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
  319. }
  320. /*
  321. * Setup extended LVT, AMD specific
  322. *
  323. * Software should use the LVT offsets the BIOS provides. The offsets
  324. * are determined by the subsystems using it like those for MCE
  325. * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
  326. * are supported. Beginning with family 10h at least 4 offsets are
  327. * available.
  328. *
  329. * Since the offsets must be consistent for all cores, we keep track
  330. * of the LVT offsets in software and reserve the offset for the same
  331. * vector also to be used on other cores. An offset is freed by
  332. * setting the entry to APIC_EILVT_MASKED.
  333. *
  334. * If the BIOS is right, there should be no conflicts. Otherwise a
  335. * "[Firmware Bug]: ..." error message is generated. However, if
  336. * software does not properly determines the offsets, it is not
  337. * necessarily a BIOS bug.
  338. */
  339. static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
  340. static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
  341. {
  342. return (old & APIC_EILVT_MASKED)
  343. || (new == APIC_EILVT_MASKED)
  344. || ((new & ~APIC_EILVT_MASKED) == old);
  345. }
  346. static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
  347. {
  348. unsigned int rsvd; /* 0: uninitialized */
  349. if (offset >= APIC_EILVT_NR_MAX)
  350. return ~0;
  351. rsvd = atomic_read(&eilvt_offsets[offset]) & ~APIC_EILVT_MASKED;
  352. do {
  353. if (rsvd &&
  354. !eilvt_entry_is_changeable(rsvd, new))
  355. /* may not change if vectors are different */
  356. return rsvd;
  357. rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
  358. } while (rsvd != new);
  359. return new;
  360. }
  361. /*
  362. * If mask=1, the LVT entry does not generate interrupts while mask=0
  363. * enables the vector. See also the BKDGs.
  364. */
  365. int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
  366. {
  367. unsigned long reg = APIC_EILVTn(offset);
  368. unsigned int new, old, reserved;
  369. new = (mask << 16) | (msg_type << 8) | vector;
  370. old = apic_read(reg);
  371. reserved = reserve_eilvt_offset(offset, new);
  372. if (reserved != new) {
  373. pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
  374. "vector 0x%x, but the register is already in use for "
  375. "vector 0x%x on another cpu\n",
  376. smp_processor_id(), reg, offset, new, reserved);
  377. return -EINVAL;
  378. }
  379. if (!eilvt_entry_is_changeable(old, new)) {
  380. pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
  381. "vector 0x%x, but the register is already in use for "
  382. "vector 0x%x on this cpu\n",
  383. smp_processor_id(), reg, offset, new, 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(__this_cpu_ptr(&cpu_info), 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(0x7FFFFFFF, &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. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  680. /* Setup the lapic or request the broadcast */
  681. setup_APIC_timer();
  682. }
  683. void __cpuinit setup_secondary_APIC_clock(void)
  684. {
  685. setup_APIC_timer();
  686. }
  687. /*
  688. * The guts of the apic timer interrupt
  689. */
  690. static void local_apic_timer_interrupt(void)
  691. {
  692. int cpu = smp_processor_id();
  693. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  694. /*
  695. * Normally we should not be here till LAPIC has been initialized but
  696. * in some cases like kdump, its possible that there is a pending LAPIC
  697. * timer interrupt from previous kernel's context and is delivered in
  698. * new kernel the moment interrupts are enabled.
  699. *
  700. * Interrupts are enabled early and LAPIC is setup much later, hence
  701. * its possible that when we get here evt->event_handler is NULL.
  702. * Check for event_handler being NULL and discard the interrupt as
  703. * spurious.
  704. */
  705. if (!evt->event_handler) {
  706. pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  707. /* Switch it off */
  708. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  709. return;
  710. }
  711. /*
  712. * the NMI deadlock-detector uses this.
  713. */
  714. inc_irq_stat(apic_timer_irqs);
  715. evt->event_handler(evt);
  716. }
  717. /*
  718. * Local APIC timer interrupt. This is the most natural way for doing
  719. * local interrupts, but local timer interrupts can be emulated by
  720. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  721. *
  722. * [ if a single-CPU system runs an SMP kernel then we call the local
  723. * interrupt as well. Thus we cannot inline the local irq ... ]
  724. */
  725. void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
  726. {
  727. struct pt_regs *old_regs = set_irq_regs(regs);
  728. /*
  729. * NOTE! We'd better ACK the irq immediately,
  730. * because timer handling can be slow.
  731. */
  732. ack_APIC_irq();
  733. /*
  734. * update_process_times() expects us to have done irq_enter().
  735. * Besides, if we don't timer interrupts ignore the global
  736. * interrupt lock, which is the WrongThing (tm) to do.
  737. */
  738. exit_idle();
  739. irq_enter();
  740. local_apic_timer_interrupt();
  741. irq_exit();
  742. set_irq_regs(old_regs);
  743. }
  744. int setup_profiling_timer(unsigned int multiplier)
  745. {
  746. return -EINVAL;
  747. }
  748. /*
  749. * Local APIC start and shutdown
  750. */
  751. /**
  752. * clear_local_APIC - shutdown the local APIC
  753. *
  754. * This is called, when a CPU is disabled and before rebooting, so the state of
  755. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  756. * leftovers during boot.
  757. */
  758. void clear_local_APIC(void)
  759. {
  760. int maxlvt;
  761. u32 v;
  762. /* APIC hasn't been mapped yet */
  763. if (!x2apic_mode && !apic_phys)
  764. return;
  765. maxlvt = lapic_get_maxlvt();
  766. /*
  767. * Masking an LVT entry can trigger a local APIC error
  768. * if the vector is zero. Mask LVTERR first to prevent this.
  769. */
  770. if (maxlvt >= 3) {
  771. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  772. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  773. }
  774. /*
  775. * Careful: we have to set masks only first to deassert
  776. * any level-triggered sources.
  777. */
  778. v = apic_read(APIC_LVTT);
  779. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  780. v = apic_read(APIC_LVT0);
  781. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  782. v = apic_read(APIC_LVT1);
  783. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  784. if (maxlvt >= 4) {
  785. v = apic_read(APIC_LVTPC);
  786. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  787. }
  788. /* lets not touch this if we didn't frob it */
  789. #ifdef CONFIG_X86_THERMAL_VECTOR
  790. if (maxlvt >= 5) {
  791. v = apic_read(APIC_LVTTHMR);
  792. apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  793. }
  794. #endif
  795. #ifdef CONFIG_X86_MCE_INTEL
  796. if (maxlvt >= 6) {
  797. v = apic_read(APIC_LVTCMCI);
  798. if (!(v & APIC_LVT_MASKED))
  799. apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
  800. }
  801. #endif
  802. /*
  803. * Clean APIC state for other OSs:
  804. */
  805. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  806. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  807. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  808. if (maxlvt >= 3)
  809. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  810. if (maxlvt >= 4)
  811. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  812. /* Integrated APIC (!82489DX) ? */
  813. if (lapic_is_integrated()) {
  814. if (maxlvt > 3)
  815. /* Clear ESR due to Pentium errata 3AP and 11AP */
  816. apic_write(APIC_ESR, 0);
  817. apic_read(APIC_ESR);
  818. }
  819. }
  820. /**
  821. * disable_local_APIC - clear and disable the local APIC
  822. */
  823. void disable_local_APIC(void)
  824. {
  825. unsigned int value;
  826. /* APIC hasn't been mapped yet */
  827. if (!x2apic_mode && !apic_phys)
  828. return;
  829. clear_local_APIC();
  830. /*
  831. * Disable APIC (implies clearing of registers
  832. * for 82489DX!).
  833. */
  834. value = apic_read(APIC_SPIV);
  835. value &= ~APIC_SPIV_APIC_ENABLED;
  836. apic_write(APIC_SPIV, value);
  837. #ifdef CONFIG_X86_32
  838. /*
  839. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  840. * restore the disabled state.
  841. */
  842. if (enabled_via_apicbase) {
  843. unsigned int l, h;
  844. rdmsr(MSR_IA32_APICBASE, l, h);
  845. l &= ~MSR_IA32_APICBASE_ENABLE;
  846. wrmsr(MSR_IA32_APICBASE, l, h);
  847. }
  848. #endif
  849. }
  850. /*
  851. * If Linux enabled the LAPIC against the BIOS default disable it down before
  852. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  853. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  854. * for the case where Linux didn't enable the LAPIC.
  855. */
  856. void lapic_shutdown(void)
  857. {
  858. unsigned long flags;
  859. if (!cpu_has_apic && !apic_from_smp_config())
  860. return;
  861. local_irq_save(flags);
  862. #ifdef CONFIG_X86_32
  863. if (!enabled_via_apicbase)
  864. clear_local_APIC();
  865. else
  866. #endif
  867. disable_local_APIC();
  868. local_irq_restore(flags);
  869. }
  870. /*
  871. * This is to verify that we're looking at a real local APIC.
  872. * Check these against your board if the CPUs aren't getting
  873. * started for no apparent reason.
  874. */
  875. int __init verify_local_APIC(void)
  876. {
  877. unsigned int reg0, reg1;
  878. /*
  879. * The version register is read-only in a real APIC.
  880. */
  881. reg0 = apic_read(APIC_LVR);
  882. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  883. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  884. reg1 = apic_read(APIC_LVR);
  885. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  886. /*
  887. * The two version reads above should print the same
  888. * numbers. If the second one is different, then we
  889. * poke at a non-APIC.
  890. */
  891. if (reg1 != reg0)
  892. return 0;
  893. /*
  894. * Check if the version looks reasonably.
  895. */
  896. reg1 = GET_APIC_VERSION(reg0);
  897. if (reg1 == 0x00 || reg1 == 0xff)
  898. return 0;
  899. reg1 = lapic_get_maxlvt();
  900. if (reg1 < 0x02 || reg1 == 0xff)
  901. return 0;
  902. /*
  903. * The ID register is read/write in a real APIC.
  904. */
  905. reg0 = apic_read(APIC_ID);
  906. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  907. apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
  908. reg1 = apic_read(APIC_ID);
  909. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  910. apic_write(APIC_ID, reg0);
  911. if (reg1 != (reg0 ^ apic->apic_id_mask))
  912. return 0;
  913. /*
  914. * The next two are just to see if we have sane values.
  915. * They're only really relevant if we're in Virtual Wire
  916. * compatibility mode, but most boxes are anymore.
  917. */
  918. reg0 = apic_read(APIC_LVT0);
  919. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  920. reg1 = apic_read(APIC_LVT1);
  921. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  922. return 1;
  923. }
  924. /**
  925. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  926. */
  927. void __init sync_Arb_IDs(void)
  928. {
  929. /*
  930. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  931. * needed on AMD.
  932. */
  933. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  934. return;
  935. /*
  936. * Wait for idle.
  937. */
  938. apic_wait_icr_idle();
  939. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  940. apic_write(APIC_ICR, APIC_DEST_ALLINC |
  941. APIC_INT_LEVELTRIG | APIC_DM_INIT);
  942. }
  943. /*
  944. * An initial setup of the virtual wire mode.
  945. */
  946. void __init init_bsp_APIC(void)
  947. {
  948. unsigned int value;
  949. /*
  950. * Don't do the setup now if we have a SMP BIOS as the
  951. * through-I/O-APIC virtual wire mode might be active.
  952. */
  953. if (smp_found_config || !cpu_has_apic)
  954. return;
  955. /*
  956. * Do not trust the local APIC being empty at bootup.
  957. */
  958. clear_local_APIC();
  959. /*
  960. * Enable APIC.
  961. */
  962. value = apic_read(APIC_SPIV);
  963. value &= ~APIC_VECTOR_MASK;
  964. value |= APIC_SPIV_APIC_ENABLED;
  965. #ifdef CONFIG_X86_32
  966. /* This bit is reserved on P4/Xeon and should be cleared */
  967. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  968. (boot_cpu_data.x86 == 15))
  969. value &= ~APIC_SPIV_FOCUS_DISABLED;
  970. else
  971. #endif
  972. value |= APIC_SPIV_FOCUS_DISABLED;
  973. value |= SPURIOUS_APIC_VECTOR;
  974. apic_write(APIC_SPIV, value);
  975. /*
  976. * Set up the virtual wire mode.
  977. */
  978. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  979. value = APIC_DM_NMI;
  980. if (!lapic_is_integrated()) /* 82489DX */
  981. value |= APIC_LVT_LEVEL_TRIGGER;
  982. apic_write(APIC_LVT1, value);
  983. }
  984. static void __cpuinit lapic_setup_esr(void)
  985. {
  986. unsigned int oldvalue, value, maxlvt;
  987. if (!lapic_is_integrated()) {
  988. pr_info("No ESR for 82489DX.\n");
  989. return;
  990. }
  991. if (apic->disable_esr) {
  992. /*
  993. * Something untraceable is creating bad interrupts on
  994. * secondary quads ... for the moment, just leave the
  995. * ESR disabled - we can't do anything useful with the
  996. * errors anyway - mbligh
  997. */
  998. pr_info("Leaving ESR disabled.\n");
  999. return;
  1000. }
  1001. maxlvt = lapic_get_maxlvt();
  1002. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1003. apic_write(APIC_ESR, 0);
  1004. oldvalue = apic_read(APIC_ESR);
  1005. /* enables sending errors */
  1006. value = ERROR_APIC_VECTOR;
  1007. apic_write(APIC_LVTERR, value);
  1008. /*
  1009. * spec says clear errors after enabling vector.
  1010. */
  1011. if (maxlvt > 3)
  1012. apic_write(APIC_ESR, 0);
  1013. value = apic_read(APIC_ESR);
  1014. if (value != oldvalue)
  1015. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  1016. "vector: 0x%08x after: 0x%08x\n",
  1017. oldvalue, value);
  1018. }
  1019. /**
  1020. * setup_local_APIC - setup the local APIC
  1021. *
  1022. * Used to setup local APIC while initializing BSP or bringin up APs.
  1023. * Always called with preemption disabled.
  1024. */
  1025. void __cpuinit setup_local_APIC(void)
  1026. {
  1027. int cpu = smp_processor_id();
  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. disable_ioapic_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. /*
  1049. * Double-check whether this APIC is really registered.
  1050. * This is meaningless in clustered apic mode, so we skip it.
  1051. */
  1052. BUG_ON(!apic->apic_id_registered());
  1053. /*
  1054. * Intel recommends to set DFR, LDR and TPR before enabling
  1055. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  1056. * document number 292116). So here it goes...
  1057. */
  1058. apic->init_apic_ldr();
  1059. /*
  1060. * Set Task Priority to 'accept all'. We never change this
  1061. * later on.
  1062. */
  1063. value = apic_read(APIC_TASKPRI);
  1064. value &= ~APIC_TPRI_MASK;
  1065. apic_write(APIC_TASKPRI, value);
  1066. /*
  1067. * After a crash, we no longer service the interrupts and a pending
  1068. * interrupt from previous kernel might still have ISR bit set.
  1069. *
  1070. * Most probably by now CPU has serviced that pending interrupt and
  1071. * it might not have done the ack_APIC_irq() because it thought,
  1072. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  1073. * does not clear the ISR bit and cpu thinks it has already serivced
  1074. * the interrupt. Hence a vector might get locked. It was noticed
  1075. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  1076. */
  1077. do {
  1078. queued = 0;
  1079. for (i = APIC_ISR_NR - 1; i >= 0; i--)
  1080. queued |= apic_read(APIC_IRR + i*0x10);
  1081. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  1082. value = apic_read(APIC_ISR + i*0x10);
  1083. for (j = 31; j >= 0; j--) {
  1084. if (value & (1<<j)) {
  1085. ack_APIC_irq();
  1086. acked++;
  1087. }
  1088. }
  1089. }
  1090. if (acked > 256) {
  1091. printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
  1092. acked);
  1093. break;
  1094. }
  1095. if (cpu_has_tsc) {
  1096. rdtscll(ntsc);
  1097. max_loops = (cpu_khz << 10) - (ntsc - tsc);
  1098. } else
  1099. max_loops--;
  1100. } while (queued && max_loops > 0);
  1101. WARN_ON(max_loops <= 0);
  1102. /*
  1103. * Now that we are all set up, enable the APIC
  1104. */
  1105. value = apic_read(APIC_SPIV);
  1106. value &= ~APIC_VECTOR_MASK;
  1107. /*
  1108. * Enable APIC
  1109. */
  1110. value |= APIC_SPIV_APIC_ENABLED;
  1111. #ifdef CONFIG_X86_32
  1112. /*
  1113. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  1114. * certain networking cards. If high frequency interrupts are
  1115. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  1116. * entry is masked/unmasked at a high rate as well then sooner or
  1117. * later IOAPIC line gets 'stuck', no more interrupts are received
  1118. * from the device. If focus CPU is disabled then the hang goes
  1119. * away, oh well :-(
  1120. *
  1121. * [ This bug can be reproduced easily with a level-triggered
  1122. * PCI Ne2000 networking cards and PII/PIII processors, dual
  1123. * BX chipset. ]
  1124. */
  1125. /*
  1126. * Actually disabling the focus CPU check just makes the hang less
  1127. * frequent as it makes the interrupt distributon model be more
  1128. * like LRU than MRU (the short-term load is more even across CPUs).
  1129. * See also the comment in end_level_ioapic_irq(). --macro
  1130. */
  1131. /*
  1132. * - enable focus processor (bit==0)
  1133. * - 64bit mode always use processor focus
  1134. * so no need to set it
  1135. */
  1136. value &= ~APIC_SPIV_FOCUS_DISABLED;
  1137. #endif
  1138. /*
  1139. * Set spurious IRQ vector
  1140. */
  1141. value |= SPURIOUS_APIC_VECTOR;
  1142. apic_write(APIC_SPIV, value);
  1143. /*
  1144. * Set up LVT0, LVT1:
  1145. *
  1146. * set up through-local-APIC on the BP's LINT0. This is not
  1147. * strictly necessary in pure symmetric-IO mode, but sometimes
  1148. * we delegate interrupts to the 8259A.
  1149. */
  1150. /*
  1151. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  1152. */
  1153. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  1154. if (!cpu && (pic_mode || !value)) {
  1155. value = APIC_DM_EXTINT;
  1156. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
  1157. } else {
  1158. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  1159. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
  1160. }
  1161. apic_write(APIC_LVT0, value);
  1162. /*
  1163. * only the BP should see the LINT1 NMI signal, obviously.
  1164. */
  1165. if (!cpu)
  1166. value = APIC_DM_NMI;
  1167. else
  1168. value = APIC_DM_NMI | APIC_LVT_MASKED;
  1169. if (!lapic_is_integrated()) /* 82489DX */
  1170. value |= APIC_LVT_LEVEL_TRIGGER;
  1171. apic_write(APIC_LVT1, value);
  1172. #ifdef CONFIG_X86_MCE_INTEL
  1173. /* Recheck CMCI information after local APIC is up on CPU #0 */
  1174. if (!cpu)
  1175. cmci_recheck();
  1176. #endif
  1177. }
  1178. void __cpuinit end_local_APIC_setup(void)
  1179. {
  1180. lapic_setup_esr();
  1181. #ifdef CONFIG_X86_32
  1182. {
  1183. unsigned int value;
  1184. /* Disable the local apic timer */
  1185. value = apic_read(APIC_LVTT);
  1186. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  1187. apic_write(APIC_LVTT, value);
  1188. }
  1189. #endif
  1190. apic_pm_activate();
  1191. }
  1192. void __init bsp_end_local_APIC_setup(void)
  1193. {
  1194. end_local_APIC_setup();
  1195. /*
  1196. * Now that local APIC setup is completed for BP, configure the fault
  1197. * handling for interrupt remapping.
  1198. */
  1199. if (intr_remapping_enabled)
  1200. enable_drhd_fault_handling();
  1201. }
  1202. #ifdef CONFIG_X86_X2APIC
  1203. void check_x2apic(void)
  1204. {
  1205. if (x2apic_enabled()) {
  1206. pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
  1207. x2apic_preenabled = x2apic_mode = 1;
  1208. }
  1209. }
  1210. void enable_x2apic(void)
  1211. {
  1212. int msr, msr2;
  1213. if (!x2apic_mode)
  1214. return;
  1215. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1216. if (!(msr & X2APIC_ENABLE)) {
  1217. printk_once(KERN_INFO "Enabling x2apic\n");
  1218. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  1219. }
  1220. }
  1221. #endif /* CONFIG_X86_X2APIC */
  1222. int __init enable_IR(void)
  1223. {
  1224. #ifdef CONFIG_INTR_REMAP
  1225. if (!intr_remapping_supported()) {
  1226. pr_debug("intr-remapping not supported\n");
  1227. return 0;
  1228. }
  1229. if (!x2apic_preenabled && skip_ioapic_setup) {
  1230. pr_info("Skipped enabling intr-remap because of skipping "
  1231. "io-apic setup\n");
  1232. return 0;
  1233. }
  1234. if (enable_intr_remapping(x2apic_supported()))
  1235. return 0;
  1236. pr_info("Enabled Interrupt-remapping\n");
  1237. return 1;
  1238. #endif
  1239. return 0;
  1240. }
  1241. void __init enable_IR_x2apic(void)
  1242. {
  1243. unsigned long flags;
  1244. struct IO_APIC_route_entry **ioapic_entries;
  1245. int ret, x2apic_enabled = 0;
  1246. int dmar_table_init_ret;
  1247. dmar_table_init_ret = dmar_table_init();
  1248. if (dmar_table_init_ret && !x2apic_supported())
  1249. return;
  1250. ioapic_entries = alloc_ioapic_entries();
  1251. if (!ioapic_entries) {
  1252. pr_err("Allocate ioapic_entries failed\n");
  1253. goto out;
  1254. }
  1255. ret = save_IO_APIC_setup(ioapic_entries);
  1256. if (ret) {
  1257. pr_info("Saving IO-APIC state failed: %d\n", ret);
  1258. goto out;
  1259. }
  1260. local_irq_save(flags);
  1261. legacy_pic->mask_all();
  1262. mask_IO_APIC_setup(ioapic_entries);
  1263. if (dmar_table_init_ret)
  1264. ret = 0;
  1265. else
  1266. ret = enable_IR();
  1267. if (!ret) {
  1268. /* IR is required if there is APIC ID > 255 even when running
  1269. * under KVM
  1270. */
  1271. if (max_physical_apicid > 255 ||
  1272. !hypervisor_x2apic_available())
  1273. goto nox2apic;
  1274. /*
  1275. * without IR all CPUs can be addressed by IOAPIC/MSI
  1276. * only in physical mode
  1277. */
  1278. x2apic_force_phys();
  1279. }
  1280. x2apic_enabled = 1;
  1281. if (x2apic_supported() && !x2apic_mode) {
  1282. x2apic_mode = 1;
  1283. enable_x2apic();
  1284. pr_info("Enabled x2apic\n");
  1285. }
  1286. nox2apic:
  1287. if (!ret) /* IR enabling failed */
  1288. restore_IO_APIC_setup(ioapic_entries);
  1289. legacy_pic->restore_mask();
  1290. local_irq_restore(flags);
  1291. out:
  1292. if (ioapic_entries)
  1293. free_ioapic_entries(ioapic_entries);
  1294. if (x2apic_enabled)
  1295. return;
  1296. if (x2apic_preenabled)
  1297. panic("x2apic: enabled by BIOS but kernel init failed.");
  1298. else if (cpu_has_x2apic)
  1299. pr_info("Not enabling x2apic, Intr-remapping init failed.\n");
  1300. }
  1301. #ifdef CONFIG_X86_64
  1302. /*
  1303. * Detect and enable local APICs on non-SMP boards.
  1304. * Original code written by Keir Fraser.
  1305. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  1306. * not correctly set up (usually the APIC timer won't work etc.)
  1307. */
  1308. static int __init detect_init_APIC(void)
  1309. {
  1310. if (!cpu_has_apic) {
  1311. pr_info("No local APIC present\n");
  1312. return -1;
  1313. }
  1314. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1315. return 0;
  1316. }
  1317. #else
  1318. static int apic_verify(void)
  1319. {
  1320. u32 features, h, l;
  1321. /*
  1322. * The APIC feature bit should now be enabled
  1323. * in `cpuid'
  1324. */
  1325. features = cpuid_edx(1);
  1326. if (!(features & (1 << X86_FEATURE_APIC))) {
  1327. pr_warning("Could not enable APIC!\n");
  1328. return -1;
  1329. }
  1330. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1331. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1332. /* The BIOS may have set up the APIC at some other address */
  1333. rdmsr(MSR_IA32_APICBASE, l, h);
  1334. if (l & MSR_IA32_APICBASE_ENABLE)
  1335. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1336. pr_info("Found and enabled local APIC!\n");
  1337. return 0;
  1338. }
  1339. int apic_force_enable(void)
  1340. {
  1341. u32 h, l;
  1342. if (disable_apic)
  1343. return -1;
  1344. /*
  1345. * Some BIOSes disable the local APIC in the APIC_BASE
  1346. * MSR. This can only be done in software for Intel P6 or later
  1347. * and AMD K7 (Model > 1) or later.
  1348. */
  1349. rdmsr(MSR_IA32_APICBASE, l, h);
  1350. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  1351. pr_info("Local APIC disabled by BIOS -- reenabling.\n");
  1352. l &= ~MSR_IA32_APICBASE_BASE;
  1353. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  1354. wrmsr(MSR_IA32_APICBASE, l, h);
  1355. enabled_via_apicbase = 1;
  1356. }
  1357. return apic_verify();
  1358. }
  1359. /*
  1360. * Detect and initialize APIC
  1361. */
  1362. static int __init detect_init_APIC(void)
  1363. {
  1364. /* Disabled by kernel option? */
  1365. if (disable_apic)
  1366. return -1;
  1367. switch (boot_cpu_data.x86_vendor) {
  1368. case X86_VENDOR_AMD:
  1369. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  1370. (boot_cpu_data.x86 >= 15))
  1371. break;
  1372. goto no_apic;
  1373. case X86_VENDOR_INTEL:
  1374. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  1375. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  1376. break;
  1377. goto no_apic;
  1378. default:
  1379. goto no_apic;
  1380. }
  1381. if (!cpu_has_apic) {
  1382. /*
  1383. * Over-ride BIOS and try to enable the local APIC only if
  1384. * "lapic" specified.
  1385. */
  1386. if (!force_enable_local_apic) {
  1387. pr_info("Local APIC disabled by BIOS -- "
  1388. "you can enable it with \"lapic\"\n");
  1389. return -1;
  1390. }
  1391. if (apic_force_enable())
  1392. return -1;
  1393. } else {
  1394. if (apic_verify())
  1395. return -1;
  1396. }
  1397. apic_pm_activate();
  1398. return 0;
  1399. no_apic:
  1400. pr_info("No local APIC present or hardware disabled\n");
  1401. return -1;
  1402. }
  1403. #endif
  1404. /**
  1405. * init_apic_mappings - initialize APIC mappings
  1406. */
  1407. void __init init_apic_mappings(void)
  1408. {
  1409. unsigned int new_apicid;
  1410. if (x2apic_mode) {
  1411. boot_cpu_physical_apicid = read_apic_id();
  1412. return;
  1413. }
  1414. /* If no local APIC can be found return early */
  1415. if (!smp_found_config && detect_init_APIC()) {
  1416. /* lets NOP'ify apic operations */
  1417. pr_info("APIC: disable apic facility\n");
  1418. apic_disable();
  1419. } else {
  1420. apic_phys = mp_lapic_addr;
  1421. /*
  1422. * acpi lapic path already maps that address in
  1423. * acpi_register_lapic_address()
  1424. */
  1425. if (!acpi_lapic && !smp_found_config)
  1426. register_lapic_address(apic_phys);
  1427. }
  1428. /*
  1429. * Fetch the APIC ID of the BSP in case we have a
  1430. * default configuration (or the MP table is broken).
  1431. */
  1432. new_apicid = read_apic_id();
  1433. if (boot_cpu_physical_apicid != new_apicid) {
  1434. boot_cpu_physical_apicid = new_apicid;
  1435. /*
  1436. * yeah -- we lie about apic_version
  1437. * in case if apic was disabled via boot option
  1438. * but it's not a problem for SMP compiled kernel
  1439. * since smp_sanity_check is prepared for such a case
  1440. * and disable smp mode
  1441. */
  1442. apic_version[new_apicid] =
  1443. GET_APIC_VERSION(apic_read(APIC_LVR));
  1444. }
  1445. }
  1446. void __init register_lapic_address(unsigned long address)
  1447. {
  1448. mp_lapic_addr = address;
  1449. if (!x2apic_mode) {
  1450. set_fixmap_nocache(FIX_APIC_BASE, address);
  1451. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1452. APIC_BASE, mp_lapic_addr);
  1453. }
  1454. if (boot_cpu_physical_apicid == -1U) {
  1455. boot_cpu_physical_apicid = read_apic_id();
  1456. apic_version[boot_cpu_physical_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. bsp_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. if (num_processors >= nr_cpu_ids) {
  1666. int max = nr_cpu_ids;
  1667. int thiscpu = max + disabled_cpus;
  1668. pr_warning(
  1669. "ACPI: NR_CPUS/possible_cpus limit of %i reached."
  1670. " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
  1671. disabled_cpus++;
  1672. return;
  1673. }
  1674. num_processors++;
  1675. if (apicid == boot_cpu_physical_apicid) {
  1676. /*
  1677. * x86_bios_cpu_apicid is required to have processors listed
  1678. * in same order as logical cpu numbers. Hence the first
  1679. * entry is BSP, and so on.
  1680. * boot_cpu_init() already hold bit 0 in cpu_present_mask
  1681. * for BSP.
  1682. */
  1683. cpu = 0;
  1684. } else
  1685. cpu = cpumask_next_zero(-1, cpu_present_mask);
  1686. /*
  1687. * Validate version
  1688. */
  1689. if (version == 0x0) {
  1690. pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
  1691. cpu, apicid);
  1692. version = 0x10;
  1693. }
  1694. apic_version[apicid] = version;
  1695. if (version != apic_version[boot_cpu_physical_apicid]) {
  1696. pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
  1697. apic_version[boot_cpu_physical_apicid], cpu, version);
  1698. }
  1699. physid_set(apicid, phys_cpu_present_map);
  1700. if (apicid > max_physical_apicid)
  1701. max_physical_apicid = apicid;
  1702. #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
  1703. early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  1704. early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  1705. #endif
  1706. set_cpu_possible(cpu, true);
  1707. set_cpu_present(cpu, true);
  1708. }
  1709. int hard_smp_processor_id(void)
  1710. {
  1711. return read_apic_id();
  1712. }
  1713. void default_init_apic_ldr(void)
  1714. {
  1715. unsigned long val;
  1716. apic_write(APIC_DFR, APIC_DFR_VALUE);
  1717. val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
  1718. val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
  1719. apic_write(APIC_LDR, val);
  1720. }
  1721. #ifdef CONFIG_X86_32
  1722. int default_apicid_to_node(int logical_apicid)
  1723. {
  1724. #ifdef CONFIG_SMP
  1725. return apicid_2_node[hard_smp_processor_id()];
  1726. #else
  1727. return 0;
  1728. #endif
  1729. }
  1730. #endif
  1731. /*
  1732. * Power management
  1733. */
  1734. #ifdef CONFIG_PM
  1735. static struct {
  1736. /*
  1737. * 'active' is true if the local APIC was enabled by us and
  1738. * not the BIOS; this signifies that we are also responsible
  1739. * for disabling it before entering apm/acpi suspend
  1740. */
  1741. int active;
  1742. /* r/w apic fields */
  1743. unsigned int apic_id;
  1744. unsigned int apic_taskpri;
  1745. unsigned int apic_ldr;
  1746. unsigned int apic_dfr;
  1747. unsigned int apic_spiv;
  1748. unsigned int apic_lvtt;
  1749. unsigned int apic_lvtpc;
  1750. unsigned int apic_lvt0;
  1751. unsigned int apic_lvt1;
  1752. unsigned int apic_lvterr;
  1753. unsigned int apic_tmict;
  1754. unsigned int apic_tdcr;
  1755. unsigned int apic_thmr;
  1756. } apic_pm_state;
  1757. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1758. {
  1759. unsigned long flags;
  1760. int maxlvt;
  1761. if (!apic_pm_state.active)
  1762. return 0;
  1763. maxlvt = lapic_get_maxlvt();
  1764. apic_pm_state.apic_id = apic_read(APIC_ID);
  1765. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1766. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1767. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1768. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1769. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1770. if (maxlvt >= 4)
  1771. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1772. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1773. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1774. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1775. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1776. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1777. #ifdef CONFIG_X86_THERMAL_VECTOR
  1778. if (maxlvt >= 5)
  1779. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1780. #endif
  1781. local_irq_save(flags);
  1782. disable_local_APIC();
  1783. if (intr_remapping_enabled)
  1784. disable_intr_remapping();
  1785. local_irq_restore(flags);
  1786. return 0;
  1787. }
  1788. static int lapic_resume(struct sys_device *dev)
  1789. {
  1790. unsigned int l, h;
  1791. unsigned long flags;
  1792. int maxlvt;
  1793. int ret = 0;
  1794. struct IO_APIC_route_entry **ioapic_entries = NULL;
  1795. if (!apic_pm_state.active)
  1796. return 0;
  1797. local_irq_save(flags);
  1798. if (intr_remapping_enabled) {
  1799. ioapic_entries = alloc_ioapic_entries();
  1800. if (!ioapic_entries) {
  1801. WARN(1, "Alloc ioapic_entries in lapic resume failed.");
  1802. ret = -ENOMEM;
  1803. goto restore;
  1804. }
  1805. ret = save_IO_APIC_setup(ioapic_entries);
  1806. if (ret) {
  1807. WARN(1, "Saving IO-APIC state failed: %d\n", ret);
  1808. free_ioapic_entries(ioapic_entries);
  1809. goto restore;
  1810. }
  1811. mask_IO_APIC_setup(ioapic_entries);
  1812. legacy_pic->mask_all();
  1813. }
  1814. if (x2apic_mode)
  1815. enable_x2apic();
  1816. else {
  1817. /*
  1818. * Make sure the APICBASE points to the right address
  1819. *
  1820. * FIXME! This will be wrong if we ever support suspend on
  1821. * SMP! We'll need to do this as part of the CPU restore!
  1822. */
  1823. rdmsr(MSR_IA32_APICBASE, l, h);
  1824. l &= ~MSR_IA32_APICBASE_BASE;
  1825. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1826. wrmsr(MSR_IA32_APICBASE, l, h);
  1827. }
  1828. maxlvt = lapic_get_maxlvt();
  1829. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1830. apic_write(APIC_ID, apic_pm_state.apic_id);
  1831. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1832. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1833. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1834. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1835. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1836. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1837. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1838. if (maxlvt >= 5)
  1839. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1840. #endif
  1841. if (maxlvt >= 4)
  1842. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1843. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1844. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1845. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1846. apic_write(APIC_ESR, 0);
  1847. apic_read(APIC_ESR);
  1848. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1849. apic_write(APIC_ESR, 0);
  1850. apic_read(APIC_ESR);
  1851. if (intr_remapping_enabled) {
  1852. reenable_intr_remapping(x2apic_mode);
  1853. legacy_pic->restore_mask();
  1854. restore_IO_APIC_setup(ioapic_entries);
  1855. free_ioapic_entries(ioapic_entries);
  1856. }
  1857. restore:
  1858. local_irq_restore(flags);
  1859. return ret;
  1860. }
  1861. /*
  1862. * This device has no shutdown method - fully functioning local APICs
  1863. * are needed on every CPU up until machine_halt/restart/poweroff.
  1864. */
  1865. static struct sysdev_class lapic_sysclass = {
  1866. .name = "lapic",
  1867. .resume = lapic_resume,
  1868. .suspend = lapic_suspend,
  1869. };
  1870. static struct sys_device device_lapic = {
  1871. .id = 0,
  1872. .cls = &lapic_sysclass,
  1873. };
  1874. static void __cpuinit apic_pm_activate(void)
  1875. {
  1876. apic_pm_state.active = 1;
  1877. }
  1878. static int __init init_lapic_sysfs(void)
  1879. {
  1880. int error;
  1881. if (!cpu_has_apic)
  1882. return 0;
  1883. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1884. error = sysdev_class_register(&lapic_sysclass);
  1885. if (!error)
  1886. error = sysdev_register(&device_lapic);
  1887. return error;
  1888. }
  1889. /* local apic needs to resume before other devices access its registers. */
  1890. core_initcall(init_lapic_sysfs);
  1891. #else /* CONFIG_PM */
  1892. static void apic_pm_activate(void) { }
  1893. #endif /* CONFIG_PM */
  1894. #ifdef CONFIG_X86_64
  1895. static int __cpuinit apic_cluster_num(void)
  1896. {
  1897. int i, clusters, zeros;
  1898. unsigned id;
  1899. u16 *bios_cpu_apicid;
  1900. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1901. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1902. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1903. for (i = 0; i < nr_cpu_ids; i++) {
  1904. /* are we being called early in kernel startup? */
  1905. if (bios_cpu_apicid) {
  1906. id = bios_cpu_apicid[i];
  1907. } else if (i < nr_cpu_ids) {
  1908. if (cpu_present(i))
  1909. id = per_cpu(x86_bios_cpu_apicid, i);
  1910. else
  1911. continue;
  1912. } else
  1913. break;
  1914. if (id != BAD_APICID)
  1915. __set_bit(APIC_CLUSTERID(id), clustermap);
  1916. }
  1917. /* Problem: Partially populated chassis may not have CPUs in some of
  1918. * the APIC clusters they have been allocated. Only present CPUs have
  1919. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1920. * Since clusters are allocated sequentially, count zeros only if
  1921. * they are bounded by ones.
  1922. */
  1923. clusters = 0;
  1924. zeros = 0;
  1925. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1926. if (test_bit(i, clustermap)) {
  1927. clusters += 1 + zeros;
  1928. zeros = 0;
  1929. } else
  1930. ++zeros;
  1931. }
  1932. return clusters;
  1933. }
  1934. static int __cpuinitdata multi_checked;
  1935. static int __cpuinitdata multi;
  1936. static int __cpuinit set_multi(const struct dmi_system_id *d)
  1937. {
  1938. if (multi)
  1939. return 0;
  1940. pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
  1941. multi = 1;
  1942. return 0;
  1943. }
  1944. static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
  1945. {
  1946. .callback = set_multi,
  1947. .ident = "IBM System Summit2",
  1948. .matches = {
  1949. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  1950. DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
  1951. },
  1952. },
  1953. {}
  1954. };
  1955. static void __cpuinit dmi_check_multi(void)
  1956. {
  1957. if (multi_checked)
  1958. return;
  1959. dmi_check_system(multi_dmi_table);
  1960. multi_checked = 1;
  1961. }
  1962. /*
  1963. * apic_is_clustered_box() -- Check if we can expect good TSC
  1964. *
  1965. * Thus far, the major user of this is IBM's Summit2 series:
  1966. * Clustered boxes may have unsynced TSC problems if they are
  1967. * multi-chassis.
  1968. * Use DMI to check them
  1969. */
  1970. __cpuinit int apic_is_clustered_box(void)
  1971. {
  1972. dmi_check_multi();
  1973. if (multi)
  1974. return 1;
  1975. if (!is_vsmp_box())
  1976. return 0;
  1977. /*
  1978. * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1979. * not guaranteed to be synced between boards
  1980. */
  1981. if (apic_cluster_num() > 1)
  1982. return 1;
  1983. return 0;
  1984. }
  1985. #endif
  1986. /*
  1987. * APIC command line parameters
  1988. */
  1989. static int __init setup_disableapic(char *arg)
  1990. {
  1991. disable_apic = 1;
  1992. setup_clear_cpu_cap(X86_FEATURE_APIC);
  1993. return 0;
  1994. }
  1995. early_param("disableapic", setup_disableapic);
  1996. /* same as disableapic, for compatibility */
  1997. static int __init setup_nolapic(char *arg)
  1998. {
  1999. return setup_disableapic(arg);
  2000. }
  2001. early_param("nolapic", setup_nolapic);
  2002. static int __init parse_lapic_timer_c2_ok(char *arg)
  2003. {
  2004. local_apic_timer_c2_ok = 1;
  2005. return 0;
  2006. }
  2007. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  2008. static int __init parse_disable_apic_timer(char *arg)
  2009. {
  2010. disable_apic_timer = 1;
  2011. return 0;
  2012. }
  2013. early_param("noapictimer", parse_disable_apic_timer);
  2014. static int __init parse_nolapic_timer(char *arg)
  2015. {
  2016. disable_apic_timer = 1;
  2017. return 0;
  2018. }
  2019. early_param("nolapic_timer", parse_nolapic_timer);
  2020. static int __init apic_set_verbosity(char *arg)
  2021. {
  2022. if (!arg) {
  2023. #ifdef CONFIG_X86_64
  2024. skip_ioapic_setup = 0;
  2025. return 0;
  2026. #endif
  2027. return -EINVAL;
  2028. }
  2029. if (strcmp("debug", arg) == 0)
  2030. apic_verbosity = APIC_DEBUG;
  2031. else if (strcmp("verbose", arg) == 0)
  2032. apic_verbosity = APIC_VERBOSE;
  2033. else {
  2034. pr_warning("APIC Verbosity level %s not recognised"
  2035. " use apic=verbose or apic=debug\n", arg);
  2036. return -EINVAL;
  2037. }
  2038. return 0;
  2039. }
  2040. early_param("apic", apic_set_verbosity);
  2041. static int __init lapic_insert_resource(void)
  2042. {
  2043. if (!apic_phys)
  2044. return -1;
  2045. /* Put local APIC into the resource map. */
  2046. lapic_resource.start = apic_phys;
  2047. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  2048. insert_resource(&iomem_resource, &lapic_resource);
  2049. return 0;
  2050. }
  2051. /*
  2052. * need call insert after e820_reserve_resources()
  2053. * that is using request_resource
  2054. */
  2055. late_initcall(lapic_insert_resource);