13d42b32bSDaniel Lezcano // SPDX-License-Identifier: GPL-2.0
23d42b32bSDaniel Lezcano /*
33d42b32bSDaniel Lezcano  * Copyright (C) 2012 Regents of the University of California
43d42b32bSDaniel Lezcano  * Copyright (C) 2017 SiFive
52f12dbf1SChristoph Hellwig  *
64f9bbcefSChristoph Hellwig  * All RISC-V systems have a timer attached to every hart.  These timers can
74f9bbcefSChristoph Hellwig  * either be read from the "time" and "timeh" CSRs, and can use the SBI to
84f9bbcefSChristoph Hellwig  * setup events, or directly accessed using MMIO registers.
93d42b32bSDaniel Lezcano  */
109f7a8ff6SAtish Patra 
119f7a8ff6SAtish Patra #define pr_fmt(fmt) "riscv-timer: " fmt
129f7a8ff6SAtish Patra 
1321f4f924SSunil V L #include <linux/acpi.h>
143d42b32bSDaniel Lezcano #include <linux/clocksource.h>
153d42b32bSDaniel Lezcano #include <linux/clockchips.h>
163d42b32bSDaniel Lezcano #include <linux/cpu.h>
173d42b32bSDaniel Lezcano #include <linux/delay.h>
183d42b32bSDaniel Lezcano #include <linux/irq.h>
19033a65deSAnup Patel #include <linux/irqdomain.h>
203a9f66cbSAtish Patra #include <linux/module.h>
213d42b32bSDaniel Lezcano #include <linux/sched_clock.h>
224f9bbcefSChristoph Hellwig #include <linux/io-64-nonatomic-lo-hi.h>
23033a65deSAnup Patel #include <linux/interrupt.h>
24033a65deSAnup Patel #include <linux/of_irq.h>
255d98446fSAnup Patel #include <linux/limits.h>
263a9f66cbSAtish Patra #include <clocksource/timer-riscv.h>
273d42b32bSDaniel Lezcano #include <asm/smp.h>
28e72c4333SXiao Wang #include <asm/cpufeature.h>
293d42b32bSDaniel Lezcano #include <asm/sbi.h>
302bc3fc87SAnup Patel #include <asm/timex.h>
314f9bbcefSChristoph Hellwig 
329f7a8ff6SAtish Patra static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
338932a953SAnup Patel static bool riscv_timer_cannot_wake_cpu;
349f7a8ff6SAtish Patra 
355d98446fSAnup Patel static void riscv_clock_event_stop(void)
365d98446fSAnup Patel {
375d98446fSAnup Patel 	if (static_branch_likely(&riscv_sstc_available)) {
385d98446fSAnup Patel 		csr_write(CSR_STIMECMP, ULONG_MAX);
395d98446fSAnup Patel 		if (IS_ENABLED(CONFIG_32BIT))
405d98446fSAnup Patel 			csr_write(CSR_STIMECMPH, ULONG_MAX);
415d98446fSAnup Patel 	} else {
425d98446fSAnup Patel 		sbi_set_timer(U64_MAX);
435d98446fSAnup Patel 	}
445d98446fSAnup Patel }
455d98446fSAnup Patel 
463d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
473d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
483d42b32bSDaniel Lezcano {
499f7a8ff6SAtish Patra 	u64 next_tval = get_cycles64() + delta;
509f7a8ff6SAtish Patra 
519f7a8ff6SAtish Patra 	if (static_branch_likely(&riscv_sstc_available)) {
529f7a8ff6SAtish Patra #if defined(CONFIG_32BIT)
539f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF);
549f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMPH, next_tval >> 32);
559f7a8ff6SAtish Patra #else
569f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMP, next_tval);
579f7a8ff6SAtish Patra #endif
589f7a8ff6SAtish Patra 	} else
599f7a8ff6SAtish Patra 		sbi_set_timer(next_tval);
609f7a8ff6SAtish Patra 
613d42b32bSDaniel Lezcano 	return 0;
623d42b32bSDaniel Lezcano }
633d42b32bSDaniel Lezcano 
64*6a902b11SJoshua Yeong static int riscv_clock_shutdown(struct clock_event_device *evt)
65*6a902b11SJoshua Yeong {
66*6a902b11SJoshua Yeong 	riscv_clock_event_stop();
67*6a902b11SJoshua Yeong 	return 0;
68*6a902b11SJoshua Yeong }
69*6a902b11SJoshua Yeong 
70033a65deSAnup Patel static unsigned int riscv_clock_event_irq;
713d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
723d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
73d9f15a9dSConor Dooley 	.features		= CLOCK_EVT_FEAT_ONESHOT,
743d42b32bSDaniel Lezcano 	.rating			= 100,
753d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
76*6a902b11SJoshua Yeong 	.set_state_shutdown	= riscv_clock_shutdown,
773d42b32bSDaniel Lezcano };
783d42b32bSDaniel Lezcano 
793d42b32bSDaniel Lezcano /*
803d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
813d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
823d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
833d42b32bSDaniel Lezcano  */
843d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
853d42b32bSDaniel Lezcano {
863d42b32bSDaniel Lezcano 	return get_cycles64();
873d42b32bSDaniel Lezcano }
883d42b32bSDaniel Lezcano 
899d05c18eSZong Li static u64 notrace riscv_sched_clock(void)
903d42b32bSDaniel Lezcano {
913d42b32bSDaniel Lezcano 	return get_cycles64();
923d42b32bSDaniel Lezcano }
933d42b32bSDaniel Lezcano 
94713203e3SAtish Patra static struct clocksource riscv_clocksource = {
953d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
96674402b0SSamuel Holland 	.rating		= 400,
9732d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
983d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
993d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
1003aff0403SLad Prabhakar #if IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY)
1013aff0403SLad Prabhakar 	.vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
1023aff0403SLad Prabhakar #else
1033aff0403SLad Prabhakar 	.vdso_clock_mode = VDSO_CLOCKMODE_NONE,
1043aff0403SLad Prabhakar #endif
1053d42b32bSDaniel Lezcano };
1063d42b32bSDaniel Lezcano 
1073d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
1083d42b32bSDaniel Lezcano {
1093d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
1103d42b32bSDaniel Lezcano 
1113d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
112033a65deSAnup Patel 	ce->irq = riscv_clock_event_irq;
1138932a953SAnup Patel 	if (riscv_timer_cannot_wake_cpu)
1148932a953SAnup Patel 		ce->features |= CLOCK_EVT_FEAT_C3STOP;
11560c46877SAnup Patel 	if (static_branch_likely(&riscv_sstc_available))
11660c46877SAnup Patel 		ce->rating = 450;
1173d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
1183d42b32bSDaniel Lezcano 
119033a65deSAnup Patel 	enable_percpu_irq(riscv_clock_event_irq,
120033a65deSAnup Patel 			  irq_get_trigger_type(riscv_clock_event_irq));
1213d42b32bSDaniel Lezcano 	return 0;
1223d42b32bSDaniel Lezcano }
1233d42b32bSDaniel Lezcano 
1243d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
1253d42b32bSDaniel Lezcano {
126033a65deSAnup Patel 	disable_percpu_irq(riscv_clock_event_irq);
1273d42b32bSDaniel Lezcano 	return 0;
1283d42b32bSDaniel Lezcano }
1293d42b32bSDaniel Lezcano 
1303a9f66cbSAtish Patra void riscv_cs_get_mult_shift(u32 *mult, u32 *shift)
1313a9f66cbSAtish Patra {
1323a9f66cbSAtish Patra 	*mult = riscv_clocksource.mult;
1333a9f66cbSAtish Patra 	*shift = riscv_clocksource.shift;
1343a9f66cbSAtish Patra }
1353a9f66cbSAtish Patra EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift);
1363a9f66cbSAtish Patra 
1373d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
138033a65deSAnup Patel static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
1393d42b32bSDaniel Lezcano {
1403d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
1413d42b32bSDaniel Lezcano 
1425d98446fSAnup Patel 	riscv_clock_event_stop();
1433d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
144033a65deSAnup Patel 
145033a65deSAnup Patel 	return IRQ_HANDLED;
1463d42b32bSDaniel Lezcano }
1473d42b32bSDaniel Lezcano 
148cd12d206SSunil V L static int __init riscv_timer_init_common(void)
1493d42b32bSDaniel Lezcano {
150cd12d206SSunil V L 	int error;
151033a65deSAnup Patel 	struct irq_domain *domain;
152cd12d206SSunil V L 	struct fwnode_handle *intc_fwnode = riscv_get_intc_hwnode();
1533d42b32bSDaniel Lezcano 
154cd12d206SSunil V L 	domain = irq_find_matching_fwnode(intc_fwnode, DOMAIN_BUS_ANY);
155033a65deSAnup Patel 	if (!domain) {
156cd12d206SSunil V L 		pr_err("Failed to find irq_domain for INTC node [%pfwP]\n",
157cd12d206SSunil V L 		       intc_fwnode);
158033a65deSAnup Patel 		return -ENODEV;
159033a65deSAnup Patel 	}
160033a65deSAnup Patel 
161033a65deSAnup Patel 	riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
162033a65deSAnup Patel 	if (!riscv_clock_event_irq) {
163cd12d206SSunil V L 		pr_err("Failed to map timer interrupt for node [%pfwP]\n", intc_fwnode);
164033a65deSAnup Patel 		return -ENODEV;
165033a65deSAnup Patel 	}
166033a65deSAnup Patel 
167713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
16826478b2fSAtish Patra 	if (error) {
169cd12d206SSunil V L 		pr_err("RISCV timer registration failed [%d]\n", error);
17026478b2fSAtish Patra 		return error;
17126478b2fSAtish Patra 	}
1723d42b32bSDaniel Lezcano 
17332d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1743d42b32bSDaniel Lezcano 
175033a65deSAnup Patel 	error = request_percpu_irq(riscv_clock_event_irq,
176033a65deSAnup Patel 				    riscv_timer_interrupt,
177033a65deSAnup Patel 				    "riscv-timer", &riscv_clock_event);
178033a65deSAnup Patel 	if (error) {
179033a65deSAnup Patel 		pr_err("registering percpu irq failed [%d]\n", error);
180033a65deSAnup Patel 		return error;
181033a65deSAnup Patel 	}
182033a65deSAnup Patel 
183225b9596SMatt Evans 	if (riscv_isa_extension_available(NULL, SSTC)) {
184225b9596SMatt Evans 		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
185225b9596SMatt Evans 		static_branch_enable(&riscv_sstc_available);
186225b9596SMatt Evans 	}
187225b9596SMatt Evans 
1883d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1893d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1903d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1913d42b32bSDaniel Lezcano 	if (error)
19226478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
19326478b2fSAtish Patra 		       error);
1949f7a8ff6SAtish Patra 
1953d42b32bSDaniel Lezcano 	return error;
1963d42b32bSDaniel Lezcano }
1973d42b32bSDaniel Lezcano 
198cd12d206SSunil V L static int __init riscv_timer_init_dt(struct device_node *n)
199cd12d206SSunil V L {
200cd12d206SSunil V L 	int cpuid, error;
201cd12d206SSunil V L 	unsigned long hartid;
202cd12d206SSunil V L 	struct device_node *child;
203cd12d206SSunil V L 
204cd12d206SSunil V L 	error = riscv_of_processor_hartid(n, &hartid);
205cd12d206SSunil V L 	if (error < 0) {
206cd12d206SSunil V L 		pr_warn("Invalid hartid for node [%pOF] error = [%lu]\n",
207cd12d206SSunil V L 			n, hartid);
208cd12d206SSunil V L 		return error;
209cd12d206SSunil V L 	}
210cd12d206SSunil V L 
211cd12d206SSunil V L 	cpuid = riscv_hartid_to_cpuid(hartid);
212cd12d206SSunil V L 	if (cpuid < 0) {
213cd12d206SSunil V L 		pr_warn("Invalid cpuid for hartid [%lu]\n", hartid);
214cd12d206SSunil V L 		return cpuid;
215cd12d206SSunil V L 	}
216cd12d206SSunil V L 
217cd12d206SSunil V L 	if (cpuid != smp_processor_id())
218cd12d206SSunil V L 		return 0;
219cd12d206SSunil V L 
220cd12d206SSunil V L 	child = of_find_compatible_node(NULL, NULL, "riscv,timer");
221cd12d206SSunil V L 	if (child) {
222cd12d206SSunil V L 		riscv_timer_cannot_wake_cpu = of_property_read_bool(child,
223cd12d206SSunil V L 					"riscv,timer-cannot-wake-cpu");
224cd12d206SSunil V L 		of_node_put(child);
225cd12d206SSunil V L 	}
226cd12d206SSunil V L 
227cd12d206SSunil V L 	return riscv_timer_init_common();
228cd12d206SSunil V L }
229cd12d206SSunil V L 
2303d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
23121f4f924SSunil V L 
23221f4f924SSunil V L #ifdef CONFIG_ACPI
23321f4f924SSunil V L static int __init riscv_timer_acpi_init(struct acpi_table_header *table)
23421f4f924SSunil V L {
235d7f546c7SSunil V L 	struct acpi_table_rhct *rhct = (struct acpi_table_rhct *)table;
236d7f546c7SSunil V L 
237d7f546c7SSunil V L 	riscv_timer_cannot_wake_cpu = rhct->flags & ACPI_RHCT_TIMER_CANNOT_WAKEUP_CPU;
238d7f546c7SSunil V L 
23921f4f924SSunil V L 	return riscv_timer_init_common();
24021f4f924SSunil V L }
24121f4f924SSunil V L 
24221f4f924SSunil V L TIMER_ACPI_DECLARE(aclint_mtimer, ACPI_SIG_RHCT, riscv_timer_acpi_init);
24321f4f924SSunil V L 
24421f4f924SSunil V L #endif
245