nvmap: fix test condition to avoid infinite loop
[linux-2.6.git] / drivers / video / tegra / host / nvhost_acm.c
1 /*
2  * drivers/video/tegra/host/nvhost_acm.c
3  *
4  * Tegra Graphics Host Automatic Clock Management
5  *
6  * Copyright (c) 2010-2014, NVIDIA Corporation. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "nvhost_acm.h"
22 #include "dev.h"
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/sched.h>
26 #include <linux/err.h>
27 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <mach/powergate.h>
31 #include <mach/clk.h>
32 #include <mach/hardware.h>
33
34 #define ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT       (2 * HZ)
35 #define POWERGATE_DELAY                         10
36 #define MAX_DEVID_LENGTH                        16
37
38 DEFINE_MUTEX(client_list_lock);
39
40 struct nvhost_module_client {
41         struct list_head node;
42         unsigned long rate[NVHOST_MODULE_MAX_CLOCKS];
43         void *priv;
44 };
45
46 static void do_powergate_locked(int id)
47 {
48         if (id != -1 && tegra_powergate_is_powered(id))
49                 tegra_powergate_partition(id);
50 }
51
52 static void do_unpowergate_locked(int id)
53 {
54         if (id != -1)
55                 tegra_unpowergate_partition(id);
56 }
57
58 static void do_module_reset_locked(struct nvhost_device *dev)
59 {
60         /* assert module and mc client reset */
61         if (dev->powergate_ids[0] != -1) {
62                 tegra_powergate_mc_disable(dev->powergate_ids[0]);
63                 tegra_periph_reset_assert(dev->clk[0]);
64                 tegra_powergate_mc_flush(dev->powergate_ids[0]);
65         }
66         if (dev->powergate_ids[1] != -1) {
67                 tegra_powergate_mc_disable(dev->powergate_ids[1]);
68                 tegra_periph_reset_assert(dev->clk[1]);
69                 tegra_powergate_mc_flush(dev->powergate_ids[1]);
70         }
71
72         udelay(POWERGATE_DELAY);
73
74         /* deassert reset */
75         if (dev->powergate_ids[0] != -1) {
76                 tegra_powergate_mc_flush_done(dev->powergate_ids[0]);
77                 tegra_periph_reset_deassert(dev->clk[0]);
78                 tegra_powergate_mc_enable(dev->powergate_ids[0]);
79         }
80         if (dev->powergate_ids[1] != -1) {
81                 tegra_powergate_mc_flush_done(dev->powergate_ids[1]);
82                 tegra_periph_reset_deassert(dev->clk[1]);
83                 tegra_powergate_mc_enable(dev->powergate_ids[1]);
84         }
85 }
86
87 void nvhost_module_reset(struct nvhost_device *dev)
88 {
89         dev_dbg(&dev->dev,
90                 "%s: asserting %s module reset (id %d, id2 %d)\n",
91                 __func__, dev->name,
92                 dev->powergate_ids[0], dev->powergate_ids[1]);
93
94         mutex_lock(&dev->lock);
95         do_module_reset_locked(dev);
96         mutex_unlock(&dev->lock);
97
98         dev_dbg(&dev->dev, "%s: module %s out of reset\n",
99                 __func__, dev->name);
100 }
101
102 static void to_state_clockgated_locked(struct nvhost_device *dev)
103 {
104         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
105
106         if (dev->powerstate == NVHOST_POWER_STATE_RUNNING) {
107                 int i, err;
108                 if (drv->prepare_clockoff) {
109                         err = drv->prepare_clockoff(dev);
110                         if (err) {
111                                 dev_err(&dev->dev, "error clock gating");
112                                 return;
113                         }
114                 }
115                 for (i = 0; i < dev->num_clks; i++)
116                         clk_disable(dev->clk[i]);
117                 if (dev->dev.parent)
118                         nvhost_module_idle(to_nvhost_device(dev->dev.parent));
119         } else if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED
120                         && dev->can_powergate) {
121                 do_unpowergate_locked(dev->powergate_ids[0]);
122                 do_unpowergate_locked(dev->powergate_ids[1]);
123
124                 if (dev->powerup_reset)
125                         do_module_reset_locked(dev);
126         }
127         dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
128 }
129
130 static void to_state_running_locked(struct nvhost_device *dev)
131 {
132         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
133         int prev_state = dev->powerstate;
134
135         if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED)
136                 to_state_clockgated_locked(dev);
137
138         if (dev->powerstate == NVHOST_POWER_STATE_CLOCKGATED) {
139                 int i;
140
141                 if (dev->dev.parent)
142                         nvhost_module_busy(to_nvhost_device(dev->dev.parent));
143
144                 for (i = 0; i < dev->num_clks; i++) {
145                         int err = clk_enable(dev->clk[i]);
146                         if (err) {
147                                 dev_err(&dev->dev, "Cannot turn on clock %s",
148                                         dev->clocks[i].name);
149                                 return;
150                         }
151                 }
152
153                 /* Invoke callback after enabling clock. This is used for
154                  * re-enabling host1x interrupts. */
155                 if (prev_state == NVHOST_POWER_STATE_CLOCKGATED
156                                 && drv->finalize_clockon)
157                         drv->finalize_clockon(dev);
158
159                 /* Invoke callback after power un-gating. This is used for
160                  * restoring context. */
161                 if (prev_state == NVHOST_POWER_STATE_POWERGATED
162                                 && drv->finalize_poweron)
163                         drv->finalize_poweron(dev);
164         }
165         dev->powerstate = NVHOST_POWER_STATE_RUNNING;
166 }
167
168 /* This gets called from powergate_handler() and from module suspend.
169  * Module suspend is done for all modules, runtime power gating only
170  * for modules with can_powergate set.
171  */
172 static int to_state_powergated_locked(struct nvhost_device *dev)
173 {
174         int err = 0;
175         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
176
177         if (drv->prepare_poweroff
178                         && dev->powerstate != NVHOST_POWER_STATE_POWERGATED) {
179                 /* Clock needs to be on in prepare_poweroff */
180                 to_state_running_locked(dev);
181                 err = drv->prepare_poweroff(dev);
182                 if (err)
183                         return err;
184         }
185
186         if (dev->powerstate == NVHOST_POWER_STATE_RUNNING)
187                 to_state_clockgated_locked(dev);
188
189         if (dev->can_powergate) {
190                 do_powergate_locked(dev->powergate_ids[0]);
191                 do_powergate_locked(dev->powergate_ids[1]);
192         }
193
194         dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
195         return 0;
196 }
197
198 static void schedule_powergating_locked(struct nvhost_device *dev)
199 {
200         if (dev->can_powergate)
201                 schedule_delayed_work(&dev->powerstate_down,
202                                 msecs_to_jiffies(dev->powergate_delay));
203 }
204
205 static void schedule_clockgating_locked(struct nvhost_device *dev)
206 {
207         schedule_delayed_work(&dev->powerstate_down,
208                         msecs_to_jiffies(dev->clockgate_delay));
209 }
210
211 void nvhost_module_busy(struct nvhost_device *dev)
212 {
213         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
214
215         if (drv->busy)
216                 drv->busy(dev);
217
218         mutex_lock(&dev->lock);
219         cancel_delayed_work(&dev->powerstate_down);
220
221         dev->refcount++;
222         if (dev->refcount > 0 && !nvhost_module_powered(dev))
223                 to_state_running_locked(dev);
224
225         mutex_unlock(&dev->lock);
226 }
227
228 static void powerstate_down_handler(struct work_struct *work)
229 {
230         struct nvhost_device *dev;
231
232         dev = container_of(to_delayed_work(work),
233                         struct nvhost_device,
234                         powerstate_down);
235
236         mutex_lock(&dev->lock);
237         if (dev->refcount == 0) {
238                 switch (dev->powerstate) {
239                 case NVHOST_POWER_STATE_RUNNING:
240                         to_state_clockgated_locked(dev);
241                         schedule_powergating_locked(dev);
242                         break;
243                 case NVHOST_POWER_STATE_CLOCKGATED:
244                         if (to_state_powergated_locked(dev))
245                                 schedule_powergating_locked(dev);
246                         break;
247                 default:
248                         break;
249                 }
250         }
251         mutex_unlock(&dev->lock);
252 }
253
254 void nvhost_module_idle_mult(struct nvhost_device *dev, int refs)
255 {
256         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
257         bool kick = false;
258
259         mutex_lock(&dev->lock);
260         dev->refcount -= refs;
261         if (dev->refcount == 0) {
262                 if (nvhost_module_powered(dev))
263                         schedule_clockgating_locked(dev);
264                 kick = true;
265         }
266         mutex_unlock(&dev->lock);
267
268         if (kick) {
269                 wake_up(&dev->idle_wq);
270
271                 if (drv->idle)
272                         drv->idle(dev);
273         }
274 }
275
276 int nvhost_module_get_rate(struct nvhost_device *dev, unsigned long *rate,
277                 int index)
278 {
279         struct clk *c;
280
281         c = dev->clk[index];
282         if (IS_ERR_OR_NULL(c))
283                 return -EINVAL;
284
285         /* Need to enable client to get correct rate */
286         nvhost_module_busy(dev);
287         *rate = clk_get_rate(c);
288         nvhost_module_idle(dev);
289         return 0;
290
291 }
292
293 static int nvhost_module_update_rate(struct nvhost_device *dev, int index)
294 {
295         unsigned long rate = 0;
296         struct nvhost_module_client *m;
297
298         if (!dev->clk[index])
299                 return -EINVAL;
300
301         list_for_each_entry(m, &dev->client_list, node) {
302                 rate = max(m->rate[index], rate);
303         }
304         if (!rate)
305                 rate = clk_round_rate(dev->clk[index],
306                                 dev->clocks[index].default_rate);
307
308         return clk_set_rate(dev->clk[index], rate);
309 }
310
311 int nvhost_module_set_rate(struct nvhost_device *dev, void *priv,
312                 unsigned long rate, int index)
313 {
314         struct nvhost_module_client *m;
315         int i, ret = 0;
316
317         mutex_lock(&client_list_lock);
318         list_for_each_entry(m, &dev->client_list, node) {
319                 if (m->priv == priv) {
320                         for (i = 0; i < dev->num_clks; i++)
321                                 m->rate[i] = clk_round_rate(dev->clk[i], rate);
322                         break;
323                 }
324         }
325
326         for (i = 0; i < dev->num_clks; i++) {
327                 ret = nvhost_module_update_rate(dev, i);
328                 if (ret < 0)
329                         break;
330         }
331         mutex_unlock(&client_list_lock);
332         return ret;
333
334 }
335
336 int nvhost_module_add_client(struct nvhost_device *dev, void *priv)
337 {
338         int i;
339         unsigned long rate;
340         struct nvhost_module_client *client;
341
342         client = kzalloc(sizeof(*client), GFP_KERNEL);
343         if (!client)
344                 return -ENOMEM;
345
346         INIT_LIST_HEAD(&client->node);
347         client->priv = priv;
348
349         for (i = 0; i < dev->num_clks; i++) {
350                 rate = clk_round_rate(dev->clk[i],
351                                 dev->clocks[i].default_rate);
352                 client->rate[i] = rate;
353         }
354         mutex_lock(&client_list_lock);
355         list_add_tail(&client->node, &dev->client_list);
356         mutex_unlock(&client_list_lock);
357         return 0;
358 }
359
360 void nvhost_module_remove_client(struct nvhost_device *dev, void *priv)
361 {
362         int i;
363         struct nvhost_module_client *m;
364         int found = 0;
365
366         mutex_lock(&client_list_lock);
367         list_for_each_entry(m, &dev->client_list, node) {
368                 if (priv == m->priv) {
369                         list_del(&m->node);
370                         found = 1;
371                         break;
372                 }
373         }
374         if (found) {
375                 kfree(m);
376                 for (i = 0; i < dev->num_clks; i++)
377                         nvhost_module_update_rate(dev, i);
378         }
379         mutex_unlock(&client_list_lock);
380 }
381
382 static ssize_t refcount_show(struct kobject *kobj,
383         struct kobj_attribute *attr, char *buf)
384 {
385         int ret;
386         struct nvhost_device_power_attr *power_attribute =
387                 container_of(attr, struct nvhost_device_power_attr, \
388                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_REFCOUNT]);
389         struct nvhost_device *dev = power_attribute->ndev;
390
391         mutex_lock(&dev->lock);
392         ret = sprintf(buf, "%d\n", dev->refcount);
393         mutex_unlock(&dev->lock);
394
395         return ret;
396 }
397
398 static ssize_t powergate_delay_store(struct kobject *kobj,
399         struct kobj_attribute *attr, const char *buf, size_t count)
400 {
401         int powergate_delay = 0, ret = 0;
402         struct nvhost_device_power_attr *power_attribute =
403                 container_of(attr, struct nvhost_device_power_attr, \
404                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY]);
405         struct nvhost_device *dev = power_attribute->ndev;
406
407         if (!dev->can_powergate) {
408                 dev_info(&dev->dev, "does not support power-gating\n");
409                 return count;
410         }
411
412         mutex_lock(&dev->lock);
413         ret = sscanf(buf, "%d", &powergate_delay);
414         if (ret == 1 && powergate_delay >= 0)
415                 dev->powergate_delay = powergate_delay;
416         else
417                 dev_err(&dev->dev, "Invalid powergate delay\n");
418         mutex_unlock(&dev->lock);
419
420         return count;
421 }
422
423 static ssize_t powergate_delay_show(struct kobject *kobj,
424         struct kobj_attribute *attr, char *buf)
425 {
426         int ret;
427         struct nvhost_device_power_attr *power_attribute =
428                 container_of(attr, struct nvhost_device_power_attr, \
429                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY]);
430         struct nvhost_device *dev = power_attribute->ndev;
431
432         mutex_lock(&dev->lock);
433         ret = sprintf(buf, "%d\n", dev->powergate_delay);
434         mutex_unlock(&dev->lock);
435
436         return ret;
437 }
438
439 static ssize_t clockgate_delay_store(struct kobject *kobj,
440         struct kobj_attribute *attr, const char *buf, size_t count)
441 {
442         int clockgate_delay = 0, ret = 0;
443         struct nvhost_device_power_attr *power_attribute =
444                 container_of(attr, struct nvhost_device_power_attr, \
445                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY]);
446         struct nvhost_device *dev = power_attribute->ndev;
447
448         mutex_lock(&dev->lock);
449         ret = sscanf(buf, "%d", &clockgate_delay);
450         if (ret == 1 && clockgate_delay >= 0)
451                 dev->clockgate_delay = clockgate_delay;
452         else
453                 dev_err(&dev->dev, "Invalid clockgate delay\n");
454         mutex_unlock(&dev->lock);
455
456         return count;
457 }
458
459 static ssize_t clockgate_delay_show(struct kobject *kobj,
460         struct kobj_attribute *attr, char *buf)
461 {
462         int ret;
463         struct nvhost_device_power_attr *power_attribute =
464                 container_of(attr, struct nvhost_device_power_attr, \
465                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY]);
466         struct nvhost_device *dev = power_attribute->ndev;
467
468         mutex_lock(&dev->lock);
469         ret = sprintf(buf, "%d\n", dev->clockgate_delay);
470         mutex_unlock(&dev->lock);
471
472         return ret;
473 }
474
475 int nvhost_module_init(struct nvhost_device *dev)
476 {
477         int i = 0, err = 0;
478         struct kobj_attribute *attr = NULL;
479
480         /* initialize clocks to known state */
481         INIT_LIST_HEAD(&dev->client_list);
482         while (dev->clocks[i].name && i < NVHOST_MODULE_MAX_CLOCKS) {
483                 char devname[MAX_DEVID_LENGTH];
484                 long rate = dev->clocks[i].default_rate;
485                 struct clk *c;
486
487                 snprintf(devname, MAX_DEVID_LENGTH, "tegra_%s", dev->name);
488                 c = clk_get_sys(devname, dev->clocks[i].name);
489                 if (IS_ERR_OR_NULL(c)) {
490                         dev_err(&dev->dev, "Cannot get clock %s\n",
491                                         dev->clocks[i].name);
492                         continue;
493                 }
494
495                 rate = clk_round_rate(c, rate);
496                 clk_enable(c);
497                 clk_set_rate(c, rate);
498                 clk_disable(c);
499                 dev->clk[i] = c;
500                 i++;
501         }
502         dev->num_clks = i;
503
504         mutex_init(&dev->lock);
505         init_waitqueue_head(&dev->idle_wq);
506         INIT_DELAYED_WORK(&dev->powerstate_down, powerstate_down_handler);
507
508         /* power gate units that we can power gate */
509         if (dev->can_powergate) {
510                 do_powergate_locked(dev->powergate_ids[0]);
511                 do_powergate_locked(dev->powergate_ids[1]);
512                 dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
513         } else {
514                 do_unpowergate_locked(dev->powergate_ids[0]);
515                 do_unpowergate_locked(dev->powergate_ids[1]);
516                 dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
517         }
518
519         /* Init the power sysfs attributes for this device */
520         dev->power_attrib = kzalloc(sizeof(struct nvhost_device_power_attr),
521                 GFP_KERNEL);
522         if (!dev->power_attrib) {
523                 dev_err(&dev->dev, "Unable to allocate sysfs attributes\n");
524                 return -ENOMEM;
525         }
526         dev->power_attrib->ndev = dev;
527
528         dev->power_kobj = kobject_create_and_add("acm", &dev->dev.kobj);
529         if (!dev->power_kobj) {
530                 dev_err(&dev->dev, "Could not add dir 'power'\n");
531                 err = -EIO;
532                 goto fail_attrib_alloc;
533         }
534
535         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY];
536         attr->attr.name = "clockgate_delay";
537         attr->attr.mode = S_IWUSR | S_IRUGO;
538         attr->show = clockgate_delay_show;
539         attr->store = clockgate_delay_store;
540         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
541                 dev_err(&dev->dev, "Could not create sysfs attribute clockgate_delay\n");
542                 err = -EIO;
543                 goto fail_clockdelay;
544         }
545
546         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY];
547         attr->attr.name = "powergate_delay";
548         attr->attr.mode = S_IWUSR | S_IRUGO;
549         attr->show = powergate_delay_show;
550         attr->store = powergate_delay_store;
551         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
552                 dev_err(&dev->dev, "Could not create sysfs attribute powergate_delay\n");
553                 err = -EIO;
554                 goto fail_powergatedelay;
555         }
556
557         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_REFCOUNT];
558         attr->attr.name = "refcount";
559         attr->attr.mode = S_IRUGO;
560         attr->show = refcount_show;
561         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
562                 dev_err(&dev->dev, "Could not create sysfs attribute refcount\n");
563                 err = -EIO;
564                 goto fail_refcount;
565         }
566
567         return 0;
568
569 fail_refcount:
570         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY];
571         sysfs_remove_file(dev->power_kobj, &attr->attr);
572
573 fail_powergatedelay:
574         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY];
575         sysfs_remove_file(dev->power_kobj, &attr->attr);
576
577 fail_clockdelay:
578         kobject_put(dev->power_kobj);
579
580 fail_attrib_alloc:
581         kfree(dev->power_attrib);
582
583         return err;
584 }
585
586 static int is_module_idle(struct nvhost_device *dev)
587 {
588         int count;
589         mutex_lock(&dev->lock);
590         count = dev->refcount;
591         mutex_unlock(&dev->lock);
592         return (count == 0);
593 }
594
595 int nvhost_module_suspend(struct nvhost_device *dev)
596 {
597         int ret;
598         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
599
600         ret = wait_event_timeout(dev->idle_wq, is_module_idle(dev),
601                         ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT);
602         if (ret == 0) {
603                 dev_info(&dev->dev, "%s prevented suspend\n",
604                                 dev->name);
605                 return -EBUSY;
606         }
607
608         mutex_lock(&dev->lock);
609         cancel_delayed_work(&dev->powerstate_down);
610         to_state_powergated_locked(dev);
611         mutex_unlock(&dev->lock);
612
613         if (drv->suspend_ndev)
614                 drv->suspend_ndev(dev);
615
616         return 0;
617 }
618
619 void nvhost_module_deinit(struct nvhost_device *dev)
620 {
621         int i;
622         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
623
624         if (drv->deinit)
625                 drv->deinit(dev);
626
627         nvhost_module_suspend(dev);
628         for (i = 0; i < dev->num_clks; i++)
629                 clk_put(dev->clk[i]);
630         dev->powerstate = NVHOST_POWER_STATE_DEINIT;
631 }
632
633 /* public host1x power management APIs */
634 bool nvhost_module_powered_ext(struct nvhost_device *dev)
635 {
636         return nvhost_module_powered(dev);
637 }
638
639 void nvhost_module_busy_ext(struct nvhost_device *dev)
640 {
641         nvhost_module_busy(dev);
642 }
643
644 void nvhost_module_idle_ext(struct nvhost_device *dev)
645 {
646         nvhost_module_idle(dev);
647 }