summaryrefslogtreecommitdiffstats
path: root/bltsville/gcbv/mirror/gcbuffer.c
blob: 7c77d4ac66e8d08cef89a4796e1fb814dd4779ef (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
/*
 * Copyright(c) 2012,
 * Texas Instruments, Inc. and Vivante Corporation.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Vivante Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "gcbv.h"

#define GCZONE_NONE		0
#define GCZONE_ALL		(~0U)
#define GCZONE_BATCH_ALLOC	(1 << 0)
#define GCZONE_BUFFER_ALLOC	(1 << 1)
#define GCZONE_FIXUP_ALLOC	(1 << 2)
#define GCZONE_FIXUP		(1 << 3)

GCDBG_FILTERDEF(buffer, GCZONE_NONE,
		"batchalloc",
		"bufferalloc"
		"fixupalloc",
		"fixup")


/*******************************************************************************
** Miscellaneous defines and macros.
*/

#define GC_BUFFER_INIT_SIZE \
( \
	GC_BUFFER_SIZE - max(sizeof(struct gcbuffer), GC_BUFFER_RESERVE) \
)

#define GC_BUFFER_RESERVE \
( \
	sizeof(struct gcmopipesel) + \
	sizeof(struct gcmommumaster) + \
	sizeof(struct gcmommuflush) + \
	sizeof(struct gcmosignal) + \
	sizeof(struct gccmdend) \
)


/*******************************************************************************
 * Batch/command buffer management.
 */

enum bverror do_end(struct bvbltparams *bvbltparams,
		    struct gcbatch *gcbatch)
{
	return BVERR_NONE;
}

enum bverror allocate_batch(struct bvbltparams *bvbltparams,
			    struct gcbatch **gcbatch)
{
	enum bverror bverror;
	struct gccontext *gccontext = get_context();
	struct gcbatch *temp;
	struct gcbuffer *gcbuffer;

	GCENTER(GCZONE_BATCH_ALLOC);

	/* Lock access to batch management. */
	GCLOCK(&gccontext->batchlock);

	if (list_empty(&gccontext->batchvac)) {
		temp = gcalloc(struct gcbatch, sizeof(struct gcbatch));
		if (temp == NULL) {
			BVSETBLTERROR(BVERR_OOM,
				      "batch header allocation failed");
			goto exit;
		}

		GCDBG(GCZONE_BATCH_ALLOC, "allocated new batch = 0x%08X\n",
		      (unsigned int) temp);
	} else {
		struct list_head *head;
		head = gccontext->batchvac.next;
		temp = list_entry(head, struct gcbatch, link);
		list_del(head);

		GCDBG(GCZONE_BATCH_ALLOC, "reusing batch = 0x%08X\n",
		      (unsigned int) temp);
	}

	memset(temp, 0, sizeof(struct gcbatch));
	temp->structsize = sizeof(struct gcbatch);
	temp->batchend = do_end;
	INIT_LIST_HEAD(&temp->buffer);
	INIT_LIST_HEAD(&temp->unmap);
	INIT_LIST_HEAD(&temp->link);

	bverror = append_buffer(bvbltparams, temp, &gcbuffer);
	if (bverror != BVERR_NONE) {
		free_batch(temp);
		goto exit;
	}

	*gcbatch = temp;

	GCDBG(GCZONE_BATCH_ALLOC, "batch allocated = 0x%08X\n",
	      (unsigned int) temp);

exit:
	/* Unlock access to batch management. */
	GCUNLOCK(&gccontext->batchlock);

	GCEXITARG(GCZONE_BATCH_ALLOC, "bv%s = %d\n",
		  (bverror == BVERR_NONE) ? "result" : "error", bverror);
	return bverror;
}

void free_batch(struct gcbatch *gcbatch)
{
	struct list_head *head;
	struct gccontext *gccontext = get_context();
	struct gcbuffer *gcbuffer;

	GCENTERARG(GCZONE_BATCH_ALLOC, "batch = 0x%08X\n",
		   (unsigned int) gcbatch);

	/* Lock access. */
	GCLOCK(&gccontext->batchlock);
	GCLOCK(&gccontext->bufferlock);
	GCLOCK(&gccontext->fixuplock);
	GCLOCK(&gccontext->maplock);

	/* Free implicit unmappings. */
	list_splice_init(&gcbatch->unmap, &gccontext->unmapvac);

	/* Free command buffers. */
	while (!list_empty(&gcbatch->buffer)) {
		head = gcbatch->buffer.next;
		gcbuffer = list_entry(head, struct gcbuffer, link);

		/* Free fixups. */
		list_splice_init(&gcbuffer->fixup, &gccontext->fixupvac);

		/* Free the command buffer. */
		list_move(&gcbuffer->link, &gccontext->buffervac);
	}

	/* Free the batch. */
	list_add(&gcbatch->link, &gccontext->batchvac);

	/* Unlock access. */
	GCUNLOCK(&gccontext->maplock);
	GCUNLOCK(&gccontext->fixuplock);
	GCUNLOCK(&gccontext->bufferlock);
	GCUNLOCK(&gccontext->batchlock);

	GCEXIT(GCZONE_BATCH_ALLOC);
}

enum bverror append_buffer(struct bvbltparams *bvbltparams,
			   struct gcbatch *gcbatch,
			   struct gcbuffer **gcbuffer)
{
	enum bverror bverror;
	struct gccontext *gccontext = get_context();
	struct gcbuffer *temp;

	GCENTERARG(GCZONE_BUFFER_ALLOC, "batch = 0x%08X\n",
		   (unsigned int) gcbatch);

	/* Lock access to buffer management. */
	GCLOCK(&gccontext->bufferlock);

	if (list_empty(&gccontext->buffervac)) {
		temp = gcalloc(struct gcbuffer, GC_BUFFER_SIZE);
		if (temp == NULL) {
			BVSETBLTERROR(BVERR_OOM,
				      "command buffer allocation failed");
			goto exit;
		}

		list_add_tail(&temp->link, &gcbatch->buffer);

		GCDBG(GCZONE_BUFFER_ALLOC, "allocated new buffer = 0x%08X\n",
		      (unsigned int) temp);
	} else {
		struct list_head *head;
		head = gccontext->buffervac.next;
		temp = list_entry(head, struct gcbuffer, link);

		list_move_tail(&temp->link, &gcbatch->buffer);

		GCDBG(GCZONE_BUFFER_ALLOC, "reusing buffer = 0x%08X\n",
		      (unsigned int) temp);
	}

	INIT_LIST_HEAD(&temp->fixup);
	temp->pixelcount = 0;
	temp->head = temp->tail = (unsigned int *) (temp + 1);
	temp->available = GC_BUFFER_INIT_SIZE;

	GCDBG(GCZONE_BUFFER_ALLOC, "new buffer appended = 0x%08X\n",
	      (unsigned int) temp);

	*gcbuffer = temp;
	bverror = BVERR_NONE;

exit:
	/* Unlock access to buffer management. */
	GCUNLOCK(&gccontext->bufferlock);

	GCEXITARG(GCZONE_BUFFER_ALLOC, "bv%s = %d\n",
		  (bverror == BVERR_NONE) ? "result" : "error", bverror);
	return bverror;
}

static enum bverror allocate_fixup(struct bvbltparams *bvbltparams,
				   struct gcbuffer *gcbuffer,
				   struct gcfixup **gcfixup)
{
	enum bverror bverror = BVERR_NONE;
	struct gccontext *gccontext = get_context();
	struct gcfixup *temp;

	if (list_empty(&gccontext->fixupvac)) {
		temp = gcalloc(struct gcfixup, sizeof(struct gcfixup));
		if (temp == NULL) {
			BVSETBLTERROR(BVERR_OOM, "fixup allocation failed");
			goto exit;
		}

		list_add_tail(&temp->link, &gcbuffer->fixup);

		GCDBG(GCZONE_FIXUP_ALLOC,
		      "new fixup struct allocated = 0x%08X\n",
		      (unsigned int) temp);
	} else {
		struct list_head *head;
		head = gccontext->fixupvac.next;
		temp = list_entry(head, struct gcfixup, link);

		list_move_tail(&temp->link, &gcbuffer->fixup);

		GCDBG(GCZONE_FIXUP_ALLOC, "fixup struct reused = 0x%08X\n",
			(unsigned int) temp);
	}

	temp->count = 0;
	*gcfixup = temp;

exit:
	return bverror;
}

enum bverror add_fixup(struct bvbltparams *bvbltparams,
		       struct gcbatch *gcbatch,
		       unsigned int *ptr,
		       unsigned int surfoffset)
{
	enum bverror bverror = BVERR_NONE;
	struct gccontext *gccontext = get_context();
	struct list_head *head;
	struct gcbuffer *buffer;
	struct gcfixup *gcfixup;

	GCENTERARG(GCZONE_FIXUP, "batch = 0x%08X, fixup ptr = 0x%08X\n",
		   (unsigned int) gcbatch, (unsigned int) ptr);

	/* Lock access to fixup management. */
	GCLOCK(&gccontext->fixuplock);

	/* Get the current command buffer. */
	if (list_empty(&gcbatch->buffer)) {
		GCERR("no command buffers are allocated");
		goto exit;
	}
	head = gcbatch->buffer.prev;
	buffer = list_entry(head, struct gcbuffer, link);

	/* No fixups? Allocate one. */
	if (list_empty(&buffer->fixup)) {
		GCDBG(GCZONE_FIXUP_ALLOC, "no fixups allocated.\n");
		bverror = allocate_fixup(bvbltparams, buffer, &gcfixup);
		if (bverror != BVERR_NONE)
			goto exit;
	} else {
		/* Get the current fixup. */
		head = buffer->fixup.prev;
		gcfixup = list_entry(head, struct gcfixup, link);

		/* No more room? */
		if (gcfixup->count == GC_FIXUP_MAX) {
			GCDBG(GCZONE_FIXUP_ALLOC,
			      "out of room, allocating new.\n");
			bverror = allocate_fixup(bvbltparams, buffer, &gcfixup);
			if (bverror != BVERR_NONE)
				goto exit;
		}
	}

	GCDBG(GCZONE_FIXUP, "buffer = 0x%08X, fixup struct = 0x%08X\n",
	      (unsigned int) buffer, (unsigned int) gcfixup);

	gcfixup->fixup[gcfixup->count].dataoffset = ptr - buffer->head;
	gcfixup->fixup[gcfixup->count].surfoffset = surfoffset;
	gcfixup->count += 1;

	GCDBG(GCZONE_FIXUP, "fixup offset = 0x%08X\n", ptr - buffer->head);
	GCDBG(GCZONE_FIXUP, "surface offset = 0x%08X\n", surfoffset);

exit:
	/* Unlock access to fixup management. */
	GCUNLOCK(&gccontext->fixuplock);

	GCEXITARG(GCZONE_FIXUP, "bv%s = %d\n",
		  (bverror == BVERR_NONE) ? "result" : "error", bverror);
	return bverror;
}

enum bverror claim_buffer(struct bvbltparams *bvbltparams,
			  struct gcbatch *gcbatch,
			  unsigned int size,
			  void **buffer)
{
	enum bverror bverror;
	struct list_head *head;
	struct gcbuffer *gcbuffer;

	GCENTERARG(GCZONE_BUFFER_ALLOC, "batch = 0x%08X, size = %d\n",
		   (unsigned int) gcbatch, size);

	if (size > GC_BUFFER_INIT_SIZE) {
		GCERR("requested size is too big.\n");
		BVSETBLTERROR(BVERR_OOM,
			      "command buffer allocation failed");
		goto exit;
	}

	/* Get the current command buffer. */
	head = gcbatch->buffer.prev;
	gcbuffer = list_entry(head, struct gcbuffer, link);

	GCDBG(GCZONE_BUFFER_ALLOC, "buffer = 0x%08X, available = %d\n",
	      (unsigned int) gcbuffer, gcbuffer->available);

	if (gcbuffer->available < size) {
		bverror = append_buffer(bvbltparams, gcbatch, &gcbuffer);
		if (bverror != BVERR_NONE)
			goto exit;
	}

	*buffer = gcbuffer->tail;
	gcbuffer->tail = (unsigned int *)
			((unsigned char *) gcbuffer->tail + size);
	gcbuffer->available -= size;
	gcbatch->size += size;
	bverror = BVERR_NONE;

exit:
	GCEXITARG(GCZONE_BUFFER_ALLOC, "bv%s = %d\n",
		  (bverror == BVERR_NONE) ? "result" : "error", bverror);
	return bverror;
}