embox

Форк
0
/
etnaviv_gem_submit.c 
263 строки · 6.7 Кб
1
/*
2
 * Copyright (C) 2015 Etnaviv Project
3
 *
4
 * This program is free software; you can redistribute it and/or modify it
5
 * under the terms of the GNU General Public License version 2 as published by
6
 * the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11
 * more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along with
14
 * this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16

17
#include <stdio.h>
18

19
#include <util/log.h>
20

21
#include "etnaviv_compat.h"
22
#include "etnaviv_cmdbuf.h"
23
#include "etnaviv_drv.h"
24
#include "etnaviv_gpu.h"
25
#include "etnaviv_gem.h"
26
#include "etnaviv_drm.h"
27

28
#include <embox_drm/drm_gem.h>
29

30
#define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
31
/* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
32
#define BO_LOCKED   0x4000
33
#define BO_PINNED   0x2000
34

35
static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
36
		struct etnaviv_gpu *gpu, size_t nr) {
37
	/* In current implementation we process single gem_submit
38
	 * at any time, so just return static structure instead of malloc */
39
	static struct etnaviv_gem_submit submit;
40
	memset(&submit, 0, sizeof(submit));
41
	submit.dev = dev;
42
	submit.gpu = gpu;
43

44
	return &submit;
45
}
46

47
static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
48
	struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
49
	unsigned nr_bos) {
50
	struct drm_etnaviv_gem_submit_bo *bo;
51
	unsigned i;
52
	int ret = 0;
53

54
	for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
55
		struct drm_gem_object *obj;
56

57
		if (bo->flags & BO_INVALID_FLAGS) {
58
			DRM_ERROR("invalid flags: %x\n", bo->flags);
59
			ret = -EINVAL;
60
			goto out_unlock;
61
		}
62

63
		submit->bos[i].flags = bo->flags;
64

65
		/* normally use drm_gem_object_lookup(), but for bulk lookup
66
		 * all under single table_lock just hit object_idr directly:
67
		 */
68
		obj = idr_find(&file->object_idr, bo->handle);
69
		if (!obj) {
70
			DRM_ERROR("invalid handle %u at index %u\n",
71
				  bo->handle, i);
72
			ret = -EINVAL;
73
			goto out_unlock;
74
		}
75

76
		/*
77
		 * Take a refcount on the object. The file table lock
78
		 * prevents the object_idr's refcount on this being dropped.
79
		 */
80
		submit->bos[i].obj = to_etnaviv_bo(obj);
81
	}
82

83
out_unlock:
84
	submit->nr_bos = i;
85

86
	return ret;
87
}
88

89
static int submit_pin_objects(struct etnaviv_gem_submit *submit) {
90
	int i, ret = 0;
91

92
	for (i = 0; i < submit->nr_bos; i++) {
93
		struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
94
		struct etnaviv_vram_mapping *mapping;
95

96
		mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
97
						  submit->gpu);
98
		log_debug("map for %d is %p\n", i, mapping);
99
		submit->bos[i].flags |= BO_PINNED;
100
		submit->bos[i].mapping = mapping;
101
	}
102

103
	return ret;
104
}
105

106
static int submit_bo(struct etnaviv_gem_submit *submit, uint32_t idx,
107
	struct etnaviv_gem_submit_bo **bo) {
108
	if (idx >= submit->nr_bos) {
109
		DRM_ERROR("invalid buffer index: %u (out of %u)\n",
110
				idx, submit->nr_bos);
111
		submit->nr_bos = idx;
112

113
		return -EINVAL;
114
	}
115

116
	*bo = &submit->bos[idx];
117

118
	return 0;
119
}
120

121
/* process the reloc's and patch up the cmdstream as needed: */
122
static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
123
		uint32_t size, const struct drm_etnaviv_gem_submit_reloc *relocs,
124
		uint32_t nr_relocs)
125
{
126
	uint32_t i, last_offset = 0;
127
	uint32_t *ptr = stream;
128
	int ret;
129

130
	for (i = 0; i < nr_relocs; i++) {
131
		const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
132
		struct etnaviv_gem_submit_bo *bo;
133
		uint32_t off;
134

135
		if (r->submit_offset % 4) {
136
			DRM_ERROR("non-aligned reloc offset: %u\n",
137
				  r->submit_offset);
138
			return -EINVAL;
139
		}
140

141
		/* offset in dwords: */
142
		off = r->submit_offset / 4;
143

144
		if ((off >= size ) ||
145
				(off < last_offset)) {
146
			DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
147
			return -EINVAL;
148
		}
149

150
		ret = submit_bo(submit, r->reloc_idx, &bo);
151
		if (ret)
152
			return ret;
153

154
		if (r->reloc_offset >= bo->obj->base.size - sizeof(*ptr)) {
155
			DRM_ERROR("relocation %u outside object", i);
156
			return -EINVAL;
157
		}
158
		ptr[off] = bo->mapping->iova + (uint32_t) r->reloc_offset;
159

160
		last_offset = off;
161
	}
162

163
	return 0;
164
}
165

166
extern void etnaviv_buffer_dump(struct etnaviv_gpu *gpu,
167
	struct etnaviv_cmdbuf *buf, uint32_t off, uint32_t len);
168

169
int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data, struct drm_file *file) {
170
	struct etnaviv_drm_private *priv = dev->dev_private;
171
	struct drm_etnaviv_gem_submit *args = data;
172
	struct drm_etnaviv_gem_submit_reloc *relocs;
173
	struct drm_etnaviv_gem_submit_bo *bos;
174
	struct etnaviv_gem_submit *submit;
175
	struct etnaviv_cmdbuf *cmdbuf;
176
	struct etnaviv_gpu *gpu;
177
	void *stream;
178
	int ret;
179

180
	log_debug("Pipe is %d", args->pipe);
181
	args->pipe = 0;
182
	if (args->pipe >= ETNA_MAX_PIPES) {
183
		return -EINVAL;
184
	}
185

186
	gpu = priv->gpu[args->pipe];
187
	if (!gpu) {
188
		return -ENXIO;
189
	}
190

191
	if (args->stream_size % 4) {
192
		log_error("non-aligned cmdstream buffer size: %u",
193
			  args->stream_size);
194
		return -EINVAL;
195
	}
196

197
	if (args->exec_state != ETNA_PIPE_3D &&
198
	    args->exec_state != ETNA_PIPE_2D &&
199
	    args->exec_state != ETNA_PIPE_VG) {
200
		DRM_ERROR("invalid exec_state: 0x%x", args->exec_state);
201
		return -EINVAL;
202
	}
203

204
	/*
205
	 * Copy the command submission and bo array to kernel space in
206
	 * one go, and do this outside of any locks.
207
	 */
208
	bos = (void *) (uint32_t) args->bos;
209
	relocs = (void *) (uint32_t) args->relocs;
210
	cmdbuf = etnaviv_cmdbuf_new(gpu->cmdbuf_suballoc,
211
				    ALIGN(args->stream_size, 8) + 8,
212
				    args->nr_bos);
213
	if (/* !bos || !relocs || */ !cmdbuf) {
214
		log_error("smth is null bos %p relocs %p cmdbuf %p", bos, relocs, cmdbuf);
215
		return -ENOMEM;
216
	}
217

218
	cmdbuf->exec_state = args->exec_state * 0; /* XXX */
219
	cmdbuf->ctx = file->driver_priv;
220

221
	stream = (void *) (int) args->stream;
222
	submit = submit_create(dev, gpu, args->nr_bos);
223
	if (!submit) {
224
		return -ENOMEM;
225
	}
226

227
	submit->flags = 0;//args->flags;
228

229
	if (bos) {
230
		if ((ret = submit_lookup_objects(submit, file, bos, args->nr_bos))) {
231
			return ret;
232
		}
233
	}
234

235
	memcpy(cmdbuf->vaddr, stream, args->stream_size);
236
	cmdbuf->user_size = ALIGN(args->stream_size, 8);
237

238
	etnaviv_buffer_dump(gpu, cmdbuf, 0, cmdbuf->user_size);
239

240
	if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
241
				relocs, args->nr_relocs)) {
242
		return -EINVAL;
243
	}
244

245
	if ((ret = submit_pin_objects(submit))) {
246
		log_error("submit_pit_objects failed with %d", ret);
247
		return ret;
248
	}
249

250
	if (relocs && 0) {
251
		ret = submit_reloc(submit, stream, args->stream_size / 4,
252
				relocs, args->nr_relocs);
253
		if (ret) {
254
			log_error("submit_reloc fail");
255
			return ret;
256
		}
257
	}
258

259
	memcpy(cmdbuf->vaddr, stream, args->stream_size);
260
	cmdbuf->user_size = ALIGN(args->stream_size, 8);
261

262
	return etnaviv_gpu_submit(gpu, submit, cmdbuf);
263
}
264

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.