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;
225e4bfd66SJiaxun Yang static unsigned int gic_count_width;
237d7de1a6SSerge Semin static bool __read_mostly gic_clock_unstable;
247d7de1a6SSerge Semin
257d7de1a6SSerge Semin static void gic_clocksource_unstable(char *reason);
26a331ce63SAndrew Bresticker
gic_read_count_2x32(void)2748016e78SPaul Burton static u64 notrace gic_read_count_2x32(void)
28e07127a0SPaul Burton {
29e07127a0SPaul Burton unsigned int hi, hi2, lo;
30e07127a0SPaul Burton
31e07127a0SPaul Burton do {
32e07127a0SPaul Burton hi = read_gic_counter_32h();
33e07127a0SPaul Burton lo = read_gic_counter_32l();
34e07127a0SPaul Burton hi2 = read_gic_counter_32h();
35e07127a0SPaul Burton } while (hi2 != hi);
36e07127a0SPaul Burton
37e07127a0SPaul Burton return (((u64) hi) << 32) + lo;
38e07127a0SPaul Burton }
39e07127a0SPaul Burton
gic_read_count_64(void)4048016e78SPaul Burton static u64 notrace gic_read_count_64(void)
4148016e78SPaul Burton {
4248016e78SPaul Burton return read_gic_counter();
4348016e78SPaul Burton }
4448016e78SPaul Burton
gic_read_count(void)4548016e78SPaul Burton static u64 notrace gic_read_count(void)
4648016e78SPaul Burton {
4748016e78SPaul Burton if (mips_cm_is64)
4848016e78SPaul Burton return gic_read_count_64();
4948016e78SPaul Burton
5048016e78SPaul Burton return gic_read_count_2x32();
5148016e78SPaul Burton }
5248016e78SPaul Burton
gic_next_event(unsigned long delta,struct clock_event_device * evt)53a331ce63SAndrew Bresticker static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
54a331ce63SAndrew Bresticker {
55f16ff2bdSMatt Redfearn int cpu = cpumask_first(evt->cpumask);
56a331ce63SAndrew Bresticker u64 cnt;
57a331ce63SAndrew Bresticker int res;
58a331ce63SAndrew Bresticker
59a331ce63SAndrew Bresticker cnt = gic_read_count();
60a331ce63SAndrew Bresticker cnt += (u64)delta;
61f16ff2bdSMatt Redfearn if (cpu == raw_smp_processor_id()) {
62f16ff2bdSMatt Redfearn write_gic_vl_compare(cnt);
63f16ff2bdSMatt Redfearn } else {
64f16ff2bdSMatt Redfearn write_gic_vl_other(mips_cm_vp_id(cpu));
65e07127a0SPaul Burton write_gic_vo_compare(cnt);
66f16ff2bdSMatt Redfearn }
67a331ce63SAndrew Bresticker res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
68a331ce63SAndrew Bresticker return res;
69a331ce63SAndrew Bresticker }
70a331ce63SAndrew Bresticker
gic_compare_interrupt(int irq,void * dev_id)715fee56e0SAndrew Bresticker static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
72a331ce63SAndrew Bresticker {
73f7ea3060SAndrew Bresticker struct clock_event_device *cd = dev_id;
74a331ce63SAndrew Bresticker
75e07127a0SPaul Burton write_gic_vl_compare(read_gic_vl_compare());
76a331ce63SAndrew Bresticker cd->event_handler(cd);
77a331ce63SAndrew Bresticker return IRQ_HANDLED;
78a331ce63SAndrew Bresticker }
79a331ce63SAndrew Bresticker
809039de40SYueHaibing static struct irqaction gic_compare_irqaction = {
81a331ce63SAndrew Bresticker .handler = gic_compare_interrupt,
82f7ea3060SAndrew Bresticker .percpu_dev_id = &gic_clockevent_device,
83a331ce63SAndrew Bresticker .flags = IRQF_PERCPU | IRQF_TIMER,
84a331ce63SAndrew Bresticker .name = "timer",
85a331ce63SAndrew Bresticker };
86a331ce63SAndrew Bresticker
gic_clockevent_cpu_init(unsigned int cpu,struct clock_event_device * cd)872dab9093SRichard Cochran static void gic_clockevent_cpu_init(unsigned int cpu,
882dab9093SRichard Cochran struct clock_event_device *cd)
89a331ce63SAndrew Bresticker {
90a331ce63SAndrew Bresticker cd->name = "MIPS GIC";
91a331ce63SAndrew Bresticker cd->features = CLOCK_EVT_FEAT_ONESHOT |
92a331ce63SAndrew Bresticker CLOCK_EVT_FEAT_C3STOP;
93a331ce63SAndrew Bresticker
94a45da565SAndrew Bresticker cd->rating = 350;
95e4752dbbSAndrew Bresticker cd->irq = gic_timer_irq;
96a331ce63SAndrew Bresticker cd->cpumask = cpumask_of(cpu);
97a331ce63SAndrew Bresticker cd->set_next_event = gic_next_event;
98a331ce63SAndrew Bresticker
99b695d8e6SAndrew Bresticker clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
100a331ce63SAndrew Bresticker
101e4752dbbSAndrew Bresticker enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
102a331ce63SAndrew Bresticker }
103a331ce63SAndrew Bresticker
gic_clockevent_cpu_exit(struct clock_event_device * cd)104e4752dbbSAndrew Bresticker static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
105e4752dbbSAndrew Bresticker {
106e4752dbbSAndrew Bresticker disable_percpu_irq(gic_timer_irq);
107e4752dbbSAndrew Bresticker }
108e4752dbbSAndrew Bresticker
gic_update_frequency(void * data)109fc6a6772SEzequiel Garcia static void gic_update_frequency(void *data)
110fc6a6772SEzequiel Garcia {
111fc6a6772SEzequiel Garcia unsigned long rate = (unsigned long)data;
112fc6a6772SEzequiel Garcia
113fc6a6772SEzequiel Garcia clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
114fc6a6772SEzequiel Garcia }
115fc6a6772SEzequiel Garcia
gic_starting_cpu(unsigned int cpu)1162dab9093SRichard Cochran static int gic_starting_cpu(unsigned int cpu)
117e4752dbbSAndrew Bresticker {
118*3128b0a2SPaul Burton /* Ensure the GIC counter is running */
119*3128b0a2SPaul Burton clear_gic_config(GIC_CONFIG_COUNTSTOP);
120*3128b0a2SPaul Burton
1212dab9093SRichard Cochran gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
1222dab9093SRichard Cochran return 0;
123e4752dbbSAndrew Bresticker }
124e4752dbbSAndrew Bresticker
gic_clk_notifier(struct notifier_block * nb,unsigned long action,void * data)125fc6a6772SEzequiel Garcia static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
126fc6a6772SEzequiel Garcia void *data)
127fc6a6772SEzequiel Garcia {
128fc6a6772SEzequiel Garcia struct clk_notifier_data *cnd = data;
129fc6a6772SEzequiel Garcia
1307d7de1a6SSerge Semin if (action == POST_RATE_CHANGE) {
1317d7de1a6SSerge Semin gic_clocksource_unstable("ref clock rate change");
132fc6a6772SEzequiel Garcia on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
1337d7de1a6SSerge Semin }
134fc6a6772SEzequiel Garcia
135fc6a6772SEzequiel Garcia return NOTIFY_OK;
136fc6a6772SEzequiel Garcia }
137fc6a6772SEzequiel Garcia
gic_dying_cpu(unsigned int cpu)1382dab9093SRichard Cochran static int gic_dying_cpu(unsigned int cpu)
1392dab9093SRichard Cochran {
1402dab9093SRichard Cochran gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
1412dab9093SRichard Cochran return 0;
1422dab9093SRichard Cochran }
143e4752dbbSAndrew Bresticker
144fc6a6772SEzequiel Garcia static struct notifier_block gic_clk_nb = {
145fc6a6772SEzequiel Garcia .notifier_call = gic_clk_notifier,
146fc6a6772SEzequiel Garcia };
147fc6a6772SEzequiel Garcia
gic_clockevent_init(void)148e4752dbbSAndrew Bresticker static int gic_clockevent_init(void)
149e4752dbbSAndrew Bresticker {
150f95ac855SEzequiel Garcia int ret;
151f95ac855SEzequiel Garcia
1526982530eSPaul Burton if (!gic_frequency)
153e4752dbbSAndrew Bresticker return -ENXIO;
154e4752dbbSAndrew Bresticker
155f95ac855SEzequiel Garcia ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
1562fd0c93cSPaul Burton if (ret < 0) {
1573ca5768dSMatt Redfearn pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
158f95ac855SEzequiel Garcia return ret;
1592fd0c93cSPaul Burton }
160e4752dbbSAndrew Bresticker
1612dab9093SRichard Cochran cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
16273c1b41eSThomas Gleixner "clockevents/mips/gic/timer:starting",
16373c1b41eSThomas Gleixner gic_starting_cpu, gic_dying_cpu);
164a331ce63SAndrew Bresticker return 0;
165a331ce63SAndrew Bresticker }
166a331ce63SAndrew Bresticker
gic_hpt_read(struct clocksource * cs)167a5a1d1c2SThomas Gleixner static u64 gic_hpt_read(struct clocksource *cs)
168fa5635a2SAndrew Bresticker {
169fa5635a2SAndrew Bresticker return gic_read_count();
170fa5635a2SAndrew Bresticker }
171fa5635a2SAndrew Bresticker
gic_hpt_read_multicluster(struct clocksource * cs)172dfe101bcSPaul Burton static u64 gic_hpt_read_multicluster(struct clocksource *cs)
173dfe101bcSPaul Burton {
174dfe101bcSPaul Burton unsigned int hi, hi2, lo;
175dfe101bcSPaul Burton u64 count;
176dfe101bcSPaul Burton
177dfe101bcSPaul Burton mips_cm_lock_other(0, 0, 0, CM_GCR_Cx_OTHER_BLOCK_GLOBAL);
178dfe101bcSPaul Burton
179dfe101bcSPaul Burton if (mips_cm_is64) {
180dfe101bcSPaul Burton count = read_gic_redir_counter();
181dfe101bcSPaul Burton goto out;
182dfe101bcSPaul Burton }
183dfe101bcSPaul Burton
184dfe101bcSPaul Burton hi = read_gic_redir_counter_32h();
185dfe101bcSPaul Burton while (true) {
186dfe101bcSPaul Burton lo = read_gic_redir_counter_32l();
187dfe101bcSPaul Burton
188dfe101bcSPaul Burton /* If hi didn't change then lo didn't wrap & we're done */
189dfe101bcSPaul Burton hi2 = read_gic_redir_counter_32h();
190dfe101bcSPaul Burton if (hi2 == hi)
191dfe101bcSPaul Burton break;
192dfe101bcSPaul Burton
193dfe101bcSPaul Burton /* Otherwise, repeat with the latest hi value */
194dfe101bcSPaul Burton hi = hi2;
195dfe101bcSPaul Burton }
196dfe101bcSPaul Burton
197dfe101bcSPaul Burton count = (((u64)hi) << 32) + lo;
198dfe101bcSPaul Burton out:
199dfe101bcSPaul Burton mips_cm_unlock_other();
200dfe101bcSPaul Burton return count;
201dfe101bcSPaul Burton }
202dfe101bcSPaul Burton
203fa5635a2SAndrew Bresticker static struct clocksource gic_clocksource = {
204fa5635a2SAndrew Bresticker .name = "GIC",
205fa5635a2SAndrew Bresticker .read = gic_hpt_read,
206fa5635a2SAndrew Bresticker .flags = CLOCK_SOURCE_IS_CONTINUOUS,
207e1bdb22eSThomas Gleixner .vdso_clock_mode = VDSO_CLOCKMODE_GIC,
208fa5635a2SAndrew Bresticker };
209fa5635a2SAndrew Bresticker
gic_clocksource_unstable(char * reason)2107d7de1a6SSerge Semin static void gic_clocksource_unstable(char *reason)
2117d7de1a6SSerge Semin {
2127d7de1a6SSerge Semin if (gic_clock_unstable)
2137d7de1a6SSerge Semin return;
2147d7de1a6SSerge Semin
2157d7de1a6SSerge Semin gic_clock_unstable = true;
2167d7de1a6SSerge Semin
2177d7de1a6SSerge Semin pr_info("GIC timer is unstable due to %s\n", reason);
2187d7de1a6SSerge Semin
2197d7de1a6SSerge Semin clocksource_mark_unstable(&gic_clocksource);
2207d7de1a6SSerge Semin }
2217d7de1a6SSerge Semin
__gic_clocksource_init(void)222d8152bf8SDaniel Lezcano static int __init __gic_clocksource_init(void)
223fa5635a2SAndrew Bresticker {
224f95ac855SEzequiel Garcia int ret;
225f95ac855SEzequiel Garcia
226fa5635a2SAndrew Bresticker /* Set clocksource mask. */
2275e4bfd66SJiaxun Yang gic_count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
2285e4bfd66SJiaxun Yang gic_count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
2295e4bfd66SJiaxun Yang gic_count_width *= 4;
2305e4bfd66SJiaxun Yang gic_count_width += 32;
2315e4bfd66SJiaxun Yang gic_clocksource.mask = CLOCKSOURCE_MASK(gic_count_width);
232fa5635a2SAndrew Bresticker
233fa5635a2SAndrew Bresticker /* Calculate a somewhat reasonable rating value. */
234cc9b2c59SJiaxun Yang if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ))
235cc9b2c59SJiaxun Yang gic_clocksource.rating = 300; /* Good when frequecy is stable */
236cc9b2c59SJiaxun Yang else
237cc9b2c59SJiaxun Yang gic_clocksource.rating = 200;
238cc9b2c59SJiaxun Yang gic_clocksource.rating += clamp(gic_frequency / 10000000, 0, 99);
239fa5635a2SAndrew Bresticker
240dfe101bcSPaul Burton if (mips_cps_multicluster_cpus()) {
241dfe101bcSPaul Burton gic_clocksource.read = &gic_hpt_read_multicluster;
242dfe101bcSPaul Burton gic_clocksource.vdso_clock_mode = VDSO_CLOCKMODE_NONE;
243dfe101bcSPaul Burton }
244dfe101bcSPaul Burton
245f95ac855SEzequiel Garcia ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
246f95ac855SEzequiel Garcia if (ret < 0)
2473ca5768dSMatt Redfearn pr_warn("Unable to register clocksource\n");
248d8152bf8SDaniel Lezcano
249d8152bf8SDaniel Lezcano return ret;
250fa5635a2SAndrew Bresticker }
251e12aa828SAndrew Bresticker
gic_clocksource_of_init(struct device_node * node)252be5769e2SPaul Gortmaker static int __init gic_clocksource_of_init(struct device_node *node)
253e12aa828SAndrew Bresticker {
2545b4e8453SAndrew Bresticker struct clk *clk;
255fc6a6772SEzequiel Garcia int ret;
2565b4e8453SAndrew Bresticker
257e07127a0SPaul Burton if (!mips_gic_present() || !node->parent ||
258d8152bf8SDaniel Lezcano !of_device_is_compatible(node->parent, "mti,gic")) {
2593ca5768dSMatt Redfearn pr_warn("No DT definition\n");
260d8152bf8SDaniel Lezcano return -ENXIO;
261d8152bf8SDaniel Lezcano }
262e12aa828SAndrew Bresticker
2635b4e8453SAndrew Bresticker clk = of_clk_get(node, 0);
2645b4e8453SAndrew Bresticker if (!IS_ERR(clk)) {
2658c3ecd60SChristophe Jaillet ret = clk_prepare_enable(clk);
2668c3ecd60SChristophe Jaillet if (ret < 0) {
2673ca5768dSMatt Redfearn pr_err("Failed to enable clock\n");
2685b4e8453SAndrew Bresticker clk_put(clk);
2698c3ecd60SChristophe Jaillet return ret;
270eb811c73SEzequiel Garcia }
271eb811c73SEzequiel Garcia
272eb811c73SEzequiel Garcia gic_frequency = clk_get_rate(clk);
2735b4e8453SAndrew Bresticker } else if (of_property_read_u32(node, "clock-frequency",
2745b4e8453SAndrew Bresticker &gic_frequency)) {
2753ca5768dSMatt Redfearn pr_err("Frequency not specified\n");
276ed7158baSIngo Molnar return -EINVAL;
277e12aa828SAndrew Bresticker }
278e12aa828SAndrew Bresticker gic_timer_irq = irq_of_parse_and_map(node, 0);
279e12aa828SAndrew Bresticker if (!gic_timer_irq) {
2803ca5768dSMatt Redfearn pr_err("IRQ not specified\n");
281ed7158baSIngo Molnar return -EINVAL;
282e12aa828SAndrew Bresticker }
283e12aa828SAndrew Bresticker
284d8152bf8SDaniel Lezcano ret = __gic_clocksource_init();
285d8152bf8SDaniel Lezcano if (ret)
286d8152bf8SDaniel Lezcano return ret;
287fc6a6772SEzequiel Garcia
288fc6a6772SEzequiel Garcia ret = gic_clockevent_init();
289fc6a6772SEzequiel Garcia if (!ret && !IS_ERR(clk)) {
290fc6a6772SEzequiel Garcia if (clk_notifier_register(clk, &gic_clk_nb) < 0)
2913ca5768dSMatt Redfearn pr_warn("Unable to register clock notifier\n");
292fc6a6772SEzequiel Garcia }
29367d4e669SEzequiel Garcia
29448016e78SPaul Burton /*
29548016e78SPaul Burton * It's safe to use the MIPS GIC timer as a sched clock source only if
29648016e78SPaul Burton * its ticks are stable, which is true on either the platforms with
29748016e78SPaul Burton * stable CPU frequency or on the platforms with CM3 and CPU frequency
29848016e78SPaul Burton * change performed by the CPC core clocks divider.
29948016e78SPaul Burton */
300dfe101bcSPaul Burton if ((mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ)) &&
301dfe101bcSPaul Burton !mips_cps_multicluster_cpus()) {
30248016e78SPaul Burton sched_clock_register(mips_cm_is64 ?
30348016e78SPaul Burton gic_read_count_64 : gic_read_count_2x32,
3045e4bfd66SJiaxun Yang gic_count_width, gic_frequency);
30548016e78SPaul Burton }
30648016e78SPaul Burton
307d8152bf8SDaniel Lezcano return 0;
308e12aa828SAndrew Bresticker }
30917273395SDaniel Lezcano TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
310e12aa828SAndrew Bresticker gic_clocksource_of_init);
311