apic.c 53 KB

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