apic.c 52 KB

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