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
5*2f12dbf1SChristoph Hellwig  *
6*2f12dbf1SChristoph Hellwig  * All RISC-V systems have a timer attached to every hart.  These timers can be
7*2f12dbf1SChristoph Hellwig  * read from the "time" and "timeh" CSRs, and can use the SBI to setup
8*2f12dbf1SChristoph Hellwig  * events.
93d42b32bSDaniel Lezcano  */
103d42b32bSDaniel Lezcano #include <linux/clocksource.h>
113d42b32bSDaniel Lezcano #include <linux/clockchips.h>
123d42b32bSDaniel Lezcano #include <linux/cpu.h>
133d42b32bSDaniel Lezcano #include <linux/delay.h>
143d42b32bSDaniel Lezcano #include <linux/irq.h>
153d42b32bSDaniel Lezcano #include <linux/sched_clock.h>
163d42b32bSDaniel Lezcano #include <asm/smp.h>
173d42b32bSDaniel Lezcano #include <asm/sbi.h>
183d42b32bSDaniel Lezcano 
193d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
203d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
213d42b32bSDaniel Lezcano {
223d42b32bSDaniel Lezcano 	csr_set(sie, SIE_STIE);
233d42b32bSDaniel Lezcano 	sbi_set_timer(get_cycles64() + delta);
243d42b32bSDaniel Lezcano 	return 0;
253d42b32bSDaniel Lezcano }
263d42b32bSDaniel Lezcano 
273d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
283d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
293d42b32bSDaniel Lezcano 	.features		= CLOCK_EVT_FEAT_ONESHOT,
303d42b32bSDaniel Lezcano 	.rating			= 100,
313d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
323d42b32bSDaniel Lezcano };
333d42b32bSDaniel Lezcano 
343d42b32bSDaniel Lezcano /*
353d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
363d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
373d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
383d42b32bSDaniel Lezcano  */
393d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
403d42b32bSDaniel Lezcano {
413d42b32bSDaniel Lezcano 	return get_cycles64();
423d42b32bSDaniel Lezcano }
433d42b32bSDaniel Lezcano 
443d42b32bSDaniel Lezcano static u64 riscv_sched_clock(void)
453d42b32bSDaniel Lezcano {
463d42b32bSDaniel Lezcano 	return get_cycles64();
473d42b32bSDaniel Lezcano }
483d42b32bSDaniel Lezcano 
49713203e3SAtish Patra static struct clocksource riscv_clocksource = {
503d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
513d42b32bSDaniel Lezcano 	.rating		= 300,
5232d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
533d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
543d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
553d42b32bSDaniel Lezcano };
563d42b32bSDaniel Lezcano 
573d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
583d42b32bSDaniel Lezcano {
593d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
603d42b32bSDaniel Lezcano 
613d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
623d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
633d42b32bSDaniel Lezcano 
643d42b32bSDaniel Lezcano 	csr_set(sie, SIE_STIE);
653d42b32bSDaniel Lezcano 	return 0;
663d42b32bSDaniel Lezcano }
673d42b32bSDaniel Lezcano 
683d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
693d42b32bSDaniel Lezcano {
703d42b32bSDaniel Lezcano 	csr_clear(sie, SIE_STIE);
713d42b32bSDaniel Lezcano 	return 0;
723d42b32bSDaniel Lezcano }
733d42b32bSDaniel Lezcano 
743d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
753d42b32bSDaniel Lezcano void riscv_timer_interrupt(void)
763d42b32bSDaniel Lezcano {
773d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
783d42b32bSDaniel Lezcano 
793d42b32bSDaniel Lezcano 	csr_clear(sie, SIE_STIE);
803d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
813d42b32bSDaniel Lezcano }
823d42b32bSDaniel Lezcano 
833d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
843d42b32bSDaniel Lezcano {
853d42b32bSDaniel Lezcano 	int cpuid, hartid, error;
863d42b32bSDaniel Lezcano 
873d42b32bSDaniel Lezcano 	hartid = riscv_of_processor_hartid(n);
8826478b2fSAtish Patra 	if (hartid < 0) {
8926478b2fSAtish Patra 		pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
9026478b2fSAtish Patra 			n, hartid);
9126478b2fSAtish Patra 		return hartid;
9226478b2fSAtish Patra 	}
9326478b2fSAtish Patra 
943d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
9526478b2fSAtish Patra 	if (cpuid < 0) {
9626478b2fSAtish Patra 		pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
9726478b2fSAtish Patra 		return cpuid;
9826478b2fSAtish Patra 	}
993d42b32bSDaniel Lezcano 
1003d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1013d42b32bSDaniel Lezcano 		return 0;
1023d42b32bSDaniel Lezcano 
10326478b2fSAtish Patra 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
10426478b2fSAtish Patra 	       __func__, cpuid, hartid);
105713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
10626478b2fSAtish Patra 	if (error) {
10726478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
10826478b2fSAtish Patra 		       error, cpuid);
10926478b2fSAtish Patra 		return error;
11026478b2fSAtish Patra 	}
1113d42b32bSDaniel Lezcano 
11232d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1133d42b32bSDaniel Lezcano 
1143d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1153d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1163d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1173d42b32bSDaniel Lezcano 	if (error)
11826478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
11926478b2fSAtish Patra 		       error);
1203d42b32bSDaniel Lezcano 	return error;
1213d42b32bSDaniel Lezcano }
1223d42b32bSDaniel Lezcano 
1233d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
124