summaryrefstree
path: root/kernel/drivers/vision/vpu/vpu-queue.c
blob: c976d7c1327c207633664894002621f704b6d49b (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
/*
 * Samsung Exynos SoC series VPU driver
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "vpu-queue.h"
#include "vpu-framemgr.h"
#include "vpu-graph.h"

extern const struct vb_ops vb_ops;
extern const struct vpu_queue_ops vpu_queue_ops;

int vpu_queue_open(struct vpu_queue *queue,
	struct vpu_memory *memory,
	struct mutex *lock)
{
	int ret = 0;
	struct vb_queue *inq, *otq;

	queue->qops = &vpu_queue_ops;
	inq = &queue->inqueue;
	otq = &queue->otqueue;
	inq->private_data = queue;
	otq->private_data = queue;

	ret = vb_queue_init(inq, memory->alloc_ctx, memory->vb2_mem_ops, &vb_ops, lock, VS4L_DIRECTION_IN);
	if (ret) {
		vpu_err("vb_queue_init is fail(%d)\n", ret);
		goto p_err;
	}

	ret = vb_queue_init(otq, memory->alloc_ctx, memory->vb2_mem_ops, &vb_ops, lock, VS4L_DIRECTION_OT);
	if (ret) {
		vpu_err("vb_queue_init is fail(%d)\n", ret);
		goto p_err;
	}

p_err:
	return ret;
}

int vpu_queue_s_format(struct vpu_queue *queue, struct vs4l_format_list *f)
{
	int ret = 0;
	struct vb_queue *q, *inq, *otq;

	BUG_ON(!queue);
	BUG_ON(!f);

	inq = &queue->inqueue;
	otq = &queue->otqueue;

	if (f->direction == VS4L_DIRECTION_IN)
		q = inq;
	else
		q = otq;

	ret = CALL_QOPS(queue, format, f);
	if (ret) {
		vpu_err("CALL_QOPS(format) is fail(%d)\n", ret);
		goto p_err;
	}

	ret = vb_queue_s_format(q, f);
	if (ret) {
		vpu_err("vb_queue_s_format is fail(%d)\n", ret);
		goto p_err;
	}

p_err:
	return ret;
}

int vpu_queue_start(struct vpu_queue *queue)
{
	int ret = 0;
	struct vb_queue *inq, *otq;

	inq = &queue->inqueue;
	otq = &queue->otqueue;

	ret = vb_queue_start(inq);
	if (ret) {
		vpu_err("vb_queue_init is fail(%d)\n", ret);
		goto p_err;
	}

	ret = vb_queue_start(otq);
	if (ret) {
		vpu_err("vb_queue_init is fail(%d)\n", ret);
		goto p_err;
	}

	ret = CALL_QOPS(queue, start);
	if (ret) {
		vpu_err("CALL_QOPS(start) is fail(%d)\n", ret);
		goto p_err;
	}

p_err:
	return ret;
}

int vpu_queue_stop(struct vpu_queue *queue)
{
	int ret = 0;
	struct vb_queue *inq, *otq;

	inq = &queue->inqueue;
	otq = &queue->otqueue;

	ret = CALL_QOPS(queue, stop);
	if (ret) {
		vpu_err("CALL_QOPS(stop) is fail(%d)\n", ret);
		goto p_err;
	}

	ret = vb_queue_stop(inq);
	if (ret) {
		vpu_err("vb_queue_init is fail(%d)\n", ret);
		goto p_err;
	}

	ret = vb_queue_stop(otq);
	if (ret) {
		vpu_err("vb_queue_init is fail(%d)\n", ret);
		goto p_err;
	}

p_err:
	return ret;
}

int vpu_queue_poll(struct vpu_queue *queue, struct file *file, poll_table *poll)
{
	int ret = 0;
	struct vb_queue *inq, *otq;
	unsigned long events;

	BUG_ON(!queue);
	BUG_ON(!file);
	BUG_ON(!poll);

	events = poll_requested_events(poll);

	inq = &queue->inqueue;
	otq = &queue->otqueue;

	if (events & POLLIN) {
		if (list_empty(&inq->done_list))
			poll_wait(file, &inq->done_wq, poll);

		if (list_empty(&inq->done_list))
			ret |= POLLIN | POLLWRNORM;
	}

	if (events & POLLOUT) {
		if (list_empty(&otq->done_list))
			poll_wait(file, &otq->done_wq, poll);

		if (list_empty(&otq->done_list))
			ret |= POLLOUT | POLLWRNORM;
	}

	return ret;
}

int vpu_queue_qbuf(struct vpu_queue *queue, struct vs4l_container_list *c)
{
	int ret = 0;
	struct vb_queue *q, *inq, *otq;
	struct vb_bundle *invb, *otvb;

	inq = &queue->inqueue;
	otq = &queue->otqueue;

	if (c->direction == VS4L_DIRECTION_IN)
		q = inq;
	else
		q = otq;

	ret = vb_queue_qbuf(q, c);
	if (ret) {
		vpu_err("vb_queue_qbuf is fail(%d)\n", ret);
		goto p_err;
	}

	if (list_empty(&inq->queued_list))
		goto p_err;

	if (list_empty(&otq->queued_list))
		goto p_err;

	invb = list_first_entry(&inq->queued_list, struct vb_bundle, queued_entry);
	otvb = list_first_entry(&otq->queued_list, struct vb_bundle, queued_entry);

	vb_queue_process(inq, invb);
	vb_queue_process(otq, otvb);

	ret = CALL_QOPS(queue, queue, &invb->clist, &otvb->clist);
	if (ret) {
		vpu_err("CALL_QOPS(queue) is fail(%d)\n", ret);
		goto p_err;
	}

p_err:
	return ret;
}

int vpu_queue_dqbuf(struct vpu_queue *queue, struct vs4l_container_list *c, bool nonblocking)
{
	int ret = 0;
	struct vb_queue *q;
	struct vb_bundle *bundle;

	BUG_ON(!queue);

	if (c->direction == VS4L_DIRECTION_IN)
		q = &queue->inqueue;
	else
		q = &queue->otqueue;

	ret = vb_queue_dqbuf(q, c, nonblocking);
	if (ret) {
		vpu_err("vb_queue_dqbuf is fail(%d)\n", ret);
		goto p_err;
	}

	if (c->index >= VPU_MAX_BUFFER) {
		vpu_err("container index(%d) is invalid\n", c->index);
		ret = -EINVAL;
		goto p_err;
	}

	bundle = q->bufs[c->index];
	if (!bundle) {
		vpu_err("bundle(%d) is NULL\n", c->index);
		ret = -EINVAL;
		goto p_err;
	}

	if (bundle->clist.index != c->index) {
		vpu_err("index is NOT matched(%d != %d)\n", bundle->clist.index, c->index);
		ret = -EINVAL;
		goto p_err;
	}

	ret = CALL_QOPS(queue, deque, &bundle->clist);
	if (ret) {
		vpu_err("CALL_QOPS(deque) is fail(%d)\n", ret);
		goto p_err;
	}

p_err:
	return ret;
}

void vpu_queue_done(struct vpu_queue *queue,
	struct vb_container_list *incl,
	struct vb_container_list *otcl,
	unsigned long flags)
{
	struct vb_queue *inq, *otq;
	struct vb_bundle *invb, *otvb;

	BUG_ON(!queue);
	BUG_ON(!incl);
	BUG_ON(!otcl);

	inq = &queue->inqueue;
	otq = &queue->otqueue;

	if (list_empty(&inq->process_list)) {
		vpu_err("inqueue is empty\n");
		BUG();
	}

	if (list_empty(&otq->process_list)) {
		vpu_err("otqueue is empty\n");
		BUG();
	}

	invb = container_of(incl, struct vb_bundle, clist);
	otvb = container_of(otcl, struct vb_bundle, clist);

	if (invb->state != VB_BUF_STATE_PROCESS) {
		vpu_err("invb state(%d) is invalid\n", invb->state);
		BUG();
	}

	if (otvb->state != VB_BUF_STATE_PROCESS) {
		vpu_err("otvb state(%d) is invalid\n", otvb->state);
		BUG();
	}

	otvb->flags |= flags;
	vb_queue_done(otq, otvb);

	invb->flags |= flags;
	vb_queue_done(inq, invb);
}