apic.c 52 KB

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