summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/nvhost_channel.c
blob: 70fb173d7f4160ee7976a889a21cf4ded7702033 (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
/*
 * drivers/video/tegra/host/nvhost_channel.c
 *
 * Tegra Graphics Host Channel
 *
 * Copyright (c) 2010, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "nvhost_channel.h"
#include "dev.h"
#include "nvhost_hwctx.h"

#include <linux/platform_device.h>

#define NVMODMUTEX_2D_FULL   (1)
#define NVMODMUTEX_2D_SIMPLE (2)
#define NVMODMUTEX_2D_SB_A   (3)
#define NVMODMUTEX_2D_SB_B   (4)
#define NVMODMUTEX_3D        (5)
#define NVMODMUTEX_DISPLAYA  (6)
#define NVMODMUTEX_DISPLAYB  (7)
#define NVMODMUTEX_VI        (8)
#define NVMODMUTEX_DSI       (9)

static void power_2d(struct nvhost_module *mod, enum nvhost_power_action action);
static void power_3d(struct nvhost_module *mod, enum nvhost_power_action action);
static void power_mpe(struct nvhost_module *mod, enum nvhost_power_action action);

static const struct nvhost_channeldesc channelmap[] = {
{
	/* channel 0 */
	.name	       = "display",
	.syncpts       = BIT(NVSYNCPT_DISP0) | BIT(NVSYNCPT_DISP1) |
			 BIT(NVSYNCPT_VBLANK0) | BIT(NVSYNCPT_VBLANK1),
	.modulemutexes = BIT(NVMODMUTEX_DISPLAYA) | BIT(NVMODMUTEX_DISPLAYB),
},
{
	/* channel 1 */
	.name	       = "gr3d",
	.syncpts       = BIT(NVSYNCPT_3D),
	.waitbases     = BIT(NVWAITBASE_3D),
	.modulemutexes = BIT(NVMODMUTEX_3D),
	.class	       = NV_GRAPHICS_3D_CLASS_ID,
	.power         = power_3d,
},
{
	/* channel 2 */
	.name	       = "gr2d",
	.syncpts       = BIT(NVSYNCPT_2D_0) | BIT(NVSYNCPT_2D_1),
	.waitbases     = BIT(NVWAITBASE_2D_0) | BIT(NVWAITBASE_2D_1),
	.modulemutexes = BIT(NVMODMUTEX_2D_FULL) | BIT(NVMODMUTEX_2D_SIMPLE) |
			 BIT(NVMODMUTEX_2D_SB_A) | BIT(NVMODMUTEX_2D_SB_B),
	.power         = power_2d,
},
{
	/* channel 3 */
	.name	 = "isp",
	.syncpts = 0,
},
{
	/* channel 4 */
	.name	       = "vi",
	.syncpts       = BIT(NVSYNCPT_VI_ISP_0) | BIT(NVSYNCPT_VI_ISP_1) |
			 BIT(NVSYNCPT_VI_ISP_2) | BIT(NVSYNCPT_VI_ISP_3) |
			 BIT(NVSYNCPT_VI_ISP_4) | BIT(NVSYNCPT_VI_ISP_5),
	.modulemutexes = BIT(NVMODMUTEX_VI),
},
{
	/* channel 5 */
	.name	       = "mpe",
	.syncpts       = BIT(NVSYNCPT_MPE) | BIT(NVSYNCPT_MPE_EBM_EOF) |
			 BIT(NVSYNCPT_MPE_WR_SAFE),
	.waitbases     = BIT(NVWAITBASE_MPE),
	.class	       = NV_VIDEO_ENCODE_MPEG_CLASS_ID,
	.power	       = power_mpe,
},
{
	/* channel 6 */
	.name	       = "dsi",
	.syncpts       = BIT(NVSYNCPT_DSI),
	.modulemutexes = BIT(NVMODMUTEX_DSI),
}};

static inline void __iomem *channel_aperture(void __iomem *p, int ndx)
{
	ndx += NVHOST_CHANNEL_BASE;
	p += NV_HOST1X_CHANNEL0_BASE;
	p += ndx * NV_HOST1X_CHANNEL_MAP_SIZE_BYTES;
	return p;
}

int __init nvhost_channel_init(struct nvhost_channel *ch,
			struct nvhost_master *dev, int index)
{
	BUILD_BUG_ON(NVHOST_NUMCHANNELS != ARRAY_SIZE(channelmap));

	ch->dev = dev;
	ch->desc = &channelmap[index];
	ch->aperture = channel_aperture(dev->aperture, index);
	mutex_init(&ch->reflock);
	mutex_init(&ch->submitlock);

	return nvhost_hwctx_handler_init(&ch->ctxhandler, ch->desc->name);
}

struct nvhost_channel *nvhost_getchannel(struct nvhost_channel *ch)
{
	int err = 0;
	mutex_lock(&ch->reflock);
	if (ch->refcount == 0) {
		err = nvhost_module_init(&ch->mod, ch->desc->name,
					ch->desc->power, &ch->dev->mod,
					&ch->dev->pdev->dev);
		if (!err) {
			err = nvhost_cdma_init(&ch->cdma);
			if (err)
				nvhost_module_deinit(&ch->mod);
		}
	}
	if (!err) {
		ch->refcount++;
	}
	mutex_unlock(&ch->reflock);

	return err ? NULL : ch;
}

void nvhost_putchannel(struct nvhost_channel *ch, struct nvhost_hwctx *ctx)
{
	if (ctx) {
		mutex_lock(&ch->submitlock);
		if (ch->cur_ctx == ctx)
			ch->cur_ctx = NULL;
		mutex_unlock(&ch->submitlock);
	}

	mutex_lock(&ch->reflock);
	if (ch->refcount == 1) {
		nvhost_module_deinit(&ch->mod);
		/* cdma may already be stopped, that's ok */
		nvhost_cdma_stop(&ch->cdma);
		nvhost_cdma_deinit(&ch->cdma);
	}
	ch->refcount--;
	mutex_unlock(&ch->reflock);
}

void nvhost_channel_suspend(struct nvhost_channel *ch)
{
	mutex_lock(&ch->reflock);
	BUG_ON(nvhost_module_powered(&ch->mod));
	nvhost_cdma_stop(&ch->cdma);
	mutex_unlock(&ch->reflock);
}

void nvhost_channel_submit(struct nvhost_channel *ch,
			   struct nvmap_client *user_nvmap,
			   struct nvhost_op_pair *ops, int num_pairs,
			   struct nvhost_cpuinterrupt *intrs, int num_intrs,
			   struct nvmap_handle **unpins, int num_unpins,
			   u32 syncpt_id, u32 syncpt_val,
			   int num_nulled_incrs)
{
	int i;
	struct nvhost_op_pair* p;

	/* schedule interrupts */
	for (i = 0; i < num_intrs; i++) {
		nvhost_intr_add_action(&ch->dev->intr, syncpt_id, intrs[i].syncpt_val,
				NVHOST_INTR_ACTION_CTXSAVE, intrs[i].intr_data, NULL);
	}

	/* begin a CDMA submit */
	nvhost_cdma_begin(&ch->cdma);

	/* push ops */
	for (i = 0, p = ops; i < num_pairs; i++, p++)
		nvhost_cdma_push(&ch->cdma, p->op1, p->op2);

	/* extra work to do for null kickoff */
	if (num_nulled_incrs) {
		u32 incr;
		u32 op_incr;

		/* TODO ideally we'd also perform host waits here */

		/* push increments that correspond to nulled out commands */
		op_incr = nvhost_opcode_imm(0, 0x100 | syncpt_id);
		for (incr = 0; incr < (num_nulled_incrs >> 1); incr++)
			nvhost_cdma_push(&ch->cdma, op_incr, op_incr);
		if (num_nulled_incrs & 1)
			nvhost_cdma_push(&ch->cdma, op_incr, NVHOST_OPCODE_NOOP);

		/* for 3d, waitbase needs to be incremented after each submit */
		if (ch->desc->class == NV_GRAPHICS_3D_CLASS_ID) {
			u32 op1 = nvhost_opcode_setclass(NV_HOST1X_CLASS_ID,
					NV_CLASS_HOST_INCR_SYNCPT_BASE, 1);
			u32 op2 = nvhost_class_host_incr_syncpt_base(NVWAITBASE_3D,
					num_nulled_incrs);

			nvhost_cdma_push(&ch->cdma, op1, op2);
		}
	}

	/* end CDMA submit & stash pinned hMems into sync queue for later cleanup */
	nvhost_cdma_end(user_nvmap, &ch->cdma, syncpt_id, syncpt_val,
                        unpins, num_unpins);
}

static void power_2d(struct nvhost_module *mod, enum nvhost_power_action action)
{
	/* TODO: [ahatala 2010-06-17] reimplement EPP hang war */
	if (action == NVHOST_POWER_ACTION_OFF) {
		/* TODO: [ahatala 2010-06-17] reset EPP */
	}
}

static void power_3d(struct nvhost_module *mod, enum nvhost_power_action action)
{
	struct nvhost_channel *ch = container_of(mod, struct nvhost_channel, mod);

	if (action == NVHOST_POWER_ACTION_OFF) {
		mutex_lock(&ch->submitlock);
		if (ch->cur_ctx) {
			DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
			struct nvhost_op_pair save;
			struct nvhost_cpuinterrupt ctxsw;
			u32 syncval;
			void *ref;
			syncval = nvhost_syncpt_incr_max(&ch->dev->syncpt,
							NVSYNCPT_3D,
							ch->cur_ctx->save_incrs);
			save.op1 = nvhost_opcode_gather(0, ch->cur_ctx->save_size);
			save.op2 = ch->cur_ctx->save_phys;
			ctxsw.intr_data = ch->cur_ctx;
			ctxsw.syncpt_val = syncval - 1;
			ch->cur_ctx->valid = true;
			ch->ctxhandler.get(ch->cur_ctx);
			ch->cur_ctx = NULL;

			nvhost_channel_submit(ch, ch->dev->nvmap,
						&save, 1, &ctxsw, 1, NULL, 0,
						NVSYNCPT_3D, syncval, 0);

			nvhost_intr_add_action(&ch->dev->intr, NVSYNCPT_3D,
					       syncval,
					       NVHOST_INTR_ACTION_WAKEUP,
					       &wq, &ref);
			wait_event(wq,
				   nvhost_syncpt_min_cmp(&ch->dev->syncpt,
							 NVSYNCPT_3D, syncval));
			nvhost_intr_put_ref(&ch->dev->intr, ref);
			nvhost_cdma_update(&ch->cdma);
		}
		mutex_unlock(&ch->submitlock);
	}
}

static void power_mpe(struct nvhost_module *mod, enum nvhost_power_action action)
{
}