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 .timer = DEVFREQ_TIMER_DELAYED, 85 .polling_ms = 50, /* ~3 frames */ 86 .target = lima_devfreq_target, 87 .get_dev_status = lima_devfreq_get_dev_status, 88 }; 89 90 void lima_devfreq_fini(struct lima_device *ldev) 91 { 92 struct lima_devfreq *devfreq = &ldev->devfreq; 93 94 if (devfreq->cooling) { 95 devfreq_cooling_unregister(devfreq->cooling); 96 devfreq->cooling = NULL; 97 } 98 99 if (devfreq->devfreq) { 100 devm_devfreq_remove_device(ldev->dev, devfreq->devfreq); 101 devfreq->devfreq = NULL; 102 } 103 } 104 105 int lima_devfreq_init(struct lima_device *ldev) 106 { 107 struct thermal_cooling_device *cooling; 108 struct device *dev = ldev->dev; 109 struct devfreq *devfreq; 110 struct lima_devfreq *ldevfreq = &ldev->devfreq; 111 struct dev_pm_opp *opp; 112 unsigned long cur_freq; 113 int ret; 114 const char *regulator_names[] = { "mali", NULL }; 115 const char *clk_names[] = { "core", NULL }; 116 struct dev_pm_opp_config config = { 117 .regulator_names = regulator_names, 118 .clk_names = clk_names, 119 }; 120 121 if (!device_property_present(dev, "operating-points-v2")) 122 /* Optional, continue without devfreq */ 123 return 0; 124 125 spin_lock_init(&ldevfreq->lock); 126 127 ret = devm_pm_opp_set_config(dev, &config); 128 if (ret) { 129 /* Continue if the optional regulator is missing */ 130 if (ret != -ENODEV) 131 return ret; 132 } 133 134 ret = devm_pm_opp_of_add_table(dev); 135 if (ret) 136 return ret; 137 138 lima_devfreq_reset(ldevfreq); 139 140 cur_freq = clk_get_rate(ldev->clk_gpu); 141 142 opp = devfreq_recommended_opp(dev, &cur_freq, 0); 143 if (IS_ERR(opp)) 144 return PTR_ERR(opp); 145 146 lima_devfreq_profile.initial_freq = cur_freq; 147 dev_pm_opp_put(opp); 148 149 /* 150 * Setup default thresholds for the simple_ondemand governor. 151 * The values are chosen based on experiments. 152 */ 153 ldevfreq->gov_data.upthreshold = 30; 154 ldevfreq->gov_data.downdifferential = 5; 155 156 devfreq = devm_devfreq_add_device(dev, &lima_devfreq_profile, 157 DEVFREQ_GOV_SIMPLE_ONDEMAND, 158 &ldevfreq->gov_data); 159 if (IS_ERR(devfreq)) { 160 dev_err(dev, "Couldn't initialize GPU devfreq\n"); 161 return PTR_ERR(devfreq); 162 } 163 164 ldevfreq->devfreq = devfreq; 165 166 cooling = of_devfreq_cooling_register(dev->of_node, devfreq); 167 if (IS_ERR(cooling)) 168 dev_info(dev, "Failed to register cooling device\n"); 169 else 170 ldevfreq->cooling = cooling; 171 172 return 0; 173 } 174 175 void lima_devfreq_record_busy(struct lima_devfreq *devfreq) 176 { 177 unsigned long irqflags; 178 179 if (!devfreq->devfreq) 180 return; 181 182 spin_lock_irqsave(&devfreq->lock, irqflags); 183 184 lima_devfreq_update_utilization(devfreq); 185 186 devfreq->busy_count++; 187 188 spin_unlock_irqrestore(&devfreq->lock, irqflags); 189 } 190 191 void lima_devfreq_record_idle(struct lima_devfreq *devfreq) 192 { 193 unsigned long irqflags; 194 195 if (!devfreq->devfreq) 196 return; 197 198 spin_lock_irqsave(&devfreq->lock, irqflags); 199 200 lima_devfreq_update_utilization(devfreq); 201 202 WARN_ON(--devfreq->busy_count < 0); 203 204 spin_unlock_irqrestore(&devfreq->lock, irqflags); 205 } 206 207 int lima_devfreq_resume(struct lima_devfreq *devfreq) 208 { 209 unsigned long irqflags; 210 211 if (!devfreq->devfreq) 212 return 0; 213 214 spin_lock_irqsave(&devfreq->lock, irqflags); 215 216 lima_devfreq_reset(devfreq); 217 218 spin_unlock_irqrestore(&devfreq->lock, irqflags); 219 220 return devfreq_resume_device(devfreq->devfreq); 221 } 222 223 int lima_devfreq_suspend(struct lima_devfreq *devfreq) 224 { 225 if (!devfreq->devfreq) 226 return 0; 227 228 return devfreq_suspend_device(devfreq->devfreq); 229 } 230