apic.c 55 KB

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