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