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  */
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>
164f9bbcefSChristoph Hellwig #include <linux/io-64-nonatomic-lo-hi.h>
173d42b32bSDaniel Lezcano #include <asm/smp.h>
183d42b32bSDaniel Lezcano #include <asm/sbi.h>
193d42b32bSDaniel Lezcano 
204f9bbcefSChristoph Hellwig u64 __iomem *riscv_time_cmp;
214f9bbcefSChristoph Hellwig u64 __iomem *riscv_time_val;
224f9bbcefSChristoph Hellwig 
234f9bbcefSChristoph Hellwig static inline void mmio_set_timer(u64 val)
244f9bbcefSChristoph Hellwig {
254f9bbcefSChristoph Hellwig 	void __iomem *r;
264f9bbcefSChristoph Hellwig 
274f9bbcefSChristoph Hellwig 	r = riscv_time_cmp + cpuid_to_hartid_map(smp_processor_id());
284f9bbcefSChristoph Hellwig 	writeq_relaxed(val, r);
294f9bbcefSChristoph Hellwig }
304f9bbcefSChristoph Hellwig 
313d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
323d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
333d42b32bSDaniel Lezcano {
34a4c3733dSChristoph Hellwig 	csr_set(CSR_IE, IE_TIE);
354f9bbcefSChristoph Hellwig 	if (IS_ENABLED(CONFIG_RISCV_SBI))
363d42b32bSDaniel Lezcano 		sbi_set_timer(get_cycles64() + delta);
374f9bbcefSChristoph Hellwig 	else
384f9bbcefSChristoph Hellwig 		mmio_set_timer(get_cycles64() + delta);
393d42b32bSDaniel Lezcano 	return 0;
403d42b32bSDaniel Lezcano }
413d42b32bSDaniel Lezcano 
423d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
433d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
443d42b32bSDaniel Lezcano 	.features		= CLOCK_EVT_FEAT_ONESHOT,
453d42b32bSDaniel Lezcano 	.rating			= 100,
463d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
473d42b32bSDaniel Lezcano };
483d42b32bSDaniel Lezcano 
493d42b32bSDaniel Lezcano /*
503d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
513d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
523d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
533d42b32bSDaniel Lezcano  */
543d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
553d42b32bSDaniel Lezcano {
563d42b32bSDaniel Lezcano 	return get_cycles64();
573d42b32bSDaniel Lezcano }
583d42b32bSDaniel Lezcano 
59*9d05c18eSZong Li static u64 notrace riscv_sched_clock(void)
603d42b32bSDaniel Lezcano {
613d42b32bSDaniel Lezcano 	return get_cycles64();
623d42b32bSDaniel Lezcano }
633d42b32bSDaniel Lezcano 
64713203e3SAtish Patra static struct clocksource riscv_clocksource = {
653d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
663d42b32bSDaniel Lezcano 	.rating		= 300,
6732d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
683d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
693d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
703d42b32bSDaniel Lezcano };
713d42b32bSDaniel Lezcano 
723d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
733d42b32bSDaniel Lezcano {
743d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
753d42b32bSDaniel Lezcano 
763d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
773d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
783d42b32bSDaniel Lezcano 
79a4c3733dSChristoph Hellwig 	csr_set(CSR_IE, IE_TIE);
803d42b32bSDaniel Lezcano 	return 0;
813d42b32bSDaniel Lezcano }
823d42b32bSDaniel Lezcano 
833d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
843d42b32bSDaniel Lezcano {
85a4c3733dSChristoph Hellwig 	csr_clear(CSR_IE, IE_TIE);
863d42b32bSDaniel Lezcano 	return 0;
873d42b32bSDaniel Lezcano }
883d42b32bSDaniel Lezcano 
893d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
903d42b32bSDaniel Lezcano void riscv_timer_interrupt(void)
913d42b32bSDaniel Lezcano {
923d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
933d42b32bSDaniel Lezcano 
94a4c3733dSChristoph Hellwig 	csr_clear(CSR_IE, IE_TIE);
953d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
963d42b32bSDaniel Lezcano }
973d42b32bSDaniel Lezcano 
983d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
993d42b32bSDaniel Lezcano {
1003d42b32bSDaniel Lezcano 	int cpuid, hartid, error;
1013d42b32bSDaniel Lezcano 
1023d42b32bSDaniel Lezcano 	hartid = riscv_of_processor_hartid(n);
10326478b2fSAtish Patra 	if (hartid < 0) {
10426478b2fSAtish Patra 		pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
10526478b2fSAtish Patra 			n, hartid);
10626478b2fSAtish Patra 		return hartid;
10726478b2fSAtish Patra 	}
10826478b2fSAtish Patra 
1093d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
11026478b2fSAtish Patra 	if (cpuid < 0) {
11126478b2fSAtish Patra 		pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
11226478b2fSAtish Patra 		return cpuid;
11326478b2fSAtish Patra 	}
1143d42b32bSDaniel Lezcano 
1153d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1163d42b32bSDaniel Lezcano 		return 0;
1173d42b32bSDaniel Lezcano 
11826478b2fSAtish Patra 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
11926478b2fSAtish Patra 	       __func__, cpuid, hartid);
120713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
12126478b2fSAtish Patra 	if (error) {
12226478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
12326478b2fSAtish Patra 		       error, cpuid);
12426478b2fSAtish Patra 		return error;
12526478b2fSAtish Patra 	}
1263d42b32bSDaniel Lezcano 
12732d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1283d42b32bSDaniel Lezcano 
1293d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1303d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1313d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1323d42b32bSDaniel Lezcano 	if (error)
13326478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
13426478b2fSAtish Patra 		       error);
1353d42b32bSDaniel Lezcano 	return error;
1363d42b32bSDaniel Lezcano }
1373d42b32bSDaniel Lezcano 
1383d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
139