1 // SPDX-License-Identifier: GPL-2.0 or MIT 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 <drm/drm_managed.h> 11 12 #include "panthor_devfreq.h" 13 #include "panthor_device.h" 14 15 /** 16 * struct panthor_devfreq - Device frequency management 17 */ 18 struct panthor_devfreq { 19 /** @devfreq: devfreq device. */ 20 struct devfreq *devfreq; 21 22 /** @gov_data: Governor data. */ 23 struct devfreq_simple_ondemand_data gov_data; 24 25 /** @busy_time: Busy time. */ 26 ktime_t busy_time; 27 28 /** @idle_time: Idle time. */ 29 ktime_t idle_time; 30 31 /** @time_last_update: Last update time. */ 32 ktime_t time_last_update; 33 34 /** @last_busy_state: True if the GPU was busy last time we updated the state. */ 35 bool last_busy_state; 36 37 /** 38 * @lock: Lock used to protect busy_time, idle_time, time_last_update and 39 * last_busy_state. 40 * 41 * These fields can be accessed concurrently by panthor_devfreq_get_dev_status() 42 * and panthor_devfreq_record_{busy,idle}(). 43 */ 44 spinlock_t lock; 45 }; 46 47 static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) 48 { 49 ktime_t now, last; 50 51 now = ktime_get(); 52 last = pdevfreq->time_last_update; 53 54 if (pdevfreq->last_busy_state) 55 pdevfreq->busy_time += ktime_sub(now, last); 56 else 57 pdevfreq->idle_time += ktime_sub(now, last); 58 59 pdevfreq->time_last_update = now; 60 } 61 62 static int panthor_devfreq_target(struct device *dev, unsigned long *freq, 63 u32 flags) 64 { 65 struct panthor_device *ptdev = dev_get_drvdata(dev); 66 struct dev_pm_opp *opp; 67 int err; 68 69 opp = devfreq_recommended_opp(dev, freq, flags); 70 if (IS_ERR(opp)) 71 return PTR_ERR(opp); 72 dev_pm_opp_put(opp); 73 74 err = dev_pm_opp_set_rate(dev, *freq); 75 if (!err) 76 ptdev->current_frequency = *freq; 77 78 return err; 79 } 80 81 static void panthor_devfreq_reset(struct panthor_devfreq *pdevfreq) 82 { 83 pdevfreq->busy_time = 0; 84 pdevfreq->idle_time = 0; 85 pdevfreq->time_last_update = ktime_get(); 86 } 87 88 static int panthor_devfreq_get_dev_status(struct device *dev, 89 struct devfreq_dev_status *status) 90 { 91 struct panthor_device *ptdev = dev_get_drvdata(dev); 92 struct panthor_devfreq *pdevfreq = ptdev->devfreq; 93 unsigned long irqflags; 94 95 status->current_frequency = clk_get_rate(ptdev->clks.core); 96 97 spin_lock_irqsave(&pdevfreq->lock, irqflags); 98 99 panthor_devfreq_update_utilization(pdevfreq); 100 101 status->total_time = ktime_to_ns(ktime_add(pdevfreq->busy_time, 102 pdevfreq->idle_time)); 103 104 status->busy_time = ktime_to_ns(pdevfreq->busy_time); 105 106 panthor_devfreq_reset(pdevfreq); 107 108 spin_unlock_irqrestore(&pdevfreq->lock, irqflags); 109 110 drm_dbg(&ptdev->base, "busy %lu total %lu %lu %% freq %lu MHz\n", 111 status->busy_time, status->total_time, 112 status->busy_time / (status->total_time / 100), 113 status->current_frequency / 1000 / 1000); 114 115 return 0; 116 } 117 118 static struct devfreq_dev_profile panthor_devfreq_profile = { 119 .timer = DEVFREQ_TIMER_DELAYED, 120 .polling_ms = 50, /* ~3 frames */ 121 .target = panthor_devfreq_target, 122 .get_dev_status = panthor_devfreq_get_dev_status, 123 }; 124 125 int panthor_devfreq_init(struct panthor_device *ptdev) 126 { 127 /* There's actually 2 regulators (mali and sram), but the OPP core only 128 * supports one. 129 * 130 * We assume the sram regulator is coupled with the mali one and let 131 * the coupling logic deal with voltage updates. 132 */ 133 static const char * const reg_names[] = { "mali", NULL }; 134 struct thermal_cooling_device *cooling; 135 struct device *dev = ptdev->base.dev; 136 struct panthor_devfreq *pdevfreq; 137 struct dev_pm_opp *opp; 138 unsigned long cur_freq; 139 unsigned long freq = ULONG_MAX; 140 int ret; 141 142 pdevfreq = drmm_kzalloc(&ptdev->base, sizeof(*ptdev->devfreq), GFP_KERNEL); 143 if (!pdevfreq) 144 return -ENOMEM; 145 146 ptdev->devfreq = pdevfreq; 147 148 ret = devm_pm_opp_set_regulators(dev, reg_names); 149 if (ret && ret != -ENODEV) { 150 if (ret != -EPROBE_DEFER) 151 DRM_DEV_ERROR(dev, "Couldn't set OPP regulators\n"); 152 return ret; 153 } 154 155 ret = devm_pm_opp_of_add_table(dev); 156 if (ret) 157 return ret; 158 159 spin_lock_init(&pdevfreq->lock); 160 161 panthor_devfreq_reset(pdevfreq); 162 163 cur_freq = clk_get_rate(ptdev->clks.core); 164 165 /* Regulator coupling only takes care of synchronizing/balancing voltage 166 * updates, but the coupled regulator needs to be enabled manually. 167 * 168 * We use devm_regulator_get_enable_optional() and keep the sram supply 169 * enabled until the device is removed, just like we do for the mali 170 * supply, which is enabled when dev_pm_opp_set_opp(dev, opp) is called, 171 * and disabled when the opp_table is torn down, using the devm action. 172 * 173 * If we really care about disabling regulators on suspend, we should: 174 * - use devm_regulator_get_optional() here 175 * - call dev_pm_opp_set_opp(dev, NULL) before leaving this function 176 * (this disables the regulator passed to the OPP layer) 177 * - call dev_pm_opp_set_opp(dev, NULL) and 178 * regulator_disable(ptdev->regulators.sram) in 179 * panthor_devfreq_suspend() 180 * - call dev_pm_opp_set_opp(dev, default_opp) and 181 * regulator_enable(ptdev->regulators.sram) in 182 * panthor_devfreq_resume() 183 * 184 * But without knowing if it's beneficial or not (in term of power 185 * consumption), or how much it slows down the suspend/resume steps, 186 * let's just keep regulators enabled for the device lifetime. 187 */ 188 ret = devm_regulator_get_enable_optional(dev, "sram"); 189 if (ret && ret != -ENODEV) { 190 if (ret != -EPROBE_DEFER) 191 DRM_DEV_ERROR(dev, "Couldn't retrieve/enable sram supply\n"); 192 return ret; 193 } 194 195 opp = devfreq_recommended_opp(dev, &cur_freq, 0); 196 if (IS_ERR(opp)) 197 return PTR_ERR(opp); 198 199 panthor_devfreq_profile.initial_freq = cur_freq; 200 ptdev->current_frequency = cur_freq; 201 202 /* 203 * Set the recommend OPP this will enable and configure the regulator 204 * if any and will avoid a switch off by regulator_late_cleanup() 205 */ 206 ret = dev_pm_opp_set_opp(dev, opp); 207 dev_pm_opp_put(opp); 208 if (ret) { 209 DRM_DEV_ERROR(dev, "Couldn't set recommended OPP\n"); 210 return ret; 211 } 212 213 /* Find the fastest defined rate */ 214 opp = dev_pm_opp_find_freq_floor(dev, &freq); 215 if (IS_ERR(opp)) 216 return PTR_ERR(opp); 217 ptdev->fast_rate = freq; 218 219 dev_pm_opp_put(opp); 220 221 /* 222 * Setup default thresholds for the simple_ondemand governor. 223 * The values are chosen based on experiments. 224 */ 225 pdevfreq->gov_data.upthreshold = 45; 226 pdevfreq->gov_data.downdifferential = 5; 227 228 pdevfreq->devfreq = devm_devfreq_add_device(dev, &panthor_devfreq_profile, 229 DEVFREQ_GOV_SIMPLE_ONDEMAND, 230 &pdevfreq->gov_data); 231 if (IS_ERR(pdevfreq->devfreq)) { 232 DRM_DEV_ERROR(dev, "Couldn't initialize GPU devfreq\n"); 233 ret = PTR_ERR(pdevfreq->devfreq); 234 pdevfreq->devfreq = NULL; 235 return ret; 236 } 237 238 cooling = devfreq_cooling_em_register(pdevfreq->devfreq, NULL); 239 if (IS_ERR(cooling)) 240 DRM_DEV_INFO(dev, "Failed to register cooling device\n"); 241 242 return 0; 243 } 244 245 void panthor_devfreq_resume(struct panthor_device *ptdev) 246 { 247 struct panthor_devfreq *pdevfreq = ptdev->devfreq; 248 249 if (!pdevfreq->devfreq) 250 return; 251 252 panthor_devfreq_reset(pdevfreq); 253 254 drm_WARN_ON(&ptdev->base, devfreq_resume_device(pdevfreq->devfreq)); 255 } 256 257 void panthor_devfreq_suspend(struct panthor_device *ptdev) 258 { 259 struct panthor_devfreq *pdevfreq = ptdev->devfreq; 260 261 if (!pdevfreq->devfreq) 262 return; 263 264 drm_WARN_ON(&ptdev->base, devfreq_suspend_device(pdevfreq->devfreq)); 265 } 266 267 void panthor_devfreq_record_busy(struct panthor_device *ptdev) 268 { 269 struct panthor_devfreq *pdevfreq = ptdev->devfreq; 270 unsigned long irqflags; 271 272 if (!pdevfreq->devfreq) 273 return; 274 275 spin_lock_irqsave(&pdevfreq->lock, irqflags); 276 277 panthor_devfreq_update_utilization(pdevfreq); 278 pdevfreq->last_busy_state = true; 279 280 spin_unlock_irqrestore(&pdevfreq->lock, irqflags); 281 } 282 283 void panthor_devfreq_record_idle(struct panthor_device *ptdev) 284 { 285 struct panthor_devfreq *pdevfreq = ptdev->devfreq; 286 unsigned long irqflags; 287 288 if (!pdevfreq->devfreq) 289 return; 290 291 spin_lock_irqsave(&pdevfreq->lock, irqflags); 292 293 panthor_devfreq_update_utilization(pdevfreq); 294 pdevfreq->last_busy_state = false; 295 296 spin_unlock_irqrestore(&pdevfreq->lock, irqflags); 297 } 298