apic.c 61 KB

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