summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/nvhost_acm.c
blob: 6d96bd023d81812dabbb82fc336e2703fdb070f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * drivers/video/tegra/host/nvhost_acm.c
 *
 * Tegra Graphics Host Automatic Clock Management
 *
 * Copyright (c) 2010-2012, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "nvhost_acm.h"
#include "dev.h"
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <mach/powergate.h>
#include <mach/clk.h>
#include <mach/hardware.h>

#define ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT (2 * HZ)
#define POWERGATE_DELAY 10
#define MAX_DEVID_LENGTH 16

DEFINE_MUTEX(client_list_lock);

struct nvhost_module_client {
	struct list_head node;
	unsigned long rate[NVHOST_MODULE_MAX_CLOCKS];
	void *priv;
};

static void do_powergate_locked(int id)
{
	if (id != -1 && tegra_powergate_is_powered(id))
		tegra_powergate_partition(id);
}

static void do_unpowergate_locked(int id)
{
	if (id != -1)
		tegra_unpowergate_partition(id);
}

static void do_module_reset_locked(struct nvhost_device *dev)
{
	/* assert module and mc client reset */
	if (dev->powergate_ids[0] != -1) {
		tegra_powergate_mc_disable(dev->powergate_ids[0]);
		tegra_periph_reset_assert(dev->clk[0]);
		tegra_powergate_mc_flush(dev->powergate_ids[0]);
	}
	if (dev->powergate_ids[1] != -1) {
		tegra_powergate_mc_disable(dev->powergate_ids[1]);
		tegra_periph_reset_assert(dev->clk[1]);
		tegra_powergate_mc_flush(dev->powergate_ids[1]);
	}

	udelay(POWERGATE_DELAY);

	/* deassert reset */
	if (dev->powergate_ids[0] != -1) {
		tegra_powergate_mc_flush_done(dev->powergate_ids[0]);
		tegra_periph_reset_deassert(dev->clk[0]);
		tegra_powergate_mc_enable(dev->powergate_ids[0]);
	}
	if (dev->powergate_ids[1] != -1) {
		tegra_powergate_mc_flush_done(dev->powergate_ids[1]);
		tegra_periph_reset_deassert(dev->clk[1]);
		tegra_powergate_mc_enable(dev->powergate_ids[1]);
	}
}

void nvhost_module_reset(struct nvhost_device *dev)
{
	dev_dbg(&dev->dev,
		"%s: asserting %s module reset (id %d, id2 %d)\n",
		__func__, dev->name,
		dev->powergate_ids[0], dev->powergate_ids[1]);

	mutex_lock(&dev->lock);
	do_module_reset_locked(dev);
	mutex_unlock(&dev->lock);

	dev_dbg(&dev->dev, "%s: module %s out of reset\n",
		__func__, dev->name);
}

static void to_state_clockgated_locked(struct nvhost_device *dev)
{
	if (dev->powerstate == NVHOST_POWER_STATE_RUNNING) {
		int i;
		for (i = 0; i < dev->num_clks; i++)
			clk_disable(dev->clk[i]);
		if (dev->dev.parent)
			nvhost_module_idle(to_nvhost_device(dev->dev.parent));
	} else if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED
			&& dev->can_powergate) {
		do_unpowergate_locked(dev->powergate_ids[0]);
		do_unpowergate_locked(dev->powergate_ids[1]);

		if (dev->powerup_reset)
			do_module_reset_locked(dev);
	}
	dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
}

static void to_state_running_locked(struct nvhost_device *dev)
{
	struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
	int prev_state = dev->powerstate;

	if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED)
		to_state_clockgated_locked(dev);

	if (dev->powerstate == NVHOST_POWER_STATE_CLOCKGATED) {
		int i;

		if (dev->dev.parent)
			nvhost_module_busy(to_nvhost_device(dev->dev.parent));

		for (i = 0; i < dev->num_clks; i++) {
			int err = clk_enable(dev->clk[i]);
			BUG_ON(err);
		}

		if (prev_state == NVHOST_POWER_STATE_POWERGATED
				&& drv->finalize_poweron)
			drv->finalize_poweron(dev);
	}
	dev->powerstate = NVHOST_POWER_STATE_RUNNING;
}

/* This gets called from powergate_handler() and from module suspend.
 * Module suspend is done for all modules, runtime power gating only
 * for modules with can_powergate set.
 */
static int to_state_powergated_locked(struct nvhost_device *dev)
{
	int err = 0;
	struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);

	if (drv->prepare_poweroff
			&& dev->powerstate != NVHOST_POWER_STATE_POWERGATED) {
		/* Clock needs to be on in prepare_poweroff */
		to_state_running_locked(dev);
		err = drv->prepare_poweroff(dev);
		if (err)
			return err;
	}

	if (dev->powerstate == NVHOST_POWER_STATE_RUNNING)
		to_state_clockgated_locked(dev);

	if (dev->can_powergate) {
		do_powergate_locked(dev->powergate_ids[0]);
		do_powergate_locked(dev->powergate_ids[1]);
	}

	dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
	return 0;
}

static void schedule_powergating_locked(struct nvhost_device *dev)
{
	if (dev->can_powergate)
		schedule_delayed_work(&dev->powerstate_down,
				msecs_to_jiffies(dev->powergate_delay));
}

static void schedule_clockgating_locked(struct nvhost_device *dev)
{
	schedule_delayed_work(&dev->powerstate_down,
			msecs_to_jiffies(dev->clockgate_delay));
}

void nvhost_module_busy(struct nvhost_device *dev)
{
	struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);

	if (drv->busy)
		drv->busy(dev);

	mutex_lock(&dev->lock);
	cancel_delayed_work(&dev->powerstate_down);

	dev->refcount++;
	if (dev->refcount > 0 && !nvhost_module_powered(dev))
		to_state_running_locked(dev);
	mutex_unlock(&dev->lock);
}

static void powerstate_down_handler(struct work_struct *work)
{
	struct nvhost_device *dev;

	dev = container_of(to_delayed_work(work),
			struct nvhost_device,
			powerstate_down);

	mutex_lock(&dev->lock);
	if (dev->refcount == 0) {
		switch (dev->powerstate) {
		case NVHOST_POWER_STATE_RUNNING:
			to_state_clockgated_locked(dev);
			schedule_powergating_locked(dev);
			break;
		case NVHOST_POWER_STATE_CLOCKGATED:
			if (to_state_powergated_locked(dev))
				schedule_powergating_locked(dev);
			break;
		default:
			break;
		}
	}
	mutex_unlock(&dev->lock);
}


void nvhost_module_idle_mult(struct nvhost_device *dev, int refs)
{
	struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
	bool kick = false;

	mutex_lock(&dev->lock);
	dev->refcount -= refs;
	if (dev->refcount == 0) {
		if (nvhost_module_powered(dev))
			schedule_clockgating_locked(dev);
		kick = true;
	}
	mutex_unlock(&dev->lock);

	if (kick) {
		wake_up(&dev->idle_wq);

		if (drv->idle)
			drv->idle(dev);
	}
}

int nvhost_module_get_rate(struct nvhost_device *dev, unsigned long *rate,
		int index)
{
	struct clk *c;

	c = dev->clk[index];
	if (IS_ERR_OR_NULL(c))
		return -EINVAL;

	/* Need to enable client to get correct rate */
	nvhost_module_busy(dev);
	*rate = clk_get_rate(c);
	nvhost_module_idle(dev);
	return 0;

}

static int nvhost_module_update_rate(struct nvhost_device *dev, int index)
{
	unsigned long rate = 0;
	struct nvhost_module_client *m;

	if (!dev->clk[index])
		return -EINVAL;

	list_for_each_entry(m, &dev->client_list, node) {
		rate = max(m->rate[index], rate);
	}
	if (!rate)
		rate = clk_round_rate(dev->clk[index],
				dev->clocks[index].default_rate);

	return clk_set_rate(dev->clk[index], rate);
}

int nvhost_module_set_rate(struct nvhost_device *dev, void *priv,
		unsigned long rate, int index)
{
	struct nvhost_module_client *m;
	int i, ret = 0;

	mutex_lock(&client_list_lock);
	list_for_each_entry(m, &dev->client_list, node) {
		if (m->priv == priv) {
			for (i = 0; i < dev->num_clks; i++)
				m->rate[i] = clk_round_rate(dev->clk[i], rate);
			break;
		}
	}

	for (i = 0; i < dev->num_clks; i++) {
		ret = nvhost_module_update_rate(dev, i);
		if (ret < 0)
			break;
	}
	mutex_unlock(&client_list_lock);
	return ret;

}

int nvhost_module_add_client(struct nvhost_device *dev, void *priv)
{
	int i;
	unsigned long rate;
	struct nvhost_module_client *client;

	client = kzalloc(sizeof(*client), GFP_KERNEL);
	if (!client)
		return -ENOMEM;

	INIT_LIST_HEAD(&client->node);
	client->priv = priv;

	for (i = 0; i < dev->num_clks; i++) {
		rate = clk_round_rate(dev->clk[i],
				dev->clocks[i].default_rate);
		client->rate[i] = rate;
	}
	mutex_lock(&client_list_lock);
	list_add_tail(&client->node, &dev->client_list);
	mutex_unlock(&client_list_lock);
	return 0;
}

void nvhost_module_remove_client(struct nvhost_device *dev, void *priv)
{
	int i;
	struct nvhost_module_client *m;

	mutex_lock(&client_list_lock);
	list_for_each_entry(m, &dev->client_list, node) {
		if (priv == m->priv) {
			list_del(&m->node);
			break;
		}
	}
	if (m) {
		kfree(m);
		for (i = 0; i < dev->num_clks; i++)
			nvhost_module_update_rate(dev, i);
	}
	mutex_unlock(&client_list_lock);
}

int nvhost_module_init(struct nvhost_device *dev)
{
	int i = 0;

	/* initialize clocks to known state */
	INIT_LIST_HEAD(&dev->client_list);
	while (dev->clocks[i].name && i < NVHOST_MODULE_MAX_CLOCKS) {
		char devname[MAX_DEVID_LENGTH];
		long rate = dev->clocks[i].default_rate;
		struct clk *c;

		snprintf(devname, MAX_DEVID_LENGTH, "tegra_%s", dev->name);
		c = clk_get_sys(devname, dev->clocks[i].name);
		BUG_ON(IS_ERR_OR_NULL(c));

		rate = clk_round_rate(c, rate);
		clk_enable(c);
		clk_set_rate(c, rate);
		clk_disable(c);
		dev->clk[i] = c;
		i++;
	}
	dev->num_clks = i;

	mutex_init(&dev->lock);
	init_waitqueue_head(&dev->idle_wq);
	INIT_DELAYED_WORK(&dev->powerstate_down, powerstate_down_handler);

	/* power gate units that we can power gate */
	if (dev->can_powergate) {
		do_powergate_locked(dev->powergate_ids[0]);
		do_powergate_locked(dev->powergate_ids[1]);
		dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
	} else {
		do_unpowergate_locked(dev->powergate_ids[0]);
		do_unpowergate_locked(dev->powergate_ids[1]);
		dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
	}

	return 0;
}

static int is_module_idle(struct nvhost_device *dev)
{
	int count;
	mutex_lock(&dev->lock);
	count = dev->refcount;
	mutex_unlock(&dev->lock);
	return (count == 0);
}

int nvhost_module_suspend(struct nvhost_device *dev)
{
	int ret;
	struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);

	ret = wait_event_timeout(dev->idle_wq, is_module_idle(dev),
			ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT);
	if (ret == 0) {
		dev_info(&dev->dev, "%s prevented suspend\n",
				dev->name);
		return -EBUSY;
	}

	mutex_lock(&dev->lock);
	cancel_delayed_work(&dev->powerstate_down);
	to_state_powergated_locked(dev);
	mutex_unlock(&dev->lock);

	if (drv->suspend_ndev)
		drv->suspend_ndev(dev);

	return 0;
}

void nvhost_module_deinit(struct nvhost_device *dev)
{
	int i;
	struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);

	if (drv->deinit)
		drv->deinit(dev);

	nvhost_module_suspend(dev);
	for (i = 0; i < dev->num_clks; i++)
		clk_put(dev->clk[i]);
	dev->powerstate = NVHOST_POWER_STATE_DEINIT;
}