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