apic.c 55 KB

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