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  */
10*9f7a8ff6SAtish Patra 
11*9f7a8ff6SAtish Patra #define pr_fmt(fmt) "riscv-timer: " fmt
12*9f7a8ff6SAtish Patra 
133d42b32bSDaniel Lezcano #include <linux/clocksource.h>
143d42b32bSDaniel Lezcano #include <linux/clockchips.h>
153d42b32bSDaniel Lezcano #include <linux/cpu.h>
163d42b32bSDaniel Lezcano #include <linux/delay.h>
173d42b32bSDaniel Lezcano #include <linux/irq.h>
18033a65deSAnup Patel #include <linux/irqdomain.h>
193a9f66cbSAtish Patra #include <linux/module.h>
203d42b32bSDaniel Lezcano #include <linux/sched_clock.h>
214f9bbcefSChristoph Hellwig #include <linux/io-64-nonatomic-lo-hi.h>
22033a65deSAnup Patel #include <linux/interrupt.h>
23033a65deSAnup Patel #include <linux/of_irq.h>
243a9f66cbSAtish Patra #include <clocksource/timer-riscv.h>
253d42b32bSDaniel Lezcano #include <asm/smp.h>
26*9f7a8ff6SAtish Patra #include <asm/hwcap.h>
273d42b32bSDaniel Lezcano #include <asm/sbi.h>
282bc3fc87SAnup Patel #include <asm/timex.h>
294f9bbcefSChristoph Hellwig 
30*9f7a8ff6SAtish Patra static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
31*9f7a8ff6SAtish Patra 
323d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
333d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
343d42b32bSDaniel Lezcano {
35*9f7a8ff6SAtish Patra 	u64 next_tval = get_cycles64() + delta;
36*9f7a8ff6SAtish Patra 
37a4c3733dSChristoph Hellwig 	csr_set(CSR_IE, IE_TIE);
38*9f7a8ff6SAtish Patra 	if (static_branch_likely(&riscv_sstc_available)) {
39*9f7a8ff6SAtish Patra #if defined(CONFIG_32BIT)
40*9f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF);
41*9f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMPH, next_tval >> 32);
42*9f7a8ff6SAtish Patra #else
43*9f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMP, next_tval);
44*9f7a8ff6SAtish Patra #endif
45*9f7a8ff6SAtish Patra 	} else
46*9f7a8ff6SAtish Patra 		sbi_set_timer(next_tval);
47*9f7a8ff6SAtish Patra 
483d42b32bSDaniel Lezcano 	return 0;
493d42b32bSDaniel Lezcano }
503d42b32bSDaniel Lezcano 
51033a65deSAnup Patel static unsigned int riscv_clock_event_irq;
523d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
533d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
54232ccac1SSamuel Holland 	.features		= CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP,
553d42b32bSDaniel Lezcano 	.rating			= 100,
563d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
573d42b32bSDaniel Lezcano };
583d42b32bSDaniel Lezcano 
593d42b32bSDaniel Lezcano /*
603d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
613d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
623d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
633d42b32bSDaniel Lezcano  */
643d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
653d42b32bSDaniel Lezcano {
663d42b32bSDaniel Lezcano 	return get_cycles64();
673d42b32bSDaniel Lezcano }
683d42b32bSDaniel Lezcano 
699d05c18eSZong Li static u64 notrace riscv_sched_clock(void)
703d42b32bSDaniel Lezcano {
713d42b32bSDaniel Lezcano 	return get_cycles64();
723d42b32bSDaniel Lezcano }
733d42b32bSDaniel Lezcano 
74713203e3SAtish Patra static struct clocksource riscv_clocksource = {
753d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
763d42b32bSDaniel Lezcano 	.rating		= 300,
7732d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
783d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
793d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
803d42b32bSDaniel Lezcano };
813d42b32bSDaniel Lezcano 
823d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
833d42b32bSDaniel Lezcano {
843d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
853d42b32bSDaniel Lezcano 
863d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
87033a65deSAnup Patel 	ce->irq = riscv_clock_event_irq;
883d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
893d42b32bSDaniel Lezcano 
90033a65deSAnup Patel 	enable_percpu_irq(riscv_clock_event_irq,
91033a65deSAnup Patel 			  irq_get_trigger_type(riscv_clock_event_irq));
923d42b32bSDaniel Lezcano 	return 0;
933d42b32bSDaniel Lezcano }
943d42b32bSDaniel Lezcano 
953d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
963d42b32bSDaniel Lezcano {
97033a65deSAnup Patel 	disable_percpu_irq(riscv_clock_event_irq);
983d42b32bSDaniel Lezcano 	return 0;
993d42b32bSDaniel Lezcano }
1003d42b32bSDaniel Lezcano 
1013a9f66cbSAtish Patra void riscv_cs_get_mult_shift(u32 *mult, u32 *shift)
1023a9f66cbSAtish Patra {
1033a9f66cbSAtish Patra 	*mult = riscv_clocksource.mult;
1043a9f66cbSAtish Patra 	*shift = riscv_clocksource.shift;
1053a9f66cbSAtish Patra }
1063a9f66cbSAtish Patra EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift);
1073a9f66cbSAtish Patra 
1083d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
109033a65deSAnup Patel static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
1103d42b32bSDaniel Lezcano {
1113d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
1123d42b32bSDaniel Lezcano 
113a4c3733dSChristoph Hellwig 	csr_clear(CSR_IE, IE_TIE);
1143d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
115033a65deSAnup Patel 
116033a65deSAnup Patel 	return IRQ_HANDLED;
1173d42b32bSDaniel Lezcano }
1183d42b32bSDaniel Lezcano 
1193d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
1203d42b32bSDaniel Lezcano {
1213d42b32bSDaniel Lezcano 	int cpuid, hartid, error;
122033a65deSAnup Patel 	struct device_node *child;
123033a65deSAnup Patel 	struct irq_domain *domain;
1243d42b32bSDaniel Lezcano 
1253d42b32bSDaniel Lezcano 	hartid = riscv_of_processor_hartid(n);
12626478b2fSAtish Patra 	if (hartid < 0) {
12726478b2fSAtish Patra 		pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
12826478b2fSAtish Patra 			n, hartid);
12926478b2fSAtish Patra 		return hartid;
13026478b2fSAtish Patra 	}
13126478b2fSAtish Patra 
1323d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
13326478b2fSAtish Patra 	if (cpuid < 0) {
13426478b2fSAtish Patra 		pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
13526478b2fSAtish Patra 		return cpuid;
13626478b2fSAtish Patra 	}
1373d42b32bSDaniel Lezcano 
1383d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1393d42b32bSDaniel Lezcano 		return 0;
1403d42b32bSDaniel Lezcano 
141033a65deSAnup Patel 	domain = NULL;
142033a65deSAnup Patel 	child = of_get_compatible_child(n, "riscv,cpu-intc");
143033a65deSAnup Patel 	if (!child) {
144033a65deSAnup Patel 		pr_err("Failed to find INTC node [%pOF]\n", n);
145033a65deSAnup Patel 		return -ENODEV;
146033a65deSAnup Patel 	}
147033a65deSAnup Patel 	domain = irq_find_host(child);
148033a65deSAnup Patel 	of_node_put(child);
149033a65deSAnup Patel 	if (!domain) {
150033a65deSAnup Patel 		pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
151033a65deSAnup Patel 		return -ENODEV;
152033a65deSAnup Patel 	}
153033a65deSAnup Patel 
154033a65deSAnup Patel 	riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
155033a65deSAnup Patel 	if (!riscv_clock_event_irq) {
156033a65deSAnup Patel 		pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
157033a65deSAnup Patel 		return -ENODEV;
158033a65deSAnup Patel 	}
159033a65deSAnup Patel 
16026478b2fSAtish Patra 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
16126478b2fSAtish Patra 	       __func__, cpuid, hartid);
162713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
16326478b2fSAtish Patra 	if (error) {
16426478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
16526478b2fSAtish Patra 		       error, cpuid);
16626478b2fSAtish Patra 		return error;
16726478b2fSAtish Patra 	}
1683d42b32bSDaniel Lezcano 
16932d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1703d42b32bSDaniel Lezcano 
171033a65deSAnup Patel 	error = request_percpu_irq(riscv_clock_event_irq,
172033a65deSAnup Patel 				    riscv_timer_interrupt,
173033a65deSAnup Patel 				    "riscv-timer", &riscv_clock_event);
174033a65deSAnup Patel 	if (error) {
175033a65deSAnup Patel 		pr_err("registering percpu irq failed [%d]\n", error);
176033a65deSAnup Patel 		return error;
177033a65deSAnup Patel 	}
178033a65deSAnup Patel 
1793d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1803d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1813d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1823d42b32bSDaniel Lezcano 	if (error)
18326478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
18426478b2fSAtish Patra 		       error);
185*9f7a8ff6SAtish Patra 
186*9f7a8ff6SAtish Patra 	if (riscv_isa_extension_available(NULL, SSTC)) {
187*9f7a8ff6SAtish Patra 		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
188*9f7a8ff6SAtish Patra 		static_branch_enable(&riscv_sstc_available);
189*9f7a8ff6SAtish Patra 	}
190*9f7a8ff6SAtish Patra 
1913d42b32bSDaniel Lezcano 	return error;
1923d42b32bSDaniel Lezcano }
1933d42b32bSDaniel Lezcano 
1943d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
195