blob: 479d9575e1245892312facdf1bc4c4f5803e5120 [file] [log] [blame]
Tomeu Vizoso6234f382014-11-24 13:28:17 +01001/*
2 * A devfreq driver for NVIDIA Tegra SoCs
3 *
4 * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
5 * Copyright (C) 2014 Google, Inc
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <linux/clk.h>
22#include <linux/cpufreq.h>
23#include <linux/devfreq.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070027#include <linux/mod_devicetable.h>
Tomeu Vizoso6234f382014-11-24 13:28:17 +010028#include <linux/platform_device.h>
29#include <linux/pm_opp.h>
30#include <linux/reset.h>
31
32#include "governor.h"
33
34#define ACTMON_GLB_STATUS 0x0
35#define ACTMON_GLB_PERIOD_CTRL 0x4
36
37#define ACTMON_DEV_CTRL 0x0
38#define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
39#define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
40#define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
41#define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
42#define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
43#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
44#define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
45#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
46#define ACTMON_DEV_CTRL_ENB BIT(31)
47
48#define ACTMON_DEV_UPPER_WMARK 0x4
49#define ACTMON_DEV_LOWER_WMARK 0x8
50#define ACTMON_DEV_INIT_AVG 0xc
51#define ACTMON_DEV_AVG_UPPER_WMARK 0x10
52#define ACTMON_DEV_AVG_LOWER_WMARK 0x14
53#define ACTMON_DEV_COUNT_WEIGHT 0x18
54#define ACTMON_DEV_AVG_COUNT 0x20
55#define ACTMON_DEV_INTR_STATUS 0x24
56
57#define ACTMON_INTR_STATUS_CLEAR 0xffffffff
58
59#define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
60#define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
61
62#define ACTMON_ABOVE_WMARK_WINDOW 1
63#define ACTMON_BELOW_WMARK_WINDOW 3
64#define ACTMON_BOOST_FREQ_STEP 16000
65
Tomeu Vizoso11573e92015-03-17 10:36:12 +010066/*
67 * Activity counter is incremented every 256 memory transactions, and each
Tomeu Vizoso6234f382014-11-24 13:28:17 +010068 * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is
69 * 4 * 256 = 1024.
70 */
71#define ACTMON_COUNT_WEIGHT 0x400
72
73/*
74 * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
75 * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
76 */
77#define ACTMON_AVERAGE_WINDOW_LOG2 6
78#define ACTMON_SAMPLING_PERIOD 12 /* ms */
79#define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
80
81#define KHZ 1000
82
Dmitry Osipenkoea678da2019-11-05 00:56:03 +030083#define KHZ_MAX (ULONG_MAX / KHZ)
84
Tomeu Vizoso6234f382014-11-24 13:28:17 +010085/* Assume that the bus is saturated if the utilization is 25% */
86#define BUS_SATURATION_RATIO 25
87
88/**
89 * struct tegra_devfreq_device_config - configuration specific to an ACTMON
90 * device
91 *
Tomeu Vizoso11573e92015-03-17 10:36:12 +010092 * Coefficients and thresholds are percentages unless otherwise noted
Tomeu Vizoso6234f382014-11-24 13:28:17 +010093 */
94struct tegra_devfreq_device_config {
95 u32 offset;
96 u32 irq_mask;
97
Tomeu Vizoso11573e92015-03-17 10:36:12 +010098 /* Factors applied to boost_freq every consecutive watermark breach */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010099 unsigned int boost_up_coeff;
100 unsigned int boost_down_coeff;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100101
102 /* Define the watermark bounds when applied to the current avg */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100103 unsigned int boost_up_threshold;
104 unsigned int boost_down_threshold;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100105
106 /*
107 * Threshold of activity (cycles) below which the CPU frequency isn't
108 * to be taken into account. This is to avoid increasing the EMC
109 * frequency when the CPU is very busy but not accessing the bus often.
110 */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100111 u32 avg_dependency_threshold;
112};
113
114enum tegra_actmon_device {
115 MCALL = 0,
116 MCCPU,
117};
118
119static struct tegra_devfreq_device_config actmon_device_configs[] = {
120 {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100121 /* MCALL: All memory accesses (including from the CPUs) */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100122 .offset = 0x1c0,
123 .irq_mask = 1 << 26,
124 .boost_up_coeff = 200,
125 .boost_down_coeff = 50,
126 .boost_up_threshold = 60,
127 .boost_down_threshold = 40,
128 },
129 {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100130 /* MCCPU: memory accesses from the CPUs */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100131 .offset = 0x200,
132 .irq_mask = 1 << 25,
133 .boost_up_coeff = 800,
134 .boost_down_coeff = 90,
135 .boost_up_threshold = 27,
136 .boost_down_threshold = 10,
137 .avg_dependency_threshold = 50000,
138 },
139};
140
141/**
142 * struct tegra_devfreq_device - state specific to an ACTMON device
143 *
144 * Frequencies are in kHz.
145 */
146struct tegra_devfreq_device {
147 const struct tegra_devfreq_device_config *config;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100148 void __iomem *regs;
149 spinlock_t lock;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100150
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100151 /* Average event count sampled in the last interrupt */
152 u32 avg_count;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100153
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100154 /*
155 * Extra frequency to increase the target by due to consecutive
156 * watermark breaches.
157 */
158 unsigned long boost_freq;
159
160 /* Optimal frequency calculated from the stats for this device */
161 unsigned long target_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100162};
163
164struct tegra_devfreq {
165 struct devfreq *devfreq;
166
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100167 struct reset_control *reset;
168 struct clk *clock;
169 void __iomem *regs;
170
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100171 struct clk *emc_clock;
172 unsigned long max_freq;
173 unsigned long cur_freq;
174 struct notifier_block rate_change_nb;
175
176 struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)];
177};
178
179struct tegra_actmon_emc_ratio {
180 unsigned long cpu_freq;
181 unsigned long emc_freq;
182};
183
184static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
Dmitry Osipenkoea678da2019-11-05 00:56:03 +0300185 { 1400000, KHZ_MAX },
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100186 { 1200000, 750000 },
187 { 1100000, 600000 },
188 { 1000000, 500000 },
189 { 800000, 375000 },
190 { 500000, 200000 },
191 { 250000, 100000 },
192};
193
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100194static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
195{
196 return readl(tegra->regs + offset);
197}
198
199static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
200{
201 writel(val, tegra->regs + offset);
202}
203
204static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
205{
206 return readl(dev->regs + offset);
207}
208
209static void device_writel(struct tegra_devfreq_device *dev, u32 val,
210 u32 offset)
211{
212 writel(val, dev->regs + offset);
213}
214
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100215static unsigned long do_percent(unsigned long val, unsigned int pct)
216{
217 return val * pct / 100;
218}
219
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100220static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
221 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100222{
223 u32 avg = dev->avg_count;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100224 u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
225 u32 band = avg_band_freq * ACTMON_SAMPLING_PERIOD;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100226
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100227 device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
228
229 avg = max(dev->avg_count, band);
230 device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100231}
232
233static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
234 struct tegra_devfreq_device *dev)
235{
236 u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
237
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100238 device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
239 ACTMON_DEV_UPPER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100240
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100241 device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
242 ACTMON_DEV_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100243}
244
245static void actmon_write_barrier(struct tegra_devfreq *tegra)
246{
247 /* ensure the update has reached the ACTMON */
248 wmb();
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100249 actmon_readl(tegra, ACTMON_GLB_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100250}
251
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100252static void actmon_isr_device(struct tegra_devfreq *tegra,
253 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100254{
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100255 unsigned long flags;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100256 u32 intr_status, dev_ctrl;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100257
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100258 spin_lock_irqsave(&dev->lock, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100259
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100260 dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
261 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100262
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100263 intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
264 dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100265
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100266 if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100267 /*
268 * new_boost = min(old_boost * up_coef + step, max_freq)
269 */
270 dev->boost_freq = do_percent(dev->boost_freq,
271 dev->config->boost_up_coeff);
272 dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100273
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100274 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
275
276 if (dev->boost_freq >= tegra->max_freq)
277 dev->boost_freq = tegra->max_freq;
278 else
279 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
280 } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100281 /*
282 * new_boost = old_boost * down_coef
283 * or 0 if (old_boost * down_coef < step / 2)
284 */
285 dev->boost_freq = do_percent(dev->boost_freq,
286 dev->config->boost_down_coeff);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100287
288 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
289
290 if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1))
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100291 dev->boost_freq = 0;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100292 else
293 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100294 }
295
296 if (dev->config->avg_dependency_threshold) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100297 if (dev->avg_count >= dev->config->avg_dependency_threshold)
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100298 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100299 else if (dev->boost_freq == 0)
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100300 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100301 }
302
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100303 device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
304
305 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100306
307 actmon_write_barrier(tegra);
308
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100309 spin_unlock_irqrestore(&dev->lock, flags);
310}
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100311
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100312static irqreturn_t actmon_isr(int irq, void *data)
313{
314 struct tegra_devfreq *tegra = data;
315 bool handled = false;
316 unsigned int i;
317 u32 val;
318
319 val = actmon_readl(tegra, ACTMON_GLB_STATUS);
320 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
321 if (val & tegra->devices[i].config->irq_mask) {
322 actmon_isr_device(tegra, tegra->devices + i);
323 handled = true;
324 }
325 }
326
327 return handled ? IRQ_WAKE_THREAD : IRQ_NONE;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100328}
329
330static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
331 unsigned long cpu_freq)
332{
333 unsigned int i;
334 struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
335
336 for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
337 if (cpu_freq >= ratio->cpu_freq) {
338 if (ratio->emc_freq >= tegra->max_freq)
339 return tegra->max_freq;
340 else
341 return ratio->emc_freq;
342 }
343 }
344
345 return 0;
346}
347
348static void actmon_update_target(struct tegra_devfreq *tegra,
349 struct tegra_devfreq_device *dev)
350{
351 unsigned long cpu_freq = 0;
352 unsigned long static_cpu_emc_freq = 0;
353 unsigned int avg_sustain_coef;
354 unsigned long flags;
355
356 if (dev->config->avg_dependency_threshold) {
357 cpu_freq = cpufreq_get(0);
358 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
359 }
360
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100361 spin_lock_irqsave(&dev->lock, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100362
363 dev->target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD;
364 avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
365 dev->target_freq = do_percent(dev->target_freq, avg_sustain_coef);
366 dev->target_freq += dev->boost_freq;
367
368 if (dev->avg_count >= dev->config->avg_dependency_threshold)
369 dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
370
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100371 spin_unlock_irqrestore(&dev->lock, flags);
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;
377
378 mutex_lock(&tegra->devfreq->lock);
379 update_devfreq(tegra->devfreq);
380 mutex_unlock(&tegra->devfreq->lock);
381
382 return IRQ_HANDLED;
383}
384
385static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
386 unsigned long action, void *ptr)
387{
388 struct clk_notifier_data *data = ptr;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100389 struct tegra_devfreq *tegra;
390 struct tegra_devfreq_device *dev;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100391 unsigned int i;
392 unsigned long flags;
393
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100394 if (action != POST_RATE_CHANGE)
395 return NOTIFY_OK;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100396
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100397 tegra = container_of(nb, struct tegra_devfreq, rate_change_nb);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100398
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100399 tegra->cur_freq = data->new_rate / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100400
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100401 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
402 dev = &tegra->devices[i];
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100403
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100404 spin_lock_irqsave(&dev->lock, flags);
405 tegra_devfreq_update_wmark(tegra, dev);
406 spin_unlock_irqrestore(&dev->lock, flags);
407 }
408
409 actmon_write_barrier(tegra);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100410
411 return NOTIFY_OK;
412}
413
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100414static void tegra_actmon_enable_interrupts(struct tegra_devfreq *tegra)
415{
416 struct tegra_devfreq_device *dev;
417 u32 val;
418 unsigned int i;
419
420 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
421 dev = &tegra->devices[i];
422
423 val = device_readl(dev, ACTMON_DEV_CTRL);
424 val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
425 val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
426 val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
427 val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
428
429 device_writel(dev, val, ACTMON_DEV_CTRL);
430 }
431
432 actmon_write_barrier(tegra);
433}
434
435static void tegra_actmon_disable_interrupts(struct tegra_devfreq *tegra)
436{
437 struct tegra_devfreq_device *dev;
438 u32 val;
439 unsigned int i;
440
441 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
442 dev = &tegra->devices[i];
443
444 val = device_readl(dev, ACTMON_DEV_CTRL);
445 val &= ~ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
446 val &= ~ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
447 val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
448 val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
449
450 device_writel(dev, val, ACTMON_DEV_CTRL);
451 }
452
453 actmon_write_barrier(tegra);
454}
455
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100456static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
457 struct tegra_devfreq_device *dev)
458{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100459 u32 val = 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100460
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100461 dev->target_freq = tegra->cur_freq;
462
463 dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100464 device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100465
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100466 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100467 tegra_devfreq_update_wmark(tegra, dev);
468
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100469 device_writel(dev, ACTMON_COUNT_WEIGHT, ACTMON_DEV_COUNT_WEIGHT);
470 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100471
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100472 val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100473 val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
474 << ACTMON_DEV_CTRL_K_VAL_SHIFT;
475 val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
476 << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
477 val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
478 << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100479 val |= ACTMON_DEV_CTRL_ENB;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100480
481 device_writel(dev, val, ACTMON_DEV_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100482
483 actmon_write_barrier(tegra);
484}
485
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100486static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
487 u32 flags)
488{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100489 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100490 struct dev_pm_opp *opp;
Dmitry Osipenko42b888f2019-05-02 02:38:00 +0300491 unsigned long rate;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100492
Dmitry Osipenko42b888f2019-05-02 02:38:00 +0300493 opp = devfreq_recommended_opp(dev, freq, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100494 if (IS_ERR(opp)) {
Dmitry Osipenko42b888f2019-05-02 02:38:00 +0300495 dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100496 return PTR_ERR(opp);
497 }
498 rate = dev_pm_opp_get_freq(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530499 dev_pm_opp_put(opp);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100500
Tomeu Vizosoc70eea72015-03-17 10:36:14 +0100501 clk_set_min_rate(tegra->emc_clock, rate);
502 clk_set_rate(tegra->emc_clock, 0);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100503
504 return 0;
505}
506
507static int tegra_devfreq_get_dev_status(struct device *dev,
508 struct devfreq_dev_status *stat)
509{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100510 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100511 struct tegra_devfreq_device *actmon_dev;
512
Dmitry Osipenko42b888f2019-05-02 02:38:00 +0300513 stat->current_frequency = tegra->cur_freq * KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100514
515 /* To be used by the tegra governor */
516 stat->private_data = tegra;
517
518 /* The below are to be used by the other governors */
519
520 actmon_dev = &tegra->devices[MCALL];
521
522 /* Number of cycles spent on memory access */
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100523 stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100524
525 /* The bus can be considered to be saturated way before 100% */
526 stat->busy_time *= 100 / BUS_SATURATION_RATIO;
527
528 /* Number of cycles in a sampling period */
529 stat->total_time = ACTMON_SAMPLING_PERIOD * tegra->cur_freq;
530
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100531 stat->busy_time = min(stat->busy_time, stat->total_time);
532
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100533 return 0;
534}
535
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100536static struct devfreq_dev_profile tegra_devfreq_profile = {
537 .polling_ms = 0,
538 .target = tegra_devfreq_target,
539 .get_dev_status = tegra_devfreq_get_dev_status,
540};
541
542static int tegra_governor_get_target(struct devfreq *devfreq,
543 unsigned long *freq)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100544{
MyungJoo Ham14de3902015-08-18 13:47:41 +0900545 struct devfreq_dev_status *stat;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100546 struct tegra_devfreq *tegra;
547 struct tegra_devfreq_device *dev;
548 unsigned long target_freq = 0;
549 unsigned int i;
550 int err;
551
MyungJoo Ham14de3902015-08-18 13:47:41 +0900552 err = devfreq_update_stats(devfreq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100553 if (err)
554 return err;
555
MyungJoo Ham14de3902015-08-18 13:47:41 +0900556 stat = &devfreq->last_status;
557
558 tegra = stat->private_data;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100559
560 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
561 dev = &tegra->devices[i];
562
563 actmon_update_target(tegra, dev);
564
565 target_freq = max(target_freq, dev->target_freq);
566 }
567
Dmitry Osipenko42b888f2019-05-02 02:38:00 +0300568 *freq = target_freq * KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100569
570 return 0;
571}
572
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100573static int tegra_governor_event_handler(struct devfreq *devfreq,
574 unsigned int event, void *data)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100575{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100576 struct tegra_devfreq *tegra;
577 int ret = 0;
578
579 tegra = dev_get_drvdata(devfreq->dev.parent);
580
581 switch (event) {
582 case DEVFREQ_GOV_START:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100583 devfreq_monitor_start(devfreq);
Tomeu Vizoso34ed5042015-03-17 10:36:17 +0100584 tegra_actmon_enable_interrupts(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100585 break;
586
587 case DEVFREQ_GOV_STOP:
588 tegra_actmon_disable_interrupts(tegra);
589 devfreq_monitor_stop(devfreq);
590 break;
591
592 case DEVFREQ_GOV_SUSPEND:
593 tegra_actmon_disable_interrupts(tegra);
594 devfreq_monitor_suspend(devfreq);
595 break;
596
597 case DEVFREQ_GOV_RESUME:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100598 devfreq_monitor_resume(devfreq);
Tomeu Vizoso34ed5042015-03-17 10:36:17 +0100599 tegra_actmon_enable_interrupts(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100600 break;
601 }
602
603 return ret;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100604}
605
606static struct devfreq_governor tegra_devfreq_governor = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100607 .name = "tegra_actmon",
608 .get_target_freq = tegra_governor_get_target,
609 .event_handler = tegra_governor_event_handler,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100610};
611
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100612static int tegra_devfreq_probe(struct platform_device *pdev)
613{
614 struct tegra_devfreq *tegra;
615 struct tegra_devfreq_device *dev;
616 struct resource *res;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100617 unsigned int i;
Tomeu Vizoso5d498b42015-03-17 10:36:15 +0100618 unsigned long rate;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100619 int irq;
620 int err;
621
622 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
623 if (!tegra)
624 return -ENOMEM;
625
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100626 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100627
628 tegra->regs = devm_ioremap_resource(&pdev->dev, res);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100629 if (IS_ERR(tegra->regs))
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100630 return PTR_ERR(tegra->regs);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100631
632 tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
633 if (IS_ERR(tegra->reset)) {
634 dev_err(&pdev->dev, "Failed to get reset\n");
635 return PTR_ERR(tegra->reset);
636 }
637
638 tegra->clock = devm_clk_get(&pdev->dev, "actmon");
639 if (IS_ERR(tegra->clock)) {
640 dev_err(&pdev->dev, "Failed to get actmon clock\n");
641 return PTR_ERR(tegra->clock);
642 }
643
644 tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
645 if (IS_ERR(tegra->emc_clock)) {
646 dev_err(&pdev->dev, "Failed to get emc clock\n");
647 return PTR_ERR(tegra->emc_clock);
648 }
649
Tomeu Vizosoc70eea72015-03-17 10:36:14 +0100650 clk_set_rate(tegra->emc_clock, ULONG_MAX);
651
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100652 tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb;
653 err = clk_notifier_register(tegra->emc_clock, &tegra->rate_change_nb);
654 if (err) {
655 dev_err(&pdev->dev,
656 "Failed to register rate change notifier\n");
657 return err;
658 }
659
660 reset_control_assert(tegra->reset);
661
662 err = clk_prepare_enable(tegra->clock);
663 if (err) {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100664 dev_err(&pdev->dev,
665 "Failed to prepare and enable ACTMON clock\n");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100666 return err;
667 }
668
669 reset_control_deassert(tegra->reset);
670
Tomeu Vizosoc70eea72015-03-17 10:36:14 +0100671 tegra->max_freq = clk_round_rate(tegra->emc_clock, ULONG_MAX) / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100672 tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
673
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100674 actmon_writel(tegra, ACTMON_SAMPLING_PERIOD - 1,
675 ACTMON_GLB_PERIOD_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100676
677 for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
678 dev = tegra->devices + i;
679 dev->config = actmon_device_configs + i;
680 dev->regs = tegra->regs + dev->config->offset;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100681 spin_lock_init(&dev->lock);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100682
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100683 tegra_actmon_configure_device(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100684 }
685
Tomeu Vizoso5d498b42015-03-17 10:36:15 +0100686 for (rate = 0; rate <= tegra->max_freq * KHZ; rate++) {
687 rate = clk_round_rate(tegra->emc_clock, rate);
688 dev_pm_opp_add(&pdev->dev, rate, 0);
689 }
690
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100691 irq = platform_get_irq(pdev, 0);
Gustavo A. R. Silva9e578b32017-07-03 07:47:38 -0500692 if (irq < 0) {
693 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
694 return irq;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100695 }
696
Tomeu Vizoso2da19b12015-03-17 10:36:16 +0100697 platform_set_drvdata(pdev, tegra);
698
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100699 err = devm_request_threaded_irq(&pdev->dev, irq, actmon_isr,
700 actmon_thread_isr, IRQF_SHARED,
701 "tegra-devfreq", tegra);
702 if (err) {
703 dev_err(&pdev->dev, "Interrupt request failed\n");
704 return err;
705 }
706
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100707 tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
708 tegra->devfreq = devm_devfreq_add_device(&pdev->dev,
709 &tegra_devfreq_profile,
710 "tegra_actmon",
711 NULL);
712
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100713 return 0;
714}
715
716static int tegra_devfreq_remove(struct platform_device *pdev)
717{
718 struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100719 int irq = platform_get_irq(pdev, 0);
720 u32 val;
721 unsigned int i;
722
723 for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
724 val = device_readl(&tegra->devices[i], ACTMON_DEV_CTRL);
725 val &= ~ACTMON_DEV_CTRL_ENB;
726 device_writel(&tegra->devices[i], val, ACTMON_DEV_CTRL);
727 }
728
729 actmon_write_barrier(tegra);
730
731 devm_free_irq(&pdev->dev, irq, tegra);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100732
733 clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb);
734
735 clk_disable_unprepare(tegra->clock);
736
737 return 0;
738}
739
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100740static const struct of_device_id tegra_devfreq_of_match[] = {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100741 { .compatible = "nvidia,tegra124-actmon" },
742 { },
743};
744
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100745MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
746
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100747static struct platform_driver tegra_devfreq_driver = {
748 .probe = tegra_devfreq_probe,
749 .remove = tegra_devfreq_remove,
750 .driver = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100751 .name = "tegra-devfreq",
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100752 .of_match_table = tegra_devfreq_of_match,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100753 },
754};
Tomeu Vizoso358b6152015-03-30 17:33:23 +0200755
756static int __init tegra_devfreq_init(void)
757{
758 int ret = 0;
759
760 ret = devfreq_add_governor(&tegra_devfreq_governor);
761 if (ret) {
762 pr_err("%s: failed to add governor: %d\n", __func__, ret);
763 return ret;
764 }
765
766 ret = platform_driver_register(&tegra_devfreq_driver);
767 if (ret)
768 devfreq_remove_governor(&tegra_devfreq_governor);
769
770 return ret;
771}
772module_init(tegra_devfreq_init)
773
774static void __exit tegra_devfreq_exit(void)
775{
776 int ret = 0;
777
778 platform_driver_unregister(&tegra_devfreq_driver);
779
780 ret = devfreq_remove_governor(&tegra_devfreq_governor);
781 if (ret)
782 pr_err("%s: failed to remove governor: %d\n", __func__, ret);
783}
784module_exit(tegra_devfreq_exit)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100785
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100786MODULE_LICENSE("GPL v2");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100787MODULE_DESCRIPTION("Tegra devfreq driver");
788MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");