blob: 3ccc4c22f54c4f1bf0c91551e98a102e2945f29b [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
Steve Mucklef132c6c2012-06-06 18:30:57 -070013
14#include <linux/export.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/interrupt.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070016#include <asm/page.h>
Jeremy Gebben4f5f0de2012-03-01 15:51:37 -070017#include <linux/pm_runtime.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018#include <mach/msm_iomap.h>
19#include <mach/msm_bus.h>
Suman Tatiraju2bdd0562012-01-26 14:49:46 -080020#include <linux/ktime.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021
22#include "kgsl.h"
23#include "kgsl_pwrscale.h"
24#include "kgsl_device.h"
Jeremy Gebbenb50f3312011-12-16 08:58:33 -070025#include "kgsl_trace.h"
Ranjhith Kalisamycb1721c2013-05-28 16:59:59 -060026#include "kgsl_sharedmem.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
Jeremy Gebbenb46f4152011-10-14 14:27:00 -060028#define KGSL_PWRFLAGS_POWER_ON 0
29#define KGSL_PWRFLAGS_CLK_ON 1
30#define KGSL_PWRFLAGS_AXI_ON 2
31#define KGSL_PWRFLAGS_IRQ_ON 3
32
Lucille Sylvester10297892012-02-27 13:54:47 -070033#define GPU_SWFI_LATENCY 3
Suman Tatiraju7fe62a32011-07-14 16:40:37 -070034#define UPDATE_BUSY_VAL 1000000
35#define UPDATE_BUSY 50
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -060037struct clk_pair {
38 const char *name;
39 uint map;
40};
41
42struct clk_pair clks[KGSL_MAX_CLKS] = {
43 {
44 .name = "src_clk",
45 .map = KGSL_CLK_SRC,
46 },
47 {
48 .name = "core_clk",
49 .map = KGSL_CLK_CORE,
50 },
51 {
52 .name = "iface_clk",
53 .map = KGSL_CLK_IFACE,
54 },
55 {
56 .name = "mem_clk",
57 .map = KGSL_CLK_MEM,
58 },
59 {
60 .name = "mem_iface_clk",
61 .map = KGSL_CLK_MEM_IFACE,
62 },
63};
64
Suman Tatiraju2bdd0562012-01-26 14:49:46 -080065/* Update the elapsed time at a particular clock level
66 * if the device is active(on_time = true).Otherwise
67 * store it as sleep time.
68 */
69static void update_clk_statistics(struct kgsl_device *device,
70 bool on_time)
71{
72 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
73 struct kgsl_clk_stats *clkstats = &pwr->clk_stats;
74 ktime_t elapsed;
75 int elapsed_us;
76 if (clkstats->start.tv64 == 0)
77 clkstats->start = ktime_get();
78 clkstats->stop = ktime_get();
79 elapsed = ktime_sub(clkstats->stop, clkstats->start);
80 elapsed_us = ktime_to_us(elapsed);
81 clkstats->elapsed += elapsed_us;
82 if (on_time)
83 clkstats->clock_time[pwr->active_pwrlevel] += elapsed_us;
84 else
85 clkstats->clock_time[pwr->num_pwrlevels - 1] += elapsed_us;
86 clkstats->start = ktime_get();
87}
88
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -070089/*
90 * Given a requested power level do bounds checking on the constraints and
91 * return the nearest possible level
92 */
93
94static inline int _adjust_pwrlevel(struct kgsl_pwrctrl *pwr, int level)
95{
Jordan Crouse00a01ba2012-12-05 15:58:16 -070096 int max_pwrlevel = max_t(int, pwr->thermal_pwrlevel, pwr->max_pwrlevel);
97 int min_pwrlevel = max_t(int, pwr->thermal_pwrlevel, pwr->min_pwrlevel);
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -070098
99 if (level < max_pwrlevel)
100 return max_pwrlevel;
101 if (level > min_pwrlevel)
102 return min_pwrlevel;
103
104 return level;
105}
106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107void kgsl_pwrctrl_pwrlevel_change(struct kgsl_device *device,
108 unsigned int new_level)
109{
110 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700111 struct kgsl_pwrlevel *pwrlevel;
112 int delta;
Jordan Crouse3c337a32012-12-04 16:16:51 -0700113 int level;
Jordan Crousea29a2e02012-08-14 09:09:23 -0600114
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700115 /* Adjust the power level to the current constraints */
116 new_level = _adjust_pwrlevel(pwr, new_level);
117
118 if (new_level == pwr->active_pwrlevel)
119 return;
120
121 delta = new_level < pwr->active_pwrlevel ? -1 : 1;
122
123 update_clk_statistics(device, true);
124
Jordan Crouse3c337a32012-12-04 16:16:51 -0700125 level = pwr->active_pwrlevel;
126
127 /*
128 * Set the active powerlevel first in case the clocks are off - if we
129 * don't do this then the pwrlevel change won't take effect when the
130 * clocks come back
131 */
132
133 pwr->active_pwrlevel = new_level;
134
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700135 if (test_bit(KGSL_PWRFLAGS_CLK_ON, &pwr->power_flags) ||
136 (device->state == KGSL_STATE_NAP)) {
137
138 /*
139 * On some platforms, instability is caused on
140 * changing clock freq when the core is busy.
141 * Idle the gpu core before changing the clock freq.
142 */
143
144 if (pwr->idle_needed == true)
145 device->ftbl->idle(device);
146
147 /*
148 * Don't shift by more than one level at a time to
149 * avoid glitches.
150 */
151
Jordan Crouse3c337a32012-12-04 16:16:51 -0700152 while (level != new_level) {
153 level += delta;
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700154
155 clk_set_rate(pwr->grp_clks[0],
Jordan Crouse3c337a32012-12-04 16:16:51 -0700156 pwr->pwrlevels[level].gpu_freq);
Kedar Joshic11d0982012-02-07 10:59:49 +0530157 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 }
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700159
160 pwrlevel = &pwr->pwrlevels[pwr->active_pwrlevel];
161
162 if (test_bit(KGSL_PWRFLAGS_AXI_ON, &pwr->power_flags)) {
163
164 if (pwr->pcl)
165 msm_bus_scale_client_update_request(pwr->pcl,
166 pwrlevel->bus_freq);
167 else if (pwr->ebi1_clk)
168 clk_set_rate(pwr->ebi1_clk, pwrlevel->bus_freq);
169 }
170
171 trace_kgsl_pwrlevel(device, pwr->active_pwrlevel, pwrlevel->gpu_freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172}
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700173
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174EXPORT_SYMBOL(kgsl_pwrctrl_pwrlevel_change);
175
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700176static int kgsl_pwrctrl_thermal_pwrlevel_store(struct device *dev,
177 struct device_attribute *attr,
178 const char *buf, size_t count)
179{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600181 struct kgsl_pwrctrl *pwr;
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700182 int ret, level;
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600183
184 if (device == NULL)
185 return 0;
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700186
187 pwr = &device->pwrctrl;
188
189 ret = sscanf(buf, "%d", &level);
190 if (ret != 1)
191 return count;
192
193 if (level < 0)
194 return count;
195
196 mutex_lock(&device->mutex);
197
198 if (level > pwr->num_pwrlevels - 2)
199 level = pwr->num_pwrlevels - 2;
200
201 pwr->thermal_pwrlevel = level;
202
203 /*
204 * If there is no power policy set the clock to the requested thermal
205 * level - if thermal now happens to be higher than max, then that will
206 * be limited by the pwrlevel change function. Otherwise if there is
207 * a policy only change the active clock if it is higher then the new
208 * thermal level
209 */
210
211 if (device->pwrscale.policy == NULL ||
212 pwr->thermal_pwrlevel > pwr->active_pwrlevel)
213 kgsl_pwrctrl_pwrlevel_change(device, pwr->thermal_pwrlevel);
214
215 mutex_unlock(&device->mutex);
216
217 return count;
218}
219
220static int kgsl_pwrctrl_thermal_pwrlevel_show(struct device *dev,
221 struct device_attribute *attr,
222 char *buf)
223{
224
225 struct kgsl_device *device = kgsl_device_from_dev(dev);
226 struct kgsl_pwrctrl *pwr;
227 if (device == NULL)
228 return 0;
229 pwr = &device->pwrctrl;
230 return snprintf(buf, PAGE_SIZE, "%d\n", pwr->thermal_pwrlevel);
231}
232
233static int kgsl_pwrctrl_max_pwrlevel_store(struct device *dev,
234 struct device_attribute *attr,
235 const char *buf, size_t count)
236{
237 struct kgsl_device *device = kgsl_device_from_dev(dev);
238 struct kgsl_pwrctrl *pwr;
239 int ret, level, max_level;
240
241 if (device == NULL)
242 return 0;
243
244 pwr = &device->pwrctrl;
245
246 ret = sscanf(buf, "%d", &level);
247 if (ret != 1)
248 return count;
249
250 /* If the use specifies a negative number, then don't change anything */
251 if (level < 0)
252 return count;
253
254 mutex_lock(&device->mutex);
255
256 /* You can't set a maximum power level lower than the minimum */
257 if (level > pwr->min_pwrlevel)
258 level = pwr->min_pwrlevel;
259
260 pwr->max_pwrlevel = level;
261
262
263 max_level = max_t(int, pwr->thermal_pwrlevel, pwr->max_pwrlevel);
264
265 /*
266 * If there is no policy then move to max by default. Otherwise only
267 * move max if the current level happens to be higher then the new max
268 */
269
270 if (device->pwrscale.policy == NULL ||
271 (max_level > pwr->active_pwrlevel))
272 kgsl_pwrctrl_pwrlevel_change(device, max_level);
273
274 mutex_unlock(&device->mutex);
275
276 return count;
277}
278
279static int kgsl_pwrctrl_max_pwrlevel_show(struct device *dev,
280 struct device_attribute *attr,
281 char *buf)
282{
283
284 struct kgsl_device *device = kgsl_device_from_dev(dev);
285 struct kgsl_pwrctrl *pwr;
286 if (device == NULL)
287 return 0;
288 pwr = &device->pwrctrl;
289 return snprintf(buf, PAGE_SIZE, "%d\n", pwr->max_pwrlevel);
290}
291
292static int kgsl_pwrctrl_min_pwrlevel_store(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
295{ struct kgsl_device *device = kgsl_device_from_dev(dev);
296 struct kgsl_pwrctrl *pwr;
297 int ret, level, min_level;
298
299 if (device == NULL)
300 return 0;
301
302 pwr = &device->pwrctrl;
303
304 ret = sscanf(buf, "%d", &level);
305 if (ret != 1)
306 return count;
307
308 /* Don't do anything on obviously incorrect values */
309 if (level < 0)
310 return count;
311
312 mutex_lock(&device->mutex);
313 if (level > pwr->num_pwrlevels - 2)
314 level = pwr->num_pwrlevels - 2;
315
316 /* You can't set a minimum power level lower than the maximum */
317 if (level < pwr->max_pwrlevel)
318 level = pwr->max_pwrlevel;
319
320 pwr->min_pwrlevel = level;
321
322 min_level = max_t(int, pwr->thermal_pwrlevel, pwr->min_pwrlevel);
323
324 /* Only move the power level higher if minimum is higher then the
325 * current level
326 */
327
328 if (min_level < pwr->active_pwrlevel)
329 kgsl_pwrctrl_pwrlevel_change(device, min_level);
330
331 mutex_unlock(&device->mutex);
332
333 return count;
334}
335
336static int kgsl_pwrctrl_min_pwrlevel_show(struct device *dev,
337 struct device_attribute *attr,
338 char *buf)
339{
340 struct kgsl_device *device = kgsl_device_from_dev(dev);
341 struct kgsl_pwrctrl *pwr;
342 if (device == NULL)
343 return 0;
344 pwr = &device->pwrctrl;
345 return snprintf(buf, PAGE_SIZE, "%d\n", pwr->min_pwrlevel);
346}
347
348static int kgsl_pwrctrl_num_pwrlevels_show(struct device *dev,
349 struct device_attribute *attr,
350 char *buf)
351{
352
353 struct kgsl_device *device = kgsl_device_from_dev(dev);
354 struct kgsl_pwrctrl *pwr;
355 if (device == NULL)
356 return 0;
357 pwr = &device->pwrctrl;
358 return snprintf(buf, PAGE_SIZE, "%d\n", pwr->num_pwrlevels - 1);
359}
360
361/* Given a GPU clock value, return the nearest powerlevel */
362
363static int _get_nearest_pwrlevel(struct kgsl_pwrctrl *pwr, unsigned int clock)
364{
365 int i;
366
367 for (i = 0; i < pwr->num_pwrlevels - 1; i++) {
368 if (abs(pwr->pwrlevels[i].gpu_freq - clock) < 5000000)
369 return i;
370 }
371
372 return -ERANGE;
373}
374
375static int kgsl_pwrctrl_max_gpuclk_store(struct device *dev,
376 struct device_attribute *attr,
377 const char *buf, size_t count)
378{
379 struct kgsl_device *device = kgsl_device_from_dev(dev);
380 struct kgsl_pwrctrl *pwr;
381 unsigned long val;
382 int ret, level;
383
384 if (device == NULL)
385 return 0;
386
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600387 pwr = &device->pwrctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388
389 ret = sscanf(buf, "%ld", &val);
390 if (ret != 1)
391 return count;
392
393 mutex_lock(&device->mutex);
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700394 level = _get_nearest_pwrlevel(pwr, val);
395 if (level < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 goto done;
397
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700398 pwr->thermal_pwrlevel = level;
399
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 /*
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700401 * if the thermal limit is lower than the current setting,
402 * move the speed down immediately
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 */
404
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700405 if (pwr->thermal_pwrlevel > pwr->active_pwrlevel)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 kgsl_pwrctrl_pwrlevel_change(device, pwr->thermal_pwrlevel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407
408done:
409 mutex_unlock(&device->mutex);
410 return count;
411}
412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413static int kgsl_pwrctrl_max_gpuclk_show(struct device *dev,
414 struct device_attribute *attr,
415 char *buf)
416{
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700417
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600419 struct kgsl_pwrctrl *pwr;
420 if (device == NULL)
421 return 0;
422 pwr = &device->pwrctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 return snprintf(buf, PAGE_SIZE, "%d\n",
424 pwr->pwrlevels[pwr->thermal_pwrlevel].gpu_freq);
425}
426
427static int kgsl_pwrctrl_gpuclk_store(struct device *dev,
428 struct device_attribute *attr,
429 const char *buf, size_t count)
430{
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700431 struct kgsl_device *device = kgsl_device_from_dev(dev);
432 struct kgsl_pwrctrl *pwr;
433 unsigned long val;
434 int ret, level;
435
436 if (device == NULL)
437 return 0;
438
439 pwr = &device->pwrctrl;
440
441 ret = sscanf(buf, "%ld", &val);
442 if (ret != 1)
443 return count;
444
445 mutex_lock(&device->mutex);
446 level = _get_nearest_pwrlevel(pwr, val);
447 if (level >= 0)
448 kgsl_pwrctrl_pwrlevel_change(device, level);
449
450 mutex_unlock(&device->mutex);
451 return count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452}
453
454static int kgsl_pwrctrl_gpuclk_show(struct device *dev,
455 struct device_attribute *attr,
456 char *buf)
457{
458 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600459 struct kgsl_pwrctrl *pwr;
460 if (device == NULL)
461 return 0;
462 pwr = &device->pwrctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 return snprintf(buf, PAGE_SIZE, "%d\n",
464 pwr->pwrlevels[pwr->active_pwrlevel].gpu_freq);
465}
466
467static int kgsl_pwrctrl_pwrnap_store(struct device *dev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
470{
471 char temp[20];
472 unsigned long val;
473 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600474 struct kgsl_pwrctrl *pwr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 int rc;
476
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600477 if (device == NULL)
478 return 0;
479 pwr = &device->pwrctrl;
480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 snprintf(temp, sizeof(temp), "%.*s",
482 (int)min(count, sizeof(temp) - 1), buf);
483 rc = strict_strtoul(temp, 0, &val);
484 if (rc)
485 return rc;
486
487 mutex_lock(&device->mutex);
488
489 if (val == 1)
490 pwr->nap_allowed = true;
491 else if (val == 0)
492 pwr->nap_allowed = false;
493
494 mutex_unlock(&device->mutex);
495
496 return count;
497}
498
499static int kgsl_pwrctrl_pwrnap_show(struct device *dev,
500 struct device_attribute *attr,
501 char *buf)
502{
503 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600504 if (device == NULL)
505 return 0;
506 return snprintf(buf, PAGE_SIZE, "%d\n", device->pwrctrl.nap_allowed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507}
508
509
510static int kgsl_pwrctrl_idle_timer_store(struct device *dev,
511 struct device_attribute *attr,
512 const char *buf, size_t count)
513{
514 char temp[20];
515 unsigned long val;
516 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600517 struct kgsl_pwrctrl *pwr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 const long div = 1000/HZ;
519 static unsigned int org_interval_timeout = 1;
520 int rc;
521
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600522 if (device == NULL)
523 return 0;
524 pwr = &device->pwrctrl;
525
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 snprintf(temp, sizeof(temp), "%.*s",
527 (int)min(count, sizeof(temp) - 1), buf);
528 rc = strict_strtoul(temp, 0, &val);
529 if (rc)
530 return rc;
531
532 if (org_interval_timeout == 1)
533 org_interval_timeout = pwr->interval_timeout;
534
535 mutex_lock(&device->mutex);
536
537 /* Let the timeout be requested in ms, but convert to jiffies. */
538 val /= div;
539 if (val >= org_interval_timeout)
540 pwr->interval_timeout = val;
541
542 mutex_unlock(&device->mutex);
543
544 return count;
545}
546
547static int kgsl_pwrctrl_idle_timer_show(struct device *dev,
548 struct device_attribute *attr,
549 char *buf)
550{
551 struct kgsl_device *device = kgsl_device_from_dev(dev);
Jordan Crouse6c2992a2011-08-08 17:00:06 -0600552 if (device == NULL)
553 return 0;
554 return snprintf(buf, PAGE_SIZE, "%d\n",
555 device->pwrctrl.interval_timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556}
557
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700558static int kgsl_pwrctrl_gpubusy_show(struct device *dev,
559 struct device_attribute *attr,
560 char *buf)
561{
562 int ret;
563 struct kgsl_device *device = kgsl_device_from_dev(dev);
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800564 struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats;
565 ret = snprintf(buf, PAGE_SIZE, "%7d %7d\n",
566 clkstats->on_time_old, clkstats->elapsed_old);
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700567 if (!test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) {
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800568 clkstats->on_time_old = 0;
569 clkstats->elapsed_old = 0;
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700570 }
571 return ret;
572}
573
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800574static int kgsl_pwrctrl_gputop_show(struct device *dev,
575 struct device_attribute *attr,
576 char *buf)
577{
578 int ret;
579 struct kgsl_device *device = kgsl_device_from_dev(dev);
580 struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats;
581 int i = 0;
582 char *ptr = buf;
583
584 ret = snprintf(buf, PAGE_SIZE, "%7d %7d ", clkstats->on_time_old,
585 clkstats->elapsed_old);
586 for (i = 0, ptr += ret; i < device->pwrctrl.num_pwrlevels;
587 i++, ptr += ret)
588 ret = snprintf(ptr, PAGE_SIZE, "%7d ",
589 clkstats->old_clock_time[i]);
590
591 if (!test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) {
592 clkstats->on_time_old = 0;
593 clkstats->elapsed_old = 0;
594 for (i = 0; i < KGSL_MAX_PWRLEVELS ; i++)
595 clkstats->old_clock_time[i] = 0;
596 }
597 return (unsigned int) (ptr - buf);
598}
599
Anshuman Dani91ede1e2012-08-21 14:44:38 +0530600static int kgsl_pwrctrl_gpu_available_frequencies_show(
601 struct device *dev,
602 struct device_attribute *attr,
603 char *buf)
604{
605 struct kgsl_device *device = kgsl_device_from_dev(dev);
606 struct kgsl_pwrctrl *pwr;
607 int index, num_chars = 0;
608
609 if (device == NULL)
610 return 0;
611 pwr = &device->pwrctrl;
612 for (index = 0; index < pwr->num_pwrlevels - 1; index++)
613 num_chars += snprintf(buf + num_chars, PAGE_SIZE, "%d ",
614 pwr->pwrlevels[index].gpu_freq);
615 buf[num_chars++] = '\n';
616 return num_chars;
617}
618
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619DEVICE_ATTR(gpuclk, 0644, kgsl_pwrctrl_gpuclk_show, kgsl_pwrctrl_gpuclk_store);
620DEVICE_ATTR(max_gpuclk, 0644, kgsl_pwrctrl_max_gpuclk_show,
621 kgsl_pwrctrl_max_gpuclk_store);
Praveena Pachipulusu263467d2011-12-22 18:07:16 +0530622DEVICE_ATTR(pwrnap, 0664, kgsl_pwrctrl_pwrnap_show, kgsl_pwrctrl_pwrnap_store);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623DEVICE_ATTR(idle_timer, 0644, kgsl_pwrctrl_idle_timer_show,
624 kgsl_pwrctrl_idle_timer_store);
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800625DEVICE_ATTR(gpubusy, 0444, kgsl_pwrctrl_gpubusy_show,
626 NULL);
627DEVICE_ATTR(gputop, 0444, kgsl_pwrctrl_gputop_show,
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700628 NULL);
Anshuman Dani91ede1e2012-08-21 14:44:38 +0530629DEVICE_ATTR(gpu_available_frequencies, 0444,
630 kgsl_pwrctrl_gpu_available_frequencies_show,
631 NULL);
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700632DEVICE_ATTR(max_pwrlevel, 0644,
633 kgsl_pwrctrl_max_pwrlevel_show,
634 kgsl_pwrctrl_max_pwrlevel_store);
635DEVICE_ATTR(min_pwrlevel, 0644,
636 kgsl_pwrctrl_min_pwrlevel_show,
637 kgsl_pwrctrl_min_pwrlevel_store);
638DEVICE_ATTR(thermal_pwrlevel, 0644,
639 kgsl_pwrctrl_thermal_pwrlevel_show,
640 kgsl_pwrctrl_thermal_pwrlevel_store);
641DEVICE_ATTR(num_pwrlevels, 0444,
642 kgsl_pwrctrl_num_pwrlevels_show,
643 NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644
645static const struct device_attribute *pwrctrl_attr_list[] = {
646 &dev_attr_gpuclk,
647 &dev_attr_max_gpuclk,
648 &dev_attr_pwrnap,
649 &dev_attr_idle_timer,
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700650 &dev_attr_gpubusy,
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800651 &dev_attr_gputop,
Anshuman Dani91ede1e2012-08-21 14:44:38 +0530652 &dev_attr_gpu_available_frequencies,
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700653 &dev_attr_max_pwrlevel,
654 &dev_attr_min_pwrlevel,
655 &dev_attr_thermal_pwrlevel,
656 &dev_attr_num_pwrlevels,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 NULL
658};
659
660int kgsl_pwrctrl_init_sysfs(struct kgsl_device *device)
661{
662 return kgsl_create_device_sysfs_files(device->dev, pwrctrl_attr_list);
663}
664
665void kgsl_pwrctrl_uninit_sysfs(struct kgsl_device *device)
666{
667 kgsl_remove_device_sysfs_files(device->dev, pwrctrl_attr_list);
668}
669
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800670static void update_statistics(struct kgsl_device *device)
671{
672 struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats;
673 unsigned int on_time = 0;
674 int i;
675 int num_pwrlevels = device->pwrctrl.num_pwrlevels - 1;
676 /*PER CLK TIME*/
677 for (i = 0; i < num_pwrlevels; i++) {
678 clkstats->old_clock_time[i] = clkstats->clock_time[i];
679 on_time += clkstats->clock_time[i];
680 clkstats->clock_time[i] = 0;
681 }
682 clkstats->old_clock_time[num_pwrlevels] =
683 clkstats->clock_time[num_pwrlevels];
684 clkstats->clock_time[num_pwrlevels] = 0;
685 clkstats->on_time_old = on_time;
686 clkstats->elapsed_old = clkstats->elapsed;
687 clkstats->elapsed = 0;
688}
689
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700690/* Track the amount of time the gpu is on vs the total system time. *
691 * Regularly update the percentage of busy time displayed by sysfs. */
692static void kgsl_pwrctrl_busy_time(struct kgsl_device *device, bool on_time)
693{
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800694 struct kgsl_clk_stats *clkstats = &device->pwrctrl.clk_stats;
695 update_clk_statistics(device, on_time);
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700696 /* Update the output regularly and reset the counters. */
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800697 if ((clkstats->elapsed > UPDATE_BUSY_VAL) ||
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700698 !test_bit(KGSL_PWRFLAGS_AXI_ON, &device->pwrctrl.power_flags)) {
Suman Tatiraju2bdd0562012-01-26 14:49:46 -0800699 update_statistics(device);
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700700 }
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700701}
702
Lucille Sylvestere4a7c1a2012-04-11 12:17:38 -0600703void kgsl_pwrctrl_clk(struct kgsl_device *device, int state,
704 int requested_state)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705{
706 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
707 int i = 0;
708 if (state == KGSL_PWRFLAGS_OFF) {
709 if (test_and_clear_bit(KGSL_PWRFLAGS_CLK_ON,
710 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700711 trace_kgsl_clk(device, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 for (i = KGSL_MAX_CLKS - 1; i > 0; i--)
713 if (pwr->grp_clks[i])
714 clk_disable(pwr->grp_clks[i]);
Lucille Sylvester5b7e57b2012-01-19 17:18:40 -0700715 /* High latency clock maintenance. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 if ((pwr->pwrlevels[0].gpu_freq > 0) &&
Lucille Sylvester5b7e57b2012-01-19 17:18:40 -0700717 (requested_state != KGSL_STATE_NAP)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 clk_set_rate(pwr->grp_clks[0],
719 pwr->pwrlevels[pwr->num_pwrlevels - 1].
720 gpu_freq);
Lucille Sylvester5b7e57b2012-01-19 17:18:40 -0700721 for (i = KGSL_MAX_CLKS - 1; i > 0; i--)
722 if (pwr->grp_clks[i])
723 clk_unprepare(pwr->grp_clks[i]);
724 }
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700725 kgsl_pwrctrl_busy_time(device, true);
Suman Tatiraju426da0d2012-09-11 13:03:28 -0700726 } else if (requested_state == KGSL_STATE_SLEEP) {
727 /* High latency clock maintenance. */
728 if ((pwr->pwrlevels[0].gpu_freq > 0))
729 clk_set_rate(pwr->grp_clks[0],
730 pwr->pwrlevels[pwr->num_pwrlevels - 1].
731 gpu_freq);
732 for (i = KGSL_MAX_CLKS - 1; i > 0; i--)
733 if (pwr->grp_clks[i])
734 clk_unprepare(pwr->grp_clks[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735 }
736 } else if (state == KGSL_PWRFLAGS_ON) {
737 if (!test_and_set_bit(KGSL_PWRFLAGS_CLK_ON,
738 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700739 trace_kgsl_clk(device, state);
Lucille Sylvester5b7e57b2012-01-19 17:18:40 -0700740 /* High latency clock maintenance. */
Ranjhith Kalisamyc9406992012-08-10 16:40:36 +0530741 if (device->state != KGSL_STATE_NAP) {
Lucille Sylvester5b7e57b2012-01-19 17:18:40 -0700742 for (i = KGSL_MAX_CLKS - 1; i > 0; i--)
743 if (pwr->grp_clks[i])
744 clk_prepare(pwr->grp_clks[i]);
Ranjhith Kalisamyc9406992012-08-10 16:40:36 +0530745
746 if (pwr->pwrlevels[0].gpu_freq > 0)
747 clk_set_rate(pwr->grp_clks[0],
748 pwr->pwrlevels
749 [pwr->active_pwrlevel].
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 gpu_freq);
Lucille Sylvester5b7e57b2012-01-19 17:18:40 -0700751 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 /* as last step, enable grp_clk
753 this is to let GPU interrupt to come */
754 for (i = KGSL_MAX_CLKS - 1; i > 0; i--)
755 if (pwr->grp_clks[i])
756 clk_enable(pwr->grp_clks[i]);
Suman Tatiraju7fe62a32011-07-14 16:40:37 -0700757 kgsl_pwrctrl_busy_time(device, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 }
759 }
760}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761
762void kgsl_pwrctrl_axi(struct kgsl_device *device, int state)
763{
764 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
765
766 if (state == KGSL_PWRFLAGS_OFF) {
767 if (test_and_clear_bit(KGSL_PWRFLAGS_AXI_ON,
768 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700769 trace_kgsl_bus(device, state);
Lynus Vaz5a641cc2011-09-15 14:43:40 +0530770 if (pwr->ebi1_clk) {
771 clk_set_rate(pwr->ebi1_clk, 0);
Lucille Sylvester064d5982012-05-08 15:42:43 -0600772 clk_disable_unprepare(pwr->ebi1_clk);
Lynus Vaz5a641cc2011-09-15 14:43:40 +0530773 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 if (pwr->pcl)
775 msm_bus_scale_client_update_request(pwr->pcl,
776 0);
777 }
778 } else if (state == KGSL_PWRFLAGS_ON) {
779 if (!test_and_set_bit(KGSL_PWRFLAGS_AXI_ON,
780 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700781 trace_kgsl_bus(device, state);
Lynus Vaz5a641cc2011-09-15 14:43:40 +0530782 if (pwr->ebi1_clk) {
Lucille Sylvester064d5982012-05-08 15:42:43 -0600783 clk_prepare_enable(pwr->ebi1_clk);
Lynus Vaz5a641cc2011-09-15 14:43:40 +0530784 clk_set_rate(pwr->ebi1_clk,
785 pwr->pwrlevels[pwr->active_pwrlevel].
786 bus_freq);
787 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 if (pwr->pcl)
789 msm_bus_scale_client_update_request(pwr->pcl,
790 pwr->pwrlevels[pwr->active_pwrlevel].
791 bus_freq);
792 }
793 }
794}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795
796void kgsl_pwrctrl_pwrrail(struct kgsl_device *device, int state)
797{
798 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
799
800 if (state == KGSL_PWRFLAGS_OFF) {
801 if (test_and_clear_bit(KGSL_PWRFLAGS_POWER_ON,
802 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700803 trace_kgsl_rail(device, state);
Pu Chen12053782012-07-24 17:04:27 -0700804 if (pwr->gpu_cx)
805 regulator_disable(pwr->gpu_cx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 if (pwr->gpu_reg)
807 regulator_disable(pwr->gpu_reg);
808 }
809 } else if (state == KGSL_PWRFLAGS_ON) {
810 if (!test_and_set_bit(KGSL_PWRFLAGS_POWER_ON,
811 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700812 trace_kgsl_rail(device, state);
Shubhraprakash Dase86ba5c2012-04-04 18:03:27 -0600813 if (pwr->gpu_reg) {
814 int status = regulator_enable(pwr->gpu_reg);
815 if (status)
Pu Chenfe0dd3a2012-06-01 14:39:08 -0700816 KGSL_DRV_ERR(device,
817 "core regulator_enable "
818 "failed: %d\n",
819 status);
820 }
Pu Chen12053782012-07-24 17:04:27 -0700821 if (pwr->gpu_cx) {
822 int status = regulator_enable(pwr->gpu_cx);
Pu Chenfe0dd3a2012-06-01 14:39:08 -0700823 if (status)
824 KGSL_DRV_ERR(device,
825 "cx regulator_enable "
826 "failed: %d\n",
827 status);
Shubhraprakash Dase86ba5c2012-04-04 18:03:27 -0600828 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 }
830 }
831}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832
833void kgsl_pwrctrl_irq(struct kgsl_device *device, int state)
834{
835 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
836
837 if (state == KGSL_PWRFLAGS_ON) {
838 if (!test_and_set_bit(KGSL_PWRFLAGS_IRQ_ON,
839 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700840 trace_kgsl_irq(device, state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 enable_irq(pwr->interrupt_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 }
843 } else if (state == KGSL_PWRFLAGS_OFF) {
844 if (test_and_clear_bit(KGSL_PWRFLAGS_IRQ_ON,
845 &pwr->power_flags)) {
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700846 trace_kgsl_irq(device, state);
Jordan Crouseb58e61b2011-08-08 13:25:36 -0600847 if (in_interrupt())
848 disable_irq_nosync(pwr->interrupt_num);
849 else
850 disable_irq(pwr->interrupt_num);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 }
852 }
853}
854EXPORT_SYMBOL(kgsl_pwrctrl_irq);
855
856int kgsl_pwrctrl_init(struct kgsl_device *device)
857{
858 int i, result = 0;
859 struct clk *clk;
860 struct platform_device *pdev =
861 container_of(device->parentdev, struct platform_device, dev);
862 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600863 struct kgsl_device_platform_data *pdata = pdev->dev.platform_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864
865 /*acquire clocks */
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600866 for (i = 0; i < KGSL_MAX_CLKS; i++) {
867 if (pdata->clk_map & clks[i].map) {
868 clk = clk_get(&pdev->dev, clks[i].name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 if (IS_ERR(clk))
870 goto clk_err;
871 pwr->grp_clks[i] = clk;
872 }
873 }
874 /* Make sure we have a source clk for freq setting */
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600875 if (pwr->grp_clks[0] == NULL)
876 pwr->grp_clks[0] = pwr->grp_clks[1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877
878 /* put the AXI bus into asynchronous mode with the graphics cores */
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600879 if (pdata->set_grp_async != NULL)
880 pdata->set_grp_async();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881
Lucille Sylvesterd260c882013-05-28 16:59:46 -0600882 if (pdata->num_levels > KGSL_MAX_PWRLEVELS ||
883 pdata->num_levels < 1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 KGSL_PWR_ERR(device, "invalid power level count: %d\n",
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600885 pdata->num_levels);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 result = -EINVAL;
887 goto done;
888 }
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600889 pwr->num_pwrlevels = pdata->num_levels;
Jordan Crouse2ddfc8a2012-11-27 11:33:06 -0700890
891 /* Initialize the user and thermal clock constraints */
892
893 pwr->max_pwrlevel = 0;
894 pwr->min_pwrlevel = pdata->num_levels - 2;
895 pwr->thermal_pwrlevel = 0;
896
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600897 pwr->active_pwrlevel = pdata->init_level;
Lucille Sylvester67b4c532012-02-08 11:24:31 -0800898 pwr->default_pwrlevel = pdata->init_level;
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600899 for (i = 0; i < pdata->num_levels; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 pwr->pwrlevels[i].gpu_freq =
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600901 (pdata->pwrlevel[i].gpu_freq > 0) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 clk_round_rate(pwr->grp_clks[0],
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600903 pdata->pwrlevel[i].
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 gpu_freq) : 0;
905 pwr->pwrlevels[i].bus_freq =
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600906 pdata->pwrlevel[i].bus_freq;
Lucille Sylvester596d4c22011-10-19 18:04:01 -0600907 pwr->pwrlevels[i].io_fraction =
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600908 pdata->pwrlevel[i].io_fraction;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 }
910 /* Do not set_rate for targets in sync with AXI */
911 if (pwr->pwrlevels[0].gpu_freq > 0)
912 clk_set_rate(pwr->grp_clks[0], pwr->
913 pwrlevels[pwr->num_pwrlevels - 1].gpu_freq);
914
Matt Wagantalld6fbf232012-05-03 20:09:28 -0700915 pwr->gpu_reg = regulator_get(&pdev->dev, "vdd");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 if (IS_ERR(pwr->gpu_reg))
917 pwr->gpu_reg = NULL;
918
Pu Chenfe0dd3a2012-06-01 14:39:08 -0700919 if (pwr->gpu_reg) {
Pu Chen12053782012-07-24 17:04:27 -0700920 pwr->gpu_cx = regulator_get(&pdev->dev, "vddcx");
921 if (IS_ERR(pwr->gpu_cx))
922 pwr->gpu_cx = NULL;
Pu Chenfe0dd3a2012-06-01 14:39:08 -0700923 } else
Pu Chen12053782012-07-24 17:04:27 -0700924 pwr->gpu_cx = NULL;
Pu Chenfe0dd3a2012-06-01 14:39:08 -0700925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 pwr->power_flags = 0;
927
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600928 pwr->nap_allowed = pdata->nap_allowed;
Kedar Joshic11d0982012-02-07 10:59:49 +0530929 pwr->idle_needed = pdata->idle_needed;
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600930 pwr->interval_timeout = pdata->idle_timeout;
Lynus Vazfe4bede2012-04-06 11:53:30 -0700931 pwr->strtstp_sleepwake = pdata->strtstp_sleepwake;
Matt Wagantall9dc01632011-08-17 18:55:04 -0700932 pwr->ebi1_clk = clk_get(&pdev->dev, "bus_clk");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 if (IS_ERR(pwr->ebi1_clk))
934 pwr->ebi1_clk = NULL;
935 else
936 clk_set_rate(pwr->ebi1_clk,
937 pwr->pwrlevels[pwr->active_pwrlevel].
938 bus_freq);
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600939 if (pdata->bus_scale_table != NULL) {
940 pwr->pcl = msm_bus_scale_register_client(pdata->
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 bus_scale_table);
942 if (!pwr->pcl) {
943 KGSL_PWR_ERR(device,
944 "msm_bus_scale_register_client failed: "
945 "id %d table %p", device->id,
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600946 pdata->bus_scale_table);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947 result = -EINVAL;
948 goto done;
949 }
950 }
951
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952
Jeremy Gebben4f5f0de2012-03-01 15:51:37 -0700953 pm_runtime_enable(device->parentdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 register_early_suspend(&device->display_off);
955 return result;
956
957clk_err:
958 result = PTR_ERR(clk);
959 KGSL_PWR_ERR(device, "clk_get(%s) failed: %d\n",
Lucille Sylvesterdce84cd2011-10-12 14:15:37 -0600960 clks[i].name, result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961
962done:
963 return result;
964}
965
966void kgsl_pwrctrl_close(struct kgsl_device *device)
967{
968 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
969 int i;
970
971 KGSL_PWR_INFO(device, "close device %d\n", device->id);
972
Jeremy Gebben4f5f0de2012-03-01 15:51:37 -0700973 pm_runtime_disable(device->parentdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 unregister_early_suspend(&device->display_off);
975
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 clk_put(pwr->ebi1_clk);
977
978 if (pwr->pcl)
979 msm_bus_scale_unregister_client(pwr->pcl);
980
981 pwr->pcl = 0;
982
983 if (pwr->gpu_reg) {
984 regulator_put(pwr->gpu_reg);
985 pwr->gpu_reg = NULL;
986 }
987
Pu Chen12053782012-07-24 17:04:27 -0700988 if (pwr->gpu_cx) {
989 regulator_put(pwr->gpu_cx);
990 pwr->gpu_cx = NULL;
Pu Chenfe0dd3a2012-06-01 14:39:08 -0700991 }
992
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 for (i = 1; i < KGSL_MAX_CLKS; i++)
994 if (pwr->grp_clks[i]) {
995 clk_put(pwr->grp_clks[i]);
996 pwr->grp_clks[i] = NULL;
997 }
998
999 pwr->grp_clks[0] = NULL;
1000 pwr->power_flags = 0;
1001}
1002
1003void kgsl_idle_check(struct work_struct *work)
1004{
1005 struct kgsl_device *device = container_of(work, struct kgsl_device,
1006 idle_check_ws);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001007 WARN_ON(device == NULL);
1008 if (device == NULL)
1009 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010
1011 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 if (device->state & (KGSL_STATE_ACTIVE | KGSL_STATE_NAP)) {
Lucille Sylvester721f7e72012-08-21 16:31:26 -06001013 kgsl_pwrscale_idle(device);
Lucille Sylvesterc4af3552011-10-27 11:44:24 -06001014
Suman Tatiraju7fe62a32011-07-14 16:40:37 -07001015 if (kgsl_pwrctrl_sleep(device) != 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 mod_timer(&device->idle_timer,
1017 jiffies +
1018 device->pwrctrl.interval_timeout);
Suman Tatiraju7fe62a32011-07-14 16:40:37 -07001019 /* If the GPU has been too busy to sleep, make sure *
1020 * that is acurately reflected in the % busy numbers. */
Suman Tatiraju2bdd0562012-01-26 14:49:46 -08001021 device->pwrctrl.clk_stats.no_nap_cnt++;
1022 if (device->pwrctrl.clk_stats.no_nap_cnt >
1023 UPDATE_BUSY) {
Suman Tatiraju7fe62a32011-07-14 16:40:37 -07001024 kgsl_pwrctrl_busy_time(device, true);
Suman Tatiraju2bdd0562012-01-26 14:49:46 -08001025 device->pwrctrl.clk_stats.no_nap_cnt = 0;
Suman Tatiraju7fe62a32011-07-14 16:40:37 -07001026 }
1027 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028 } else if (device->state & (KGSL_STATE_HUNG |
Tarun Karrad20d71a2013-01-25 15:38:57 -08001029 KGSL_STATE_DUMP_AND_FT)) {
Jeremy Gebben388c2972011-12-16 09:05:07 -07001030 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 }
1032
1033 mutex_unlock(&device->mutex);
1034}
1035
1036void kgsl_timer(unsigned long data)
1037{
1038 struct kgsl_device *device = (struct kgsl_device *) data;
1039
1040 KGSL_PWR_INFO(device, "idle timer expired device %d\n", device->id);
Anoop Kumar Yerukala03ba25f2012-01-23 17:32:02 +05301041 if (device->requested_state != KGSL_STATE_SUSPEND) {
Lynus Vazfe4bede2012-04-06 11:53:30 -07001042 if (device->pwrctrl.restore_slumber ||
1043 device->pwrctrl.strtstp_sleepwake)
Lucille Sylvestera985adf2012-01-16 11:11:55 -07001044 kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER);
1045 else
1046 kgsl_pwrctrl_request_state(device, KGSL_STATE_SLEEP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 /* Have work run in a non-interrupt context. */
1048 queue_work(device->work_queue, &device->idle_check_ws);
1049 }
1050}
1051
1052void kgsl_pre_hwaccess(struct kgsl_device *device)
1053{
1054 BUG_ON(!mutex_is_locked(&device->mutex));
Lucille Sylvester8b803e92012-01-12 15:19:55 -07001055 switch (device->state) {
1056 case KGSL_STATE_ACTIVE:
1057 return;
1058 case KGSL_STATE_NAP:
1059 case KGSL_STATE_SLEEP:
1060 case KGSL_STATE_SLUMBER:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 kgsl_pwrctrl_wake(device);
Lucille Sylvester8b803e92012-01-12 15:19:55 -07001062 break;
1063 case KGSL_STATE_SUSPEND:
1064 kgsl_check_suspended(device);
1065 break;
1066 case KGSL_STATE_INIT:
1067 case KGSL_STATE_HUNG:
Tarun Karrad20d71a2013-01-25 15:38:57 -08001068 case KGSL_STATE_DUMP_AND_FT:
Lucille Sylvester8b803e92012-01-12 15:19:55 -07001069 if (test_bit(KGSL_PWRFLAGS_CLK_ON,
1070 &device->pwrctrl.power_flags))
1071 break;
1072 else
1073 KGSL_PWR_ERR(device,
1074 "hw access while clocks off from state %d\n",
1075 device->state);
1076 break;
1077 default:
1078 KGSL_PWR_ERR(device, "hw access while in unknown state %d\n",
1079 device->state);
1080 break;
1081 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082}
1083EXPORT_SYMBOL(kgsl_pre_hwaccess);
1084
1085void kgsl_check_suspended(struct kgsl_device *device)
1086{
1087 if (device->requested_state == KGSL_STATE_SUSPEND ||
1088 device->state == KGSL_STATE_SUSPEND) {
1089 mutex_unlock(&device->mutex);
1090 wait_for_completion(&device->hwaccess_gate);
1091 mutex_lock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001092 } else if (device->state == KGSL_STATE_DUMP_AND_FT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 mutex_unlock(&device->mutex);
Tarun Karrad20d71a2013-01-25 15:38:57 -08001094 wait_for_completion(&device->ft_gate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095 mutex_lock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -07001096 } else if (device->state == KGSL_STATE_SLUMBER)
1097 kgsl_pwrctrl_wake(device);
1098}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099
Suman Tatiraju24569022011-10-27 11:11:12 -07001100static int
Jeremy Gebben388c2972011-12-16 09:05:07 -07001101_nap(struct kgsl_device *device)
Suman Tatiraju24569022011-10-27 11:11:12 -07001102{
Suman Tatiraju24569022011-10-27 11:11:12 -07001103 switch (device->state) {
1104 case KGSL_STATE_ACTIVE:
Jeremy Gebben388c2972011-12-16 09:05:07 -07001105 if (!device->ftbl->isidle(device)) {
1106 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
1107 return -EBUSY;
1108 }
1109 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
Lucille Sylvestere4a7c1a2012-04-11 12:17:38 -06001110 kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_NAP);
1111 kgsl_pwrctrl_set_state(device, KGSL_STATE_NAP);
Suman Tatiraju24569022011-10-27 11:11:12 -07001112 case KGSL_STATE_NAP:
1113 case KGSL_STATE_SLEEP:
Jeremy Gebben388c2972011-12-16 09:05:07 -07001114 case KGSL_STATE_SLUMBER:
Suman Tatiraju24569022011-10-27 11:11:12 -07001115 break;
1116 default:
Jeremy Gebben388c2972011-12-16 09:05:07 -07001117 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Suman Tatiraju24569022011-10-27 11:11:12 -07001118 break;
1119 }
Jeremy Gebben388c2972011-12-16 09:05:07 -07001120 return 0;
1121}
1122
1123static void
1124_sleep_accounting(struct kgsl_device *device)
1125{
1126 kgsl_pwrctrl_busy_time(device, false);
Suman Tatiraju2bdd0562012-01-26 14:49:46 -08001127 device->pwrctrl.clk_stats.start = ktime_set(0, 0);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001128 device->pwrctrl.time = 0;
1129 kgsl_pwrscale_sleep(device);
1130}
1131
1132static int
1133_sleep(struct kgsl_device *device)
1134{
Jeremy Gebben388c2972011-12-16 09:05:07 -07001135 switch (device->state) {
1136 case KGSL_STATE_ACTIVE:
1137 if (!device->ftbl->isidle(device)) {
1138 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
1139 return -EBUSY;
1140 }
1141 /* fall through */
1142 case KGSL_STATE_NAP:
1143 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
1144 kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001145 _sleep_accounting(device);
Lucille Sylvestere4a7c1a2012-04-11 12:17:38 -06001146 kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001147 kgsl_pwrctrl_set_state(device, KGSL_STATE_SLEEP);
Lucille Sylvester10297892012-02-27 13:54:47 -07001148 pm_qos_update_request(&device->pm_qos_req_dma,
1149 PM_QOS_DEFAULT_VALUE);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001150 break;
1151 case KGSL_STATE_SLEEP:
1152 case KGSL_STATE_SLUMBER:
1153 break;
1154 default:
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001155 KGSL_PWR_WARN(device, "unhandled state %s\n",
1156 kgsl_pwrstate_to_str(device->state));
Jeremy Gebben388c2972011-12-16 09:05:07 -07001157 break;
1158 }
Shubhraprakash Das7a0c93c2012-11-20 15:15:08 -07001159
1160 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
1161
Jeremy Gebben388c2972011-12-16 09:05:07 -07001162 return 0;
1163}
1164
1165static int
1166_slumber(struct kgsl_device *device)
1167{
1168 switch (device->state) {
1169 case KGSL_STATE_ACTIVE:
1170 if (!device->ftbl->isidle(device)) {
1171 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001172 return -EBUSY;
1173 }
1174 /* fall through */
1175 case KGSL_STATE_NAP:
1176 case KGSL_STATE_SLEEP:
1177 del_timer_sync(&device->idle_timer);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001178 device->ftbl->suspend_context(device);
1179 device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001180 _sleep_accounting(device);
1181 kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju3005cdd2012-03-19 14:38:11 -07001182 pm_qos_update_request(&device->pm_qos_req_dma,
1183 PM_QOS_DEFAULT_VALUE);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001184 break;
1185 case KGSL_STATE_SLUMBER:
1186 break;
1187 default:
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001188 KGSL_PWR_WARN(device, "unhandled state %s\n",
1189 kgsl_pwrstate_to_str(device->state));
Jeremy Gebben388c2972011-12-16 09:05:07 -07001190 break;
1191 }
1192 return 0;
Suman Tatiraju24569022011-10-27 11:11:12 -07001193}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194
1195/******************************************************************/
1196/* Caller must hold the device mutex. */
1197int kgsl_pwrctrl_sleep(struct kgsl_device *device)
1198{
Jeremy Gebben388c2972011-12-16 09:05:07 -07001199 int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 KGSL_PWR_INFO(device, "sleep device %d\n", device->id);
1201
1202 /* Work through the legal state transitions */
Jeremy Gebben388c2972011-12-16 09:05:07 -07001203 switch (device->requested_state) {
1204 case KGSL_STATE_NAP:
Jeremy Gebben388c2972011-12-16 09:05:07 -07001205 status = _nap(device);
1206 break;
1207 case KGSL_STATE_SLEEP:
Lucille Sylvestera985adf2012-01-16 11:11:55 -07001208 status = _sleep(device);
Shubhraprakash Das602497c2013-05-28 16:53:24 -06001209 kgsl_mmu_disable_clk_on_ts(&device->mmu, 0, false);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001210 break;
1211 case KGSL_STATE_SLUMBER:
1212 status = _slumber(device);
1213 break;
1214 default:
1215 KGSL_PWR_INFO(device, "bad state request 0x%x\n",
1216 device->requested_state);
1217 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
1218 status = -EINVAL;
1219 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 }
Suman Tatiraju24569022011-10-27 11:11:12 -07001221 return status;
1222}
Jeremy Gebben388c2972011-12-16 09:05:07 -07001223EXPORT_SYMBOL(kgsl_pwrctrl_sleep);
Suman Tatiraju24569022011-10-27 11:11:12 -07001224
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225/******************************************************************/
1226/* Caller must hold the device mutex. */
1227void kgsl_pwrctrl_wake(struct kgsl_device *device)
1228{
Jeremy Gebben388c2972011-12-16 09:05:07 -07001229 int status;
Ranjhith Kalisamycb1721c2013-05-28 16:59:59 -06001230 unsigned int context_id;
1231 unsigned int state = device->state;
1232 unsigned int ts_processed = 0xdeaddead;
1233 struct kgsl_context *context;
1234
Jeremy Gebben388c2972011-12-16 09:05:07 -07001235 kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);
1236 switch (device->state) {
1237 case KGSL_STATE_SLUMBER:
1238 status = device->ftbl->start(device, 0);
1239 if (status) {
1240 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
1241 KGSL_DRV_ERR(device, "start failed %d\n", status);
1242 break;
1243 }
1244 /* fall through */
1245 case KGSL_STATE_SLEEP:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON);
1247 kgsl_pwrscale_wake(device);
Ranjhith Kalisamycb1721c2013-05-28 16:59:59 -06001248 kgsl_sharedmem_readl(&device->memstore,
1249 (unsigned int *) &context_id,
1250 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL,
1251 current_context));
1252 context = idr_find(&device->context_idr, context_id);
1253 if (context)
1254 ts_processed = kgsl_readtimestamp(device, context,
1255 KGSL_TIMESTAMP_RETIRED);
1256 KGSL_PWR_INFO(device, "Wake from %s state. CTXT: %d RTRD TS: %08X\n",
1257 kgsl_pwrstate_to_str(state),
1258 context ? context->id : -1, ts_processed);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001259 /* fall through */
1260 case KGSL_STATE_NAP:
1261 /* Turn on the core clocks */
Lucille Sylvestere4a7c1a2012-04-11 12:17:38 -06001262 kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001263 /* Enable state before turning on irq */
1264 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
1265 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
1266 /* Re-enable HW access */
1267 mod_timer(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001268 jiffies + device->pwrctrl.interval_timeout);
Devin Kim66ad4c02012-09-21 20:28:50 -07001269 pm_qos_update_request(&device->pm_qos_req_dma,
1270 GPU_SWFI_LATENCY);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001271 case KGSL_STATE_ACTIVE:
Vinay Roy65c41b32012-11-25 00:48:38 +05301272 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001273 break;
1274 default:
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001275 KGSL_PWR_WARN(device, "unhandled state %s\n",
1276 kgsl_pwrstate_to_str(device->state));
Jeremy Gebben388c2972011-12-16 09:05:07 -07001277 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
1278 break;
1279 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280}
1281EXPORT_SYMBOL(kgsl_pwrctrl_wake);
1282
1283void kgsl_pwrctrl_enable(struct kgsl_device *device)
1284{
1285 /* Order pwrrail/clk sequence based upon platform */
1286 kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_ON);
Lucille Sylvestere4a7c1a2012-04-11 12:17:38 -06001287 kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_ON, KGSL_STATE_ACTIVE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_ON);
1289}
1290EXPORT_SYMBOL(kgsl_pwrctrl_enable);
1291
1292void kgsl_pwrctrl_disable(struct kgsl_device *device)
1293{
1294 /* Order pwrrail/clk sequence based upon platform */
1295 kgsl_pwrctrl_axi(device, KGSL_PWRFLAGS_OFF);
Lucille Sylvestere4a7c1a2012-04-11 12:17:38 -06001296 kgsl_pwrctrl_clk(device, KGSL_PWRFLAGS_OFF, KGSL_STATE_SLEEP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 kgsl_pwrctrl_pwrrail(device, KGSL_PWRFLAGS_OFF);
1298}
1299EXPORT_SYMBOL(kgsl_pwrctrl_disable);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001300
1301void kgsl_pwrctrl_set_state(struct kgsl_device *device, unsigned int state)
1302{
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001303 trace_kgsl_pwr_set_state(device, state);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001304 device->state = state;
1305 device->requested_state = KGSL_STATE_NONE;
1306}
1307EXPORT_SYMBOL(kgsl_pwrctrl_set_state);
1308
1309void kgsl_pwrctrl_request_state(struct kgsl_device *device, unsigned int state)
1310{
1311 if (state != KGSL_STATE_NONE && state != device->requested_state)
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001312 trace_kgsl_pwr_request_state(device, state);
Jeremy Gebben388c2972011-12-16 09:05:07 -07001313 device->requested_state = state;
1314}
1315EXPORT_SYMBOL(kgsl_pwrctrl_request_state);
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001316
1317const char *kgsl_pwrstate_to_str(unsigned int state)
1318{
1319 switch (state) {
1320 case KGSL_STATE_NONE:
1321 return "NONE";
1322 case KGSL_STATE_INIT:
1323 return "INIT";
1324 case KGSL_STATE_ACTIVE:
1325 return "ACTIVE";
1326 case KGSL_STATE_NAP:
1327 return "NAP";
1328 case KGSL_STATE_SLEEP:
1329 return "SLEEP";
1330 case KGSL_STATE_SUSPEND:
1331 return "SUSPEND";
1332 case KGSL_STATE_HUNG:
1333 return "HUNG";
Tarun Karrad20d71a2013-01-25 15:38:57 -08001334 case KGSL_STATE_DUMP_AND_FT:
Jeremy Gebbenb50f3312011-12-16 08:58:33 -07001335 return "DNR";
1336 case KGSL_STATE_SLUMBER:
1337 return "SLUMBER";
1338 default:
1339 break;
1340 }
1341 return "UNKNOWN";
1342}
1343EXPORT_SYMBOL(kgsl_pwrstate_to_str);
1344