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