apic.c 64 KB

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