arm_arch_timer.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/clocksource/arm_arch_timer.c
  4. *
  5. * Copyright (C) 2011 ARM Ltd.
  6. * All Rights Reserved
  7. */
  8. #define pr_fmt(fmt) "arch_timer: " fmt
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/smp.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpu_pm.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/clocksource_ids.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kstrtox.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_address.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/sched/clock.h>
  25. #include <linux/sched_clock.h>
  26. #include <linux/acpi.h>
  27. #include <linux/arm-smccc.h>
  28. #include <linux/ptp_kvm.h>
  29. #include <asm/arch_timer.h>
  30. #include <asm/virt.h>
  31. #include <clocksource/arm_arch_timer.h>
  32. #define CNTTIDR 0x08
  33. #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
  34. #define CNTACR(n) (0x40 + ((n) * 4))
  35. #define CNTACR_RPCT BIT(0)
  36. #define CNTACR_RVCT BIT(1)
  37. #define CNTACR_RFRQ BIT(2)
  38. #define CNTACR_RVOFF BIT(3)
  39. #define CNTACR_RWVT BIT(4)
  40. #define CNTACR_RWPT BIT(5)
  41. #define CNTPCT_LO 0x00
  42. #define CNTVCT_LO 0x08
  43. #define CNTFRQ 0x10
  44. #define CNTP_CVAL_LO 0x20
  45. #define CNTP_CTL 0x2c
  46. #define CNTV_CVAL_LO 0x30
  47. #define CNTV_CTL 0x3c
  48. /*
  49. * The minimum amount of time a generic counter is guaranteed to not roll over
  50. * (40 years)
  51. */
  52. #define MIN_ROLLOVER_SECS (40ULL * 365 * 24 * 3600)
  53. static unsigned arch_timers_present __initdata;
  54. struct arch_timer {
  55. void __iomem *base;
  56. struct clock_event_device evt;
  57. };
  58. static struct arch_timer *arch_timer_mem __ro_after_init;
  59. #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
  60. static u32 arch_timer_rate __ro_after_init;
  61. static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI] __ro_after_init;
  62. static const char *arch_timer_ppi_names[ARCH_TIMER_MAX_TIMER_PPI] = {
  63. [ARCH_TIMER_PHYS_SECURE_PPI] = "sec-phys",
  64. [ARCH_TIMER_PHYS_NONSECURE_PPI] = "phys",
  65. [ARCH_TIMER_VIRT_PPI] = "virt",
  66. [ARCH_TIMER_HYP_PPI] = "hyp-phys",
  67. [ARCH_TIMER_HYP_VIRT_PPI] = "hyp-virt",
  68. };
  69. static struct clock_event_device __percpu *arch_timer_evt;
  70. static enum arch_timer_ppi_nr arch_timer_uses_ppi __ro_after_init = ARCH_TIMER_VIRT_PPI;
  71. static bool arch_timer_c3stop __ro_after_init;
  72. static bool arch_timer_mem_use_virtual __ro_after_init;
  73. static bool arch_counter_suspend_stop __ro_after_init;
  74. #ifdef CONFIG_GENERIC_GETTIMEOFDAY
  75. static enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_ARCHTIMER;
  76. #else
  77. static enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_NONE;
  78. #endif /* CONFIG_GENERIC_GETTIMEOFDAY */
  79. static cpumask_t evtstrm_available = CPU_MASK_NONE;
  80. static bool evtstrm_enable __ro_after_init = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
  81. static int __init early_evtstrm_cfg(char *buf)
  82. {
  83. return kstrtobool(buf, &evtstrm_enable);
  84. }
  85. early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
  86. /*
  87. * Makes an educated guess at a valid counter width based on the Generic Timer
  88. * specification. Of note:
  89. * 1) the system counter is at least 56 bits wide
  90. * 2) a roll-over time of not less than 40 years
  91. *
  92. * See 'ARM DDI 0487G.a D11.1.2 ("The system counter")' for more details.
  93. */
  94. static int arch_counter_get_width(void)
  95. {
  96. u64 min_cycles = MIN_ROLLOVER_SECS * arch_timer_rate;
  97. /* guarantee the returned width is within the valid range */
  98. return clamp_val(ilog2(min_cycles - 1) + 1, 56, 64);
  99. }
  100. /*
  101. * Architected system timer support.
  102. */
  103. static __always_inline
  104. void arch_timer_reg_write(int access, enum arch_timer_reg reg, u64 val,
  105. struct clock_event_device *clk)
  106. {
  107. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  108. struct arch_timer *timer = to_arch_timer(clk);
  109. switch (reg) {
  110. case ARCH_TIMER_REG_CTRL:
  111. writel_relaxed((u32)val, timer->base + CNTP_CTL);
  112. break;
  113. case ARCH_TIMER_REG_CVAL:
  114. /*
  115. * Not guaranteed to be atomic, so the timer
  116. * must be disabled at this point.
  117. */
  118. writeq_relaxed(val, timer->base + CNTP_CVAL_LO);
  119. break;
  120. default:
  121. BUILD_BUG();
  122. }
  123. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  124. struct arch_timer *timer = to_arch_timer(clk);
  125. switch (reg) {
  126. case ARCH_TIMER_REG_CTRL:
  127. writel_relaxed((u32)val, timer->base + CNTV_CTL);
  128. break;
  129. case ARCH_TIMER_REG_CVAL:
  130. /* Same restriction as above */
  131. writeq_relaxed(val, timer->base + CNTV_CVAL_LO);
  132. break;
  133. default:
  134. BUILD_BUG();
  135. }
  136. } else {
  137. arch_timer_reg_write_cp15(access, reg, val);
  138. }
  139. }
  140. static __always_inline
  141. u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
  142. struct clock_event_device *clk)
  143. {
  144. u32 val;
  145. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  146. struct arch_timer *timer = to_arch_timer(clk);
  147. switch (reg) {
  148. case ARCH_TIMER_REG_CTRL:
  149. val = readl_relaxed(timer->base + CNTP_CTL);
  150. break;
  151. default:
  152. BUILD_BUG();
  153. }
  154. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  155. struct arch_timer *timer = to_arch_timer(clk);
  156. switch (reg) {
  157. case ARCH_TIMER_REG_CTRL:
  158. val = readl_relaxed(timer->base + CNTV_CTL);
  159. break;
  160. default:
  161. BUILD_BUG();
  162. }
  163. } else {
  164. val = arch_timer_reg_read_cp15(access, reg);
  165. }
  166. return val;
  167. }
  168. static noinstr u64 raw_counter_get_cntpct_stable(void)
  169. {
  170. return __arch_counter_get_cntpct_stable();
  171. }
  172. static notrace u64 arch_counter_get_cntpct_stable(void)
  173. {
  174. u64 val;
  175. preempt_disable_notrace();
  176. val = __arch_counter_get_cntpct_stable();
  177. preempt_enable_notrace();
  178. return val;
  179. }
  180. static noinstr u64 arch_counter_get_cntpct(void)
  181. {
  182. return __arch_counter_get_cntpct();
  183. }
  184. static noinstr u64 raw_counter_get_cntvct_stable(void)
  185. {
  186. return __arch_counter_get_cntvct_stable();
  187. }
  188. static notrace u64 arch_counter_get_cntvct_stable(void)
  189. {
  190. u64 val;
  191. preempt_disable_notrace();
  192. val = __arch_counter_get_cntvct_stable();
  193. preempt_enable_notrace();
  194. return val;
  195. }
  196. static noinstr u64 arch_counter_get_cntvct(void)
  197. {
  198. return __arch_counter_get_cntvct();
  199. }
  200. /*
  201. * Default to cp15 based access because arm64 uses this function for
  202. * sched_clock() before DT is probed and the cp15 method is guaranteed
  203. * to exist on arm64. arm doesn't use this before DT is probed so even
  204. * if we don't have the cp15 accessors we won't have a problem.
  205. */
  206. u64 (*arch_timer_read_counter)(void) __ro_after_init = arch_counter_get_cntvct;
  207. EXPORT_SYMBOL_GPL(arch_timer_read_counter);
  208. static u64 arch_counter_read(struct clocksource *cs)
  209. {
  210. return arch_timer_read_counter();
  211. }
  212. static u64 arch_counter_read_cc(const struct cyclecounter *cc)
  213. {
  214. return arch_timer_read_counter();
  215. }
  216. static struct clocksource clocksource_counter = {
  217. .name = "arch_sys_counter",
  218. .id = CSID_ARM_ARCH_COUNTER,
  219. .rating = 400,
  220. .read = arch_counter_read,
  221. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  222. };
  223. static struct cyclecounter cyclecounter __ro_after_init = {
  224. .read = arch_counter_read_cc,
  225. };
  226. struct ate_acpi_oem_info {
  227. char oem_id[ACPI_OEM_ID_SIZE + 1];
  228. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
  229. u32 oem_revision;
  230. };
  231. #ifdef CONFIG_FSL_ERRATUM_A008585
  232. /*
  233. * The number of retries is an arbitrary value well beyond the highest number
  234. * of iterations the loop has been observed to take.
  235. */
  236. #define __fsl_a008585_read_reg(reg) ({ \
  237. u64 _old, _new; \
  238. int _retries = 200; \
  239. \
  240. do { \
  241. _old = read_sysreg(reg); \
  242. _new = read_sysreg(reg); \
  243. _retries--; \
  244. } while (unlikely(_old != _new) && _retries); \
  245. \
  246. WARN_ON_ONCE(!_retries); \
  247. _new; \
  248. })
  249. static u64 notrace fsl_a008585_read_cntpct_el0(void)
  250. {
  251. return __fsl_a008585_read_reg(cntpct_el0);
  252. }
  253. static u64 notrace fsl_a008585_read_cntvct_el0(void)
  254. {
  255. return __fsl_a008585_read_reg(cntvct_el0);
  256. }
  257. #endif
  258. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  259. /*
  260. * Verify whether the value of the second read is larger than the first by
  261. * less than 32 is the only way to confirm the value is correct, so clear the
  262. * lower 5 bits to check whether the difference is greater than 32 or not.
  263. * Theoretically the erratum should not occur more than twice in succession
  264. * when reading the system counter, but it is possible that some interrupts
  265. * may lead to more than twice read errors, triggering the warning, so setting
  266. * the number of retries far beyond the number of iterations the loop has been
  267. * observed to take.
  268. */
  269. #define __hisi_161010101_read_reg(reg) ({ \
  270. u64 _old, _new; \
  271. int _retries = 50; \
  272. \
  273. do { \
  274. _old = read_sysreg(reg); \
  275. _new = read_sysreg(reg); \
  276. _retries--; \
  277. } while (unlikely((_new - _old) >> 5) && _retries); \
  278. \
  279. WARN_ON_ONCE(!_retries); \
  280. _new; \
  281. })
  282. static u64 notrace hisi_161010101_read_cntpct_el0(void)
  283. {
  284. return __hisi_161010101_read_reg(cntpct_el0);
  285. }
  286. static u64 notrace hisi_161010101_read_cntvct_el0(void)
  287. {
  288. return __hisi_161010101_read_reg(cntvct_el0);
  289. }
  290. static const struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
  291. /*
  292. * Note that trailing spaces are required to properly match
  293. * the OEM table information.
  294. */
  295. {
  296. .oem_id = "HISI ",
  297. .oem_table_id = "HIP05 ",
  298. .oem_revision = 0,
  299. },
  300. {
  301. .oem_id = "HISI ",
  302. .oem_table_id = "HIP06 ",
  303. .oem_revision = 0,
  304. },
  305. {
  306. .oem_id = "HISI ",
  307. .oem_table_id = "HIP07 ",
  308. .oem_revision = 0,
  309. },
  310. { /* Sentinel indicating the end of the OEM array */ },
  311. };
  312. #endif
  313. #ifdef CONFIG_ARM64_ERRATUM_858921
  314. static u64 notrace arm64_858921_read_cntpct_el0(void)
  315. {
  316. u64 old, new;
  317. old = read_sysreg(cntpct_el0);
  318. new = read_sysreg(cntpct_el0);
  319. return (((old ^ new) >> 32) & 1) ? old : new;
  320. }
  321. static u64 notrace arm64_858921_read_cntvct_el0(void)
  322. {
  323. u64 old, new;
  324. old = read_sysreg(cntvct_el0);
  325. new = read_sysreg(cntvct_el0);
  326. return (((old ^ new) >> 32) & 1) ? old : new;
  327. }
  328. #endif
  329. #ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
  330. /*
  331. * The low bits of the counter registers are indeterminate while bit 10 or
  332. * greater is rolling over. Since the counter value can jump both backward
  333. * (7ff -> 000 -> 800) and forward (7ff -> fff -> 800), ignore register values
  334. * with all ones or all zeros in the low bits. Bound the loop by the maximum
  335. * number of CPU cycles in 3 consecutive 24 MHz counter periods.
  336. */
  337. #define __sun50i_a64_read_reg(reg) ({ \
  338. u64 _val; \
  339. int _retries = 150; \
  340. \
  341. do { \
  342. _val = read_sysreg(reg); \
  343. _retries--; \
  344. } while (((_val + 1) & GENMASK(8, 0)) <= 1 && _retries); \
  345. \
  346. WARN_ON_ONCE(!_retries); \
  347. _val; \
  348. })
  349. static u64 notrace sun50i_a64_read_cntpct_el0(void)
  350. {
  351. return __sun50i_a64_read_reg(cntpct_el0);
  352. }
  353. static u64 notrace sun50i_a64_read_cntvct_el0(void)
  354. {
  355. return __sun50i_a64_read_reg(cntvct_el0);
  356. }
  357. #endif
  358. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  359. DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *, timer_unstable_counter_workaround);
  360. EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
  361. static atomic_t timer_unstable_counter_workaround_in_use = ATOMIC_INIT(0);
  362. /*
  363. * Force the inlining of this function so that the register accesses
  364. * can be themselves correctly inlined.
  365. */
  366. static __always_inline
  367. void erratum_set_next_event_generic(const int access, unsigned long evt,
  368. struct clock_event_device *clk)
  369. {
  370. unsigned long ctrl;
  371. u64 cval;
  372. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  373. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  374. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  375. if (access == ARCH_TIMER_PHYS_ACCESS) {
  376. cval = evt + arch_counter_get_cntpct_stable();
  377. write_sysreg(cval, cntp_cval_el0);
  378. } else {
  379. cval = evt + arch_counter_get_cntvct_stable();
  380. write_sysreg(cval, cntv_cval_el0);
  381. }
  382. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  383. }
  384. static __maybe_unused int erratum_set_next_event_virt(unsigned long evt,
  385. struct clock_event_device *clk)
  386. {
  387. erratum_set_next_event_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  388. return 0;
  389. }
  390. static __maybe_unused int erratum_set_next_event_phys(unsigned long evt,
  391. struct clock_event_device *clk)
  392. {
  393. erratum_set_next_event_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  394. return 0;
  395. }
  396. static const struct arch_timer_erratum_workaround ool_workarounds[] = {
  397. #ifdef CONFIG_FSL_ERRATUM_A008585
  398. {
  399. .match_type = ate_match_dt,
  400. .id = "fsl,erratum-a008585",
  401. .desc = "Freescale erratum a005858",
  402. .read_cntpct_el0 = fsl_a008585_read_cntpct_el0,
  403. .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
  404. .set_next_event_phys = erratum_set_next_event_phys,
  405. .set_next_event_virt = erratum_set_next_event_virt,
  406. },
  407. #endif
  408. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  409. {
  410. .match_type = ate_match_dt,
  411. .id = "hisilicon,erratum-161010101",
  412. .desc = "HiSilicon erratum 161010101",
  413. .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
  414. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  415. .set_next_event_phys = erratum_set_next_event_phys,
  416. .set_next_event_virt = erratum_set_next_event_virt,
  417. },
  418. {
  419. .match_type = ate_match_acpi_oem_info,
  420. .id = hisi_161010101_oem_info,
  421. .desc = "HiSilicon erratum 161010101",
  422. .read_cntpct_el0 = hisi_161010101_read_cntpct_el0,
  423. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  424. .set_next_event_phys = erratum_set_next_event_phys,
  425. .set_next_event_virt = erratum_set_next_event_virt,
  426. },
  427. #endif
  428. #ifdef CONFIG_ARM64_ERRATUM_858921
  429. {
  430. .match_type = ate_match_local_cap_id,
  431. .id = (void *)ARM64_WORKAROUND_858921,
  432. .desc = "ARM erratum 858921",
  433. .read_cntpct_el0 = arm64_858921_read_cntpct_el0,
  434. .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
  435. .set_next_event_phys = erratum_set_next_event_phys,
  436. .set_next_event_virt = erratum_set_next_event_virt,
  437. },
  438. #endif
  439. #ifdef CONFIG_SUN50I_ERRATUM_UNKNOWN1
  440. {
  441. .match_type = ate_match_dt,
  442. .id = "allwinner,erratum-unknown1",
  443. .desc = "Allwinner erratum UNKNOWN1",
  444. .read_cntpct_el0 = sun50i_a64_read_cntpct_el0,
  445. .read_cntvct_el0 = sun50i_a64_read_cntvct_el0,
  446. .set_next_event_phys = erratum_set_next_event_phys,
  447. .set_next_event_virt = erratum_set_next_event_virt,
  448. },
  449. #endif
  450. #ifdef CONFIG_ARM64_ERRATUM_1418040
  451. {
  452. .match_type = ate_match_local_cap_id,
  453. .id = (void *)ARM64_WORKAROUND_1418040,
  454. .desc = "ARM erratum 1418040",
  455. .disable_compat_vdso = true,
  456. },
  457. #endif
  458. };
  459. typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
  460. const void *);
  461. static
  462. bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
  463. const void *arg)
  464. {
  465. const struct device_node *np = arg;
  466. return of_property_read_bool(np, wa->id);
  467. }
  468. static
  469. bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
  470. const void *arg)
  471. {
  472. return this_cpu_has_cap((uintptr_t)wa->id);
  473. }
  474. static
  475. bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
  476. const void *arg)
  477. {
  478. static const struct ate_acpi_oem_info empty_oem_info = {};
  479. const struct ate_acpi_oem_info *info = wa->id;
  480. const struct acpi_table_header *table = arg;
  481. /* Iterate over the ACPI OEM info array, looking for a match */
  482. while (memcmp(info, &empty_oem_info, sizeof(*info))) {
  483. if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
  484. !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
  485. info->oem_revision == table->oem_revision)
  486. return true;
  487. info++;
  488. }
  489. return false;
  490. }
  491. static const struct arch_timer_erratum_workaround *
  492. arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
  493. ate_match_fn_t match_fn,
  494. void *arg)
  495. {
  496. int i;
  497. for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
  498. if (ool_workarounds[i].match_type != type)
  499. continue;
  500. if (match_fn(&ool_workarounds[i], arg))
  501. return &ool_workarounds[i];
  502. }
  503. return NULL;
  504. }
  505. static
  506. void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
  507. bool local)
  508. {
  509. int i;
  510. if (local) {
  511. __this_cpu_write(timer_unstable_counter_workaround, wa);
  512. } else {
  513. for_each_possible_cpu(i)
  514. per_cpu(timer_unstable_counter_workaround, i) = wa;
  515. }
  516. if (wa->read_cntvct_el0 || wa->read_cntpct_el0)
  517. atomic_set(&timer_unstable_counter_workaround_in_use, 1);
  518. /*
  519. * Don't use the vdso fastpath if errata require using the
  520. * out-of-line counter accessor. We may change our mind pretty
  521. * late in the game (with a per-CPU erratum, for example), so
  522. * change both the default value and the vdso itself.
  523. */
  524. if (wa->read_cntvct_el0) {
  525. clocksource_counter.vdso_clock_mode = VDSO_CLOCKMODE_NONE;
  526. vdso_default = VDSO_CLOCKMODE_NONE;
  527. } else if (wa->disable_compat_vdso && vdso_default != VDSO_CLOCKMODE_NONE) {
  528. vdso_default = VDSO_CLOCKMODE_ARCHTIMER_NOCOMPAT;
  529. clocksource_counter.vdso_clock_mode = vdso_default;
  530. }
  531. }
  532. static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
  533. void *arg)
  534. {
  535. const struct arch_timer_erratum_workaround *wa, *__wa;
  536. ate_match_fn_t match_fn = NULL;
  537. bool local = false;
  538. switch (type) {
  539. case ate_match_dt:
  540. match_fn = arch_timer_check_dt_erratum;
  541. break;
  542. case ate_match_local_cap_id:
  543. match_fn = arch_timer_check_local_cap_erratum;
  544. local = true;
  545. break;
  546. case ate_match_acpi_oem_info:
  547. match_fn = arch_timer_check_acpi_oem_erratum;
  548. break;
  549. default:
  550. WARN_ON(1);
  551. return;
  552. }
  553. wa = arch_timer_iterate_errata(type, match_fn, arg);
  554. if (!wa)
  555. return;
  556. __wa = __this_cpu_read(timer_unstable_counter_workaround);
  557. if (__wa && wa != __wa)
  558. pr_warn("Can't enable workaround for %s (clashes with %s\n)",
  559. wa->desc, __wa->desc);
  560. if (__wa)
  561. return;
  562. arch_timer_enable_workaround(wa, local);
  563. pr_info("Enabling %s workaround for %s\n",
  564. local ? "local" : "global", wa->desc);
  565. }
  566. static bool arch_timer_this_cpu_has_cntvct_wa(void)
  567. {
  568. return has_erratum_handler(read_cntvct_el0);
  569. }
  570. static bool arch_timer_counter_has_wa(void)
  571. {
  572. return atomic_read(&timer_unstable_counter_workaround_in_use);
  573. }
  574. #else
  575. #define arch_timer_check_ool_workaround(t,a) do { } while(0)
  576. #define arch_timer_this_cpu_has_cntvct_wa() ({false;})
  577. #define arch_timer_counter_has_wa() ({false;})
  578. #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
  579. static __always_inline irqreturn_t timer_handler(const int access,
  580. struct clock_event_device *evt)
  581. {
  582. unsigned long ctrl;
  583. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
  584. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  585. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  586. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
  587. evt->event_handler(evt);
  588. return IRQ_HANDLED;
  589. }
  590. return IRQ_NONE;
  591. }
  592. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  593. {
  594. struct clock_event_device *evt = dev_id;
  595. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  596. }
  597. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  598. {
  599. struct clock_event_device *evt = dev_id;
  600. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  601. }
  602. static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
  603. {
  604. struct clock_event_device *evt = dev_id;
  605. return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
  606. }
  607. static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
  608. {
  609. struct clock_event_device *evt = dev_id;
  610. return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
  611. }
  612. static __always_inline int arch_timer_shutdown(const int access,
  613. struct clock_event_device *clk)
  614. {
  615. unsigned long ctrl;
  616. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  617. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  618. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  619. return 0;
  620. }
  621. static int arch_timer_shutdown_virt(struct clock_event_device *clk)
  622. {
  623. return arch_timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
  624. }
  625. static int arch_timer_shutdown_phys(struct clock_event_device *clk)
  626. {
  627. return arch_timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
  628. }
  629. static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
  630. {
  631. return arch_timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
  632. }
  633. static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
  634. {
  635. return arch_timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
  636. }
  637. static __always_inline void set_next_event(const int access, unsigned long evt,
  638. struct clock_event_device *clk)
  639. {
  640. unsigned long ctrl;
  641. u64 cnt;
  642. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  643. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  644. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  645. if (access == ARCH_TIMER_PHYS_ACCESS)
  646. cnt = __arch_counter_get_cntpct();
  647. else
  648. cnt = __arch_counter_get_cntvct();
  649. arch_timer_reg_write(access, ARCH_TIMER_REG_CVAL, evt + cnt, clk);
  650. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  651. }
  652. static int arch_timer_set_next_event_virt(unsigned long evt,
  653. struct clock_event_device *clk)
  654. {
  655. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  656. return 0;
  657. }
  658. static int arch_timer_set_next_event_phys(unsigned long evt,
  659. struct clock_event_device *clk)
  660. {
  661. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  662. return 0;
  663. }
  664. static noinstr u64 arch_counter_get_cnt_mem(struct arch_timer *t, int offset_lo)
  665. {
  666. u32 cnt_lo, cnt_hi, tmp_hi;
  667. do {
  668. cnt_hi = __le32_to_cpu((__le32 __force)__raw_readl(t->base + offset_lo + 4));
  669. cnt_lo = __le32_to_cpu((__le32 __force)__raw_readl(t->base + offset_lo));
  670. tmp_hi = __le32_to_cpu((__le32 __force)__raw_readl(t->base + offset_lo + 4));
  671. } while (cnt_hi != tmp_hi);
  672. return ((u64) cnt_hi << 32) | cnt_lo;
  673. }
  674. static __always_inline void set_next_event_mem(const int access, unsigned long evt,
  675. struct clock_event_device *clk)
  676. {
  677. struct arch_timer *timer = to_arch_timer(clk);
  678. unsigned long ctrl;
  679. u64 cnt;
  680. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  681. /* Timer must be disabled before programming CVAL */
  682. if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
  683. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  684. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  685. }
  686. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  687. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  688. if (access == ARCH_TIMER_MEM_VIRT_ACCESS)
  689. cnt = arch_counter_get_cnt_mem(timer, CNTVCT_LO);
  690. else
  691. cnt = arch_counter_get_cnt_mem(timer, CNTPCT_LO);
  692. arch_timer_reg_write(access, ARCH_TIMER_REG_CVAL, evt + cnt, clk);
  693. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  694. }
  695. static int arch_timer_set_next_event_virt_mem(unsigned long evt,
  696. struct clock_event_device *clk)
  697. {
  698. set_next_event_mem(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
  699. return 0;
  700. }
  701. static int arch_timer_set_next_event_phys_mem(unsigned long evt,
  702. struct clock_event_device *clk)
  703. {
  704. set_next_event_mem(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
  705. return 0;
  706. }
  707. static u64 __arch_timer_check_delta(void)
  708. {
  709. #ifdef CONFIG_ARM64
  710. const struct midr_range broken_cval_midrs[] = {
  711. /*
  712. * XGene-1 implements CVAL in terms of TVAL, meaning
  713. * that the maximum timer range is 32bit. Shame on them.
  714. *
  715. * Note that TVAL is signed, thus has only 31 of its
  716. * 32 bits to express magnitude.
  717. */
  718. MIDR_REV_RANGE(MIDR_CPU_MODEL(ARM_CPU_IMP_APM,
  719. APM_CPU_PART_XGENE),
  720. APM_CPU_VAR_POTENZA, 0x0, 0xf),
  721. {},
  722. };
  723. if (is_midr_in_range_list(read_cpuid_id(), broken_cval_midrs)) {
  724. pr_warn_once("Broken CNTx_CVAL_EL1, using 31 bit TVAL instead.\n");
  725. return CLOCKSOURCE_MASK(31);
  726. }
  727. #endif
  728. return CLOCKSOURCE_MASK(arch_counter_get_width());
  729. }
  730. static void __arch_timer_setup(unsigned type,
  731. struct clock_event_device *clk)
  732. {
  733. u64 max_delta;
  734. clk->features = CLOCK_EVT_FEAT_ONESHOT;
  735. if (type == ARCH_TIMER_TYPE_CP15) {
  736. typeof(clk->set_next_event) sne;
  737. arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
  738. if (arch_timer_c3stop)
  739. clk->features |= CLOCK_EVT_FEAT_C3STOP;
  740. clk->name = "arch_sys_timer";
  741. clk->rating = 450;
  742. clk->cpumask = cpumask_of(smp_processor_id());
  743. clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
  744. switch (arch_timer_uses_ppi) {
  745. case ARCH_TIMER_VIRT_PPI:
  746. clk->set_state_shutdown = arch_timer_shutdown_virt;
  747. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
  748. sne = erratum_handler(set_next_event_virt);
  749. break;
  750. case ARCH_TIMER_PHYS_SECURE_PPI:
  751. case ARCH_TIMER_PHYS_NONSECURE_PPI:
  752. case ARCH_TIMER_HYP_PPI:
  753. clk->set_state_shutdown = arch_timer_shutdown_phys;
  754. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
  755. sne = erratum_handler(set_next_event_phys);
  756. break;
  757. default:
  758. BUG();
  759. }
  760. clk->set_next_event = sne;
  761. max_delta = __arch_timer_check_delta();
  762. } else {
  763. clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
  764. clk->name = "arch_mem_timer";
  765. clk->rating = 400;
  766. clk->cpumask = cpu_possible_mask;
  767. if (arch_timer_mem_use_virtual) {
  768. clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
  769. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
  770. clk->set_next_event =
  771. arch_timer_set_next_event_virt_mem;
  772. } else {
  773. clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
  774. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
  775. clk->set_next_event =
  776. arch_timer_set_next_event_phys_mem;
  777. }
  778. max_delta = CLOCKSOURCE_MASK(56);
  779. }
  780. clk->set_state_shutdown(clk);
  781. clockevents_config_and_register(clk, arch_timer_rate, 0xf, max_delta);
  782. }
  783. static void arch_timer_evtstrm_enable(unsigned int divider)
  784. {
  785. u32 cntkctl = arch_timer_get_cntkctl();
  786. #ifdef CONFIG_ARM64
  787. /* ECV is likely to require a large divider. Use the EVNTIS flag. */
  788. if (cpus_have_final_cap(ARM64_HAS_ECV) && divider > 15) {
  789. cntkctl |= ARCH_TIMER_EVT_INTERVAL_SCALE;
  790. divider -= 8;
  791. }
  792. #endif
  793. divider = min(divider, 15U);
  794. cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
  795. /* Set the divider and enable virtual event stream */
  796. cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
  797. | ARCH_TIMER_VIRT_EVT_EN;
  798. arch_timer_set_cntkctl(cntkctl);
  799. arch_timer_set_evtstrm_feature();
  800. cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
  801. }
  802. static void arch_timer_configure_evtstream(void)
  803. {
  804. int evt_stream_div, lsb;
  805. /*
  806. * As the event stream can at most be generated at half the frequency
  807. * of the counter, use half the frequency when computing the divider.
  808. */
  809. evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ / 2;
  810. /*
  811. * Find the closest power of two to the divisor. If the adjacent bit
  812. * of lsb (last set bit, starts from 0) is set, then we use (lsb + 1).
  813. */
  814. lsb = fls(evt_stream_div) - 1;
  815. if (lsb > 0 && (evt_stream_div & BIT(lsb - 1)))
  816. lsb++;
  817. /* enable event stream */
  818. arch_timer_evtstrm_enable(max(0, lsb));
  819. }
  820. static int arch_timer_evtstrm_starting_cpu(unsigned int cpu)
  821. {
  822. arch_timer_configure_evtstream();
  823. return 0;
  824. }
  825. static int arch_timer_evtstrm_dying_cpu(unsigned int cpu)
  826. {
  827. cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
  828. return 0;
  829. }
  830. static int __init arch_timer_evtstrm_register(void)
  831. {
  832. if (!arch_timer_evt || !evtstrm_enable)
  833. return 0;
  834. return cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_EVTSTRM_STARTING,
  835. "clockevents/arm/arch_timer_evtstrm:starting",
  836. arch_timer_evtstrm_starting_cpu,
  837. arch_timer_evtstrm_dying_cpu);
  838. }
  839. core_initcall(arch_timer_evtstrm_register);
  840. static void arch_counter_set_user_access(void)
  841. {
  842. u32 cntkctl = arch_timer_get_cntkctl();
  843. /* Disable user access to the timers and both counters */
  844. /* Also disable virtual event stream */
  845. cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
  846. | ARCH_TIMER_USR_VT_ACCESS_EN
  847. | ARCH_TIMER_USR_VCT_ACCESS_EN
  848. | ARCH_TIMER_VIRT_EVT_EN
  849. | ARCH_TIMER_USR_PCT_ACCESS_EN);
  850. /*
  851. * Enable user access to the virtual counter if it doesn't
  852. * need to be workaround. The vdso may have been already
  853. * disabled though.
  854. */
  855. if (arch_timer_this_cpu_has_cntvct_wa())
  856. pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
  857. else
  858. cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
  859. arch_timer_set_cntkctl(cntkctl);
  860. }
  861. static bool arch_timer_has_nonsecure_ppi(void)
  862. {
  863. return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
  864. arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  865. }
  866. static u32 check_ppi_trigger(int irq)
  867. {
  868. u32 flags = irq_get_trigger_type(irq);
  869. if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
  870. pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
  871. pr_warn("WARNING: Please fix your firmware\n");
  872. flags = IRQF_TRIGGER_LOW;
  873. }
  874. return flags;
  875. }
  876. static int arch_timer_starting_cpu(unsigned int cpu)
  877. {
  878. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  879. u32 flags;
  880. __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
  881. flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
  882. enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
  883. if (arch_timer_has_nonsecure_ppi()) {
  884. flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  885. enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
  886. flags);
  887. }
  888. arch_counter_set_user_access();
  889. return 0;
  890. }
  891. static int validate_timer_rate(void)
  892. {
  893. if (!arch_timer_rate)
  894. return -EINVAL;
  895. /* Arch timer frequency < 1MHz can cause trouble */
  896. WARN_ON(arch_timer_rate < 1000000);
  897. return 0;
  898. }
  899. /*
  900. * For historical reasons, when probing with DT we use whichever (non-zero)
  901. * rate was probed first, and don't verify that others match. If the first node
  902. * probed has a clock-frequency property, this overrides the HW register.
  903. */
  904. static void __init arch_timer_of_configure_rate(u32 rate, struct device_node *np)
  905. {
  906. /* Who has more than one independent system counter? */
  907. if (arch_timer_rate)
  908. return;
  909. if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
  910. arch_timer_rate = rate;
  911. /* Check the timer frequency. */
  912. if (validate_timer_rate())
  913. pr_warn("frequency not available\n");
  914. }
  915. static void __init arch_timer_banner(unsigned type)
  916. {
  917. pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
  918. type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
  919. type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
  920. " and " : "",
  921. type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
  922. (unsigned long)arch_timer_rate / 1000000,
  923. (unsigned long)(arch_timer_rate / 10000) % 100,
  924. type & ARCH_TIMER_TYPE_CP15 ?
  925. (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
  926. "",
  927. type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
  928. type & ARCH_TIMER_TYPE_MEM ?
  929. arch_timer_mem_use_virtual ? "virt" : "phys" :
  930. "");
  931. }
  932. u32 arch_timer_get_rate(void)
  933. {
  934. return arch_timer_rate;
  935. }
  936. bool arch_timer_evtstrm_available(void)
  937. {
  938. /*
  939. * We might get called from a preemptible context. This is fine
  940. * because availability of the event stream should be always the same
  941. * for a preemptible context and context where we might resume a task.
  942. */
  943. return cpumask_test_cpu(raw_smp_processor_id(), &evtstrm_available);
  944. }
  945. static noinstr u64 arch_counter_get_cntvct_mem(void)
  946. {
  947. return arch_counter_get_cnt_mem(arch_timer_mem, CNTVCT_LO);
  948. }
  949. static struct arch_timer_kvm_info arch_timer_kvm_info;
  950. struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
  951. {
  952. return &arch_timer_kvm_info;
  953. }
  954. static void __init arch_counter_register(unsigned type)
  955. {
  956. u64 (*scr)(void);
  957. u64 start_count;
  958. int width;
  959. /* Register the CP15 based counter if we have one */
  960. if (type & ARCH_TIMER_TYPE_CP15) {
  961. u64 (*rd)(void);
  962. if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
  963. arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) {
  964. if (arch_timer_counter_has_wa()) {
  965. rd = arch_counter_get_cntvct_stable;
  966. scr = raw_counter_get_cntvct_stable;
  967. } else {
  968. rd = arch_counter_get_cntvct;
  969. scr = arch_counter_get_cntvct;
  970. }
  971. } else {
  972. if (arch_timer_counter_has_wa()) {
  973. rd = arch_counter_get_cntpct_stable;
  974. scr = raw_counter_get_cntpct_stable;
  975. } else {
  976. rd = arch_counter_get_cntpct;
  977. scr = arch_counter_get_cntpct;
  978. }
  979. }
  980. arch_timer_read_counter = rd;
  981. clocksource_counter.vdso_clock_mode = vdso_default;
  982. } else {
  983. arch_timer_read_counter = arch_counter_get_cntvct_mem;
  984. scr = arch_counter_get_cntvct_mem;
  985. }
  986. width = arch_counter_get_width();
  987. clocksource_counter.mask = CLOCKSOURCE_MASK(width);
  988. cyclecounter.mask = CLOCKSOURCE_MASK(width);
  989. if (!arch_counter_suspend_stop)
  990. clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
  991. start_count = arch_timer_read_counter();
  992. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  993. cyclecounter.mult = clocksource_counter.mult;
  994. cyclecounter.shift = clocksource_counter.shift;
  995. timecounter_init(&arch_timer_kvm_info.timecounter,
  996. &cyclecounter, start_count);
  997. sched_clock_register(scr, width, arch_timer_rate);
  998. }
  999. static void arch_timer_stop(struct clock_event_device *clk)
  1000. {
  1001. pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
  1002. disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
  1003. if (arch_timer_has_nonsecure_ppi())
  1004. disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  1005. clk->set_state_shutdown(clk);
  1006. }
  1007. static int arch_timer_dying_cpu(unsigned int cpu)
  1008. {
  1009. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  1010. arch_timer_stop(clk);
  1011. return 0;
  1012. }
  1013. #ifdef CONFIG_CPU_PM
  1014. static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
  1015. static int arch_timer_cpu_pm_notify(struct notifier_block *self,
  1016. unsigned long action, void *hcpu)
  1017. {
  1018. if (action == CPU_PM_ENTER) {
  1019. __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
  1020. cpumask_clear_cpu(smp_processor_id(), &evtstrm_available);
  1021. } else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) {
  1022. arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
  1023. if (arch_timer_have_evtstrm_feature())
  1024. cpumask_set_cpu(smp_processor_id(), &evtstrm_available);
  1025. }
  1026. return NOTIFY_OK;
  1027. }
  1028. static struct notifier_block arch_timer_cpu_pm_notifier = {
  1029. .notifier_call = arch_timer_cpu_pm_notify,
  1030. };
  1031. static int __init arch_timer_cpu_pm_init(void)
  1032. {
  1033. return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
  1034. }
  1035. static void __init arch_timer_cpu_pm_deinit(void)
  1036. {
  1037. WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
  1038. }
  1039. #else
  1040. static int __init arch_timer_cpu_pm_init(void)
  1041. {
  1042. return 0;
  1043. }
  1044. static void __init arch_timer_cpu_pm_deinit(void)
  1045. {
  1046. }
  1047. #endif
  1048. static int __init arch_timer_register(void)
  1049. {
  1050. int err;
  1051. int ppi;
  1052. arch_timer_evt = alloc_percpu(struct clock_event_device);
  1053. if (!arch_timer_evt) {
  1054. err = -ENOMEM;
  1055. goto out;
  1056. }
  1057. ppi = arch_timer_ppi[arch_timer_uses_ppi];
  1058. switch (arch_timer_uses_ppi) {
  1059. case ARCH_TIMER_VIRT_PPI:
  1060. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  1061. "arch_timer", arch_timer_evt);
  1062. break;
  1063. case ARCH_TIMER_PHYS_SECURE_PPI:
  1064. case ARCH_TIMER_PHYS_NONSECURE_PPI:
  1065. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  1066. "arch_timer", arch_timer_evt);
  1067. if (!err && arch_timer_has_nonsecure_ppi()) {
  1068. ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
  1069. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  1070. "arch_timer", arch_timer_evt);
  1071. if (err)
  1072. free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
  1073. arch_timer_evt);
  1074. }
  1075. break;
  1076. case ARCH_TIMER_HYP_PPI:
  1077. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  1078. "arch_timer", arch_timer_evt);
  1079. break;
  1080. default:
  1081. BUG();
  1082. }
  1083. if (err) {
  1084. pr_err("can't register interrupt %d (%d)\n", ppi, err);
  1085. goto out_free;
  1086. }
  1087. err = arch_timer_cpu_pm_init();
  1088. if (err)
  1089. goto out_unreg_notify;
  1090. /* Register and immediately configure the timer on the boot CPU */
  1091. err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
  1092. "clockevents/arm/arch_timer:starting",
  1093. arch_timer_starting_cpu, arch_timer_dying_cpu);
  1094. if (err)
  1095. goto out_unreg_cpupm;
  1096. return 0;
  1097. out_unreg_cpupm:
  1098. arch_timer_cpu_pm_deinit();
  1099. out_unreg_notify:
  1100. free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
  1101. if (arch_timer_has_nonsecure_ppi())
  1102. free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
  1103. arch_timer_evt);
  1104. out_free:
  1105. free_percpu(arch_timer_evt);
  1106. arch_timer_evt = NULL;
  1107. out:
  1108. return err;
  1109. }
  1110. static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
  1111. {
  1112. int ret;
  1113. irq_handler_t func;
  1114. arch_timer_mem = kzalloc(sizeof(*arch_timer_mem), GFP_KERNEL);
  1115. if (!arch_timer_mem)
  1116. return -ENOMEM;
  1117. arch_timer_mem->base = base;
  1118. arch_timer_mem->evt.irq = irq;
  1119. __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &arch_timer_mem->evt);
  1120. if (arch_timer_mem_use_virtual)
  1121. func = arch_timer_handler_virt_mem;
  1122. else
  1123. func = arch_timer_handler_phys_mem;
  1124. ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &arch_timer_mem->evt);
  1125. if (ret) {
  1126. pr_err("Failed to request mem timer irq\n");
  1127. kfree(arch_timer_mem);
  1128. arch_timer_mem = NULL;
  1129. }
  1130. return ret;
  1131. }
  1132. static const struct of_device_id arch_timer_of_match[] __initconst = {
  1133. { .compatible = "arm,armv7-timer", },
  1134. { .compatible = "arm,armv8-timer", },
  1135. {},
  1136. };
  1137. static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
  1138. { .compatible = "arm,armv7-timer-mem", },
  1139. {},
  1140. };
  1141. static bool __init arch_timer_needs_of_probing(void)
  1142. {
  1143. struct device_node *dn;
  1144. bool needs_probing = false;
  1145. unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
  1146. /* We have two timers, and both device-tree nodes are probed. */
  1147. if ((arch_timers_present & mask) == mask)
  1148. return false;
  1149. /*
  1150. * Only one type of timer is probed,
  1151. * check if we have another type of timer node in device-tree.
  1152. */
  1153. if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
  1154. dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
  1155. else
  1156. dn = of_find_matching_node(NULL, arch_timer_of_match);
  1157. if (dn && of_device_is_available(dn))
  1158. needs_probing = true;
  1159. of_node_put(dn);
  1160. return needs_probing;
  1161. }
  1162. static int __init arch_timer_common_init(void)
  1163. {
  1164. arch_timer_banner(arch_timers_present);
  1165. arch_counter_register(arch_timers_present);
  1166. return arch_timer_arch_init();
  1167. }
  1168. /**
  1169. * arch_timer_select_ppi() - Select suitable PPI for the current system.
  1170. *
  1171. * If HYP mode is available, we know that the physical timer
  1172. * has been configured to be accessible from PL1. Use it, so
  1173. * that a guest can use the virtual timer instead.
  1174. *
  1175. * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
  1176. * accesses to CNTP_*_EL1 registers are silently redirected to
  1177. * their CNTHP_*_EL2 counterparts, and use a different PPI
  1178. * number.
  1179. *
  1180. * If no interrupt provided for virtual timer, we'll have to
  1181. * stick to the physical timer. It'd better be accessible...
  1182. * For arm64 we never use the secure interrupt.
  1183. *
  1184. * Return: a suitable PPI type for the current system.
  1185. */
  1186. static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
  1187. {
  1188. if (is_kernel_in_hyp_mode())
  1189. return ARCH_TIMER_HYP_PPI;
  1190. if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
  1191. return ARCH_TIMER_VIRT_PPI;
  1192. if (IS_ENABLED(CONFIG_ARM64))
  1193. return ARCH_TIMER_PHYS_NONSECURE_PPI;
  1194. return ARCH_TIMER_PHYS_SECURE_PPI;
  1195. }
  1196. static void __init arch_timer_populate_kvm_info(void)
  1197. {
  1198. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
  1199. if (is_kernel_in_hyp_mode())
  1200. arch_timer_kvm_info.physical_irq = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
  1201. }
  1202. static int __init arch_timer_of_init(struct device_node *np)
  1203. {
  1204. int i, irq, ret;
  1205. u32 rate;
  1206. bool has_names;
  1207. if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
  1208. pr_warn("multiple nodes in dt, skipping\n");
  1209. return 0;
  1210. }
  1211. arch_timers_present |= ARCH_TIMER_TYPE_CP15;
  1212. has_names = of_property_read_bool(np, "interrupt-names");
  1213. for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++) {
  1214. if (has_names)
  1215. irq = of_irq_get_byname(np, arch_timer_ppi_names[i]);
  1216. else
  1217. irq = of_irq_get(np, i);
  1218. if (irq > 0)
  1219. arch_timer_ppi[i] = irq;
  1220. }
  1221. arch_timer_populate_kvm_info();
  1222. rate = arch_timer_get_cntfrq();
  1223. arch_timer_of_configure_rate(rate, np);
  1224. arch_timer_c3stop = !of_property_read_bool(np, "always-on");
  1225. /* Check for globally applicable workarounds */
  1226. arch_timer_check_ool_workaround(ate_match_dt, np);
  1227. /*
  1228. * If we cannot rely on firmware initializing the timer registers then
  1229. * we should use the physical timers instead.
  1230. */
  1231. if (IS_ENABLED(CONFIG_ARM) &&
  1232. of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  1233. arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
  1234. else
  1235. arch_timer_uses_ppi = arch_timer_select_ppi();
  1236. if (!arch_timer_ppi[arch_timer_uses_ppi]) {
  1237. pr_err("No interrupt available, giving up\n");
  1238. return -EINVAL;
  1239. }
  1240. /* On some systems, the counter stops ticking when in suspend. */
  1241. arch_counter_suspend_stop = of_property_read_bool(np,
  1242. "arm,no-tick-in-suspend");
  1243. ret = arch_timer_register();
  1244. if (ret)
  1245. return ret;
  1246. if (arch_timer_needs_of_probing())
  1247. return 0;
  1248. return arch_timer_common_init();
  1249. }
  1250. TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
  1251. TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
  1252. static u32 __init
  1253. arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
  1254. {
  1255. void __iomem *base;
  1256. u32 rate;
  1257. base = ioremap(frame->cntbase, frame->size);
  1258. if (!base) {
  1259. pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
  1260. return 0;
  1261. }
  1262. rate = readl_relaxed(base + CNTFRQ);
  1263. iounmap(base);
  1264. return rate;
  1265. }
  1266. static struct arch_timer_mem_frame * __init
  1267. arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
  1268. {
  1269. struct arch_timer_mem_frame *frame, *best_frame = NULL;
  1270. void __iomem *cntctlbase;
  1271. u32 cnttidr;
  1272. int i;
  1273. cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
  1274. if (!cntctlbase) {
  1275. pr_err("Can't map CNTCTLBase @ %pa\n",
  1276. &timer_mem->cntctlbase);
  1277. return NULL;
  1278. }
  1279. cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
  1280. /*
  1281. * Try to find a virtual capable frame. Otherwise fall back to a
  1282. * physical capable frame.
  1283. */
  1284. for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  1285. u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
  1286. CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
  1287. frame = &timer_mem->frame[i];
  1288. if (!frame->valid)
  1289. continue;
  1290. /* Try enabling everything, and see what sticks */
  1291. writel_relaxed(cntacr, cntctlbase + CNTACR(i));
  1292. cntacr = readl_relaxed(cntctlbase + CNTACR(i));
  1293. if ((cnttidr & CNTTIDR_VIRT(i)) &&
  1294. !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
  1295. best_frame = frame;
  1296. arch_timer_mem_use_virtual = true;
  1297. break;
  1298. }
  1299. if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
  1300. continue;
  1301. best_frame = frame;
  1302. }
  1303. iounmap(cntctlbase);
  1304. return best_frame;
  1305. }
  1306. static int __init
  1307. arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
  1308. {
  1309. void __iomem *base;
  1310. int ret, irq;
  1311. if (arch_timer_mem_use_virtual)
  1312. irq = frame->virt_irq;
  1313. else
  1314. irq = frame->phys_irq;
  1315. if (!irq) {
  1316. pr_err("Frame missing %s irq.\n",
  1317. arch_timer_mem_use_virtual ? "virt" : "phys");
  1318. return -EINVAL;
  1319. }
  1320. if (!request_mem_region(frame->cntbase, frame->size,
  1321. "arch_mem_timer"))
  1322. return -EBUSY;
  1323. base = ioremap(frame->cntbase, frame->size);
  1324. if (!base) {
  1325. pr_err("Can't map frame's registers\n");
  1326. return -ENXIO;
  1327. }
  1328. ret = arch_timer_mem_register(base, irq);
  1329. if (ret) {
  1330. iounmap(base);
  1331. return ret;
  1332. }
  1333. arch_timers_present |= ARCH_TIMER_TYPE_MEM;
  1334. return 0;
  1335. }
  1336. static int __init arch_timer_mem_of_init(struct device_node *np)
  1337. {
  1338. struct arch_timer_mem *timer_mem;
  1339. struct arch_timer_mem_frame *frame;
  1340. struct resource res;
  1341. int ret = -EINVAL;
  1342. u32 rate;
  1343. timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
  1344. if (!timer_mem)
  1345. return -ENOMEM;
  1346. if (of_address_to_resource(np, 0, &res))
  1347. goto out;
  1348. timer_mem->cntctlbase = res.start;
  1349. timer_mem->size = resource_size(&res);
  1350. for_each_available_child_of_node_scoped(np, frame_node) {
  1351. u32 n;
  1352. struct arch_timer_mem_frame *frame;
  1353. if (of_property_read_u32(frame_node, "frame-number", &n)) {
  1354. pr_err(FW_BUG "Missing frame-number.\n");
  1355. goto out;
  1356. }
  1357. if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
  1358. pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
  1359. ARCH_TIMER_MEM_MAX_FRAMES - 1);
  1360. goto out;
  1361. }
  1362. frame = &timer_mem->frame[n];
  1363. if (frame->valid) {
  1364. pr_err(FW_BUG "Duplicated frame-number.\n");
  1365. goto out;
  1366. }
  1367. if (of_address_to_resource(frame_node, 0, &res))
  1368. goto out;
  1369. frame->cntbase = res.start;
  1370. frame->size = resource_size(&res);
  1371. frame->virt_irq = irq_of_parse_and_map(frame_node,
  1372. ARCH_TIMER_VIRT_SPI);
  1373. frame->phys_irq = irq_of_parse_and_map(frame_node,
  1374. ARCH_TIMER_PHYS_SPI);
  1375. frame->valid = true;
  1376. }
  1377. frame = arch_timer_mem_find_best_frame(timer_mem);
  1378. if (!frame) {
  1379. pr_err("Unable to find a suitable frame in timer @ %pa\n",
  1380. &timer_mem->cntctlbase);
  1381. ret = -EINVAL;
  1382. goto out;
  1383. }
  1384. rate = arch_timer_mem_frame_get_cntfrq(frame);
  1385. arch_timer_of_configure_rate(rate, np);
  1386. ret = arch_timer_mem_frame_register(frame);
  1387. if (!ret && !arch_timer_needs_of_probing())
  1388. ret = arch_timer_common_init();
  1389. out:
  1390. kfree(timer_mem);
  1391. return ret;
  1392. }
  1393. TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
  1394. arch_timer_mem_of_init);
  1395. #ifdef CONFIG_ACPI_GTDT
  1396. static int __init
  1397. arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
  1398. {
  1399. struct arch_timer_mem_frame *frame;
  1400. u32 rate;
  1401. int i;
  1402. for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  1403. frame = &timer_mem->frame[i];
  1404. if (!frame->valid)
  1405. continue;
  1406. rate = arch_timer_mem_frame_get_cntfrq(frame);
  1407. if (rate == arch_timer_rate)
  1408. continue;
  1409. pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
  1410. &frame->cntbase,
  1411. (unsigned long)rate, (unsigned long)arch_timer_rate);
  1412. return -EINVAL;
  1413. }
  1414. return 0;
  1415. }
  1416. static int __init arch_timer_mem_acpi_init(int platform_timer_count)
  1417. {
  1418. struct arch_timer_mem *timers, *timer;
  1419. struct arch_timer_mem_frame *frame, *best_frame = NULL;
  1420. int timer_count, i, ret = 0;
  1421. timers = kcalloc(platform_timer_count, sizeof(*timers),
  1422. GFP_KERNEL);
  1423. if (!timers)
  1424. return -ENOMEM;
  1425. ret = acpi_arch_timer_mem_init(timers, &timer_count);
  1426. if (ret || !timer_count)
  1427. goto out;
  1428. /*
  1429. * While unlikely, it's theoretically possible that none of the frames
  1430. * in a timer expose the combination of feature we want.
  1431. */
  1432. for (i = 0; i < timer_count; i++) {
  1433. timer = &timers[i];
  1434. frame = arch_timer_mem_find_best_frame(timer);
  1435. if (!best_frame)
  1436. best_frame = frame;
  1437. ret = arch_timer_mem_verify_cntfrq(timer);
  1438. if (ret) {
  1439. pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
  1440. goto out;
  1441. }
  1442. if (!best_frame) /* implies !frame */
  1443. /*
  1444. * Only complain about missing suitable frames if we
  1445. * haven't already found one in a previous iteration.
  1446. */
  1447. pr_err("Unable to find a suitable frame in timer @ %pa\n",
  1448. &timer->cntctlbase);
  1449. }
  1450. if (best_frame)
  1451. ret = arch_timer_mem_frame_register(best_frame);
  1452. out:
  1453. kfree(timers);
  1454. return ret;
  1455. }
  1456. /* Initialize per-processor generic timer and memory-mapped timer(if present) */
  1457. static int __init arch_timer_acpi_init(struct acpi_table_header *table)
  1458. {
  1459. int ret, platform_timer_count;
  1460. if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
  1461. pr_warn("already initialized, skipping\n");
  1462. return -EINVAL;
  1463. }
  1464. arch_timers_present |= ARCH_TIMER_TYPE_CP15;
  1465. ret = acpi_gtdt_init(table, &platform_timer_count);
  1466. if (ret)
  1467. return ret;
  1468. arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
  1469. acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
  1470. arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
  1471. acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
  1472. arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
  1473. acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
  1474. arch_timer_populate_kvm_info();
  1475. /*
  1476. * When probing via ACPI, we have no mechanism to override the sysreg
  1477. * CNTFRQ value. This *must* be correct.
  1478. */
  1479. arch_timer_rate = arch_timer_get_cntfrq();
  1480. ret = validate_timer_rate();
  1481. if (ret) {
  1482. pr_err(FW_BUG "frequency not available.\n");
  1483. return ret;
  1484. }
  1485. arch_timer_uses_ppi = arch_timer_select_ppi();
  1486. if (!arch_timer_ppi[arch_timer_uses_ppi]) {
  1487. pr_err("No interrupt available, giving up\n");
  1488. return -EINVAL;
  1489. }
  1490. /* Always-on capability */
  1491. arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
  1492. /* Check for globally applicable workarounds */
  1493. arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
  1494. ret = arch_timer_register();
  1495. if (ret)
  1496. return ret;
  1497. if (platform_timer_count &&
  1498. arch_timer_mem_acpi_init(platform_timer_count))
  1499. pr_err("Failed to initialize memory-mapped timer.\n");
  1500. return arch_timer_common_init();
  1501. }
  1502. TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
  1503. #endif
  1504. int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *ts,
  1505. enum clocksource_ids *cs_id)
  1506. {
  1507. struct arm_smccc_res hvc_res;
  1508. u32 ptp_counter;
  1509. ktime_t ktime;
  1510. if (!IS_ENABLED(CONFIG_HAVE_ARM_SMCCC_DISCOVERY))
  1511. return -EOPNOTSUPP;
  1512. if (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
  1513. ptp_counter = KVM_PTP_VIRT_COUNTER;
  1514. else
  1515. ptp_counter = KVM_PTP_PHYS_COUNTER;
  1516. arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID,
  1517. ptp_counter, &hvc_res);
  1518. if ((int)(hvc_res.a0) < 0)
  1519. return -EOPNOTSUPP;
  1520. ktime = (u64)hvc_res.a0 << 32 | hvc_res.a1;
  1521. *ts = ktime_to_timespec64(ktime);
  1522. if (cycle)
  1523. *cycle = (u64)hvc_res.a2 << 32 | hvc_res.a3;
  1524. if (cs_id)
  1525. *cs_id = CSID_ARM_ARCH_COUNTER;
  1526. return 0;
  1527. }
  1528. EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_crosststamp);