apic.c 52 KB

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