apic.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  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 int oldvalue, value, maxlvt;
  928. if (!lapic_is_integrated()) {
  929. printk(KERN_INFO "No ESR for 82489DX.\n");
  930. return;
  931. }
  932. if (esr_disable) {
  933. /*
  934. * Something untraceable is creating bad interrupts on
  935. * secondary quads ... for the moment, just leave the
  936. * ESR disabled - we can't do anything useful with the
  937. * errors anyway - mbligh
  938. */
  939. printk(KERN_INFO "Leaving ESR disabled.\n");
  940. return;
  941. }
  942. maxlvt = lapic_get_maxlvt();
  943. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  944. apic_write(APIC_ESR, 0);
  945. oldvalue = apic_read(APIC_ESR);
  946. /* enables sending errors */
  947. value = ERROR_APIC_VECTOR;
  948. apic_write(APIC_LVTERR, value);
  949. /*
  950. * spec says clear errors after enabling vector.
  951. */
  952. if (maxlvt > 3)
  953. apic_write(APIC_ESR, 0);
  954. value = apic_read(APIC_ESR);
  955. if (value != oldvalue)
  956. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  957. "vector: 0x%08x after: 0x%08x\n",
  958. oldvalue, value);
  959. }
  960. /**
  961. * setup_local_APIC - setup the local APIC
  962. */
  963. void __cpuinit setup_local_APIC(void)
  964. {
  965. unsigned int value;
  966. int i, j;
  967. #ifdef CONFIG_X86_32
  968. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  969. if (esr_disable) {
  970. apic_write(APIC_ESR, 0);
  971. apic_write(APIC_ESR, 0);
  972. apic_write(APIC_ESR, 0);
  973. apic_write(APIC_ESR, 0);
  974. }
  975. #endif
  976. preempt_disable();
  977. /*
  978. * Double-check whether this APIC is really registered.
  979. * This is meaningless in clustered apic mode, so we skip it.
  980. */
  981. if (!apic_id_registered())
  982. BUG();
  983. /*
  984. * Intel recommends to set DFR, LDR and TPR before enabling
  985. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  986. * document number 292116). So here it goes...
  987. */
  988. init_apic_ldr();
  989. /*
  990. * Set Task Priority to 'accept all'. We never change this
  991. * later on.
  992. */
  993. value = apic_read(APIC_TASKPRI);
  994. value &= ~APIC_TPRI_MASK;
  995. apic_write(APIC_TASKPRI, value);
  996. /*
  997. * After a crash, we no longer service the interrupts and a pending
  998. * interrupt from previous kernel might still have ISR bit set.
  999. *
  1000. * Most probably by now CPU has serviced that pending interrupt and
  1001. * it might not have done the ack_APIC_irq() because it thought,
  1002. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  1003. * does not clear the ISR bit and cpu thinks it has already serivced
  1004. * the interrupt. Hence a vector might get locked. It was noticed
  1005. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  1006. */
  1007. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  1008. value = apic_read(APIC_ISR + i*0x10);
  1009. for (j = 31; j >= 0; j--) {
  1010. if (value & (1<<j))
  1011. ack_APIC_irq();
  1012. }
  1013. }
  1014. /*
  1015. * Now that we are all set up, enable the APIC
  1016. */
  1017. value = apic_read(APIC_SPIV);
  1018. value &= ~APIC_VECTOR_MASK;
  1019. /*
  1020. * Enable APIC
  1021. */
  1022. value |= APIC_SPIV_APIC_ENABLED;
  1023. #ifdef CONFIG_X86_32
  1024. /*
  1025. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  1026. * certain networking cards. If high frequency interrupts are
  1027. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  1028. * entry is masked/unmasked at a high rate as well then sooner or
  1029. * later IOAPIC line gets 'stuck', no more interrupts are received
  1030. * from the device. If focus CPU is disabled then the hang goes
  1031. * away, oh well :-(
  1032. *
  1033. * [ This bug can be reproduced easily with a level-triggered
  1034. * PCI Ne2000 networking cards and PII/PIII processors, dual
  1035. * BX chipset. ]
  1036. */
  1037. /*
  1038. * Actually disabling the focus CPU check just makes the hang less
  1039. * frequent as it makes the interrupt distributon model be more
  1040. * like LRU than MRU (the short-term load is more even across CPUs).
  1041. * See also the comment in end_level_ioapic_irq(). --macro
  1042. */
  1043. /*
  1044. * - enable focus processor (bit==0)
  1045. * - 64bit mode always use processor focus
  1046. * so no need to set it
  1047. */
  1048. value &= ~APIC_SPIV_FOCUS_DISABLED;
  1049. #endif
  1050. /*
  1051. * Set spurious IRQ vector
  1052. */
  1053. value |= SPURIOUS_APIC_VECTOR;
  1054. apic_write(APIC_SPIV, value);
  1055. /*
  1056. * Set up LVT0, LVT1:
  1057. *
  1058. * set up through-local-APIC on the BP's LINT0. This is not
  1059. * strictly necessary in pure symmetric-IO mode, but sometimes
  1060. * we delegate interrupts to the 8259A.
  1061. */
  1062. /*
  1063. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  1064. */
  1065. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  1066. if (!smp_processor_id() && (pic_mode || !value)) {
  1067. value = APIC_DM_EXTINT;
  1068. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  1069. smp_processor_id());
  1070. } else {
  1071. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  1072. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  1073. smp_processor_id());
  1074. }
  1075. apic_write(APIC_LVT0, value);
  1076. /*
  1077. * only the BP should see the LINT1 NMI signal, obviously.
  1078. */
  1079. if (!smp_processor_id())
  1080. value = APIC_DM_NMI;
  1081. else
  1082. value = APIC_DM_NMI | APIC_LVT_MASKED;
  1083. if (!lapic_is_integrated()) /* 82489DX */
  1084. value |= APIC_LVT_LEVEL_TRIGGER;
  1085. apic_write(APIC_LVT1, value);
  1086. preempt_enable();
  1087. }
  1088. void __cpuinit end_local_APIC_setup(void)
  1089. {
  1090. lapic_setup_esr();
  1091. #ifdef CONFIG_X86_32
  1092. {
  1093. unsigned int value;
  1094. /* Disable the local apic timer */
  1095. value = apic_read(APIC_LVTT);
  1096. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  1097. apic_write(APIC_LVTT, value);
  1098. }
  1099. #endif
  1100. setup_apic_nmi_watchdog(NULL);
  1101. apic_pm_activate();
  1102. }
  1103. #ifdef HAVE_X2APIC
  1104. void check_x2apic(void)
  1105. {
  1106. int msr, msr2;
  1107. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1108. if (msr & X2APIC_ENABLE) {
  1109. printk("x2apic enabled by BIOS, switching to x2apic ops\n");
  1110. x2apic_preenabled = x2apic = 1;
  1111. apic_ops = &x2apic_ops;
  1112. }
  1113. }
  1114. void enable_x2apic(void)
  1115. {
  1116. int msr, msr2;
  1117. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1118. if (!(msr & X2APIC_ENABLE)) {
  1119. printk("Enabling x2apic\n");
  1120. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  1121. }
  1122. }
  1123. void enable_IR_x2apic(void)
  1124. {
  1125. #ifdef CONFIG_INTR_REMAP
  1126. int ret;
  1127. unsigned long flags;
  1128. if (!cpu_has_x2apic)
  1129. return;
  1130. if (!x2apic_preenabled && disable_x2apic) {
  1131. printk(KERN_INFO
  1132. "Skipped enabling x2apic and Interrupt-remapping "
  1133. "because of nox2apic\n");
  1134. return;
  1135. }
  1136. if (x2apic_preenabled && disable_x2apic)
  1137. panic("Bios already enabled x2apic, can't enforce nox2apic");
  1138. if (!x2apic_preenabled && skip_ioapic_setup) {
  1139. printk(KERN_INFO
  1140. "Skipped enabling x2apic and Interrupt-remapping "
  1141. "because of skipping io-apic setup\n");
  1142. return;
  1143. }
  1144. ret = dmar_table_init();
  1145. if (ret) {
  1146. printk(KERN_INFO
  1147. "dmar_table_init() failed with %d:\n", ret);
  1148. if (x2apic_preenabled)
  1149. panic("x2apic enabled by bios. But IR enabling failed");
  1150. else
  1151. printk(KERN_INFO
  1152. "Not enabling x2apic,Intr-remapping\n");
  1153. return;
  1154. }
  1155. local_irq_save(flags);
  1156. mask_8259A();
  1157. save_mask_IO_APIC_setup();
  1158. ret = enable_intr_remapping(1);
  1159. if (ret && x2apic_preenabled) {
  1160. local_irq_restore(flags);
  1161. panic("x2apic enabled by bios. But IR enabling failed");
  1162. }
  1163. if (ret)
  1164. goto end;
  1165. if (!x2apic) {
  1166. x2apic = 1;
  1167. apic_ops = &x2apic_ops;
  1168. enable_x2apic();
  1169. }
  1170. end:
  1171. if (ret)
  1172. /*
  1173. * IR enabling failed
  1174. */
  1175. restore_IO_APIC_setup();
  1176. else
  1177. reinit_intr_remapped_IO_APIC(x2apic_preenabled);
  1178. unmask_8259A();
  1179. local_irq_restore(flags);
  1180. if (!ret) {
  1181. if (!x2apic_preenabled)
  1182. printk(KERN_INFO
  1183. "Enabled x2apic and interrupt-remapping\n");
  1184. else
  1185. printk(KERN_INFO
  1186. "Enabled Interrupt-remapping\n");
  1187. } else
  1188. printk(KERN_ERR
  1189. "Failed to enable Interrupt-remapping and x2apic\n");
  1190. #else
  1191. if (!cpu_has_x2apic)
  1192. return;
  1193. if (x2apic_preenabled)
  1194. panic("x2apic enabled prior OS handover,"
  1195. " enable CONFIG_INTR_REMAP");
  1196. printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
  1197. " and x2apic\n");
  1198. #endif
  1199. return;
  1200. }
  1201. #endif /* HAVE_X2APIC */
  1202. #ifdef CONFIG_X86_64
  1203. /*
  1204. * Detect and enable local APICs on non-SMP boards.
  1205. * Original code written by Keir Fraser.
  1206. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  1207. * not correctly set up (usually the APIC timer won't work etc.)
  1208. */
  1209. static int __init detect_init_APIC(void)
  1210. {
  1211. if (!cpu_has_apic) {
  1212. printk(KERN_INFO "No local APIC present\n");
  1213. return -1;
  1214. }
  1215. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1216. boot_cpu_physical_apicid = 0;
  1217. return 0;
  1218. }
  1219. #else
  1220. /*
  1221. * Detect and initialize APIC
  1222. */
  1223. static int __init detect_init_APIC(void)
  1224. {
  1225. u32 h, l, features;
  1226. /* Disabled by kernel option? */
  1227. if (disable_apic)
  1228. return -1;
  1229. switch (boot_cpu_data.x86_vendor) {
  1230. case X86_VENDOR_AMD:
  1231. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  1232. (boot_cpu_data.x86 == 15))
  1233. break;
  1234. goto no_apic;
  1235. case X86_VENDOR_INTEL:
  1236. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  1237. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  1238. break;
  1239. goto no_apic;
  1240. default:
  1241. goto no_apic;
  1242. }
  1243. if (!cpu_has_apic) {
  1244. /*
  1245. * Over-ride BIOS and try to enable the local APIC only if
  1246. * "lapic" specified.
  1247. */
  1248. if (!force_enable_local_apic) {
  1249. printk(KERN_INFO "Local APIC disabled by BIOS -- "
  1250. "you can enable it with \"lapic\"\n");
  1251. return -1;
  1252. }
  1253. /*
  1254. * Some BIOSes disable the local APIC in the APIC_BASE
  1255. * MSR. This can only be done in software for Intel P6 or later
  1256. * and AMD K7 (Model > 1) or later.
  1257. */
  1258. rdmsr(MSR_IA32_APICBASE, l, h);
  1259. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  1260. printk(KERN_INFO
  1261. "Local APIC disabled by BIOS -- reenabling.\n");
  1262. l &= ~MSR_IA32_APICBASE_BASE;
  1263. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  1264. wrmsr(MSR_IA32_APICBASE, l, h);
  1265. enabled_via_apicbase = 1;
  1266. }
  1267. }
  1268. /*
  1269. * The APIC feature bit should now be enabled
  1270. * in `cpuid'
  1271. */
  1272. features = cpuid_edx(1);
  1273. if (!(features & (1 << X86_FEATURE_APIC))) {
  1274. printk(KERN_WARNING "Could not enable APIC!\n");
  1275. return -1;
  1276. }
  1277. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1278. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1279. /* The BIOS may have set up the APIC at some other address */
  1280. rdmsr(MSR_IA32_APICBASE, l, h);
  1281. if (l & MSR_IA32_APICBASE_ENABLE)
  1282. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1283. printk(KERN_INFO "Found and enabled local APIC!\n");
  1284. apic_pm_activate();
  1285. return 0;
  1286. no_apic:
  1287. printk(KERN_INFO "No local APIC present or hardware disabled\n");
  1288. return -1;
  1289. }
  1290. #endif
  1291. #ifdef CONFIG_X86_64
  1292. void __init early_init_lapic_mapping(void)
  1293. {
  1294. unsigned long phys_addr;
  1295. /*
  1296. * If no local APIC can be found then go out
  1297. * : it means there is no mpatable and MADT
  1298. */
  1299. if (!smp_found_config)
  1300. return;
  1301. phys_addr = mp_lapic_addr;
  1302. set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
  1303. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1304. APIC_BASE, phys_addr);
  1305. /*
  1306. * Fetch the APIC ID of the BSP in case we have a
  1307. * default configuration (or the MP table is broken).
  1308. */
  1309. boot_cpu_physical_apicid = read_apic_id();
  1310. }
  1311. #endif
  1312. /**
  1313. * init_apic_mappings - initialize APIC mappings
  1314. */
  1315. void __init init_apic_mappings(void)
  1316. {
  1317. #ifdef HAVE_X2APIC
  1318. if (x2apic) {
  1319. boot_cpu_physical_apicid = read_apic_id();
  1320. return;
  1321. }
  1322. #endif
  1323. /*
  1324. * If no local APIC can be found then set up a fake all
  1325. * zeroes page to simulate the local APIC and another
  1326. * one for the IO-APIC.
  1327. */
  1328. if (!smp_found_config && detect_init_APIC()) {
  1329. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  1330. apic_phys = __pa(apic_phys);
  1331. } else
  1332. apic_phys = mp_lapic_addr;
  1333. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  1334. apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
  1335. APIC_BASE, apic_phys);
  1336. /*
  1337. * Fetch the APIC ID of the BSP in case we have a
  1338. * default configuration (or the MP table is broken).
  1339. */
  1340. if (boot_cpu_physical_apicid == -1U)
  1341. boot_cpu_physical_apicid = read_apic_id();
  1342. }
  1343. /*
  1344. * This initializes the IO-APIC and APIC hardware if this is
  1345. * a UP kernel.
  1346. */
  1347. int apic_version[MAX_APICS];
  1348. int __init APIC_init_uniprocessor(void)
  1349. {
  1350. #ifdef CONFIG_X86_64
  1351. if (disable_apic) {
  1352. printk(KERN_INFO "Apic disabled\n");
  1353. return -1;
  1354. }
  1355. if (!cpu_has_apic) {
  1356. disable_apic = 1;
  1357. printk(KERN_INFO "Apic disabled by BIOS\n");
  1358. return -1;
  1359. }
  1360. #else
  1361. if (!smp_found_config && !cpu_has_apic)
  1362. return -1;
  1363. /*
  1364. * Complain if the BIOS pretends there is one.
  1365. */
  1366. if (!cpu_has_apic &&
  1367. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1368. printk(KERN_ERR "BIOS bug, local APIC 0x%x not detected!...\n",
  1369. boot_cpu_physical_apicid);
  1370. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1371. return -1;
  1372. }
  1373. #endif
  1374. #ifdef HAVE_X2APIC
  1375. enable_IR_x2apic();
  1376. #endif
  1377. #ifdef CONFIG_X86_64
  1378. setup_apic_routing();
  1379. #endif
  1380. verify_local_APIC();
  1381. connect_bsp_APIC();
  1382. #ifdef CONFIG_X86_64
  1383. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  1384. #else
  1385. /*
  1386. * Hack: In case of kdump, after a crash, kernel might be booting
  1387. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1388. * might be zero if read from MP tables. Get it from LAPIC.
  1389. */
  1390. # ifdef CONFIG_CRASH_DUMP
  1391. boot_cpu_physical_apicid = read_apic_id();
  1392. # endif
  1393. #endif
  1394. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  1395. setup_local_APIC();
  1396. #ifdef CONFIG_X86_64
  1397. /*
  1398. * Now enable IO-APICs, actually call clear_IO_APIC
  1399. * We need clear_IO_APIC before enabling vector on BP
  1400. */
  1401. if (!skip_ioapic_setup && nr_ioapics)
  1402. enable_IO_APIC();
  1403. #endif
  1404. #ifdef CONFIG_X86_IO_APIC
  1405. if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
  1406. #endif
  1407. localise_nmi_watchdog();
  1408. end_local_APIC_setup();
  1409. #ifdef CONFIG_X86_IO_APIC
  1410. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1411. setup_IO_APIC();
  1412. # ifdef CONFIG_X86_64
  1413. else
  1414. nr_ioapics = 0;
  1415. # endif
  1416. #endif
  1417. #ifdef CONFIG_X86_64
  1418. setup_boot_APIC_clock();
  1419. check_nmi_watchdog();
  1420. #else
  1421. setup_boot_clock();
  1422. #endif
  1423. return 0;
  1424. }
  1425. /*
  1426. * Local APIC interrupts
  1427. */
  1428. /*
  1429. * This interrupt should _never_ happen with our APIC/SMP architecture
  1430. */
  1431. void smp_spurious_interrupt(struct pt_regs *regs)
  1432. {
  1433. u32 v;
  1434. #ifdef CONFIG_X86_64
  1435. exit_idle();
  1436. #endif
  1437. irq_enter();
  1438. /*
  1439. * Check if this really is a spurious interrupt and ACK it
  1440. * if it is a vectored one. Just in case...
  1441. * Spurious interrupts should not be ACKed.
  1442. */
  1443. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1444. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1445. ack_APIC_irq();
  1446. #ifdef CONFIG_X86_64
  1447. add_pda(irq_spurious_count, 1);
  1448. #else
  1449. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1450. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
  1451. "should never happen.\n", smp_processor_id());
  1452. __get_cpu_var(irq_stat).irq_spurious_count++;
  1453. #endif
  1454. irq_exit();
  1455. }
  1456. /*
  1457. * This interrupt should never happen with our APIC/SMP architecture
  1458. */
  1459. void smp_error_interrupt(struct pt_regs *regs)
  1460. {
  1461. u32 v, v1;
  1462. #ifdef CONFIG_X86_64
  1463. exit_idle();
  1464. #endif
  1465. irq_enter();
  1466. /* First tickle the hardware, only then report what went on. -- REW */
  1467. v = apic_read(APIC_ESR);
  1468. apic_write(APIC_ESR, 0);
  1469. v1 = apic_read(APIC_ESR);
  1470. ack_APIC_irq();
  1471. atomic_inc(&irq_err_count);
  1472. /* Here is what the APIC error bits mean:
  1473. 0: Send CS error
  1474. 1: Receive CS error
  1475. 2: Send accept error
  1476. 3: Receive accept error
  1477. 4: Reserved
  1478. 5: Send illegal vector
  1479. 6: Received illegal vector
  1480. 7: Illegal register address
  1481. */
  1482. printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  1483. smp_processor_id(), v , v1);
  1484. irq_exit();
  1485. }
  1486. /**
  1487. * connect_bsp_APIC - attach the APIC to the interrupt system
  1488. */
  1489. void __init connect_bsp_APIC(void)
  1490. {
  1491. #ifdef CONFIG_X86_32
  1492. if (pic_mode) {
  1493. /*
  1494. * Do not trust the local APIC being empty at bootup.
  1495. */
  1496. clear_local_APIC();
  1497. /*
  1498. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1499. * local APIC to INT and NMI lines.
  1500. */
  1501. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1502. "enabling APIC mode.\n");
  1503. outb(0x70, 0x22);
  1504. outb(0x01, 0x23);
  1505. }
  1506. #endif
  1507. enable_apic_mode();
  1508. }
  1509. /**
  1510. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1511. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1512. *
  1513. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1514. * APIC is disabled.
  1515. */
  1516. void disconnect_bsp_APIC(int virt_wire_setup)
  1517. {
  1518. unsigned int value;
  1519. #ifdef CONFIG_X86_32
  1520. if (pic_mode) {
  1521. /*
  1522. * Put the board back into PIC mode (has an effect only on
  1523. * certain older boards). Note that APIC interrupts, including
  1524. * IPIs, won't work beyond this point! The only exception are
  1525. * INIT IPIs.
  1526. */
  1527. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1528. "entering PIC mode.\n");
  1529. outb(0x70, 0x22);
  1530. outb(0x00, 0x23);
  1531. return;
  1532. }
  1533. #endif
  1534. /* Go back to Virtual Wire compatibility mode */
  1535. /* For the spurious interrupt use vector F, and enable it */
  1536. value = apic_read(APIC_SPIV);
  1537. value &= ~APIC_VECTOR_MASK;
  1538. value |= APIC_SPIV_APIC_ENABLED;
  1539. value |= 0xf;
  1540. apic_write(APIC_SPIV, value);
  1541. if (!virt_wire_setup) {
  1542. /*
  1543. * For LVT0 make it edge triggered, active high,
  1544. * external and enabled
  1545. */
  1546. value = apic_read(APIC_LVT0);
  1547. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1548. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1549. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1550. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1551. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1552. apic_write(APIC_LVT0, value);
  1553. } else {
  1554. /* Disable LVT0 */
  1555. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  1556. }
  1557. /*
  1558. * For LVT1 make it edge triggered, active high,
  1559. * nmi and enabled
  1560. */
  1561. value = apic_read(APIC_LVT1);
  1562. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1563. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1564. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1565. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1566. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1567. apic_write(APIC_LVT1, value);
  1568. }
  1569. void __cpuinit generic_processor_info(int apicid, int version)
  1570. {
  1571. int cpu;
  1572. cpumask_t tmp_map;
  1573. /*
  1574. * Validate version
  1575. */
  1576. if (version == 0x0) {
  1577. printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
  1578. "fixing up to 0x10. (tell your hw vendor)\n",
  1579. version);
  1580. version = 0x10;
  1581. }
  1582. apic_version[apicid] = version;
  1583. if (num_processors >= NR_CPUS) {
  1584. printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
  1585. " Processor ignored.\n", NR_CPUS);
  1586. return;
  1587. }
  1588. num_processors++;
  1589. cpus_complement(tmp_map, cpu_present_map);
  1590. cpu = first_cpu(tmp_map);
  1591. physid_set(apicid, phys_cpu_present_map);
  1592. if (apicid == boot_cpu_physical_apicid) {
  1593. /*
  1594. * x86_bios_cpu_apicid is required to have processors listed
  1595. * in same order as logical cpu numbers. Hence the first
  1596. * entry is BSP, and so on.
  1597. */
  1598. cpu = 0;
  1599. }
  1600. if (apicid > max_physical_apicid)
  1601. max_physical_apicid = apicid;
  1602. #ifdef CONFIG_X86_32
  1603. /*
  1604. * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
  1605. * but we need to work other dependencies like SMP_SUSPEND etc
  1606. * before this can be done without some confusion.
  1607. * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
  1608. * - Ashok Raj <ashok.raj@intel.com>
  1609. */
  1610. if (max_physical_apicid >= 8) {
  1611. switch (boot_cpu_data.x86_vendor) {
  1612. case X86_VENDOR_INTEL:
  1613. if (!APIC_XAPIC(version)) {
  1614. def_to_bigsmp = 0;
  1615. break;
  1616. }
  1617. /* If P4 and above fall through */
  1618. case X86_VENDOR_AMD:
  1619. def_to_bigsmp = 1;
  1620. }
  1621. }
  1622. #endif
  1623. #if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
  1624. /* are we being called early in kernel startup? */
  1625. if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
  1626. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  1627. u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1628. cpu_to_apicid[cpu] = apicid;
  1629. bios_cpu_apicid[cpu] = apicid;
  1630. } else {
  1631. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  1632. per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  1633. }
  1634. #endif
  1635. cpu_set(cpu, cpu_possible_map);
  1636. cpu_set(cpu, cpu_present_map);
  1637. }
  1638. #ifdef CONFIG_X86_64
  1639. int hard_smp_processor_id(void)
  1640. {
  1641. return read_apic_id();
  1642. }
  1643. #endif
  1644. /*
  1645. * Power management
  1646. */
  1647. #ifdef CONFIG_PM
  1648. static struct {
  1649. /*
  1650. * 'active' is true if the local APIC was enabled by us and
  1651. * not the BIOS; this signifies that we are also responsible
  1652. * for disabling it before entering apm/acpi suspend
  1653. */
  1654. int active;
  1655. /* r/w apic fields */
  1656. unsigned int apic_id;
  1657. unsigned int apic_taskpri;
  1658. unsigned int apic_ldr;
  1659. unsigned int apic_dfr;
  1660. unsigned int apic_spiv;
  1661. unsigned int apic_lvtt;
  1662. unsigned int apic_lvtpc;
  1663. unsigned int apic_lvt0;
  1664. unsigned int apic_lvt1;
  1665. unsigned int apic_lvterr;
  1666. unsigned int apic_tmict;
  1667. unsigned int apic_tdcr;
  1668. unsigned int apic_thmr;
  1669. } apic_pm_state;
  1670. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1671. {
  1672. unsigned long flags;
  1673. int maxlvt;
  1674. if (!apic_pm_state.active)
  1675. return 0;
  1676. maxlvt = lapic_get_maxlvt();
  1677. apic_pm_state.apic_id = apic_read(APIC_ID);
  1678. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1679. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1680. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1681. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1682. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1683. if (maxlvt >= 4)
  1684. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1685. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1686. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1687. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1688. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1689. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1690. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1691. if (maxlvt >= 5)
  1692. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1693. #endif
  1694. local_irq_save(flags);
  1695. disable_local_APIC();
  1696. local_irq_restore(flags);
  1697. return 0;
  1698. }
  1699. static int lapic_resume(struct sys_device *dev)
  1700. {
  1701. unsigned int l, h;
  1702. unsigned long flags;
  1703. int maxlvt;
  1704. if (!apic_pm_state.active)
  1705. return 0;
  1706. maxlvt = lapic_get_maxlvt();
  1707. local_irq_save(flags);
  1708. #ifdef HAVE_X2APIC
  1709. if (x2apic)
  1710. enable_x2apic();
  1711. else
  1712. #endif
  1713. {
  1714. /*
  1715. * Make sure the APICBASE points to the right address
  1716. *
  1717. * FIXME! This will be wrong if we ever support suspend on
  1718. * SMP! We'll need to do this as part of the CPU restore!
  1719. */
  1720. rdmsr(MSR_IA32_APICBASE, l, h);
  1721. l &= ~MSR_IA32_APICBASE_BASE;
  1722. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1723. wrmsr(MSR_IA32_APICBASE, l, h);
  1724. }
  1725. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1726. apic_write(APIC_ID, apic_pm_state.apic_id);
  1727. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1728. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1729. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1730. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1731. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1732. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1733. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1734. if (maxlvt >= 5)
  1735. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1736. #endif
  1737. if (maxlvt >= 4)
  1738. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1739. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1740. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1741. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1742. apic_write(APIC_ESR, 0);
  1743. apic_read(APIC_ESR);
  1744. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1745. apic_write(APIC_ESR, 0);
  1746. apic_read(APIC_ESR);
  1747. local_irq_restore(flags);
  1748. return 0;
  1749. }
  1750. /*
  1751. * This device has no shutdown method - fully functioning local APICs
  1752. * are needed on every CPU up until machine_halt/restart/poweroff.
  1753. */
  1754. static struct sysdev_class lapic_sysclass = {
  1755. .name = "lapic",
  1756. .resume = lapic_resume,
  1757. .suspend = lapic_suspend,
  1758. };
  1759. static struct sys_device device_lapic = {
  1760. .id = 0,
  1761. .cls = &lapic_sysclass,
  1762. };
  1763. static void __cpuinit apic_pm_activate(void)
  1764. {
  1765. apic_pm_state.active = 1;
  1766. }
  1767. static int __init init_lapic_sysfs(void)
  1768. {
  1769. int error;
  1770. if (!cpu_has_apic)
  1771. return 0;
  1772. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1773. error = sysdev_class_register(&lapic_sysclass);
  1774. if (!error)
  1775. error = sysdev_register(&device_lapic);
  1776. return error;
  1777. }
  1778. device_initcall(init_lapic_sysfs);
  1779. #else /* CONFIG_PM */
  1780. static void apic_pm_activate(void) { }
  1781. #endif /* CONFIG_PM */
  1782. #ifdef CONFIG_X86_64
  1783. /*
  1784. * apic_is_clustered_box() -- Check if we can expect good TSC
  1785. *
  1786. * Thus far, the major user of this is IBM's Summit2 series:
  1787. *
  1788. * Clustered boxes may have unsynced TSC problems if they are
  1789. * multi-chassis. Use available data to take a good guess.
  1790. * If in doubt, go HPET.
  1791. */
  1792. __cpuinit int apic_is_clustered_box(void)
  1793. {
  1794. int i, clusters, zeros;
  1795. unsigned id;
  1796. u16 *bios_cpu_apicid;
  1797. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1798. /*
  1799. * there is not this kind of box with AMD CPU yet.
  1800. * Some AMD box with quadcore cpu and 8 sockets apicid
  1801. * will be [4, 0x23] or [8, 0x27] could be thought to
  1802. * vsmp box still need checking...
  1803. */
  1804. if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
  1805. return 0;
  1806. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1807. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1808. for (i = 0; i < NR_CPUS; i++) {
  1809. /* are we being called early in kernel startup? */
  1810. if (bios_cpu_apicid) {
  1811. id = bios_cpu_apicid[i];
  1812. }
  1813. else if (i < nr_cpu_ids) {
  1814. if (cpu_present(i))
  1815. id = per_cpu(x86_bios_cpu_apicid, i);
  1816. else
  1817. continue;
  1818. }
  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. printk(KERN_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);