apic.c 61 KB

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