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