|
| 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_features.h" |
| 11 | +#include "panfrost_issues.h" |
| 12 | +#include "panfrost_gpu.h" |
| 13 | +#include "panfrost_regs.h" |
| 14 | + |
| 15 | +static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot); |
| 16 | + |
| 17 | +static int panfrost_devfreq_target(struct device *dev, unsigned long *freq, |
| 18 | + u32 flags) |
| 19 | +{ |
| 20 | + struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev)); |
| 21 | + struct dev_pm_opp *opp; |
| 22 | + unsigned long old_clk_rate = pfdev->devfreq.cur_freq; |
| 23 | + unsigned long target_volt, target_rate; |
| 24 | + int err; |
| 25 | + |
| 26 | + opp = devfreq_recommended_opp(dev, freq, flags); |
| 27 | + if (IS_ERR(opp)) |
| 28 | + return PTR_ERR(opp); |
| 29 | + |
| 30 | + target_rate = dev_pm_opp_get_freq(opp); |
| 31 | + target_volt = dev_pm_opp_get_voltage(opp); |
| 32 | + dev_pm_opp_put(opp); |
| 33 | + |
| 34 | + if (old_clk_rate == target_rate) |
| 35 | + return 0; |
| 36 | + |
| 37 | + /* |
| 38 | + * If frequency scaling from low to high, adjust voltage first. |
| 39 | + * If frequency scaling from high to low, adjust frequency first. |
| 40 | + */ |
| 41 | + if (old_clk_rate < target_rate) { |
| 42 | + err = regulator_set_voltage(pfdev->regulator, target_volt, |
| 43 | + target_volt); |
| 44 | + if (err) { |
| 45 | + dev_err(dev, "Cannot set voltage %lu uV\n", |
| 46 | + target_volt); |
| 47 | + return err; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + err = clk_set_rate(pfdev->clock, target_rate); |
| 52 | + if (err) { |
| 53 | + dev_err(dev, "Cannot set frequency %lu (%d)\n", target_rate, |
| 54 | + err); |
| 55 | + regulator_set_voltage(pfdev->regulator, pfdev->devfreq.cur_volt, |
| 56 | + pfdev->devfreq.cur_volt); |
| 57 | + return err; |
| 58 | + } |
| 59 | + |
| 60 | + if (old_clk_rate > target_rate) { |
| 61 | + err = regulator_set_voltage(pfdev->regulator, target_volt, |
| 62 | + target_volt); |
| 63 | + if (err) |
| 64 | + dev_err(dev, "Cannot set voltage %lu uV\n", target_volt); |
| 65 | + } |
| 66 | + |
| 67 | + pfdev->devfreq.cur_freq = target_rate; |
| 68 | + pfdev->devfreq.cur_volt = target_volt; |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +static void panfrost_devfreq_reset(struct panfrost_device *pfdev) |
| 74 | +{ |
| 75 | + ktime_t now = ktime_get(); |
| 76 | + int i; |
| 77 | + |
| 78 | + for (i = 0; i < NUM_JOB_SLOTS; i++) { |
| 79 | + pfdev->devfreq.slot[i].busy_time = 0; |
| 80 | + pfdev->devfreq.slot[i].idle_time = 0; |
| 81 | + pfdev->devfreq.slot[i].time_last_update = now; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +static int panfrost_devfreq_get_dev_status(struct device *dev, |
| 86 | + struct devfreq_dev_status *status) |
| 87 | +{ |
| 88 | + struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev)); |
| 89 | + int i; |
| 90 | + |
| 91 | + for (i = 0; i < NUM_JOB_SLOTS; i++) { |
| 92 | + panfrost_devfreq_update_utilization(pfdev, i); |
| 93 | + } |
| 94 | + |
| 95 | + status->current_frequency = clk_get_rate(pfdev->clock); |
| 96 | + status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.slot[0].busy_time, |
| 97 | + pfdev->devfreq.slot[0].idle_time)); |
| 98 | + |
| 99 | + status->busy_time = 0; |
| 100 | + for (i = 0; i < NUM_JOB_SLOTS; i++) { |
| 101 | + status->busy_time += ktime_to_ns(pfdev->devfreq.slot[i].busy_time); |
| 102 | + } |
| 103 | + |
| 104 | + /* We're scheduling only to one core atm, so don't divide for now */ |
| 105 | + /* status->busy_time /= NUM_JOB_SLOTS; */ |
| 106 | + |
| 107 | + panfrost_devfreq_reset(pfdev); |
| 108 | + |
| 109 | + dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time, |
| 110 | + status->total_time, |
| 111 | + status->busy_time / (status->total_time / 100), |
| 112 | + status->current_frequency / 1000 / 1000); |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +static int panfrost_devfreq_get_cur_freq(struct device *dev, unsigned long *freq) |
| 118 | +{ |
| 119 | + struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev)); |
| 120 | + |
| 121 | + *freq = pfdev->devfreq.cur_freq; |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +static struct devfreq_dev_profile panfrost_devfreq_profile = { |
| 127 | + .polling_ms = 50, /* ~3 frames */ |
| 128 | + .target = panfrost_devfreq_target, |
| 129 | + .get_dev_status = panfrost_devfreq_get_dev_status, |
| 130 | + .get_cur_freq = panfrost_devfreq_get_cur_freq, |
| 131 | +}; |
| 132 | + |
| 133 | +int panfrost_devfreq_init(struct panfrost_device *pfdev) |
| 134 | +{ |
| 135 | + int ret; |
| 136 | + struct dev_pm_opp *opp; |
| 137 | + |
| 138 | + if (!pfdev->regulator) |
| 139 | + return 0; |
| 140 | + |
| 141 | + ret = dev_pm_opp_of_add_table(&pfdev->pdev->dev); |
| 142 | + if (ret == -ENODEV) /* Optional, continue without devfreq */ |
| 143 | + return 0; |
| 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, "simple_ondemand", NULL); |
| 158 | + if (IS_ERR(pfdev->devfreq.devfreq)) { |
| 159 | + DRM_DEV_ERROR(&pfdev->pdev->dev, "Couldn't initialize GPU devfreq\n"); |
| 160 | + ret = PTR_ERR(pfdev->devfreq.devfreq); |
| 161 | + pfdev->devfreq.devfreq = NULL; |
| 162 | + return ret; |
| 163 | + } |
| 164 | + |
| 165 | + return 0; |
| 166 | +} |
| 167 | + |
| 168 | +void panfrost_devfreq_resume(struct panfrost_device *pfdev) |
| 169 | +{ |
| 170 | + int i; |
| 171 | + |
| 172 | + if (!pfdev->devfreq.devfreq) |
| 173 | + return; |
| 174 | + |
| 175 | + panfrost_devfreq_reset(pfdev); |
| 176 | + for (i = 0; i < NUM_JOB_SLOTS; i++) |
| 177 | + pfdev->devfreq.slot[i].busy = false; |
| 178 | + |
| 179 | + devfreq_resume_device(pfdev->devfreq.devfreq); |
| 180 | +} |
| 181 | + |
| 182 | +void panfrost_devfreq_suspend(struct panfrost_device *pfdev) |
| 183 | +{ |
| 184 | + if (!pfdev->devfreq.devfreq) |
| 185 | + return; |
| 186 | + |
| 187 | + devfreq_suspend_device(pfdev->devfreq.devfreq); |
| 188 | +} |
| 189 | + |
| 190 | +static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot) |
| 191 | +{ |
| 192 | + struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot]; |
| 193 | + ktime_t now; |
| 194 | + ktime_t last; |
| 195 | + |
| 196 | + if (!pfdev->devfreq.devfreq) |
| 197 | + return; |
| 198 | + |
| 199 | + now = ktime_get(); |
| 200 | + last = pfdev->devfreq.slot[slot].time_last_update; |
| 201 | + |
| 202 | + /* If we last recorded a transition to busy, we have been idle since */ |
| 203 | + if (devfreq_slot->busy) |
| 204 | + pfdev->devfreq.slot[slot].busy_time += ktime_sub(now, last); |
| 205 | + else |
| 206 | + pfdev->devfreq.slot[slot].idle_time += ktime_sub(now, last); |
| 207 | + |
| 208 | + pfdev->devfreq.slot[slot].time_last_update = now; |
| 209 | +} |
| 210 | + |
| 211 | +/* The job scheduler is expected to call this at every transition busy <-> idle */ |
| 212 | +void panfrost_devfreq_record_transition(struct panfrost_device *pfdev, int slot) |
| 213 | +{ |
| 214 | + struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot]; |
| 215 | + |
| 216 | + panfrost_devfreq_update_utilization(pfdev, slot); |
| 217 | + devfreq_slot->busy = !devfreq_slot->busy; |
| 218 | +} |
0 commit comments