Linux Audio

Check our new training course

Loading...
v5.4
  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>
  6#include <linux/clk.h>
  7#include <linux/regulator/consumer.h>
  8
  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"
 15
 16static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot);
 
 
 
 
 
 
 
 
 
 
 
 
 
 17
 18static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
 19				   u32 flags)
 20{
 21	struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev));
 22	struct dev_pm_opp *opp;
 23	unsigned long old_clk_rate = pfdev->devfreq.cur_freq;
 24	unsigned long target_volt, target_rate;
 25	int err;
 26
 27	opp = devfreq_recommended_opp(dev, freq, flags);
 28	if (IS_ERR(opp))
 29		return PTR_ERR(opp);
 30
 31	target_rate = dev_pm_opp_get_freq(opp);
 32	target_volt = dev_pm_opp_get_voltage(opp);
 33	dev_pm_opp_put(opp);
 34
 35	if (old_clk_rate == target_rate)
 36		return 0;
 37
 38	/*
 39	 * If frequency scaling from low to high, adjust voltage first.
 40	 * If frequency scaling from high to low, adjust frequency first.
 41	 */
 42	if (old_clk_rate < target_rate) {
 43		err = regulator_set_voltage(pfdev->regulator, target_volt,
 44					    target_volt);
 45		if (err) {
 46			dev_err(dev, "Cannot set voltage %lu uV\n",
 47				target_volt);
 48			return err;
 49		}
 50	}
 51
 52	err = clk_set_rate(pfdev->clock, target_rate);
 53	if (err) {
 54		dev_err(dev, "Cannot set frequency %lu (%d)\n", target_rate,
 55			err);
 56		regulator_set_voltage(pfdev->regulator, pfdev->devfreq.cur_volt,
 57				      pfdev->devfreq.cur_volt);
 58		return err;
 59	}
 60
 61	if (old_clk_rate > target_rate) {
 62		err = regulator_set_voltage(pfdev->regulator, target_volt,
 63					    target_volt);
 64		if (err)
 65			dev_err(dev, "Cannot set voltage %lu uV\n", target_volt);
 66	}
 67
 68	pfdev->devfreq.cur_freq = target_rate;
 69	pfdev->devfreq.cur_volt = target_volt;
 70
 71	return 0;
 72}
 73
 74static void panfrost_devfreq_reset(struct panfrost_device *pfdev)
 75{
 76	ktime_t now = ktime_get();
 77	int i;
 78
 79	for (i = 0; i < NUM_JOB_SLOTS; i++) {
 80		pfdev->devfreq.slot[i].busy_time = 0;
 81		pfdev->devfreq.slot[i].idle_time = 0;
 82		pfdev->devfreq.slot[i].time_last_update = now;
 83	}
 84}
 85
 86static int panfrost_devfreq_get_dev_status(struct device *dev,
 87					   struct devfreq_dev_status *status)
 88{
 89	struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev));
 90	int i;
 91
 92	for (i = 0; i < NUM_JOB_SLOTS; i++) {
 93		panfrost_devfreq_update_utilization(pfdev, i);
 94	}
 95
 96	status->current_frequency = clk_get_rate(pfdev->clock);
 97	status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.slot[0].busy_time,
 98						   pfdev->devfreq.slot[0].idle_time));
 99
100	status->busy_time = 0;
101	for (i = 0; i < NUM_JOB_SLOTS; i++) {
102		status->busy_time += ktime_to_ns(pfdev->devfreq.slot[i].busy_time);
103	}
104
105	/* We're scheduling only to one core atm, so don't divide for now */
106	/* status->busy_time /= NUM_JOB_SLOTS; */
107
108	panfrost_devfreq_reset(pfdev);
 
109
110	dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time,
111		status->total_time,
112		status->busy_time / (status->total_time / 100),
113		status->current_frequency / 1000 / 1000);
114
115	return 0;
116}
117
118static int panfrost_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
119{
120	struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev));
121
122	*freq = pfdev->devfreq.cur_freq;
 
 
 
123
124	return 0;
125}
126
127static struct devfreq_dev_profile panfrost_devfreq_profile = {
 
128	.polling_ms = 50, /* ~3 frames */
129	.target = panfrost_devfreq_target,
130	.get_dev_status = panfrost_devfreq_get_dev_status,
131	.get_cur_freq = panfrost_devfreq_get_cur_freq,
132};
133
134int panfrost_devfreq_init(struct panfrost_device *pfdev)
135{
136	int ret;
137	struct dev_pm_opp *opp;
138
139	ret = dev_pm_opp_of_add_table(&pfdev->pdev->dev);
140	if (ret == -ENODEV) /* Optional, continue without devfreq */
 
 
 
 
 
 
 
 
 
141		return 0;
142	else if (ret)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143		return ret;
 
 
 
 
144
145	panfrost_devfreq_reset(pfdev);
146
147	pfdev->devfreq.cur_freq = clk_get_rate(pfdev->clock);
148
149	opp = devfreq_recommended_opp(&pfdev->pdev->dev, &pfdev->devfreq.cur_freq, 0);
150	if (IS_ERR(opp))
151		return PTR_ERR(opp);
152
153	panfrost_devfreq_profile.initial_freq = pfdev->devfreq.cur_freq;
154	dev_pm_opp_put(opp);
155
156	pfdev->devfreq.devfreq = devm_devfreq_add_device(&pfdev->pdev->dev,
157			&panfrost_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
158			NULL);
159	if (IS_ERR(pfdev->devfreq.devfreq)) {
160		DRM_DEV_ERROR(&pfdev->pdev->dev, "Couldn't initialize GPU devfreq\n");
161		ret = PTR_ERR(pfdev->devfreq.devfreq);
162		pfdev->devfreq.devfreq = NULL;
163		dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
164		return ret;
165	}
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167	return 0;
168}
169
170void panfrost_devfreq_fini(struct panfrost_device *pfdev)
171{
172	dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
 
 
 
 
 
173}
174
175void panfrost_devfreq_resume(struct panfrost_device *pfdev)
176{
177	int i;
178
179	if (!pfdev->devfreq.devfreq)
180		return;
181
182	panfrost_devfreq_reset(pfdev);
183	for (i = 0; i < NUM_JOB_SLOTS; i++)
184		pfdev->devfreq.slot[i].busy = false;
185
186	devfreq_resume_device(pfdev->devfreq.devfreq);
187}
188
189void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
190{
191	if (!pfdev->devfreq.devfreq)
 
 
192		return;
193
194	devfreq_suspend_device(pfdev->devfreq.devfreq);
195}
196
197static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot)
198{
199	struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot];
200	ktime_t now;
201	ktime_t last;
202
203	if (!pfdev->devfreq.devfreq)
204		return;
205
206	now = ktime_get();
207	last = pfdev->devfreq.slot[slot].time_last_update;
208
209	/* If we last recorded a transition to busy, we have been idle since */
210	if (devfreq_slot->busy)
211		pfdev->devfreq.slot[slot].busy_time += ktime_sub(now, last);
212	else
213		pfdev->devfreq.slot[slot].idle_time += ktime_sub(now, last);
214
215	pfdev->devfreq.slot[slot].time_last_update = now;
216}
217
218/* The job scheduler is expected to call this at every transition busy <-> idle */
219void panfrost_devfreq_record_transition(struct panfrost_device *pfdev, int slot)
220{
221	struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot];
 
 
 
 
 
 
 
 
 
222
223	panfrost_devfreq_update_utilization(pfdev, slot);
224	devfreq_slot->busy = !devfreq_slot->busy;
225}
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright 2019 Collabora ltd. */
  3
  4#include <linux/clk.h>
  5#include <linux/devfreq.h>
  6#include <linux/devfreq_cooling.h>
  7#include <linux/platform_device.h>
  8#include <linux/pm_opp.h>
 
 
  9
 10#include "panfrost_device.h"
 11#include "panfrost_devfreq.h"
 
 
 
 
 12
 13static void panfrost_devfreq_update_utilization(struct panfrost_devfreq *pfdevfreq)
 14{
 15	ktime_t now, last;
 16
 17	now = ktime_get();
 18	last = pfdevfreq->time_last_update;
 19
 20	if (pfdevfreq->busy_count > 0)
 21		pfdevfreq->busy_time += ktime_sub(now, last);
 22	else
 23		pfdevfreq->idle_time += ktime_sub(now, last);
 24
 25	pfdevfreq->time_last_update = now;
 26}
 27
 28static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
 29				   u32 flags)
 30{
 
 31	struct dev_pm_opp *opp;
 
 
 
 32
 33	opp = devfreq_recommended_opp(dev, freq, flags);
 34	if (IS_ERR(opp))
 35		return PTR_ERR(opp);
 
 
 
 36	dev_pm_opp_put(opp);
 37
 38	return dev_pm_opp_set_rate(dev, *freq);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 39}
 40
 41static void panfrost_devfreq_reset(struct panfrost_devfreq *pfdevfreq)
 42{
 43	pfdevfreq->busy_time = 0;
 44	pfdevfreq->idle_time = 0;
 45	pfdevfreq->time_last_update = ktime_get();
 
 
 
 
 
 46}
 47
 48static int panfrost_devfreq_get_dev_status(struct device *dev,
 49					   struct devfreq_dev_status *status)
 50{
 51	struct panfrost_device *pfdev = dev_get_drvdata(dev);
 52	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
 53	unsigned long irqflags;
 
 
 
 54
 55	status->current_frequency = clk_get_rate(pfdev->clock);
 
 
 56
 57	spin_lock_irqsave(&pfdevfreq->lock, irqflags);
 
 
 
 58
 59	panfrost_devfreq_update_utilization(pfdevfreq);
 
 60
 61	status->total_time = ktime_to_ns(ktime_add(pfdevfreq->busy_time,
 62						   pfdevfreq->idle_time));
 63
 64	status->busy_time = ktime_to_ns(pfdevfreq->busy_time);
 
 
 
 65
 66	panfrost_devfreq_reset(pfdevfreq);
 
 67
 68	spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
 
 
 69
 70	dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n",
 71		status->busy_time, status->total_time,
 72		status->busy_time / (status->total_time / 100),
 73		status->current_frequency / 1000 / 1000);
 74
 75	return 0;
 76}
 77
 78static struct devfreq_dev_profile panfrost_devfreq_profile = {
 79	.timer = DEVFREQ_TIMER_DELAYED,
 80	.polling_ms = 50, /* ~3 frames */
 81	.target = panfrost_devfreq_target,
 82	.get_dev_status = panfrost_devfreq_get_dev_status,
 
 83};
 84
 85int panfrost_devfreq_init(struct panfrost_device *pfdev)
 86{
 87	int ret;
 88	struct dev_pm_opp *opp;
 89	unsigned long cur_freq;
 90	struct device *dev = &pfdev->pdev->dev;
 91	struct devfreq *devfreq;
 92	struct thermal_cooling_device *cooling;
 93	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
 94
 95	if (pfdev->comp->num_supplies > 1) {
 96		/*
 97		 * GPUs with more than 1 supply require platform-specific handling:
 98		 * continue without devfreq
 99		 */
100		DRM_DEV_INFO(dev, "More than 1 supply is not supported yet\n");
101		return 0;
102	}
103
104	ret = devm_pm_opp_set_regulators(dev, pfdev->comp->supply_names);
105	if (ret) {
106		/* Continue if the optional regulator is missing */
107		if (ret != -ENODEV) {
108			if (ret != -EPROBE_DEFER)
109				DRM_DEV_ERROR(dev, "Couldn't set OPP regulators\n");
110			return ret;
111		}
112	}
113
114	ret = devm_pm_opp_of_add_table(dev);
115	if (ret) {
116		/* Optional, continue without devfreq */
117		if (ret == -ENODEV)
118			ret = 0;
119		return ret;
120	}
121	pfdevfreq->opp_of_table_added = true;
122
123	spin_lock_init(&pfdevfreq->lock);
124
125	panfrost_devfreq_reset(pfdevfreq);
126
127	cur_freq = clk_get_rate(pfdev->clock);
128
129	opp = devfreq_recommended_opp(dev, &cur_freq, 0);
130	if (IS_ERR(opp))
131		return PTR_ERR(opp);
132
133	panfrost_devfreq_profile.initial_freq = cur_freq;
 
134
135	/*
136	 * Set the recommend OPP this will enable and configure the regulator
137	 * if any and will avoid a switch off by regulator_late_cleanup()
138	 */
139	ret = dev_pm_opp_set_opp(dev, opp);
140	if (ret) {
141		DRM_DEV_ERROR(dev, "Couldn't set recommended OPP\n");
 
142		return ret;
143	}
144
145	dev_pm_opp_put(opp);
146
147	/*
148	 * Setup default thresholds for the simple_ondemand governor.
149	 * The values are chosen based on experiments.
150	 */
151	pfdevfreq->gov_data.upthreshold = 45;
152	pfdevfreq->gov_data.downdifferential = 5;
153
154	devfreq = devm_devfreq_add_device(dev, &panfrost_devfreq_profile,
155					  DEVFREQ_GOV_SIMPLE_ONDEMAND,
156					  &pfdevfreq->gov_data);
157	if (IS_ERR(devfreq)) {
158		DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n");
159		return PTR_ERR(devfreq);
160	}
161	pfdevfreq->devfreq = devfreq;
162
163	cooling = devfreq_cooling_em_register(devfreq, NULL);
164	if (IS_ERR(cooling))
165		DRM_DEV_INFO(dev, "Failed to register cooling device\n");
166	else
167		pfdevfreq->cooling = cooling;
168
169	return 0;
170}
171
172void panfrost_devfreq_fini(struct panfrost_device *pfdev)
173{
174	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
175
176	if (pfdevfreq->cooling) {
177		devfreq_cooling_unregister(pfdevfreq->cooling);
178		pfdevfreq->cooling = NULL;
179	}
180}
181
182void panfrost_devfreq_resume(struct panfrost_device *pfdev)
183{
184	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
185
186	if (!pfdevfreq->devfreq)
187		return;
188
189	panfrost_devfreq_reset(pfdevfreq);
 
 
190
191	devfreq_resume_device(pfdevfreq->devfreq);
192}
193
194void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
195{
196	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
197
198	if (!pfdevfreq->devfreq)
199		return;
200
201	devfreq_suspend_device(pfdevfreq->devfreq);
202}
203
204void panfrost_devfreq_record_busy(struct panfrost_devfreq *pfdevfreq)
205{
206	unsigned long irqflags;
 
 
207
208	if (!pfdevfreq->devfreq)
209		return;
210
211	spin_lock_irqsave(&pfdevfreq->lock, irqflags);
 
212
213	panfrost_devfreq_update_utilization(pfdevfreq);
214
215	pfdevfreq->busy_count++;
 
 
216
217	spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
218}
219
220void panfrost_devfreq_record_idle(struct panfrost_devfreq *pfdevfreq)
 
221{
222	unsigned long irqflags;
223
224	if (!pfdevfreq->devfreq)
225		return;
226
227	spin_lock_irqsave(&pfdevfreq->lock, irqflags);
228
229	panfrost_devfreq_update_utilization(pfdevfreq);
230
231	WARN_ON(--pfdevfreq->busy_count < 0);
232
233	spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
 
234}