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); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 446 | if (pwr->gpu_reg) |
| 447 | regulator_enable(pwr->gpu_reg); |
| 448 | } |
| 449 | } |
| 450 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 451 | |
| 452 | void kgsl_pwrctrl_irq(struct kgsl_device *device, int state) |
| 453 | { |
| 454 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 455 | |
| 456 | if (state == KGSL_PWRFLAGS_ON) { |
| 457 | if (!test_and_set_bit(KGSL_PWRFLAGS_IRQ_ON, |
| 458 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 459 | trace_kgsl_irq(device, state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 460 | enable_irq(pwr->interrupt_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 461 | } |
| 462 | } else if (state == KGSL_PWRFLAGS_OFF) { |
| 463 | if (test_and_clear_bit(KGSL_PWRFLAGS_IRQ_ON, |
| 464 | &pwr->power_flags)) { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 465 | trace_kgsl_irq(device, state); |
Jordan Crouse | b58e61b | 2011-08-08 13:25:36 -0600 | [diff] [blame] | 466 | if (in_interrupt()) |
| 467 | disable_irq_nosync(pwr->interrupt_num); |
| 468 | else |
| 469 | disable_irq(pwr->interrupt_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } |
| 473 | EXPORT_SYMBOL(kgsl_pwrctrl_irq); |
| 474 | |
| 475 | int kgsl_pwrctrl_init(struct kgsl_device *device) |
| 476 | { |
| 477 | int i, result = 0; |
| 478 | struct clk *clk; |
| 479 | struct platform_device *pdev = |
| 480 | container_of(device->parentdev, struct platform_device, dev); |
| 481 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 482 | struct kgsl_device_platform_data *pdata = pdev->dev.platform_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 483 | |
| 484 | /*acquire clocks */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 485 | for (i = 0; i < KGSL_MAX_CLKS; i++) { |
| 486 | if (pdata->clk_map & clks[i].map) { |
| 487 | clk = clk_get(&pdev->dev, clks[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 488 | if (IS_ERR(clk)) |
| 489 | goto clk_err; |
| 490 | pwr->grp_clks[i] = clk; |
| 491 | } |
| 492 | } |
| 493 | /* Make sure we have a source clk for freq setting */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 494 | if (pwr->grp_clks[0] == NULL) |
| 495 | pwr->grp_clks[0] = pwr->grp_clks[1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 496 | |
| 497 | /* put the AXI bus into asynchronous mode with the graphics cores */ |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 498 | if (pdata->set_grp_async != NULL) |
| 499 | pdata->set_grp_async(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 500 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 501 | if (pdata->num_levels > KGSL_MAX_PWRLEVELS) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 502 | KGSL_PWR_ERR(device, "invalid power level count: %d\n", |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 503 | pdata->num_levels); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 504 | result = -EINVAL; |
| 505 | goto done; |
| 506 | } |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 507 | pwr->num_pwrlevels = pdata->num_levels; |
| 508 | pwr->active_pwrlevel = pdata->init_level; |
Lucille Sylvester | 67b4c53 | 2012-02-08 11:24:31 -0800 | [diff] [blame] | 509 | pwr->default_pwrlevel = pdata->init_level; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 510 | for (i = 0; i < pdata->num_levels; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 511 | pwr->pwrlevels[i].gpu_freq = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 512 | (pdata->pwrlevel[i].gpu_freq > 0) ? |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 513 | clk_round_rate(pwr->grp_clks[0], |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 514 | pdata->pwrlevel[i]. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 515 | gpu_freq) : 0; |
| 516 | pwr->pwrlevels[i].bus_freq = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 517 | pdata->pwrlevel[i].bus_freq; |
Lucille Sylvester | 596d4c2 | 2011-10-19 18:04:01 -0600 | [diff] [blame] | 518 | pwr->pwrlevels[i].io_fraction = |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 519 | pdata->pwrlevel[i].io_fraction; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 520 | } |
| 521 | /* Do not set_rate for targets in sync with AXI */ |
| 522 | if (pwr->pwrlevels[0].gpu_freq > 0) |
| 523 | clk_set_rate(pwr->grp_clks[0], pwr-> |
| 524 | pwrlevels[pwr->num_pwrlevels - 1].gpu_freq); |
| 525 | |
Matt Wagantall | d6fbf23 | 2012-05-03 20:09:28 -0700 | [diff] [blame] | 526 | pwr->gpu_reg = regulator_get(&pdev->dev, "vdd"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 527 | if (IS_ERR(pwr->gpu_reg)) |
| 528 | pwr->gpu_reg = NULL; |
| 529 | |
| 530 | pwr->power_flags = 0; |
| 531 | |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 532 | pwr->nap_allowed = pdata->nap_allowed; |
Kedar Joshi | c11d098 | 2012-02-07 10:59:49 +0530 | [diff] [blame] | 533 | pwr->idle_needed = pdata->idle_needed; |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 534 | pwr->interval_timeout = pdata->idle_timeout; |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 535 | pwr->strtstp_sleepwake = pdata->strtstp_sleepwake; |
Matt Wagantall | 9dc0163 | 2011-08-17 18:55:04 -0700 | [diff] [blame] | 536 | pwr->ebi1_clk = clk_get(&pdev->dev, "bus_clk"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 537 | if (IS_ERR(pwr->ebi1_clk)) |
| 538 | pwr->ebi1_clk = NULL; |
| 539 | else |
| 540 | clk_set_rate(pwr->ebi1_clk, |
| 541 | pwr->pwrlevels[pwr->active_pwrlevel]. |
| 542 | bus_freq); |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 543 | if (pdata->bus_scale_table != NULL) { |
| 544 | pwr->pcl = msm_bus_scale_register_client(pdata-> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 545 | bus_scale_table); |
| 546 | if (!pwr->pcl) { |
| 547 | KGSL_PWR_ERR(device, |
| 548 | "msm_bus_scale_register_client failed: " |
| 549 | "id %d table %p", device->id, |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 550 | pdata->bus_scale_table); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | result = -EINVAL; |
| 552 | goto done; |
| 553 | } |
| 554 | } |
| 555 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 556 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 557 | pm_runtime_enable(device->parentdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 558 | register_early_suspend(&device->display_off); |
| 559 | return result; |
| 560 | |
| 561 | clk_err: |
| 562 | result = PTR_ERR(clk); |
| 563 | KGSL_PWR_ERR(device, "clk_get(%s) failed: %d\n", |
Lucille Sylvester | dce84cd | 2011-10-12 14:15:37 -0600 | [diff] [blame] | 564 | clks[i].name, result); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 565 | |
| 566 | done: |
| 567 | return result; |
| 568 | } |
| 569 | |
| 570 | void kgsl_pwrctrl_close(struct kgsl_device *device) |
| 571 | { |
| 572 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 573 | int i; |
| 574 | |
| 575 | KGSL_PWR_INFO(device, "close device %d\n", device->id); |
| 576 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 577 | pm_runtime_disable(device->parentdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 578 | unregister_early_suspend(&device->display_off); |
| 579 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | clk_put(pwr->ebi1_clk); |
| 581 | |
| 582 | if (pwr->pcl) |
| 583 | msm_bus_scale_unregister_client(pwr->pcl); |
| 584 | |
| 585 | pwr->pcl = 0; |
| 586 | |
| 587 | if (pwr->gpu_reg) { |
| 588 | regulator_put(pwr->gpu_reg); |
| 589 | pwr->gpu_reg = NULL; |
| 590 | } |
| 591 | |
| 592 | for (i = 1; i < KGSL_MAX_CLKS; i++) |
| 593 | if (pwr->grp_clks[i]) { |
| 594 | clk_put(pwr->grp_clks[i]); |
| 595 | pwr->grp_clks[i] = NULL; |
| 596 | } |
| 597 | |
| 598 | pwr->grp_clks[0] = NULL; |
| 599 | pwr->power_flags = 0; |
| 600 | } |
| 601 | |
| 602 | void kgsl_idle_check(struct work_struct *work) |
| 603 | { |
| 604 | struct kgsl_device *device = container_of(work, struct kgsl_device, |
| 605 | idle_check_ws); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 606 | WARN_ON(device == NULL); |
| 607 | if (device == NULL) |
| 608 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 609 | |
| 610 | mutex_lock(&device->mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | if (device->state & (KGSL_STATE_ACTIVE | KGSL_STATE_NAP)) { |
Lucille Sylvester | 6e36241 | 2011-12-09 16:21:42 -0700 | [diff] [blame] | 612 | kgsl_pwrscale_idle(device); |
Lucille Sylvester | c4af355 | 2011-10-27 11:44:24 -0600 | [diff] [blame] | 613 | |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 614 | if (kgsl_pwrctrl_sleep(device) != 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | mod_timer(&device->idle_timer, |
| 616 | jiffies + |
| 617 | device->pwrctrl.interval_timeout); |
Suman Tatiraju | 7fe62a3 | 2011-07-14 16:40:37 -0700 | [diff] [blame] | 618 | /* If the GPU has been too busy to sleep, make sure * |
| 619 | * that is acurately reflected in the % busy numbers. */ |
| 620 | device->pwrctrl.busy.no_nap_cnt++; |
| 621 | if (device->pwrctrl.busy.no_nap_cnt > UPDATE_BUSY) { |
| 622 | kgsl_pwrctrl_busy_time(device, true); |
| 623 | device->pwrctrl.busy.no_nap_cnt = 0; |
| 624 | } |
| 625 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 626 | } else if (device->state & (KGSL_STATE_HUNG | |
| 627 | KGSL_STATE_DUMP_AND_RECOVER)) { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 628 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | mutex_unlock(&device->mutex); |
| 632 | } |
| 633 | |
| 634 | void kgsl_timer(unsigned long data) |
| 635 | { |
| 636 | struct kgsl_device *device = (struct kgsl_device *) data; |
| 637 | |
| 638 | 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] | 639 | if (device->requested_state != KGSL_STATE_SUSPEND) { |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 640 | if (device->pwrctrl.restore_slumber || |
| 641 | device->pwrctrl.strtstp_sleepwake) |
Lucille Sylvester | a985adf | 2012-01-16 11:11:55 -0700 | [diff] [blame] | 642 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER); |
| 643 | else |
| 644 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLEEP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 645 | /* Have work run in a non-interrupt context. */ |
| 646 | queue_work(device->work_queue, &device->idle_check_ws); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | void kgsl_pre_hwaccess(struct kgsl_device *device) |
| 651 | { |
| 652 | BUG_ON(!mutex_is_locked(&device->mutex)); |
Lucille Sylvester | 8b803e9 | 2012-01-12 15:19:55 -0700 | [diff] [blame] | 653 | switch (device->state) { |
| 654 | case KGSL_STATE_ACTIVE: |
| 655 | return; |
| 656 | case KGSL_STATE_NAP: |
| 657 | case KGSL_STATE_SLEEP: |
| 658 | case KGSL_STATE_SLUMBER: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 659 | kgsl_pwrctrl_wake(device); |
Lucille Sylvester | 8b803e9 | 2012-01-12 15:19:55 -0700 | [diff] [blame] | 660 | break; |
| 661 | case KGSL_STATE_SUSPEND: |
| 662 | kgsl_check_suspended(device); |
| 663 | break; |
| 664 | case KGSL_STATE_INIT: |
| 665 | case KGSL_STATE_HUNG: |
| 666 | case KGSL_STATE_DUMP_AND_RECOVER: |
| 667 | if (test_bit(KGSL_PWRFLAGS_CLK_ON, |
| 668 | &device->pwrctrl.power_flags)) |
| 669 | break; |
| 670 | else |
| 671 | KGSL_PWR_ERR(device, |
| 672 | "hw access while clocks off from state %d\n", |
| 673 | device->state); |
| 674 | break; |
| 675 | default: |
| 676 | KGSL_PWR_ERR(device, "hw access while in unknown state %d\n", |
| 677 | device->state); |
| 678 | break; |
| 679 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 680 | } |
| 681 | EXPORT_SYMBOL(kgsl_pre_hwaccess); |
| 682 | |
| 683 | void kgsl_check_suspended(struct kgsl_device *device) |
| 684 | { |
| 685 | if (device->requested_state == KGSL_STATE_SUSPEND || |
| 686 | device->state == KGSL_STATE_SUSPEND) { |
| 687 | mutex_unlock(&device->mutex); |
| 688 | wait_for_completion(&device->hwaccess_gate); |
| 689 | mutex_lock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 690 | } else if (device->state == KGSL_STATE_DUMP_AND_RECOVER) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 691 | mutex_unlock(&device->mutex); |
| 692 | wait_for_completion(&device->recovery_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_SLUMBER) |
| 695 | kgsl_pwrctrl_wake(device); |
| 696 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 697 | |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 698 | static int |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 699 | _nap(struct kgsl_device *device) |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 700 | { |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 701 | switch (device->state) { |
| 702 | case KGSL_STATE_ACTIVE: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 703 | if (!device->ftbl->isidle(device)) { |
| 704 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 705 | return -EBUSY; |
| 706 | } |
| 707 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 708 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_NAP); |
| 709 | kgsl_pwrctrl_set_state(device, KGSL_STATE_NAP); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 710 | if (device->idle_wakelock.name) |
| 711 | wake_unlock(&device->idle_wakelock); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 712 | case KGSL_STATE_NAP: |
| 713 | case KGSL_STATE_SLEEP: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 714 | case KGSL_STATE_SLUMBER: |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 715 | break; |
| 716 | default: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 717 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 718 | break; |
| 719 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static void |
| 724 | _sleep_accounting(struct kgsl_device *device) |
| 725 | { |
| 726 | kgsl_pwrctrl_busy_time(device, false); |
| 727 | device->pwrctrl.busy.start.tv_sec = 0; |
| 728 | device->pwrctrl.time = 0; |
| 729 | kgsl_pwrscale_sleep(device); |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | _sleep(struct kgsl_device *device) |
| 734 | { |
| 735 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 736 | switch (device->state) { |
| 737 | case KGSL_STATE_ACTIVE: |
| 738 | if (!device->ftbl->isidle(device)) { |
| 739 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 740 | return -EBUSY; |
| 741 | } |
| 742 | /* fall through */ |
| 743 | case KGSL_STATE_NAP: |
| 744 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
| 745 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF); |
| 746 | if (pwr->pwrlevels[0].gpu_freq > 0) |
| 747 | clk_set_rate(pwr->grp_clks[0], |
| 748 | pwr->pwrlevels[pwr->num_pwrlevels - 1]. |
| 749 | gpu_freq); |
| 750 | _sleep_accounting(device); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 751 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 752 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLEEP); |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 753 | wake_unlock(&device->idle_wakelock); |
| 754 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 755 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 756 | break; |
| 757 | case KGSL_STATE_SLEEP: |
| 758 | case KGSL_STATE_SLUMBER: |
| 759 | break; |
| 760 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 761 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 762 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | static int |
| 769 | _slumber(struct kgsl_device *device) |
| 770 | { |
| 771 | switch (device->state) { |
| 772 | case KGSL_STATE_ACTIVE: |
| 773 | if (!device->ftbl->isidle(device)) { |
| 774 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 775 | device->pwrctrl.restore_slumber = true; |
| 776 | return -EBUSY; |
| 777 | } |
| 778 | /* fall through */ |
| 779 | case KGSL_STATE_NAP: |
| 780 | case KGSL_STATE_SLEEP: |
| 781 | del_timer_sync(&device->idle_timer); |
Lynus Vaz | fe4bede | 2012-04-06 11:53:30 -0700 | [diff] [blame] | 782 | if (!device->pwrctrl.strtstp_sleepwake) |
| 783 | kgsl_pwrctrl_pwrlevel_change(device, |
| 784 | KGSL_PWRLEVEL_NOMINAL); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 785 | device->pwrctrl.restore_slumber = true; |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 786 | device->ftbl->suspend_context(device); |
| 787 | device->ftbl->stop(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 788 | _sleep_accounting(device); |
| 789 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER); |
| 790 | if (device->idle_wakelock.name) |
| 791 | wake_unlock(&device->idle_wakelock); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 792 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 793 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 794 | break; |
| 795 | case KGSL_STATE_SLUMBER: |
| 796 | break; |
| 797 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 798 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 799 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | return 0; |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 803 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 804 | |
| 805 | /******************************************************************/ |
| 806 | /* Caller must hold the device mutex. */ |
| 807 | int kgsl_pwrctrl_sleep(struct kgsl_device *device) |
| 808 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 809 | int status = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 810 | KGSL_PWR_INFO(device, "sleep device %d\n", device->id); |
| 811 | |
| 812 | /* Work through the legal state transitions */ |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 813 | switch (device->requested_state) { |
| 814 | case KGSL_STATE_NAP: |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 815 | status = _nap(device); |
| 816 | break; |
| 817 | case KGSL_STATE_SLEEP: |
Lucille Sylvester | a985adf | 2012-01-16 11:11:55 -0700 | [diff] [blame] | 818 | status = _sleep(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 819 | break; |
| 820 | case KGSL_STATE_SLUMBER: |
| 821 | status = _slumber(device); |
| 822 | break; |
| 823 | default: |
| 824 | KGSL_PWR_INFO(device, "bad state request 0x%x\n", |
| 825 | device->requested_state); |
| 826 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 827 | status = -EINVAL; |
| 828 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 829 | } |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 830 | return status; |
| 831 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 832 | EXPORT_SYMBOL(kgsl_pwrctrl_sleep); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 833 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 834 | /******************************************************************/ |
| 835 | /* Caller must hold the device mutex. */ |
| 836 | void kgsl_pwrctrl_wake(struct kgsl_device *device) |
| 837 | { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 838 | int status; |
| 839 | kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE); |
| 840 | switch (device->state) { |
| 841 | case KGSL_STATE_SLUMBER: |
| 842 | status = device->ftbl->start(device, 0); |
| 843 | if (status) { |
| 844 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 845 | KGSL_DRV_ERR(device, "start failed %d\n", status); |
| 846 | break; |
| 847 | } |
| 848 | /* fall through */ |
| 849 | case KGSL_STATE_SLEEP: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 850 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON); |
| 851 | kgsl_pwrscale_wake(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 852 | /* fall through */ |
| 853 | case KGSL_STATE_NAP: |
| 854 | /* Turn on the core clocks */ |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 855 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 856 | /* Enable state before turning on irq */ |
| 857 | kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE); |
| 858 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON); |
| 859 | /* Re-enable HW access */ |
| 860 | mod_timer(&device->idle_timer, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 861 | jiffies + device->pwrctrl.interval_timeout); |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 862 | wake_lock(&device->idle_wakelock); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 863 | if (device->pwrctrl.restore_slumber == false) |
| 864 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 865 | GPU_SWFI_LATENCY); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 866 | case KGSL_STATE_ACTIVE: |
| 867 | break; |
| 868 | default: |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 869 | KGSL_PWR_WARN(device, "unhandled state %s\n", |
| 870 | kgsl_pwrstate_to_str(device->state)); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 871 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
| 872 | break; |
| 873 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 874 | } |
| 875 | EXPORT_SYMBOL(kgsl_pwrctrl_wake); |
| 876 | |
| 877 | void kgsl_pwrctrl_enable(struct kgsl_device *device) |
| 878 | { |
| 879 | /* Order pwrrail/clk sequence based upon platform */ |
| 880 | kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_ON); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 881 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 882 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON); |
| 883 | } |
| 884 | EXPORT_SYMBOL(kgsl_pwrctrl_enable); |
| 885 | |
| 886 | void kgsl_pwrctrl_disable(struct kgsl_device *device) |
| 887 | { |
| 888 | /* Order pwrrail/clk sequence based upon platform */ |
| 889 | kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF); |
Lucille Sylvester | e4a7c1a | 2012-04-11 12:17:38 -0600 | [diff] [blame] | 890 | kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 891 | kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_OFF); |
| 892 | } |
| 893 | EXPORT_SYMBOL(kgsl_pwrctrl_disable); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 894 | |
| 895 | void kgsl_pwrctrl_set_state(struct kgsl_device *device, unsigned int state) |
| 896 | { |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 897 | trace_kgsl_pwr_set_state(device, state); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 898 | device->state = state; |
| 899 | device->requested_state = KGSL_STATE_NONE; |
| 900 | } |
| 901 | EXPORT_SYMBOL(kgsl_pwrctrl_set_state); |
| 902 | |
| 903 | void kgsl_pwrctrl_request_state(struct kgsl_device *device, unsigned int state) |
| 904 | { |
| 905 | if (state != KGSL_STATE_NONE && state != device->requested_state) |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 906 | trace_kgsl_pwr_request_state(device, state); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 907 | device->requested_state = state; |
| 908 | } |
| 909 | EXPORT_SYMBOL(kgsl_pwrctrl_request_state); |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 910 | |
| 911 | const char *kgsl_pwrstate_to_str(unsigned int state) |
| 912 | { |
| 913 | switch (state) { |
| 914 | case KGSL_STATE_NONE: |
| 915 | return "NONE"; |
| 916 | case KGSL_STATE_INIT: |
| 917 | return "INIT"; |
| 918 | case KGSL_STATE_ACTIVE: |
| 919 | return "ACTIVE"; |
| 920 | case KGSL_STATE_NAP: |
| 921 | return "NAP"; |
| 922 | case KGSL_STATE_SLEEP: |
| 923 | return "SLEEP"; |
| 924 | case KGSL_STATE_SUSPEND: |
| 925 | return "SUSPEND"; |
| 926 | case KGSL_STATE_HUNG: |
| 927 | return "HUNG"; |
| 928 | case KGSL_STATE_DUMP_AND_RECOVER: |
| 929 | return "DNR"; |
| 930 | case KGSL_STATE_SLUMBER: |
| 931 | return "SLUMBER"; |
| 932 | default: |
| 933 | break; |
| 934 | } |
| 935 | return "UNKNOWN"; |
| 936 | } |
| 937 | EXPORT_SYMBOL(kgsl_pwrstate_to_str); |
| 938 | |