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
53d42b32bSDaniel Lezcano  */
63d42b32bSDaniel Lezcano #include <linux/clocksource.h>
73d42b32bSDaniel Lezcano #include <linux/clockchips.h>
83d42b32bSDaniel Lezcano #include <linux/cpu.h>
93d42b32bSDaniel Lezcano #include <linux/delay.h>
103d42b32bSDaniel Lezcano #include <linux/irq.h>
113d42b32bSDaniel Lezcano #include <linux/sched_clock.h>
123d42b32bSDaniel Lezcano #include <asm/smp.h>
133d42b32bSDaniel Lezcano #include <asm/sbi.h>
143d42b32bSDaniel Lezcano 
153d42b32bSDaniel Lezcano /*
163d42b32bSDaniel Lezcano  * All RISC-V systems have a timer attached to every hart.  These timers can be
173d42b32bSDaniel Lezcano  * read by the 'rdcycle' pseudo instruction, and can use the SBI to setup
183d42b32bSDaniel Lezcano  * events.  In order to abstract the architecture-specific timer reading and
193d42b32bSDaniel Lezcano  * setting functions away from the clock event insertion code, we provide
203d42b32bSDaniel Lezcano  * function pointers to the clockevent subsystem that perform two basic
213d42b32bSDaniel Lezcano  * operations: rdtime() reads the timer on the current CPU, and
223d42b32bSDaniel Lezcano  * next_event(delta) sets the next timer event to 'delta' cycles in the future.
233d42b32bSDaniel Lezcano  * As the timers are inherently a per-cpu resource, these callbacks perform
243d42b32bSDaniel Lezcano  * operations on the current hart.  There is guaranteed to be exactly one timer
253d42b32bSDaniel Lezcano  * per hart on all RISC-V systems.
263d42b32bSDaniel Lezcano  */
273d42b32bSDaniel Lezcano 
283d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
293d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
303d42b32bSDaniel Lezcano {
313d42b32bSDaniel Lezcano 	csr_set(sie, SIE_STIE);
323d42b32bSDaniel Lezcano 	sbi_set_timer(get_cycles64() + delta);
333d42b32bSDaniel Lezcano 	return 0;
343d42b32bSDaniel Lezcano }
353d42b32bSDaniel Lezcano 
363d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
373d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
383d42b32bSDaniel Lezcano 	.features		= CLOCK_EVT_FEAT_ONESHOT,
393d42b32bSDaniel Lezcano 	.rating			= 100,
403d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
413d42b32bSDaniel Lezcano };
423d42b32bSDaniel Lezcano 
433d42b32bSDaniel Lezcano /*
443d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
453d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
463d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
473d42b32bSDaniel Lezcano  */
483d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
493d42b32bSDaniel Lezcano {
503d42b32bSDaniel Lezcano 	return get_cycles64();
513d42b32bSDaniel Lezcano }
523d42b32bSDaniel Lezcano 
533d42b32bSDaniel Lezcano static u64 riscv_sched_clock(void)
543d42b32bSDaniel Lezcano {
553d42b32bSDaniel Lezcano 	return get_cycles64();
563d42b32bSDaniel Lezcano }
573d42b32bSDaniel Lezcano 
583d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clocksource, riscv_clocksource) = {
593d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
603d42b32bSDaniel Lezcano 	.rating		= 300,
613d42b32bSDaniel Lezcano 	.mask		= CLOCKSOURCE_MASK(BITS_PER_LONG),
623d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
633d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
643d42b32bSDaniel Lezcano };
653d42b32bSDaniel Lezcano 
663d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
673d42b32bSDaniel Lezcano {
683d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
693d42b32bSDaniel Lezcano 
703d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
713d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
723d42b32bSDaniel Lezcano 
733d42b32bSDaniel Lezcano 	csr_set(sie, SIE_STIE);
743d42b32bSDaniel Lezcano 	return 0;
753d42b32bSDaniel Lezcano }
763d42b32bSDaniel Lezcano 
773d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
783d42b32bSDaniel Lezcano {
793d42b32bSDaniel Lezcano 	csr_clear(sie, SIE_STIE);
803d42b32bSDaniel Lezcano 	return 0;
813d42b32bSDaniel Lezcano }
823d42b32bSDaniel Lezcano 
833d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
843d42b32bSDaniel Lezcano void riscv_timer_interrupt(void)
853d42b32bSDaniel Lezcano {
863d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
873d42b32bSDaniel Lezcano 
883d42b32bSDaniel Lezcano 	csr_clear(sie, SIE_STIE);
893d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
903d42b32bSDaniel Lezcano }
913d42b32bSDaniel Lezcano 
923d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
933d42b32bSDaniel Lezcano {
943d42b32bSDaniel Lezcano 	int cpuid, hartid, error;
953d42b32bSDaniel Lezcano 	struct clocksource *cs;
963d42b32bSDaniel Lezcano 
973d42b32bSDaniel Lezcano 	hartid = riscv_of_processor_hartid(n);
98*26478b2fSAtish Patra 	if (hartid < 0) {
99*26478b2fSAtish Patra 		pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
100*26478b2fSAtish Patra 			n, hartid);
101*26478b2fSAtish Patra 		return hartid;
102*26478b2fSAtish Patra 	}
103*26478b2fSAtish Patra 
1043d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
105*26478b2fSAtish Patra 	if (cpuid < 0) {
106*26478b2fSAtish Patra 		pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
107*26478b2fSAtish Patra 		return cpuid;
108*26478b2fSAtish Patra 	}
1093d42b32bSDaniel Lezcano 
1103d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1113d42b32bSDaniel Lezcano 		return 0;
1123d42b32bSDaniel Lezcano 
113*26478b2fSAtish Patra 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
114*26478b2fSAtish Patra 	       __func__, cpuid, hartid);
1153d42b32bSDaniel Lezcano 	cs = per_cpu_ptr(&riscv_clocksource, cpuid);
116*26478b2fSAtish Patra 	error = clocksource_register_hz(cs, riscv_timebase);
117*26478b2fSAtish Patra 	if (error) {
118*26478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
119*26478b2fSAtish Patra 		       error, cpuid);
120*26478b2fSAtish Patra 		return error;
121*26478b2fSAtish Patra 	}
1223d42b32bSDaniel Lezcano 
1233d42b32bSDaniel Lezcano 	sched_clock_register(riscv_sched_clock,
1243d42b32bSDaniel Lezcano 			BITS_PER_LONG, riscv_timebase);
1253d42b32bSDaniel Lezcano 
1263d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1273d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1283d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1293d42b32bSDaniel Lezcano 	if (error)
130*26478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
131*26478b2fSAtish Patra 		       error);
1323d42b32bSDaniel Lezcano 	return error;
1333d42b32bSDaniel Lezcano }
1343d42b32bSDaniel Lezcano 
1353d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
136