Jeremy Gebben | b7bc955 | 2012-01-09 13:32:49 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 13 | |
| 14 | #include <linux/export.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 16 | #include <asm/page.h> |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include <mach/msm_iomap.h> |
| 19 | #include <mach/msm_bus.h> |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 20 | #include <linux/ktime.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 21 | |
| 22 | #include "kgsl.h" |
| 23 | #include "kgsl_pwrscale.h" |
| 24 | #include "kgsl_device.h" |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 25 | #include "kgsl_trace.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | |
Jeremy Gebben | b46f415 | 2011-10-14 14:27:00 -0600 | [diff] [blame] | 27 | #define KGSL_PWRFLAGS_POWER_ON 0 |
| 28 | #define KGSL_PWRFLAGS_CLK_ON 1 |
| 29 | #define KGSL_PWRFLAGS_AXI_ON 2 |
| 30 | #define KGSL_PWRFLAGS_IRQ_ON 3 |
| 31 | |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 32 | #define GPU_SWFI_LATENCY 3 |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 33 | #define UPDATE_BUSY_VAL 1000000 |
| 34 | #define UPDATE_BUSY 50 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 36 | struct clk_pair { |
| 37 | const char *name; |
| 38 | uint map; |
| 39 | }; |
| 40 | |
| 41 | struct clk_pair clks[KGSL_MAX_CLKS] = { |
| 42 | { |
| 43 | .name = "src_clk", |
| 44 | .map = KGSL_CLK_SRC, |
| 45 | }, |
| 46 | { |
| 47 | .name = "core_clk", |
| 48 | .map = KGSL_CLK_CORE, |
| 49 | }, |
| 50 | { |
| 51 | .name = "iface_clk", |
| 52 | .map = KGSL_CLK_IFACE, |
| 53 | }, |
| 54 | { |
| 55 | .name = "mem_clk", |
| 56 | .map = KGSL_CLK_MEM, |
| 57 | }, |
| 58 | { |
| 59 | .name = "mem_iface_clk", |
| 60 | .map = KGSL_CLK_MEM_IFACE, |
| 61 | }, |
| 62 | }; |
| 63 | |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 64 | /* Update the elapsed time at a particular clock level |
| 65 | * if the device is active(on_time = true).Otherwise |
| 66 | * store it as sleep time. |
| 67 | */ |
| 68 | static void update_clk_statistics(struct kgsl_device *device, |
| 69 | bool on_time) |
| 70 | { |
| 71 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 72 | struct kgsl_clk_stats *clkstats = &pwr->clk_stats; |
| 73 | ktime_t elapsed; |
| 74 | int elapsed_us; |
| 75 | if (clkstats->start.tv64 == 0) |
| 76 | clkstats->start = ktime_get(); |
| 77 | clkstats->stop = ktime_get(); |
| 78 | elapsed = ktime_sub(clkstats->stop, clkstats->start); |
| 79 | elapsed_us = ktime_to_us(elapsed); |
| 80 | clkstats->elapsed += elapsed_us; |
| 81 | if (on_time) |
| 82 | clkstats->clock_time[pwr->active_pwrlevel] += elapsed_us; |
| 83 | else |
| 84 | clkstats->clock_time[pwr->num_pwrlevels - 1] += elapsed_us; |
| 85 | clkstats->start = ktime_get(); |
| 86 | } |
| 87 | |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 88 | /* |
| 89 | * Given a requested power level do bounds checking on the constraints and |
| 90 | * return the nearest possible level |
| 91 | */ |
| 92 | |
| 93 | static inline int _adjust_pwrlevel(struct kgsl_pwrctrl *pwr, int level) |
| 94 | { |
Jordan Crouse | 00a01ba | 2012-12-05 15:58:16 -0700 | [diff] [blame^] | 95 | int max_pwrlevel = max_t(int, pwr->thermal_pwrlevel, pwr->max_pwrlevel); |
| 96 | int min_pwrlevel = max_t(int, pwr->thermal_pwrlevel, pwr->min_pwrlevel); |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 97 | |
| 98 | if (level < max_pwrlevel) |
| 99 | return max_pwrlevel; |
| 100 | if (level > min_pwrlevel) |
| 101 | return min_pwrlevel; |
| 102 | |
| 103 | return level; |
| 104 | } |
| 105 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | void kgsl_pwrctrl_pwrlevel_change(struct kgsl_device *device, |
| 107 | unsigned int new_level) |
| 108 | { |
| 109 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 110 | struct kgsl_pwrlevel *pwrlevel; |
| 111 | int delta; |
Jordan Crouse | 3c337a3 | 2012-12-04 16:16:51 -0700 | [diff] [blame] | 112 | int level; |
Jordan Crouse | a29a2e0 | 2012-08-14 09:09:23 -0600 | [diff] [blame] | 113 | |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 114 | /* Adjust the power level to the current constraints */ |
| 115 | new_level = _adjust_pwrlevel(pwr, new_level); |
| 116 | |
| 117 | if (new_level == pwr->active_pwrlevel) |
| 118 | return; |
| 119 | |
| 120 | delta = new_level < pwr->active_pwrlevel ? -1 : 1; |
| 121 | |
| 122 | update_clk_statistics(device, true); |
| 123 | |
Jordan Crouse | 3c337a3 | 2012-12-04 16:16:51 -0700 | [diff] [blame] | 124 | level = pwr->active_pwrlevel; |
| 125 | |
| 126 | /* |
| 127 | * Set the active powerlevel first in case the clocks are off - if we |
| 128 | * don't do this then the pwrlevel change won't take effect when the |
| 129 | * clocks come back |
| 130 | */ |
| 131 | |
| 132 | pwr->active_pwrlevel = new_level; |
| 133 | |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 134 | if (test_bit(KGSL_PWRFLAGS_CLK_ON, &pwr->power_flags) || |
| 135 | (device->state == KGSL_STATE_NAP)) { |
| 136 | |
| 137 | /* |
| 138 | * On some platforms, instability is caused on |
| 139 | * changing clock freq when the core is busy. |
| 140 | * Idle the gpu core before changing the clock freq. |
| 141 | */ |
| 142 | |
| 143 | if (pwr->idle_needed == true) |
| 144 | device->ftbl->idle(device); |
| 145 | |
| 146 | /* |
| 147 | * Don't shift by more than one level at a time to |
| 148 | * avoid glitches. |
| 149 | */ |
| 150 | |
Jordan Crouse | 3c337a3 | 2012-12-04 16:16:51 -0700 | [diff] [blame] | 151 | while (level != new_level) { |
| 152 | level += delta; |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 153 | |
| 154 | clk_set_rate(pwr->grp_clks[0], |
Jordan Crouse | 3c337a3 | 2012-12-04 16:16:51 -0700 | [diff] [blame] | 155 | pwr->pwrlevels[level].gpu_freq); |
Kedar Joshi | c11d098 | 2012-02-07 10:59:49 +0530 | [diff] [blame] | 156 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | } |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 158 | |
| 159 | pwrlevel = &pwr->pwrlevels[pwr->active_pwrlevel]; |
| 160 | |
| 161 | if (test_bit(KGSL_PWRFLAGS_AXI_ON, &pwr->power_flags)) { |
| 162 | |
| 163 | if (pwr->pcl) |
| 164 | msm_bus_scale_client_update_request(pwr->pcl, |
| 165 | pwrlevel->bus_freq); |
| 166 | else if (pwr->ebi1_clk) |
| 167 | clk_set_rate(pwr->ebi1_clk, pwrlevel->bus_freq); |
| 168 | } |
| 169 | |
| 170 | trace_kgsl_pwrlevel(device, pwr->active_pwrlevel, pwrlevel->gpu_freq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | } |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 172 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 173 | EXPORT_SYMBOL(kgsl_pwrctrl_pwrlevel_change); |
| 174 | |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 175 | static int kgsl_pwrctrl_thermal_pwrlevel_store(struct device *dev, |
| 176 | struct device_attribute *attr, |
| 177 | const char *buf, size_t count) |
| 178 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 179 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 180 | struct kgsl_pwrctrl *pwr; |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 181 | int ret, level; |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 182 | |
| 183 | if (device == NULL) |
| 184 | return 0; |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 185 | |
| 186 | pwr = &device->pwrctrl; |
| 187 | |
| 188 | ret = sscanf(buf, "%d", &level); |
| 189 | if (ret != 1) |
| 190 | return count; |
| 191 | |
| 192 | if (level < 0) |
| 193 | return count; |
| 194 | |
| 195 | mutex_lock(&device->mutex); |
| 196 | |
| 197 | if (level > pwr->num_pwrlevels - 2) |
| 198 | level = pwr->num_pwrlevels - 2; |
| 199 | |
| 200 | pwr->thermal_pwrlevel = level; |
| 201 | |
| 202 | /* |
| 203 | * If there is no power policy set the clock to the requested thermal |
| 204 | * level - if thermal now happens to be higher than max, then that will |
| 205 | * be limited by the pwrlevel change function. Otherwise if there is |
| 206 | * a policy only change the active clock if it is higher then the new |
| 207 | * thermal level |
| 208 | */ |
| 209 | |
| 210 | if (device->pwrscale.policy == NULL || |
| 211 | pwr->thermal_pwrlevel > pwr->active_pwrlevel) |
| 212 | kgsl_pwrctrl_pwrlevel_change(device, pwr->thermal_pwrlevel); |
| 213 | |
| 214 | mutex_unlock(&device->mutex); |
| 215 | |
| 216 | return count; |
| 217 | } |
| 218 | |
| 219 | static int kgsl_pwrctrl_thermal_pwrlevel_show(struct device *dev, |
| 220 | struct device_attribute *attr, |
| 221 | char *buf) |
| 222 | { |
| 223 | |
| 224 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 225 | struct kgsl_pwrctrl *pwr; |
| 226 | if (device == NULL) |
| 227 | return 0; |
| 228 | pwr = &device->pwrctrl; |
| 229 | return snprintf(buf, PAGE_SIZE, "%d\n", pwr->thermal_pwrlevel); |
| 230 | } |
| 231 | |
| 232 | static int kgsl_pwrctrl_max_pwrlevel_store(struct device *dev, |
| 233 | struct device_attribute *attr, |
| 234 | const char *buf, size_t count) |
| 235 | { |
| 236 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 237 | struct kgsl_pwrctrl *pwr; |
| 238 | int ret, level, max_level; |
| 239 | |
| 240 | if (device == NULL) |
| 241 | return 0; |
| 242 | |
| 243 | pwr = &device->pwrctrl; |
| 244 | |
| 245 | ret = sscanf(buf, "%d", &level); |
| 246 | if (ret != 1) |
| 247 | return count; |
| 248 | |
| 249 | /* If the use specifies a negative number, then don't change anything */ |
| 250 | if (level < 0) |
| 251 | return count; |
| 252 | |
| 253 | mutex_lock(&device->mutex); |
| 254 | |
| 255 | /* You can't set a maximum power level lower than the minimum */ |
| 256 | if (level > pwr->min_pwrlevel) |
| 257 | level = pwr->min_pwrlevel; |
| 258 | |
| 259 | pwr->max_pwrlevel = level; |
| 260 | |
| 261 | |
| 262 | max_level = max_t(int, pwr->thermal_pwrlevel, pwr->max_pwrlevel); |
| 263 | |
| 264 | /* |
| 265 | * If there is no policy then move to max by default. Otherwise only |
| 266 | * move max if the current level happens to be higher then the new max |
| 267 | */ |
| 268 | |
| 269 | if (device->pwrscale.policy == NULL || |
| 270 | (max_level > pwr->active_pwrlevel)) |
| 271 | kgsl_pwrctrl_pwrlevel_change(device, max_level); |
| 272 | |
| 273 | mutex_unlock(&device->mutex); |
| 274 | |
| 275 | return count; |
| 276 | } |
| 277 | |
| 278 | static int kgsl_pwrctrl_max_pwrlevel_show(struct device *dev, |
| 279 | struct device_attribute *attr, |
| 280 | char *buf) |
| 281 | { |
| 282 | |
| 283 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 284 | struct kgsl_pwrctrl *pwr; |
| 285 | if (device == NULL) |
| 286 | return 0; |
| 287 | pwr = &device->pwrctrl; |
| 288 | return snprintf(buf, PAGE_SIZE, "%d\n", pwr->max_pwrlevel); |
| 289 | } |
| 290 | |
| 291 | static int kgsl_pwrctrl_min_pwrlevel_store(struct device *dev, |
| 292 | struct device_attribute *attr, |
| 293 | const char *buf, size_t count) |
| 294 | { struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 295 | struct kgsl_pwrctrl *pwr; |
| 296 | int ret, level, min_level; |
| 297 | |
| 298 | if (device == NULL) |
| 299 | return 0; |
| 300 | |
| 301 | pwr = &device->pwrctrl; |
| 302 | |
| 303 | ret = sscanf(buf, "%d", &level); |
| 304 | if (ret != 1) |
| 305 | return count; |
| 306 | |
| 307 | /* Don't do anything on obviously incorrect values */ |
| 308 | if (level < 0) |
| 309 | return count; |
| 310 | |
| 311 | mutex_lock(&device->mutex); |
| 312 | if (level > pwr->num_pwrlevels - 2) |
| 313 | level = pwr->num_pwrlevels - 2; |
| 314 | |
| 315 | /* You can't set a minimum power level lower than the maximum */ |
| 316 | if (level < pwr->max_pwrlevel) |
| 317 | level = pwr->max_pwrlevel; |
| 318 | |
| 319 | pwr->min_pwrlevel = level; |
| 320 | |
| 321 | min_level = max_t(int, pwr->thermal_pwrlevel, pwr->min_pwrlevel); |
| 322 | |
| 323 | /* Only move the power level higher if minimum is higher then the |
| 324 | * current level |
| 325 | */ |
| 326 | |
| 327 | if (min_level < pwr->active_pwrlevel) |
| 328 | kgsl_pwrctrl_pwrlevel_change(device, min_level); |
| 329 | |
| 330 | mutex_unlock(&device->mutex); |
| 331 | |
| 332 | return count; |
| 333 | } |
| 334 | |
| 335 | static int kgsl_pwrctrl_min_pwrlevel_show(struct device *dev, |
| 336 | struct device_attribute *attr, |
| 337 | char *buf) |
| 338 | { |
| 339 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 340 | struct kgsl_pwrctrl *pwr; |
| 341 | if (device == NULL) |
| 342 | return 0; |
| 343 | pwr = &device->pwrctrl; |
| 344 | return snprintf(buf, PAGE_SIZE, "%d\n", pwr->min_pwrlevel); |
| 345 | } |
| 346 | |
| 347 | static int kgsl_pwrctrl_num_pwrlevels_show(struct device *dev, |
| 348 | struct device_attribute *attr, |
| 349 | char *buf) |
| 350 | { |
| 351 | |
| 352 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 353 | struct kgsl_pwrctrl *pwr; |
| 354 | if (device == NULL) |
| 355 | return 0; |
| 356 | pwr = &device->pwrctrl; |
| 357 | return snprintf(buf, PAGE_SIZE, "%d\n", pwr->num_pwrlevels - 1); |
| 358 | } |
| 359 | |
| 360 | /* Given a GPU clock value, return the nearest powerlevel */ |
| 361 | |
| 362 | static int _get_nearest_pwrlevel(struct kgsl_pwrctrl *pwr, unsigned int clock) |
| 363 | { |
| 364 | int i; |
| 365 | |
| 366 | for (i = 0; i < pwr->num_pwrlevels - 1; i++) { |
| 367 | if (abs(pwr->pwrlevels[i].gpu_freq - clock) < 5000000) |
| 368 | return i; |
| 369 | } |
| 370 | |
| 371 | return -ERANGE; |
| 372 | } |
| 373 | |
| 374 | static int kgsl_pwrctrl_max_gpuclk_store(struct device *dev, |
| 375 | struct device_attribute *attr, |
| 376 | const char *buf, size_t count) |
| 377 | { |
| 378 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 379 | struct kgsl_pwrctrl *pwr; |
| 380 | unsigned long val; |
| 381 | int ret, level; |
| 382 | |
| 383 | if (device == NULL) |
| 384 | return 0; |
| 385 | |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 386 | pwr = &device->pwrctrl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 387 | |
| 388 | ret = sscanf(buf, "%ld", &val); |
| 389 | if (ret != 1) |
| 390 | return count; |
| 391 | |
| 392 | mutex_lock(&device->mutex); |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 393 | level = _get_nearest_pwrlevel(pwr, val); |
| 394 | if (level < 0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 395 | goto done; |
| 396 | |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 397 | pwr->thermal_pwrlevel = level; |
| 398 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | /* |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 400 | * if the thermal limit is lower than the current setting, |
| 401 | * move the speed down immediately |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | */ |
| 403 | |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 404 | if (pwr->thermal_pwrlevel > pwr->active_pwrlevel) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 405 | kgsl_pwrctrl_pwrlevel_change(device, pwr->thermal_pwrlevel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 406 | |
| 407 | done: |
| 408 | mutex_unlock(&device->mutex); |
| 409 | return count; |
| 410 | } |
| 411 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 412 | static int kgsl_pwrctrl_max_gpuclk_show(struct device *dev, |
| 413 | struct device_attribute *attr, |
| 414 | char *buf) |
| 415 | { |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 416 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 417 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 418 | struct kgsl_pwrctrl *pwr; |
| 419 | if (device == NULL) |
| 420 | return 0; |
| 421 | pwr = &device->pwrctrl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 423 | pwr->pwrlevels[pwr->thermal_pwrlevel].gpu_freq); |
| 424 | } |
| 425 | |
| 426 | static int kgsl_pwrctrl_gpuclk_store(struct device *dev, |
| 427 | struct device_attribute *attr, |
| 428 | const char *buf, size_t count) |
| 429 | { |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 430 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 431 | struct kgsl_pwrctrl *pwr; |
| 432 | unsigned long val; |
| 433 | int ret, level; |
| 434 | |
| 435 | if (device == NULL) |
| 436 | return 0; |
| 437 | |
| 438 | pwr = &device->pwrctrl; |
| 439 | |
| 440 | ret = sscanf(buf, "%ld", &val); |
| 441 | if (ret != 1) |
| 442 | return count; |
| 443 | |
| 444 | mutex_lock(&device->mutex); |
| 445 | level = _get_nearest_pwrlevel(pwr, val); |
| 446 | if (level >= 0) |
| 447 | kgsl_pwrctrl_pwrlevel_change(device, level); |
| 448 | |
| 449 | mutex_unlock(&device->mutex); |
| 450 | return count; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static int kgsl_pwrctrl_gpuclk_show(struct device *dev, |
| 454 | struct device_attribute *attr, |
| 455 | char *buf) |
| 456 | { |
| 457 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 458 | struct kgsl_pwrctrl *pwr; |
| 459 | if (device == NULL) |
| 460 | return 0; |
| 461 | pwr = &device->pwrctrl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 462 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 463 | pwr->pwrlevels[pwr->active_pwrlevel].gpu_freq); |
| 464 | } |
| 465 | |
| 466 | static int kgsl_pwrctrl_pwrnap_store(struct device *dev, |
| 467 | struct device_attribute *attr, |
| 468 | const char *buf, size_t count) |
| 469 | { |
| 470 | char temp[20]; |
| 471 | unsigned long val; |
| 472 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 473 | struct kgsl_pwrctrl *pwr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | int rc; |
| 475 | |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 476 | if (device == NULL) |
| 477 | return 0; |
| 478 | pwr = &device->pwrctrl; |
| 479 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 480 | snprintf(temp, sizeof(temp), "%.*s", |
| 481 | (int)min(count, sizeof(temp) - 1), buf); |
| 482 | rc = strict_strtoul(temp, 0, &val); |
| 483 | if (rc) |
| 484 | return rc; |
| 485 | |
| 486 | mutex_lock(&device->mutex); |
| 487 | |
| 488 | if (val == 1) |
| 489 | pwr->nap_allowed = true; |
| 490 | else if (val == 0) |
| 491 | pwr->nap_allowed = false; |
| 492 | |
| 493 | mutex_unlock(&device->mutex); |
| 494 | |
| 495 | return count; |
| 496 | } |
| 497 | |
| 498 | static int kgsl_pwrctrl_pwrnap_show(struct device *dev, |
| 499 | struct device_attribute *attr, |
| 500 | char *buf) |
| 501 | { |
| 502 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 503 | if (device == NULL) |
| 504 | return 0; |
| 505 | return snprintf(buf, PAGE_SIZE, "%d\n", device->pwrctrl.nap_allowed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
| 509 | static int kgsl_pwrctrl_idle_timer_store(struct device *dev, |
| 510 | struct device_attribute *attr, |
| 511 | const char *buf, size_t count) |
| 512 | { |
| 513 | char temp[20]; |
| 514 | unsigned long val; |
| 515 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 516 | struct kgsl_pwrctrl *pwr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | const long div = 1000/HZ; |
| 518 | static unsigned int org_interval_timeout = 1; |
| 519 | int rc; |
| 520 | |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 521 | if (device == NULL) |
| 522 | return 0; |
| 523 | pwr = &device->pwrctrl; |
| 524 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | snprintf(temp, sizeof(temp), "%.*s", |
| 526 | (int)min(count, sizeof(temp) - 1), buf); |
| 527 | rc = strict_strtoul(temp, 0, &val); |
| 528 | if (rc) |
| 529 | return rc; |
| 530 | |
| 531 | if (org_interval_timeout == 1) |
| 532 | org_interval_timeout = pwr->interval_timeout; |
| 533 | |
| 534 | mutex_lock(&device->mutex); |
| 535 | |
| 536 | /* Let the timeout be requested in ms, but convert to jiffies. */ |
| 537 | val /= div; |
| 538 | if (val >= org_interval_timeout) |
| 539 | pwr->interval_timeout = val; |
| 540 | |
| 541 | mutex_unlock(&device->mutex); |
| 542 | |
| 543 | return count; |
| 544 | } |
| 545 | |
| 546 | static int kgsl_pwrctrl_idle_timer_show(struct device *dev, |
| 547 | struct device_attribute *attr, |
| 548 | char *buf) |
| 549 | { |
| 550 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 551 | if (device == NULL) |
| 552 | return 0; |
| 553 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 554 | device->pwrctrl.interval_timeout); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 557 | static int kgsl_pwrctrl_gpubusy_show(struct device *dev, |
| 558 | struct device_attribute *attr, |
| 559 | char *buf) |
| 560 | { |
| 561 | int ret; |
| 562 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 563 | struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats; |
| 564 | ret = snprintf(buf, PAGE_SIZE, "%7d %7d\n", |
| 565 | clkstats->on_time_old, clkstats->elapsed_old); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 566 | if (!test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) { |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 567 | clkstats->on_time_old = 0; |
| 568 | clkstats->elapsed_old = 0; |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 569 | } |
| 570 | return ret; |
| 571 | } |
| 572 | |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 573 | static int kgsl_pwrctrl_gputop_show(struct device *dev, |
| 574 | struct device_attribute *attr, |
| 575 | char *buf) |
| 576 | { |
| 577 | int ret; |
| 578 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 579 | struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats; |
| 580 | int i = 0; |
| 581 | char *ptr = buf; |
| 582 | |
| 583 | ret = snprintf(buf, PAGE_SIZE, "%7d %7d ", clkstats->on_time_old, |
| 584 | clkstats->elapsed_old); |
| 585 | for (i = 0, ptr += ret; i < device->pwrctrl.num_pwrlevels; |
| 586 | i++, ptr += ret) |
| 587 | ret = snprintf(ptr, PAGE_SIZE, "%7d ", |
| 588 | clkstats->old_clock_time[i]); |
| 589 | |
| 590 | if (!test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) { |
| 591 | clkstats->on_time_old = 0; |
| 592 | clkstats->elapsed_old = 0; |
| 593 | for (i = 0; i < KGSL_MAX_PWRLEVELS ; i++) |
| 594 | clkstats->old_clock_time[i] = 0; |
| 595 | } |
| 596 | return (unsigned int) (ptr - buf); |
| 597 | } |
| 598 | |
Anshuman Dani | 91ede1e | 2012-08-21 14:44:38 +0530 | [diff] [blame] | 599 | static int kgsl_pwrctrl_gpu_available_frequencies_show( |
| 600 | struct device *dev, |
| 601 | struct device_attribute *attr, |
| 602 | char *buf) |
| 603 | { |
| 604 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 605 | struct kgsl_pwrctrl *pwr; |
| 606 | int index, num_chars = 0; |
| 607 | |
| 608 | if (device == NULL) |
| 609 | return 0; |
| 610 | pwr = &device->pwrctrl; |
| 611 | for (index = 0; index < pwr->num_pwrlevels - 1; index++) |
| 612 | num_chars += snprintf(buf + num_chars, PAGE_SIZE, "%d ", |
| 613 | pwr->pwrlevels[index].gpu_freq); |
| 614 | buf[num_chars++] = '\n'; |
| 615 | return num_chars; |
| 616 | } |
| 617 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 618 | DEVICE_ATTR(gpuclk, 0644, kgsl_pwrctrl_gpuclk_show, kgsl_pwrctrl_gpuclk_store); |
| 619 | DEVICE_ATTR(max_gpuclk, 0644, kgsl_pwrctrl_max_gpuclk_show, |
| 620 | kgsl_pwrctrl_max_gpuclk_store); |
Praveena Pachipulusu | 263467d | 2011-12-22 18:07:16 +0530 | [diff] [blame] | 621 | DEVICE_ATTR(pwrnap, 0664, kgsl_pwrctrl_pwrnap_show, kgsl_pwrctrl_pwrnap_store); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 622 | DEVICE_ATTR(idle_timer, 0644, kgsl_pwrctrl_idle_timer_show, |
| 623 | kgsl_pwrctrl_idle_timer_store); |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 624 | DEVICE_ATTR(gpubusy, 0444, kgsl_pwrctrl_gpubusy_show, |
| 625 | NULL); |
| 626 | DEVICE_ATTR(gputop, 0444, kgsl_pwrctrl_gputop_show, |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 627 | NULL); |
Anshuman Dani | 91ede1e | 2012-08-21 14:44:38 +0530 | [diff] [blame] | 628 | DEVICE_ATTR(gpu_available_frequencies, 0444, |
| 629 | kgsl_pwrctrl_gpu_available_frequencies_show, |
| 630 | NULL); |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 631 | DEVICE_ATTR(max_pwrlevel, 0644, |
| 632 | kgsl_pwrctrl_max_pwrlevel_show, |
| 633 | kgsl_pwrctrl_max_pwrlevel_store); |
| 634 | DEVICE_ATTR(min_pwrlevel, 0644, |
| 635 | kgsl_pwrctrl_min_pwrlevel_show, |
| 636 | kgsl_pwrctrl_min_pwrlevel_store); |
| 637 | DEVICE_ATTR(thermal_pwrlevel, 0644, |
| 638 | kgsl_pwrctrl_thermal_pwrlevel_show, |
| 639 | kgsl_pwrctrl_thermal_pwrlevel_store); |
| 640 | DEVICE_ATTR(num_pwrlevels, 0444, |
| 641 | kgsl_pwrctrl_num_pwrlevels_show, |
| 642 | NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 643 | |
| 644 | static const struct device_attribute *pwrctrl_attr_list[] = { |
| 645 | &dev_attr_gpuclk, |
| 646 | &dev_attr_max_gpuclk, |
| 647 | &dev_attr_pwrnap, |
| 648 | &dev_attr_idle_timer, |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 649 | &dev_attr_gpubusy, |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 650 | &dev_attr_gputop, |
Anshuman Dani | 91ede1e | 2012-08-21 14:44:38 +0530 | [diff] [blame] | 651 | &dev_attr_gpu_available_frequencies, |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 652 | &dev_attr_max_pwrlevel, |
| 653 | &dev_attr_min_pwrlevel, |
| 654 | &dev_attr_thermal_pwrlevel, |
| 655 | &dev_attr_num_pwrlevels, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 656 | NULL |
| 657 | }; |
| 658 | |
| 659 | int kgsl_pwrctrl_init_sysfs(struct kgsl_device *device) |
| 660 | { |
| 661 | return kgsl_create_device_sysfs_files(device->dev, pwrctrl_attr_list); |
| 662 | } |
| 663 | |
| 664 | void kgsl_pwrctrl_uninit_sysfs(struct kgsl_device *device) |
| 665 | { |
| 666 | kgsl_remove_device_sysfs_files(device->dev, pwrctrl_attr_list); |
| 667 | } |
| 668 | |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 669 | static void update_statistics(struct kgsl_device *device) |
| 670 | { |
| 671 | struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats; |
| 672 | unsigned int on_time = 0; |
| 673 | int i; |
| 674 | int num_pwrlevels = device->pwrctrl.num_pwrlevels - 1; |
| 675 | /*PER CLK TIME*/ |
| 676 | for (i = 0; i < num_pwrlevels; i++) { |
| 677 | clkstats->old_clock_time[i] = clkstats->clock_time[i]; |
| 678 | on_time += clkstats->clock_time[i]; |
| 679 | clkstats->clock_time[i] = 0; |
| 680 | } |
| 681 | clkstats->old_clock_time[num_pwrlevels] = |
| 682 | clkstats->clock_time[num_pwrlevels]; |
| 683 | clkstats->clock_time[num_pwrlevels] = 0; |
| 684 | clkstats->on_time_old = on_time; |
| 685 | clkstats->elapsed_old = clkstats->elapsed; |
| 686 | clkstats->elapsed = 0; |
| 687 | } |
| 688 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 689 | /* Track the amount of time the gpu is on vs the total system time. * |
| 690 | * Regularly update the percentage of busy time displayed by sysfs. */ |
| 691 | static void kgsl_pwrctrl_busy_time(struct kgsl_device *device, bool on_time) |
| 692 | { |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 693 | struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats; |
| 694 | update_clk_statistics(device, on_time); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 695 | /* Update the output regularly and reset the counters. */ |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 696 | if ((clkstats->elapsed > UPDATE_BUSY_VAL) || |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 697 | !test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) { |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 698 | update_statistics(device); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 699 | } |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 702 | void kgsl_pwrctrl_clk(struct kgsl_device *device, int state, |
| 703 | int requested_state) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 704 | { |
| 705 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 706 | int i = 0; |
| 707 | if (state == KGSL_PWRFLAGS_OFF) { |
| 708 | if (test_and_clear_bit(KGSL_PWRFLAGS_CLK_ON, |
| 709 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 710 | trace_kgsl_clk(device, state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 711 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 712 | if (pwr->grp_clks[i]) |
| 713 | clk_disable(pwr->grp_clks[i]); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 714 | /* High latency clock maintenance. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 715 | if ((pwr->pwrlevels[0].gpu_freq > 0) && |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 716 | (requested_state != KGSL_STATE_NAP)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 717 | clk_set_rate(pwr->grp_clks[0], |
| 718 | pwr->pwrlevels[pwr->num_pwrlevels - 1]. |
| 719 | gpu_freq); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 720 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 721 | if (pwr->grp_clks[i]) |
| 722 | clk_unprepare(pwr->grp_clks[i]); |
| 723 | } |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 724 | kgsl_pwrctrl_busy_time(device, true); |
Suman Tatiraju | 426da0d | 2012-09-11 13:03:28 -0700 | [diff] [blame] | 725 | } else if (requested_state == KGSL_STATE_SLEEP) { |
| 726 | /* High latency clock maintenance. */ |
| 727 | if ((pwr->pwrlevels[0].gpu_freq > 0)) |
| 728 | clk_set_rate(pwr->grp_clks[0], |
| 729 | pwr->pwrlevels[pwr->num_pwrlevels - 1]. |
| 730 | gpu_freq); |
| 731 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 732 | if (pwr->grp_clks[i]) |
| 733 | clk_unprepare(pwr->grp_clks[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 734 | } |
| 735 | } else if (state == KGSL_PWRFLAGS_ON) { |
| 736 | if (!test_and_set_bit(KGSL_PWRFLAGS_CLK_ON, |
| 737 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 738 | trace_kgsl_clk(device, state); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 739 | /* High latency clock maintenance. */ |
Ranjhith Kalisamy | c940699 | 2012-08-10 16:40:36 +0530 | [diff] [blame] | 740 | if (device->state != KGSL_STATE_NAP) { |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 741 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 742 | if (pwr->grp_clks[i]) |
| 743 | clk_prepare(pwr->grp_clks[i]); |
Ranjhith Kalisamy | c940699 | 2012-08-10 16:40:36 +0530 | [diff] [blame] | 744 | |
| 745 | if (pwr->pwrlevels[0].gpu_freq > 0) |
| 746 | clk_set_rate(pwr->grp_clks[0], |
| 747 | pwr->pwrlevels |
| 748 | [pwr->active_pwrlevel]. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 749 | gpu_freq); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 750 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 751 | /* as last step, enable grp_clk |
| 752 | this is to let GPU interrupt to come */ |
| 753 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 754 | if (pwr->grp_clks[i]) |
| 755 | clk_enable(pwr->grp_clks[i]); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 756 | kgsl_pwrctrl_busy_time(device, false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 760 | |
| 761 | void kgsl_pwrctrl_axi(struct kgsl_device *device, int state) |
| 762 | { |
| 763 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 764 | |
| 765 | if (state == KGSL_PWRFLAGS_OFF) { |
| 766 | if (test_and_clear_bit(KGSL_PWRFLAGS_AXI_ON, |
| 767 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 768 | trace_kgsl_bus(device, state); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 769 | if (pwr->ebi1_clk) { |
| 770 | clk_set_rate(pwr->ebi1_clk, 0); |
Lucille Sylvester | 064d598 | 2012-05-08 15:42:43 -0600 | [diff] [blame] | 771 | clk_disable_unprepare(pwr->ebi1_clk); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 772 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 773 | if (pwr->pcl) |
| 774 | msm_bus_scale_client_update_request(pwr->pcl, |
| 775 | 0); |
| 776 | } |
| 777 | } else if (state == KGSL_PWRFLAGS_ON) { |
| 778 | if (!test_and_set_bit(KGSL_PWRFLAGS_AXI_ON, |
| 779 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 780 | trace_kgsl_bus(device, state); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 781 | if (pwr->ebi1_clk) { |
Lucille Sylvester | 064d598 | 2012-05-08 15:42:43 -0600 | [diff] [blame] | 782 | clk_prepare_enable(pwr->ebi1_clk); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 783 | clk_set_rate(pwr->ebi1_clk, |
| 784 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 785 | bus_freq); |
| 786 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 787 | if (pwr->pcl) |
| 788 | msm_bus_scale_client_update_request(pwr->pcl, |
| 789 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 790 | bus_freq); |
| 791 | } |
| 792 | } |
| 793 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 794 | |
| 795 | void kgsl_pwrctrl_pwrrail(struct kgsl_device *device, int state) |
| 796 | { |
| 797 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 798 | |
| 799 | if (state == KGSL_PWRFLAGS_OFF) { |
| 800 | if (test_and_clear_bit(KGSL_PWRFLAGS_POWER_ON, |
| 801 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 802 | trace_kgsl_rail(device, state); |
Pu Chen | 1205378 | 2012-07-24 17:04:27 -0700 | [diff] [blame] | 803 | if (pwr->gpu_cx) |
| 804 | regulator_disable(pwr->gpu_cx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 805 | if (pwr->gpu_reg) |
| 806 | regulator_disable(pwr->gpu_reg); |
| 807 | } |
| 808 | } else if (state == KGSL_PWRFLAGS_ON) { |
| 809 | if (!test_and_set_bit(KGSL_PWRFLAGS_POWER_ON, |
| 810 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 811 | trace_kgsl_rail(device, state); |
Shubhraprakash Das | e86ba5c | 2012-04-04 18:03:27 -0600 | [diff] [blame] | 812 | if (pwr->gpu_reg) { |
| 813 | int status = regulator_enable(pwr->gpu_reg); |
| 814 | if (status) |
Pu Chen | fe0dd3a | 2012-06-01 14:39:08 -0700 | [diff] [blame] | 815 | KGSL_DRV_ERR(device, |
| 816 | "core regulator_enable " |
| 817 | "failed: %d\n", |
| 818 | status); |
| 819 | } |
Pu Chen | 1205378 | 2012-07-24 17:04:27 -0700 | [diff] [blame] | 820 | if (pwr->gpu_cx) { |
| 821 | int status = regulator_enable(pwr->gpu_cx); |
Pu Chen | fe0dd3a | 2012-06-01 14:39:08 -0700 | [diff] [blame] | 822 | if (status) |
| 823 | KGSL_DRV_ERR(device, |
| 824 | "cx regulator_enable " |
| 825 | "failed: %d\n", |
| 826 | status); |
Shubhraprakash Das | e86ba5c | 2012-04-04 18:03:27 -0600 | [diff] [blame] | 827 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 831 | |
| 832 | void kgsl_pwrctrl_irq(struct kgsl_device *device, int state) |
| 833 | { |
| 834 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 835 | |
| 836 | if (state == KGSL_PWRFLAGS_ON) { |
| 837 | if (!test_and_set_bit(KGSL_PWRFLAGS_IRQ_ON, |
| 838 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 839 | trace_kgsl_irq(device, state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 840 | enable_irq(pwr->interrupt_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 841 | } |
| 842 | } else if (state == KGSL_PWRFLAGS_OFF) { |
| 843 | if (test_and_clear_bit(KGSL_PWRFLAGS_IRQ_ON, |
| 844 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 845 | trace_kgsl_irq(device, state); |
Jordan Crouse | b58e61b | 2011-08-08 13:25:36 -0600 | [diff] [blame] | 846 | if (in_interrupt()) |
| 847 | disable_irq_nosync(pwr->interrupt_num); |
| 848 | else |
| 849 | disable_irq(pwr->interrupt_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | } |
| 853 | EXPORT_SYMBOL(kgsl_pwrctrl_irq); |
| 854 | |
| 855 | int kgsl_pwrctrl_init(struct kgsl_device *device) |
| 856 | { |
| 857 | int i, result = 0; |
| 858 | struct clk *clk; |
| 859 | struct platform_device *pdev = |
| 860 | container_of(device->parentdev, struct platform_device, dev); |
| 861 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 862 | struct kgsl_device_platform_data *pdata = pdev->dev.platform_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 863 | |
| 864 | /*acquire clocks */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 865 | for (i = 0; i < KGSL_MAX_CLKS; i++) { |
| 866 | if (pdata->clk_map & clks[i].map) { |
| 867 | clk = clk_get(&pdev->dev, clks[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 868 | if (IS_ERR(clk)) |
| 869 | goto clk_err; |
| 870 | pwr->grp_clks[i] = clk; |
| 871 | } |
| 872 | } |
| 873 | /* Make sure we have a source clk for freq setting */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 874 | if (pwr->grp_clks[0] == NULL) |
| 875 | pwr->grp_clks[0] = pwr->grp_clks[1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 876 | |
| 877 | /* put the AXI bus into asynchronous mode with the graphics cores */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 878 | if (pdata->set_grp_async != NULL) |
| 879 | pdata->set_grp_async(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 880 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 881 | if (pdata->num_levels > KGSL_MAX_PWRLEVELS) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 882 | KGSL_PWR_ERR(device, "invalid power level count: %d\n", |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 883 | pdata->num_levels); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 884 | result = -EINVAL; |
| 885 | goto done; |
| 886 | } |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 887 | pwr->num_pwrlevels = pdata->num_levels; |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 888 | |
| 889 | /* Initialize the user and thermal clock constraints */ |
| 890 | |
| 891 | pwr->max_pwrlevel = 0; |
| 892 | pwr->min_pwrlevel = pdata->num_levels - 2; |
| 893 | pwr->thermal_pwrlevel = 0; |
| 894 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 895 | pwr->active_pwrlevel = pdata->init_level; |
Lucille Sylvester | 67b4c53 | 2012-02-08 11:24:31 -0800 | [diff] [blame] | 896 | pwr->default_pwrlevel = pdata->init_level; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 897 | for (i = 0; i < pdata->num_levels; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 898 | pwr->pwrlevels[i].gpu_freq = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 899 | (pdata->pwrlevel[i].gpu_freq > 0) ? |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 900 | clk_round_rate(pwr->grp_clks[0], |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 901 | pdata->pwrlevel[i]. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 902 | gpu_freq) : 0; |
| 903 | pwr->pwrlevels[i].bus_freq = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 904 | pdata->pwrlevel[i].bus_freq; |
Lucille Sylvester | 596d4c2 | 2011-10-19 18:04:01 -0600 | [diff] [blame] | 905 | pwr->pwrlevels[i].io_fraction = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 906 | pdata->pwrlevel[i].io_fraction; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 907 | } |
| 908 | /* Do not set_rate for targets in sync with AXI */ |
| 909 | if (pwr->pwrlevels[0].gpu_freq > 0) |
| 910 | clk_set_rate(pwr->grp_clks[0], pwr-> |
| 911 | pwrlevels[pwr->num_pwrlevels - 1].gpu_freq); |
| 912 | |
Matt Wagantall | d6fbf23 | 2012-05-03 20:09:28 -0700 | [diff] [blame] | 913 | pwr->gpu_reg = regulator_get(&pdev->dev, "vdd"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 914 | if (IS_ERR(pwr->gpu_reg)) |
| 915 | pwr->gpu_reg = NULL; |
| 916 | |
Pu Chen | fe0dd3a | 2012-06-01 14:39:08 -0700 | [diff] [blame] | 917 | if (pwr->gpu_reg) { |
Pu Chen | 1205378 | 2012-07-24 17:04:27 -0700 | [diff] [blame] | 918 | pwr->gpu_cx = regulator_get(&pdev->dev, "vddcx"); |
| 919 | if (IS_ERR(pwr->gpu_cx)) |
| 920 | pwr->gpu_cx = NULL; |
Pu Chen | fe0dd3a | 2012-06-01 14:39:08 -0700 | [diff] [blame] | 921 | } else |
Pu Chen | 1205378 | 2012-07-24 17:04:27 -0700 | [diff] [blame] | 922 | pwr->gpu_cx = NULL; |
Pu Chen | fe0dd3a | 2012-06-01 14:39:08 -0700 | [diff] [blame] | 923 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 924 | pwr->power_flags = 0; |
| 925 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 926 | pwr->nap_allowed = pdata->nap_allowed; |
Kedar Joshi | c11d098 | 2012-02-07 10:59:49 +0530 | [diff] [blame] | 927 | pwr->idle_needed = pdata->idle_needed; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 928 | pwr->interval_timeout = pdata->idle_timeout; |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 929 | pwr->strtstp_sleepwake = pdata->strtstp_sleepwake; |
Matt Wagantall | 9dc0163 | 2011-08-17 18:55:04 -0700 | [diff] [blame] | 930 | pwr->ebi1_clk = clk_get(&pdev->dev, "bus_clk"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 931 | if (IS_ERR(pwr->ebi1_clk)) |
| 932 | pwr->ebi1_clk = NULL; |
| 933 | else |
| 934 | clk_set_rate(pwr->ebi1_clk, |
| 935 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 936 | bus_freq); |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 937 | if (pdata->bus_scale_table != NULL) { |
| 938 | pwr->pcl = msm_bus_scale_register_client(pdata-> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | bus_scale_table); |
| 940 | if (!pwr->pcl) { |
| 941 | KGSL_PWR_ERR(device, |
| 942 | "msm_bus_scale_register_client failed: " |
| 943 | "id %d table %p", device->id, |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 944 | pdata->bus_scale_table); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 945 | result = -EINVAL; |
| 946 | goto done; |
| 947 | } |
| 948 | } |
| 949 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 950 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 951 | pm_runtime_enable(device->parentdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 952 | register_early_suspend(&device->display_off); |
| 953 | return result; |
| 954 | |
| 955 | clk_err: |
| 956 | result = PTR_ERR(clk); |
| 957 | KGSL_PWR_ERR(device, "clk_get(%s) failed: %d\n", |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 958 | clks[i].name, result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 959 | |
| 960 | done: |
| 961 | return result; |
| 962 | } |
| 963 | |
| 964 | void kgsl_pwrctrl_close(struct kgsl_device *device) |
| 965 | { |
| 966 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 967 | int i; |
| 968 | |
| 969 | KGSL_PWR_INFO(device, "close device %d\n", device->id); |
| 970 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 971 | pm_runtime_disable(device->parentdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 972 | unregister_early_suspend(&device->display_off); |
| 973 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 974 | clk_put(pwr->ebi1_clk); |
| 975 | |
| 976 | if (pwr->pcl) |
| 977 | msm_bus_scale_unregister_client(pwr->pcl); |
| 978 | |
| 979 | pwr->pcl = 0; |
| 980 | |
| 981 | if (pwr->gpu_reg) { |
| 982 | regulator_put(pwr->gpu_reg); |
| 983 | pwr->gpu_reg = NULL; |
| 984 | } |
| 985 | |
Pu Chen | 1205378 | 2012-07-24 17:04:27 -0700 | [diff] [blame] | 986 | if (pwr->gpu_cx) { |
| 987 | regulator_put(pwr->gpu_cx); |
| 988 | pwr->gpu_cx = NULL; |
Pu Chen | fe0dd3a | 2012-06-01 14:39:08 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 991 | for (i = 1; i < KGSL_MAX_CLKS; i++) |
| 992 | if (pwr->grp_clks[i]) { |
| 993 | clk_put(pwr->grp_clks[i]); |
| 994 | pwr->grp_clks[i] = NULL; |
| 995 | } |
| 996 | |
| 997 | pwr->grp_clks[0] = NULL; |
| 998 | pwr->power_flags = 0; |
| 999 | } |
| 1000 | |
| 1001 | void kgsl_idle_check(struct work_struct *work) |
| 1002 | { |
| 1003 | struct kgsl_device *device = container_of(work, struct kgsl_device, |
| 1004 | idle_check_ws); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1005 | WARN_ON(device == NULL); |
| 1006 | if (device == NULL) |
| 1007 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1008 | |
| 1009 | mutex_lock(&device->mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1010 | if (device->state & (KGSL_STATE_ACTIVE | KGSL_STATE_NAP)) { |
Suman Tatiraju | 3de11cc | 2012-06-20 10:33:32 -0700 | [diff] [blame] | 1011 | kgsl_pwrscale_idle(device, 0); |
Lucille Sylvester | c4af355 | 2011-10-27 11:44:24 -0600 | [diff] [blame] | 1012 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 1013 | if (kgsl_pwrctrl_sleep(device) != 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1014 | mod_timer(&device->idle_timer, |
| 1015 | jiffies + |
| 1016 | device->pwrctrl.interval_timeout); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 1017 | /* If the GPU has been too busy to sleep, make sure * |
| 1018 | * that is acurately reflected in the % busy numbers. */ |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 1019 | device->pwrctrl.clk_stats.no_nap_cnt++; |
| 1020 | if (device->pwrctrl.clk_stats.no_nap_cnt > |
| 1021 | UPDATE_BUSY) { |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 1022 | kgsl_pwrctrl_busy_time(device, true); |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 1023 | device->pwrctrl.clk_stats.no_nap_cnt = 0; |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 1024 | } |
| 1025 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1026 | } else if (device->state & (KGSL_STATE_HUNG | |
| 1027 | KGSL_STATE_DUMP_AND_RECOVER)) { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1028 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | mutex_unlock(&device->mutex); |
| 1032 | } |
| 1033 | |
| 1034 | void kgsl_timer(unsigned long data) |
| 1035 | { |
| 1036 | struct kgsl_device *device = (struct kgsl_device *) data; |
| 1037 | |
| 1038 | KGSL_PWR_INFO(device, "idle timer expired device %d\n", device->id); |
Anoop Kumar Yerukala | 03ba25f | 2012-01-23 17:32:02 +0530 | [diff] [blame] | 1039 | if (device->requested_state != KGSL_STATE_SUSPEND) { |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 1040 | if (device->pwrctrl.restore_slumber || |
| 1041 | device->pwrctrl.strtstp_sleepwake) |
Lucille Sylvester | a985adf | 2012-01-16 11:11:55 -0700 | [diff] [blame] | 1042 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER); |
| 1043 | else |
| 1044 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLEEP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1045 | /* Have work run in a non-interrupt context. */ |
| 1046 | queue_work(device->work_queue, &device->idle_check_ws); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | void kgsl_pre_hwaccess(struct kgsl_device *device) |
| 1051 | { |
| 1052 | BUG_ON(!mutex_is_locked(&device->mutex)); |
Lucille Sylvester | 8b803e9 | 2012-01-12 15:19:55 -0700 | [diff] [blame] | 1053 | switch (device->state) { |
| 1054 | case KGSL_STATE_ACTIVE: |
| 1055 | return; |
| 1056 | case KGSL_STATE_NAP: |
| 1057 | case KGSL_STATE_SLEEP: |
| 1058 | case KGSL_STATE_SLUMBER: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1059 | kgsl_pwrctrl_wake(device); |
Lucille Sylvester | 8b803e9 | 2012-01-12 15:19:55 -0700 | [diff] [blame] | 1060 | break; |
| 1061 | case KGSL_STATE_SUSPEND: |
| 1062 | kgsl_check_suspended(device); |
| 1063 | break; |
| 1064 | case KGSL_STATE_INIT: |
| 1065 | case KGSL_STATE_HUNG: |
| 1066 | case KGSL_STATE_DUMP_AND_RECOVER: |
| 1067 | if (test_bit(KGSL_PWRFLAGS_CLK_ON, |
| 1068 | &device->pwrctrl.power_flags)) |
| 1069 | break; |
| 1070 | else |
| 1071 | KGSL_PWR_ERR(device, |
| 1072 | "hw access while clocks off from state %d\n", |
| 1073 | device->state); |
| 1074 | break; |
| 1075 | default: |
| 1076 | KGSL_PWR_ERR(device, "hw access while in unknown state %d\n", |
| 1077 | device->state); |
| 1078 | break; |
| 1079 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1080 | } |
| 1081 | EXPORT_SYMBOL(kgsl_pre_hwaccess); |
| 1082 | |
| 1083 | void kgsl_check_suspended(struct kgsl_device *device) |
| 1084 | { |
| 1085 | if (device->requested_state == KGSL_STATE_SUSPEND || |
| 1086 | device->state == KGSL_STATE_SUSPEND) { |
| 1087 | mutex_unlock(&device->mutex); |
| 1088 | wait_for_completion(&device->hwaccess_gate); |
| 1089 | mutex_lock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1090 | } else if (device->state == KGSL_STATE_DUMP_AND_RECOVER) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1091 | mutex_unlock(&device->mutex); |
| 1092 | wait_for_completion(&device->recovery_gate); |
| 1093 | mutex_lock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1094 | } else if (device->state == KGSL_STATE_SLUMBER) |
| 1095 | kgsl_pwrctrl_wake(device); |
| 1096 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1097 | |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1098 | static int |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1099 | _nap(struct kgsl_device *device) |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1100 | { |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1101 | switch (device->state) { |
| 1102 | case KGSL_STATE_ACTIVE: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1103 | if (!device->ftbl->isidle(device)) { |
| 1104 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 1105 | return -EBUSY; |
| 1106 | } |
| 1107 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 1108 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_NAP); |
| 1109 | kgsl_pwrctrl_set_state(device, KGSL_STATE_NAP); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1110 | case KGSL_STATE_NAP: |
| 1111 | case KGSL_STATE_SLEEP: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1112 | case KGSL_STATE_SLUMBER: |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1113 | break; |
| 1114 | default: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1115 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1116 | break; |
| 1117 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static void |
| 1122 | _sleep_accounting(struct kgsl_device *device) |
| 1123 | { |
| 1124 | kgsl_pwrctrl_busy_time(device, false); |
Suman Tatiraju | 2bdd056 | 2012-01-26 14:49:46 -0800 | [diff] [blame] | 1125 | device->pwrctrl.clk_stats.start = ktime_set(0, 0); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1126 | device->pwrctrl.time = 0; |
| 1127 | kgsl_pwrscale_sleep(device); |
| 1128 | } |
| 1129 | |
| 1130 | static int |
| 1131 | _sleep(struct kgsl_device *device) |
| 1132 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1133 | switch (device->state) { |
| 1134 | case KGSL_STATE_ACTIVE: |
| 1135 | if (!device->ftbl->isidle(device)) { |
| 1136 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 1137 | return -EBUSY; |
| 1138 | } |
| 1139 | /* fall through */ |
| 1140 | case KGSL_STATE_NAP: |
| 1141 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
| 1142 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1143 | _sleep_accounting(device); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 1144 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1145 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLEEP); |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 1146 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 1147 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1148 | break; |
| 1149 | case KGSL_STATE_SLEEP: |
| 1150 | case KGSL_STATE_SLUMBER: |
| 1151 | break; |
| 1152 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 1153 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 1154 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | static int |
| 1161 | _slumber(struct kgsl_device *device) |
| 1162 | { |
| 1163 | switch (device->state) { |
| 1164 | case KGSL_STATE_ACTIVE: |
| 1165 | if (!device->ftbl->isidle(device)) { |
| 1166 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1167 | return -EBUSY; |
| 1168 | } |
| 1169 | /* fall through */ |
| 1170 | case KGSL_STATE_NAP: |
| 1171 | case KGSL_STATE_SLEEP: |
| 1172 | del_timer_sync(&device->idle_timer); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1173 | device->ftbl->suspend_context(device); |
| 1174 | device->ftbl->stop(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1175 | _sleep_accounting(device); |
| 1176 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 1177 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 1178 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1179 | break; |
| 1180 | case KGSL_STATE_SLUMBER: |
| 1181 | break; |
| 1182 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 1183 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 1184 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1185 | break; |
| 1186 | } |
| 1187 | return 0; |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1188 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1189 | |
| 1190 | /******************************************************************/ |
| 1191 | /* Caller must hold the device mutex. */ |
| 1192 | int kgsl_pwrctrl_sleep(struct kgsl_device *device) |
| 1193 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1194 | int status = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1195 | KGSL_PWR_INFO(device, "sleep device %d\n", device->id); |
| 1196 | |
| 1197 | /* Work through the legal state transitions */ |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1198 | switch (device->requested_state) { |
| 1199 | case KGSL_STATE_NAP: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1200 | status = _nap(device); |
| 1201 | break; |
| 1202 | case KGSL_STATE_SLEEP: |
Lucille Sylvester | a985adf | 2012-01-16 11:11:55 -0700 | [diff] [blame] | 1203 | status = _sleep(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | case KGSL_STATE_SLUMBER: |
| 1206 | status = _slumber(device); |
| 1207 | break; |
| 1208 | default: |
| 1209 | KGSL_PWR_INFO(device, "bad state request 0x%x\n", |
| 1210 | device->requested_state); |
| 1211 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 1212 | status = -EINVAL; |
| 1213 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1214 | } |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1215 | return status; |
| 1216 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1217 | EXPORT_SYMBOL(kgsl_pwrctrl_sleep); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 1218 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1219 | /******************************************************************/ |
| 1220 | /* Caller must hold the device mutex. */ |
| 1221 | void kgsl_pwrctrl_wake(struct kgsl_device *device) |
| 1222 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1223 | int status; |
| 1224 | kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE); |
| 1225 | switch (device->state) { |
| 1226 | case KGSL_STATE_SLUMBER: |
| 1227 | status = device->ftbl->start(device, 0); |
| 1228 | if (status) { |
| 1229 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 1230 | KGSL_DRV_ERR(device, "start failed %d\n", status); |
| 1231 | break; |
| 1232 | } |
| 1233 | /* fall through */ |
| 1234 | case KGSL_STATE_SLEEP: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1235 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON); |
| 1236 | kgsl_pwrscale_wake(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1237 | /* fall through */ |
| 1238 | case KGSL_STATE_NAP: |
| 1239 | /* Turn on the core clocks */ |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 1240 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1241 | /* Enable state before turning on irq */ |
| 1242 | kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE); |
| 1243 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON); |
| 1244 | /* Re-enable HW access */ |
| 1245 | mod_timer(&device->idle_timer, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1246 | jiffies + device->pwrctrl.interval_timeout); |
Devin Kim | 66ad4c0 | 2012-09-21 20:28:50 -0700 | [diff] [blame] | 1247 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 1248 | GPU_SWFI_LATENCY); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1249 | case KGSL_STATE_ACTIVE: |
Vinay Roy | 65c41b3 | 2012-11-25 00:48:38 +0530 | [diff] [blame] | 1250 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1251 | break; |
| 1252 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 1253 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 1254 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1255 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 1256 | break; |
| 1257 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1258 | } |
| 1259 | EXPORT_SYMBOL(kgsl_pwrctrl_wake); |
| 1260 | |
| 1261 | void kgsl_pwrctrl_enable(struct kgsl_device *device) |
| 1262 | { |
| 1263 | /* Order pwrrail/clk sequence based upon platform */ |
| 1264 | kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_ON); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 1265 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1266 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON); |
| 1267 | } |
| 1268 | EXPORT_SYMBOL(kgsl_pwrctrl_enable); |
| 1269 | |
| 1270 | void kgsl_pwrctrl_disable(struct kgsl_device *device) |
| 1271 | { |
| 1272 | /* Order pwrrail/clk sequence based upon platform */ |
| 1273 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 1274 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1275 | kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_OFF); |
| 1276 | } |
| 1277 | EXPORT_SYMBOL(kgsl_pwrctrl_disable); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1278 | |
| 1279 | void kgsl_pwrctrl_set_state(struct kgsl_device *device, unsigned int state) |
| 1280 | { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 1281 | trace_kgsl_pwr_set_state(device, state); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1282 | device->state = state; |
| 1283 | device->requested_state = KGSL_STATE_NONE; |
| 1284 | } |
| 1285 | EXPORT_SYMBOL(kgsl_pwrctrl_set_state); |
| 1286 | |
| 1287 | void kgsl_pwrctrl_request_state(struct kgsl_device *device, unsigned int state) |
| 1288 | { |
| 1289 | if (state != KGSL_STATE_NONE && state != device->requested_state) |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 1290 | trace_kgsl_pwr_request_state(device, state); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 1291 | device->requested_state = state; |
| 1292 | } |
| 1293 | EXPORT_SYMBOL(kgsl_pwrctrl_request_state); |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 1294 | |
| 1295 | const char *kgsl_pwrstate_to_str(unsigned int state) |
| 1296 | { |
| 1297 | switch (state) { |
| 1298 | case KGSL_STATE_NONE: |
| 1299 | return "NONE"; |
| 1300 | case KGSL_STATE_INIT: |
| 1301 | return "INIT"; |
| 1302 | case KGSL_STATE_ACTIVE: |
| 1303 | return "ACTIVE"; |
| 1304 | case KGSL_STATE_NAP: |
| 1305 | return "NAP"; |
| 1306 | case KGSL_STATE_SLEEP: |
| 1307 | return "SLEEP"; |
| 1308 | case KGSL_STATE_SUSPEND: |
| 1309 | return "SUSPEND"; |
| 1310 | case KGSL_STATE_HUNG: |
| 1311 | return "HUNG"; |
| 1312 | case KGSL_STATE_DUMP_AND_RECOVER: |
| 1313 | return "DNR"; |
| 1314 | case KGSL_STATE_SLUMBER: |
| 1315 | return "SLUMBER"; |
| 1316 | default: |
| 1317 | break; |
| 1318 | } |
| 1319 | return "UNKNOWN"; |
| 1320 | } |
| 1321 | EXPORT_SYMBOL(kgsl_pwrstate_to_str); |
| 1322 | |