clocksource/drivers/timer-riscv: Set CLOCK_EVT_FEAT_C3STOP based on DT
[linux-block.git] / drivers / clocksource / timer-riscv.c
CommitLineData
62b01943
PD
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2012 Regents of the University of California
4 * Copyright (C) 2017 SiFive
2f12dbf1 5 *
4f9bbcef
CH
6 * All RISC-V systems have a timer attached to every hart. These timers can
7 * either be read from the "time" and "timeh" CSRs, and can use the SBI to
8 * setup events, or directly accessed using MMIO registers.
62b01943 9 */
9f7a8ff6
AP
10
11#define pr_fmt(fmt) "riscv-timer: " fmt
12
62b01943
PD
13#include <linux/clocksource.h>
14#include <linux/clockchips.h>
15#include <linux/cpu.h>
16#include <linux/delay.h>
17#include <linux/irq.h>
033a65de 18#include <linux/irqdomain.h>
3a9f66cb 19#include <linux/module.h>
92e0d143 20#include <linux/sched_clock.h>
4f9bbcef 21#include <linux/io-64-nonatomic-lo-hi.h>
033a65de
AP
22#include <linux/interrupt.h>
23#include <linux/of_irq.h>
3a9f66cb 24#include <clocksource/timer-riscv.h>
f99fb607 25#include <asm/smp.h>
9f7a8ff6 26#include <asm/hwcap.h>
62b01943 27#include <asm/sbi.h>
2bc3fc87 28#include <asm/timex.h>
4f9bbcef 29
9f7a8ff6 30static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
8932a953 31static bool riscv_timer_cannot_wake_cpu;
9f7a8ff6 32
62b01943
PD
33static int riscv_clock_next_event(unsigned long delta,
34 struct clock_event_device *ce)
35{
9f7a8ff6
AP
36 u64 next_tval = get_cycles64() + delta;
37
a4c3733d 38 csr_set(CSR_IE, IE_TIE);
9f7a8ff6
AP
39 if (static_branch_likely(&riscv_sstc_available)) {
40#if defined(CONFIG_32BIT)
41 csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF);
42 csr_write(CSR_STIMECMPH, next_tval >> 32);
43#else
44 csr_write(CSR_STIMECMP, next_tval);
45#endif
46 } else
47 sbi_set_timer(next_tval);
48
62b01943
PD
49 return 0;
50}
51
033a65de 52static unsigned int riscv_clock_event_irq;
62b01943
PD
53static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
54 .name = "riscv_timer_clockevent",
d9f15a9d 55 .features = CLOCK_EVT_FEAT_ONESHOT,
62b01943
PD
56 .rating = 100,
57 .set_next_event = riscv_clock_next_event,
58};
59
60/*
61 * It is guaranteed that all the timers across all the harts are synchronized
62 * within one tick of each other, so while this could technically go
63 * backwards when hopping between CPUs, practically it won't happen.
64 */
65static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
66{
67 return get_cycles64();
68}
69
9d05c18e 70static u64 notrace riscv_sched_clock(void)
92e0d143
AP
71{
72 return get_cycles64();
73}
74
713203e3 75static struct clocksource riscv_clocksource = {
62b01943
PD
76 .name = "riscv_clocksource",
77 .rating = 300,
32d0be01 78 .mask = CLOCKSOURCE_MASK(64),
62b01943
PD
79 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
80 .read = riscv_clocksource_rdtime,
81};
82
83static int riscv_timer_starting_cpu(unsigned int cpu)
84{
85 struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
86
87 ce->cpumask = cpumask_of(cpu);
033a65de 88 ce->irq = riscv_clock_event_irq;
8932a953
AP
89 if (riscv_timer_cannot_wake_cpu)
90 ce->features |= CLOCK_EVT_FEAT_C3STOP;
62b01943
PD
91 clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
92
033a65de
AP
93 enable_percpu_irq(riscv_clock_event_irq,
94 irq_get_trigger_type(riscv_clock_event_irq));
62b01943
PD
95 return 0;
96}
97
98static int riscv_timer_dying_cpu(unsigned int cpu)
99{
033a65de 100 disable_percpu_irq(riscv_clock_event_irq);
62b01943
PD
101 return 0;
102}
103
3a9f66cb
AP
104void riscv_cs_get_mult_shift(u32 *mult, u32 *shift)
105{
106 *mult = riscv_clocksource.mult;
107 *shift = riscv_clocksource.shift;
108}
109EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift);
110
62b01943 111/* called directly from the low-level interrupt handler */
033a65de 112static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
62b01943
PD
113{
114 struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
115
a4c3733d 116 csr_clear(CSR_IE, IE_TIE);
62b01943 117 evdev->event_handler(evdev);
033a65de
AP
118
119 return IRQ_HANDLED;
62b01943
PD
120}
121
122static int __init riscv_timer_init_dt(struct device_node *n)
123{
ad635e72
S
124 int cpuid, error;
125 unsigned long hartid;
033a65de
AP
126 struct device_node *child;
127 struct irq_domain *domain;
62b01943 128
ad635e72
S
129 error = riscv_of_processor_hartid(n, &hartid);
130 if (error < 0) {
131 pr_warn("Not valid hartid for node [%pOF] error = [%lu]\n",
26478b2f 132 n, hartid);
ad635e72 133 return error;
26478b2f
AP
134 }
135
f99fb607 136 cpuid = riscv_hartid_to_cpuid(hartid);
26478b2f 137 if (cpuid < 0) {
ad635e72 138 pr_warn("Invalid cpuid for hartid [%lu]\n", hartid);
26478b2f
AP
139 return cpuid;
140 }
f99fb607
AP
141
142 if (cpuid != smp_processor_id())
62b01943
PD
143 return 0;
144
8932a953
AP
145 child = of_find_compatible_node(NULL, NULL, "riscv,timer");
146 if (child) {
147 riscv_timer_cannot_wake_cpu = of_property_read_bool(child,
148 "riscv,timer-cannot-wake-cpu");
149 of_node_put(child);
150 }
151
033a65de
AP
152 domain = NULL;
153 child = of_get_compatible_child(n, "riscv,cpu-intc");
154 if (!child) {
155 pr_err("Failed to find INTC node [%pOF]\n", n);
156 return -ENODEV;
157 }
158 domain = irq_find_host(child);
159 of_node_put(child);
160 if (!domain) {
161 pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
162 return -ENODEV;
163 }
164
165 riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
166 if (!riscv_clock_event_irq) {
167 pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
168 return -ENODEV;
169 }
170
ad635e72 171 pr_info("%s: Registering clocksource cpuid [%d] hartid [%lu]\n",
26478b2f 172 __func__, cpuid, hartid);
713203e3 173 error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
26478b2f
AP
174 if (error) {
175 pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
176 error, cpuid);
177 return error;
178 }
62b01943 179
32d0be01 180 sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
92e0d143 181
033a65de
AP
182 error = request_percpu_irq(riscv_clock_event_irq,
183 riscv_timer_interrupt,
184 "riscv-timer", &riscv_clock_event);
185 if (error) {
186 pr_err("registering percpu irq failed [%d]\n", error);
187 return error;
188 }
189
62b01943
PD
190 error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
191 "clockevents/riscv/timer:starting",
192 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
193 if (error)
26478b2f
AP
194 pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
195 error);
9f7a8ff6
AP
196
197 if (riscv_isa_extension_available(NULL, SSTC)) {
198 pr_info("Timer interrupt in S-mode is available via sstc extension\n");
199 static_branch_enable(&riscv_sstc_available);
200 }
201
62b01943
PD
202 return error;
203}
204
205TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);