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 | */ |
| 13 | #include <linux/interrupt.h> |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 14 | #include <linux/pm_runtime.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <mach/msm_iomap.h> |
| 16 | #include <mach/msm_bus.h> |
| 17 | |
| 18 | #include "kgsl.h" |
| 19 | #include "kgsl_pwrscale.h" |
| 20 | #include "kgsl_device.h" |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 21 | #include "kgsl_trace.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | |
Jeremy Gebben | b46f415 | 2011-10-14 14:27:00 -0600 | [diff] [blame] | 23 | #define KGSL_PWRFLAGS_POWER_ON 0 |
| 24 | #define KGSL_PWRFLAGS_CLK_ON 1 |
| 25 | #define KGSL_PWRFLAGS_AXI_ON 2 |
| 26 | #define KGSL_PWRFLAGS_IRQ_ON 3 |
| 27 | |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 28 | #define GPU_SWFI_LATENCY 3 |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 29 | #define UPDATE_BUSY_VAL 1000000 |
| 30 | #define UPDATE_BUSY 50 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 32 | struct clk_pair { |
| 33 | const char *name; |
| 34 | uint map; |
| 35 | }; |
| 36 | |
| 37 | struct clk_pair clks[KGSL_MAX_CLKS] = { |
| 38 | { |
| 39 | .name = "src_clk", |
| 40 | .map = KGSL_CLK_SRC, |
| 41 | }, |
| 42 | { |
| 43 | .name = "core_clk", |
| 44 | .map = KGSL_CLK_CORE, |
| 45 | }, |
| 46 | { |
| 47 | .name = "iface_clk", |
| 48 | .map = KGSL_CLK_IFACE, |
| 49 | }, |
| 50 | { |
| 51 | .name = "mem_clk", |
| 52 | .map = KGSL_CLK_MEM, |
| 53 | }, |
| 54 | { |
| 55 | .name = "mem_iface_clk", |
| 56 | .map = KGSL_CLK_MEM_IFACE, |
| 57 | }, |
| 58 | }; |
| 59 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 60 | void kgsl_pwrctrl_pwrlevel_change(struct kgsl_device *device, |
| 61 | unsigned int new_level) |
| 62 | { |
| 63 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 64 | if (new_level < (pwr->num_pwrlevels - 1) && |
| 65 | new_level >= pwr->thermal_pwrlevel && |
| 66 | new_level != pwr->active_pwrlevel) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 67 | struct kgsl_pwrlevel *pwrlevel = &pwr->pwrlevels[new_level]; |
Lucille Sylvester | 8d2ff3b | 2012-04-12 15:05:51 -0600 | [diff] [blame] | 68 | int diff = new_level - pwr->active_pwrlevel; |
| 69 | int d = (diff > 0) ? 1 : -1; |
| 70 | int level = pwr->active_pwrlevel; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 71 | pwr->active_pwrlevel = new_level; |
Lucille Sylvester | cd42c82 | 2011-09-08 17:37:26 -0600 | [diff] [blame] | 72 | if ((test_bit(KGSL_PWRFLAGS_CLK_ON, &pwr->power_flags)) || |
Kedar Joshi | c11d098 | 2012-02-07 10:59:49 +0530 | [diff] [blame] | 73 | (device->state == KGSL_STATE_NAP)) { |
| 74 | /* |
| 75 | * On some platforms, instability is caused on |
| 76 | * changing clock freq when the core is busy. |
| 77 | * Idle the gpu core before changing the clock freq. |
| 78 | */ |
| 79 | if (pwr->idle_needed == true) |
| 80 | device->ftbl->idle(device, |
| 81 | KGSL_TIMEOUT_DEFAULT); |
Lucille Sylvester | 8d2ff3b | 2012-04-12 15:05:51 -0600 | [diff] [blame] | 82 | /* Don't shift by more than one level at a time to |
| 83 | * avoid glitches. |
| 84 | */ |
| 85 | while (level != new_level) { |
| 86 | level += d; |
| 87 | clk_set_rate(pwr->grp_clks[0], |
| 88 | pwr->pwrlevels[level].gpu_freq); |
| 89 | } |
Kedar Joshi | c11d098 | 2012-02-07 10:59:49 +0530 | [diff] [blame] | 90 | } |
Lucille Sylvester | 622927a | 2011-08-10 14:42:25 -0600 | [diff] [blame] | 91 | if (test_bit(KGSL_PWRFLAGS_AXI_ON, &pwr->power_flags)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | if (pwr->pcl) |
| 93 | msm_bus_scale_client_update_request(pwr->pcl, |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 94 | pwrlevel->bus_freq); |
Lucille Sylvester | 622927a | 2011-08-10 14:42:25 -0600 | [diff] [blame] | 95 | else if (pwr->ebi1_clk) |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 96 | clk_set_rate(pwr->ebi1_clk, pwrlevel->bus_freq); |
Lucille Sylvester | 622927a | 2011-08-10 14:42:25 -0600 | [diff] [blame] | 97 | } |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 98 | trace_kgsl_pwrlevel(device, pwr->active_pwrlevel, |
| 99 | pwrlevel->gpu_freq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | EXPORT_SYMBOL(kgsl_pwrctrl_pwrlevel_change); |
| 103 | |
| 104 | static int __gpuclk_store(int max, struct device *dev, |
| 105 | struct device_attribute *attr, |
| 106 | const char *buf, size_t count) |
| 107 | { int ret, i, delta = 5000000; |
| 108 | unsigned long val; |
| 109 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 110 | struct kgsl_pwrctrl *pwr; |
| 111 | |
| 112 | if (device == NULL) |
| 113 | return 0; |
| 114 | pwr = &device->pwrctrl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 115 | |
| 116 | ret = sscanf(buf, "%ld", &val); |
| 117 | if (ret != 1) |
| 118 | return count; |
| 119 | |
| 120 | mutex_lock(&device->mutex); |
| 121 | for (i = 0; i < pwr->num_pwrlevels; i++) { |
| 122 | if (abs(pwr->pwrlevels[i].gpu_freq - val) < delta) { |
| 123 | if (max) |
| 124 | pwr->thermal_pwrlevel = i; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (i == pwr->num_pwrlevels) |
| 130 | goto done; |
| 131 | |
| 132 | /* |
| 133 | * If the current or requested clock speed is greater than the |
| 134 | * thermal limit, bump down immediately. |
| 135 | */ |
| 136 | |
| 137 | if (pwr->pwrlevels[pwr->active_pwrlevel].gpu_freq > |
| 138 | pwr->pwrlevels[pwr->thermal_pwrlevel].gpu_freq) |
| 139 | kgsl_pwrctrl_pwrlevel_change(device, pwr->thermal_pwrlevel); |
| 140 | else if (!max) |
| 141 | kgsl_pwrctrl_pwrlevel_change(device, i); |
| 142 | |
| 143 | done: |
| 144 | mutex_unlock(&device->mutex); |
| 145 | return count; |
| 146 | } |
| 147 | |
| 148 | static int kgsl_pwrctrl_max_gpuclk_store(struct device *dev, |
| 149 | struct device_attribute *attr, |
| 150 | const char *buf, size_t count) |
| 151 | { |
| 152 | return __gpuclk_store(1, dev, attr, buf, count); |
| 153 | } |
| 154 | |
| 155 | static int kgsl_pwrctrl_max_gpuclk_show(struct device *dev, |
| 156 | struct device_attribute *attr, |
| 157 | char *buf) |
| 158 | { |
| 159 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 160 | struct kgsl_pwrctrl *pwr; |
| 161 | if (device == NULL) |
| 162 | return 0; |
| 163 | pwr = &device->pwrctrl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 164 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 165 | pwr->pwrlevels[pwr->thermal_pwrlevel].gpu_freq); |
| 166 | } |
| 167 | |
| 168 | static int kgsl_pwrctrl_gpuclk_store(struct device *dev, |
| 169 | struct device_attribute *attr, |
| 170 | const char *buf, size_t count) |
| 171 | { |
| 172 | return __gpuclk_store(0, dev, attr, buf, count); |
| 173 | } |
| 174 | |
| 175 | static int kgsl_pwrctrl_gpuclk_show(struct device *dev, |
| 176 | struct device_attribute *attr, |
| 177 | char *buf) |
| 178 | { |
| 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; |
| 181 | if (device == NULL) |
| 182 | return 0; |
| 183 | pwr = &device->pwrctrl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 184 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 185 | pwr->pwrlevels[pwr->active_pwrlevel].gpu_freq); |
| 186 | } |
| 187 | |
| 188 | static int kgsl_pwrctrl_pwrnap_store(struct device *dev, |
| 189 | struct device_attribute *attr, |
| 190 | const char *buf, size_t count) |
| 191 | { |
| 192 | char temp[20]; |
| 193 | unsigned long val; |
| 194 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 195 | struct kgsl_pwrctrl *pwr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | int rc; |
| 197 | |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 198 | if (device == NULL) |
| 199 | return 0; |
| 200 | pwr = &device->pwrctrl; |
| 201 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 202 | snprintf(temp, sizeof(temp), "%.*s", |
| 203 | (int)min(count, sizeof(temp) - 1), buf); |
| 204 | rc = strict_strtoul(temp, 0, &val); |
| 205 | if (rc) |
| 206 | return rc; |
| 207 | |
| 208 | mutex_lock(&device->mutex); |
| 209 | |
| 210 | if (val == 1) |
| 211 | pwr->nap_allowed = true; |
| 212 | else if (val == 0) |
| 213 | pwr->nap_allowed = false; |
| 214 | |
| 215 | mutex_unlock(&device->mutex); |
| 216 | |
| 217 | return count; |
| 218 | } |
| 219 | |
| 220 | static int kgsl_pwrctrl_pwrnap_show(struct device *dev, |
| 221 | struct device_attribute *attr, |
| 222 | char *buf) |
| 223 | { |
| 224 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 225 | if (device == NULL) |
| 226 | return 0; |
| 227 | return snprintf(buf, PAGE_SIZE, "%d\n", device->pwrctrl.nap_allowed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | static int kgsl_pwrctrl_idle_timer_store(struct device *dev, |
| 232 | struct device_attribute *attr, |
| 233 | const char *buf, size_t count) |
| 234 | { |
| 235 | char temp[20]; |
| 236 | unsigned long val; |
| 237 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 238 | struct kgsl_pwrctrl *pwr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 239 | const long div = 1000/HZ; |
| 240 | static unsigned int org_interval_timeout = 1; |
| 241 | int rc; |
| 242 | |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 243 | if (device == NULL) |
| 244 | return 0; |
| 245 | pwr = &device->pwrctrl; |
| 246 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 247 | snprintf(temp, sizeof(temp), "%.*s", |
| 248 | (int)min(count, sizeof(temp) - 1), buf); |
| 249 | rc = strict_strtoul(temp, 0, &val); |
| 250 | if (rc) |
| 251 | return rc; |
| 252 | |
| 253 | if (org_interval_timeout == 1) |
| 254 | org_interval_timeout = pwr->interval_timeout; |
| 255 | |
| 256 | mutex_lock(&device->mutex); |
| 257 | |
| 258 | /* Let the timeout be requested in ms, but convert to jiffies. */ |
| 259 | val /= div; |
| 260 | if (val >= org_interval_timeout) |
| 261 | pwr->interval_timeout = val; |
| 262 | |
| 263 | mutex_unlock(&device->mutex); |
| 264 | |
| 265 | return count; |
| 266 | } |
| 267 | |
| 268 | static int kgsl_pwrctrl_idle_timer_show(struct device *dev, |
| 269 | struct device_attribute *attr, |
| 270 | char *buf) |
| 271 | { |
| 272 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
Jordan Crouse | 6c2992a | 2011-08-08 17:00:06 -0600 | [diff] [blame] | 273 | if (device == NULL) |
| 274 | return 0; |
| 275 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 276 | device->pwrctrl.interval_timeout); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 279 | static int kgsl_pwrctrl_gpubusy_show(struct device *dev, |
| 280 | struct device_attribute *attr, |
| 281 | char *buf) |
| 282 | { |
| 283 | int ret; |
| 284 | struct kgsl_device *device = kgsl_device_from_dev(dev); |
| 285 | struct kgsl_busy *b = &device->pwrctrl.busy; |
| 286 | ret = snprintf(buf, 17, "%7d %7d\n", |
| 287 | b->on_time_old, b->time_old); |
| 288 | if (!test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) { |
| 289 | b->on_time_old = 0; |
| 290 | b->time_old = 0; |
| 291 | } |
| 292 | return ret; |
| 293 | } |
| 294 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 295 | DEVICE_ATTR(gpuclk, 0644, kgsl_pwrctrl_gpuclk_show, kgsl_pwrctrl_gpuclk_store); |
| 296 | DEVICE_ATTR(max_gpuclk, 0644, kgsl_pwrctrl_max_gpuclk_show, |
| 297 | kgsl_pwrctrl_max_gpuclk_store); |
Praveena Pachipulusu | 263467d | 2011-12-22 18:07:16 +0530 | [diff] [blame] | 298 | DEVICE_ATTR(pwrnap, 0664, kgsl_pwrctrl_pwrnap_show, kgsl_pwrctrl_pwrnap_store); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | DEVICE_ATTR(idle_timer, 0644, kgsl_pwrctrl_idle_timer_show, |
| 300 | kgsl_pwrctrl_idle_timer_store); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 301 | DEVICE_ATTR(gpubusy, 0644, kgsl_pwrctrl_gpubusy_show, |
| 302 | NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 303 | |
| 304 | static const struct device_attribute *pwrctrl_attr_list[] = { |
| 305 | &dev_attr_gpuclk, |
| 306 | &dev_attr_max_gpuclk, |
| 307 | &dev_attr_pwrnap, |
| 308 | &dev_attr_idle_timer, |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 309 | &dev_attr_gpubusy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 310 | NULL |
| 311 | }; |
| 312 | |
| 313 | int kgsl_pwrctrl_init_sysfs(struct kgsl_device *device) |
| 314 | { |
| 315 | return kgsl_create_device_sysfs_files(device->dev, pwrctrl_attr_list); |
| 316 | } |
| 317 | |
| 318 | void kgsl_pwrctrl_uninit_sysfs(struct kgsl_device *device) |
| 319 | { |
| 320 | kgsl_remove_device_sysfs_files(device->dev, pwrctrl_attr_list); |
| 321 | } |
| 322 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 323 | /* Track the amount of time the gpu is on vs the total system time. * |
| 324 | * Regularly update the percentage of busy time displayed by sysfs. */ |
| 325 | static void kgsl_pwrctrl_busy_time(struct kgsl_device *device, bool on_time) |
| 326 | { |
| 327 | struct kgsl_busy *b = &device->pwrctrl.busy; |
| 328 | int elapsed; |
| 329 | if (b->start.tv_sec == 0) |
| 330 | do_gettimeofday(&(b->start)); |
| 331 | do_gettimeofday(&(b->stop)); |
| 332 | elapsed = (b->stop.tv_sec - b->start.tv_sec) * 1000000; |
| 333 | elapsed += b->stop.tv_usec - b->start.tv_usec; |
| 334 | b->time += elapsed; |
| 335 | if (on_time) |
| 336 | b->on_time += elapsed; |
| 337 | /* Update the output regularly and reset the counters. */ |
| 338 | if ((b->time > UPDATE_BUSY_VAL) || |
| 339 | !test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) { |
| 340 | b->on_time_old = b->on_time; |
| 341 | b->time_old = b->time; |
| 342 | b->on_time = 0; |
| 343 | b->time = 0; |
| 344 | } |
| 345 | do_gettimeofday(&(b->start)); |
| 346 | } |
| 347 | |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 348 | void kgsl_pwrctrl_clk(struct kgsl_device *device, int state, |
| 349 | int requested_state) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | { |
| 351 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 352 | int i = 0; |
| 353 | if (state == KGSL_PWRFLAGS_OFF) { |
| 354 | if (test_and_clear_bit(KGSL_PWRFLAGS_CLK_ON, |
| 355 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 356 | trace_kgsl_clk(device, state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 357 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 358 | if (pwr->grp_clks[i]) |
| 359 | clk_disable(pwr->grp_clks[i]); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 360 | /* High latency clock maintenance. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 361 | if ((pwr->pwrlevels[0].gpu_freq > 0) && |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 362 | (requested_state != KGSL_STATE_NAP)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 363 | clk_set_rate(pwr->grp_clks[0], |
| 364 | pwr->pwrlevels[pwr->num_pwrlevels - 1]. |
| 365 | gpu_freq); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 366 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 367 | if (pwr->grp_clks[i]) |
| 368 | clk_unprepare(pwr->grp_clks[i]); |
| 369 | } |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 370 | kgsl_pwrctrl_busy_time(device, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 371 | } |
| 372 | } else if (state == KGSL_PWRFLAGS_ON) { |
| 373 | if (!test_and_set_bit(KGSL_PWRFLAGS_CLK_ON, |
| 374 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 375 | trace_kgsl_clk(device, state); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 376 | /* High latency clock maintenance. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 377 | if ((pwr->pwrlevels[0].gpu_freq > 0) && |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 378 | (device->state != KGSL_STATE_NAP)) { |
| 379 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 380 | if (pwr->grp_clks[i]) |
| 381 | clk_prepare(pwr->grp_clks[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | clk_set_rate(pwr->grp_clks[0], |
| 383 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 384 | gpu_freq); |
Lucille Sylvester | 5b7e57b | 2012-01-19 17:18:40 -0700 | [diff] [blame] | 385 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 386 | |
| 387 | /* as last step, enable grp_clk |
| 388 | this is to let GPU interrupt to come */ |
| 389 | for (i = KGSL_MAX_CLKS - 1; i > 0; i--) |
| 390 | if (pwr->grp_clks[i]) |
| 391 | clk_enable(pwr->grp_clks[i]); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 392 | kgsl_pwrctrl_busy_time(device, false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 396 | |
| 397 | void kgsl_pwrctrl_axi(struct kgsl_device *device, int state) |
| 398 | { |
| 399 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 400 | |
| 401 | if (state == KGSL_PWRFLAGS_OFF) { |
| 402 | if (test_and_clear_bit(KGSL_PWRFLAGS_AXI_ON, |
| 403 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 404 | trace_kgsl_bus(device, state); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 405 | if (pwr->ebi1_clk) { |
| 406 | clk_set_rate(pwr->ebi1_clk, 0); |
Lucille Sylvester | 064d598 | 2012-05-08 15:42:43 -0600 | [diff] [blame] | 407 | clk_disable_unprepare(pwr->ebi1_clk); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 408 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 409 | if (pwr->pcl) |
| 410 | msm_bus_scale_client_update_request(pwr->pcl, |
| 411 | 0); |
| 412 | } |
| 413 | } else if (state == KGSL_PWRFLAGS_ON) { |
| 414 | if (!test_and_set_bit(KGSL_PWRFLAGS_AXI_ON, |
| 415 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 416 | trace_kgsl_bus(device, state); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 417 | if (pwr->ebi1_clk) { |
Lucille Sylvester | 064d598 | 2012-05-08 15:42:43 -0600 | [diff] [blame] | 418 | clk_prepare_enable(pwr->ebi1_clk); |
Lynus Vaz | 5a641cc | 2011-09-15 14:43:40 +0530 | [diff] [blame] | 419 | clk_set_rate(pwr->ebi1_clk, |
| 420 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 421 | bus_freq); |
| 422 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | if (pwr->pcl) |
| 424 | msm_bus_scale_client_update_request(pwr->pcl, |
| 425 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 426 | bus_freq); |
| 427 | } |
| 428 | } |
| 429 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 430 | |
| 431 | void kgsl_pwrctrl_pwrrail(struct kgsl_device *device, int state) |
| 432 | { |
| 433 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 434 | |
| 435 | if (state == KGSL_PWRFLAGS_OFF) { |
| 436 | if (test_and_clear_bit(KGSL_PWRFLAGS_POWER_ON, |
| 437 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 438 | trace_kgsl_rail(device, state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 439 | if (pwr->gpu_reg) |
| 440 | regulator_disable(pwr->gpu_reg); |
| 441 | } |
| 442 | } else if (state == KGSL_PWRFLAGS_ON) { |
| 443 | if (!test_and_set_bit(KGSL_PWRFLAGS_POWER_ON, |
| 444 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 445 | trace_kgsl_rail(device, state); |
Shubhraprakash Das | e86ba5c | 2012-04-04 18:03:27 -0600 | [diff] [blame^] | 446 | if (pwr->gpu_reg) { |
| 447 | int status = regulator_enable(pwr->gpu_reg); |
| 448 | if (status) |
| 449 | KGSL_DRV_ERR(device, "regulator_enable " |
| 450 | "failed: %d\n", status); |
| 451 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 455 | |
| 456 | void kgsl_pwrctrl_irq(struct kgsl_device *device, int state) |
| 457 | { |
| 458 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 459 | |
| 460 | if (state == KGSL_PWRFLAGS_ON) { |
| 461 | if (!test_and_set_bit(KGSL_PWRFLAGS_IRQ_ON, |
| 462 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 463 | trace_kgsl_irq(device, state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 464 | enable_irq(pwr->interrupt_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 465 | } |
| 466 | } else if (state == KGSL_PWRFLAGS_OFF) { |
| 467 | if (test_and_clear_bit(KGSL_PWRFLAGS_IRQ_ON, |
| 468 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 469 | trace_kgsl_irq(device, state); |
Jordan Crouse | b58e61b | 2011-08-08 13:25:36 -0600 | [diff] [blame] | 470 | if (in_interrupt()) |
| 471 | disable_irq_nosync(pwr->interrupt_num); |
| 472 | else |
| 473 | disable_irq(pwr->interrupt_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | EXPORT_SYMBOL(kgsl_pwrctrl_irq); |
| 478 | |
| 479 | int kgsl_pwrctrl_init(struct kgsl_device *device) |
| 480 | { |
| 481 | int i, result = 0; |
| 482 | struct clk *clk; |
| 483 | struct platform_device *pdev = |
| 484 | container_of(device->parentdev, struct platform_device, dev); |
| 485 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 486 | struct kgsl_device_platform_data *pdata = pdev->dev.platform_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 487 | |
| 488 | /*acquire clocks */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 489 | for (i = 0; i < KGSL_MAX_CLKS; i++) { |
| 490 | if (pdata->clk_map & clks[i].map) { |
| 491 | clk = clk_get(&pdev->dev, clks[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 492 | if (IS_ERR(clk)) |
| 493 | goto clk_err; |
| 494 | pwr->grp_clks[i] = clk; |
| 495 | } |
| 496 | } |
| 497 | /* Make sure we have a source clk for freq setting */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 498 | if (pwr->grp_clks[0] == NULL) |
| 499 | pwr->grp_clks[0] = pwr->grp_clks[1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 500 | |
| 501 | /* put the AXI bus into asynchronous mode with the graphics cores */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 502 | if (pdata->set_grp_async != NULL) |
| 503 | pdata->set_grp_async(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 504 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 505 | if (pdata->num_levels > KGSL_MAX_PWRLEVELS) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 506 | KGSL_PWR_ERR(device, "invalid power level count: %d\n", |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 507 | pdata->num_levels); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 508 | result = -EINVAL; |
| 509 | goto done; |
| 510 | } |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 511 | pwr->num_pwrlevels = pdata->num_levels; |
| 512 | pwr->active_pwrlevel = pdata->init_level; |
Lucille Sylvester | 67b4c53 | 2012-02-08 11:24:31 -0800 | [diff] [blame] | 513 | pwr->default_pwrlevel = pdata->init_level; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 514 | for (i = 0; i < pdata->num_levels; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 515 | pwr->pwrlevels[i].gpu_freq = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 516 | (pdata->pwrlevel[i].gpu_freq > 0) ? |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | clk_round_rate(pwr->grp_clks[0], |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 518 | pdata->pwrlevel[i]. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 519 | gpu_freq) : 0; |
| 520 | pwr->pwrlevels[i].bus_freq = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 521 | pdata->pwrlevel[i].bus_freq; |
Lucille Sylvester | 596d4c2 | 2011-10-19 18:04:01 -0600 | [diff] [blame] | 522 | pwr->pwrlevels[i].io_fraction = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 523 | pdata->pwrlevel[i].io_fraction; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 524 | } |
| 525 | /* Do not set_rate for targets in sync with AXI */ |
| 526 | if (pwr->pwrlevels[0].gpu_freq > 0) |
| 527 | clk_set_rate(pwr->grp_clks[0], pwr-> |
| 528 | pwrlevels[pwr->num_pwrlevels - 1].gpu_freq); |
| 529 | |
Matt Wagantall | d6fbf23 | 2012-05-03 20:09:28 -0700 | [diff] [blame] | 530 | pwr->gpu_reg = regulator_get(&pdev->dev, "vdd"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 531 | if (IS_ERR(pwr->gpu_reg)) |
| 532 | pwr->gpu_reg = NULL; |
| 533 | |
| 534 | pwr->power_flags = 0; |
| 535 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 536 | pwr->nap_allowed = pdata->nap_allowed; |
Kedar Joshi | c11d098 | 2012-02-07 10:59:49 +0530 | [diff] [blame] | 537 | pwr->idle_needed = pdata->idle_needed; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 538 | pwr->interval_timeout = pdata->idle_timeout; |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 539 | pwr->strtstp_sleepwake = pdata->strtstp_sleepwake; |
Matt Wagantall | 9dc0163 | 2011-08-17 18:55:04 -0700 | [diff] [blame] | 540 | pwr->ebi1_clk = clk_get(&pdev->dev, "bus_clk"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 541 | if (IS_ERR(pwr->ebi1_clk)) |
| 542 | pwr->ebi1_clk = NULL; |
| 543 | else |
| 544 | clk_set_rate(pwr->ebi1_clk, |
| 545 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 546 | bus_freq); |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 547 | if (pdata->bus_scale_table != NULL) { |
| 548 | pwr->pcl = msm_bus_scale_register_client(pdata-> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 549 | bus_scale_table); |
| 550 | if (!pwr->pcl) { |
| 551 | KGSL_PWR_ERR(device, |
| 552 | "msm_bus_scale_register_client failed: " |
| 553 | "id %d table %p", device->id, |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 554 | pdata->bus_scale_table); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | result = -EINVAL; |
| 556 | goto done; |
| 557 | } |
| 558 | } |
| 559 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 560 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 561 | pm_runtime_enable(device->parentdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 562 | register_early_suspend(&device->display_off); |
| 563 | return result; |
| 564 | |
| 565 | clk_err: |
| 566 | result = PTR_ERR(clk); |
| 567 | KGSL_PWR_ERR(device, "clk_get(%s) failed: %d\n", |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 568 | clks[i].name, result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 569 | |
| 570 | done: |
| 571 | return result; |
| 572 | } |
| 573 | |
| 574 | void kgsl_pwrctrl_close(struct kgsl_device *device) |
| 575 | { |
| 576 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 577 | int i; |
| 578 | |
| 579 | KGSL_PWR_INFO(device, "close device %d\n", device->id); |
| 580 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 581 | pm_runtime_disable(device->parentdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 582 | unregister_early_suspend(&device->display_off); |
| 583 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 584 | clk_put(pwr->ebi1_clk); |
| 585 | |
| 586 | if (pwr->pcl) |
| 587 | msm_bus_scale_unregister_client(pwr->pcl); |
| 588 | |
| 589 | pwr->pcl = 0; |
| 590 | |
| 591 | if (pwr->gpu_reg) { |
| 592 | regulator_put(pwr->gpu_reg); |
| 593 | pwr->gpu_reg = NULL; |
| 594 | } |
| 595 | |
| 596 | for (i = 1; i < KGSL_MAX_CLKS; i++) |
| 597 | if (pwr->grp_clks[i]) { |
| 598 | clk_put(pwr->grp_clks[i]); |
| 599 | pwr->grp_clks[i] = NULL; |
| 600 | } |
| 601 | |
| 602 | pwr->grp_clks[0] = NULL; |
| 603 | pwr->power_flags = 0; |
| 604 | } |
| 605 | |
| 606 | void kgsl_idle_check(struct work_struct *work) |
| 607 | { |
| 608 | struct kgsl_device *device = container_of(work, struct kgsl_device, |
| 609 | idle_check_ws); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 610 | WARN_ON(device == NULL); |
| 611 | if (device == NULL) |
| 612 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 613 | |
| 614 | mutex_lock(&device->mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | if (device->state & (KGSL_STATE_ACTIVE | KGSL_STATE_NAP)) { |
Lucille Sylvester | 6e36241 | 2011-12-09 16:21:42 -0700 | [diff] [blame] | 616 | kgsl_pwrscale_idle(device); |
Lucille Sylvester | c4af355 | 2011-10-27 11:44:24 -0600 | [diff] [blame] | 617 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 618 | if (kgsl_pwrctrl_sleep(device) != 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 619 | mod_timer(&device->idle_timer, |
| 620 | jiffies + |
| 621 | device->pwrctrl.interval_timeout); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 622 | /* If the GPU has been too busy to sleep, make sure * |
| 623 | * that is acurately reflected in the % busy numbers. */ |
| 624 | device->pwrctrl.busy.no_nap_cnt++; |
| 625 | if (device->pwrctrl.busy.no_nap_cnt > UPDATE_BUSY) { |
| 626 | kgsl_pwrctrl_busy_time(device, true); |
| 627 | device->pwrctrl.busy.no_nap_cnt = 0; |
| 628 | } |
| 629 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 630 | } else if (device->state & (KGSL_STATE_HUNG | |
| 631 | KGSL_STATE_DUMP_AND_RECOVER)) { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 632 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | mutex_unlock(&device->mutex); |
| 636 | } |
| 637 | |
| 638 | void kgsl_timer(unsigned long data) |
| 639 | { |
| 640 | struct kgsl_device *device = (struct kgsl_device *) data; |
| 641 | |
| 642 | 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] | 643 | if (device->requested_state != KGSL_STATE_SUSPEND) { |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 644 | if (device->pwrctrl.restore_slumber || |
| 645 | device->pwrctrl.strtstp_sleepwake) |
Lucille Sylvester | a985adf | 2012-01-16 11:11:55 -0700 | [diff] [blame] | 646 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER); |
| 647 | else |
| 648 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLEEP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 649 | /* Have work run in a non-interrupt context. */ |
| 650 | queue_work(device->work_queue, &device->idle_check_ws); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | void kgsl_pre_hwaccess(struct kgsl_device *device) |
| 655 | { |
| 656 | BUG_ON(!mutex_is_locked(&device->mutex)); |
Lucille Sylvester | 8b803e9 | 2012-01-12 15:19:55 -0700 | [diff] [blame] | 657 | switch (device->state) { |
| 658 | case KGSL_STATE_ACTIVE: |
| 659 | return; |
| 660 | case KGSL_STATE_NAP: |
| 661 | case KGSL_STATE_SLEEP: |
| 662 | case KGSL_STATE_SLUMBER: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 663 | kgsl_pwrctrl_wake(device); |
Lucille Sylvester | 8b803e9 | 2012-01-12 15:19:55 -0700 | [diff] [blame] | 664 | break; |
| 665 | case KGSL_STATE_SUSPEND: |
| 666 | kgsl_check_suspended(device); |
| 667 | break; |
| 668 | case KGSL_STATE_INIT: |
| 669 | case KGSL_STATE_HUNG: |
| 670 | case KGSL_STATE_DUMP_AND_RECOVER: |
| 671 | if (test_bit(KGSL_PWRFLAGS_CLK_ON, |
| 672 | &device->pwrctrl.power_flags)) |
| 673 | break; |
| 674 | else |
| 675 | KGSL_PWR_ERR(device, |
| 676 | "hw access while clocks off from state %d\n", |
| 677 | device->state); |
| 678 | break; |
| 679 | default: |
| 680 | KGSL_PWR_ERR(device, "hw access while in unknown state %d\n", |
| 681 | device->state); |
| 682 | break; |
| 683 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 684 | } |
| 685 | EXPORT_SYMBOL(kgsl_pre_hwaccess); |
| 686 | |
| 687 | void kgsl_check_suspended(struct kgsl_device *device) |
| 688 | { |
| 689 | if (device->requested_state == KGSL_STATE_SUSPEND || |
| 690 | device->state == KGSL_STATE_SUSPEND) { |
| 691 | mutex_unlock(&device->mutex); |
| 692 | wait_for_completion(&device->hwaccess_gate); |
| 693 | mutex_lock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 694 | } else if (device->state == KGSL_STATE_DUMP_AND_RECOVER) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 695 | mutex_unlock(&device->mutex); |
| 696 | wait_for_completion(&device->recovery_gate); |
| 697 | mutex_lock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 698 | } else if (device->state == KGSL_STATE_SLUMBER) |
| 699 | kgsl_pwrctrl_wake(device); |
| 700 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 701 | |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 702 | static int |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 703 | _nap(struct kgsl_device *device) |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 704 | { |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 705 | switch (device->state) { |
| 706 | case KGSL_STATE_ACTIVE: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 707 | if (!device->ftbl->isidle(device)) { |
| 708 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 709 | return -EBUSY; |
| 710 | } |
| 711 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 712 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_NAP); |
Shubhraprakash Das | c6e2101 | 2012-05-11 17:24:51 -0600 | [diff] [blame] | 713 | kgsl_mmu_disable_clk(&device->mmu); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 714 | kgsl_pwrctrl_set_state(device, KGSL_STATE_NAP); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 715 | if (device->idle_wakelock.name) |
| 716 | wake_unlock(&device->idle_wakelock); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 717 | case KGSL_STATE_NAP: |
| 718 | case KGSL_STATE_SLEEP: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 719 | case KGSL_STATE_SLUMBER: |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 720 | break; |
| 721 | default: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 722 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 723 | break; |
| 724 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static void |
| 729 | _sleep_accounting(struct kgsl_device *device) |
| 730 | { |
| 731 | kgsl_pwrctrl_busy_time(device, false); |
| 732 | device->pwrctrl.busy.start.tv_sec = 0; |
| 733 | device->pwrctrl.time = 0; |
| 734 | kgsl_pwrscale_sleep(device); |
| 735 | } |
| 736 | |
| 737 | static int |
| 738 | _sleep(struct kgsl_device *device) |
| 739 | { |
| 740 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 741 | switch (device->state) { |
| 742 | case KGSL_STATE_ACTIVE: |
| 743 | if (!device->ftbl->isidle(device)) { |
| 744 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 745 | return -EBUSY; |
| 746 | } |
| 747 | /* fall through */ |
| 748 | case KGSL_STATE_NAP: |
| 749 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
| 750 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF); |
| 751 | if (pwr->pwrlevels[0].gpu_freq > 0) |
| 752 | clk_set_rate(pwr->grp_clks[0], |
| 753 | pwr->pwrlevels[pwr->num_pwrlevels - 1]. |
| 754 | gpu_freq); |
| 755 | _sleep_accounting(device); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 756 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP); |
Shubhraprakash Das | c6e2101 | 2012-05-11 17:24:51 -0600 | [diff] [blame] | 757 | kgsl_mmu_disable_clk(&device->mmu); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 758 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLEEP); |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 759 | wake_unlock(&device->idle_wakelock); |
| 760 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 761 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 762 | break; |
| 763 | case KGSL_STATE_SLEEP: |
| 764 | case KGSL_STATE_SLUMBER: |
| 765 | break; |
| 766 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 767 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 768 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int |
| 775 | _slumber(struct kgsl_device *device) |
| 776 | { |
| 777 | switch (device->state) { |
| 778 | case KGSL_STATE_ACTIVE: |
| 779 | if (!device->ftbl->isidle(device)) { |
| 780 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 781 | device->pwrctrl.restore_slumber = true; |
| 782 | return -EBUSY; |
| 783 | } |
| 784 | /* fall through */ |
| 785 | case KGSL_STATE_NAP: |
| 786 | case KGSL_STATE_SLEEP: |
| 787 | del_timer_sync(&device->idle_timer); |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 788 | if (!device->pwrctrl.strtstp_sleepwake) |
| 789 | kgsl_pwrctrl_pwrlevel_change(device, |
| 790 | KGSL_PWRLEVEL_NOMINAL); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 791 | device->pwrctrl.restore_slumber = true; |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 792 | device->ftbl->suspend_context(device); |
| 793 | device->ftbl->stop(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 794 | _sleep_accounting(device); |
| 795 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER); |
| 796 | if (device->idle_wakelock.name) |
| 797 | wake_unlock(&device->idle_wakelock); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 798 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 799 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 800 | break; |
| 801 | case KGSL_STATE_SLUMBER: |
| 802 | break; |
| 803 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 804 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 805 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 806 | break; |
| 807 | } |
| 808 | return 0; |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 809 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 810 | |
| 811 | /******************************************************************/ |
| 812 | /* Caller must hold the device mutex. */ |
| 813 | int kgsl_pwrctrl_sleep(struct kgsl_device *device) |
| 814 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 815 | int status = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 816 | KGSL_PWR_INFO(device, "sleep device %d\n", device->id); |
| 817 | |
| 818 | /* Work through the legal state transitions */ |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 819 | switch (device->requested_state) { |
| 820 | case KGSL_STATE_NAP: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 821 | status = _nap(device); |
| 822 | break; |
| 823 | case KGSL_STATE_SLEEP: |
Lucille Sylvester | a985adf | 2012-01-16 11:11:55 -0700 | [diff] [blame] | 824 | status = _sleep(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 825 | break; |
| 826 | case KGSL_STATE_SLUMBER: |
| 827 | status = _slumber(device); |
| 828 | break; |
| 829 | default: |
| 830 | KGSL_PWR_INFO(device, "bad state request 0x%x\n", |
| 831 | device->requested_state); |
| 832 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 833 | status = -EINVAL; |
| 834 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 835 | } |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 836 | return status; |
| 837 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 838 | EXPORT_SYMBOL(kgsl_pwrctrl_sleep); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 839 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 840 | /******************************************************************/ |
| 841 | /* Caller must hold the device mutex. */ |
| 842 | void kgsl_pwrctrl_wake(struct kgsl_device *device) |
| 843 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 844 | int status; |
| 845 | kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE); |
| 846 | switch (device->state) { |
| 847 | case KGSL_STATE_SLUMBER: |
| 848 | status = device->ftbl->start(device, 0); |
| 849 | if (status) { |
| 850 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 851 | KGSL_DRV_ERR(device, "start failed %d\n", status); |
| 852 | break; |
| 853 | } |
| 854 | /* fall through */ |
| 855 | case KGSL_STATE_SLEEP: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 856 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON); |
| 857 | kgsl_pwrscale_wake(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 858 | /* fall through */ |
| 859 | case KGSL_STATE_NAP: |
| 860 | /* Turn on the core clocks */ |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 861 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 862 | /* Enable state before turning on irq */ |
| 863 | kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE); |
| 864 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON); |
| 865 | /* Re-enable HW access */ |
| 866 | mod_timer(&device->idle_timer, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 867 | jiffies + device->pwrctrl.interval_timeout); |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 868 | wake_lock(&device->idle_wakelock); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 869 | if (device->pwrctrl.restore_slumber == false) |
| 870 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 871 | GPU_SWFI_LATENCY); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 872 | case KGSL_STATE_ACTIVE: |
| 873 | break; |
| 874 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 875 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 876 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 877 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 878 | break; |
| 879 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 880 | } |
| 881 | EXPORT_SYMBOL(kgsl_pwrctrl_wake); |
| 882 | |
| 883 | void kgsl_pwrctrl_enable(struct kgsl_device *device) |
| 884 | { |
| 885 | /* Order pwrrail/clk sequence based upon platform */ |
| 886 | kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_ON); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 887 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 888 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON); |
| 889 | } |
| 890 | EXPORT_SYMBOL(kgsl_pwrctrl_enable); |
| 891 | |
| 892 | void kgsl_pwrctrl_disable(struct kgsl_device *device) |
| 893 | { |
| 894 | /* Order pwrrail/clk sequence based upon platform */ |
| 895 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 896 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP); |
Shubhraprakash Das | c6e2101 | 2012-05-11 17:24:51 -0600 | [diff] [blame] | 897 | kgsl_mmu_disable_clk(&device->mmu); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 898 | kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_OFF); |
| 899 | } |
| 900 | EXPORT_SYMBOL(kgsl_pwrctrl_disable); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 901 | |
| 902 | void kgsl_pwrctrl_set_state(struct kgsl_device *device, unsigned int state) |
| 903 | { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 904 | trace_kgsl_pwr_set_state(device, state); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 905 | device->state = state; |
| 906 | device->requested_state = KGSL_STATE_NONE; |
| 907 | } |
| 908 | EXPORT_SYMBOL(kgsl_pwrctrl_set_state); |
| 909 | |
| 910 | void kgsl_pwrctrl_request_state(struct kgsl_device *device, unsigned int state) |
| 911 | { |
| 912 | if (state != KGSL_STATE_NONE && state != device->requested_state) |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 913 | trace_kgsl_pwr_request_state(device, state); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 914 | device->requested_state = state; |
| 915 | } |
| 916 | EXPORT_SYMBOL(kgsl_pwrctrl_request_state); |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 917 | |
| 918 | const char *kgsl_pwrstate_to_str(unsigned int state) |
| 919 | { |
| 920 | switch (state) { |
| 921 | case KGSL_STATE_NONE: |
| 922 | return "NONE"; |
| 923 | case KGSL_STATE_INIT: |
| 924 | return "INIT"; |
| 925 | case KGSL_STATE_ACTIVE: |
| 926 | return "ACTIVE"; |
| 927 | case KGSL_STATE_NAP: |
| 928 | return "NAP"; |
| 929 | case KGSL_STATE_SLEEP: |
| 930 | return "SLEEP"; |
| 931 | case KGSL_STATE_SUSPEND: |
| 932 | return "SUSPEND"; |
| 933 | case KGSL_STATE_HUNG: |
| 934 | return "HUNG"; |
| 935 | case KGSL_STATE_DUMP_AND_RECOVER: |
| 936 | return "DNR"; |
| 937 | case KGSL_STATE_SLUMBER: |
| 938 | return "SLUMBER"; |
| 939 | default: |
| 940 | break; |
| 941 | } |
| 942 | return "UNKNOWN"; |
| 943 | } |
| 944 | EXPORT_SYMBOL(kgsl_pwrstate_to_str); |
| 945 | |