1dcfa47d4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2dcfa47d4SThomas Gleixner // Copyright (C) 2012 MIPS Technologies, Inc.  All rights reserved.
33ca5768dSMatt Redfearn 
43ca5768dSMatt Redfearn #define pr_fmt(fmt) "mips-gic-timer: " fmt
53ca5768dSMatt Redfearn 
65b4e8453SAndrew Bresticker #include <linux/clk.h>
7a331ce63SAndrew Bresticker #include <linux/clockchips.h>
8e4752dbbSAndrew Bresticker #include <linux/cpu.h>
9fa5635a2SAndrew Bresticker #include <linux/init.h>
10a331ce63SAndrew Bresticker #include <linux/interrupt.h>
11e4752dbbSAndrew Bresticker #include <linux/notifier.h>
12e12aa828SAndrew Bresticker #include <linux/of_irq.h>
13a331ce63SAndrew Bresticker #include <linux/percpu.h>
1448016e78SPaul Burton #include <linux/sched_clock.h>
15a331ce63SAndrew Bresticker #include <linux/smp.h>
16fa5635a2SAndrew Bresticker #include <linux/time.h>
17e07127a0SPaul Burton #include <asm/mips-cps.h>
18fa5635a2SAndrew Bresticker 
195fee56e0SAndrew Bresticker static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
20e4752dbbSAndrew Bresticker static int gic_timer_irq;
21b0854514SAndrew Bresticker static unsigned int gic_frequency;
227d7de1a6SSerge Semin static bool __read_mostly gic_clock_unstable;
237d7de1a6SSerge Semin 
247d7de1a6SSerge Semin static void gic_clocksource_unstable(char *reason);
25a331ce63SAndrew Bresticker 
2648016e78SPaul Burton static u64 notrace gic_read_count_2x32(void)
27e07127a0SPaul Burton {
28e07127a0SPaul Burton 	unsigned int hi, hi2, lo;
29e07127a0SPaul Burton 
30e07127a0SPaul Burton 	do {
31e07127a0SPaul Burton 		hi = read_gic_counter_32h();
32e07127a0SPaul Burton 		lo = read_gic_counter_32l();
33e07127a0SPaul Burton 		hi2 = read_gic_counter_32h();
34e07127a0SPaul Burton 	} while (hi2 != hi);
35e07127a0SPaul Burton 
36e07127a0SPaul Burton 	return (((u64) hi) << 32) + lo;
37e07127a0SPaul Burton }
38e07127a0SPaul Burton 
3948016e78SPaul Burton static u64 notrace gic_read_count_64(void)
4048016e78SPaul Burton {
4148016e78SPaul Burton 	return read_gic_counter();
4248016e78SPaul Burton }
4348016e78SPaul Burton 
4448016e78SPaul Burton static u64 notrace gic_read_count(void)
4548016e78SPaul Burton {
4648016e78SPaul Burton 	if (mips_cm_is64)
4748016e78SPaul Burton 		return gic_read_count_64();
4848016e78SPaul Burton 
4948016e78SPaul Burton 	return gic_read_count_2x32();
5048016e78SPaul Burton }
5148016e78SPaul Burton 
52a331ce63SAndrew Bresticker static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
53a331ce63SAndrew Bresticker {
54f16ff2bdSMatt Redfearn 	int cpu = cpumask_first(evt->cpumask);
55a331ce63SAndrew Bresticker 	u64 cnt;
56a331ce63SAndrew Bresticker 	int res;
57a331ce63SAndrew Bresticker 
58a331ce63SAndrew Bresticker 	cnt = gic_read_count();
59a331ce63SAndrew Bresticker 	cnt += (u64)delta;
60f16ff2bdSMatt Redfearn 	if (cpu == raw_smp_processor_id()) {
61f16ff2bdSMatt Redfearn 		write_gic_vl_compare(cnt);
62f16ff2bdSMatt Redfearn 	} else {
63f16ff2bdSMatt Redfearn 		write_gic_vl_other(mips_cm_vp_id(cpu));
64e07127a0SPaul Burton 		write_gic_vo_compare(cnt);
65f16ff2bdSMatt Redfearn 	}
66a331ce63SAndrew Bresticker 	res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
67a331ce63SAndrew Bresticker 	return res;
68a331ce63SAndrew Bresticker }
69a331ce63SAndrew Bresticker 
705fee56e0SAndrew Bresticker static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
71a331ce63SAndrew Bresticker {
72f7ea3060SAndrew Bresticker 	struct clock_event_device *cd = dev_id;
73a331ce63SAndrew Bresticker 
74e07127a0SPaul Burton 	write_gic_vl_compare(read_gic_vl_compare());
75a331ce63SAndrew Bresticker 	cd->event_handler(cd);
76a331ce63SAndrew Bresticker 	return IRQ_HANDLED;
77a331ce63SAndrew Bresticker }
78a331ce63SAndrew Bresticker 
799039de40SYueHaibing static struct irqaction gic_compare_irqaction = {
80a331ce63SAndrew Bresticker 	.handler = gic_compare_interrupt,
81f7ea3060SAndrew Bresticker 	.percpu_dev_id = &gic_clockevent_device,
82a331ce63SAndrew Bresticker 	.flags = IRQF_PERCPU | IRQF_TIMER,
83a331ce63SAndrew Bresticker 	.name = "timer",
84a331ce63SAndrew Bresticker };
85a331ce63SAndrew Bresticker 
862dab9093SRichard Cochran static void gic_clockevent_cpu_init(unsigned int cpu,
872dab9093SRichard Cochran 				    struct clock_event_device *cd)
88a331ce63SAndrew Bresticker {
89a331ce63SAndrew Bresticker 	cd->name		= "MIPS GIC";
90a331ce63SAndrew Bresticker 	cd->features		= CLOCK_EVT_FEAT_ONESHOT |
91a331ce63SAndrew Bresticker 				  CLOCK_EVT_FEAT_C3STOP;
92a331ce63SAndrew Bresticker 
93a45da565SAndrew Bresticker 	cd->rating		= 350;
94e4752dbbSAndrew Bresticker 	cd->irq			= gic_timer_irq;
95a331ce63SAndrew Bresticker 	cd->cpumask		= cpumask_of(cpu);
96a331ce63SAndrew Bresticker 	cd->set_next_event	= gic_next_event;
97a331ce63SAndrew Bresticker 
98b695d8e6SAndrew Bresticker 	clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
99a331ce63SAndrew Bresticker 
100e4752dbbSAndrew Bresticker 	enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
101a331ce63SAndrew Bresticker }
102a331ce63SAndrew Bresticker 
103e4752dbbSAndrew Bresticker static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
104e4752dbbSAndrew Bresticker {
105e4752dbbSAndrew Bresticker 	disable_percpu_irq(gic_timer_irq);
106e4752dbbSAndrew Bresticker }
107e4752dbbSAndrew Bresticker 
108fc6a6772SEzequiel Garcia static void gic_update_frequency(void *data)
109fc6a6772SEzequiel Garcia {
110fc6a6772SEzequiel Garcia 	unsigned long rate = (unsigned long)data;
111fc6a6772SEzequiel Garcia 
112fc6a6772SEzequiel Garcia 	clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
113fc6a6772SEzequiel Garcia }
114fc6a6772SEzequiel Garcia 
1152dab9093SRichard Cochran static int gic_starting_cpu(unsigned int cpu)
116e4752dbbSAndrew Bresticker {
1172dab9093SRichard Cochran 	gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
1182dab9093SRichard Cochran 	return 0;
119e4752dbbSAndrew Bresticker }
120e4752dbbSAndrew Bresticker 
121fc6a6772SEzequiel Garcia static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
122fc6a6772SEzequiel Garcia 			    void *data)
123fc6a6772SEzequiel Garcia {
124fc6a6772SEzequiel Garcia 	struct clk_notifier_data *cnd = data;
125fc6a6772SEzequiel Garcia 
1267d7de1a6SSerge Semin 	if (action == POST_RATE_CHANGE) {
1277d7de1a6SSerge Semin 		gic_clocksource_unstable("ref clock rate change");
128fc6a6772SEzequiel Garcia 		on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
1297d7de1a6SSerge Semin 	}
130fc6a6772SEzequiel Garcia 
131fc6a6772SEzequiel Garcia 	return NOTIFY_OK;
132fc6a6772SEzequiel Garcia }
133fc6a6772SEzequiel Garcia 
1342dab9093SRichard Cochran static int gic_dying_cpu(unsigned int cpu)
1352dab9093SRichard Cochran {
1362dab9093SRichard Cochran 	gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
1372dab9093SRichard Cochran 	return 0;
1382dab9093SRichard Cochran }
139e4752dbbSAndrew Bresticker 
140fc6a6772SEzequiel Garcia static struct notifier_block gic_clk_nb = {
141fc6a6772SEzequiel Garcia 	.notifier_call = gic_clk_notifier,
142fc6a6772SEzequiel Garcia };
143fc6a6772SEzequiel Garcia 
144e4752dbbSAndrew Bresticker static int gic_clockevent_init(void)
145e4752dbbSAndrew Bresticker {
146f95ac855SEzequiel Garcia 	int ret;
147f95ac855SEzequiel Garcia 
1486982530eSPaul Burton 	if (!gic_frequency)
149e4752dbbSAndrew Bresticker 		return -ENXIO;
150e4752dbbSAndrew Bresticker 
151f95ac855SEzequiel Garcia 	ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
1522fd0c93cSPaul Burton 	if (ret < 0) {
1533ca5768dSMatt Redfearn 		pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
154f95ac855SEzequiel Garcia 		return ret;
1552fd0c93cSPaul Burton 	}
156e4752dbbSAndrew Bresticker 
1572dab9093SRichard Cochran 	cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
15873c1b41eSThomas Gleixner 			  "clockevents/mips/gic/timer:starting",
15973c1b41eSThomas Gleixner 			  gic_starting_cpu, gic_dying_cpu);
160a331ce63SAndrew Bresticker 	return 0;
161a331ce63SAndrew Bresticker }
162a331ce63SAndrew Bresticker 
163a5a1d1c2SThomas Gleixner static u64 gic_hpt_read(struct clocksource *cs)
164fa5635a2SAndrew Bresticker {
165fa5635a2SAndrew Bresticker 	return gic_read_count();
166fa5635a2SAndrew Bresticker }
167fa5635a2SAndrew Bresticker 
168fa5635a2SAndrew Bresticker static struct clocksource gic_clocksource = {
169fa5635a2SAndrew Bresticker 	.name			= "GIC",
170fa5635a2SAndrew Bresticker 	.read			= gic_hpt_read,
171fa5635a2SAndrew Bresticker 	.flags			= CLOCK_SOURCE_IS_CONTINUOUS,
172e1bdb22eSThomas Gleixner 	.vdso_clock_mode	= VDSO_CLOCKMODE_GIC,
173fa5635a2SAndrew Bresticker };
174fa5635a2SAndrew Bresticker 
1757d7de1a6SSerge Semin static void gic_clocksource_unstable(char *reason)
1767d7de1a6SSerge Semin {
1777d7de1a6SSerge Semin 	if (gic_clock_unstable)
1787d7de1a6SSerge Semin 		return;
1797d7de1a6SSerge Semin 
1807d7de1a6SSerge Semin 	gic_clock_unstable = true;
1817d7de1a6SSerge Semin 
1827d7de1a6SSerge Semin 	pr_info("GIC timer is unstable due to %s\n", reason);
1837d7de1a6SSerge Semin 
1847d7de1a6SSerge Semin 	clocksource_mark_unstable(&gic_clocksource);
1857d7de1a6SSerge Semin }
1867d7de1a6SSerge Semin 
187d8152bf8SDaniel Lezcano static int __init __gic_clocksource_init(void)
188fa5635a2SAndrew Bresticker {
189e07127a0SPaul Burton 	unsigned int count_width;
190f95ac855SEzequiel Garcia 	int ret;
191f95ac855SEzequiel Garcia 
192fa5635a2SAndrew Bresticker 	/* Set clocksource mask. */
193e07127a0SPaul Burton 	count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
1945753405eSFelix Fietkau 	count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
195e07127a0SPaul Burton 	count_width *= 4;
196e07127a0SPaul Burton 	count_width += 32;
197e07127a0SPaul Burton 	gic_clocksource.mask = CLOCKSOURCE_MASK(count_width);
198fa5635a2SAndrew Bresticker 
199fa5635a2SAndrew Bresticker 	/* Calculate a somewhat reasonable rating value. */
200*cc9b2c59SJiaxun Yang 	if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ))
201*cc9b2c59SJiaxun Yang 		gic_clocksource.rating = 300; /* Good when frequecy is stable */
202*cc9b2c59SJiaxun Yang 	else
203*cc9b2c59SJiaxun Yang 		gic_clocksource.rating = 200;
204*cc9b2c59SJiaxun Yang 	gic_clocksource.rating += clamp(gic_frequency / 10000000, 0, 99);
205fa5635a2SAndrew Bresticker 
206f95ac855SEzequiel Garcia 	ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
207f95ac855SEzequiel Garcia 	if (ret < 0)
2083ca5768dSMatt Redfearn 		pr_warn("Unable to register clocksource\n");
209d8152bf8SDaniel Lezcano 
210d8152bf8SDaniel Lezcano 	return ret;
211fa5635a2SAndrew Bresticker }
212e12aa828SAndrew Bresticker 
213be5769e2SPaul Gortmaker static int __init gic_clocksource_of_init(struct device_node *node)
214e12aa828SAndrew Bresticker {
2155b4e8453SAndrew Bresticker 	struct clk *clk;
216fc6a6772SEzequiel Garcia 	int ret;
2175b4e8453SAndrew Bresticker 
218e07127a0SPaul Burton 	if (!mips_gic_present() || !node->parent ||
219d8152bf8SDaniel Lezcano 	    !of_device_is_compatible(node->parent, "mti,gic")) {
2203ca5768dSMatt Redfearn 		pr_warn("No DT definition\n");
221d8152bf8SDaniel Lezcano 		return -ENXIO;
222d8152bf8SDaniel Lezcano 	}
223e12aa828SAndrew Bresticker 
2245b4e8453SAndrew Bresticker 	clk = of_clk_get(node, 0);
2255b4e8453SAndrew Bresticker 	if (!IS_ERR(clk)) {
2268c3ecd60SChristophe Jaillet 		ret = clk_prepare_enable(clk);
2278c3ecd60SChristophe Jaillet 		if (ret < 0) {
2283ca5768dSMatt Redfearn 			pr_err("Failed to enable clock\n");
2295b4e8453SAndrew Bresticker 			clk_put(clk);
2308c3ecd60SChristophe Jaillet 			return ret;
231eb811c73SEzequiel Garcia 		}
232eb811c73SEzequiel Garcia 
233eb811c73SEzequiel Garcia 		gic_frequency = clk_get_rate(clk);
2345b4e8453SAndrew Bresticker 	} else if (of_property_read_u32(node, "clock-frequency",
2355b4e8453SAndrew Bresticker 					&gic_frequency)) {
2363ca5768dSMatt Redfearn 		pr_err("Frequency not specified\n");
237ed7158baSIngo Molnar 		return -EINVAL;
238e12aa828SAndrew Bresticker 	}
239e12aa828SAndrew Bresticker 	gic_timer_irq = irq_of_parse_and_map(node, 0);
240e12aa828SAndrew Bresticker 	if (!gic_timer_irq) {
2413ca5768dSMatt Redfearn 		pr_err("IRQ not specified\n");
242ed7158baSIngo Molnar 		return -EINVAL;
243e12aa828SAndrew Bresticker 	}
244e12aa828SAndrew Bresticker 
245d8152bf8SDaniel Lezcano 	ret = __gic_clocksource_init();
246d8152bf8SDaniel Lezcano 	if (ret)
247d8152bf8SDaniel Lezcano 		return ret;
248fc6a6772SEzequiel Garcia 
249fc6a6772SEzequiel Garcia 	ret = gic_clockevent_init();
250fc6a6772SEzequiel Garcia 	if (!ret && !IS_ERR(clk)) {
251fc6a6772SEzequiel Garcia 		if (clk_notifier_register(clk, &gic_clk_nb) < 0)
2523ca5768dSMatt Redfearn 			pr_warn("Unable to register clock notifier\n");
253fc6a6772SEzequiel Garcia 	}
25467d4e669SEzequiel Garcia 
25567d4e669SEzequiel Garcia 	/* And finally start the counter */
256e07127a0SPaul Burton 	clear_gic_config(GIC_CONFIG_COUNTSTOP);
257d8152bf8SDaniel Lezcano 
25848016e78SPaul Burton 	/*
25948016e78SPaul Burton 	 * It's safe to use the MIPS GIC timer as a sched clock source only if
26048016e78SPaul Burton 	 * its ticks are stable, which is true on either the platforms with
26148016e78SPaul Burton 	 * stable CPU frequency or on the platforms with CM3 and CPU frequency
26248016e78SPaul Burton 	 * change performed by the CPC core clocks divider.
26348016e78SPaul Burton 	 */
26448016e78SPaul Burton 	if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ)) {
26548016e78SPaul Burton 		sched_clock_register(mips_cm_is64 ?
26648016e78SPaul Burton 				     gic_read_count_64 : gic_read_count_2x32,
26748016e78SPaul Burton 				     64, gic_frequency);
26848016e78SPaul Burton 	}
26948016e78SPaul Burton 
270d8152bf8SDaniel Lezcano 	return 0;
271e12aa828SAndrew Bresticker }
27217273395SDaniel Lezcano TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
273e12aa828SAndrew Bresticker 		       gic_clocksource_of_init);
274