apic.c 52 KB

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