Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / etnaviv / etnaviv_gem_submit.c
blob1ac9a95e1d78c22930427a9c574ec913592f00c6
1 /*
2 * Copyright (C) 2015 Etnaviv Project
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.
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.
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/>.
17 #include <linux/reservation.h>
18 #include "etnaviv_drv.h"
19 #include "etnaviv_gpu.h"
20 #include "etnaviv_gem.h"
23 * Cmdstream submission:
26 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE)
27 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */
28 #define BO_LOCKED 0x4000
29 #define BO_PINNED 0x2000
31 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev,
32 struct etnaviv_gpu *gpu, size_t nr)
34 struct etnaviv_gem_submit *submit;
35 size_t sz = size_vstruct(nr, sizeof(submit->bos[0]), sizeof(*submit));
37 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
38 if (submit) {
39 submit->dev = dev;
40 submit->gpu = gpu;
42 /* initially, until copy_from_user() and bo lookup succeeds: */
43 submit->nr_bos = 0;
45 ww_acquire_init(&submit->ticket, &reservation_ww_class);
48 return submit;
51 static int submit_lookup_objects(struct etnaviv_gem_submit *submit,
52 struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos,
53 unsigned nr_bos)
55 struct drm_etnaviv_gem_submit_bo *bo;
56 unsigned i;
57 int ret = 0;
59 spin_lock(&file->table_lock);
61 for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) {
62 struct drm_gem_object *obj;
64 if (bo->flags & BO_INVALID_FLAGS) {
65 DRM_ERROR("invalid flags: %x\n", bo->flags);
66 ret = -EINVAL;
67 goto out_unlock;
70 submit->bos[i].flags = bo->flags;
72 /* normally use drm_gem_object_lookup(), but for bulk lookup
73 * all under single table_lock just hit object_idr directly:
75 obj = idr_find(&file->object_idr, bo->handle);
76 if (!obj) {
77 DRM_ERROR("invalid handle %u at index %u\n",
78 bo->handle, i);
79 ret = -EINVAL;
80 goto out_unlock;
84 * Take a refcount on the object. The file table lock
85 * prevents the object_idr's refcount on this being dropped.
87 drm_gem_object_reference(obj);
89 submit->bos[i].obj = to_etnaviv_bo(obj);
92 out_unlock:
93 submit->nr_bos = i;
94 spin_unlock(&file->table_lock);
96 return ret;
99 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i)
101 if (submit->bos[i].flags & BO_LOCKED) {
102 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
104 ww_mutex_unlock(&etnaviv_obj->resv->lock);
105 submit->bos[i].flags &= ~BO_LOCKED;
109 static int submit_lock_objects(struct etnaviv_gem_submit *submit)
111 int contended, slow_locked = -1, i, ret = 0;
113 retry:
114 for (i = 0; i < submit->nr_bos; i++) {
115 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
117 if (slow_locked == i)
118 slow_locked = -1;
120 contended = i;
122 if (!(submit->bos[i].flags & BO_LOCKED)) {
123 ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock,
124 &submit->ticket);
125 if (ret == -EALREADY)
126 DRM_ERROR("BO at index %u already on submit list\n",
128 if (ret)
129 goto fail;
130 submit->bos[i].flags |= BO_LOCKED;
134 ww_acquire_done(&submit->ticket);
136 return 0;
138 fail:
139 for (; i >= 0; i--)
140 submit_unlock_object(submit, i);
142 if (slow_locked > 0)
143 submit_unlock_object(submit, slow_locked);
145 if (ret == -EDEADLK) {
146 struct etnaviv_gem_object *etnaviv_obj;
148 etnaviv_obj = submit->bos[contended].obj;
150 /* we lost out in a seqno race, lock and retry.. */
151 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock,
152 &submit->ticket);
153 if (!ret) {
154 submit->bos[contended].flags |= BO_LOCKED;
155 slow_locked = contended;
156 goto retry;
160 return ret;
163 static int submit_fence_sync(const struct etnaviv_gem_submit *submit)
165 unsigned int context = submit->gpu->fence_context;
166 int i, ret = 0;
168 for (i = 0; i < submit->nr_bos; i++) {
169 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
170 bool write = submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE;
172 ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write);
173 if (ret)
174 break;
177 return ret;
180 static void submit_unpin_objects(struct etnaviv_gem_submit *submit)
182 int i;
184 for (i = 0; i < submit->nr_bos; i++) {
185 if (submit->bos[i].flags & BO_PINNED)
186 etnaviv_gem_mapping_unreference(submit->bos[i].mapping);
188 submit->bos[i].mapping = NULL;
189 submit->bos[i].flags &= ~BO_PINNED;
193 static int submit_pin_objects(struct etnaviv_gem_submit *submit)
195 int i, ret = 0;
197 for (i = 0; i < submit->nr_bos; i++) {
198 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
199 struct etnaviv_vram_mapping *mapping;
201 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
202 submit->gpu);
203 if (IS_ERR(mapping)) {
204 ret = PTR_ERR(mapping);
205 break;
208 submit->bos[i].flags |= BO_PINNED;
209 submit->bos[i].mapping = mapping;
212 return ret;
215 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx,
216 struct etnaviv_gem_submit_bo **bo)
218 if (idx >= submit->nr_bos) {
219 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
220 idx, submit->nr_bos);
221 return -EINVAL;
224 *bo = &submit->bos[idx];
226 return 0;
229 /* process the reloc's and patch up the cmdstream as needed: */
230 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream,
231 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs,
232 u32 nr_relocs)
234 u32 i, last_offset = 0;
235 u32 *ptr = stream;
236 int ret;
238 for (i = 0; i < nr_relocs; i++) {
239 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i;
240 struct etnaviv_gem_submit_bo *bo;
241 u32 off;
243 if (unlikely(r->flags)) {
244 DRM_ERROR("invalid reloc flags\n");
245 return -EINVAL;
248 if (r->submit_offset % 4) {
249 DRM_ERROR("non-aligned reloc offset: %u\n",
250 r->submit_offset);
251 return -EINVAL;
254 /* offset in dwords: */
255 off = r->submit_offset / 4;
257 if ((off >= size ) ||
258 (off < last_offset)) {
259 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
260 return -EINVAL;
263 ret = submit_bo(submit, r->reloc_idx, &bo);
264 if (ret)
265 return ret;
267 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) {
268 DRM_ERROR("relocation %u outside object\n", i);
269 return -EINVAL;
272 ptr[off] = bo->mapping->iova + r->reloc_offset;
274 last_offset = off;
277 return 0;
280 static void submit_cleanup(struct etnaviv_gem_submit *submit)
282 unsigned i;
284 for (i = 0; i < submit->nr_bos; i++) {
285 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
287 submit_unlock_object(submit, i);
288 drm_gem_object_unreference_unlocked(&etnaviv_obj->base);
291 ww_acquire_fini(&submit->ticket);
292 kfree(submit);
295 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
296 struct drm_file *file)
298 struct etnaviv_drm_private *priv = dev->dev_private;
299 struct drm_etnaviv_gem_submit *args = data;
300 struct drm_etnaviv_gem_submit_reloc *relocs;
301 struct drm_etnaviv_gem_submit_bo *bos;
302 struct etnaviv_gem_submit *submit;
303 struct etnaviv_cmdbuf *cmdbuf;
304 struct etnaviv_gpu *gpu;
305 void *stream;
306 int ret;
308 if (args->pipe >= ETNA_MAX_PIPES)
309 return -EINVAL;
311 gpu = priv->gpu[args->pipe];
312 if (!gpu)
313 return -ENXIO;
315 if (args->stream_size % 4) {
316 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
317 args->stream_size);
318 return -EINVAL;
321 if (args->exec_state != ETNA_PIPE_3D &&
322 args->exec_state != ETNA_PIPE_2D &&
323 args->exec_state != ETNA_PIPE_VG) {
324 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state);
325 return -EINVAL;
329 * Copy the command submission and bo array to kernel space in
330 * one go, and do this outside of any locks.
332 bos = drm_malloc_ab(args->nr_bos, sizeof(*bos));
333 relocs = drm_malloc_ab(args->nr_relocs, sizeof(*relocs));
334 stream = drm_malloc_ab(1, args->stream_size);
335 cmdbuf = etnaviv_gpu_cmdbuf_new(gpu, ALIGN(args->stream_size, 8) + 8,
336 args->nr_bos);
337 if (!bos || !relocs || !stream || !cmdbuf) {
338 ret = -ENOMEM;
339 goto err_submit_cmds;
342 cmdbuf->exec_state = args->exec_state;
343 cmdbuf->ctx = file->driver_priv;
345 ret = copy_from_user(bos, u64_to_user_ptr(args->bos),
346 args->nr_bos * sizeof(*bos));
347 if (ret) {
348 ret = -EFAULT;
349 goto err_submit_cmds;
352 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs),
353 args->nr_relocs * sizeof(*relocs));
354 if (ret) {
355 ret = -EFAULT;
356 goto err_submit_cmds;
359 ret = copy_from_user(stream, u64_to_user_ptr(args->stream),
360 args->stream_size);
361 if (ret) {
362 ret = -EFAULT;
363 goto err_submit_cmds;
366 submit = submit_create(dev, gpu, args->nr_bos);
367 if (!submit) {
368 ret = -ENOMEM;
369 goto err_submit_cmds;
372 ret = submit_lookup_objects(submit, file, bos, args->nr_bos);
373 if (ret)
374 goto err_submit_objects;
376 ret = submit_lock_objects(submit);
377 if (ret)
378 goto err_submit_objects;
380 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4,
381 relocs, args->nr_relocs)) {
382 ret = -EINVAL;
383 goto err_submit_objects;
386 ret = submit_fence_sync(submit);
387 if (ret)
388 goto err_submit_objects;
390 ret = submit_pin_objects(submit);
391 if (ret)
392 goto out;
394 ret = submit_reloc(submit, stream, args->stream_size / 4,
395 relocs, args->nr_relocs);
396 if (ret)
397 goto out;
399 memcpy(cmdbuf->vaddr, stream, args->stream_size);
400 cmdbuf->user_size = ALIGN(args->stream_size, 8);
402 ret = etnaviv_gpu_submit(gpu, submit, cmdbuf);
403 if (ret == 0)
404 cmdbuf = NULL;
406 args->fence = submit->fence;
408 out:
409 submit_unpin_objects(submit);
412 * If we're returning -EAGAIN, it may be due to the userptr code
413 * wanting to run its workqueue outside of any locks. Flush our
414 * workqueue to ensure that it is run in a timely manner.
416 if (ret == -EAGAIN)
417 flush_workqueue(priv->wq);
419 err_submit_objects:
420 submit_cleanup(submit);
422 err_submit_cmds:
423 /* if we still own the cmdbuf */
424 if (cmdbuf)
425 etnaviv_gpu_cmdbuf_free(cmdbuf);
426 if (stream)
427 drm_free_large(stream);
428 if (bos)
429 drm_free_large(bos);
430 if (relocs)
431 drm_free_large(relocs);
433 return ret;