|
| 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 | + int err; |
| 39 | + |
| 40 | + opp = devfreq_recommended_opp(dev, freq, flags); |
| 41 | + if (IS_ERR(opp)) |
| 42 | + return PTR_ERR(opp); |
| 43 | + dev_pm_opp_put(opp); |
| 44 | + |
| 45 | + err = dev_pm_opp_set_rate(dev, *freq); |
| 46 | + if (err) |
| 47 | + return err; |
| 48 | + |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static void lima_devfreq_reset(struct lima_devfreq *devfreq) |
| 53 | +{ |
| 54 | + devfreq->busy_time = 0; |
| 55 | + devfreq->idle_time = 0; |
| 56 | + devfreq->time_last_update = ktime_get(); |
| 57 | +} |
| 58 | + |
| 59 | +static int lima_devfreq_get_dev_status(struct device *dev, |
| 60 | + struct devfreq_dev_status *status) |
| 61 | +{ |
| 62 | + struct lima_device *ldev = dev_get_drvdata(dev); |
| 63 | + struct lima_devfreq *devfreq = &ldev->devfreq; |
| 64 | + unsigned long irqflags; |
| 65 | + |
| 66 | + status->current_frequency = clk_get_rate(ldev->clk_gpu); |
| 67 | + |
| 68 | + spin_lock_irqsave(&devfreq->lock, irqflags); |
| 69 | + |
| 70 | + lima_devfreq_update_utilization(devfreq); |
| 71 | + |
| 72 | + status->total_time = ktime_to_ns(ktime_add(devfreq->busy_time, |
| 73 | + devfreq->idle_time)); |
| 74 | + status->busy_time = ktime_to_ns(devfreq->busy_time); |
| 75 | + |
| 76 | + lima_devfreq_reset(devfreq); |
| 77 | + |
| 78 | + spin_unlock_irqrestore(&devfreq->lock, irqflags); |
| 79 | + |
| 80 | + dev_dbg(ldev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", |
| 81 | + status->busy_time, status->total_time, |
| 82 | + status->busy_time / (status->total_time / 100), |
| 83 | + status->current_frequency / 1000 / 1000); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static struct devfreq_dev_profile lima_devfreq_profile = { |
| 89 | + .polling_ms = 50, /* ~3 frames */ |
| 90 | + .target = lima_devfreq_target, |
| 91 | + .get_dev_status = lima_devfreq_get_dev_status, |
| 92 | +}; |
| 93 | + |
| 94 | +void lima_devfreq_fini(struct lima_device *ldev) |
| 95 | +{ |
| 96 | + struct lima_devfreq *devfreq = &ldev->devfreq; |
| 97 | + |
| 98 | + if (devfreq->cooling) { |
| 99 | + devfreq_cooling_unregister(devfreq->cooling); |
| 100 | + devfreq->cooling = NULL; |
| 101 | + } |
| 102 | + |
| 103 | + if (devfreq->devfreq) { |
| 104 | + devm_devfreq_remove_device(&ldev->pdev->dev, |
| 105 | + devfreq->devfreq); |
| 106 | + devfreq->devfreq = NULL; |
| 107 | + } |
| 108 | + |
| 109 | + if (devfreq->opp_of_table_added) { |
| 110 | + dev_pm_opp_of_remove_table(&ldev->pdev->dev); |
| 111 | + devfreq->opp_of_table_added = false; |
| 112 | + } |
| 113 | + |
| 114 | + if (devfreq->regulators_opp_table) { |
| 115 | + dev_pm_opp_put_regulators(devfreq->regulators_opp_table); |
| 116 | + devfreq->regulators_opp_table = NULL; |
| 117 | + } |
| 118 | + |
| 119 | + if (devfreq->clkname_opp_table) { |
| 120 | + dev_pm_opp_put_clkname(devfreq->clkname_opp_table); |
| 121 | + devfreq->clkname_opp_table = NULL; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +int lima_devfreq_init(struct lima_device *ldev) |
| 126 | +{ |
| 127 | + struct thermal_cooling_device *cooling; |
| 128 | + struct device *dev = &ldev->pdev->dev; |
| 129 | + struct opp_table *opp_table; |
| 130 | + struct devfreq *devfreq; |
| 131 | + struct lima_devfreq *ldevfreq = &ldev->devfreq; |
| 132 | + struct dev_pm_opp *opp; |
| 133 | + unsigned long cur_freq; |
| 134 | + int ret; |
| 135 | + |
| 136 | + if (!device_property_present(dev, "operating-points-v2")) |
| 137 | + /* Optional, continue without devfreq */ |
| 138 | + return 0; |
| 139 | + |
| 140 | + spin_lock_init(&ldevfreq->lock); |
| 141 | + |
| 142 | + opp_table = dev_pm_opp_set_clkname(dev, "core"); |
| 143 | + if (IS_ERR(opp_table)) { |
| 144 | + ret = PTR_ERR(opp_table); |
| 145 | + goto err_fini; |
| 146 | + } |
| 147 | + |
| 148 | + ldevfreq->clkname_opp_table = opp_table; |
| 149 | + |
| 150 | + opp_table = dev_pm_opp_set_regulators(dev, |
| 151 | + (const char *[]){ "mali" }, |
| 152 | + 1); |
| 153 | + if (IS_ERR(opp_table)) { |
| 154 | + ret = PTR_ERR(opp_table); |
| 155 | + |
| 156 | + /* Continue if the optional regulator is missing */ |
| 157 | + if (ret != -ENODEV) |
| 158 | + goto err_fini; |
| 159 | + } else { |
| 160 | + ldevfreq->regulators_opp_table = opp_table; |
| 161 | + } |
| 162 | + |
| 163 | + ret = dev_pm_opp_of_add_table(dev); |
| 164 | + if (ret) |
| 165 | + goto err_fini; |
| 166 | + ldevfreq->opp_of_table_added = true; |
| 167 | + |
| 168 | + lima_devfreq_reset(ldevfreq); |
| 169 | + |
| 170 | + cur_freq = clk_get_rate(ldev->clk_gpu); |
| 171 | + |
| 172 | + opp = devfreq_recommended_opp(dev, &cur_freq, 0); |
| 173 | + if (IS_ERR(opp)) { |
| 174 | + ret = PTR_ERR(opp); |
| 175 | + goto err_fini; |
| 176 | + } |
| 177 | + |
| 178 | + lima_devfreq_profile.initial_freq = cur_freq; |
| 179 | + dev_pm_opp_put(opp); |
| 180 | + |
| 181 | + devfreq = devm_devfreq_add_device(dev, &lima_devfreq_profile, |
| 182 | + DEVFREQ_GOV_SIMPLE_ONDEMAND, NULL); |
| 183 | + if (IS_ERR(devfreq)) { |
| 184 | + dev_err(dev, "Couldn't initialize GPU devfreq\n"); |
| 185 | + ret = PTR_ERR(devfreq); |
| 186 | + goto err_fini; |
| 187 | + } |
| 188 | + |
| 189 | + ldevfreq->devfreq = devfreq; |
| 190 | + |
| 191 | + cooling = of_devfreq_cooling_register(dev->of_node, devfreq); |
| 192 | + if (IS_ERR(cooling)) |
| 193 | + dev_info(dev, "Failed to register cooling device\n"); |
| 194 | + else |
| 195 | + ldevfreq->cooling = cooling; |
| 196 | + |
| 197 | + return 0; |
| 198 | + |
| 199 | +err_fini: |
| 200 | + lima_devfreq_fini(ldev); |
| 201 | + return ret; |
| 202 | +} |
| 203 | + |
| 204 | +void lima_devfreq_record_busy(struct lima_devfreq *devfreq) |
| 205 | +{ |
| 206 | + unsigned long irqflags; |
| 207 | + |
| 208 | + if (!devfreq->devfreq) |
| 209 | + return; |
| 210 | + |
| 211 | + spin_lock_irqsave(&devfreq->lock, irqflags); |
| 212 | + |
| 213 | + lima_devfreq_update_utilization(devfreq); |
| 214 | + |
| 215 | + devfreq->busy_count++; |
| 216 | + |
| 217 | + spin_unlock_irqrestore(&devfreq->lock, irqflags); |
| 218 | +} |
| 219 | + |
| 220 | +void lima_devfreq_record_idle(struct lima_devfreq *devfreq) |
| 221 | +{ |
| 222 | + unsigned long irqflags; |
| 223 | + |
| 224 | + if (!devfreq->devfreq) |
| 225 | + return; |
| 226 | + |
| 227 | + spin_lock_irqsave(&devfreq->lock, irqflags); |
| 228 | + |
| 229 | + lima_devfreq_update_utilization(devfreq); |
| 230 | + |
| 231 | + WARN_ON(--devfreq->busy_count < 0); |
| 232 | + |
| 233 | + spin_unlock_irqrestore(&devfreq->lock, irqflags); |
| 234 | +} |
0 commit comments