blob: 65ecf17a36f491a2b16317b0ec7952ab906d0a1c [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Tomeu Vizoso6234f382014-11-24 13:28:17 +01002/*
3 * A devfreq driver for NVIDIA Tegra SoCs
4 *
5 * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
6 * Copyright (C) 2014 Google, Inc
Tomeu Vizoso6234f382014-11-24 13:28:17 +01007 */
8
9#include <linux/clk.h>
10#include <linux/cpufreq.h>
11#include <linux/devfreq.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +030014#include <linux/irq.h>
Tomeu Vizoso6234f382014-11-24 13:28:17 +010015#include <linux/module.h>
Dmitry Osipenko9cff2172019-11-05 00:56:10 +030016#include <linux/of_device.h>
Tomeu Vizoso6234f382014-11-24 13:28:17 +010017#include <linux/platform_device.h>
18#include <linux/pm_opp.h>
19#include <linux/reset.h>
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +030020#include <linux/workqueue.h>
Tomeu Vizoso6234f382014-11-24 13:28:17 +010021
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +030022#include <soc/tegra/fuse.h>
23
Tomeu Vizoso6234f382014-11-24 13:28:17 +010024#include "governor.h"
25
26#define ACTMON_GLB_STATUS 0x0
27#define ACTMON_GLB_PERIOD_CTRL 0x4
28
29#define ACTMON_DEV_CTRL 0x0
30#define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
31#define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
32#define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
33#define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
34#define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
35#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
36#define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
37#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
38#define ACTMON_DEV_CTRL_ENB BIT(31)
39
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +030040#define ACTMON_DEV_CTRL_STOP 0x00000000
41
Tomeu Vizoso6234f382014-11-24 13:28:17 +010042#define ACTMON_DEV_UPPER_WMARK 0x4
43#define ACTMON_DEV_LOWER_WMARK 0x8
44#define ACTMON_DEV_INIT_AVG 0xc
45#define ACTMON_DEV_AVG_UPPER_WMARK 0x10
46#define ACTMON_DEV_AVG_LOWER_WMARK 0x14
47#define ACTMON_DEV_COUNT_WEIGHT 0x18
48#define ACTMON_DEV_AVG_COUNT 0x20
49#define ACTMON_DEV_INTR_STATUS 0x24
50
51#define ACTMON_INTR_STATUS_CLEAR 0xffffffff
52
53#define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
54#define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
55
56#define ACTMON_ABOVE_WMARK_WINDOW 1
57#define ACTMON_BELOW_WMARK_WINDOW 3
58#define ACTMON_BOOST_FREQ_STEP 16000
59
Tomeu Vizoso11573e92015-03-17 10:36:12 +010060/*
Tomeu Vizoso6234f382014-11-24 13:28:17 +010061 * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
62 * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
63 */
64#define ACTMON_AVERAGE_WINDOW_LOG2 6
65#define ACTMON_SAMPLING_PERIOD 12 /* ms */
66#define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
67
68#define KHZ 1000
69
Dmitry Osipenko53b4b2a2019-11-05 00:56:03 +030070#define KHZ_MAX (ULONG_MAX / KHZ)
71
Tomeu Vizoso6234f382014-11-24 13:28:17 +010072/* Assume that the bus is saturated if the utilization is 25% */
73#define BUS_SATURATION_RATIO 25
74
75/**
76 * struct tegra_devfreq_device_config - configuration specific to an ACTMON
77 * device
78 *
Tomeu Vizoso11573e92015-03-17 10:36:12 +010079 * Coefficients and thresholds are percentages unless otherwise noted
Tomeu Vizoso6234f382014-11-24 13:28:17 +010080 */
81struct tegra_devfreq_device_config {
82 u32 offset;
83 u32 irq_mask;
84
Tomeu Vizoso11573e92015-03-17 10:36:12 +010085 /* Factors applied to boost_freq every consecutive watermark breach */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010086 unsigned int boost_up_coeff;
87 unsigned int boost_down_coeff;
Tomeu Vizoso11573e92015-03-17 10:36:12 +010088
89 /* Define the watermark bounds when applied to the current avg */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010090 unsigned int boost_up_threshold;
91 unsigned int boost_down_threshold;
Tomeu Vizoso11573e92015-03-17 10:36:12 +010092
93 /*
Dmitry Osipenko28615e32019-11-05 00:56:13 +030094 * Threshold of activity (cycles translated to kHz) below which the
95 * CPU frequency isn't to be taken into account. This is to avoid
96 * increasing the EMC frequency when the CPU is very busy but not
97 * accessing the bus often.
Tomeu Vizoso11573e92015-03-17 10:36:12 +010098 */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010099 u32 avg_dependency_threshold;
100};
101
102enum tegra_actmon_device {
103 MCALL = 0,
104 MCCPU,
105};
106
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300107static const struct tegra_devfreq_device_config tegra124_device_configs[] = {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100108 {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100109 /* MCALL: All memory accesses (including from the CPUs) */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100110 .offset = 0x1c0,
111 .irq_mask = 1 << 26,
112 .boost_up_coeff = 200,
113 .boost_down_coeff = 50,
114 .boost_up_threshold = 60,
115 .boost_down_threshold = 40,
116 },
117 {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100118 /* MCCPU: memory accesses from the CPUs */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100119 .offset = 0x200,
120 .irq_mask = 1 << 25,
121 .boost_up_coeff = 800,
Dmitry Osipenkofee22852019-11-05 00:56:16 +0300122 .boost_down_coeff = 40,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100123 .boost_up_threshold = 27,
124 .boost_down_threshold = 10,
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300125 .avg_dependency_threshold = 16000, /* 16MHz in kHz units */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100126 },
127};
128
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300129static const struct tegra_devfreq_device_config tegra30_device_configs[] = {
130 {
131 /* MCALL: All memory accesses (including from the CPUs) */
132 .offset = 0x1c0,
133 .irq_mask = 1 << 26,
134 .boost_up_coeff = 200,
135 .boost_down_coeff = 50,
136 .boost_up_threshold = 20,
137 .boost_down_threshold = 10,
138 },
139 {
140 /* MCCPU: memory accesses from the CPUs */
141 .offset = 0x200,
142 .irq_mask = 1 << 25,
143 .boost_up_coeff = 800,
144 .boost_down_coeff = 40,
145 .boost_up_threshold = 27,
146 .boost_down_threshold = 10,
147 .avg_dependency_threshold = 16000, /* 16MHz in kHz units */
148 },
149};
150
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100151/**
152 * struct tegra_devfreq_device - state specific to an ACTMON device
153 *
154 * Frequencies are in kHz.
155 */
156struct tegra_devfreq_device {
157 const struct tegra_devfreq_device_config *config;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100158 void __iomem *regs;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100159
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100160 /* Average event count sampled in the last interrupt */
161 u32 avg_count;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100162
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100163 /*
164 * Extra frequency to increase the target by due to consecutive
165 * watermark breaches.
166 */
167 unsigned long boost_freq;
168
169 /* Optimal frequency calculated from the stats for this device */
170 unsigned long target_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100171};
172
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300173struct tegra_devfreq_soc_data {
174 const struct tegra_devfreq_device_config *configs;
175 /* Weight value for count measurements */
176 unsigned int count_weight;
177};
178
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100179struct tegra_devfreq {
180 struct devfreq *devfreq;
181
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100182 struct reset_control *reset;
183 struct clk *clock;
184 void __iomem *regs;
185
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100186 struct clk *emc_clock;
187 unsigned long max_freq;
188 unsigned long cur_freq;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300189 struct notifier_block clk_rate_change_nb;
190
191 struct delayed_work cpufreq_update_work;
192 struct notifier_block cpu_rate_change_nb;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100193
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300194 struct tegra_devfreq_device devices[2];
Dmitry Osipenko7514dd02019-05-02 02:38:06 +0300195
Dmitry Osipenkodccdea02019-11-05 00:55:59 +0300196 unsigned int irq;
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300197
198 bool started;
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300199
200 const struct tegra_devfreq_soc_data *soc;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100201};
202
203struct tegra_actmon_emc_ratio {
204 unsigned long cpu_freq;
205 unsigned long emc_freq;
206};
207
Dmitry Osipenkob87dea32019-11-05 00:56:09 +0300208static const struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
Dmitry Osipenko53b4b2a2019-11-05 00:56:03 +0300209 { 1400000, KHZ_MAX },
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100210 { 1200000, 750000 },
211 { 1100000, 600000 },
212 { 1000000, 500000 },
213 { 800000, 375000 },
214 { 500000, 200000 },
215 { 250000, 100000 },
216};
217
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100218static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
219{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300220 return readl_relaxed(tegra->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100221}
222
223static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
224{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300225 writel_relaxed(val, tegra->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100226}
227
228static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
229{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300230 return readl_relaxed(dev->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100231}
232
233static void device_writel(struct tegra_devfreq_device *dev, u32 val,
234 u32 offset)
235{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300236 writel_relaxed(val, dev->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100237}
238
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300239static unsigned long do_percent(unsigned long long val, unsigned int pct)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100240{
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300241 val = val * pct;
242 do_div(val, 100);
243
244 /*
245 * High freq + high boosting percent + large polling interval are
246 * resulting in integer overflow when watermarks are calculated.
247 */
248 return min_t(u64, val, U32_MAX);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100249}
250
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100251static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
252 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100253{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100254 u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300255 u32 band = avg_band_freq * tegra->devfreq->profile->polling_ms;
256 u32 avg;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100257
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300258 avg = min(dev->avg_count, U32_MAX - band);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100259 device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
260
261 avg = max(dev->avg_count, band);
262 device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100263}
264
265static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
266 struct tegra_devfreq_device *dev)
267{
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300268 u32 val = tegra->cur_freq * tegra->devfreq->profile->polling_ms;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100269
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100270 device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
271 ACTMON_DEV_UPPER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100272
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100273 device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
274 ACTMON_DEV_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100275}
276
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100277static void actmon_isr_device(struct tegra_devfreq *tegra,
278 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100279{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100280 u32 intr_status, dev_ctrl;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100281
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100282 dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
283 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100284
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100285 intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
286 dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100287
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100288 if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100289 /*
290 * new_boost = min(old_boost * up_coef + step, max_freq)
291 */
292 dev->boost_freq = do_percent(dev->boost_freq,
293 dev->config->boost_up_coeff);
294 dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100295
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100296 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
297
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300298 if (dev->boost_freq >= tegra->max_freq) {
299 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100300 dev->boost_freq = tegra->max_freq;
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300301 }
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100302 } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100303 /*
304 * new_boost = old_boost * down_coef
305 * or 0 if (old_boost * down_coef < step / 2)
306 */
307 dev->boost_freq = do_percent(dev->boost_freq,
308 dev->config->boost_down_coeff);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100309
310 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
311
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300312 if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1)) {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100313 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300314 dev->boost_freq = 0;
315 }
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100316 }
317
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100318 device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
319
320 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100321}
322
323static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
324 unsigned long cpu_freq)
325{
326 unsigned int i;
Dmitry Osipenkob87dea32019-11-05 00:56:09 +0300327 const struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100328
329 for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
330 if (cpu_freq >= ratio->cpu_freq) {
331 if (ratio->emc_freq >= tegra->max_freq)
332 return tegra->max_freq;
333 else
334 return ratio->emc_freq;
335 }
336 }
337
338 return 0;
339}
340
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300341static unsigned long actmon_device_target_freq(struct tegra_devfreq *tegra,
342 struct tegra_devfreq_device *dev)
343{
344 unsigned int avg_sustain_coef;
345 unsigned long target_freq;
346
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300347 target_freq = dev->avg_count / tegra->devfreq->profile->polling_ms;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300348 avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
349 target_freq = do_percent(target_freq, avg_sustain_coef);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300350
351 return target_freq;
352}
353
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100354static void actmon_update_target(struct tegra_devfreq *tegra,
355 struct tegra_devfreq_device *dev)
356{
357 unsigned long cpu_freq = 0;
358 unsigned long static_cpu_emc_freq = 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100359
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300360 dev->target_freq = actmon_device_target_freq(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100361
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300362 if (dev->config->avg_dependency_threshold &&
363 dev->config->avg_dependency_threshold <= dev->target_freq) {
364 cpu_freq = cpufreq_quick_get(0);
365 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
366
367 dev->target_freq += dev->boost_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100368 dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300369 } else {
370 dev->target_freq += dev->boost_freq;
371 }
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100372}
373
374static irqreturn_t actmon_thread_isr(int irq, void *data)
375{
376 struct tegra_devfreq *tegra = data;
Dmitry Osipenkodd3f2612019-05-02 02:38:05 +0300377 bool handled = false;
378 unsigned int i;
379 u32 val;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100380
381 mutex_lock(&tegra->devfreq->lock);
Dmitry Osipenkodd3f2612019-05-02 02:38:05 +0300382
383 val = actmon_readl(tegra, ACTMON_GLB_STATUS);
384 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
385 if (val & tegra->devices[i].config->irq_mask) {
386 actmon_isr_device(tegra, tegra->devices + i);
387 handled = true;
388 }
389 }
390
391 if (handled)
392 update_devfreq(tegra->devfreq);
393
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100394 mutex_unlock(&tegra->devfreq->lock);
395
Dmitry Osipenkodd3f2612019-05-02 02:38:05 +0300396 return handled ? IRQ_HANDLED : IRQ_NONE;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100397}
398
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300399static int tegra_actmon_clk_notify_cb(struct notifier_block *nb,
400 unsigned long action, void *ptr)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100401{
402 struct clk_notifier_data *data = ptr;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100403 struct tegra_devfreq *tegra;
404 struct tegra_devfreq_device *dev;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100405 unsigned int i;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100406
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100407 if (action != POST_RATE_CHANGE)
408 return NOTIFY_OK;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100409
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300410 tegra = container_of(nb, struct tegra_devfreq, clk_rate_change_nb);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100411
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100412 tegra->cur_freq = data->new_rate / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100413
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100414 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
415 dev = &tegra->devices[i];
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100416
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100417 tegra_devfreq_update_wmark(tegra, dev);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100418 }
419
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100420 return NOTIFY_OK;
421}
422
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300423static void tegra_actmon_delayed_update(struct work_struct *work)
424{
425 struct tegra_devfreq *tegra = container_of(work, struct tegra_devfreq,
426 cpufreq_update_work.work);
427
428 mutex_lock(&tegra->devfreq->lock);
429 update_devfreq(tegra->devfreq);
430 mutex_unlock(&tegra->devfreq->lock);
431}
432
433static unsigned long
434tegra_actmon_cpufreq_contribution(struct tegra_devfreq *tegra,
435 unsigned int cpu_freq)
436{
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300437 struct tegra_devfreq_device *actmon_dev = &tegra->devices[MCCPU];
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300438 unsigned long static_cpu_emc_freq, dev_freq;
439
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300440 dev_freq = actmon_device_target_freq(tegra, actmon_dev);
441
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300442 /* check whether CPU's freq is taken into account at all */
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300443 if (dev_freq < actmon_dev->config->avg_dependency_threshold)
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300444 return 0;
445
446 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300447
Dmitry Osipenkod2216ba2020-04-03 01:24:48 +0300448 if (dev_freq + actmon_dev->boost_freq >= static_cpu_emc_freq)
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300449 return 0;
450
451 return static_cpu_emc_freq;
452}
453
454static int tegra_actmon_cpu_notify_cb(struct notifier_block *nb,
455 unsigned long action, void *ptr)
456{
457 struct cpufreq_freqs *freqs = ptr;
458 struct tegra_devfreq *tegra;
459 unsigned long old, new, delay;
460
461 if (action != CPUFREQ_POSTCHANGE)
462 return NOTIFY_OK;
463
464 tegra = container_of(nb, struct tegra_devfreq, cpu_rate_change_nb);
465
466 /*
467 * Quickly check whether CPU frequency should be taken into account
468 * at all, without blocking CPUFreq's core.
469 */
470 if (mutex_trylock(&tegra->devfreq->lock)) {
471 old = tegra_actmon_cpufreq_contribution(tegra, freqs->old);
472 new = tegra_actmon_cpufreq_contribution(tegra, freqs->new);
473 mutex_unlock(&tegra->devfreq->lock);
474
475 /*
476 * If CPU's frequency shouldn't be taken into account at
477 * the moment, then there is no need to update the devfreq's
478 * state because ISR will re-check CPU's frequency on the
479 * next interrupt.
480 */
481 if (old == new)
482 return NOTIFY_OK;
483 }
484
485 /*
486 * CPUFreq driver should support CPUFREQ_ASYNC_NOTIFICATION in order
487 * to allow asynchronous notifications. This means we can't block
488 * here for too long, otherwise CPUFreq's core will complain with a
489 * warning splat.
490 */
491 delay = msecs_to_jiffies(ACTMON_SAMPLING_PERIOD);
492 schedule_delayed_work(&tegra->cpufreq_update_work, delay);
493
494 return NOTIFY_OK;
495}
496
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100497static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
498 struct tegra_devfreq_device *dev)
499{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100500 u32 val = 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100501
Dmitry Osipenko14266552019-11-05 00:56:07 +0300502 /* reset boosting on governor's restart */
503 dev->boost_freq = 0;
504
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100505 dev->target_freq = tegra->cur_freq;
506
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300507 dev->avg_count = tegra->cur_freq * tegra->devfreq->profile->polling_ms;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100508 device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100509
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100510 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100511 tegra_devfreq_update_wmark(tegra, dev);
512
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300513 device_writel(dev, tegra->soc->count_weight, ACTMON_DEV_COUNT_WEIGHT);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100514 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100515
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100516 val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100517 val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
518 << ACTMON_DEV_CTRL_K_VAL_SHIFT;
519 val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
520 << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
521 val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
522 << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300523 val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
524 val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300525 val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100526 val |= ACTMON_DEV_CTRL_ENB;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100527
528 device_writel(dev, val, ACTMON_DEV_CTRL);
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300529}
530
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300531static void tegra_actmon_stop_devices(struct tegra_devfreq *tegra)
532{
533 struct tegra_devfreq_device *dev = tegra->devices;
534 unsigned int i;
535
536 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++, dev++) {
537 device_writel(dev, ACTMON_DEV_CTRL_STOP, ACTMON_DEV_CTRL);
538 device_writel(dev, ACTMON_INTR_STATUS_CLEAR,
539 ACTMON_DEV_INTR_STATUS);
540 }
541}
542
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300543static int tegra_actmon_resume(struct tegra_devfreq *tegra)
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300544{
545 unsigned int i;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300546 int err;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300547
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300548 if (!tegra->devfreq->profile->polling_ms || !tegra->started)
549 return 0;
550
551 actmon_writel(tegra, tegra->devfreq->profile->polling_ms - 1,
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300552 ACTMON_GLB_PERIOD_CTRL);
553
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300554 /*
555 * CLK notifications are needed in order to reconfigure the upper
556 * consecutive watermark in accordance to the actual clock rate
557 * to avoid unnecessary upper interrupts.
558 */
559 err = clk_notifier_register(tegra->emc_clock,
560 &tegra->clk_rate_change_nb);
561 if (err) {
562 dev_err(tegra->devfreq->dev.parent,
563 "Failed to register rate change notifier\n");
564 return err;
565 }
566
567 tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
568
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300569 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++)
570 tegra_actmon_configure_device(tegra, &tegra->devices[i]);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100571
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300572 /*
573 * We are estimating CPU's memory bandwidth requirement based on
574 * amount of memory accesses and system's load, judging by CPU's
575 * frequency. We also don't want to receive events about CPU's
576 * frequency transaction when governor is stopped, hence notifier
577 * is registered dynamically.
578 */
579 err = cpufreq_register_notifier(&tegra->cpu_rate_change_nb,
580 CPUFREQ_TRANSITION_NOTIFIER);
581 if (err) {
582 dev_err(tegra->devfreq->dev.parent,
583 "Failed to register rate change notifier: %d\n", err);
584 goto err_stop;
585 }
586
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300587 enable_irq(tegra->irq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300588
589 return 0;
590
591err_stop:
592 tegra_actmon_stop_devices(tegra);
593
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300594 clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
595
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300596 return err;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300597}
598
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300599static int tegra_actmon_start(struct tegra_devfreq *tegra)
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300600{
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300601 int ret = 0;
602
603 if (!tegra->started) {
604 tegra->started = true;
605
606 ret = tegra_actmon_resume(tegra);
607 if (ret)
608 tegra->started = false;
609 }
610
611 return ret;
612}
613
614static void tegra_actmon_pause(struct tegra_devfreq *tegra)
615{
616 if (!tegra->devfreq->profile->polling_ms || !tegra->started)
617 return;
618
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300619 disable_irq(tegra->irq);
620
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300621 cpufreq_unregister_notifier(&tegra->cpu_rate_change_nb,
622 CPUFREQ_TRANSITION_NOTIFIER);
623
624 cancel_delayed_work_sync(&tegra->cpufreq_update_work);
625
626 tegra_actmon_stop_devices(tegra);
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300627
628 clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100629}
630
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300631static void tegra_actmon_stop(struct tegra_devfreq *tegra)
632{
633 tegra_actmon_pause(tegra);
634 tegra->started = false;
635}
636
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100637static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
638 u32 flags)
639{
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100640 struct dev_pm_opp *opp;
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300641 int ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100642
Dmitry Osipenko62bacb02019-05-02 02:38:00 +0300643 opp = devfreq_recommended_opp(dev, freq, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100644 if (IS_ERR(opp)) {
Dmitry Osipenko62bacb02019-05-02 02:38:00 +0300645 dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100646 return PTR_ERR(opp);
647 }
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300648
Viresh Kumarc7f14212021-01-21 15:27:55 +0530649 ret = dev_pm_opp_set_opp(dev, opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530650 dev_pm_opp_put(opp);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100651
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300652 return ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100653}
654
655static int tegra_devfreq_get_dev_status(struct device *dev,
656 struct devfreq_dev_status *stat)
657{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100658 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100659 struct tegra_devfreq_device *actmon_dev;
Dmitry Osipenko151531f2019-05-02 02:38:08 +0300660 unsigned long cur_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100661
Dmitry Osipenko151531f2019-05-02 02:38:08 +0300662 cur_freq = READ_ONCE(tegra->cur_freq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100663
664 /* To be used by the tegra governor */
665 stat->private_data = tegra;
666
667 /* The below are to be used by the other governors */
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300668 stat->current_frequency = cur_freq * KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100669
670 actmon_dev = &tegra->devices[MCALL];
671
672 /* Number of cycles spent on memory access */
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100673 stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100674
675 /* The bus can be considered to be saturated way before 100% */
676 stat->busy_time *= 100 / BUS_SATURATION_RATIO;
677
678 /* Number of cycles in a sampling period */
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300679 stat->total_time = tegra->devfreq->profile->polling_ms * cur_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100680
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100681 stat->busy_time = min(stat->busy_time, stat->total_time);
682
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100683 return 0;
684}
685
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100686static struct devfreq_dev_profile tegra_devfreq_profile = {
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300687 .polling_ms = ACTMON_SAMPLING_PERIOD,
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100688 .target = tegra_devfreq_target,
689 .get_dev_status = tegra_devfreq_get_dev_status,
Dmitry Osipenko5e480ab2021-05-11 00:10:02 +0300690 .is_cooling_device = true,
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100691};
692
693static int tegra_governor_get_target(struct devfreq *devfreq,
694 unsigned long *freq)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100695{
MyungJoo Ham14de3902015-08-18 13:47:41 +0900696 struct devfreq_dev_status *stat;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100697 struct tegra_devfreq *tegra;
698 struct tegra_devfreq_device *dev;
699 unsigned long target_freq = 0;
700 unsigned int i;
701 int err;
702
MyungJoo Ham14de3902015-08-18 13:47:41 +0900703 err = devfreq_update_stats(devfreq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100704 if (err)
705 return err;
706
MyungJoo Ham14de3902015-08-18 13:47:41 +0900707 stat = &devfreq->last_status;
708
709 tegra = stat->private_data;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100710
711 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
712 dev = &tegra->devices[i];
713
714 actmon_update_target(tegra, dev);
715
716 target_freq = max(target_freq, dev->target_freq);
717 }
718
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300719 /*
720 * tegra-devfreq driver operates with KHz units, while OPP table
721 * entries use Hz units. Hence we need to convert the units for the
722 * devfreq core.
723 */
724 *freq = target_freq * KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100725
726 return 0;
727}
728
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100729static int tegra_governor_event_handler(struct devfreq *devfreq,
730 unsigned int event, void *data)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100731{
Yangtao Li1d1397c2019-02-16 10:18:26 -0500732 struct tegra_devfreq *tegra = dev_get_drvdata(devfreq->dev.parent);
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300733 unsigned int *new_delay = data;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300734 int ret = 0;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100735
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300736 /*
737 * Couple devfreq-device with the governor early because it is
738 * needed at the moment of governor's start (used by ISR).
739 */
740 tegra->devfreq = devfreq;
741
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100742 switch (event) {
743 case DEVFREQ_GOV_START:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100744 devfreq_monitor_start(devfreq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300745 ret = tegra_actmon_start(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100746 break;
747
748 case DEVFREQ_GOV_STOP:
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300749 tegra_actmon_stop(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100750 devfreq_monitor_stop(devfreq);
751 break;
752
Chanwoo Choi3a1ec2e2020-01-29 13:24:18 +0900753 case DEVFREQ_GOV_UPDATE_INTERVAL:
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300754 /*
755 * ACTMON hardware supports up to 256 milliseconds for the
756 * sampling period.
757 */
758 if (*new_delay > 256) {
759 ret = -EINVAL;
760 break;
761 }
762
763 tegra_actmon_pause(tegra);
Chanwoo Choi3a1ec2e2020-01-29 13:24:18 +0900764 devfreq_update_interval(devfreq, new_delay);
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300765 ret = tegra_actmon_resume(tegra);
766 break;
767
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100768 case DEVFREQ_GOV_SUSPEND:
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300769 tegra_actmon_stop(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100770 devfreq_monitor_suspend(devfreq);
771 break;
772
773 case DEVFREQ_GOV_RESUME:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100774 devfreq_monitor_resume(devfreq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300775 ret = tegra_actmon_start(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100776 break;
777 }
778
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300779 return ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100780}
781
782static struct devfreq_governor tegra_devfreq_governor = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100783 .name = "tegra_actmon",
Chanwoo Choi5f1a9062020-07-03 17:20:27 +0900784 .attrs = DEVFREQ_GOV_ATTR_POLLING_INTERVAL,
Chanwoo Choi0dd25a02020-10-05 14:48:01 +0900785 .flags = DEVFREQ_GOV_FLAG_IMMUTABLE
786 | DEVFREQ_GOV_FLAG_IRQ_DRIVEN,
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100787 .get_target_freq = tegra_governor_get_target,
788 .event_handler = tegra_governor_event_handler,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100789};
790
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300791static void devm_tegra_devfreq_deinit_hw(void *data)
792{
793 struct tegra_devfreq *tegra = data;
794
795 reset_control_reset(tegra->reset);
796 clk_disable_unprepare(tegra->clock);
797}
798
799static int devm_tegra_devfreq_init_hw(struct device *dev,
800 struct tegra_devfreq *tegra)
801{
802 int err;
803
804 err = clk_prepare_enable(tegra->clock);
805 if (err) {
806 dev_err(dev, "Failed to prepare and enable ACTMON clock\n");
807 return err;
808 }
809
810 err = devm_add_action_or_reset(dev, devm_tegra_devfreq_deinit_hw,
811 tegra);
812 if (err)
813 return err;
814
815 err = reset_control_reset(tegra->reset);
816 if (err) {
817 dev_err(dev, "Failed to reset hardware: %d\n", err);
818 return err;
819 }
820
821 return err;
822}
823
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100824static int tegra_devfreq_probe(struct platform_device *pdev)
825{
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300826 u32 hw_version = BIT(tegra_sku_info.soc_speedo_id);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100827 struct tegra_devfreq_device *dev;
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300828 struct tegra_devfreq *tegra;
829 struct devfreq *devfreq;
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300830 unsigned int i;
Dmitry Osipenko72964432019-11-05 00:56:01 +0300831 long rate;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100832 int err;
833
834 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
835 if (!tegra)
836 return -ENOMEM;
837
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300838 tegra->soc = of_device_get_match_data(&pdev->dev);
839
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300840 tegra->regs = devm_platform_ioremap_resource(pdev, 0);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100841 if (IS_ERR(tegra->regs))
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100842 return PTR_ERR(tegra->regs);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100843
844 tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
845 if (IS_ERR(tegra->reset)) {
846 dev_err(&pdev->dev, "Failed to get reset\n");
847 return PTR_ERR(tegra->reset);
848 }
849
850 tegra->clock = devm_clk_get(&pdev->dev, "actmon");
851 if (IS_ERR(tegra->clock)) {
852 dev_err(&pdev->dev, "Failed to get actmon clock\n");
853 return PTR_ERR(tegra->clock);
854 }
855
856 tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
Dmitry Osipenko09d56d92020-10-26 01:17:33 +0300857 if (IS_ERR(tegra->emc_clock))
858 return dev_err_probe(&pdev->dev, PTR_ERR(tegra->emc_clock),
859 "Failed to get emc clock\n");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100860
Dmitry Osipenkodccdea02019-11-05 00:55:59 +0300861 err = platform_get_irq(pdev, 0);
Markus Elfring0716f9f2020-04-04 20:34:02 +0200862 if (err < 0)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100863 return err;
Markus Elfring0716f9f2020-04-04 20:34:02 +0200864
Dmitry Osipenkodccdea02019-11-05 00:55:59 +0300865 tegra->irq = err;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100866
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300867 irq_set_status_flags(tegra->irq, IRQ_NOAUTOEN);
868
869 err = devm_request_threaded_irq(&pdev->dev, tegra->irq, NULL,
870 actmon_thread_isr, IRQF_ONESHOT,
871 "tegra-devfreq", tegra);
872 if (err) {
873 dev_err(&pdev->dev, "Interrupt request failed: %d\n", err);
874 return err;
875 }
876
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300877 err = devm_pm_opp_set_supported_hw(&pdev->dev, &hw_version, 1);
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300878 if (err) {
879 dev_err(&pdev->dev, "Failed to set supported HW: %d\n", err);
880 return err;
881 }
882
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300883 err = devm_pm_opp_of_add_table_noclk(&pdev->dev, 0);
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300884 if (err) {
885 dev_err(&pdev->dev, "Failed to add OPP table: %d\n", err);
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300886 return err;
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300887 }
888
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300889 err = devm_tegra_devfreq_init_hw(&pdev->dev, tegra);
890 if (err)
891 return err;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100892
Dmitry Osipenko72964432019-11-05 00:56:01 +0300893 rate = clk_round_rate(tegra->emc_clock, ULONG_MAX);
Dmitry Osipenko4844bdb2021-09-20 20:22:49 +0300894 if (rate <= 0) {
Dmitry Osipenko72964432019-11-05 00:56:01 +0300895 dev_err(&pdev->dev, "Failed to round clock rate: %ld\n", rate);
Dmitry Osipenko4844bdb2021-09-20 20:22:49 +0300896 return rate ?: -EINVAL;
Dmitry Osipenko72964432019-11-05 00:56:01 +0300897 }
898
Dmitry Osipenko72964432019-11-05 00:56:01 +0300899 tegra->max_freq = rate / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100900
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300901 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100902 dev = tegra->devices + i;
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300903 dev->config = tegra->soc->configs + i;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100904 dev->regs = tegra->regs + dev->config->offset;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100905 }
906
Tomeu Vizoso2da19b12015-03-17 10:36:16 +0100907 platform_set_drvdata(pdev, tegra);
908
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300909 tegra->clk_rate_change_nb.notifier_call = tegra_actmon_clk_notify_cb;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300910 tegra->cpu_rate_change_nb.notifier_call = tegra_actmon_cpu_notify_cb;
911
912 INIT_DELAYED_WORK(&tegra->cpufreq_update_work,
913 tegra_actmon_delayed_update);
914
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300915 err = devm_devfreq_add_governor(&pdev->dev, &tegra_devfreq_governor);
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300916 if (err) {
917 dev_err(&pdev->dev, "Failed to add governor: %d\n", err);
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300918 return err;
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300919 }
920
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300921 tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
Dmitry Osipenko0ce38842019-11-05 00:56:04 +0300922
Dmitry Osipenko68b79f22021-09-20 20:22:48 +0300923 devfreq = devm_devfreq_add_device(&pdev->dev, &tegra_devfreq_profile,
924 "tegra_actmon", NULL);
925 if (IS_ERR(devfreq))
926 return PTR_ERR(devfreq);
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300927
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100928 return 0;
929}
930
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300931static const struct tegra_devfreq_soc_data tegra124_soc = {
932 .configs = tegra124_device_configs,
933
934 /*
935 * Activity counter is incremented every 256 memory transactions,
936 * and each transaction takes 4 EMC clocks.
937 */
938 .count_weight = 4 * 256,
939};
940
941static const struct tegra_devfreq_soc_data tegra30_soc = {
942 .configs = tegra30_device_configs,
943 .count_weight = 2 * 256,
944};
945
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100946static const struct of_device_id tegra_devfreq_of_match[] = {
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300947 { .compatible = "nvidia,tegra30-actmon", .data = &tegra30_soc, },
948 { .compatible = "nvidia,tegra124-actmon", .data = &tegra124_soc, },
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100949 { },
950};
951
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100952MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
953
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100954static struct platform_driver tegra_devfreq_driver = {
955 .probe = tegra_devfreq_probe,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100956 .driver = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100957 .name = "tegra-devfreq",
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100958 .of_match_table = tegra_devfreq_of_match,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100959 },
960};
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300961module_platform_driver(tegra_devfreq_driver);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100962
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100963MODULE_LICENSE("GPL v2");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100964MODULE_DESCRIPTION("Tegra devfreq driver");
965MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");