apic.c 61 KB

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