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>
15033a65deSAnup Patel #include <linux/irqdomain.h>
16*3a9f66cbSAtish Patra #include <linux/module.h>
173d42b32bSDaniel Lezcano #include <linux/sched_clock.h>
184f9bbcefSChristoph Hellwig #include <linux/io-64-nonatomic-lo-hi.h>
19033a65deSAnup Patel #include <linux/interrupt.h>
20033a65deSAnup Patel #include <linux/of_irq.h>
21*3a9f66cbSAtish Patra #include <clocksource/timer-riscv.h>
223d42b32bSDaniel Lezcano #include <asm/smp.h>
233d42b32bSDaniel Lezcano #include <asm/sbi.h>
242bc3fc87SAnup Patel #include <asm/timex.h>
254f9bbcefSChristoph Hellwig 
263d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
273d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
283d42b32bSDaniel Lezcano {
29a4c3733dSChristoph Hellwig 	csr_set(CSR_IE, IE_TIE);
303d42b32bSDaniel Lezcano 	sbi_set_timer(get_cycles64() + delta);
313d42b32bSDaniel Lezcano 	return 0;
323d42b32bSDaniel Lezcano }
333d42b32bSDaniel Lezcano 
34033a65deSAnup Patel static unsigned int riscv_clock_event_irq;
353d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
363d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
373d42b32bSDaniel Lezcano 	.features		= CLOCK_EVT_FEAT_ONESHOT,
383d42b32bSDaniel Lezcano 	.rating			= 100,
393d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
403d42b32bSDaniel Lezcano };
413d42b32bSDaniel Lezcano 
423d42b32bSDaniel Lezcano /*
433d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
443d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
453d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
463d42b32bSDaniel Lezcano  */
473d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
483d42b32bSDaniel Lezcano {
493d42b32bSDaniel Lezcano 	return get_cycles64();
503d42b32bSDaniel Lezcano }
513d42b32bSDaniel Lezcano 
529d05c18eSZong Li static u64 notrace riscv_sched_clock(void)
533d42b32bSDaniel Lezcano {
543d42b32bSDaniel Lezcano 	return get_cycles64();
553d42b32bSDaniel Lezcano }
563d42b32bSDaniel Lezcano 
57713203e3SAtish Patra static struct clocksource riscv_clocksource = {
583d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
593d42b32bSDaniel Lezcano 	.rating		= 300,
6032d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
613d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
623d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
633d42b32bSDaniel Lezcano };
643d42b32bSDaniel Lezcano 
653d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
663d42b32bSDaniel Lezcano {
673d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
683d42b32bSDaniel Lezcano 
693d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
70033a65deSAnup Patel 	ce->irq = riscv_clock_event_irq;
713d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
723d42b32bSDaniel Lezcano 
73033a65deSAnup Patel 	enable_percpu_irq(riscv_clock_event_irq,
74033a65deSAnup Patel 			  irq_get_trigger_type(riscv_clock_event_irq));
753d42b32bSDaniel Lezcano 	return 0;
763d42b32bSDaniel Lezcano }
773d42b32bSDaniel Lezcano 
783d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
793d42b32bSDaniel Lezcano {
80033a65deSAnup Patel 	disable_percpu_irq(riscv_clock_event_irq);
813d42b32bSDaniel Lezcano 	return 0;
823d42b32bSDaniel Lezcano }
833d42b32bSDaniel Lezcano 
84*3a9f66cbSAtish Patra void riscv_cs_get_mult_shift(u32 *mult, u32 *shift)
85*3a9f66cbSAtish Patra {
86*3a9f66cbSAtish Patra 	*mult = riscv_clocksource.mult;
87*3a9f66cbSAtish Patra 	*shift = riscv_clocksource.shift;
88*3a9f66cbSAtish Patra }
89*3a9f66cbSAtish Patra EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift);
90*3a9f66cbSAtish Patra 
913d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
92033a65deSAnup Patel static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
933d42b32bSDaniel Lezcano {
943d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
953d42b32bSDaniel Lezcano 
96a4c3733dSChristoph Hellwig 	csr_clear(CSR_IE, IE_TIE);
973d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
98033a65deSAnup Patel 
99033a65deSAnup Patel 	return IRQ_HANDLED;
1003d42b32bSDaniel Lezcano }
1013d42b32bSDaniel Lezcano 
1023d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
1033d42b32bSDaniel Lezcano {
1043d42b32bSDaniel Lezcano 	int cpuid, hartid, error;
105033a65deSAnup Patel 	struct device_node *child;
106033a65deSAnup Patel 	struct irq_domain *domain;
1073d42b32bSDaniel Lezcano 
1083d42b32bSDaniel Lezcano 	hartid = riscv_of_processor_hartid(n);
10926478b2fSAtish Patra 	if (hartid < 0) {
11026478b2fSAtish Patra 		pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
11126478b2fSAtish Patra 			n, hartid);
11226478b2fSAtish Patra 		return hartid;
11326478b2fSAtish Patra 	}
11426478b2fSAtish Patra 
1153d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
11626478b2fSAtish Patra 	if (cpuid < 0) {
11726478b2fSAtish Patra 		pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
11826478b2fSAtish Patra 		return cpuid;
11926478b2fSAtish Patra 	}
1203d42b32bSDaniel Lezcano 
1213d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1223d42b32bSDaniel Lezcano 		return 0;
1233d42b32bSDaniel Lezcano 
124033a65deSAnup Patel 	domain = NULL;
125033a65deSAnup Patel 	child = of_get_compatible_child(n, "riscv,cpu-intc");
126033a65deSAnup Patel 	if (!child) {
127033a65deSAnup Patel 		pr_err("Failed to find INTC node [%pOF]\n", n);
128033a65deSAnup Patel 		return -ENODEV;
129033a65deSAnup Patel 	}
130033a65deSAnup Patel 	domain = irq_find_host(child);
131033a65deSAnup Patel 	of_node_put(child);
132033a65deSAnup Patel 	if (!domain) {
133033a65deSAnup Patel 		pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
134033a65deSAnup Patel 		return -ENODEV;
135033a65deSAnup Patel 	}
136033a65deSAnup Patel 
137033a65deSAnup Patel 	riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
138033a65deSAnup Patel 	if (!riscv_clock_event_irq) {
139033a65deSAnup Patel 		pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
140033a65deSAnup Patel 		return -ENODEV;
141033a65deSAnup Patel 	}
142033a65deSAnup Patel 
14326478b2fSAtish Patra 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
14426478b2fSAtish Patra 	       __func__, cpuid, hartid);
145713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
14626478b2fSAtish Patra 	if (error) {
14726478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
14826478b2fSAtish Patra 		       error, cpuid);
14926478b2fSAtish Patra 		return error;
15026478b2fSAtish Patra 	}
1513d42b32bSDaniel Lezcano 
15232d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1533d42b32bSDaniel Lezcano 
154033a65deSAnup Patel 	error = request_percpu_irq(riscv_clock_event_irq,
155033a65deSAnup Patel 				    riscv_timer_interrupt,
156033a65deSAnup Patel 				    "riscv-timer", &riscv_clock_event);
157033a65deSAnup Patel 	if (error) {
158033a65deSAnup Patel 		pr_err("registering percpu irq failed [%d]\n", error);
159033a65deSAnup Patel 		return error;
160033a65deSAnup Patel 	}
161033a65deSAnup Patel 
1623d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1633d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1643d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1653d42b32bSDaniel Lezcano 	if (error)
16626478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
16726478b2fSAtish Patra 		       error);
1683d42b32bSDaniel Lezcano 	return error;
1693d42b32bSDaniel Lezcano }
1703d42b32bSDaniel Lezcano 
1713d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
172