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