1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Collabora ltd. */
3 #include <linux/devfreq.h>
4 #include <linux/platform_device.h>
5 #include <linux/pm_opp.h>
7 #include <linux/regulator/consumer.h>
9 #include "panfrost_device.h"
10 #include "panfrost_devfreq.h"
11 #include "panfrost_features.h"
12 #include "panfrost_issues.h"
13 #include "panfrost_gpu.h"
14 #include "panfrost_regs.h"
16 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot);
18 static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
21 struct panfrost_device *pfdev = dev_get_drvdata(dev);
24 err = dev_pm_opp_set_rate(dev, *freq);
28 *freq = clk_get_rate(pfdev->clock);
33 static void panfrost_devfreq_reset(struct panfrost_device *pfdev)
35 ktime_t now = ktime_get();
38 for (i = 0; i < NUM_JOB_SLOTS; i++) {
39 pfdev->devfreq.slot[i].busy_time = 0;
40 pfdev->devfreq.slot[i].idle_time = 0;
41 pfdev->devfreq.slot[i].time_last_update = now;
45 static int panfrost_devfreq_get_dev_status(struct device *dev,
46 struct devfreq_dev_status *status)
48 struct panfrost_device *pfdev = dev_get_drvdata(dev);
51 for (i = 0; i < NUM_JOB_SLOTS; i++) {
52 panfrost_devfreq_update_utilization(pfdev, i);
55 status->current_frequency = clk_get_rate(pfdev->clock);
56 status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.slot[0].busy_time,
57 pfdev->devfreq.slot[0].idle_time));
59 status->busy_time = 0;
60 for (i = 0; i < NUM_JOB_SLOTS; i++) {
61 status->busy_time += ktime_to_ns(pfdev->devfreq.slot[i].busy_time);
64 /* We're scheduling only to one core atm, so don't divide for now */
65 /* status->busy_time /= NUM_JOB_SLOTS; */
67 panfrost_devfreq_reset(pfdev);
69 dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time,
71 status->busy_time / (status->total_time / 100),
72 status->current_frequency / 1000 / 1000);
77 static int panfrost_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
79 struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev));
81 *freq = clk_get_rate(pfdev->clock);
86 static struct devfreq_dev_profile panfrost_devfreq_profile = {
87 .polling_ms = 50, /* ~3 frames */
88 .target = panfrost_devfreq_target,
89 .get_dev_status = panfrost_devfreq_get_dev_status,
90 .get_cur_freq = panfrost_devfreq_get_cur_freq,
93 int panfrost_devfreq_init(struct panfrost_device *pfdev)
96 struct dev_pm_opp *opp;
97 unsigned long cur_freq;
99 ret = dev_pm_opp_of_add_table(&pfdev->pdev->dev);
100 if (ret == -ENODEV) /* Optional, continue without devfreq */
105 panfrost_devfreq_reset(pfdev);
107 cur_freq = clk_get_rate(pfdev->clock);
109 opp = devfreq_recommended_opp(&pfdev->pdev->dev, &cur_freq, 0);
113 panfrost_devfreq_profile.initial_freq = cur_freq;
116 pfdev->devfreq.devfreq = devm_devfreq_add_device(&pfdev->pdev->dev,
117 &panfrost_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
119 if (IS_ERR(pfdev->devfreq.devfreq)) {
120 DRM_DEV_ERROR(&pfdev->pdev->dev, "Couldn't initialize GPU devfreq\n");
121 ret = PTR_ERR(pfdev->devfreq.devfreq);
122 pfdev->devfreq.devfreq = NULL;
123 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
130 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
132 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
135 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
139 if (!pfdev->devfreq.devfreq)
142 panfrost_devfreq_reset(pfdev);
143 for (i = 0; i < NUM_JOB_SLOTS; i++)
144 pfdev->devfreq.slot[i].busy = false;
146 devfreq_resume_device(pfdev->devfreq.devfreq);
149 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
151 if (!pfdev->devfreq.devfreq)
154 devfreq_suspend_device(pfdev->devfreq.devfreq);
157 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot)
159 struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot];
163 if (!pfdev->devfreq.devfreq)
167 last = pfdev->devfreq.slot[slot].time_last_update;
169 /* If we last recorded a transition to busy, we have been idle since */
170 if (devfreq_slot->busy)
171 pfdev->devfreq.slot[slot].busy_time += ktime_sub(now, last);
173 pfdev->devfreq.slot[slot].idle_time += ktime_sub(now, last);
175 pfdev->devfreq.slot[slot].time_last_update = now;
178 /* The job scheduler is expected to call this at every transition busy <-> idle */
179 void panfrost_devfreq_record_transition(struct panfrost_device *pfdev, int slot)
181 struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot];
183 panfrost_devfreq_update_utilization(pfdev, slot);
184 devfreq_slot->busy = !devfreq_slot->busy;