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 | */ |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/smp.h> |
| 15 | #include <linux/cpu.h> |
Sudeep KarkadaNagesha | 346e748 | 2013-08-23 15:53:15 +0100 | [diff] [blame^] | 16 | #include <linux/cpu_pm.h> |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 17 | #include <linux/clockchips.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/of_irq.h> |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 20 | #include <linux/of_address.h> |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 21 | #include <linux/io.h> |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 23 | |
| 24 | #include <asm/arch_timer.h> |
Marc Zyngier | 8266891 | 2013-01-10 11:13:07 +0000 | [diff] [blame] | 25 | #include <asm/virt.h> |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 26 | |
| 27 | #include <clocksource/arm_arch_timer.h> |
| 28 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 29 | #define CNTTIDR 0x08 |
| 30 | #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4)) |
| 31 | |
| 32 | #define CNTVCT_LO 0x08 |
| 33 | #define CNTVCT_HI 0x0c |
| 34 | #define CNTFRQ 0x10 |
| 35 | #define CNTP_TVAL 0x28 |
| 36 | #define CNTP_CTL 0x2c |
| 37 | #define CNTV_TVAL 0x38 |
| 38 | #define CNTV_CTL 0x3c |
| 39 | |
| 40 | #define ARCH_CP15_TIMER BIT(0) |
| 41 | #define ARCH_MEM_TIMER BIT(1) |
| 42 | static unsigned arch_timers_present __initdata; |
| 43 | |
| 44 | static void __iomem *arch_counter_base; |
| 45 | |
| 46 | struct arch_timer { |
| 47 | void __iomem *base; |
| 48 | struct clock_event_device evt; |
| 49 | }; |
| 50 | |
| 51 | #define to_arch_timer(e) container_of(e, struct arch_timer, evt) |
| 52 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 53 | static u32 arch_timer_rate; |
| 54 | |
| 55 | enum ppi_nr { |
| 56 | PHYS_SECURE_PPI, |
| 57 | PHYS_NONSECURE_PPI, |
| 58 | VIRT_PPI, |
| 59 | HYP_PPI, |
| 60 | MAX_TIMER_PPI |
| 61 | }; |
| 62 | |
| 63 | static int arch_timer_ppi[MAX_TIMER_PPI]; |
| 64 | |
| 65 | static struct clock_event_device __percpu *arch_timer_evt; |
| 66 | |
| 67 | static bool arch_timer_use_virtual = true; |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 68 | static bool arch_timer_mem_use_virtual; |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Architected system timer support. |
| 72 | */ |
| 73 | |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 74 | static __always_inline |
| 75 | 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] | 76 | struct clock_event_device *clk) |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 77 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 78 | if (access == ARCH_TIMER_MEM_PHYS_ACCESS) { |
| 79 | struct arch_timer *timer = to_arch_timer(clk); |
| 80 | switch (reg) { |
| 81 | case ARCH_TIMER_REG_CTRL: |
| 82 | writel_relaxed(val, timer->base + CNTP_CTL); |
| 83 | break; |
| 84 | case ARCH_TIMER_REG_TVAL: |
| 85 | writel_relaxed(val, timer->base + CNTP_TVAL); |
| 86 | break; |
| 87 | } |
| 88 | } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) { |
| 89 | struct arch_timer *timer = to_arch_timer(clk); |
| 90 | switch (reg) { |
| 91 | case ARCH_TIMER_REG_CTRL: |
| 92 | writel_relaxed(val, timer->base + CNTV_CTL); |
| 93 | break; |
| 94 | case ARCH_TIMER_REG_TVAL: |
| 95 | writel_relaxed(val, timer->base + CNTV_TVAL); |
| 96 | break; |
| 97 | } |
| 98 | } else { |
| 99 | arch_timer_reg_write_cp15(access, reg, val); |
| 100 | } |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static __always_inline |
| 104 | u32 arch_timer_reg_read(int access, enum arch_timer_reg reg, |
Thomas Gleixner | cfb6d65 | 2013-08-21 14:59:23 +0200 | [diff] [blame] | 105 | struct clock_event_device *clk) |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 106 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 107 | u32 val; |
| 108 | |
| 109 | if (access == ARCH_TIMER_MEM_PHYS_ACCESS) { |
| 110 | struct arch_timer *timer = to_arch_timer(clk); |
| 111 | switch (reg) { |
| 112 | case ARCH_TIMER_REG_CTRL: |
| 113 | val = readl_relaxed(timer->base + CNTP_CTL); |
| 114 | break; |
| 115 | case ARCH_TIMER_REG_TVAL: |
| 116 | val = readl_relaxed(timer->base + CNTP_TVAL); |
| 117 | break; |
| 118 | } |
| 119 | } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) { |
| 120 | struct arch_timer *timer = to_arch_timer(clk); |
| 121 | switch (reg) { |
| 122 | case ARCH_TIMER_REG_CTRL: |
| 123 | val = readl_relaxed(timer->base + CNTV_CTL); |
| 124 | break; |
| 125 | case ARCH_TIMER_REG_TVAL: |
| 126 | val = readl_relaxed(timer->base + CNTV_TVAL); |
| 127 | break; |
| 128 | } |
| 129 | } else { |
| 130 | val = arch_timer_reg_read_cp15(access, reg); |
| 131 | } |
| 132 | |
| 133 | return val; |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Stephen Boyd | e09f3cc | 2013-07-18 16:59:28 -0700 | [diff] [blame] | 136 | static __always_inline irqreturn_t timer_handler(const int access, |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 137 | struct clock_event_device *evt) |
| 138 | { |
| 139 | unsigned long ctrl; |
Thomas Gleixner | cfb6d65 | 2013-08-21 14:59:23 +0200 | [diff] [blame] | 140 | |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 141 | ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 142 | if (ctrl & ARCH_TIMER_CTRL_IT_STAT) { |
| 143 | ctrl |= ARCH_TIMER_CTRL_IT_MASK; |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 144 | arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 145 | evt->event_handler(evt); |
| 146 | return IRQ_HANDLED; |
| 147 | } |
| 148 | |
| 149 | return IRQ_NONE; |
| 150 | } |
| 151 | |
| 152 | static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id) |
| 153 | { |
| 154 | struct clock_event_device *evt = dev_id; |
| 155 | |
| 156 | return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt); |
| 157 | } |
| 158 | |
| 159 | static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id) |
| 160 | { |
| 161 | struct clock_event_device *evt = dev_id; |
| 162 | |
| 163 | return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt); |
| 164 | } |
| 165 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 166 | static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id) |
| 167 | { |
| 168 | struct clock_event_device *evt = dev_id; |
| 169 | |
| 170 | return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt); |
| 171 | } |
| 172 | |
| 173 | static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id) |
| 174 | { |
| 175 | struct clock_event_device *evt = dev_id; |
| 176 | |
| 177 | return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt); |
| 178 | } |
| 179 | |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 180 | static __always_inline void timer_set_mode(const int access, int mode, |
| 181 | struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 182 | { |
| 183 | unsigned long ctrl; |
| 184 | switch (mode) { |
| 185 | case CLOCK_EVT_MODE_UNUSED: |
| 186 | case CLOCK_EVT_MODE_SHUTDOWN: |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 187 | ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 188 | ctrl &= ~ARCH_TIMER_CTRL_ENABLE; |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 189 | arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 190 | break; |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void arch_timer_set_mode_virt(enum clock_event_mode mode, |
| 197 | struct clock_event_device *clk) |
| 198 | { |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 199 | timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void arch_timer_set_mode_phys(enum clock_event_mode mode, |
| 203 | struct clock_event_device *clk) |
| 204 | { |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 205 | timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 208 | static void arch_timer_set_mode_virt_mem(enum clock_event_mode mode, |
| 209 | struct clock_event_device *clk) |
| 210 | { |
| 211 | timer_set_mode(ARCH_TIMER_MEM_VIRT_ACCESS, mode, clk); |
| 212 | } |
| 213 | |
| 214 | static void arch_timer_set_mode_phys_mem(enum clock_event_mode mode, |
| 215 | struct clock_event_device *clk) |
| 216 | { |
| 217 | timer_set_mode(ARCH_TIMER_MEM_PHYS_ACCESS, mode, clk); |
| 218 | } |
| 219 | |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 220 | 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] | 221 | struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 222 | { |
| 223 | unsigned long ctrl; |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 224 | ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 225 | ctrl |= ARCH_TIMER_CTRL_ENABLE; |
| 226 | ctrl &= ~ARCH_TIMER_CTRL_IT_MASK; |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 227 | arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk); |
| 228 | arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int arch_timer_set_next_event_virt(unsigned long evt, |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 232 | struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 233 | { |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 234 | set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int arch_timer_set_next_event_phys(unsigned long evt, |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 239 | struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 240 | { |
Stephen Boyd | 60faddf | 2013-07-18 16:59:31 -0700 | [diff] [blame] | 241 | set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 245 | static int arch_timer_set_next_event_virt_mem(unsigned long evt, |
| 246 | struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 247 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 248 | set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk); |
| 249 | return 0; |
| 250 | } |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 251 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 252 | static int arch_timer_set_next_event_phys_mem(unsigned long evt, |
| 253 | struct clock_event_device *clk) |
| 254 | { |
| 255 | set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Thomas Gleixner | cfb6d65 | 2013-08-21 14:59:23 +0200 | [diff] [blame] | 259 | static void __arch_timer_setup(unsigned type, |
| 260 | struct clock_event_device *clk) |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 261 | { |
| 262 | clk->features = CLOCK_EVT_FEAT_ONESHOT; |
| 263 | |
| 264 | if (type == ARCH_CP15_TIMER) { |
| 265 | clk->features |= CLOCK_EVT_FEAT_C3STOP; |
| 266 | clk->name = "arch_sys_timer"; |
| 267 | clk->rating = 450; |
| 268 | clk->cpumask = cpumask_of(smp_processor_id()); |
| 269 | if (arch_timer_use_virtual) { |
| 270 | clk->irq = arch_timer_ppi[VIRT_PPI]; |
| 271 | clk->set_mode = arch_timer_set_mode_virt; |
| 272 | clk->set_next_event = arch_timer_set_next_event_virt; |
| 273 | } else { |
| 274 | clk->irq = arch_timer_ppi[PHYS_SECURE_PPI]; |
| 275 | clk->set_mode = arch_timer_set_mode_phys; |
| 276 | clk->set_next_event = arch_timer_set_next_event_phys; |
| 277 | } |
| 278 | } else { |
| 279 | clk->name = "arch_mem_timer"; |
| 280 | clk->rating = 400; |
| 281 | clk->cpumask = cpu_all_mask; |
| 282 | if (arch_timer_mem_use_virtual) { |
| 283 | clk->set_mode = arch_timer_set_mode_virt_mem; |
| 284 | clk->set_next_event = |
| 285 | arch_timer_set_next_event_virt_mem; |
| 286 | } else { |
| 287 | clk->set_mode = arch_timer_set_mode_phys_mem; |
| 288 | clk->set_next_event = |
| 289 | arch_timer_set_next_event_phys_mem; |
| 290 | } |
| 291 | } |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 292 | |
Stephen Boyd | 1ff99ea | 2013-07-18 16:59:30 -0700 | [diff] [blame] | 293 | clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 294 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 295 | clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff); |
| 296 | } |
| 297 | |
Will Deacon | 037f637 | 2013-08-23 15:32:29 +0100 | [diff] [blame] | 298 | static void arch_timer_configure_evtstream(void) |
| 299 | { |
| 300 | int evt_stream_div, pos; |
| 301 | |
| 302 | /* Find the closest power of two to the divisor */ |
| 303 | evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ; |
| 304 | pos = fls(evt_stream_div); |
| 305 | if (pos > 1 && !(evt_stream_div & (1 << (pos - 2)))) |
| 306 | pos--; |
| 307 | /* enable event stream */ |
| 308 | arch_timer_evtstrm_enable(min(pos, 15)); |
| 309 | } |
| 310 | |
Paul Gortmaker | 8c37bb3 | 2013-06-19 11:32:08 -0400 | [diff] [blame] | 311 | static int arch_timer_setup(struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 312 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 313 | __arch_timer_setup(ARCH_CP15_TIMER, clk); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 314 | |
| 315 | if (arch_timer_use_virtual) |
| 316 | enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0); |
| 317 | else { |
| 318 | enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0); |
| 319 | if (arch_timer_ppi[PHYS_NONSECURE_PPI]) |
| 320 | enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0); |
| 321 | } |
| 322 | |
| 323 | arch_counter_set_user_access(); |
Will Deacon | 037f637 | 2013-08-23 15:32:29 +0100 | [diff] [blame] | 324 | if (IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM)) |
| 325 | arch_timer_configure_evtstream(); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 330 | static void |
| 331 | arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 332 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 333 | /* Who has more than one independent system counter? */ |
| 334 | if (arch_timer_rate) |
| 335 | return; |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 336 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 337 | /* Try to determine the frequency from the device tree or CNTFRQ */ |
| 338 | if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) { |
| 339 | if (cntbase) |
| 340 | arch_timer_rate = readl_relaxed(cntbase + CNTFRQ); |
| 341 | else |
| 342 | arch_timer_rate = arch_timer_get_cntfrq(); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 345 | /* Check the timer frequency. */ |
| 346 | if (arch_timer_rate == 0) |
| 347 | pr_warn("Architected timer frequency not available\n"); |
| 348 | } |
| 349 | |
| 350 | static void arch_timer_banner(unsigned type) |
| 351 | { |
| 352 | pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n", |
| 353 | type & ARCH_CP15_TIMER ? "cp15" : "", |
| 354 | type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "", |
| 355 | type & ARCH_MEM_TIMER ? "mmio" : "", |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 356 | (unsigned long)arch_timer_rate / 1000000, |
| 357 | (unsigned long)(arch_timer_rate / 10000) % 100, |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 358 | type & ARCH_CP15_TIMER ? |
| 359 | arch_timer_use_virtual ? "virt" : "phys" : |
| 360 | "", |
| 361 | type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "", |
| 362 | type & ARCH_MEM_TIMER ? |
| 363 | arch_timer_mem_use_virtual ? "virt" : "phys" : |
| 364 | ""); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | u32 arch_timer_get_rate(void) |
| 368 | { |
| 369 | return arch_timer_rate; |
| 370 | } |
| 371 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 372 | static u64 arch_counter_get_cntvct_mem(void) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 373 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 374 | u32 vct_lo, vct_hi, tmp_hi; |
| 375 | |
| 376 | do { |
| 377 | vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI); |
| 378 | vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO); |
| 379 | tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI); |
| 380 | } while (vct_hi != tmp_hi); |
| 381 | |
| 382 | return ((u64) vct_hi << 32) | vct_lo; |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 385 | /* |
| 386 | * Default to cp15 based access because arm64 uses this function for |
| 387 | * sched_clock() before DT is probed and the cp15 method is guaranteed |
| 388 | * to exist on arm64. arm doesn't use this before DT is probed so even |
| 389 | * if we don't have the cp15 accessors we won't have a problem. |
| 390 | */ |
| 391 | u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct; |
| 392 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 393 | static cycle_t arch_counter_read(struct clocksource *cs) |
| 394 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 395 | return arch_timer_read_counter(); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static cycle_t arch_counter_read_cc(const struct cyclecounter *cc) |
| 399 | { |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 400 | return arch_timer_read_counter(); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static struct clocksource clocksource_counter = { |
| 404 | .name = "arch_sys_counter", |
| 405 | .rating = 400, |
| 406 | .read = arch_counter_read, |
| 407 | .mask = CLOCKSOURCE_MASK(56), |
| 408 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 409 | }; |
| 410 | |
| 411 | static struct cyclecounter cyclecounter = { |
| 412 | .read = arch_counter_read_cc, |
| 413 | .mask = CLOCKSOURCE_MASK(56), |
| 414 | }; |
| 415 | |
| 416 | static struct timecounter timecounter; |
| 417 | |
| 418 | struct timecounter *arch_timer_get_timecounter(void) |
| 419 | { |
| 420 | return &timecounter; |
| 421 | } |
| 422 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 423 | static void __init arch_counter_register(unsigned type) |
| 424 | { |
| 425 | u64 start_count; |
| 426 | |
| 427 | /* Register the CP15 based counter if we have one */ |
| 428 | if (type & ARCH_CP15_TIMER) |
| 429 | arch_timer_read_counter = arch_counter_get_cntvct; |
| 430 | else |
| 431 | arch_timer_read_counter = arch_counter_get_cntvct_mem; |
| 432 | |
| 433 | start_count = arch_timer_read_counter(); |
| 434 | clocksource_register_hz(&clocksource_counter, arch_timer_rate); |
| 435 | cyclecounter.mult = clocksource_counter.mult; |
| 436 | cyclecounter.shift = clocksource_counter.shift; |
| 437 | timecounter_init(&timecounter, &cyclecounter, start_count); |
| 438 | } |
| 439 | |
Paul Gortmaker | 8c37bb3 | 2013-06-19 11:32:08 -0400 | [diff] [blame] | 440 | static void arch_timer_stop(struct clock_event_device *clk) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 441 | { |
| 442 | pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n", |
| 443 | clk->irq, smp_processor_id()); |
| 444 | |
| 445 | if (arch_timer_use_virtual) |
| 446 | disable_percpu_irq(arch_timer_ppi[VIRT_PPI]); |
| 447 | else { |
| 448 | disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]); |
| 449 | if (arch_timer_ppi[PHYS_NONSECURE_PPI]) |
| 450 | disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]); |
| 451 | } |
| 452 | |
| 453 | clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk); |
| 454 | } |
| 455 | |
Paul Gortmaker | 8c37bb3 | 2013-06-19 11:32:08 -0400 | [diff] [blame] | 456 | static int arch_timer_cpu_notify(struct notifier_block *self, |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 457 | unsigned long action, void *hcpu) |
| 458 | { |
Stephen Boyd | f31c2f1 | 2013-04-17 16:26:18 -0700 | [diff] [blame] | 459 | /* |
| 460 | * Grab cpu pointer in each case to avoid spurious |
| 461 | * preemptible warnings |
| 462 | */ |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 463 | switch (action & ~CPU_TASKS_FROZEN) { |
| 464 | case CPU_STARTING: |
Stephen Boyd | f31c2f1 | 2013-04-17 16:26:18 -0700 | [diff] [blame] | 465 | arch_timer_setup(this_cpu_ptr(arch_timer_evt)); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 466 | break; |
| 467 | case CPU_DYING: |
Stephen Boyd | f31c2f1 | 2013-04-17 16:26:18 -0700 | [diff] [blame] | 468 | arch_timer_stop(this_cpu_ptr(arch_timer_evt)); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | |
| 472 | return NOTIFY_OK; |
| 473 | } |
| 474 | |
Paul Gortmaker | 8c37bb3 | 2013-06-19 11:32:08 -0400 | [diff] [blame] | 475 | static struct notifier_block arch_timer_cpu_nb = { |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 476 | .notifier_call = arch_timer_cpu_notify, |
| 477 | }; |
| 478 | |
Sudeep KarkadaNagesha | 346e748 | 2013-08-23 15:53:15 +0100 | [diff] [blame^] | 479 | #ifdef CONFIG_CPU_PM |
| 480 | static unsigned int saved_cntkctl; |
| 481 | static int arch_timer_cpu_pm_notify(struct notifier_block *self, |
| 482 | unsigned long action, void *hcpu) |
| 483 | { |
| 484 | if (action == CPU_PM_ENTER) |
| 485 | saved_cntkctl = arch_timer_get_cntkctl(); |
| 486 | else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT) |
| 487 | arch_timer_set_cntkctl(saved_cntkctl); |
| 488 | return NOTIFY_OK; |
| 489 | } |
| 490 | |
| 491 | static struct notifier_block arch_timer_cpu_pm_notifier = { |
| 492 | .notifier_call = arch_timer_cpu_pm_notify, |
| 493 | }; |
| 494 | |
| 495 | static int __init arch_timer_cpu_pm_init(void) |
| 496 | { |
| 497 | return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier); |
| 498 | } |
| 499 | #else |
| 500 | static int __init arch_timer_cpu_pm_init(void) |
| 501 | { |
| 502 | return 0; |
| 503 | } |
| 504 | #endif |
| 505 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 506 | static int __init arch_timer_register(void) |
| 507 | { |
| 508 | int err; |
| 509 | int ppi; |
| 510 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 511 | arch_timer_evt = alloc_percpu(struct clock_event_device); |
| 512 | if (!arch_timer_evt) { |
| 513 | err = -ENOMEM; |
| 514 | goto out; |
| 515 | } |
| 516 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 517 | if (arch_timer_use_virtual) { |
| 518 | ppi = arch_timer_ppi[VIRT_PPI]; |
| 519 | err = request_percpu_irq(ppi, arch_timer_handler_virt, |
| 520 | "arch_timer", arch_timer_evt); |
| 521 | } else { |
| 522 | ppi = arch_timer_ppi[PHYS_SECURE_PPI]; |
| 523 | err = request_percpu_irq(ppi, arch_timer_handler_phys, |
| 524 | "arch_timer", arch_timer_evt); |
| 525 | if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) { |
| 526 | ppi = arch_timer_ppi[PHYS_NONSECURE_PPI]; |
| 527 | err = request_percpu_irq(ppi, arch_timer_handler_phys, |
| 528 | "arch_timer", arch_timer_evt); |
| 529 | if (err) |
| 530 | free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], |
| 531 | arch_timer_evt); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (err) { |
| 536 | pr_err("arch_timer: can't register interrupt %d (%d)\n", |
| 537 | ppi, err); |
| 538 | goto out_free; |
| 539 | } |
| 540 | |
| 541 | err = register_cpu_notifier(&arch_timer_cpu_nb); |
| 542 | if (err) |
| 543 | goto out_free_irq; |
| 544 | |
Sudeep KarkadaNagesha | 346e748 | 2013-08-23 15:53:15 +0100 | [diff] [blame^] | 545 | err = arch_timer_cpu_pm_init(); |
| 546 | if (err) |
| 547 | goto out_unreg_notify; |
| 548 | |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 549 | /* Immediately configure the timer on the boot CPU */ |
| 550 | arch_timer_setup(this_cpu_ptr(arch_timer_evt)); |
| 551 | |
| 552 | return 0; |
| 553 | |
Sudeep KarkadaNagesha | 346e748 | 2013-08-23 15:53:15 +0100 | [diff] [blame^] | 554 | out_unreg_notify: |
| 555 | unregister_cpu_notifier(&arch_timer_cpu_nb); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 556 | out_free_irq: |
| 557 | if (arch_timer_use_virtual) |
| 558 | free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt); |
| 559 | else { |
| 560 | free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], |
| 561 | arch_timer_evt); |
| 562 | if (arch_timer_ppi[PHYS_NONSECURE_PPI]) |
| 563 | free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], |
| 564 | arch_timer_evt); |
| 565 | } |
| 566 | |
| 567 | out_free: |
| 568 | free_percpu(arch_timer_evt); |
| 569 | out: |
| 570 | return err; |
| 571 | } |
| 572 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 573 | static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq) |
| 574 | { |
| 575 | int ret; |
| 576 | irq_handler_t func; |
| 577 | struct arch_timer *t; |
| 578 | |
| 579 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 580 | if (!t) |
| 581 | return -ENOMEM; |
| 582 | |
| 583 | t->base = base; |
| 584 | t->evt.irq = irq; |
| 585 | __arch_timer_setup(ARCH_MEM_TIMER, &t->evt); |
| 586 | |
| 587 | if (arch_timer_mem_use_virtual) |
| 588 | func = arch_timer_handler_virt_mem; |
| 589 | else |
| 590 | func = arch_timer_handler_phys_mem; |
| 591 | |
| 592 | ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt); |
| 593 | if (ret) { |
| 594 | pr_err("arch_timer: Failed to request mem timer irq\n"); |
| 595 | kfree(t); |
| 596 | } |
| 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | static const struct of_device_id arch_timer_of_match[] __initconst = { |
| 602 | { .compatible = "arm,armv7-timer", }, |
| 603 | { .compatible = "arm,armv8-timer", }, |
| 604 | {}, |
| 605 | }; |
| 606 | |
| 607 | static const struct of_device_id arch_timer_mem_of_match[] __initconst = { |
| 608 | { .compatible = "arm,armv7-timer-mem", }, |
| 609 | {}, |
| 610 | }; |
| 611 | |
| 612 | static void __init arch_timer_common_init(void) |
| 613 | { |
| 614 | unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER; |
| 615 | |
| 616 | /* Wait until both nodes are probed if we have two timers */ |
| 617 | if ((arch_timers_present & mask) != mask) { |
| 618 | if (of_find_matching_node(NULL, arch_timer_mem_of_match) && |
| 619 | !(arch_timers_present & ARCH_MEM_TIMER)) |
| 620 | return; |
| 621 | if (of_find_matching_node(NULL, arch_timer_of_match) && |
| 622 | !(arch_timers_present & ARCH_CP15_TIMER)) |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | arch_timer_banner(arch_timers_present); |
| 627 | arch_counter_register(arch_timers_present); |
| 628 | arch_timer_arch_init(); |
| 629 | } |
| 630 | |
Rob Herring | 0583fe4 | 2013-04-10 18:27:51 -0500 | [diff] [blame] | 631 | static void __init arch_timer_init(struct device_node *np) |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 632 | { |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 633 | int i; |
| 634 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 635 | if (arch_timers_present & ARCH_CP15_TIMER) { |
Rob Herring | 0583fe4 | 2013-04-10 18:27:51 -0500 | [diff] [blame] | 636 | pr_warn("arch_timer: multiple nodes in dt, skipping\n"); |
| 637 | return; |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 640 | arch_timers_present |= ARCH_CP15_TIMER; |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 641 | for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++) |
| 642 | arch_timer_ppi[i] = irq_of_parse_and_map(np, i); |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 643 | arch_timer_detect_rate(NULL, np); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 644 | |
| 645 | /* |
Marc Zyngier | 8266891 | 2013-01-10 11:13:07 +0000 | [diff] [blame] | 646 | * If HYP mode is available, we know that the physical timer |
| 647 | * has been configured to be accessible from PL1. Use it, so |
| 648 | * that a guest can use the virtual timer instead. |
| 649 | * |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 650 | * If no interrupt provided for virtual timer, we'll have to |
| 651 | * stick to the physical timer. It'd better be accessible... |
| 652 | */ |
Marc Zyngier | 8266891 | 2013-01-10 11:13:07 +0000 | [diff] [blame] | 653 | if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) { |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 654 | arch_timer_use_virtual = false; |
| 655 | |
| 656 | if (!arch_timer_ppi[PHYS_SECURE_PPI] || |
| 657 | !arch_timer_ppi[PHYS_NONSECURE_PPI]) { |
| 658 | pr_warn("arch_timer: No interrupt available, giving up\n"); |
Rob Herring | 0583fe4 | 2013-04-10 18:27:51 -0500 | [diff] [blame] | 659 | return; |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Rob Herring | 0583fe4 | 2013-04-10 18:27:51 -0500 | [diff] [blame] | 663 | arch_timer_register(); |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 664 | arch_timer_common_init(); |
Mark Rutland | 8a4da6e | 2012-11-12 14:33:44 +0000 | [diff] [blame] | 665 | } |
Rob Herring | 0583fe4 | 2013-04-10 18:27:51 -0500 | [diff] [blame] | 666 | CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_init); |
| 667 | CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_init); |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 668 | |
| 669 | static void __init arch_timer_mem_init(struct device_node *np) |
| 670 | { |
| 671 | struct device_node *frame, *best_frame = NULL; |
| 672 | void __iomem *cntctlbase, *base; |
| 673 | unsigned int irq; |
| 674 | u32 cnttidr; |
| 675 | |
| 676 | arch_timers_present |= ARCH_MEM_TIMER; |
| 677 | cntctlbase = of_iomap(np, 0); |
| 678 | if (!cntctlbase) { |
| 679 | pr_err("arch_timer: Can't find CNTCTLBase\n"); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | cnttidr = readl_relaxed(cntctlbase + CNTTIDR); |
| 684 | iounmap(cntctlbase); |
| 685 | |
| 686 | /* |
| 687 | * Try to find a virtual capable frame. Otherwise fall back to a |
| 688 | * physical capable frame. |
| 689 | */ |
| 690 | for_each_available_child_of_node(np, frame) { |
| 691 | int n; |
| 692 | |
| 693 | if (of_property_read_u32(frame, "frame-number", &n)) { |
| 694 | pr_err("arch_timer: Missing frame-number\n"); |
| 695 | of_node_put(best_frame); |
| 696 | of_node_put(frame); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | if (cnttidr & CNTTIDR_VIRT(n)) { |
| 701 | of_node_put(best_frame); |
| 702 | best_frame = frame; |
| 703 | arch_timer_mem_use_virtual = true; |
| 704 | break; |
| 705 | } |
| 706 | of_node_put(best_frame); |
| 707 | best_frame = of_node_get(frame); |
| 708 | } |
| 709 | |
| 710 | base = arch_counter_base = of_iomap(best_frame, 0); |
| 711 | if (!base) { |
| 712 | pr_err("arch_timer: Can't map frame's registers\n"); |
| 713 | of_node_put(best_frame); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | if (arch_timer_mem_use_virtual) |
| 718 | irq = irq_of_parse_and_map(best_frame, 1); |
| 719 | else |
| 720 | irq = irq_of_parse_and_map(best_frame, 0); |
| 721 | of_node_put(best_frame); |
| 722 | if (!irq) { |
| 723 | pr_err("arch_timer: Frame missing %s irq", |
Thomas Gleixner | cfb6d65 | 2013-08-21 14:59:23 +0200 | [diff] [blame] | 724 | arch_timer_mem_use_virtual ? "virt" : "phys"); |
Stephen Boyd | 2200699 | 2013-07-18 16:59:32 -0700 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | |
| 728 | arch_timer_detect_rate(base, np); |
| 729 | arch_timer_mem_register(base, irq); |
| 730 | arch_timer_common_init(); |
| 731 | } |
| 732 | CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem", |
| 733 | arch_timer_mem_init); |