1 // SPDX-License-Identifier: GPL-2.0-only
3 * A devfreq driver for NVIDIA Tegra SoCs
5 * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
6 * Copyright (C) 2014 Google, Inc
10 #include <linux/cpufreq.h>
11 #include <linux/devfreq.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_opp.h>
18 #include <linux/reset.h>
22 #define ACTMON_GLB_STATUS 0x0
23 #define ACTMON_GLB_PERIOD_CTRL 0x4
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)
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
45 #define ACTMON_INTR_STATUS_CLEAR 0xffffffff
47 #define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
48 #define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
50 #define ACTMON_ABOVE_WMARK_WINDOW 1
51 #define ACTMON_BELOW_WMARK_WINDOW 3
52 #define ACTMON_BOOST_FREQ_STEP 16000
55 * Activity counter is incremented every 256 memory transactions, and each
56 * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is
59 #define ACTMON_COUNT_WEIGHT 0x400
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
65 #define ACTMON_AVERAGE_WINDOW_LOG2 6
66 #define ACTMON_SAMPLING_PERIOD 12 /* ms */
67 #define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
71 #define KHZ_MAX (ULONG_MAX / KHZ)
73 /* Assume that the bus is saturated if the utilization is 25% */
74 #define BUS_SATURATION_RATIO 25
77 * struct tegra_devfreq_device_config - configuration specific to an ACTMON
80 * Coefficients and thresholds are percentages unless otherwise noted
82 struct tegra_devfreq_device_config {
86 /* Factors applied to boost_freq every consecutive watermark breach */
87 unsigned int boost_up_coeff;
88 unsigned int boost_down_coeff;
90 /* Define the watermark bounds when applied to the current avg */
91 unsigned int boost_up_threshold;
92 unsigned int boost_down_threshold;
95 * Threshold of activity (cycles) below which the CPU frequency isn't
96 * to be taken into account. This is to avoid increasing the EMC
97 * frequency when the CPU is very busy but not accessing the bus often.
99 u32 avg_dependency_threshold;
102 enum tegra_actmon_device {
107 static struct tegra_devfreq_device_config actmon_device_configs[] = {
109 /* MCALL: All memory accesses (including from the CPUs) */
112 .boost_up_coeff = 200,
113 .boost_down_coeff = 50,
114 .boost_up_threshold = 60,
115 .boost_down_threshold = 40,
118 /* MCCPU: memory accesses from the CPUs */
121 .boost_up_coeff = 800,
122 .boost_down_coeff = 90,
123 .boost_up_threshold = 27,
124 .boost_down_threshold = 10,
125 .avg_dependency_threshold = 50000,
130 * struct tegra_devfreq_device - state specific to an ACTMON device
132 * Frequencies are in kHz.
134 struct tegra_devfreq_device {
135 const struct tegra_devfreq_device_config *config;
138 /* Average event count sampled in the last interrupt */
142 * Extra frequency to increase the target by due to consecutive
143 * watermark breaches.
145 unsigned long boost_freq;
147 /* Optimal frequency calculated from the stats for this device */
148 unsigned long target_freq;
151 struct tegra_devfreq {
152 struct devfreq *devfreq;
154 struct reset_control *reset;
158 struct clk *emc_clock;
159 unsigned long max_freq;
160 unsigned long cur_freq;
161 struct notifier_block rate_change_nb;
163 struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)];
168 struct tegra_actmon_emc_ratio {
169 unsigned long cpu_freq;
170 unsigned long emc_freq;
173 static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
174 { 1400000, KHZ_MAX },
183 static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
185 return readl_relaxed(tegra->regs + offset);
188 static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
190 writel_relaxed(val, tegra->regs + offset);
193 static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
195 return readl_relaxed(dev->regs + offset);
198 static void device_writel(struct tegra_devfreq_device *dev, u32 val,
201 writel_relaxed(val, dev->regs + offset);
204 static unsigned long do_percent(unsigned long val, unsigned int pct)
206 return val * pct / 100;
209 static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
210 struct tegra_devfreq_device *dev)
212 u32 avg = dev->avg_count;
213 u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
214 u32 band = avg_band_freq * ACTMON_SAMPLING_PERIOD;
216 device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
218 avg = max(dev->avg_count, band);
219 device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
222 static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
223 struct tegra_devfreq_device *dev)
225 u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
227 device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
228 ACTMON_DEV_UPPER_WMARK);
230 device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
231 ACTMON_DEV_LOWER_WMARK);
234 static void actmon_write_barrier(struct tegra_devfreq *tegra)
236 /* ensure the update has reached the ACTMON */
237 readl(tegra->regs + ACTMON_GLB_STATUS);
240 static void actmon_isr_device(struct tegra_devfreq *tegra,
241 struct tegra_devfreq_device *dev)
243 u32 intr_status, dev_ctrl;
245 dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
246 tegra_devfreq_update_avg_wmark(tegra, dev);
248 intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
249 dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
251 if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
253 * new_boost = min(old_boost * up_coef + step, max_freq)
255 dev->boost_freq = do_percent(dev->boost_freq,
256 dev->config->boost_up_coeff);
257 dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
259 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
261 if (dev->boost_freq >= tegra->max_freq)
262 dev->boost_freq = tegra->max_freq;
264 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
265 } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
267 * new_boost = old_boost * down_coef
268 * or 0 if (old_boost * down_coef < step / 2)
270 dev->boost_freq = do_percent(dev->boost_freq,
271 dev->config->boost_down_coeff);
273 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
275 if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1))
278 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
281 if (dev->config->avg_dependency_threshold) {
282 if (dev->avg_count >= dev->config->avg_dependency_threshold)
283 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
284 else if (dev->boost_freq == 0)
285 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
288 device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
290 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
292 actmon_write_barrier(tegra);
295 static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
296 unsigned long cpu_freq)
299 struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
301 for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
302 if (cpu_freq >= ratio->cpu_freq) {
303 if (ratio->emc_freq >= tegra->max_freq)
304 return tegra->max_freq;
306 return ratio->emc_freq;
313 static void actmon_update_target(struct tegra_devfreq *tegra,
314 struct tegra_devfreq_device *dev)
316 unsigned long cpu_freq = 0;
317 unsigned long static_cpu_emc_freq = 0;
318 unsigned int avg_sustain_coef;
320 if (dev->config->avg_dependency_threshold) {
321 cpu_freq = cpufreq_get(0);
322 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
325 dev->target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD;
326 avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
327 dev->target_freq = do_percent(dev->target_freq, avg_sustain_coef);
328 dev->target_freq += dev->boost_freq;
330 if (dev->avg_count >= dev->config->avg_dependency_threshold)
331 dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
334 static irqreturn_t actmon_thread_isr(int irq, void *data)
336 struct tegra_devfreq *tegra = data;
337 bool handled = false;
341 mutex_lock(&tegra->devfreq->lock);
343 val = actmon_readl(tegra, ACTMON_GLB_STATUS);
344 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
345 if (val & tegra->devices[i].config->irq_mask) {
346 actmon_isr_device(tegra, tegra->devices + i);
352 update_devfreq(tegra->devfreq);
354 mutex_unlock(&tegra->devfreq->lock);
356 return handled ? IRQ_HANDLED : IRQ_NONE;
359 static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
360 unsigned long action, void *ptr)
362 struct clk_notifier_data *data = ptr;
363 struct tegra_devfreq *tegra;
364 struct tegra_devfreq_device *dev;
367 if (action != POST_RATE_CHANGE)
370 tegra = container_of(nb, struct tegra_devfreq, rate_change_nb);
372 tegra->cur_freq = data->new_rate / KHZ;
374 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
375 dev = &tegra->devices[i];
377 tegra_devfreq_update_wmark(tegra, dev);
380 actmon_write_barrier(tegra);
385 static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
386 struct tegra_devfreq_device *dev)
390 dev->target_freq = tegra->cur_freq;
392 dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
393 device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
395 tegra_devfreq_update_avg_wmark(tegra, dev);
396 tegra_devfreq_update_wmark(tegra, dev);
398 device_writel(dev, ACTMON_COUNT_WEIGHT, ACTMON_DEV_COUNT_WEIGHT);
399 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
401 val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
402 val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
403 << ACTMON_DEV_CTRL_K_VAL_SHIFT;
404 val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
405 << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
406 val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
407 << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
408 val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
409 val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
410 val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
411 val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
412 val |= ACTMON_DEV_CTRL_ENB;
414 device_writel(dev, val, ACTMON_DEV_CTRL);
417 static void tegra_actmon_start(struct tegra_devfreq *tegra)
421 disable_irq(tegra->irq);
423 actmon_writel(tegra, ACTMON_SAMPLING_PERIOD - 1,
424 ACTMON_GLB_PERIOD_CTRL);
426 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++)
427 tegra_actmon_configure_device(tegra, &tegra->devices[i]);
429 actmon_write_barrier(tegra);
431 enable_irq(tegra->irq);
434 static void tegra_actmon_stop(struct tegra_devfreq *tegra)
438 disable_irq(tegra->irq);
440 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
441 device_writel(&tegra->devices[i], 0x00000000, ACTMON_DEV_CTRL);
442 device_writel(&tegra->devices[i], ACTMON_INTR_STATUS_CLEAR,
443 ACTMON_DEV_INTR_STATUS);
446 actmon_write_barrier(tegra);
448 enable_irq(tegra->irq);
451 static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
454 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
455 struct devfreq *devfreq = tegra->devfreq;
456 struct dev_pm_opp *opp;
460 opp = devfreq_recommended_opp(dev, freq, flags);
462 dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
465 rate = dev_pm_opp_get_freq(opp);
468 err = clk_set_min_rate(tegra->emc_clock, rate);
472 err = clk_set_rate(tegra->emc_clock, 0);
474 goto restore_min_rate;
479 clk_set_min_rate(tegra->emc_clock, devfreq->previous_freq);
484 static int tegra_devfreq_get_dev_status(struct device *dev,
485 struct devfreq_dev_status *stat)
487 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
488 struct tegra_devfreq_device *actmon_dev;
489 unsigned long cur_freq;
491 cur_freq = READ_ONCE(tegra->cur_freq);
493 /* To be used by the tegra governor */
494 stat->private_data = tegra;
496 /* The below are to be used by the other governors */
497 stat->current_frequency = cur_freq * KHZ;
499 actmon_dev = &tegra->devices[MCALL];
501 /* Number of cycles spent on memory access */
502 stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
504 /* The bus can be considered to be saturated way before 100% */
505 stat->busy_time *= 100 / BUS_SATURATION_RATIO;
507 /* Number of cycles in a sampling period */
508 stat->total_time = ACTMON_SAMPLING_PERIOD * cur_freq;
510 stat->busy_time = min(stat->busy_time, stat->total_time);
515 static struct devfreq_dev_profile tegra_devfreq_profile = {
517 .target = tegra_devfreq_target,
518 .get_dev_status = tegra_devfreq_get_dev_status,
521 static int tegra_governor_get_target(struct devfreq *devfreq,
524 struct devfreq_dev_status *stat;
525 struct tegra_devfreq *tegra;
526 struct tegra_devfreq_device *dev;
527 unsigned long target_freq = 0;
531 err = devfreq_update_stats(devfreq);
535 stat = &devfreq->last_status;
537 tegra = stat->private_data;
539 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
540 dev = &tegra->devices[i];
542 actmon_update_target(tegra, dev);
544 target_freq = max(target_freq, dev->target_freq);
547 *freq = target_freq * KHZ;
552 static int tegra_governor_event_handler(struct devfreq *devfreq,
553 unsigned int event, void *data)
555 struct tegra_devfreq *tegra = dev_get_drvdata(devfreq->dev.parent);
558 case DEVFREQ_GOV_START:
559 devfreq_monitor_start(devfreq);
560 tegra_actmon_start(tegra);
563 case DEVFREQ_GOV_STOP:
564 tegra_actmon_stop(tegra);
565 devfreq_monitor_stop(devfreq);
568 case DEVFREQ_GOV_SUSPEND:
569 tegra_actmon_stop(tegra);
570 devfreq_monitor_suspend(devfreq);
573 case DEVFREQ_GOV_RESUME:
574 devfreq_monitor_resume(devfreq);
575 tegra_actmon_start(tegra);
582 static struct devfreq_governor tegra_devfreq_governor = {
583 .name = "tegra_actmon",
584 .get_target_freq = tegra_governor_get_target,
585 .event_handler = tegra_governor_event_handler,
589 static int tegra_devfreq_probe(struct platform_device *pdev)
591 struct tegra_devfreq *tegra;
592 struct tegra_devfreq_device *dev;
597 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
601 tegra->regs = devm_platform_ioremap_resource(pdev, 0);
602 if (IS_ERR(tegra->regs))
603 return PTR_ERR(tegra->regs);
605 tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
606 if (IS_ERR(tegra->reset)) {
607 dev_err(&pdev->dev, "Failed to get reset\n");
608 return PTR_ERR(tegra->reset);
611 tegra->clock = devm_clk_get(&pdev->dev, "actmon");
612 if (IS_ERR(tegra->clock)) {
613 dev_err(&pdev->dev, "Failed to get actmon clock\n");
614 return PTR_ERR(tegra->clock);
617 tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
618 if (IS_ERR(tegra->emc_clock)) {
619 dev_err(&pdev->dev, "Failed to get emc clock\n");
620 return PTR_ERR(tegra->emc_clock);
623 tegra->irq = platform_get_irq(pdev, 0);
624 if (tegra->irq < 0) {
626 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", err);
630 reset_control_assert(tegra->reset);
632 err = clk_prepare_enable(tegra->clock);
635 "Failed to prepare and enable ACTMON clock\n");
639 reset_control_deassert(tegra->reset);
641 tegra->max_freq = clk_round_rate(tegra->emc_clock, ULONG_MAX) / KHZ;
642 tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
644 for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
645 dev = tegra->devices + i;
646 dev->config = actmon_device_configs + i;
647 dev->regs = tegra->regs + dev->config->offset;
650 for (rate = 0; rate <= tegra->max_freq * KHZ; rate++) {
651 rate = clk_round_rate(tegra->emc_clock, rate);
653 err = dev_pm_opp_add(&pdev->dev, rate, 0);
655 dev_err(&pdev->dev, "Failed to add OPP: %d\n", err);
660 platform_set_drvdata(pdev, tegra);
662 tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb;
663 err = clk_notifier_register(tegra->emc_clock, &tegra->rate_change_nb);
666 "Failed to register rate change notifier\n");
670 err = devfreq_add_governor(&tegra_devfreq_governor);
672 dev_err(&pdev->dev, "Failed to add governor: %d\n", err);
676 tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
677 tegra->devfreq = devfreq_add_device(&pdev->dev,
678 &tegra_devfreq_profile,
681 if (IS_ERR(tegra->devfreq)) {
682 err = PTR_ERR(tegra->devfreq);
683 goto remove_governor;
686 err = devm_request_threaded_irq(&pdev->dev, tegra->irq, NULL,
687 actmon_thread_isr, IRQF_ONESHOT,
688 "tegra-devfreq", tegra);
690 dev_err(&pdev->dev, "Interrupt request failed: %d\n", err);
697 devfreq_remove_device(tegra->devfreq);
700 devfreq_remove_governor(&tegra_devfreq_governor);
703 clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb);
706 dev_pm_opp_remove_all_dynamic(&pdev->dev);
708 reset_control_reset(tegra->reset);
709 clk_disable_unprepare(tegra->clock);
714 static int tegra_devfreq_remove(struct platform_device *pdev)
716 struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
718 devfreq_remove_device(tegra->devfreq);
719 devfreq_remove_governor(&tegra_devfreq_governor);
721 clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb);
722 dev_pm_opp_remove_all_dynamic(&pdev->dev);
724 reset_control_reset(tegra->reset);
725 clk_disable_unprepare(tegra->clock);
730 static const struct of_device_id tegra_devfreq_of_match[] = {
731 { .compatible = "nvidia,tegra30-actmon" },
732 { .compatible = "nvidia,tegra124-actmon" },
736 MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
738 static struct platform_driver tegra_devfreq_driver = {
739 .probe = tegra_devfreq_probe,
740 .remove = tegra_devfreq_remove,
742 .name = "tegra-devfreq",
743 .of_match_table = tegra_devfreq_of_match,
746 module_platform_driver(tegra_devfreq_driver);
748 MODULE_LICENSE("GPL v2");
749 MODULE_DESCRIPTION("Tegra devfreq driver");
750 MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");