xref: /linux/drivers/gpu/drm/panfrost/panfrost_devfreq.c (revision 1f8644d56e91f1c3cbeb1a36c3a998397d06b9dc)
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 
13 static 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 
28 static 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 
41 static 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 
48 static 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 
78 static 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 
85 int 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 opp_table *opp_table;
93 	struct thermal_cooling_device *cooling;
94 	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
95 
96 	opp_table = dev_pm_opp_set_regulators(dev, pfdev->comp->supply_names,
97 					      pfdev->comp->num_supplies);
98 	if (IS_ERR(opp_table)) {
99 		ret = PTR_ERR(opp_table);
100 		/* Continue if the optional regulator is missing */
101 		if (ret != -ENODEV) {
102 			DRM_DEV_ERROR(dev, "Couldn't set OPP regulators\n");
103 			goto err_fini;
104 		}
105 	} else {
106 		pfdevfreq->regulators_opp_table = opp_table;
107 	}
108 
109 	ret = dev_pm_opp_of_add_table(dev);
110 	if (ret) {
111 		/* Optional, continue without devfreq */
112 		if (ret == -ENODEV)
113 			ret = 0;
114 		goto err_fini;
115 	}
116 	pfdevfreq->opp_of_table_added = true;
117 
118 	spin_lock_init(&pfdevfreq->lock);
119 
120 	panfrost_devfreq_reset(pfdevfreq);
121 
122 	cur_freq = clk_get_rate(pfdev->clock);
123 
124 	opp = devfreq_recommended_opp(dev, &cur_freq, 0);
125 	if (IS_ERR(opp)) {
126 		ret = PTR_ERR(opp);
127 		goto err_fini;
128 	}
129 
130 	panfrost_devfreq_profile.initial_freq = cur_freq;
131 	dev_pm_opp_put(opp);
132 
133 	/*
134 	 * Setup default thresholds for the simple_ondemand governor.
135 	 * The values are chosen based on experiments.
136 	 */
137 	pfdevfreq->gov_data.upthreshold = 45;
138 	pfdevfreq->gov_data.downdifferential = 5;
139 
140 	devfreq = devm_devfreq_add_device(dev, &panfrost_devfreq_profile,
141 					  DEVFREQ_GOV_SIMPLE_ONDEMAND,
142 					  &pfdevfreq->gov_data);
143 	if (IS_ERR(devfreq)) {
144 		DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n");
145 		ret = PTR_ERR(devfreq);
146 		goto err_fini;
147 	}
148 	pfdevfreq->devfreq = devfreq;
149 
150 	cooling = devfreq_cooling_em_register(devfreq, NULL);
151 	if (IS_ERR(cooling))
152 		DRM_DEV_INFO(dev, "Failed to register cooling device\n");
153 	else
154 		pfdevfreq->cooling = cooling;
155 
156 	return 0;
157 
158 err_fini:
159 	panfrost_devfreq_fini(pfdev);
160 	return ret;
161 }
162 
163 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
164 {
165 	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
166 
167 	if (pfdevfreq->cooling) {
168 		devfreq_cooling_unregister(pfdevfreq->cooling);
169 		pfdevfreq->cooling = NULL;
170 	}
171 
172 	if (pfdevfreq->opp_of_table_added) {
173 		dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
174 		pfdevfreq->opp_of_table_added = false;
175 	}
176 
177 	dev_pm_opp_put_regulators(pfdevfreq->regulators_opp_table);
178 	pfdevfreq->regulators_opp_table = NULL;
179 }
180 
181 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
182 {
183 	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
184 
185 	if (!pfdevfreq->devfreq)
186 		return;
187 
188 	panfrost_devfreq_reset(pfdevfreq);
189 
190 	devfreq_resume_device(pfdevfreq->devfreq);
191 }
192 
193 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
194 {
195 	struct panfrost_devfreq *pfdevfreq = &pfdev->pfdevfreq;
196 
197 	if (!pfdevfreq->devfreq)
198 		return;
199 
200 	devfreq_suspend_device(pfdevfreq->devfreq);
201 }
202 
203 void panfrost_devfreq_record_busy(struct panfrost_devfreq *pfdevfreq)
204 {
205 	unsigned long irqflags;
206 
207 	if (!pfdevfreq->devfreq)
208 		return;
209 
210 	spin_lock_irqsave(&pfdevfreq->lock, irqflags);
211 
212 	panfrost_devfreq_update_utilization(pfdevfreq);
213 
214 	pfdevfreq->busy_count++;
215 
216 	spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
217 }
218 
219 void panfrost_devfreq_record_idle(struct panfrost_devfreq *pfdevfreq)
220 {
221 	unsigned long irqflags;
222 
223 	if (!pfdevfreq->devfreq)
224 		return;
225 
226 	spin_lock_irqsave(&pfdevfreq->lock, irqflags);
227 
228 	panfrost_devfreq_update_utilization(pfdevfreq);
229 
230 	WARN_ON(--pfdevfreq->busy_count < 0);
231 
232 	spin_unlock_irqrestore(&pfdevfreq->lock, irqflags);
233 }
234