apic.c 59 KB

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