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