summaryrefslogtreecommitdiff
path: root/tools/null_state_gen/intel_batchbuffer.c
blob: 62e052a3e8a48bb2d7d169ca09c6fa722f31e9b9 (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
/**************************************************************************
 *
 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Copyright 2014 Intel Corporation
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "intel_batchbuffer.h"

int intel_batch_reset(struct intel_batchbuffer *batch,
		      void *p,
		      uint32_t size,
		      uint32_t off)
{
	batch->err = -EINVAL;
	batch->base = batch->base_ptr = p;
	batch->state_base = batch->state_ptr = p;

	if (off >= size || ALIGN(off, 4) != off)
		return -EINVAL;

	batch->size = size;

	batch->state_base = batch->state_ptr = &batch->base[off];

	batch->num_relocs = 0;
	batch->err = 0;

	return batch->err;
}

uint32_t intel_batch_state_used(struct intel_batchbuffer *batch)
{
	return batch->state_ptr - batch->state_base;
}

uint32_t intel_batch_state_offset(struct intel_batchbuffer *batch)
{
	return batch->state_ptr - batch->base;
}

void *intel_batch_state_alloc(struct intel_batchbuffer *batch,
			      uint32_t size,
			      uint32_t align)
{
	uint32_t cur;
	uint32_t offset;

	if (batch->err)
		return NULL;

	cur  = intel_batch_state_offset(batch);
	offset = ALIGN(cur, align);

	if (offset + size > batch->size) {
		batch->err = -ENOSPC;
		return NULL;
	}

	batch->state_ptr = batch->base + offset + size;

	memset(batch->base + cur, 0, size);

	return batch->base + offset;
}

int intel_batch_offset(struct intel_batchbuffer *batch, const void *ptr)
{
	return (uint8_t *)ptr - batch->base;
}

int intel_batch_state_copy(struct intel_batchbuffer *batch,
			   const void *ptr,
			   const uint32_t size,
			   const uint32_t align)
{
	void * const p = intel_batch_state_alloc(batch, size, align);

	if (p == NULL)
		return -1;

	return intel_batch_offset(batch, memcpy(p, ptr, size));
}

uint32_t intel_batch_cmds_used(struct intel_batchbuffer *batch)
{
	return batch->base_ptr - batch->base;
}

uint32_t intel_batch_total_used(struct intel_batchbuffer *batch)
{
	return batch->state_ptr - batch->base;
}

static uint32_t intel_batch_space(struct intel_batchbuffer *batch)
{
	return batch->state_base - batch->base_ptr;
}

int intel_batch_emit_dword(struct intel_batchbuffer *batch, uint32_t dword)
{
	uint32_t offset;

	if (batch->err)
		return -1;

	if (intel_batch_space(batch) < 4) {
		batch->err = -ENOSPC;
		return -1;
	}

	offset = intel_batch_offset(batch, batch->base_ptr);

	*(uint32_t *) (batch->base_ptr) = dword;
	batch->base_ptr += 4;

	return offset;
}

int intel_batch_emit_reloc(struct intel_batchbuffer *batch,
			   const uint32_t delta)
{
	uint32_t offset;

	if (batch->err)
		return -1;

	if (delta >= batch->size) {
		batch->err = -EINVAL;
		return -1;
	}

	offset = intel_batch_emit_dword(batch, delta);

	if (batch->err)
		return -1;

	if (batch->num_relocs >= MAX_RELOCS) {
		batch->err = -ENOSPC;
		return -1;
	}

	batch->relocs[batch->num_relocs++] = offset;

	return offset;
}