apic.c 54 KB

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