xref: /openbmc/linux/drivers/gpu/drm/lima/lima_devfreq.c (revision 3aa139aa9fdc138a84243dc49dc18d9b40e1c6e4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
4  *
5  * Based on panfrost_devfreq.c:
6  *   Copyright 2019 Collabora ltd.
7  */
8 #include <linux/clk.h>
9 #include <linux/devfreq.h>
10 #include <linux/devfreq_cooling.h>
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <linux/property.h>
15 
16 #include "lima_device.h"
17 #include "lima_devfreq.h"
18 
19 static void lima_devfreq_update_utilization(struct lima_devfreq *devfreq)
20 {
21 	ktime_t now, last;
22 
23 	now = ktime_get();
24 	last = devfreq->time_last_update;
25 
26 	if (devfreq->busy_count > 0)
27 		devfreq->busy_time += ktime_sub(now, last);
28 	else
29 		devfreq->idle_time += ktime_sub(now, last);
30 
31 	devfreq->time_last_update = now;
32 }
33 
34 static int lima_devfreq_target(struct device *dev, unsigned long *freq,
35 			       u32 flags)
36 {
37 	struct dev_pm_opp *opp;
38 
39 	opp = devfreq_recommended_opp(dev, freq, flags);
40 	if (IS_ERR(opp))
41 		return PTR_ERR(opp);
42 	dev_pm_opp_put(opp);
43 
44 	return dev_pm_opp_set_rate(dev, *freq);
45 }
46 
47 static void lima_devfreq_reset(struct lima_devfreq *devfreq)
48 {
49 	devfreq->busy_time = 0;
50 	devfreq->idle_time = 0;
51 	devfreq->time_last_update = ktime_get();
52 }
53 
54 static int lima_devfreq_get_dev_status(struct device *dev,
55 				       struct devfreq_dev_status *status)
56 {
57 	struct lima_device *ldev = dev_get_drvdata(dev);
58 	struct lima_devfreq *devfreq = &ldev->devfreq;
59 	unsigned long irqflags;
60 
61 	status->current_frequency = clk_get_rate(ldev->clk_gpu);
62 
63 	spin_lock_irqsave(&devfreq->lock, irqflags);
64 
65 	lima_devfreq_update_utilization(devfreq);
66 
67 	status->total_time = ktime_to_ns(ktime_add(devfreq->busy_time,
68 						   devfreq->idle_time));
69 	status->busy_time = ktime_to_ns(devfreq->busy_time);
70 
71 	lima_devfreq_reset(devfreq);
72 
73 	spin_unlock_irqrestore(&devfreq->lock, irqflags);
74 
75 	dev_dbg(ldev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n",
76 		status->busy_time, status->total_time,
77 		status->busy_time / (status->total_time / 100),
78 		status->current_frequency / 1000 / 1000);
79 
80 	return 0;
81 }
82 
83 static struct devfreq_dev_profile lima_devfreq_profile = {
84 	.polling_ms = 50, /* ~3 frames */
85 	.target = lima_devfreq_target,
86 	.get_dev_status = lima_devfreq_get_dev_status,
87 };
88 
89 void lima_devfreq_fini(struct lima_device *ldev)
90 {
91 	struct lima_devfreq *devfreq = &ldev->devfreq;
92 
93 	if (devfreq->cooling) {
94 		devfreq_cooling_unregister(devfreq->cooling);
95 		devfreq->cooling = NULL;
96 	}
97 
98 	if (devfreq->devfreq) {
99 		devm_devfreq_remove_device(ldev->dev, devfreq->devfreq);
100 		devfreq->devfreq = NULL;
101 	}
102 }
103 
104 int lima_devfreq_init(struct lima_device *ldev)
105 {
106 	struct thermal_cooling_device *cooling;
107 	struct device *dev = ldev->dev;
108 	struct devfreq *devfreq;
109 	struct lima_devfreq *ldevfreq = &ldev->devfreq;
110 	struct dev_pm_opp *opp;
111 	unsigned long cur_freq;
112 	int ret;
113 
114 	if (!device_property_present(dev, "operating-points-v2"))
115 		/* Optional, continue without devfreq */
116 		return 0;
117 
118 	spin_lock_init(&ldevfreq->lock);
119 
120 	ret = devm_pm_opp_set_clkname(dev, "core");
121 	if (ret)
122 		return ret;
123 
124 	ret = devm_pm_opp_set_regulators(dev, (const char *[]){ "mali" }, 1);
125 	if (ret) {
126 		/* Continue if the optional regulator is missing */
127 		if (ret != -ENODEV)
128 			return ret;
129 	}
130 
131 	ret = devm_pm_opp_of_add_table(dev);
132 	if (ret)
133 		return ret;
134 
135 	lima_devfreq_reset(ldevfreq);
136 
137 	cur_freq = clk_get_rate(ldev->clk_gpu);
138 
139 	opp = devfreq_recommended_opp(dev, &cur_freq, 0);
140 	if (IS_ERR(opp))
141 		return PTR_ERR(opp);
142 
143 	lima_devfreq_profile.initial_freq = cur_freq;
144 	dev_pm_opp_put(opp);
145 
146 	devfreq = devm_devfreq_add_device(dev, &lima_devfreq_profile,
147 					  DEVFREQ_GOV_SIMPLE_ONDEMAND, NULL);
148 	if (IS_ERR(devfreq)) {
149 		dev_err(dev, "Couldn't initialize GPU devfreq\n");
150 		return PTR_ERR(devfreq);
151 	}
152 
153 	ldevfreq->devfreq = devfreq;
154 
155 	cooling = of_devfreq_cooling_register(dev->of_node, devfreq);
156 	if (IS_ERR(cooling))
157 		dev_info(dev, "Failed to register cooling device\n");
158 	else
159 		ldevfreq->cooling = cooling;
160 
161 	return 0;
162 }
163 
164 void lima_devfreq_record_busy(struct lima_devfreq *devfreq)
165 {
166 	unsigned long irqflags;
167 
168 	if (!devfreq->devfreq)
169 		return;
170 
171 	spin_lock_irqsave(&devfreq->lock, irqflags);
172 
173 	lima_devfreq_update_utilization(devfreq);
174 
175 	devfreq->busy_count++;
176 
177 	spin_unlock_irqrestore(&devfreq->lock, irqflags);
178 }
179 
180 void lima_devfreq_record_idle(struct lima_devfreq *devfreq)
181 {
182 	unsigned long irqflags;
183 
184 	if (!devfreq->devfreq)
185 		return;
186 
187 	spin_lock_irqsave(&devfreq->lock, irqflags);
188 
189 	lima_devfreq_update_utilization(devfreq);
190 
191 	WARN_ON(--devfreq->busy_count < 0);
192 
193 	spin_unlock_irqrestore(&devfreq->lock, irqflags);
194 }
195 
196 int lima_devfreq_resume(struct lima_devfreq *devfreq)
197 {
198 	unsigned long irqflags;
199 
200 	if (!devfreq->devfreq)
201 		return 0;
202 
203 	spin_lock_irqsave(&devfreq->lock, irqflags);
204 
205 	lima_devfreq_reset(devfreq);
206 
207 	spin_unlock_irqrestore(&devfreq->lock, irqflags);
208 
209 	return devfreq_resume_device(devfreq->devfreq);
210 }
211 
212 int lima_devfreq_suspend(struct lima_devfreq *devfreq)
213 {
214 	if (!devfreq->devfreq)
215 		return 0;
216 
217 	return devfreq_suspend_device(devfreq->devfreq);
218 }
219