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 
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>
269f7a8ff6SAtish Patra #include <asm/hwcap.h>
273d42b32bSDaniel Lezcano #include <asm/sbi.h>
282bc3fc87SAnup Patel #include <asm/timex.h>
294f9bbcefSChristoph Hellwig 
309f7a8ff6SAtish Patra static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
318932a953SAnup Patel static bool riscv_timer_cannot_wake_cpu;
329f7a8ff6SAtish Patra 
333d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
343d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
353d42b32bSDaniel Lezcano {
369f7a8ff6SAtish Patra 	u64 next_tval = get_cycles64() + delta;
379f7a8ff6SAtish Patra 
38a4c3733dSChristoph Hellwig 	csr_set(CSR_IE, IE_TIE);
399f7a8ff6SAtish Patra 	if (static_branch_likely(&riscv_sstc_available)) {
409f7a8ff6SAtish Patra #if defined(CONFIG_32BIT)
419f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF);
429f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMPH, next_tval >> 32);
439f7a8ff6SAtish Patra #else
449f7a8ff6SAtish Patra 		csr_write(CSR_STIMECMP, next_tval);
459f7a8ff6SAtish Patra #endif
469f7a8ff6SAtish Patra 	} else
479f7a8ff6SAtish Patra 		sbi_set_timer(next_tval);
489f7a8ff6SAtish Patra 
493d42b32bSDaniel Lezcano 	return 0;
503d42b32bSDaniel Lezcano }
513d42b32bSDaniel Lezcano 
52033a65deSAnup Patel static unsigned int riscv_clock_event_irq;
533d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
543d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
55d9f15a9dSConor Dooley 	.features		= CLOCK_EVT_FEAT_ONESHOT,
563d42b32bSDaniel Lezcano 	.rating			= 100,
573d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
583d42b32bSDaniel Lezcano };
593d42b32bSDaniel Lezcano 
603d42b32bSDaniel Lezcano /*
613d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
623d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
633d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
643d42b32bSDaniel Lezcano  */
653d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
663d42b32bSDaniel Lezcano {
673d42b32bSDaniel Lezcano 	return get_cycles64();
683d42b32bSDaniel Lezcano }
693d42b32bSDaniel Lezcano 
709d05c18eSZong Li static u64 notrace riscv_sched_clock(void)
713d42b32bSDaniel Lezcano {
723d42b32bSDaniel Lezcano 	return get_cycles64();
733d42b32bSDaniel Lezcano }
743d42b32bSDaniel Lezcano 
75713203e3SAtish Patra static struct clocksource riscv_clocksource = {
763d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
77674402b0SSamuel Holland 	.rating		= 400,
7832d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
793d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
803d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
813aff0403SLad Prabhakar #if IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY)
823aff0403SLad Prabhakar 	.vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
833aff0403SLad Prabhakar #else
843aff0403SLad Prabhakar 	.vdso_clock_mode = VDSO_CLOCKMODE_NONE,
853aff0403SLad Prabhakar #endif
863d42b32bSDaniel Lezcano };
873d42b32bSDaniel Lezcano 
883d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
893d42b32bSDaniel Lezcano {
903d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
913d42b32bSDaniel Lezcano 
923d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
93033a65deSAnup Patel 	ce->irq = riscv_clock_event_irq;
948932a953SAnup Patel 	if (riscv_timer_cannot_wake_cpu)
958932a953SAnup Patel 		ce->features |= CLOCK_EVT_FEAT_C3STOP;
963d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
973d42b32bSDaniel Lezcano 
98033a65deSAnup Patel 	enable_percpu_irq(riscv_clock_event_irq,
99033a65deSAnup Patel 			  irq_get_trigger_type(riscv_clock_event_irq));
1003d42b32bSDaniel Lezcano 	return 0;
1013d42b32bSDaniel Lezcano }
1023d42b32bSDaniel Lezcano 
1033d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
1043d42b32bSDaniel Lezcano {
105033a65deSAnup Patel 	disable_percpu_irq(riscv_clock_event_irq);
1063d42b32bSDaniel Lezcano 	return 0;
1073d42b32bSDaniel Lezcano }
1083d42b32bSDaniel Lezcano 
1093a9f66cbSAtish Patra void riscv_cs_get_mult_shift(u32 *mult, u32 *shift)
1103a9f66cbSAtish Patra {
1113a9f66cbSAtish Patra 	*mult = riscv_clocksource.mult;
1123a9f66cbSAtish Patra 	*shift = riscv_clocksource.shift;
1133a9f66cbSAtish Patra }
1143a9f66cbSAtish Patra EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift);
1153a9f66cbSAtish Patra 
1163d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
117033a65deSAnup Patel static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
1183d42b32bSDaniel Lezcano {
1193d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
1203d42b32bSDaniel Lezcano 
121a4c3733dSChristoph Hellwig 	csr_clear(CSR_IE, IE_TIE);
1223d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
123033a65deSAnup Patel 
124033a65deSAnup Patel 	return IRQ_HANDLED;
1253d42b32bSDaniel Lezcano }
1263d42b32bSDaniel Lezcano 
1273d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
1283d42b32bSDaniel Lezcano {
129ad635e72SSunil V L 	int cpuid, error;
130ad635e72SSunil V L 	unsigned long hartid;
131033a65deSAnup Patel 	struct device_node *child;
132033a65deSAnup Patel 	struct irq_domain *domain;
1333d42b32bSDaniel Lezcano 
134ad635e72SSunil V L 	error = riscv_of_processor_hartid(n, &hartid);
135ad635e72SSunil V L 	if (error < 0) {
136ad635e72SSunil V L 		pr_warn("Not valid hartid for node [%pOF] error = [%lu]\n",
13726478b2fSAtish Patra 			n, hartid);
138ad635e72SSunil V L 		return error;
13926478b2fSAtish Patra 	}
14026478b2fSAtish Patra 
1413d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
14226478b2fSAtish Patra 	if (cpuid < 0) {
143ad635e72SSunil V L 		pr_warn("Invalid cpuid for hartid [%lu]\n", hartid);
14426478b2fSAtish Patra 		return cpuid;
14526478b2fSAtish Patra 	}
1463d42b32bSDaniel Lezcano 
1473d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1483d42b32bSDaniel Lezcano 		return 0;
1493d42b32bSDaniel Lezcano 
1508932a953SAnup Patel 	child = of_find_compatible_node(NULL, NULL, "riscv,timer");
1518932a953SAnup Patel 	if (child) {
1528932a953SAnup Patel 		riscv_timer_cannot_wake_cpu = of_property_read_bool(child,
1538932a953SAnup Patel 					"riscv,timer-cannot-wake-cpu");
1548932a953SAnup Patel 		of_node_put(child);
1558932a953SAnup Patel 	}
1568932a953SAnup Patel 
157033a65deSAnup Patel 	domain = NULL;
158033a65deSAnup Patel 	child = of_get_compatible_child(n, "riscv,cpu-intc");
159033a65deSAnup Patel 	if (!child) {
160033a65deSAnup Patel 		pr_err("Failed to find INTC node [%pOF]\n", n);
161033a65deSAnup Patel 		return -ENODEV;
162033a65deSAnup Patel 	}
163033a65deSAnup Patel 	domain = irq_find_host(child);
164033a65deSAnup Patel 	of_node_put(child);
165033a65deSAnup Patel 	if (!domain) {
166033a65deSAnup Patel 		pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
167033a65deSAnup Patel 		return -ENODEV;
168033a65deSAnup Patel 	}
169033a65deSAnup Patel 
170033a65deSAnup Patel 	riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
171033a65deSAnup Patel 	if (!riscv_clock_event_irq) {
172033a65deSAnup Patel 		pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
173033a65deSAnup Patel 		return -ENODEV;
174033a65deSAnup Patel 	}
175033a65deSAnup Patel 
176ad635e72SSunil V L 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%lu]\n",
17726478b2fSAtish Patra 	       __func__, cpuid, hartid);
178713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
17926478b2fSAtish Patra 	if (error) {
18026478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
18126478b2fSAtish Patra 		       error, cpuid);
18226478b2fSAtish Patra 		return error;
18326478b2fSAtish Patra 	}
1843d42b32bSDaniel Lezcano 
18532d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1863d42b32bSDaniel Lezcano 
187033a65deSAnup Patel 	error = request_percpu_irq(riscv_clock_event_irq,
188033a65deSAnup Patel 				    riscv_timer_interrupt,
189033a65deSAnup Patel 				    "riscv-timer", &riscv_clock_event);
190033a65deSAnup Patel 	if (error) {
191033a65deSAnup Patel 		pr_err("registering percpu irq failed [%d]\n", error);
192033a65deSAnup Patel 		return error;
193033a65deSAnup Patel 	}
194033a65deSAnup Patel 
195*225b9596SMatt Evans 	if (riscv_isa_extension_available(NULL, SSTC)) {
196*225b9596SMatt Evans 		pr_info("Timer interrupt in S-mode is available via sstc extension\n");
197*225b9596SMatt Evans 		static_branch_enable(&riscv_sstc_available);
198*225b9596SMatt Evans 	}
199*225b9596SMatt Evans 
2003d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
2013d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
2023d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
2033d42b32bSDaniel Lezcano 	if (error)
20426478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
20526478b2fSAtish Patra 		       error);
2069f7a8ff6SAtish Patra 
2073d42b32bSDaniel Lezcano 	return error;
2083d42b32bSDaniel Lezcano }
2093d42b32bSDaniel Lezcano 
2103d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
211