apic_64.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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 function we calibrate APIC bus clocks to the external
  411. * timer. Unfortunately we cannot use jiffies and the timer irq
  412. * to calibrate, since some later bootup code depends on getting
  413. * the first irq? Ugh.
  414. *
  415. * We want to do the calibration only once since we
  416. * want to have local timer irqs syncron. CPUs connected
  417. * by the same APIC bus have the very same bus frequency.
  418. * And we want to have irqs off anyways, no accidental
  419. * APIC irq that way.
  420. */
  421. #define TICK_COUNT 100000000
  422. static int __init calibrate_APIC_clock(void)
  423. {
  424. unsigned apic, apic_start;
  425. unsigned long tsc, tsc_start;
  426. int result;
  427. local_irq_disable();
  428. /*
  429. * Put whatever arbitrary (but long enough) timeout
  430. * value into the APIC clock, we just want to get the
  431. * counter running for calibration.
  432. *
  433. * No interrupt enable !
  434. */
  435. __setup_APIC_LVTT(250000000, 0, 0);
  436. apic_start = apic_read(APIC_TMCCT);
  437. #ifdef CONFIG_X86_PM_TIMER
  438. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  439. pmtimer_wait(5000); /* 5ms wait */
  440. apic = apic_read(APIC_TMCCT);
  441. result = (apic_start - apic) * 1000L / 5;
  442. } else
  443. #endif
  444. {
  445. rdtscll(tsc_start);
  446. do {
  447. apic = apic_read(APIC_TMCCT);
  448. rdtscll(tsc);
  449. } while ((tsc - tsc_start) < TICK_COUNT &&
  450. (apic_start - apic) < TICK_COUNT);
  451. result = (apic_start - apic) * 1000L * tsc_khz /
  452. (tsc - tsc_start);
  453. }
  454. local_irq_enable();
  455. printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
  456. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  457. result / 1000 / 1000, result / 1000 % 1000);
  458. /* Calculate the scaled math multiplication factor */
  459. lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
  460. lapic_clockevent.shift);
  461. lapic_clockevent.max_delta_ns =
  462. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  463. lapic_clockevent.min_delta_ns =
  464. clockevent_delta2ns(0xF, &lapic_clockevent);
  465. calibration_result = (result * APIC_DIVISOR) / HZ;
  466. /*
  467. * Do a sanity check on the APIC calibration result
  468. */
  469. if (calibration_result < (1000000 / HZ)) {
  470. printk(KERN_WARNING
  471. "APIC frequency too slow, disabling apic timer\n");
  472. return -1;
  473. }
  474. return 0;
  475. }
  476. /*
  477. * Setup the boot APIC
  478. *
  479. * Calibrate and verify the result.
  480. */
  481. void __init setup_boot_APIC_clock(void)
  482. {
  483. /*
  484. * The local apic timer can be disabled via the kernel
  485. * commandline or from the CPU detection code. Register the lapic
  486. * timer as a dummy clock event source on SMP systems, so the
  487. * broadcast mechanism is used. On UP systems simply ignore it.
  488. */
  489. if (disable_apic_timer) {
  490. printk(KERN_INFO "Disabling APIC timer\n");
  491. /* No broadcast on UP ! */
  492. if (num_possible_cpus() > 1) {
  493. lapic_clockevent.mult = 1;
  494. setup_APIC_timer();
  495. }
  496. return;
  497. }
  498. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
  499. "calibrating APIC timer ...\n");
  500. if (calibrate_APIC_clock()) {
  501. /* No broadcast on UP ! */
  502. if (num_possible_cpus() > 1)
  503. setup_APIC_timer();
  504. return;
  505. }
  506. /*
  507. * If nmi_watchdog is set to IO_APIC, we need the
  508. * PIT/HPET going. Otherwise register lapic as a dummy
  509. * device.
  510. */
  511. if (nmi_watchdog != NMI_IO_APIC)
  512. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  513. else
  514. printk(KERN_WARNING "APIC timer registered as dummy,"
  515. " due to nmi_watchdog=%d!\n", nmi_watchdog);
  516. /* Setup the lapic or request the broadcast */
  517. setup_APIC_timer();
  518. }
  519. void __cpuinit setup_secondary_APIC_clock(void)
  520. {
  521. setup_APIC_timer();
  522. }
  523. /*
  524. * The guts of the apic timer interrupt
  525. */
  526. static void local_apic_timer_interrupt(void)
  527. {
  528. int cpu = smp_processor_id();
  529. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  530. /*
  531. * Normally we should not be here till LAPIC has been initialized but
  532. * in some cases like kdump, its possible that there is a pending LAPIC
  533. * timer interrupt from previous kernel's context and is delivered in
  534. * new kernel the moment interrupts are enabled.
  535. *
  536. * Interrupts are enabled early and LAPIC is setup much later, hence
  537. * its possible that when we get here evt->event_handler is NULL.
  538. * Check for event_handler being NULL and discard the interrupt as
  539. * spurious.
  540. */
  541. if (!evt->event_handler) {
  542. printk(KERN_WARNING
  543. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  544. /* Switch it off */
  545. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  546. return;
  547. }
  548. /*
  549. * the NMI deadlock-detector uses this.
  550. */
  551. #ifdef CONFIG_X86_64
  552. add_pda(apic_timer_irqs, 1);
  553. #else
  554. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  555. #endif
  556. evt->event_handler(evt);
  557. }
  558. /*
  559. * Local APIC timer interrupt. This is the most natural way for doing
  560. * local interrupts, but local timer interrupts can be emulated by
  561. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  562. *
  563. * [ if a single-CPU system runs an SMP kernel then we call the local
  564. * interrupt as well. Thus we cannot inline the local irq ... ]
  565. */
  566. void smp_apic_timer_interrupt(struct pt_regs *regs)
  567. {
  568. struct pt_regs *old_regs = set_irq_regs(regs);
  569. /*
  570. * NOTE! We'd better ACK the irq immediately,
  571. * because timer handling can be slow.
  572. */
  573. ack_APIC_irq();
  574. /*
  575. * update_process_times() expects us to have done irq_enter().
  576. * Besides, if we don't timer interrupts ignore the global
  577. * interrupt lock, which is the WrongThing (tm) to do.
  578. */
  579. #ifdef CONFIG_X86_64
  580. exit_idle();
  581. #endif
  582. irq_enter();
  583. local_apic_timer_interrupt();
  584. irq_exit();
  585. set_irq_regs(old_regs);
  586. }
  587. int setup_profiling_timer(unsigned int multiplier)
  588. {
  589. return -EINVAL;
  590. }
  591. /*
  592. * Local APIC start and shutdown
  593. */
  594. /**
  595. * clear_local_APIC - shutdown the local APIC
  596. *
  597. * This is called, when a CPU is disabled and before rebooting, so the state of
  598. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  599. * leftovers during boot.
  600. */
  601. void clear_local_APIC(void)
  602. {
  603. int maxlvt;
  604. u32 v;
  605. /* APIC hasn't been mapped yet */
  606. if (!apic_phys)
  607. return;
  608. maxlvt = lapic_get_maxlvt();
  609. /*
  610. * Masking an LVT entry can trigger a local APIC error
  611. * if the vector is zero. Mask LVTERR first to prevent this.
  612. */
  613. if (maxlvt >= 3) {
  614. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  615. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  616. }
  617. /*
  618. * Careful: we have to set masks only first to deassert
  619. * any level-triggered sources.
  620. */
  621. v = apic_read(APIC_LVTT);
  622. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  623. v = apic_read(APIC_LVT0);
  624. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  625. v = apic_read(APIC_LVT1);
  626. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  627. if (maxlvt >= 4) {
  628. v = apic_read(APIC_LVTPC);
  629. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  630. }
  631. /* lets not touch this if we didn't frob it */
  632. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
  633. if (maxlvt >= 5) {
  634. v = apic_read(APIC_LVTTHMR);
  635. apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  636. }
  637. #endif
  638. /*
  639. * Clean APIC state for other OSs:
  640. */
  641. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  642. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  643. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  644. if (maxlvt >= 3)
  645. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  646. if (maxlvt >= 4)
  647. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  648. /* Integrated APIC (!82489DX) ? */
  649. if (lapic_is_integrated()) {
  650. if (maxlvt > 3)
  651. /* Clear ESR due to Pentium errata 3AP and 11AP */
  652. apic_write(APIC_ESR, 0);
  653. apic_read(APIC_ESR);
  654. }
  655. }
  656. /**
  657. * disable_local_APIC - clear and disable the local APIC
  658. */
  659. void disable_local_APIC(void)
  660. {
  661. unsigned int value;
  662. clear_local_APIC();
  663. /*
  664. * Disable APIC (implies clearing of registers
  665. * for 82489DX!).
  666. */
  667. value = apic_read(APIC_SPIV);
  668. value &= ~APIC_SPIV_APIC_ENABLED;
  669. apic_write(APIC_SPIV, value);
  670. #ifdef CONFIG_X86_32
  671. /*
  672. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  673. * restore the disabled state.
  674. */
  675. if (enabled_via_apicbase) {
  676. unsigned int l, h;
  677. rdmsr(MSR_IA32_APICBASE, l, h);
  678. l &= ~MSR_IA32_APICBASE_ENABLE;
  679. wrmsr(MSR_IA32_APICBASE, l, h);
  680. }
  681. #endif
  682. }
  683. /*
  684. * If Linux enabled the LAPIC against the BIOS default disable it down before
  685. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  686. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  687. * for the case where Linux didn't enable the LAPIC.
  688. */
  689. void lapic_shutdown(void)
  690. {
  691. unsigned long flags;
  692. if (!cpu_has_apic)
  693. return;
  694. local_irq_save(flags);
  695. #ifdef CONFIG_X86_32
  696. if (!enabled_via_apicbase)
  697. clear_local_APIC();
  698. else
  699. #endif
  700. disable_local_APIC();
  701. local_irq_restore(flags);
  702. }
  703. /*
  704. * This is to verify that we're looking at a real local APIC.
  705. * Check these against your board if the CPUs aren't getting
  706. * started for no apparent reason.
  707. */
  708. int __init verify_local_APIC(void)
  709. {
  710. unsigned int reg0, reg1;
  711. /*
  712. * The version register is read-only in a real APIC.
  713. */
  714. reg0 = apic_read(APIC_LVR);
  715. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  716. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  717. reg1 = apic_read(APIC_LVR);
  718. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  719. /*
  720. * The two version reads above should print the same
  721. * numbers. If the second one is different, then we
  722. * poke at a non-APIC.
  723. */
  724. if (reg1 != reg0)
  725. return 0;
  726. /*
  727. * Check if the version looks reasonably.
  728. */
  729. reg1 = GET_APIC_VERSION(reg0);
  730. if (reg1 == 0x00 || reg1 == 0xff)
  731. return 0;
  732. reg1 = lapic_get_maxlvt();
  733. if (reg1 < 0x02 || reg1 == 0xff)
  734. return 0;
  735. /*
  736. * The ID register is read/write in a real APIC.
  737. */
  738. reg0 = apic_read(APIC_ID);
  739. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  740. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  741. reg1 = apic_read(APIC_ID);
  742. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  743. apic_write(APIC_ID, reg0);
  744. if (reg1 != (reg0 ^ APIC_ID_MASK))
  745. return 0;
  746. /*
  747. * The next two are just to see if we have sane values.
  748. * They're only really relevant if we're in Virtual Wire
  749. * compatibility mode, but most boxes are anymore.
  750. */
  751. reg0 = apic_read(APIC_LVT0);
  752. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  753. reg1 = apic_read(APIC_LVT1);
  754. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  755. return 1;
  756. }
  757. /**
  758. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  759. */
  760. void __init sync_Arb_IDs(void)
  761. {
  762. /*
  763. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  764. * needed on AMD.
  765. */
  766. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  767. return;
  768. /*
  769. * Wait for idle.
  770. */
  771. apic_wait_icr_idle();
  772. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  773. apic_write(APIC_ICR, APIC_DEST_ALLINC |
  774. APIC_INT_LEVELTRIG | APIC_DM_INIT);
  775. }
  776. /*
  777. * An initial setup of the virtual wire mode.
  778. */
  779. void __init init_bsp_APIC(void)
  780. {
  781. unsigned int value;
  782. /*
  783. * Don't do the setup now if we have a SMP BIOS as the
  784. * through-I/O-APIC virtual wire mode might be active.
  785. */
  786. if (smp_found_config || !cpu_has_apic)
  787. return;
  788. /*
  789. * Do not trust the local APIC being empty at bootup.
  790. */
  791. clear_local_APIC();
  792. /*
  793. * Enable APIC.
  794. */
  795. value = apic_read(APIC_SPIV);
  796. value &= ~APIC_VECTOR_MASK;
  797. value |= APIC_SPIV_APIC_ENABLED;
  798. #ifdef CONFIG_X86_32
  799. /* This bit is reserved on P4/Xeon and should be cleared */
  800. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  801. (boot_cpu_data.x86 == 15))
  802. value &= ~APIC_SPIV_FOCUS_DISABLED;
  803. else
  804. #endif
  805. value |= APIC_SPIV_FOCUS_DISABLED;
  806. value |= SPURIOUS_APIC_VECTOR;
  807. apic_write(APIC_SPIV, value);
  808. /*
  809. * Set up the virtual wire mode.
  810. */
  811. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  812. value = APIC_DM_NMI;
  813. if (!lapic_is_integrated()) /* 82489DX */
  814. value |= APIC_LVT_LEVEL_TRIGGER;
  815. apic_write(APIC_LVT1, value);
  816. }
  817. static void __cpuinit lapic_setup_esr(void)
  818. {
  819. unsigned long oldvalue, value, maxlvt;
  820. if (lapic_is_integrated() && !esr_disable) {
  821. if (esr_disable) {
  822. /*
  823. * Something untraceable is creating bad interrupts on
  824. * secondary quads ... for the moment, just leave the
  825. * ESR disabled - we can't do anything useful with the
  826. * errors anyway - mbligh
  827. */
  828. printk(KERN_INFO "Leaving ESR disabled.\n");
  829. return;
  830. }
  831. /* !82489DX */
  832. maxlvt = lapic_get_maxlvt();
  833. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  834. apic_write(APIC_ESR, 0);
  835. oldvalue = apic_read(APIC_ESR);
  836. /* enables sending errors */
  837. value = ERROR_APIC_VECTOR;
  838. apic_write(APIC_LVTERR, value);
  839. /*
  840. * spec says clear errors after enabling vector.
  841. */
  842. if (maxlvt > 3)
  843. apic_write(APIC_ESR, 0);
  844. value = apic_read(APIC_ESR);
  845. if (value != oldvalue)
  846. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  847. "vector: 0x%08lx after: 0x%08lx\n",
  848. oldvalue, value);
  849. } else {
  850. printk(KERN_INFO "No ESR for 82489DX.\n");
  851. }
  852. }
  853. /**
  854. * setup_local_APIC - setup the local APIC
  855. */
  856. void __cpuinit setup_local_APIC(void)
  857. {
  858. unsigned int value;
  859. int i, j;
  860. #ifdef CONFIG_X86_32
  861. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  862. if (esr_disable) {
  863. apic_write(APIC_ESR, 0);
  864. apic_write(APIC_ESR, 0);
  865. apic_write(APIC_ESR, 0);
  866. apic_write(APIC_ESR, 0);
  867. }
  868. #endif
  869. preempt_disable();
  870. /*
  871. * Double-check whether this APIC is really registered.
  872. * This is meaningless in clustered apic mode, so we skip it.
  873. */
  874. if (!apic_id_registered())
  875. BUG();
  876. /*
  877. * Intel recommends to set DFR, LDR and TPR before enabling
  878. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  879. * document number 292116). So here it goes...
  880. */
  881. init_apic_ldr();
  882. /*
  883. * Set Task Priority to 'accept all'. We never change this
  884. * later on.
  885. */
  886. value = apic_read(APIC_TASKPRI);
  887. value &= ~APIC_TPRI_MASK;
  888. apic_write(APIC_TASKPRI, value);
  889. /*
  890. * After a crash, we no longer service the interrupts and a pending
  891. * interrupt from previous kernel might still have ISR bit set.
  892. *
  893. * Most probably by now CPU has serviced that pending interrupt and
  894. * it might not have done the ack_APIC_irq() because it thought,
  895. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  896. * does not clear the ISR bit and cpu thinks it has already serivced
  897. * the interrupt. Hence a vector might get locked. It was noticed
  898. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  899. */
  900. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  901. value = apic_read(APIC_ISR + i*0x10);
  902. for (j = 31; j >= 0; j--) {
  903. if (value & (1<<j))
  904. ack_APIC_irq();
  905. }
  906. }
  907. /*
  908. * Now that we are all set up, enable the APIC
  909. */
  910. value = apic_read(APIC_SPIV);
  911. value &= ~APIC_VECTOR_MASK;
  912. /*
  913. * Enable APIC
  914. */
  915. value |= APIC_SPIV_APIC_ENABLED;
  916. #ifdef CONFIG_X86_32
  917. /*
  918. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  919. * certain networking cards. If high frequency interrupts are
  920. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  921. * entry is masked/unmasked at a high rate as well then sooner or
  922. * later IOAPIC line gets 'stuck', no more interrupts are received
  923. * from the device. If focus CPU is disabled then the hang goes
  924. * away, oh well :-(
  925. *
  926. * [ This bug can be reproduced easily with a level-triggered
  927. * PCI Ne2000 networking cards and PII/PIII processors, dual
  928. * BX chipset. ]
  929. */
  930. /*
  931. * Actually disabling the focus CPU check just makes the hang less
  932. * frequent as it makes the interrupt distributon model be more
  933. * like LRU than MRU (the short-term load is more even across CPUs).
  934. * See also the comment in end_level_ioapic_irq(). --macro
  935. */
  936. /*
  937. * - enable focus processor (bit==0)
  938. * - 64bit mode always use processor focus
  939. * so no need to set it
  940. */
  941. value &= ~APIC_SPIV_FOCUS_DISABLED;
  942. #endif
  943. /*
  944. * Set spurious IRQ vector
  945. */
  946. value |= SPURIOUS_APIC_VECTOR;
  947. apic_write(APIC_SPIV, value);
  948. /*
  949. * Set up LVT0, LVT1:
  950. *
  951. * set up through-local-APIC on the BP's LINT0. This is not
  952. * strictly necessary in pure symmetric-IO mode, but sometimes
  953. * we delegate interrupts to the 8259A.
  954. */
  955. /*
  956. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  957. */
  958. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  959. if (!smp_processor_id() && (pic_mode || !value)) {
  960. value = APIC_DM_EXTINT;
  961. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  962. smp_processor_id());
  963. } else {
  964. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  965. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  966. smp_processor_id());
  967. }
  968. apic_write(APIC_LVT0, value);
  969. /*
  970. * only the BP should see the LINT1 NMI signal, obviously.
  971. */
  972. if (!smp_processor_id())
  973. value = APIC_DM_NMI;
  974. else
  975. value = APIC_DM_NMI | APIC_LVT_MASKED;
  976. if (!lapic_is_integrated()) /* 82489DX */
  977. value |= APIC_LVT_LEVEL_TRIGGER;
  978. apic_write(APIC_LVT1, value);
  979. preempt_enable();
  980. }
  981. void __cpuinit end_local_APIC_setup(void)
  982. {
  983. lapic_setup_esr();
  984. #ifdef CONFIG_X86_32
  985. {
  986. unsigned int value;
  987. /* Disable the local apic timer */
  988. value = apic_read(APIC_LVTT);
  989. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  990. apic_write(APIC_LVTT, value);
  991. }
  992. #endif
  993. setup_apic_nmi_watchdog(NULL);
  994. apic_pm_activate();
  995. }
  996. #ifdef HAVE_X2APIC
  997. void check_x2apic(void)
  998. {
  999. int msr, msr2;
  1000. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1001. if (msr & X2APIC_ENABLE) {
  1002. printk("x2apic enabled by BIOS, switching to x2apic ops\n");
  1003. x2apic_preenabled = x2apic = 1;
  1004. apic_ops = &x2apic_ops;
  1005. }
  1006. }
  1007. void enable_x2apic(void)
  1008. {
  1009. int msr, msr2;
  1010. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1011. if (!(msr & X2APIC_ENABLE)) {
  1012. printk("Enabling x2apic\n");
  1013. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  1014. }
  1015. }
  1016. void enable_IR_x2apic(void)
  1017. {
  1018. #ifdef CONFIG_INTR_REMAP
  1019. int ret;
  1020. unsigned long flags;
  1021. if (!cpu_has_x2apic)
  1022. return;
  1023. if (!x2apic_preenabled && disable_x2apic) {
  1024. printk(KERN_INFO
  1025. "Skipped enabling x2apic and Interrupt-remapping "
  1026. "because of nox2apic\n");
  1027. return;
  1028. }
  1029. if (x2apic_preenabled && disable_x2apic)
  1030. panic("Bios already enabled x2apic, can't enforce nox2apic");
  1031. if (!x2apic_preenabled && skip_ioapic_setup) {
  1032. printk(KERN_INFO
  1033. "Skipped enabling x2apic and Interrupt-remapping "
  1034. "because of skipping io-apic setup\n");
  1035. return;
  1036. }
  1037. ret = dmar_table_init();
  1038. if (ret) {
  1039. printk(KERN_INFO
  1040. "dmar_table_init() failed with %d:\n", ret);
  1041. if (x2apic_preenabled)
  1042. panic("x2apic enabled by bios. But IR enabling failed");
  1043. else
  1044. printk(KERN_INFO
  1045. "Not enabling x2apic,Intr-remapping\n");
  1046. return;
  1047. }
  1048. local_irq_save(flags);
  1049. mask_8259A();
  1050. save_mask_IO_APIC_setup();
  1051. ret = enable_intr_remapping(1);
  1052. if (ret && x2apic_preenabled) {
  1053. local_irq_restore(flags);
  1054. panic("x2apic enabled by bios. But IR enabling failed");
  1055. }
  1056. if (ret)
  1057. goto end;
  1058. if (!x2apic) {
  1059. x2apic = 1;
  1060. apic_ops = &x2apic_ops;
  1061. enable_x2apic();
  1062. }
  1063. end:
  1064. if (ret)
  1065. /*
  1066. * IR enabling failed
  1067. */
  1068. restore_IO_APIC_setup();
  1069. else
  1070. reinit_intr_remapped_IO_APIC(x2apic_preenabled);
  1071. unmask_8259A();
  1072. local_irq_restore(flags);
  1073. if (!ret) {
  1074. if (!x2apic_preenabled)
  1075. printk(KERN_INFO
  1076. "Enabled x2apic and interrupt-remapping\n");
  1077. else
  1078. printk(KERN_INFO
  1079. "Enabled Interrupt-remapping\n");
  1080. } else
  1081. printk(KERN_ERR
  1082. "Failed to enable Interrupt-remapping and x2apic\n");
  1083. #else
  1084. if (!cpu_has_x2apic)
  1085. return;
  1086. if (x2apic_preenabled)
  1087. panic("x2apic enabled prior OS handover,"
  1088. " enable CONFIG_INTR_REMAP");
  1089. printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
  1090. " and x2apic\n");
  1091. #endif
  1092. return;
  1093. }
  1094. #endif /* HAVE_X2APIC */
  1095. #ifdef CONFIG_X86_64
  1096. /*
  1097. * Detect and enable local APICs on non-SMP boards.
  1098. * Original code written by Keir Fraser.
  1099. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  1100. * not correctly set up (usually the APIC timer won't work etc.)
  1101. */
  1102. static int __init detect_init_APIC(void)
  1103. {
  1104. if (!cpu_has_apic) {
  1105. printk(KERN_INFO "No local APIC present\n");
  1106. return -1;
  1107. }
  1108. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1109. boot_cpu_physical_apicid = 0;
  1110. return 0;
  1111. }
  1112. #else
  1113. /*
  1114. * Detect and initialize APIC
  1115. */
  1116. static int __init detect_init_APIC(void)
  1117. {
  1118. u32 h, l, features;
  1119. /* Disabled by kernel option? */
  1120. if (disable_apic)
  1121. return -1;
  1122. switch (boot_cpu_data.x86_vendor) {
  1123. case X86_VENDOR_AMD:
  1124. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  1125. (boot_cpu_data.x86 == 15))
  1126. break;
  1127. goto no_apic;
  1128. case X86_VENDOR_INTEL:
  1129. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  1130. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  1131. break;
  1132. goto no_apic;
  1133. default:
  1134. goto no_apic;
  1135. }
  1136. if (!cpu_has_apic) {
  1137. /*
  1138. * Over-ride BIOS and try to enable the local APIC only if
  1139. * "lapic" specified.
  1140. */
  1141. if (!force_enable_local_apic) {
  1142. printk(KERN_INFO "Local APIC disabled by BIOS -- "
  1143. "you can enable it with \"lapic\"\n");
  1144. return -1;
  1145. }
  1146. /*
  1147. * Some BIOSes disable the local APIC in the APIC_BASE
  1148. * MSR. This can only be done in software for Intel P6 or later
  1149. * and AMD K7 (Model > 1) or later.
  1150. */
  1151. rdmsr(MSR_IA32_APICBASE, l, h);
  1152. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  1153. printk(KERN_INFO
  1154. "Local APIC disabled by BIOS -- reenabling.\n");
  1155. l &= ~MSR_IA32_APICBASE_BASE;
  1156. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  1157. wrmsr(MSR_IA32_APICBASE, l, h);
  1158. enabled_via_apicbase = 1;
  1159. }
  1160. }
  1161. /*
  1162. * The APIC feature bit should now be enabled
  1163. * in `cpuid'
  1164. */
  1165. features = cpuid_edx(1);
  1166. if (!(features & (1 << X86_FEATURE_APIC))) {
  1167. printk(KERN_WARNING "Could not enable APIC!\n");
  1168. return -1;
  1169. }
  1170. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1171. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1172. /* The BIOS may have set up the APIC at some other address */
  1173. rdmsr(MSR_IA32_APICBASE, l, h);
  1174. if (l & MSR_IA32_APICBASE_ENABLE)
  1175. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1176. printk(KERN_INFO "Found and enabled local APIC!\n");
  1177. apic_pm_activate();
  1178. return 0;
  1179. no_apic:
  1180. printk(KERN_INFO "No local APIC present or hardware disabled\n");
  1181. return -1;
  1182. }
  1183. #endif
  1184. #ifdef CONFIG_X86_64
  1185. void __init early_init_lapic_mapping(void)
  1186. {
  1187. unsigned long phys_addr;
  1188. /*
  1189. * If no local APIC can be found then go out
  1190. * : it means there is no mpatable and MADT
  1191. */
  1192. if (!smp_found_config)
  1193. return;
  1194. phys_addr = mp_lapic_addr;
  1195. set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
  1196. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1197. APIC_BASE, phys_addr);
  1198. /*
  1199. * Fetch the APIC ID of the BSP in case we have a
  1200. * default configuration (or the MP table is broken).
  1201. */
  1202. boot_cpu_physical_apicid = read_apic_id();
  1203. }
  1204. #endif
  1205. /**
  1206. * init_apic_mappings - initialize APIC mappings
  1207. */
  1208. void __init init_apic_mappings(void)
  1209. {
  1210. #ifdef HAVE_X2APIC
  1211. if (x2apic) {
  1212. boot_cpu_physical_apicid = read_apic_id();
  1213. return;
  1214. }
  1215. #endif
  1216. /*
  1217. * If no local APIC can be found then set up a fake all
  1218. * zeroes page to simulate the local APIC and another
  1219. * one for the IO-APIC.
  1220. */
  1221. if (!smp_found_config && detect_init_APIC()) {
  1222. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  1223. apic_phys = __pa(apic_phys);
  1224. } else
  1225. apic_phys = mp_lapic_addr;
  1226. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  1227. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1228. APIC_BASE, apic_phys);
  1229. /*
  1230. * Fetch the APIC ID of the BSP in case we have a
  1231. * default configuration (or the MP table is broken).
  1232. */
  1233. if (boot_cpu_physical_apicid == -1U)
  1234. boot_cpu_physical_apicid = read_apic_id();
  1235. }
  1236. /*
  1237. * This initializes the IO-APIC and APIC hardware if this is
  1238. * a UP kernel.
  1239. */
  1240. int apic_version[MAX_APICS];
  1241. int __init APIC_init_uniprocessor(void)
  1242. {
  1243. #ifdef CONFIG_X86_64
  1244. if (disable_apic) {
  1245. printk(KERN_INFO "Apic disabled\n");
  1246. return -1;
  1247. }
  1248. if (!cpu_has_apic) {
  1249. disable_apic = 1;
  1250. printk(KERN_INFO "Apic disabled by BIOS\n");
  1251. return -1;
  1252. }
  1253. #else
  1254. if (!smp_found_config && !cpu_has_apic)
  1255. return -1;
  1256. /*
  1257. * Complain if the BIOS pretends there is one.
  1258. */
  1259. if (!cpu_has_apic &&
  1260. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1261. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1262. boot_cpu_physical_apicid);
  1263. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1264. return -1;
  1265. }
  1266. #endif
  1267. #ifdef HAVE_X2APIC
  1268. enable_IR_x2apic();
  1269. #endif
  1270. #ifdef CONFIG_X86_64
  1271. setup_apic_routing();
  1272. #endif
  1273. verify_local_APIC();
  1274. connect_bsp_APIC();
  1275. #ifdef CONFIG_X86_64
  1276. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  1277. #else
  1278. /*
  1279. * Hack: In case of kdump, after a crash, kernel might be booting
  1280. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1281. * might be zero if read from MP tables. Get it from LAPIC.
  1282. */
  1283. # ifdef CONFIG_CRASH_DUMP
  1284. boot_cpu_physical_apicid = read_apic_id();
  1285. # endif
  1286. #endif
  1287. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  1288. setup_local_APIC();
  1289. #ifdef CONFIG_X86_64
  1290. /*
  1291. * Now enable IO-APICs, actually call clear_IO_APIC
  1292. * We need clear_IO_APIC before enabling vector on BP
  1293. */
  1294. if (!skip_ioapic_setup && nr_ioapics)
  1295. enable_IO_APIC();
  1296. #endif
  1297. #ifdef CONFIG_X86_IO_APIC
  1298. if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
  1299. #endif
  1300. localise_nmi_watchdog();
  1301. end_local_APIC_setup();
  1302. #ifdef CONFIG_X86_IO_APIC
  1303. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1304. setup_IO_APIC();
  1305. # ifdef CONFIG_X86_64
  1306. else
  1307. nr_ioapics = 0;
  1308. # endif
  1309. #endif
  1310. #ifdef CONFIG_X86_64
  1311. setup_boot_APIC_clock();
  1312. check_nmi_watchdog();
  1313. #else
  1314. setup_boot_clock();
  1315. #endif
  1316. return 0;
  1317. }
  1318. /*
  1319. * Local APIC interrupts
  1320. */
  1321. /*
  1322. * This interrupt should _never_ happen with our APIC/SMP architecture
  1323. */
  1324. #ifdef CONFIG_X86_64
  1325. asmlinkage void smp_spurious_interrupt(void)
  1326. #else
  1327. void smp_spurious_interrupt(struct pt_regs *regs)
  1328. #endif
  1329. {
  1330. u32 v;
  1331. #ifdef CONFIG_X86_64
  1332. exit_idle();
  1333. #endif
  1334. irq_enter();
  1335. /*
  1336. * Check if this really is a spurious interrupt and ACK it
  1337. * if it is a vectored one. Just in case...
  1338. * Spurious interrupts should not be ACKed.
  1339. */
  1340. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1341. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1342. ack_APIC_irq();
  1343. #ifdef CONFIG_X86_64
  1344. add_pda(irq_spurious_count, 1);
  1345. #else
  1346. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1347. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
  1348. "should never happen.\n", smp_processor_id());
  1349. __get_cpu_var(irq_stat).irq_spurious_count++;
  1350. #endif
  1351. irq_exit();
  1352. }
  1353. /*
  1354. * This interrupt should never happen with our APIC/SMP architecture
  1355. */
  1356. #ifdef CONFIG_X86_64
  1357. asmlinkage void smp_error_interrupt(void)
  1358. #else
  1359. void smp_error_interrupt(struct pt_regs *regs)
  1360. #endif
  1361. {
  1362. u32 v, v1;
  1363. #ifdef CONFIG_X86_64
  1364. exit_idle();
  1365. #endif
  1366. irq_enter();
  1367. /* First tickle the hardware, only then report what went on. -- REW */
  1368. v = apic_read(APIC_ESR);
  1369. apic_write(APIC_ESR, 0);
  1370. v1 = apic_read(APIC_ESR);
  1371. ack_APIC_irq();
  1372. atomic_inc(&irq_err_count);
  1373. /* Here is what the APIC error bits mean:
  1374. 0: Send CS error
  1375. 1: Receive CS error
  1376. 2: Send accept error
  1377. 3: Receive accept error
  1378. 4: Reserved
  1379. 5: Send illegal vector
  1380. 6: Received illegal vector
  1381. 7: Illegal register address
  1382. */
  1383. printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  1384. smp_processor_id(), v , v1);
  1385. irq_exit();
  1386. }
  1387. /**
  1388. * connect_bsp_APIC - attach the APIC to the interrupt system
  1389. */
  1390. void __init connect_bsp_APIC(void)
  1391. {
  1392. #ifdef CONFIG_X86_32
  1393. if (pic_mode) {
  1394. /*
  1395. * Do not trust the local APIC being empty at bootup.
  1396. */
  1397. clear_local_APIC();
  1398. /*
  1399. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1400. * local APIC to INT and NMI lines.
  1401. */
  1402. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1403. "enabling APIC mode.\n");
  1404. outb(0x70, 0x22);
  1405. outb(0x01, 0x23);
  1406. }
  1407. #endif
  1408. enable_apic_mode();
  1409. }
  1410. /**
  1411. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1412. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1413. *
  1414. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1415. * APIC is disabled.
  1416. */
  1417. void disconnect_bsp_APIC(int virt_wire_setup)
  1418. {
  1419. unsigned int value;
  1420. #ifdef CONFIG_X86_32
  1421. if (pic_mode) {
  1422. /*
  1423. * Put the board back into PIC mode (has an effect only on
  1424. * certain older boards). Note that APIC interrupts, including
  1425. * IPIs, won't work beyond this point! The only exception are
  1426. * INIT IPIs.
  1427. */
  1428. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1429. "entering PIC mode.\n");
  1430. outb(0x70, 0x22);
  1431. outb(0x00, 0x23);
  1432. return;
  1433. }
  1434. #endif
  1435. /* Go back to Virtual Wire compatibility mode */
  1436. /* For the spurious interrupt use vector F, and enable it */
  1437. value = apic_read(APIC_SPIV);
  1438. value &= ~APIC_VECTOR_MASK;
  1439. value |= APIC_SPIV_APIC_ENABLED;
  1440. value |= 0xf;
  1441. apic_write(APIC_SPIV, value);
  1442. if (!virt_wire_setup) {
  1443. /*
  1444. * For LVT0 make it edge triggered, active high,
  1445. * external and enabled
  1446. */
  1447. value = apic_read(APIC_LVT0);
  1448. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1449. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1450. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1451. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1452. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1453. apic_write(APIC_LVT0, value);
  1454. } else {
  1455. /* Disable LVT0 */
  1456. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  1457. }
  1458. /*
  1459. * For LVT1 make it edge triggered, active high,
  1460. * nmi and enabled
  1461. */
  1462. value = apic_read(APIC_LVT1);
  1463. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1464. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1465. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1466. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1467. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1468. apic_write(APIC_LVT1, value);
  1469. }
  1470. void __cpuinit generic_processor_info(int apicid, int version)
  1471. {
  1472. int cpu;
  1473. cpumask_t tmp_map;
  1474. /*
  1475. * Validate version
  1476. */
  1477. if (version == 0x0) {
  1478. printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
  1479. "fixing up to 0x10. (tell your hw vendor)\n",
  1480. version);
  1481. version = 0x10;
  1482. }
  1483. apic_version[apicid] = version;
  1484. if (num_processors >= NR_CPUS) {
  1485. printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
  1486. " Processor ignored.\n", NR_CPUS);
  1487. return;
  1488. }
  1489. num_processors++;
  1490. cpus_complement(tmp_map, cpu_present_map);
  1491. cpu = first_cpu(tmp_map);
  1492. physid_set(apicid, phys_cpu_present_map);
  1493. if (apicid == boot_cpu_physical_apicid) {
  1494. /*
  1495. * x86_bios_cpu_apicid is required to have processors listed
  1496. * in same order as logical cpu numbers. Hence the first
  1497. * entry is BSP, and so on.
  1498. */
  1499. cpu = 0;
  1500. }
  1501. if (apicid > max_physical_apicid)
  1502. max_physical_apicid = apicid;
  1503. #ifdef CONFIG_X86_32
  1504. /*
  1505. * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
  1506. * but we need to work other dependencies like SMP_SUSPEND etc
  1507. * before this can be done without some confusion.
  1508. * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
  1509. * - Ashok Raj <ashok.raj@intel.com>
  1510. */
  1511. if (max_physical_apicid >= 8) {
  1512. switch (boot_cpu_data.x86_vendor) {
  1513. case X86_VENDOR_INTEL:
  1514. if (!APIC_XAPIC(version)) {
  1515. def_to_bigsmp = 0;
  1516. break;
  1517. }
  1518. /* If P4 and above fall through */
  1519. case X86_VENDOR_AMD:
  1520. def_to_bigsmp = 1;
  1521. }
  1522. }
  1523. #endif
  1524. #if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
  1525. /* are we being called early in kernel startup? */
  1526. if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
  1527. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  1528. u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1529. cpu_to_apicid[cpu] = apicid;
  1530. bios_cpu_apicid[cpu] = apicid;
  1531. } else {
  1532. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  1533. per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  1534. }
  1535. #endif
  1536. cpu_set(cpu, cpu_possible_map);
  1537. cpu_set(cpu, cpu_present_map);
  1538. }
  1539. #ifdef CONFIG_X86_64
  1540. int hard_smp_processor_id(void)
  1541. {
  1542. return read_apic_id();
  1543. }
  1544. #endif
  1545. /*
  1546. * Power management
  1547. */
  1548. #ifdef CONFIG_PM
  1549. static struct {
  1550. /*
  1551. * 'active' is true if the local APIC was enabled by us and
  1552. * not the BIOS; this signifies that we are also responsible
  1553. * for disabling it before entering apm/acpi suspend
  1554. */
  1555. int active;
  1556. /* r/w apic fields */
  1557. unsigned int apic_id;
  1558. unsigned int apic_taskpri;
  1559. unsigned int apic_ldr;
  1560. unsigned int apic_dfr;
  1561. unsigned int apic_spiv;
  1562. unsigned int apic_lvtt;
  1563. unsigned int apic_lvtpc;
  1564. unsigned int apic_lvt0;
  1565. unsigned int apic_lvt1;
  1566. unsigned int apic_lvterr;
  1567. unsigned int apic_tmict;
  1568. unsigned int apic_tdcr;
  1569. unsigned int apic_thmr;
  1570. } apic_pm_state;
  1571. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1572. {
  1573. unsigned long flags;
  1574. int maxlvt;
  1575. if (!apic_pm_state.active)
  1576. return 0;
  1577. maxlvt = lapic_get_maxlvt();
  1578. apic_pm_state.apic_id = apic_read(APIC_ID);
  1579. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1580. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1581. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1582. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1583. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1584. if (maxlvt >= 4)
  1585. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1586. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1587. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1588. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1589. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1590. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1591. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1592. if (maxlvt >= 5)
  1593. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1594. #endif
  1595. local_irq_save(flags);
  1596. disable_local_APIC();
  1597. local_irq_restore(flags);
  1598. return 0;
  1599. }
  1600. static int lapic_resume(struct sys_device *dev)
  1601. {
  1602. unsigned int l, h;
  1603. unsigned long flags;
  1604. int maxlvt;
  1605. if (!apic_pm_state.active)
  1606. return 0;
  1607. maxlvt = lapic_get_maxlvt();
  1608. local_irq_save(flags);
  1609. #ifdef HAVE_X2APIC
  1610. if (x2apic)
  1611. enable_x2apic();
  1612. else
  1613. #endif
  1614. {
  1615. /*
  1616. * Make sure the APICBASE points to the right address
  1617. *
  1618. * FIXME! This will be wrong if we ever support suspend on
  1619. * SMP! We'll need to do this as part of the CPU restore!
  1620. */
  1621. rdmsr(MSR_IA32_APICBASE, l, h);
  1622. l &= ~MSR_IA32_APICBASE_BASE;
  1623. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1624. wrmsr(MSR_IA32_APICBASE, l, h);
  1625. }
  1626. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1627. apic_write(APIC_ID, apic_pm_state.apic_id);
  1628. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1629. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1630. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1631. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1632. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1633. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1634. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1635. if (maxlvt >= 5)
  1636. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1637. #endif
  1638. if (maxlvt >= 4)
  1639. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1640. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1641. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1642. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1643. apic_write(APIC_ESR, 0);
  1644. apic_read(APIC_ESR);
  1645. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1646. apic_write(APIC_ESR, 0);
  1647. apic_read(APIC_ESR);
  1648. local_irq_restore(flags);
  1649. return 0;
  1650. }
  1651. /*
  1652. * This device has no shutdown method - fully functioning local APICs
  1653. * are needed on every CPU up until machine_halt/restart/poweroff.
  1654. */
  1655. static struct sysdev_class lapic_sysclass = {
  1656. .name = "lapic",
  1657. .resume = lapic_resume,
  1658. .suspend = lapic_suspend,
  1659. };
  1660. static struct sys_device device_lapic = {
  1661. .id = 0,
  1662. .cls = &lapic_sysclass,
  1663. };
  1664. static void __cpuinit apic_pm_activate(void)
  1665. {
  1666. apic_pm_state.active = 1;
  1667. }
  1668. static int __init init_lapic_sysfs(void)
  1669. {
  1670. int error;
  1671. if (!cpu_has_apic)
  1672. return 0;
  1673. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1674. error = sysdev_class_register(&lapic_sysclass);
  1675. if (!error)
  1676. error = sysdev_register(&device_lapic);
  1677. return error;
  1678. }
  1679. device_initcall(init_lapic_sysfs);
  1680. #else /* CONFIG_PM */
  1681. static void apic_pm_activate(void) { }
  1682. #endif /* CONFIG_PM */
  1683. #ifdef CONFIG_X86_64
  1684. /*
  1685. * apic_is_clustered_box() -- Check if we can expect good TSC
  1686. *
  1687. * Thus far, the major user of this is IBM's Summit2 series:
  1688. *
  1689. * Clustered boxes may have unsynced TSC problems if they are
  1690. * multi-chassis. Use available data to take a good guess.
  1691. * If in doubt, go HPET.
  1692. */
  1693. __cpuinit int apic_is_clustered_box(void)
  1694. {
  1695. int i, clusters, zeros;
  1696. unsigned id;
  1697. u16 *bios_cpu_apicid;
  1698. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1699. /*
  1700. * there is not this kind of box with AMD CPU yet.
  1701. * Some AMD box with quadcore cpu and 8 sockets apicid
  1702. * will be [4, 0x23] or [8, 0x27] could be thought to
  1703. * vsmp box still need checking...
  1704. */
  1705. if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
  1706. return 0;
  1707. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1708. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1709. for (i = 0; i < NR_CPUS; i++) {
  1710. /* are we being called early in kernel startup? */
  1711. if (bios_cpu_apicid) {
  1712. id = bios_cpu_apicid[i];
  1713. }
  1714. else if (i < nr_cpu_ids) {
  1715. if (cpu_present(i))
  1716. id = per_cpu(x86_bios_cpu_apicid, i);
  1717. else
  1718. continue;
  1719. }
  1720. else
  1721. break;
  1722. if (id != BAD_APICID)
  1723. __set_bit(APIC_CLUSTERID(id), clustermap);
  1724. }
  1725. /* Problem: Partially populated chassis may not have CPUs in some of
  1726. * the APIC clusters they have been allocated. Only present CPUs have
  1727. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1728. * Since clusters are allocated sequentially, count zeros only if
  1729. * they are bounded by ones.
  1730. */
  1731. clusters = 0;
  1732. zeros = 0;
  1733. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1734. if (test_bit(i, clustermap)) {
  1735. clusters += 1 + zeros;
  1736. zeros = 0;
  1737. } else
  1738. ++zeros;
  1739. }
  1740. /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1741. * not guaranteed to be synced between boards
  1742. */
  1743. if (is_vsmp_box() && clusters > 1)
  1744. return 1;
  1745. /*
  1746. * If clusters > 2, then should be multi-chassis.
  1747. * May have to revisit this when multi-core + hyperthreaded CPUs come
  1748. * out, but AFAIK this will work even for them.
  1749. */
  1750. return (clusters > 2);
  1751. }
  1752. #endif
  1753. /*
  1754. * APIC command line parameters
  1755. */
  1756. static int __init setup_disableapic(char *arg)
  1757. {
  1758. disable_apic = 1;
  1759. setup_clear_cpu_cap(X86_FEATURE_APIC);
  1760. return 0;
  1761. }
  1762. early_param("disableapic", setup_disableapic);
  1763. /* same as disableapic, for compatibility */
  1764. static int __init setup_nolapic(char *arg)
  1765. {
  1766. return setup_disableapic(arg);
  1767. }
  1768. early_param("nolapic", setup_nolapic);
  1769. static int __init parse_lapic_timer_c2_ok(char *arg)
  1770. {
  1771. local_apic_timer_c2_ok = 1;
  1772. return 0;
  1773. }
  1774. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1775. static int __init parse_disable_apic_timer(char *arg)
  1776. {
  1777. disable_apic_timer = 1;
  1778. return 0;
  1779. }
  1780. early_param("noapictimer", parse_disable_apic_timer);
  1781. static int __init parse_nolapic_timer(char *arg)
  1782. {
  1783. disable_apic_timer = 1;
  1784. return 0;
  1785. }
  1786. early_param("nolapic_timer", parse_nolapic_timer);
  1787. static int __init apic_set_verbosity(char *arg)
  1788. {
  1789. if (!arg) {
  1790. #ifdef CONFIG_X86_64
  1791. skip_ioapic_setup = 0;
  1792. return 0;
  1793. #endif
  1794. return -EINVAL;
  1795. }
  1796. if (strcmp("debug", arg) == 0)
  1797. apic_verbosity = APIC_DEBUG;
  1798. else if (strcmp("verbose", arg) == 0)
  1799. apic_verbosity = APIC_VERBOSE;
  1800. else {
  1801. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  1802. " use apic=verbose or apic=debug\n", arg);
  1803. return -EINVAL;
  1804. }
  1805. return 0;
  1806. }
  1807. early_param("apic", apic_set_verbosity);
  1808. static int __init lapic_insert_resource(void)
  1809. {
  1810. if (!apic_phys)
  1811. return -1;
  1812. /* Put local APIC into the resource map. */
  1813. lapic_resource.start = apic_phys;
  1814. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  1815. insert_resource(&iomem_resource, &lapic_resource);
  1816. return 0;
  1817. }
  1818. /*
  1819. * need call insert after e820_reserve_resources()
  1820. * that is using request_resource
  1821. */
  1822. late_initcall(lapic_insert_resource);