apic.c 65 KB

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