apic.c 52 KB

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