blob: 35c38aad8b4f4eac44fb24477d526c3b75b82d9b [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>
14#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070015#include <linux/mod_devicetable.h>
Tomeu Vizoso6234f382014-11-24 13:28:17 +010016#include <linux/platform_device.h>
17#include <linux/pm_opp.h>
18#include <linux/reset.h>
19
20#include "governor.h"
21
22#define ACTMON_GLB_STATUS 0x0
23#define ACTMON_GLB_PERIOD_CTRL 0x4
24
25#define ACTMON_DEV_CTRL 0x0
26#define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
27#define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
28#define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
29#define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
30#define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
31#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
32#define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
33#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
34#define ACTMON_DEV_CTRL_ENB BIT(31)
35
36#define ACTMON_DEV_UPPER_WMARK 0x4
37#define ACTMON_DEV_LOWER_WMARK 0x8
38#define ACTMON_DEV_INIT_AVG 0xc
39#define ACTMON_DEV_AVG_UPPER_WMARK 0x10
40#define ACTMON_DEV_AVG_LOWER_WMARK 0x14
41#define ACTMON_DEV_COUNT_WEIGHT 0x18
42#define ACTMON_DEV_AVG_COUNT 0x20
43#define ACTMON_DEV_INTR_STATUS 0x24
44
45#define ACTMON_INTR_STATUS_CLEAR 0xffffffff
46
47#define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
48#define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
49
50#define ACTMON_ABOVE_WMARK_WINDOW 1
51#define ACTMON_BELOW_WMARK_WINDOW 3
52#define ACTMON_BOOST_FREQ_STEP 16000
53
Tomeu Vizoso11573e92015-03-17 10:36:12 +010054/*
55 * Activity counter is incremented every 256 memory transactions, and each
Tomeu Vizoso6234f382014-11-24 13:28:17 +010056 * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is
57 * 4 * 256 = 1024.
58 */
59#define ACTMON_COUNT_WEIGHT 0x400
60
61/*
62 * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
63 * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
64 */
65#define ACTMON_AVERAGE_WINDOW_LOG2 6
66#define ACTMON_SAMPLING_PERIOD 12 /* ms */
67#define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
68
69#define KHZ 1000
70
71/* Assume that the bus is saturated if the utilization is 25% */
72#define BUS_SATURATION_RATIO 25
73
74/**
75 * struct tegra_devfreq_device_config - configuration specific to an ACTMON
76 * device
77 *
Tomeu Vizoso11573e92015-03-17 10:36:12 +010078 * Coefficients and thresholds are percentages unless otherwise noted
Tomeu Vizoso6234f382014-11-24 13:28:17 +010079 */
80struct tegra_devfreq_device_config {
81 u32 offset;
82 u32 irq_mask;
83
Tomeu Vizoso11573e92015-03-17 10:36:12 +010084 /* Factors applied to boost_freq every consecutive watermark breach */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010085 unsigned int boost_up_coeff;
86 unsigned int boost_down_coeff;
Tomeu Vizoso11573e92015-03-17 10:36:12 +010087
88 /* Define the watermark bounds when applied to the current avg */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010089 unsigned int boost_up_threshold;
90 unsigned int boost_down_threshold;
Tomeu Vizoso11573e92015-03-17 10:36:12 +010091
92 /*
93 * Threshold of activity (cycles) below which the CPU frequency isn't
94 * to be taken into account. This is to avoid increasing the EMC
95 * frequency when the CPU is very busy but not accessing the bus often.
96 */
Tomeu Vizoso6234f382014-11-24 13:28:17 +010097 u32 avg_dependency_threshold;
98};
99
100enum tegra_actmon_device {
101 MCALL = 0,
102 MCCPU,
103};
104
105static struct tegra_devfreq_device_config actmon_device_configs[] = {
106 {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100107 /* MCALL: All memory accesses (including from the CPUs) */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100108 .offset = 0x1c0,
109 .irq_mask = 1 << 26,
110 .boost_up_coeff = 200,
111 .boost_down_coeff = 50,
112 .boost_up_threshold = 60,
113 .boost_down_threshold = 40,
114 },
115 {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100116 /* MCCPU: memory accesses from the CPUs */
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100117 .offset = 0x200,
118 .irq_mask = 1 << 25,
119 .boost_up_coeff = 800,
120 .boost_down_coeff = 90,
121 .boost_up_threshold = 27,
122 .boost_down_threshold = 10,
123 .avg_dependency_threshold = 50000,
124 },
125};
126
127/**
128 * struct tegra_devfreq_device - state specific to an ACTMON device
129 *
130 * Frequencies are in kHz.
131 */
132struct tegra_devfreq_device {
133 const struct tegra_devfreq_device_config *config;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100134 void __iomem *regs;
135 spinlock_t lock;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100136
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100137 /* Average event count sampled in the last interrupt */
138 u32 avg_count;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100139
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100140 /*
141 * Extra frequency to increase the target by due to consecutive
142 * watermark breaches.
143 */
144 unsigned long boost_freq;
145
146 /* Optimal frequency calculated from the stats for this device */
147 unsigned long target_freq;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100148};
149
150struct tegra_devfreq {
151 struct devfreq *devfreq;
152
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100153 struct reset_control *reset;
154 struct clk *clock;
155 void __iomem *regs;
156
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100157 struct clk *emc_clock;
158 unsigned long max_freq;
159 unsigned long cur_freq;
160 struct notifier_block rate_change_nb;
161
162 struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)];
163};
164
165struct tegra_actmon_emc_ratio {
166 unsigned long cpu_freq;
167 unsigned long emc_freq;
168};
169
170static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
171 { 1400000, ULONG_MAX },
172 { 1200000, 750000 },
173 { 1100000, 600000 },
174 { 1000000, 500000 },
175 { 800000, 375000 },
176 { 500000, 200000 },
177 { 250000, 100000 },
178};
179
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100180static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
181{
182 return readl(tegra->regs + offset);
183}
184
185static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
186{
187 writel(val, tegra->regs + offset);
188}
189
190static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
191{
192 return readl(dev->regs + offset);
193}
194
195static void device_writel(struct tegra_devfreq_device *dev, u32 val,
196 u32 offset)
197{
198 writel(val, dev->regs + offset);
199}
200
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100201static unsigned long do_percent(unsigned long val, unsigned int pct)
202{
203 return val * pct / 100;
204}
205
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100206static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
207 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100208{
209 u32 avg = dev->avg_count;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100210 u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
211 u32 band = avg_band_freq * ACTMON_SAMPLING_PERIOD;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100212
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100213 device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
214
215 avg = max(dev->avg_count, band);
216 device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100217}
218
219static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
220 struct tegra_devfreq_device *dev)
221{
222 u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
223
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100224 device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
225 ACTMON_DEV_UPPER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100226
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100227 device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
228 ACTMON_DEV_LOWER_WMARK);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100229}
230
231static void actmon_write_barrier(struct tegra_devfreq *tegra)
232{
233 /* ensure the update has reached the ACTMON */
234 wmb();
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100235 actmon_readl(tegra, ACTMON_GLB_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100236}
237
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100238static void actmon_isr_device(struct tegra_devfreq *tegra,
239 struct tegra_devfreq_device *dev)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100240{
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100241 unsigned long flags;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100242 u32 intr_status, dev_ctrl;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100243
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100244 spin_lock_irqsave(&dev->lock, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100245
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100246 dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
247 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100248
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100249 intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
250 dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100251
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100252 if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100253 /*
254 * new_boost = min(old_boost * up_coef + step, max_freq)
255 */
256 dev->boost_freq = do_percent(dev->boost_freq,
257 dev->config->boost_up_coeff);
258 dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100259
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100260 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
261
262 if (dev->boost_freq >= tegra->max_freq)
263 dev->boost_freq = tegra->max_freq;
264 else
265 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
266 } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100267 /*
268 * new_boost = old_boost * down_coef
269 * or 0 if (old_boost * down_coef < step / 2)
270 */
271 dev->boost_freq = do_percent(dev->boost_freq,
272 dev->config->boost_down_coeff);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100273
274 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
275
276 if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1))
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100277 dev->boost_freq = 0;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100278 else
279 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100280 }
281
282 if (dev->config->avg_dependency_threshold) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100283 if (dev->avg_count >= dev->config->avg_dependency_threshold)
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100284 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100285 else if (dev->boost_freq == 0)
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100286 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100287 }
288
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100289 device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
290
291 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100292
293 actmon_write_barrier(tegra);
294
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100295 spin_unlock_irqrestore(&dev->lock, flags);
296}
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100297
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100298static irqreturn_t actmon_isr(int irq, void *data)
299{
300 struct tegra_devfreq *tegra = data;
301 bool handled = false;
302 unsigned int i;
303 u32 val;
304
305 val = actmon_readl(tegra, ACTMON_GLB_STATUS);
306 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
307 if (val & tegra->devices[i].config->irq_mask) {
308 actmon_isr_device(tegra, tegra->devices + i);
309 handled = true;
310 }
311 }
312
313 return handled ? IRQ_WAKE_THREAD : IRQ_NONE;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100314}
315
316static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
317 unsigned long cpu_freq)
318{
319 unsigned int i;
320 struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
321
322 for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
323 if (cpu_freq >= ratio->cpu_freq) {
324 if (ratio->emc_freq >= tegra->max_freq)
325 return tegra->max_freq;
326 else
327 return ratio->emc_freq;
328 }
329 }
330
331 return 0;
332}
333
334static void actmon_update_target(struct tegra_devfreq *tegra,
335 struct tegra_devfreq_device *dev)
336{
337 unsigned long cpu_freq = 0;
338 unsigned long static_cpu_emc_freq = 0;
339 unsigned int avg_sustain_coef;
340 unsigned long flags;
341
342 if (dev->config->avg_dependency_threshold) {
343 cpu_freq = cpufreq_get(0);
344 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
345 }
346
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100347 spin_lock_irqsave(&dev->lock, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100348
349 dev->target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD;
350 avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
351 dev->target_freq = do_percent(dev->target_freq, avg_sustain_coef);
352 dev->target_freq += dev->boost_freq;
353
354 if (dev->avg_count >= dev->config->avg_dependency_threshold)
355 dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
356
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100357 spin_unlock_irqrestore(&dev->lock, flags);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100358}
359
360static irqreturn_t actmon_thread_isr(int irq, void *data)
361{
362 struct tegra_devfreq *tegra = data;
363
364 mutex_lock(&tegra->devfreq->lock);
365 update_devfreq(tegra->devfreq);
366 mutex_unlock(&tegra->devfreq->lock);
367
368 return IRQ_HANDLED;
369}
370
371static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
372 unsigned long action, void *ptr)
373{
374 struct clk_notifier_data *data = ptr;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100375 struct tegra_devfreq *tegra;
376 struct tegra_devfreq_device *dev;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100377 unsigned int i;
378 unsigned long flags;
379
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100380 if (action != POST_RATE_CHANGE)
381 return NOTIFY_OK;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100382
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100383 tegra = container_of(nb, struct tegra_devfreq, rate_change_nb);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100384
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100385 tegra->cur_freq = data->new_rate / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100386
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100387 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
388 dev = &tegra->devices[i];
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100389
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100390 spin_lock_irqsave(&dev->lock, flags);
391 tegra_devfreq_update_wmark(tegra, dev);
392 spin_unlock_irqrestore(&dev->lock, flags);
393 }
394
395 actmon_write_barrier(tegra);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100396
397 return NOTIFY_OK;
398}
399
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100400static void tegra_actmon_enable_interrupts(struct tegra_devfreq *tegra)
401{
402 struct tegra_devfreq_device *dev;
403 u32 val;
404 unsigned int i;
405
406 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
407 dev = &tegra->devices[i];
408
409 val = device_readl(dev, ACTMON_DEV_CTRL);
410 val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
411 val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
412 val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
413 val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
414
415 device_writel(dev, val, ACTMON_DEV_CTRL);
416 }
417
418 actmon_write_barrier(tegra);
419}
420
421static void tegra_actmon_disable_interrupts(struct tegra_devfreq *tegra)
422{
423 struct tegra_devfreq_device *dev;
424 u32 val;
425 unsigned int i;
426
427 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
428 dev = &tegra->devices[i];
429
430 val = device_readl(dev, ACTMON_DEV_CTRL);
431 val &= ~ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
432 val &= ~ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
433 val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
434 val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
435
436 device_writel(dev, val, ACTMON_DEV_CTRL);
437 }
438
439 actmon_write_barrier(tegra);
440}
441
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100442static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
443 struct tegra_devfreq_device *dev)
444{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100445 u32 val = 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100446
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100447 dev->target_freq = tegra->cur_freq;
448
449 dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100450 device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100451
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100452 tegra_devfreq_update_avg_wmark(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100453 tegra_devfreq_update_wmark(tegra, dev);
454
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100455 device_writel(dev, ACTMON_COUNT_WEIGHT, ACTMON_DEV_COUNT_WEIGHT);
456 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100457
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100458 val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100459 val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
460 << ACTMON_DEV_CTRL_K_VAL_SHIFT;
461 val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
462 << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
463 val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
464 << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100465 val |= ACTMON_DEV_CTRL_ENB;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100466
467 device_writel(dev, val, ACTMON_DEV_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100468
469 actmon_write_barrier(tegra);
470}
471
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100472static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
473 u32 flags)
474{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100475 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100476 struct dev_pm_opp *opp;
477 unsigned long rate = *freq * KHZ;
478
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100479 opp = devfreq_recommended_opp(dev, &rate, flags);
480 if (IS_ERR(opp)) {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100481 dev_err(dev, "Failed to find opp for %lu KHz\n", *freq);
482 return PTR_ERR(opp);
483 }
484 rate = dev_pm_opp_get_freq(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530485 dev_pm_opp_put(opp);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100486
Tomeu Vizosoc70eea72015-03-17 10:36:14 +0100487 clk_set_min_rate(tegra->emc_clock, rate);
488 clk_set_rate(tegra->emc_clock, 0);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100489
Tomeu Vizosodbb0c7c2016-01-21 08:52:26 +0100490 *freq = rate;
491
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100492 return 0;
493}
494
495static int tegra_devfreq_get_dev_status(struct device *dev,
496 struct devfreq_dev_status *stat)
497{
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100498 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100499 struct tegra_devfreq_device *actmon_dev;
500
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100501 stat->current_frequency = tegra->cur_freq;
502
503 /* To be used by the tegra governor */
504 stat->private_data = tegra;
505
506 /* The below are to be used by the other governors */
507
508 actmon_dev = &tegra->devices[MCALL];
509
510 /* Number of cycles spent on memory access */
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100511 stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100512
513 /* The bus can be considered to be saturated way before 100% */
514 stat->busy_time *= 100 / BUS_SATURATION_RATIO;
515
516 /* Number of cycles in a sampling period */
517 stat->total_time = ACTMON_SAMPLING_PERIOD * tegra->cur_freq;
518
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100519 stat->busy_time = min(stat->busy_time, stat->total_time);
520
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100521 return 0;
522}
523
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100524static struct devfreq_dev_profile tegra_devfreq_profile = {
525 .polling_ms = 0,
526 .target = tegra_devfreq_target,
527 .get_dev_status = tegra_devfreq_get_dev_status,
528};
529
530static int tegra_governor_get_target(struct devfreq *devfreq,
531 unsigned long *freq)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100532{
MyungJoo Ham14de3902015-08-18 13:47:41 +0900533 struct devfreq_dev_status *stat;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100534 struct tegra_devfreq *tegra;
535 struct tegra_devfreq_device *dev;
536 unsigned long target_freq = 0;
537 unsigned int i;
538 int err;
539
MyungJoo Ham14de3902015-08-18 13:47:41 +0900540 err = devfreq_update_stats(devfreq);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100541 if (err)
542 return err;
543
MyungJoo Ham14de3902015-08-18 13:47:41 +0900544 stat = &devfreq->last_status;
545
546 tegra = stat->private_data;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100547
548 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
549 dev = &tegra->devices[i];
550
551 actmon_update_target(tegra, dev);
552
553 target_freq = max(target_freq, dev->target_freq);
554 }
555
556 *freq = target_freq;
557
558 return 0;
559}
560
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100561static int tegra_governor_event_handler(struct devfreq *devfreq,
562 unsigned int event, void *data)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100563{
Yangtao Li1d1397c2019-02-16 10:18:26 -0500564 struct tegra_devfreq *tegra = dev_get_drvdata(devfreq->dev.parent);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100565
566 switch (event) {
567 case DEVFREQ_GOV_START:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100568 devfreq_monitor_start(devfreq);
Tomeu Vizoso34ed5042015-03-17 10:36:17 +0100569 tegra_actmon_enable_interrupts(tegra);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100570 break;
571
572 case DEVFREQ_GOV_STOP:
573 tegra_actmon_disable_interrupts(tegra);
574 devfreq_monitor_stop(devfreq);
575 break;
576
577 case DEVFREQ_GOV_SUSPEND:
578 tegra_actmon_disable_interrupts(tegra);
579 devfreq_monitor_suspend(devfreq);
580 break;
581
582 case DEVFREQ_GOV_RESUME:
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100583 devfreq_monitor_resume(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
Yangtao Li1d1397c2019-02-16 10:18:26 -0500588 return 0;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100589}
590
591static struct devfreq_governor tegra_devfreq_governor = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100592 .name = "tegra_actmon",
593 .get_target_freq = tegra_governor_get_target,
594 .event_handler = tegra_governor_event_handler,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100595};
596
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100597static int tegra_devfreq_probe(struct platform_device *pdev)
598{
599 struct tegra_devfreq *tegra;
600 struct tegra_devfreq_device *dev;
601 struct resource *res;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100602 unsigned int i;
Tomeu Vizoso5d498b42015-03-17 10:36:15 +0100603 unsigned long rate;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100604 int irq;
605 int err;
606
607 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
608 if (!tegra)
609 return -ENOMEM;
610
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100611 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100612
613 tegra->regs = devm_ioremap_resource(&pdev->dev, res);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100614 if (IS_ERR(tegra->regs))
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100615 return PTR_ERR(tegra->regs);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100616
617 tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
618 if (IS_ERR(tegra->reset)) {
619 dev_err(&pdev->dev, "Failed to get reset\n");
620 return PTR_ERR(tegra->reset);
621 }
622
623 tegra->clock = devm_clk_get(&pdev->dev, "actmon");
624 if (IS_ERR(tegra->clock)) {
625 dev_err(&pdev->dev, "Failed to get actmon clock\n");
626 return PTR_ERR(tegra->clock);
627 }
628
629 tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
630 if (IS_ERR(tegra->emc_clock)) {
631 dev_err(&pdev->dev, "Failed to get emc clock\n");
632 return PTR_ERR(tegra->emc_clock);
633 }
634
Tomeu Vizosoc70eea72015-03-17 10:36:14 +0100635 clk_set_rate(tegra->emc_clock, ULONG_MAX);
636
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100637 tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb;
638 err = clk_notifier_register(tegra->emc_clock, &tegra->rate_change_nb);
639 if (err) {
640 dev_err(&pdev->dev,
641 "Failed to register rate change notifier\n");
642 return err;
643 }
644
645 reset_control_assert(tegra->reset);
646
647 err = clk_prepare_enable(tegra->clock);
648 if (err) {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100649 dev_err(&pdev->dev,
650 "Failed to prepare and enable ACTMON clock\n");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100651 return err;
652 }
653
654 reset_control_deassert(tegra->reset);
655
Tomeu Vizosoc70eea72015-03-17 10:36:14 +0100656 tegra->max_freq = clk_round_rate(tegra->emc_clock, ULONG_MAX) / KHZ;
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100657 tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
658
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100659 actmon_writel(tegra, ACTMON_SAMPLING_PERIOD - 1,
660 ACTMON_GLB_PERIOD_CTRL);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100661
662 for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
663 dev = tegra->devices + i;
664 dev->config = actmon_device_configs + i;
665 dev->regs = tegra->regs + dev->config->offset;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100666 spin_lock_init(&dev->lock);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100667
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100668 tegra_actmon_configure_device(tegra, dev);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100669 }
670
Tomeu Vizoso5d498b42015-03-17 10:36:15 +0100671 for (rate = 0; rate <= tegra->max_freq * KHZ; rate++) {
672 rate = clk_round_rate(tegra->emc_clock, rate);
673 dev_pm_opp_add(&pdev->dev, rate, 0);
674 }
675
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100676 irq = platform_get_irq(pdev, 0);
Gustavo A. R. Silva9e578b32017-07-03 07:47:38 -0500677 if (irq < 0) {
678 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
679 return irq;
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100680 }
681
Tomeu Vizoso2da19b12015-03-17 10:36:16 +0100682 platform_set_drvdata(pdev, tegra);
683
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100684 err = devm_request_threaded_irq(&pdev->dev, irq, actmon_isr,
685 actmon_thread_isr, IRQF_SHARED,
686 "tegra-devfreq", tegra);
687 if (err) {
688 dev_err(&pdev->dev, "Interrupt request failed\n");
689 return err;
690 }
691
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100692 tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
693 tegra->devfreq = devm_devfreq_add_device(&pdev->dev,
694 &tegra_devfreq_profile,
695 "tegra_actmon",
696 NULL);
697
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100698 return 0;
699}
700
701static int tegra_devfreq_remove(struct platform_device *pdev)
702{
703 struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100704 int irq = platform_get_irq(pdev, 0);
705 u32 val;
706 unsigned int i;
707
708 for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
709 val = device_readl(&tegra->devices[i], ACTMON_DEV_CTRL);
710 val &= ~ACTMON_DEV_CTRL_ENB;
711 device_writel(&tegra->devices[i], val, ACTMON_DEV_CTRL);
712 }
713
714 actmon_write_barrier(tegra);
715
716 devm_free_irq(&pdev->dev, irq, tegra);
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100717
718 clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb);
719
720 clk_disable_unprepare(tegra->clock);
721
722 return 0;
723}
724
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100725static const struct of_device_id tegra_devfreq_of_match[] = {
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100726 { .compatible = "nvidia,tegra124-actmon" },
727 { },
728};
729
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100730MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
731
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100732static struct platform_driver tegra_devfreq_driver = {
733 .probe = tegra_devfreq_probe,
734 .remove = tegra_devfreq_remove,
735 .driver = {
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100736 .name = "tegra-devfreq",
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100737 .of_match_table = tegra_devfreq_of_match,
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100738 },
739};
Tomeu Vizoso358b6152015-03-30 17:33:23 +0200740
741static int __init tegra_devfreq_init(void)
742{
743 int ret = 0;
744
745 ret = devfreq_add_governor(&tegra_devfreq_governor);
746 if (ret) {
747 pr_err("%s: failed to add governor: %d\n", __func__, ret);
748 return ret;
749 }
750
751 ret = platform_driver_register(&tegra_devfreq_driver);
752 if (ret)
753 devfreq_remove_governor(&tegra_devfreq_governor);
754
755 return ret;
756}
757module_init(tegra_devfreq_init)
758
759static void __exit tegra_devfreq_exit(void)
760{
761 int ret = 0;
762
763 platform_driver_unregister(&tegra_devfreq_driver);
764
765 ret = devfreq_remove_governor(&tegra_devfreq_governor);
766 if (ret)
767 pr_err("%s: failed to remove governor: %d\n", __func__, ret);
768}
769module_exit(tegra_devfreq_exit)
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100770
Tomeu Vizoso11573e92015-03-17 10:36:12 +0100771MODULE_LICENSE("GPL v2");
Tomeu Vizoso6234f382014-11-24 13:28:17 +0100772MODULE_DESCRIPTION("Tegra devfreq driver");
773MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");