arm_arch_timer.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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. #define CNTTIDR 0x08
  32. #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
  33. #define CNTACR(n) (0x40 + ((n) * 4))
  34. #define CNTACR_RPCT BIT(0)
  35. #define CNTACR_RVCT BIT(1)
  36. #define CNTACR_RFRQ BIT(2)
  37. #define CNTACR_RVOFF BIT(3)
  38. #define CNTACR_RWVT BIT(4)
  39. #define CNTACR_RWPT BIT(5)
  40. #define CNTVCT_LO 0x08
  41. #define CNTVCT_HI 0x0c
  42. #define CNTFRQ 0x10
  43. #define CNTP_TVAL 0x28
  44. #define CNTP_CTL 0x2c
  45. #define CNTV_TVAL 0x38
  46. #define CNTV_CTL 0x3c
  47. #define ARCH_CP15_TIMER BIT(0)
  48. #define ARCH_MEM_TIMER BIT(1)
  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. enum ppi_nr {
  58. PHYS_SECURE_PPI,
  59. PHYS_NONSECURE_PPI,
  60. VIRT_PPI,
  61. HYP_PPI,
  62. MAX_TIMER_PPI
  63. };
  64. static int arch_timer_ppi[MAX_TIMER_PPI];
  65. static struct clock_event_device __percpu *arch_timer_evt;
  66. static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI;
  67. static bool arch_timer_c3stop;
  68. static bool arch_timer_mem_use_virtual;
  69. static bool arch_counter_suspend_stop;
  70. static bool vdso_default = true;
  71. static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
  72. static int __init early_evtstrm_cfg(char *buf)
  73. {
  74. return strtobool(buf, &evtstrm_enable);
  75. }
  76. early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
  77. /*
  78. * Architected system timer support.
  79. */
  80. static __always_inline
  81. void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
  82. struct clock_event_device *clk)
  83. {
  84. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  85. struct arch_timer *timer = to_arch_timer(clk);
  86. switch (reg) {
  87. case ARCH_TIMER_REG_CTRL:
  88. writel_relaxed(val, timer->base + CNTP_CTL);
  89. break;
  90. case ARCH_TIMER_REG_TVAL:
  91. writel_relaxed(val, timer->base + CNTP_TVAL);
  92. break;
  93. }
  94. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  95. struct arch_timer *timer = to_arch_timer(clk);
  96. switch (reg) {
  97. case ARCH_TIMER_REG_CTRL:
  98. writel_relaxed(val, timer->base + CNTV_CTL);
  99. break;
  100. case ARCH_TIMER_REG_TVAL:
  101. writel_relaxed(val, timer->base + CNTV_TVAL);
  102. break;
  103. }
  104. } else {
  105. arch_timer_reg_write_cp15(access, reg, val);
  106. }
  107. }
  108. static __always_inline
  109. u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
  110. struct clock_event_device *clk)
  111. {
  112. u32 val;
  113. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  114. struct arch_timer *timer = to_arch_timer(clk);
  115. switch (reg) {
  116. case ARCH_TIMER_REG_CTRL:
  117. val = readl_relaxed(timer->base + CNTP_CTL);
  118. break;
  119. case ARCH_TIMER_REG_TVAL:
  120. val = readl_relaxed(timer->base + CNTP_TVAL);
  121. break;
  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. val = readl_relaxed(timer->base + CNTV_CTL);
  128. break;
  129. case ARCH_TIMER_REG_TVAL:
  130. val = readl_relaxed(timer->base + CNTV_TVAL);
  131. break;
  132. }
  133. } else {
  134. val = arch_timer_reg_read_cp15(access, reg);
  135. }
  136. return val;
  137. }
  138. /*
  139. * Default to cp15 based access because arm64 uses this function for
  140. * sched_clock() before DT is probed and the cp15 method is guaranteed
  141. * to exist on arm64. arm doesn't use this before DT is probed so even
  142. * if we don't have the cp15 accessors we won't have a problem.
  143. */
  144. u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
  145. static u64 arch_counter_read(struct clocksource *cs)
  146. {
  147. return arch_timer_read_counter();
  148. }
  149. static u64 arch_counter_read_cc(const struct cyclecounter *cc)
  150. {
  151. return arch_timer_read_counter();
  152. }
  153. static struct clocksource clocksource_counter = {
  154. .name = "arch_sys_counter",
  155. .rating = 400,
  156. .read = arch_counter_read,
  157. .mask = CLOCKSOURCE_MASK(56),
  158. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  159. };
  160. static struct cyclecounter cyclecounter __ro_after_init = {
  161. .read = arch_counter_read_cc,
  162. .mask = CLOCKSOURCE_MASK(56),
  163. };
  164. struct ate_acpi_oem_info {
  165. char oem_id[ACPI_OEM_ID_SIZE + 1];
  166. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
  167. u32 oem_revision;
  168. };
  169. #ifdef CONFIG_FSL_ERRATUM_A008585
  170. /*
  171. * The number of retries is an arbitrary value well beyond the highest number
  172. * of iterations the loop has been observed to take.
  173. */
  174. #define __fsl_a008585_read_reg(reg) ({ \
  175. u64 _old, _new; \
  176. int _retries = 200; \
  177. \
  178. do { \
  179. _old = read_sysreg(reg); \
  180. _new = read_sysreg(reg); \
  181. _retries--; \
  182. } while (unlikely(_old != _new) && _retries); \
  183. \
  184. WARN_ON_ONCE(!_retries); \
  185. _new; \
  186. })
  187. static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
  188. {
  189. return __fsl_a008585_read_reg(cntp_tval_el0);
  190. }
  191. static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
  192. {
  193. return __fsl_a008585_read_reg(cntv_tval_el0);
  194. }
  195. static u64 notrace fsl_a008585_read_cntvct_el0(void)
  196. {
  197. return __fsl_a008585_read_reg(cntvct_el0);
  198. }
  199. #endif
  200. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  201. /*
  202. * Verify whether the value of the second read is larger than the first by
  203. * less than 32 is the only way to confirm the value is correct, so clear the
  204. * lower 5 bits to check whether the difference is greater than 32 or not.
  205. * Theoretically the erratum should not occur more than twice in succession
  206. * when reading the system counter, but it is possible that some interrupts
  207. * may lead to more than twice read errors, triggering the warning, so setting
  208. * the number of retries far beyond the number of iterations the loop has been
  209. * observed to take.
  210. */
  211. #define __hisi_161010101_read_reg(reg) ({ \
  212. u64 _old, _new; \
  213. int _retries = 50; \
  214. \
  215. do { \
  216. _old = read_sysreg(reg); \
  217. _new = read_sysreg(reg); \
  218. _retries--; \
  219. } while (unlikely((_new - _old) >> 5) && _retries); \
  220. \
  221. WARN_ON_ONCE(!_retries); \
  222. _new; \
  223. })
  224. static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
  225. {
  226. return __hisi_161010101_read_reg(cntp_tval_el0);
  227. }
  228. static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
  229. {
  230. return __hisi_161010101_read_reg(cntv_tval_el0);
  231. }
  232. static u64 notrace hisi_161010101_read_cntvct_el0(void)
  233. {
  234. return __hisi_161010101_read_reg(cntvct_el0);
  235. }
  236. static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
  237. /*
  238. * Note that trailing spaces are required to properly match
  239. * the OEM table information.
  240. */
  241. {
  242. .oem_id = "HISI ",
  243. .oem_table_id = "HIP05 ",
  244. .oem_revision = 0,
  245. },
  246. {
  247. .oem_id = "HISI ",
  248. .oem_table_id = "HIP06 ",
  249. .oem_revision = 0,
  250. },
  251. {
  252. .oem_id = "HISI ",
  253. .oem_table_id = "HIP07 ",
  254. .oem_revision = 0,
  255. },
  256. { /* Sentinel indicating the end of the OEM array */ },
  257. };
  258. #endif
  259. #ifdef CONFIG_ARM64_ERRATUM_858921
  260. static u64 notrace arm64_858921_read_cntvct_el0(void)
  261. {
  262. u64 old, new;
  263. old = read_sysreg(cntvct_el0);
  264. new = read_sysreg(cntvct_el0);
  265. return (((old ^ new) >> 32) & 1) ? old : new;
  266. }
  267. #endif
  268. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  269. DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
  270. timer_unstable_counter_workaround);
  271. EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
  272. DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
  273. EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
  274. static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
  275. struct clock_event_device *clk)
  276. {
  277. unsigned long ctrl;
  278. u64 cval = evt + arch_counter_get_cntvct();
  279. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  280. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  281. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  282. if (access == ARCH_TIMER_PHYS_ACCESS)
  283. write_sysreg(cval, cntp_cval_el0);
  284. else
  285. write_sysreg(cval, cntv_cval_el0);
  286. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  287. }
  288. static int erratum_set_next_event_tval_virt(unsigned long evt,
  289. struct clock_event_device *clk)
  290. {
  291. erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  292. return 0;
  293. }
  294. static int erratum_set_next_event_tval_phys(unsigned long evt,
  295. struct clock_event_device *clk)
  296. {
  297. erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  298. return 0;
  299. }
  300. static const struct arch_timer_erratum_workaround ool_workarounds[] = {
  301. #ifdef CONFIG_FSL_ERRATUM_A008585
  302. {
  303. .match_type = ate_match_dt,
  304. .id = "fsl,erratum-a008585",
  305. .desc = "Freescale erratum a005858",
  306. .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
  307. .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
  308. .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
  309. .set_next_event_phys = erratum_set_next_event_tval_phys,
  310. .set_next_event_virt = erratum_set_next_event_tval_virt,
  311. },
  312. #endif
  313. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  314. {
  315. .match_type = ate_match_dt,
  316. .id = "hisilicon,erratum-161010101",
  317. .desc = "HiSilicon erratum 161010101",
  318. .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
  319. .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
  320. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  321. .set_next_event_phys = erratum_set_next_event_tval_phys,
  322. .set_next_event_virt = erratum_set_next_event_tval_virt,
  323. },
  324. {
  325. .match_type = ate_match_acpi_oem_info,
  326. .id = hisi_161010101_oem_info,
  327. .desc = "HiSilicon erratum 161010101",
  328. .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
  329. .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
  330. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  331. .set_next_event_phys = erratum_set_next_event_tval_phys,
  332. .set_next_event_virt = erratum_set_next_event_tval_virt,
  333. },
  334. #endif
  335. #ifdef CONFIG_ARM64_ERRATUM_858921
  336. {
  337. .match_type = ate_match_local_cap_id,
  338. .id = (void *)ARM64_WORKAROUND_858921,
  339. .desc = "ARM erratum 858921",
  340. .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
  341. },
  342. #endif
  343. };
  344. typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
  345. const void *);
  346. static
  347. bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
  348. const void *arg)
  349. {
  350. const struct device_node *np = arg;
  351. return of_property_read_bool(np, wa->id);
  352. }
  353. static
  354. bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
  355. const void *arg)
  356. {
  357. return this_cpu_has_cap((uintptr_t)wa->id);
  358. }
  359. static
  360. bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
  361. const void *arg)
  362. {
  363. static const struct ate_acpi_oem_info empty_oem_info = {};
  364. const struct ate_acpi_oem_info *info = wa->id;
  365. const struct acpi_table_header *table = arg;
  366. /* Iterate over the ACPI OEM info array, looking for a match */
  367. while (memcmp(info, &empty_oem_info, sizeof(*info))) {
  368. if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
  369. !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
  370. info->oem_revision == table->oem_revision)
  371. return true;
  372. info++;
  373. }
  374. return false;
  375. }
  376. static const struct arch_timer_erratum_workaround *
  377. arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
  378. ate_match_fn_t match_fn,
  379. void *arg)
  380. {
  381. int i;
  382. for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
  383. if (ool_workarounds[i].match_type != type)
  384. continue;
  385. if (match_fn(&ool_workarounds[i], arg))
  386. return &ool_workarounds[i];
  387. }
  388. return NULL;
  389. }
  390. static
  391. void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
  392. bool local)
  393. {
  394. int i;
  395. if (local) {
  396. __this_cpu_write(timer_unstable_counter_workaround, wa);
  397. } else {
  398. for_each_possible_cpu(i)
  399. per_cpu(timer_unstable_counter_workaround, i) = wa;
  400. }
  401. static_branch_enable(&arch_timer_read_ool_enabled);
  402. /*
  403. * Don't use the vdso fastpath if errata require using the
  404. * out-of-line counter accessor. We may change our mind pretty
  405. * late in the game (with a per-CPU erratum, for example), so
  406. * change both the default value and the vdso itself.
  407. */
  408. if (wa->read_cntvct_el0) {
  409. clocksource_counter.archdata.vdso_direct = false;
  410. vdso_default = false;
  411. }
  412. }
  413. static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
  414. void *arg)
  415. {
  416. const struct arch_timer_erratum_workaround *wa;
  417. ate_match_fn_t match_fn = NULL;
  418. bool local = false;
  419. switch (type) {
  420. case ate_match_dt:
  421. match_fn = arch_timer_check_dt_erratum;
  422. break;
  423. case ate_match_local_cap_id:
  424. match_fn = arch_timer_check_local_cap_erratum;
  425. local = true;
  426. break;
  427. case ate_match_acpi_oem_info:
  428. match_fn = arch_timer_check_acpi_oem_erratum;
  429. break;
  430. default:
  431. WARN_ON(1);
  432. return;
  433. }
  434. wa = arch_timer_iterate_errata(type, match_fn, arg);
  435. if (!wa)
  436. return;
  437. if (needs_unstable_timer_counter_workaround()) {
  438. const struct arch_timer_erratum_workaround *__wa;
  439. __wa = __this_cpu_read(timer_unstable_counter_workaround);
  440. if (__wa && wa != __wa)
  441. pr_warn("Can't enable workaround for %s (clashes with %s\n)",
  442. wa->desc, __wa->desc);
  443. if (__wa)
  444. return;
  445. }
  446. arch_timer_enable_workaround(wa, local);
  447. pr_info("Enabling %s workaround for %s\n",
  448. local ? "local" : "global", wa->desc);
  449. }
  450. #define erratum_handler(fn, r, ...) \
  451. ({ \
  452. bool __val; \
  453. if (needs_unstable_timer_counter_workaround()) { \
  454. const struct arch_timer_erratum_workaround *__wa; \
  455. __wa = __this_cpu_read(timer_unstable_counter_workaround); \
  456. if (__wa && __wa->fn) { \
  457. r = __wa->fn(__VA_ARGS__); \
  458. __val = true; \
  459. } else { \
  460. __val = false; \
  461. } \
  462. } else { \
  463. __val = false; \
  464. } \
  465. __val; \
  466. })
  467. static bool arch_timer_this_cpu_has_cntvct_wa(void)
  468. {
  469. const struct arch_timer_erratum_workaround *wa;
  470. wa = __this_cpu_read(timer_unstable_counter_workaround);
  471. return wa && wa->read_cntvct_el0;
  472. }
  473. #else
  474. #define arch_timer_check_ool_workaround(t,a) do { } while(0)
  475. #define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
  476. #define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
  477. #define erratum_handler(fn, r, ...) ({false;})
  478. #define arch_timer_this_cpu_has_cntvct_wa() ({false;})
  479. #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
  480. static __always_inline irqreturn_t timer_handler(const int access,
  481. struct clock_event_device *evt)
  482. {
  483. unsigned long ctrl;
  484. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
  485. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  486. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  487. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
  488. evt->event_handler(evt);
  489. return IRQ_HANDLED;
  490. }
  491. return IRQ_NONE;
  492. }
  493. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  494. {
  495. struct clock_event_device *evt = dev_id;
  496. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  497. }
  498. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  499. {
  500. struct clock_event_device *evt = dev_id;
  501. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  502. }
  503. static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
  504. {
  505. struct clock_event_device *evt = dev_id;
  506. return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
  507. }
  508. static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
  509. {
  510. struct clock_event_device *evt = dev_id;
  511. return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
  512. }
  513. static __always_inline int timer_shutdown(const int access,
  514. struct clock_event_device *clk)
  515. {
  516. unsigned long ctrl;
  517. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  518. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  519. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  520. return 0;
  521. }
  522. static int arch_timer_shutdown_virt(struct clock_event_device *clk)
  523. {
  524. return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
  525. }
  526. static int arch_timer_shutdown_phys(struct clock_event_device *clk)
  527. {
  528. return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
  529. }
  530. static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
  531. {
  532. return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
  533. }
  534. static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
  535. {
  536. return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
  537. }
  538. static __always_inline void set_next_event(const int access, unsigned long evt,
  539. struct clock_event_device *clk)
  540. {
  541. unsigned long ctrl;
  542. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  543. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  544. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  545. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
  546. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  547. }
  548. static int arch_timer_set_next_event_virt(unsigned long evt,
  549. struct clock_event_device *clk)
  550. {
  551. int ret;
  552. if (erratum_handler(set_next_event_virt, ret, evt, clk))
  553. return ret;
  554. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  555. return 0;
  556. }
  557. static int arch_timer_set_next_event_phys(unsigned long evt,
  558. struct clock_event_device *clk)
  559. {
  560. int ret;
  561. if (erratum_handler(set_next_event_phys, ret, evt, clk))
  562. return ret;
  563. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  564. return 0;
  565. }
  566. static int arch_timer_set_next_event_virt_mem(unsigned long evt,
  567. struct clock_event_device *clk)
  568. {
  569. set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
  570. return 0;
  571. }
  572. static int arch_timer_set_next_event_phys_mem(unsigned long evt,
  573. struct clock_event_device *clk)
  574. {
  575. set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
  576. return 0;
  577. }
  578. static void __arch_timer_setup(unsigned type,
  579. struct clock_event_device *clk)
  580. {
  581. clk->features = CLOCK_EVT_FEAT_ONESHOT;
  582. if (type == ARCH_CP15_TIMER) {
  583. if (arch_timer_c3stop)
  584. clk->features |= CLOCK_EVT_FEAT_C3STOP;
  585. clk->name = "arch_sys_timer";
  586. clk->rating = 450;
  587. clk->cpumask = cpumask_of(smp_processor_id());
  588. clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
  589. switch (arch_timer_uses_ppi) {
  590. case VIRT_PPI:
  591. clk->set_state_shutdown = arch_timer_shutdown_virt;
  592. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
  593. clk->set_next_event = arch_timer_set_next_event_virt;
  594. break;
  595. case PHYS_SECURE_PPI:
  596. case PHYS_NONSECURE_PPI:
  597. case HYP_PPI:
  598. clk->set_state_shutdown = arch_timer_shutdown_phys;
  599. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
  600. clk->set_next_event = arch_timer_set_next_event_phys;
  601. break;
  602. default:
  603. BUG();
  604. }
  605. arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
  606. } else {
  607. clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
  608. clk->name = "arch_mem_timer";
  609. clk->rating = 400;
  610. clk->cpumask = cpu_all_mask;
  611. if (arch_timer_mem_use_virtual) {
  612. clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
  613. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
  614. clk->set_next_event =
  615. arch_timer_set_next_event_virt_mem;
  616. } else {
  617. clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
  618. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
  619. clk->set_next_event =
  620. arch_timer_set_next_event_phys_mem;
  621. }
  622. }
  623. clk->set_state_shutdown(clk);
  624. clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
  625. }
  626. static void arch_timer_evtstrm_enable(int divider)
  627. {
  628. u32 cntkctl = arch_timer_get_cntkctl();
  629. cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
  630. /* Set the divider and enable virtual event stream */
  631. cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
  632. | ARCH_TIMER_VIRT_EVT_EN;
  633. arch_timer_set_cntkctl(cntkctl);
  634. elf_hwcap |= HWCAP_EVTSTRM;
  635. #ifdef CONFIG_COMPAT
  636. compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
  637. #endif
  638. }
  639. static void arch_timer_configure_evtstream(void)
  640. {
  641. int evt_stream_div, pos;
  642. /* Find the closest power of two to the divisor */
  643. evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
  644. pos = fls(evt_stream_div);
  645. if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
  646. pos--;
  647. /* enable event stream */
  648. arch_timer_evtstrm_enable(min(pos, 15));
  649. }
  650. static void arch_counter_set_user_access(void)
  651. {
  652. u32 cntkctl = arch_timer_get_cntkctl();
  653. /* Disable user access to the timers and both counters */
  654. /* Also disable virtual event stream */
  655. cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
  656. | ARCH_TIMER_USR_VT_ACCESS_EN
  657. | ARCH_TIMER_USR_VCT_ACCESS_EN
  658. | ARCH_TIMER_VIRT_EVT_EN
  659. | ARCH_TIMER_USR_PCT_ACCESS_EN);
  660. /*
  661. * Enable user access to the virtual counter if it doesn't
  662. * need to be workaround. The vdso may have been already
  663. * disabled though.
  664. */
  665. if (arch_timer_this_cpu_has_cntvct_wa())
  666. pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
  667. else
  668. cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
  669. arch_timer_set_cntkctl(cntkctl);
  670. }
  671. static bool arch_timer_has_nonsecure_ppi(void)
  672. {
  673. return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
  674. arch_timer_ppi[PHYS_NONSECURE_PPI]);
  675. }
  676. static u32 check_ppi_trigger(int irq)
  677. {
  678. u32 flags = irq_get_trigger_type(irq);
  679. if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
  680. pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
  681. pr_warn("WARNING: Please fix your firmware\n");
  682. flags = IRQF_TRIGGER_LOW;
  683. }
  684. return flags;
  685. }
  686. static int arch_timer_starting_cpu(unsigned int cpu)
  687. {
  688. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  689. u32 flags;
  690. __arch_timer_setup(ARCH_CP15_TIMER, clk);
  691. flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
  692. enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
  693. if (arch_timer_has_nonsecure_ppi()) {
  694. flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  695. enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
  696. }
  697. arch_counter_set_user_access();
  698. if (evtstrm_enable)
  699. arch_timer_configure_evtstream();
  700. return 0;
  701. }
  702. static void
  703. arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
  704. {
  705. /* Who has more than one independent system counter? */
  706. if (arch_timer_rate)
  707. return;
  708. /*
  709. * Try to determine the frequency from the device tree or CNTFRQ,
  710. * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
  711. */
  712. if (!acpi_disabled ||
  713. of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
  714. if (cntbase)
  715. arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
  716. else
  717. arch_timer_rate = arch_timer_get_cntfrq();
  718. }
  719. /* Check the timer frequency. */
  720. if (arch_timer_rate == 0)
  721. pr_warn("Architected timer frequency not available\n");
  722. }
  723. static void arch_timer_banner(unsigned type)
  724. {
  725. pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
  726. type & ARCH_CP15_TIMER ? "cp15" : "",
  727. type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
  728. type & ARCH_MEM_TIMER ? "mmio" : "",
  729. (unsigned long)arch_timer_rate / 1000000,
  730. (unsigned long)(arch_timer_rate / 10000) % 100,
  731. type & ARCH_CP15_TIMER ?
  732. (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
  733. "",
  734. type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
  735. type & ARCH_MEM_TIMER ?
  736. arch_timer_mem_use_virtual ? "virt" : "phys" :
  737. "");
  738. }
  739. u32 arch_timer_get_rate(void)
  740. {
  741. return arch_timer_rate;
  742. }
  743. static u64 arch_counter_get_cntvct_mem(void)
  744. {
  745. u32 vct_lo, vct_hi, tmp_hi;
  746. do {
  747. vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  748. vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
  749. tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  750. } while (vct_hi != tmp_hi);
  751. return ((u64) vct_hi << 32) | vct_lo;
  752. }
  753. static struct arch_timer_kvm_info arch_timer_kvm_info;
  754. struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
  755. {
  756. return &arch_timer_kvm_info;
  757. }
  758. static void __init arch_counter_register(unsigned type)
  759. {
  760. u64 start_count;
  761. /* Register the CP15 based counter if we have one */
  762. if (type & ARCH_CP15_TIMER) {
  763. if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
  764. arch_timer_read_counter = arch_counter_get_cntvct;
  765. else
  766. arch_timer_read_counter = arch_counter_get_cntpct;
  767. clocksource_counter.archdata.vdso_direct = vdso_default;
  768. } else {
  769. arch_timer_read_counter = arch_counter_get_cntvct_mem;
  770. }
  771. if (!arch_counter_suspend_stop)
  772. clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
  773. start_count = arch_timer_read_counter();
  774. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  775. cyclecounter.mult = clocksource_counter.mult;
  776. cyclecounter.shift = clocksource_counter.shift;
  777. timecounter_init(&arch_timer_kvm_info.timecounter,
  778. &cyclecounter, start_count);
  779. /* 56 bits minimum, so we assume worst case rollover */
  780. sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
  781. }
  782. static void arch_timer_stop(struct clock_event_device *clk)
  783. {
  784. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  785. clk->irq, smp_processor_id());
  786. disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
  787. if (arch_timer_has_nonsecure_ppi())
  788. disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  789. clk->set_state_shutdown(clk);
  790. }
  791. static int arch_timer_dying_cpu(unsigned int cpu)
  792. {
  793. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  794. arch_timer_stop(clk);
  795. return 0;
  796. }
  797. #ifdef CONFIG_CPU_PM
  798. static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
  799. static int arch_timer_cpu_pm_notify(struct notifier_block *self,
  800. unsigned long action, void *hcpu)
  801. {
  802. if (action == CPU_PM_ENTER)
  803. __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
  804. else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
  805. arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
  806. return NOTIFY_OK;
  807. }
  808. static struct notifier_block arch_timer_cpu_pm_notifier = {
  809. .notifier_call = arch_timer_cpu_pm_notify,
  810. };
  811. static int __init arch_timer_cpu_pm_init(void)
  812. {
  813. return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
  814. }
  815. static void __init arch_timer_cpu_pm_deinit(void)
  816. {
  817. WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
  818. }
  819. #else
  820. static int __init arch_timer_cpu_pm_init(void)
  821. {
  822. return 0;
  823. }
  824. static void __init arch_timer_cpu_pm_deinit(void)
  825. {
  826. }
  827. #endif
  828. static int __init arch_timer_register(void)
  829. {
  830. int err;
  831. int ppi;
  832. arch_timer_evt = alloc_percpu(struct clock_event_device);
  833. if (!arch_timer_evt) {
  834. err = -ENOMEM;
  835. goto out;
  836. }
  837. ppi = arch_timer_ppi[arch_timer_uses_ppi];
  838. switch (arch_timer_uses_ppi) {
  839. case VIRT_PPI:
  840. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  841. "arch_timer", arch_timer_evt);
  842. break;
  843. case PHYS_SECURE_PPI:
  844. case PHYS_NONSECURE_PPI:
  845. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  846. "arch_timer", arch_timer_evt);
  847. if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  848. ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
  849. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  850. "arch_timer", arch_timer_evt);
  851. if (err)
  852. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  853. arch_timer_evt);
  854. }
  855. break;
  856. case HYP_PPI:
  857. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  858. "arch_timer", arch_timer_evt);
  859. break;
  860. default:
  861. BUG();
  862. }
  863. if (err) {
  864. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  865. ppi, err);
  866. goto out_free;
  867. }
  868. err = arch_timer_cpu_pm_init();
  869. if (err)
  870. goto out_unreg_notify;
  871. /* Register and immediately configure the timer on the boot CPU */
  872. err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
  873. "clockevents/arm/arch_timer:starting",
  874. arch_timer_starting_cpu, arch_timer_dying_cpu);
  875. if (err)
  876. goto out_unreg_cpupm;
  877. return 0;
  878. out_unreg_cpupm:
  879. arch_timer_cpu_pm_deinit();
  880. out_unreg_notify:
  881. free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
  882. if (arch_timer_has_nonsecure_ppi())
  883. free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
  884. arch_timer_evt);
  885. out_free:
  886. free_percpu(arch_timer_evt);
  887. out:
  888. return err;
  889. }
  890. static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
  891. {
  892. int ret;
  893. irq_handler_t func;
  894. struct arch_timer *t;
  895. t = kzalloc(sizeof(*t), GFP_KERNEL);
  896. if (!t)
  897. return -ENOMEM;
  898. t->base = base;
  899. t->evt.irq = irq;
  900. __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
  901. if (arch_timer_mem_use_virtual)
  902. func = arch_timer_handler_virt_mem;
  903. else
  904. func = arch_timer_handler_phys_mem;
  905. ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
  906. if (ret) {
  907. pr_err("arch_timer: Failed to request mem timer irq\n");
  908. kfree(t);
  909. }
  910. return ret;
  911. }
  912. static const struct of_device_id arch_timer_of_match[] __initconst = {
  913. { .compatible = "arm,armv7-timer", },
  914. { .compatible = "arm,armv8-timer", },
  915. {},
  916. };
  917. static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
  918. { .compatible = "arm,armv7-timer-mem", },
  919. {},
  920. };
  921. static bool __init
  922. arch_timer_needs_probing(int type, const struct of_device_id *matches)
  923. {
  924. struct device_node *dn;
  925. bool needs_probing = false;
  926. dn = of_find_matching_node(NULL, matches);
  927. if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
  928. needs_probing = true;
  929. of_node_put(dn);
  930. return needs_probing;
  931. }
  932. static int __init arch_timer_common_init(void)
  933. {
  934. unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
  935. /* Wait until both nodes are probed if we have two timers */
  936. if ((arch_timers_present & mask) != mask) {
  937. if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
  938. return 0;
  939. if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
  940. return 0;
  941. }
  942. arch_timer_banner(arch_timers_present);
  943. arch_counter_register(arch_timers_present);
  944. return arch_timer_arch_init();
  945. }
  946. static int __init arch_timer_init(void)
  947. {
  948. int ret;
  949. /*
  950. * If HYP mode is available, we know that the physical timer
  951. * has been configured to be accessible from PL1. Use it, so
  952. * that a guest can use the virtual timer instead.
  953. *
  954. * If no interrupt provided for virtual timer, we'll have to
  955. * stick to the physical timer. It'd better be accessible...
  956. *
  957. * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
  958. * accesses to CNTP_*_EL1 registers are silently redirected to
  959. * their CNTHP_*_EL2 counterparts, and use a different PPI
  960. * number.
  961. */
  962. if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
  963. bool has_ppi;
  964. if (is_kernel_in_hyp_mode()) {
  965. arch_timer_uses_ppi = HYP_PPI;
  966. has_ppi = !!arch_timer_ppi[HYP_PPI];
  967. } else {
  968. arch_timer_uses_ppi = PHYS_SECURE_PPI;
  969. has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
  970. !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
  971. }
  972. if (!has_ppi) {
  973. pr_warn("arch_timer: No interrupt available, giving up\n");
  974. return -EINVAL;
  975. }
  976. }
  977. ret = arch_timer_register();
  978. if (ret)
  979. return ret;
  980. ret = arch_timer_common_init();
  981. if (ret)
  982. return ret;
  983. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
  984. return 0;
  985. }
  986. static int __init arch_timer_of_init(struct device_node *np)
  987. {
  988. int i;
  989. if (arch_timers_present & ARCH_CP15_TIMER) {
  990. pr_warn("arch_timer: multiple nodes in dt, skipping\n");
  991. return 0;
  992. }
  993. arch_timers_present |= ARCH_CP15_TIMER;
  994. for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
  995. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  996. arch_timer_detect_rate(NULL, np);
  997. arch_timer_c3stop = !of_property_read_bool(np, "always-on");
  998. /* Check for globally applicable workarounds */
  999. arch_timer_check_ool_workaround(ate_match_dt, np);
  1000. /*
  1001. * If we cannot rely on firmware initializing the timer registers then
  1002. * we should use the physical timers instead.
  1003. */
  1004. if (IS_ENABLED(CONFIG_ARM) &&
  1005. of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  1006. arch_timer_uses_ppi = PHYS_SECURE_PPI;
  1007. /* On some systems, the counter stops ticking when in suspend. */
  1008. arch_counter_suspend_stop = of_property_read_bool(np,
  1009. "arm,no-tick-in-suspend");
  1010. return arch_timer_init();
  1011. }
  1012. CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
  1013. CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
  1014. static int __init arch_timer_mem_init(struct device_node *np)
  1015. {
  1016. struct device_node *frame, *best_frame = NULL;
  1017. void __iomem *cntctlbase, *base;
  1018. unsigned int irq, ret = -EINVAL;
  1019. u32 cnttidr;
  1020. arch_timers_present |= ARCH_MEM_TIMER;
  1021. cntctlbase = of_iomap(np, 0);
  1022. if (!cntctlbase) {
  1023. pr_err("arch_timer: Can't find CNTCTLBase\n");
  1024. return -ENXIO;
  1025. }
  1026. cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
  1027. /*
  1028. * Try to find a virtual capable frame. Otherwise fall back to a
  1029. * physical capable frame.
  1030. */
  1031. for_each_available_child_of_node(np, frame) {
  1032. int n;
  1033. u32 cntacr;
  1034. if (of_property_read_u32(frame, "frame-number", &n)) {
  1035. pr_err("arch_timer: Missing frame-number\n");
  1036. of_node_put(frame);
  1037. goto out;
  1038. }
  1039. /* Try enabling everything, and see what sticks */
  1040. cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
  1041. CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
  1042. writel_relaxed(cntacr, cntctlbase + CNTACR(n));
  1043. cntacr = readl_relaxed(cntctlbase + CNTACR(n));
  1044. if ((cnttidr & CNTTIDR_VIRT(n)) &&
  1045. !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
  1046. of_node_put(best_frame);
  1047. best_frame = frame;
  1048. arch_timer_mem_use_virtual = true;
  1049. break;
  1050. }
  1051. if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
  1052. continue;
  1053. of_node_put(best_frame);
  1054. best_frame = of_node_get(frame);
  1055. }
  1056. ret= -ENXIO;
  1057. base = arch_counter_base = of_io_request_and_map(best_frame, 0,
  1058. "arch_mem_timer");
  1059. if (IS_ERR(base)) {
  1060. pr_err("arch_timer: Can't map frame's registers\n");
  1061. goto out;
  1062. }
  1063. if (arch_timer_mem_use_virtual)
  1064. irq = irq_of_parse_and_map(best_frame, 1);
  1065. else
  1066. irq = irq_of_parse_and_map(best_frame, 0);
  1067. ret = -EINVAL;
  1068. if (!irq) {
  1069. pr_err("arch_timer: Frame missing %s irq",
  1070. arch_timer_mem_use_virtual ? "virt" : "phys");
  1071. goto out;
  1072. }
  1073. arch_timer_detect_rate(base, np);
  1074. ret = arch_timer_mem_register(base, irq);
  1075. if (ret)
  1076. goto out;
  1077. return arch_timer_common_init();
  1078. out:
  1079. iounmap(cntctlbase);
  1080. of_node_put(best_frame);
  1081. return ret;
  1082. }
  1083. CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
  1084. arch_timer_mem_init);
  1085. #ifdef CONFIG_ACPI
  1086. static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
  1087. {
  1088. int trigger, polarity;
  1089. if (!interrupt)
  1090. return 0;
  1091. trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
  1092. : ACPI_LEVEL_SENSITIVE;
  1093. polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
  1094. : ACPI_ACTIVE_HIGH;
  1095. return acpi_register_gsi(NULL, interrupt, trigger, polarity);
  1096. }
  1097. /* Initialize per-processor generic timer */
  1098. static int __init arch_timer_acpi_init(struct acpi_table_header *table)
  1099. {
  1100. struct acpi_table_gtdt *gtdt;
  1101. if (arch_timers_present & ARCH_CP15_TIMER) {
  1102. pr_warn("arch_timer: already initialized, skipping\n");
  1103. return -EINVAL;
  1104. }
  1105. gtdt = container_of(table, struct acpi_table_gtdt, header);
  1106. arch_timers_present |= ARCH_CP15_TIMER;
  1107. arch_timer_ppi[PHYS_SECURE_PPI] =
  1108. map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
  1109. gtdt->secure_el1_flags);
  1110. arch_timer_ppi[PHYS_NONSECURE_PPI] =
  1111. map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
  1112. gtdt->non_secure_el1_flags);
  1113. arch_timer_ppi[VIRT_PPI] =
  1114. map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
  1115. gtdt->virtual_timer_flags);
  1116. arch_timer_ppi[HYP_PPI] =
  1117. map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
  1118. gtdt->non_secure_el2_flags);
  1119. /* Get the frequency from CNTFRQ */
  1120. arch_timer_detect_rate(NULL, NULL);
  1121. /* Always-on capability */
  1122. arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
  1123. /* Check for globally applicable workarounds */
  1124. arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
  1125. arch_timer_init();
  1126. return 0;
  1127. }
  1128. CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
  1129. #endif