blob: 10661eb2aed8700e9f9375de9e998431b3e86719 [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;
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300181 struct opp_table *opp_table;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100182
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100183 struct reset_control *reset;
184 struct clk *clock;
185 void __iomem *regs;
186
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100187 struct clk *emc_clock;
188 unsigned long max_freq;
189 unsigned long cur_freq;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300190 struct notifier_block clk_rate_change_nb;
191
192 struct delayed_work cpufreq_update_work;
193 struct notifier_block cpu_rate_change_nb;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100194
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300195 struct tegra_devfreq_device devices[2];
Dmitry Osipenko7514dd02019-05-02 02:38:06 +0300196
Dmitry Osipenkodccdea02019-11-05 00:55:59 +0300197 unsigned int irq;
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300198
199 bool started;
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300200
201 const struct tegra_devfreq_soc_data *soc;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100202};
203
204struct tegra_actmon_emc_ratio {
205 unsigned long cpu_freq;
206 unsigned long emc_freq;
207};
208
Dmitry Osipenkob87dea32019-11-05 00:56:09 +0300209static const struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
Dmitry Osipenko53b4b2a2019-11-05 00:56:03 +0300210 { 1400000, KHZ_MAX },
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100211 { 1200000, 750000 },
212 { 1100000, 600000 },
213 { 1000000, 500000 },
214 { 800000, 375000 },
215 { 500000, 200000 },
216 { 250000, 100000 },
217};
218
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100219static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
220{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300221 return readl_relaxed(tegra->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100222}
223
224static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
225{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300226 writel_relaxed(val, tegra->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100227}
228
229static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
230{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300231 return readl_relaxed(dev->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100232}
233
234static void device_writel(struct tegra_devfreq_device *dev, u32 val,
235 u32 offset)
236{
Dmitry Osipenkoefe90432019-05-02 02:38:01 +0300237 writel_relaxed(val, dev->regs + offset);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100238}
239
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300240static unsigned long do_percent(unsigned long long val, unsigned int pct)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100241{
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300242 val = val * pct;
243 do_div(val, 100);
244
245 /*
246 * High freq + high boosting percent + large polling interval are
247 * resulting in integer overflow when watermarks are calculated.
248 */
249 return min_t(u64, val, U32_MAX);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100250}
251
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100252static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
253 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100254{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100255 u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300256 u32 band = avg_band_freq * tegra->devfreq->profile->polling_ms;
257 u32 avg;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100258
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300259 avg = min(dev->avg_count, U32_MAX - band);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100260 device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
261
262 avg = max(dev->avg_count, band);
263 device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100264}
265
266static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
267 struct tegra_devfreq_device *dev)
268{
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300269 u32 val = tegra->cur_freq * tegra->devfreq->profile->polling_ms;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100270
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100271 device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
272 ACTMON_DEV_UPPER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100273
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100274 device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
275 ACTMON_DEV_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100276}
277
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100278static void actmon_isr_device(struct tegra_devfreq *tegra,
279 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100280{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100281 u32 intr_status, dev_ctrl;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100282
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100283 dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
284 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100285
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100286 intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
287 dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100288
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100289 if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100290 /*
291 * new_boost = min(old_boost * up_coef + step, max_freq)
292 */
293 dev->boost_freq = do_percent(dev->boost_freq,
294 dev->config->boost_up_coeff);
295 dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100296
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100297 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
298
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300299 if (dev->boost_freq >= tegra->max_freq) {
300 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100301 dev->boost_freq = tegra->max_freq;
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300302 }
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100303 } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100304 /*
305 * new_boost = old_boost * down_coef
306 * or 0 if (old_boost * down_coef < step / 2)
307 */
308 dev->boost_freq = do_percent(dev->boost_freq,
309 dev->config->boost_down_coeff);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100310
311 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
312
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300313 if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1)) {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100314 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Dmitry Osipenko88ec8162019-11-05 00:56:12 +0300315 dev->boost_freq = 0;
316 }
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100317 }
318
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100319 device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
320
321 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100322}
323
324static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
325 unsigned long cpu_freq)
326{
327 unsigned int i;
Dmitry Osipenkob87dea32019-11-05 00:56:09 +0300328 const struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100329
330 for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
331 if (cpu_freq >= ratio->cpu_freq) {
332 if (ratio->emc_freq >= tegra->max_freq)
333 return tegra->max_freq;
334 else
335 return ratio->emc_freq;
336 }
337 }
338
339 return 0;
340}
341
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300342static unsigned long actmon_device_target_freq(struct tegra_devfreq *tegra,
343 struct tegra_devfreq_device *dev)
344{
345 unsigned int avg_sustain_coef;
346 unsigned long target_freq;
347
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300348 target_freq = dev->avg_count / tegra->devfreq->profile->polling_ms;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300349 avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
350 target_freq = do_percent(target_freq, avg_sustain_coef);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300351
352 return target_freq;
353}
354
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100355static void actmon_update_target(struct tegra_devfreq *tegra,
356 struct tegra_devfreq_device *dev)
357{
358 unsigned long cpu_freq = 0;
359 unsigned long static_cpu_emc_freq = 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100360
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300361 dev->target_freq = actmon_device_target_freq(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100362
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300363 if (dev->config->avg_dependency_threshold &&
364 dev->config->avg_dependency_threshold <= dev->target_freq) {
365 cpu_freq = cpufreq_quick_get(0);
366 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
367
368 dev->target_freq += dev->boost_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100369 dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300370 } else {
371 dev->target_freq += dev->boost_freq;
372 }
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100373}
374
375static irqreturn_t actmon_thread_isr(int irq, void *data)
376{
377 struct tegra_devfreq *tegra = data;
Dmitry Osipenkodd3f2612019-05-02 02:38:05 +0300378 bool handled = false;
379 unsigned int i;
380 u32 val;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100381
382 mutex_lock(&tegra->devfreq->lock);
Dmitry Osipenkodd3f2612019-05-02 02:38:05 +0300383
384 val = actmon_readl(tegra, ACTMON_GLB_STATUS);
385 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
386 if (val & tegra->devices[i].config->irq_mask) {
387 actmon_isr_device(tegra, tegra->devices + i);
388 handled = true;
389 }
390 }
391
392 if (handled)
393 update_devfreq(tegra->devfreq);
394
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100395 mutex_unlock(&tegra->devfreq->lock);
396
Dmitry Osipenkodd3f2612019-05-02 02:38:05 +0300397 return handled ? IRQ_HANDLED : IRQ_NONE;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100398}
399
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300400static int tegra_actmon_clk_notify_cb(struct notifier_block *nb,
401 unsigned long action, void *ptr)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100402{
403 struct clk_notifier_data *data = ptr;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100404 struct tegra_devfreq *tegra;
405 struct tegra_devfreq_device *dev;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100406 unsigned int i;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100407
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100408 if (action != POST_RATE_CHANGE)
409 return NOTIFY_OK;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100410
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300411 tegra = container_of(nb, struct tegra_devfreq, clk_rate_change_nb);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100412
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100413 tegra->cur_freq = data->new_rate / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100414
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100415 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
416 dev = &tegra->devices[i];
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100417
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100418 tegra_devfreq_update_wmark(tegra, dev);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100419 }
420
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100421 return NOTIFY_OK;
422}
423
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300424static void tegra_actmon_delayed_update(struct work_struct *work)
425{
426 struct tegra_devfreq *tegra = container_of(work, struct tegra_devfreq,
427 cpufreq_update_work.work);
428
429 mutex_lock(&tegra->devfreq->lock);
430 update_devfreq(tegra->devfreq);
431 mutex_unlock(&tegra->devfreq->lock);
432}
433
434static unsigned long
435tegra_actmon_cpufreq_contribution(struct tegra_devfreq *tegra,
436 unsigned int cpu_freq)
437{
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300438 struct tegra_devfreq_device *actmon_dev = &tegra->devices[MCCPU];
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300439 unsigned long static_cpu_emc_freq, dev_freq;
440
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300441 dev_freq = actmon_device_target_freq(tegra, actmon_dev);
442
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300443 /* check whether CPU's freq is taken into account at all */
Dmitry Osipenko28615e32019-11-05 00:56:13 +0300444 if (dev_freq < actmon_dev->config->avg_dependency_threshold)
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300445 return 0;
446
447 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300448
Dmitry Osipenkod2216ba2020-04-03 01:24:48 +0300449 if (dev_freq + actmon_dev->boost_freq >= static_cpu_emc_freq)
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300450 return 0;
451
452 return static_cpu_emc_freq;
453}
454
455static int tegra_actmon_cpu_notify_cb(struct notifier_block *nb,
456 unsigned long action, void *ptr)
457{
458 struct cpufreq_freqs *freqs = ptr;
459 struct tegra_devfreq *tegra;
460 unsigned long old, new, delay;
461
462 if (action != CPUFREQ_POSTCHANGE)
463 return NOTIFY_OK;
464
465 tegra = container_of(nb, struct tegra_devfreq, cpu_rate_change_nb);
466
467 /*
468 * Quickly check whether CPU frequency should be taken into account
469 * at all, without blocking CPUFreq's core.
470 */
471 if (mutex_trylock(&tegra->devfreq->lock)) {
472 old = tegra_actmon_cpufreq_contribution(tegra, freqs->old);
473 new = tegra_actmon_cpufreq_contribution(tegra, freqs->new);
474 mutex_unlock(&tegra->devfreq->lock);
475
476 /*
477 * If CPU's frequency shouldn't be taken into account at
478 * the moment, then there is no need to update the devfreq's
479 * state because ISR will re-check CPU's frequency on the
480 * next interrupt.
481 */
482 if (old == new)
483 return NOTIFY_OK;
484 }
485
486 /*
487 * CPUFreq driver should support CPUFREQ_ASYNC_NOTIFICATION in order
488 * to allow asynchronous notifications. This means we can't block
489 * here for too long, otherwise CPUFreq's core will complain with a
490 * warning splat.
491 */
492 delay = msecs_to_jiffies(ACTMON_SAMPLING_PERIOD);
493 schedule_delayed_work(&tegra->cpufreq_update_work, delay);
494
495 return NOTIFY_OK;
496}
497
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100498static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
499 struct tegra_devfreq_device *dev)
500{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100501 u32 val = 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100502
Dmitry Osipenko14266552019-11-05 00:56:07 +0300503 /* reset boosting on governor's restart */
504 dev->boost_freq = 0;
505
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100506 dev->target_freq = tegra->cur_freq;
507
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300508 dev->avg_count = tegra->cur_freq * tegra->devfreq->profile->polling_ms;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100509 device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100510
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100511 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100512 tegra_devfreq_update_wmark(tegra, dev);
513
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300514 device_writel(dev, tegra->soc->count_weight, ACTMON_DEV_COUNT_WEIGHT);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100515 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100516
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100517 val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100518 val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
519 << ACTMON_DEV_CTRL_K_VAL_SHIFT;
520 val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
521 << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
522 val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
523 << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300524 val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
525 val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300526 val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100527 val |= ACTMON_DEV_CTRL_ENB;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100528
529 device_writel(dev, val, ACTMON_DEV_CTRL);
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300530}
531
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300532static void tegra_actmon_stop_devices(struct tegra_devfreq *tegra)
533{
534 struct tegra_devfreq_device *dev = tegra->devices;
535 unsigned int i;
536
537 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++, dev++) {
538 device_writel(dev, ACTMON_DEV_CTRL_STOP, ACTMON_DEV_CTRL);
539 device_writel(dev, ACTMON_INTR_STATUS_CLEAR,
540 ACTMON_DEV_INTR_STATUS);
541 }
542}
543
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300544static int tegra_actmon_resume(struct tegra_devfreq *tegra)
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300545{
546 unsigned int i;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300547 int err;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300548
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300549 if (!tegra->devfreq->profile->polling_ms || !tegra->started)
550 return 0;
551
552 actmon_writel(tegra, tegra->devfreq->profile->polling_ms - 1,
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300553 ACTMON_GLB_PERIOD_CTRL);
554
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300555 /*
556 * CLK notifications are needed in order to reconfigure the upper
557 * consecutive watermark in accordance to the actual clock rate
558 * to avoid unnecessary upper interrupts.
559 */
560 err = clk_notifier_register(tegra->emc_clock,
561 &tegra->clk_rate_change_nb);
562 if (err) {
563 dev_err(tegra->devfreq->dev.parent,
564 "Failed to register rate change notifier\n");
565 return err;
566 }
567
568 tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
569
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300570 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++)
571 tegra_actmon_configure_device(tegra, &tegra->devices[i]);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100572
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300573 /*
574 * We are estimating CPU's memory bandwidth requirement based on
575 * amount of memory accesses and system's load, judging by CPU's
576 * frequency. We also don't want to receive events about CPU's
577 * frequency transaction when governor is stopped, hence notifier
578 * is registered dynamically.
579 */
580 err = cpufreq_register_notifier(&tegra->cpu_rate_change_nb,
581 CPUFREQ_TRANSITION_NOTIFIER);
582 if (err) {
583 dev_err(tegra->devfreq->dev.parent,
584 "Failed to register rate change notifier: %d\n", err);
585 goto err_stop;
586 }
587
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300588 enable_irq(tegra->irq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300589
590 return 0;
591
592err_stop:
593 tegra_actmon_stop_devices(tegra);
594
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300595 clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
596
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300597 return err;
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300598}
599
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300600static int tegra_actmon_start(struct tegra_devfreq *tegra)
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300601{
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300602 int ret = 0;
603
604 if (!tegra->started) {
605 tegra->started = true;
606
607 ret = tegra_actmon_resume(tegra);
608 if (ret)
609 tegra->started = false;
610 }
611
612 return ret;
613}
614
615static void tegra_actmon_pause(struct tegra_devfreq *tegra)
616{
617 if (!tegra->devfreq->profile->polling_ms || !tegra->started)
618 return;
619
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300620 disable_irq(tegra->irq);
621
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300622 cpufreq_unregister_notifier(&tegra->cpu_rate_change_nb,
623 CPUFREQ_TRANSITION_NOTIFIER);
624
625 cancel_delayed_work_sync(&tegra->cpufreq_update_work);
626
627 tegra_actmon_stop_devices(tegra);
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300628
629 clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100630}
631
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300632static void tegra_actmon_stop(struct tegra_devfreq *tegra)
633{
634 tegra_actmon_pause(tegra);
635 tegra->started = false;
636}
637
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100638static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
639 u32 flags)
640{
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100641 struct dev_pm_opp *opp;
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300642 int ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100643
Dmitry Osipenko62bacb02019-05-02 02:38:00 +0300644 opp = devfreq_recommended_opp(dev, freq, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100645 if (IS_ERR(opp)) {
Dmitry Osipenko62bacb02019-05-02 02:38:00 +0300646 dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100647 return PTR_ERR(opp);
648 }
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300649
Viresh Kumarc7f14212021-01-21 15:27:55 +0530650 ret = dev_pm_opp_set_opp(dev, opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530651 dev_pm_opp_put(opp);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100652
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300653 return ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100654}
655
656static int tegra_devfreq_get_dev_status(struct device *dev,
657 struct devfreq_dev_status *stat)
658{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100659 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100660 struct tegra_devfreq_device *actmon_dev;
Dmitry Osipenko151531f2019-05-02 02:38:08 +0300661 unsigned long cur_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100662
Dmitry Osipenko151531f2019-05-02 02:38:08 +0300663 cur_freq = READ_ONCE(tegra->cur_freq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100664
665 /* To be used by the tegra governor */
666 stat->private_data = tegra;
667
668 /* The below are to be used by the other governors */
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300669 stat->current_frequency = cur_freq * KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100670
671 actmon_dev = &tegra->devices[MCALL];
672
673 /* Number of cycles spent on memory access */
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100674 stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100675
676 /* The bus can be considered to be saturated way before 100% */
677 stat->busy_time *= 100 / BUS_SATURATION_RATIO;
678
679 /* Number of cycles in a sampling period */
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300680 stat->total_time = tegra->devfreq->profile->polling_ms * cur_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100681
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100682 stat->busy_time = min(stat->busy_time, stat->total_time);
683
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100684 return 0;
685}
686
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100687static struct devfreq_dev_profile tegra_devfreq_profile = {
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300688 .polling_ms = ACTMON_SAMPLING_PERIOD,
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100689 .target = tegra_devfreq_target,
690 .get_dev_status = tegra_devfreq_get_dev_status,
Dmitry Osipenko5e480ab2021-05-11 00:10:02 +0300691 .is_cooling_device = true,
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100692};
693
694static int tegra_governor_get_target(struct devfreq *devfreq,
695 unsigned long *freq)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100696{
MyungJoo Ham14de3902015-08-18 13:47:41 +0900697 struct devfreq_dev_status *stat;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100698 struct tegra_devfreq *tegra;
699 struct tegra_devfreq_device *dev;
700 unsigned long target_freq = 0;
701 unsigned int i;
702 int err;
703
MyungJoo Ham14de3902015-08-18 13:47:41 +0900704 err = devfreq_update_stats(devfreq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100705 if (err)
706 return err;
707
MyungJoo Ham14de3902015-08-18 13:47:41 +0900708 stat = &devfreq->last_status;
709
710 tegra = stat->private_data;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100711
712 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
713 dev = &tegra->devices[i];
714
715 actmon_update_target(tegra, dev);
716
717 target_freq = max(target_freq, dev->target_freq);
718 }
719
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300720 /*
721 * tegra-devfreq driver operates with KHz units, while OPP table
722 * entries use Hz units. Hence we need to convert the units for the
723 * devfreq core.
724 */
725 *freq = target_freq * KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100726
727 return 0;
728}
729
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100730static int tegra_governor_event_handler(struct devfreq *devfreq,
731 unsigned int event, void *data)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100732{
Yangtao Li1d1397c2019-02-16 10:18:26 -0500733 struct tegra_devfreq *tegra = dev_get_drvdata(devfreq->dev.parent);
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300734 unsigned int *new_delay = data;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300735 int ret = 0;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100736
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300737 /*
738 * Couple devfreq-device with the governor early because it is
739 * needed at the moment of governor's start (used by ISR).
740 */
741 tegra->devfreq = devfreq;
742
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100743 switch (event) {
744 case DEVFREQ_GOV_START:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100745 devfreq_monitor_start(devfreq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300746 ret = tegra_actmon_start(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100747 break;
748
749 case DEVFREQ_GOV_STOP:
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300750 tegra_actmon_stop(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100751 devfreq_monitor_stop(devfreq);
752 break;
753
Chanwoo Choi3a1ec2e2020-01-29 13:24:18 +0900754 case DEVFREQ_GOV_UPDATE_INTERVAL:
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300755 /*
756 * ACTMON hardware supports up to 256 milliseconds for the
757 * sampling period.
758 */
759 if (*new_delay > 256) {
760 ret = -EINVAL;
761 break;
762 }
763
764 tegra_actmon_pause(tegra);
Chanwoo Choi3a1ec2e2020-01-29 13:24:18 +0900765 devfreq_update_interval(devfreq, new_delay);
Dmitry Osipenkof61ee202019-11-05 00:56:15 +0300766 ret = tegra_actmon_resume(tegra);
767 break;
768
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100769 case DEVFREQ_GOV_SUSPEND:
Dmitry Osipenko546ff092019-05-02 02:38:11 +0300770 tegra_actmon_stop(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100771 devfreq_monitor_suspend(devfreq);
772 break;
773
774 case DEVFREQ_GOV_RESUME:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100775 devfreq_monitor_resume(devfreq);
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300776 ret = tegra_actmon_start(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100777 break;
778 }
779
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300780 return ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100781}
782
783static struct devfreq_governor tegra_devfreq_governor = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100784 .name = "tegra_actmon",
Chanwoo Choi5f1a9062020-07-03 17:20:27 +0900785 .attrs = DEVFREQ_GOV_ATTR_POLLING_INTERVAL,
Chanwoo Choi0dd25a02020-10-05 14:48:01 +0900786 .flags = DEVFREQ_GOV_FLAG_IMMUTABLE
787 | DEVFREQ_GOV_FLAG_IRQ_DRIVEN,
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100788 .get_target_freq = tegra_governor_get_target,
789 .event_handler = tegra_governor_event_handler,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100790};
791
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100792static int tegra_devfreq_probe(struct platform_device *pdev)
793{
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300794 u32 hw_version = BIT(tegra_sku_info.soc_speedo_id);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100795 struct tegra_devfreq_device *dev;
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300796 struct tegra_devfreq *tegra;
797 struct devfreq *devfreq;
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300798 unsigned int i;
Dmitry Osipenko72964432019-11-05 00:56:01 +0300799 long rate;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100800 int err;
801
802 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
803 if (!tegra)
804 return -ENOMEM;
805
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300806 tegra->soc = of_device_get_match_data(&pdev->dev);
807
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300808 tegra->regs = devm_platform_ioremap_resource(pdev, 0);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100809 if (IS_ERR(tegra->regs))
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100810 return PTR_ERR(tegra->regs);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100811
812 tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
813 if (IS_ERR(tegra->reset)) {
814 dev_err(&pdev->dev, "Failed to get reset\n");
815 return PTR_ERR(tegra->reset);
816 }
817
818 tegra->clock = devm_clk_get(&pdev->dev, "actmon");
819 if (IS_ERR(tegra->clock)) {
820 dev_err(&pdev->dev, "Failed to get actmon clock\n");
821 return PTR_ERR(tegra->clock);
822 }
823
824 tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
Dmitry Osipenko09d56d92020-10-26 01:17:33 +0300825 if (IS_ERR(tegra->emc_clock))
826 return dev_err_probe(&pdev->dev, PTR_ERR(tegra->emc_clock),
827 "Failed to get emc clock\n");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100828
Dmitry Osipenkodccdea02019-11-05 00:55:59 +0300829 err = platform_get_irq(pdev, 0);
Markus Elfring0716f9f2020-04-04 20:34:02 +0200830 if (err < 0)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100831 return err;
Markus Elfring0716f9f2020-04-04 20:34:02 +0200832
Dmitry Osipenkodccdea02019-11-05 00:55:59 +0300833 tegra->irq = err;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100834
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300835 irq_set_status_flags(tegra->irq, IRQ_NOAUTOEN);
836
837 err = devm_request_threaded_irq(&pdev->dev, tegra->irq, NULL,
838 actmon_thread_isr, IRQF_ONESHOT,
839 "tegra-devfreq", tegra);
840 if (err) {
841 dev_err(&pdev->dev, "Interrupt request failed: %d\n", err);
842 return err;
843 }
844
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300845 tegra->opp_table = dev_pm_opp_set_supported_hw(&pdev->dev,
846 &hw_version, 1);
847 err = PTR_ERR_OR_ZERO(tegra->opp_table);
848 if (err) {
849 dev_err(&pdev->dev, "Failed to set supported HW: %d\n", err);
850 return err;
851 }
852
Viresh Kumarc7f14212021-01-21 15:27:55 +0530853 err = dev_pm_opp_of_add_table_noclk(&pdev->dev, 0);
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300854 if (err) {
855 dev_err(&pdev->dev, "Failed to add OPP table: %d\n", err);
856 goto put_hw;
857 }
858
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100859 err = clk_prepare_enable(tegra->clock);
860 if (err) {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100861 dev_err(&pdev->dev,
862 "Failed to prepare and enable ACTMON clock\n");
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300863 goto remove_table;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100864 }
865
Dmitry Osipenkod353d122020-09-27 23:51:39 +0300866 err = reset_control_reset(tegra->reset);
867 if (err) {
868 dev_err(&pdev->dev, "Failed to reset hardware: %d\n", err);
869 goto disable_clk;
870 }
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100871
Dmitry Osipenko72964432019-11-05 00:56:01 +0300872 rate = clk_round_rate(tegra->emc_clock, ULONG_MAX);
873 if (rate < 0) {
874 dev_err(&pdev->dev, "Failed to round clock rate: %ld\n", rate);
Dan Carpenter6bf56072020-09-08 10:25:57 +0300875 err = rate;
876 goto disable_clk;
Dmitry Osipenko72964432019-11-05 00:56:01 +0300877 }
878
Dmitry Osipenko72964432019-11-05 00:56:01 +0300879 tegra->max_freq = rate / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100880
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300881 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100882 dev = tegra->devices + i;
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300883 dev->config = tegra->soc->configs + i;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100884 dev->regs = tegra->regs + dev->config->offset;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100885 }
886
Tomeu Vizoso2da19b12015-03-17 10:36:16 +0100887 platform_set_drvdata(pdev, tegra);
888
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300889 tegra->clk_rate_change_nb.notifier_call = tegra_actmon_clk_notify_cb;
Dmitry Osipenko11eb6ec2019-11-05 00:56:05 +0300890 tegra->cpu_rate_change_nb.notifier_call = tegra_actmon_cpu_notify_cb;
891
892 INIT_DELAYED_WORK(&tegra->cpufreq_update_work,
893 tegra_actmon_delayed_update);
894
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300895 err = devfreq_add_governor(&tegra_devfreq_governor);
896 if (err) {
897 dev_err(&pdev->dev, "Failed to add governor: %d\n", err);
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300898 goto remove_opps;
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300899 }
900
Dmitry Osipenko6f2a35d2019-11-05 00:56:06 +0300901 tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
Dmitry Osipenko0ce38842019-11-05 00:56:04 +0300902
Dmitry Osipenkod49eeb12019-11-05 00:56:00 +0300903 devfreq = devfreq_add_device(&pdev->dev, &tegra_devfreq_profile,
904 "tegra_actmon", NULL);
905 if (IS_ERR(devfreq)) {
906 err = PTR_ERR(devfreq);
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300907 goto remove_governor;
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300908 }
909
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100910 return 0;
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300911
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300912remove_governor:
913 devfreq_remove_governor(&tegra_devfreq_governor);
914
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300915remove_opps:
916 dev_pm_opp_remove_all_dynamic(&pdev->dev);
917
918 reset_control_reset(tegra->reset);
Dan Carpenter6bf56072020-09-08 10:25:57 +0300919disable_clk:
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300920 clk_disable_unprepare(tegra->clock);
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300921remove_table:
922 dev_pm_opp_of_remove_table(&pdev->dev);
923put_hw:
924 dev_pm_opp_put_supported_hw(tegra->opp_table);
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300925
926 return err;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100927}
928
929static int tegra_devfreq_remove(struct platform_device *pdev)
930{
931 struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100932
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300933 devfreq_remove_device(tegra->devfreq);
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300934 devfreq_remove_governor(&tegra_devfreq_governor);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100935
Dmitry Osipenko8fda5c12019-05-02 02:38:07 +0300936 reset_control_reset(tegra->reset);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100937 clk_disable_unprepare(tegra->clock);
938
Dmitry Osipenko16e8b2a2020-12-03 22:24:38 +0300939 dev_pm_opp_of_remove_table(&pdev->dev);
940 dev_pm_opp_put_supported_hw(tegra->opp_table);
941
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100942 return 0;
943}
944
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300945static const struct tegra_devfreq_soc_data tegra124_soc = {
946 .configs = tegra124_device_configs,
947
948 /*
949 * Activity counter is incremented every 256 memory transactions,
950 * and each transaction takes 4 EMC clocks.
951 */
952 .count_weight = 4 * 256,
953};
954
955static const struct tegra_devfreq_soc_data tegra30_soc = {
956 .configs = tegra30_device_configs,
957 .count_weight = 2 * 256,
958};
959
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100960static const struct of_device_id tegra_devfreq_of_match[] = {
Dmitry Osipenko6a575e82020-12-03 22:24:39 +0300961 { .compatible = "nvidia,tegra30-actmon", .data = &tegra30_soc, },
962 { .compatible = "nvidia,tegra124-actmon", .data = &tegra124_soc, },
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100963 { },
964};
965
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100966MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
967
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100968static struct platform_driver tegra_devfreq_driver = {
969 .probe = tegra_devfreq_probe,
970 .remove = tegra_devfreq_remove,
971 .driver = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100972 .name = "tegra-devfreq",
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100973 .of_match_table = tegra_devfreq_of_match,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100974 },
975};
Dmitry Osipenko5a7e10c2019-05-02 02:38:10 +0300976module_platform_driver(tegra_devfreq_driver);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100977
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100978MODULE_LICENSE("GPL v2");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100979MODULE_DESCRIPTION("Tegra devfreq driver");
980MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");