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>
15*033a65deSAnup Patel #include <linux/irqdomain.h>
163d42b32bSDaniel Lezcano #include <linux/sched_clock.h>
174f9bbcefSChristoph Hellwig #include <linux/io-64-nonatomic-lo-hi.h>
18*033a65deSAnup Patel #include <linux/interrupt.h>
19*033a65deSAnup Patel #include <linux/of_irq.h>
203d42b32bSDaniel Lezcano #include <asm/smp.h>
213d42b32bSDaniel Lezcano #include <asm/sbi.h>
223d42b32bSDaniel Lezcano 
234f9bbcefSChristoph Hellwig u64 __iomem *riscv_time_cmp;
244f9bbcefSChristoph Hellwig u64 __iomem *riscv_time_val;
254f9bbcefSChristoph Hellwig 
264f9bbcefSChristoph Hellwig static inline void mmio_set_timer(u64 val)
274f9bbcefSChristoph Hellwig {
284f9bbcefSChristoph Hellwig 	void __iomem *r;
294f9bbcefSChristoph Hellwig 
304f9bbcefSChristoph Hellwig 	r = riscv_time_cmp + cpuid_to_hartid_map(smp_processor_id());
314f9bbcefSChristoph Hellwig 	writeq_relaxed(val, r);
324f9bbcefSChristoph Hellwig }
334f9bbcefSChristoph Hellwig 
343d42b32bSDaniel Lezcano static int riscv_clock_next_event(unsigned long delta,
353d42b32bSDaniel Lezcano 		struct clock_event_device *ce)
363d42b32bSDaniel Lezcano {
37a4c3733dSChristoph Hellwig 	csr_set(CSR_IE, IE_TIE);
384f9bbcefSChristoph Hellwig 	if (IS_ENABLED(CONFIG_RISCV_SBI))
393d42b32bSDaniel Lezcano 		sbi_set_timer(get_cycles64() + delta);
404f9bbcefSChristoph Hellwig 	else
414f9bbcefSChristoph Hellwig 		mmio_set_timer(get_cycles64() + delta);
423d42b32bSDaniel Lezcano 	return 0;
433d42b32bSDaniel Lezcano }
443d42b32bSDaniel Lezcano 
45*033a65deSAnup Patel static unsigned int riscv_clock_event_irq;
463d42b32bSDaniel Lezcano static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
473d42b32bSDaniel Lezcano 	.name			= "riscv_timer_clockevent",
483d42b32bSDaniel Lezcano 	.features		= CLOCK_EVT_FEAT_ONESHOT,
493d42b32bSDaniel Lezcano 	.rating			= 100,
503d42b32bSDaniel Lezcano 	.set_next_event		= riscv_clock_next_event,
513d42b32bSDaniel Lezcano };
523d42b32bSDaniel Lezcano 
533d42b32bSDaniel Lezcano /*
543d42b32bSDaniel Lezcano  * It is guaranteed that all the timers across all the harts are synchronized
553d42b32bSDaniel Lezcano  * within one tick of each other, so while this could technically go
563d42b32bSDaniel Lezcano  * backwards when hopping between CPUs, practically it won't happen.
573d42b32bSDaniel Lezcano  */
583d42b32bSDaniel Lezcano static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
593d42b32bSDaniel Lezcano {
603d42b32bSDaniel Lezcano 	return get_cycles64();
613d42b32bSDaniel Lezcano }
623d42b32bSDaniel Lezcano 
639d05c18eSZong Li static u64 notrace riscv_sched_clock(void)
643d42b32bSDaniel Lezcano {
653d42b32bSDaniel Lezcano 	return get_cycles64();
663d42b32bSDaniel Lezcano }
673d42b32bSDaniel Lezcano 
68713203e3SAtish Patra static struct clocksource riscv_clocksource = {
693d42b32bSDaniel Lezcano 	.name		= "riscv_clocksource",
703d42b32bSDaniel Lezcano 	.rating		= 300,
7132d0be01SAtish Patra 	.mask		= CLOCKSOURCE_MASK(64),
723d42b32bSDaniel Lezcano 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
733d42b32bSDaniel Lezcano 	.read		= riscv_clocksource_rdtime,
743d42b32bSDaniel Lezcano };
753d42b32bSDaniel Lezcano 
763d42b32bSDaniel Lezcano static int riscv_timer_starting_cpu(unsigned int cpu)
773d42b32bSDaniel Lezcano {
783d42b32bSDaniel Lezcano 	struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
793d42b32bSDaniel Lezcano 
803d42b32bSDaniel Lezcano 	ce->cpumask = cpumask_of(cpu);
81*033a65deSAnup Patel 	ce->irq = riscv_clock_event_irq;
823d42b32bSDaniel Lezcano 	clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
833d42b32bSDaniel Lezcano 
84*033a65deSAnup Patel 	enable_percpu_irq(riscv_clock_event_irq,
85*033a65deSAnup Patel 			  irq_get_trigger_type(riscv_clock_event_irq));
863d42b32bSDaniel Lezcano 	return 0;
873d42b32bSDaniel Lezcano }
883d42b32bSDaniel Lezcano 
893d42b32bSDaniel Lezcano static int riscv_timer_dying_cpu(unsigned int cpu)
903d42b32bSDaniel Lezcano {
91*033a65deSAnup Patel 	disable_percpu_irq(riscv_clock_event_irq);
923d42b32bSDaniel Lezcano 	return 0;
933d42b32bSDaniel Lezcano }
943d42b32bSDaniel Lezcano 
953d42b32bSDaniel Lezcano /* called directly from the low-level interrupt handler */
96*033a65deSAnup Patel static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
973d42b32bSDaniel Lezcano {
983d42b32bSDaniel Lezcano 	struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
993d42b32bSDaniel Lezcano 
100a4c3733dSChristoph Hellwig 	csr_clear(CSR_IE, IE_TIE);
1013d42b32bSDaniel Lezcano 	evdev->event_handler(evdev);
102*033a65deSAnup Patel 
103*033a65deSAnup Patel 	return IRQ_HANDLED;
1043d42b32bSDaniel Lezcano }
1053d42b32bSDaniel Lezcano 
1063d42b32bSDaniel Lezcano static int __init riscv_timer_init_dt(struct device_node *n)
1073d42b32bSDaniel Lezcano {
1083d42b32bSDaniel Lezcano 	int cpuid, hartid, error;
109*033a65deSAnup Patel 	struct device_node *child;
110*033a65deSAnup Patel 	struct irq_domain *domain;
1113d42b32bSDaniel Lezcano 
1123d42b32bSDaniel Lezcano 	hartid = riscv_of_processor_hartid(n);
11326478b2fSAtish Patra 	if (hartid < 0) {
11426478b2fSAtish Patra 		pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
11526478b2fSAtish Patra 			n, hartid);
11626478b2fSAtish Patra 		return hartid;
11726478b2fSAtish Patra 	}
11826478b2fSAtish Patra 
1193d42b32bSDaniel Lezcano 	cpuid = riscv_hartid_to_cpuid(hartid);
12026478b2fSAtish Patra 	if (cpuid < 0) {
12126478b2fSAtish Patra 		pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
12226478b2fSAtish Patra 		return cpuid;
12326478b2fSAtish Patra 	}
1243d42b32bSDaniel Lezcano 
1253d42b32bSDaniel Lezcano 	if (cpuid != smp_processor_id())
1263d42b32bSDaniel Lezcano 		return 0;
1273d42b32bSDaniel Lezcano 
128*033a65deSAnup Patel 	domain = NULL;
129*033a65deSAnup Patel 	child = of_get_compatible_child(n, "riscv,cpu-intc");
130*033a65deSAnup Patel 	if (!child) {
131*033a65deSAnup Patel 		pr_err("Failed to find INTC node [%pOF]\n", n);
132*033a65deSAnup Patel 		return -ENODEV;
133*033a65deSAnup Patel 	}
134*033a65deSAnup Patel 	domain = irq_find_host(child);
135*033a65deSAnup Patel 	of_node_put(child);
136*033a65deSAnup Patel 	if (!domain) {
137*033a65deSAnup Patel 		pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
138*033a65deSAnup Patel 		return -ENODEV;
139*033a65deSAnup Patel 	}
140*033a65deSAnup Patel 
141*033a65deSAnup Patel 	riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
142*033a65deSAnup Patel 	if (!riscv_clock_event_irq) {
143*033a65deSAnup Patel 		pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
144*033a65deSAnup Patel 		return -ENODEV;
145*033a65deSAnup Patel 	}
146*033a65deSAnup Patel 
14726478b2fSAtish Patra 	pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
14826478b2fSAtish Patra 	       __func__, cpuid, hartid);
149713203e3SAtish Patra 	error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
15026478b2fSAtish Patra 	if (error) {
15126478b2fSAtish Patra 		pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
15226478b2fSAtish Patra 		       error, cpuid);
15326478b2fSAtish Patra 		return error;
15426478b2fSAtish Patra 	}
1553d42b32bSDaniel Lezcano 
15632d0be01SAtish Patra 	sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
1573d42b32bSDaniel Lezcano 
158*033a65deSAnup Patel 	error = request_percpu_irq(riscv_clock_event_irq,
159*033a65deSAnup Patel 				    riscv_timer_interrupt,
160*033a65deSAnup Patel 				    "riscv-timer", &riscv_clock_event);
161*033a65deSAnup Patel 	if (error) {
162*033a65deSAnup Patel 		pr_err("registering percpu irq failed [%d]\n", error);
163*033a65deSAnup Patel 		return error;
164*033a65deSAnup Patel 	}
165*033a65deSAnup Patel 
1663d42b32bSDaniel Lezcano 	error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
1673d42b32bSDaniel Lezcano 			 "clockevents/riscv/timer:starting",
1683d42b32bSDaniel Lezcano 			 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
1693d42b32bSDaniel Lezcano 	if (error)
17026478b2fSAtish Patra 		pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
17126478b2fSAtish Patra 		       error);
1723d42b32bSDaniel Lezcano 	return error;
1733d42b32bSDaniel Lezcano }
1743d42b32bSDaniel Lezcano 
1753d42b32bSDaniel Lezcano TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);
176