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