arm_arch_timer.c 41 KB

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