apic.c 67 KB

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