blob: c51c5ed15aa75bb852ee4e3fbcc0554a43324628 [file] [log] [blame]
Palmer Dabbelt62b01942018-08-04 10:23:19 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2012 Regents of the University of California
4 * Copyright (C) 2017 SiFive
Christoph Hellwig2f12dbf12019-08-21 23:58:36 +09005 *
Christoph Hellwig4f9bbce2019-10-28 13:10:37 +01006 * All RISC-V systems have a timer attached to every hart. These timers can
7 * either be read from the "time" and "timeh" CSRs, and can use the SBI to
8 * setup events, or directly accessed using MMIO registers.
Palmer Dabbelt62b01942018-08-04 10:23:19 +02009 */
10#include <linux/clocksource.h>
11#include <linux/clockchips.h>
12#include <linux/cpu.h>
13#include <linux/delay.h>
14#include <linux/irq.h>
Anup Patel033a65d2020-06-01 14:45:41 +053015#include <linux/irqdomain.h>
Anup Patel92e0d142018-12-04 15:59:52 +053016#include <linux/sched_clock.h>
Christoph Hellwig4f9bbce2019-10-28 13:10:37 +010017#include <linux/io-64-nonatomic-lo-hi.h>
Anup Patel033a65d2020-06-01 14:45:41 +053018#include <linux/interrupt.h>
19#include <linux/of_irq.h>
Atish Patraf99fb602018-10-02 12:15:05 -070020#include <asm/smp.h>
Palmer Dabbelt62b01942018-08-04 10:23:19 +020021#include <asm/sbi.h>
Anup Patel2bc3fc82020-08-17 18:12:50 +053022#include <asm/timex.h>
Christoph Hellwig4f9bbce2019-10-28 13:10:37 +010023
Palmer Dabbelt62b01942018-08-04 10:23:19 +020024static int riscv_clock_next_event(unsigned long delta,
25 struct clock_event_device *ce)
26{
Christoph Hellwiga4c37332019-10-28 13:10:32 +010027 csr_set(CSR_IE, IE_TIE);
Anup Patel2bc3fc82020-08-17 18:12:50 +053028 sbi_set_timer(get_cycles64() + delta);
Palmer Dabbelt62b01942018-08-04 10:23:19 +020029 return 0;
30}
31
Anup Patel033a65d2020-06-01 14:45:41 +053032static unsigned int riscv_clock_event_irq;
Palmer Dabbelt62b01942018-08-04 10:23:19 +020033static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
34 .name = "riscv_timer_clockevent",
35 .features = CLOCK_EVT_FEAT_ONESHOT,
36 .rating = 100,
37 .set_next_event = riscv_clock_next_event,
38};
39
40/*
41 * It is guaranteed that all the timers across all the harts are synchronized
42 * within one tick of each other, so while this could technically go
43 * backwards when hopping between CPUs, practically it won't happen.
44 */
45static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
46{
47 return get_cycles64();
48}
49
Zong Li9d05c182019-12-23 16:46:14 +080050static u64 notrace riscv_sched_clock(void)
Anup Patel92e0d142018-12-04 15:59:52 +053051{
52 return get_cycles64();
53}
54
Atish Patra713203e2019-08-02 21:27:20 -070055static struct clocksource riscv_clocksource = {
Palmer Dabbelt62b01942018-08-04 10:23:19 +020056 .name = "riscv_clocksource",
57 .rating = 300,
Atish Patra32d0be02019-03-22 14:54:11 -070058 .mask = CLOCKSOURCE_MASK(64),
Palmer Dabbelt62b01942018-08-04 10:23:19 +020059 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
60 .read = riscv_clocksource_rdtime,
61};
62
63static int riscv_timer_starting_cpu(unsigned int cpu)
64{
65 struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
66
67 ce->cpumask = cpumask_of(cpu);
Anup Patel033a65d2020-06-01 14:45:41 +053068 ce->irq = riscv_clock_event_irq;
Palmer Dabbelt62b01942018-08-04 10:23:19 +020069 clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
70
Anup Patel033a65d2020-06-01 14:45:41 +053071 enable_percpu_irq(riscv_clock_event_irq,
72 irq_get_trigger_type(riscv_clock_event_irq));
Palmer Dabbelt62b01942018-08-04 10:23:19 +020073 return 0;
74}
75
76static int riscv_timer_dying_cpu(unsigned int cpu)
77{
Anup Patel033a65d2020-06-01 14:45:41 +053078 disable_percpu_irq(riscv_clock_event_irq);
Palmer Dabbelt62b01942018-08-04 10:23:19 +020079 return 0;
80}
81
82/* called directly from the low-level interrupt handler */
Anup Patel033a65d2020-06-01 14:45:41 +053083static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
Palmer Dabbelt62b01942018-08-04 10:23:19 +020084{
85 struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
86
Christoph Hellwiga4c37332019-10-28 13:10:32 +010087 csr_clear(CSR_IE, IE_TIE);
Palmer Dabbelt62b01942018-08-04 10:23:19 +020088 evdev->event_handler(evdev);
Anup Patel033a65d2020-06-01 14:45:41 +053089
90 return IRQ_HANDLED;
Palmer Dabbelt62b01942018-08-04 10:23:19 +020091}
92
93static int __init riscv_timer_init_dt(struct device_node *n)
94{
Atish Patraf99fb602018-10-02 12:15:05 -070095 int cpuid, hartid, error;
Anup Patel033a65d2020-06-01 14:45:41 +053096 struct device_node *child;
97 struct irq_domain *domain;
Palmer Dabbelt62b01942018-08-04 10:23:19 +020098
Atish Patraf99fb602018-10-02 12:15:05 -070099 hartid = riscv_of_processor_hartid(n);
Atish Patra26478b22019-02-13 12:18:10 -0800100 if (hartid < 0) {
101 pr_warn("Not valid hartid for node [%pOF] error = [%d]\n",
102 n, hartid);
103 return hartid;
104 }
105
Atish Patraf99fb602018-10-02 12:15:05 -0700106 cpuid = riscv_hartid_to_cpuid(hartid);
Atish Patra26478b22019-02-13 12:18:10 -0800107 if (cpuid < 0) {
108 pr_warn("Invalid cpuid for hartid [%d]\n", hartid);
109 return cpuid;
110 }
Atish Patraf99fb602018-10-02 12:15:05 -0700111
112 if (cpuid != smp_processor_id())
Palmer Dabbelt62b01942018-08-04 10:23:19 +0200113 return 0;
114
Anup Patel033a65d2020-06-01 14:45:41 +0530115 domain = NULL;
116 child = of_get_compatible_child(n, "riscv,cpu-intc");
117 if (!child) {
118 pr_err("Failed to find INTC node [%pOF]\n", n);
119 return -ENODEV;
120 }
121 domain = irq_find_host(child);
122 of_node_put(child);
123 if (!domain) {
124 pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
125 return -ENODEV;
126 }
127
128 riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
129 if (!riscv_clock_event_irq) {
130 pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
131 return -ENODEV;
132 }
133
Atish Patra26478b22019-02-13 12:18:10 -0800134 pr_info("%s: Registering clocksource cpuid [%d] hartid [%d]\n",
135 __func__, cpuid, hartid);
Atish Patra713203e2019-08-02 21:27:20 -0700136 error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
Atish Patra26478b22019-02-13 12:18:10 -0800137 if (error) {
138 pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
139 error, cpuid);
140 return error;
141 }
Palmer Dabbelt62b01942018-08-04 10:23:19 +0200142
Atish Patra32d0be02019-03-22 14:54:11 -0700143 sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
Anup Patel92e0d142018-12-04 15:59:52 +0530144
Anup Patel033a65d2020-06-01 14:45:41 +0530145 error = request_percpu_irq(riscv_clock_event_irq,
146 riscv_timer_interrupt,
147 "riscv-timer", &riscv_clock_event);
148 if (error) {
149 pr_err("registering percpu irq failed [%d]\n", error);
150 return error;
151 }
152
Palmer Dabbelt62b01942018-08-04 10:23:19 +0200153 error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
154 "clockevents/riscv/timer:starting",
155 riscv_timer_starting_cpu, riscv_timer_dying_cpu);
156 if (error)
Atish Patra26478b22019-02-13 12:18:10 -0800157 pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
158 error);
Palmer Dabbelt62b01942018-08-04 10:23:19 +0200159 return error;
160}
161
162TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);