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