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
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
);
42 /* initially, until copy_from_user() and bo lookup succeeds: */
45 ww_acquire_init(&submit
->ticket
, &reservation_ww_class
);
51 static int submit_lookup_objects(struct etnaviv_gem_submit
*submit
,
52 struct drm_file
*file
, struct drm_etnaviv_gem_submit_bo
*submit_bos
,
55 struct drm_etnaviv_gem_submit_bo
*bo
;
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
);
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
);
77 DRM_ERROR("invalid handle %u at index %u\n",
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
);
94 spin_unlock(&file
->table_lock
);
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;
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
)
122 if (!(submit
->bos
[i
].flags
& BO_LOCKED
)) {
123 ret
= ww_mutex_lock_interruptible(&etnaviv_obj
->resv
->lock
,
125 if (ret
== -EALREADY
)
126 DRM_ERROR("BO at index %u already on submit list\n",
130 submit
->bos
[i
].flags
|= BO_LOCKED
;
134 ww_acquire_done(&submit
->ticket
);
140 submit_unlock_object(submit
, i
);
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
,
154 submit
->bos
[contended
].flags
|= BO_LOCKED
;
155 slow_locked
= contended
;
163 static int submit_fence_sync(const struct etnaviv_gem_submit
*submit
)
165 unsigned int context
= submit
->gpu
->fence_context
;
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
);
180 static void submit_unpin_objects(struct etnaviv_gem_submit
*submit
)
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
)
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
,
203 if (IS_ERR(mapping
)) {
204 ret
= PTR_ERR(mapping
);
208 submit
->bos
[i
].flags
|= BO_PINNED
;
209 submit
->bos
[i
].mapping
= mapping
;
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
);
224 *bo
= &submit
->bos
[idx
];
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
,
234 u32 i
, last_offset
= 0;
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
;
243 if (unlikely(r
->flags
)) {
244 DRM_ERROR("invalid reloc flags\n");
248 if (r
->submit_offset
% 4) {
249 DRM_ERROR("non-aligned reloc offset: %u\n",
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
);
263 ret
= submit_bo(submit
, r
->reloc_idx
, &bo
);
267 if (r
->reloc_offset
>= bo
->obj
->base
.size
- sizeof(*ptr
)) {
268 DRM_ERROR("relocation %u outside object", i
);
272 ptr
[off
] = bo
->mapping
->iova
+ r
->reloc_offset
;
280 static void submit_cleanup(struct etnaviv_gem_submit
*submit
)
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
);
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
;
308 if (args
->pipe
>= ETNA_MAX_PIPES
)
311 gpu
= priv
->gpu
[args
->pipe
];
315 if (args
->stream_size
% 4) {
316 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
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
);
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,
337 if (!bos
|| !relocs
|| !stream
|| !cmdbuf
) {
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
));
349 goto err_submit_cmds
;
352 ret
= copy_from_user(relocs
, u64_to_user_ptr(args
->relocs
),
353 args
->nr_relocs
* sizeof(*relocs
));
356 goto err_submit_cmds
;
359 ret
= copy_from_user(stream
, u64_to_user_ptr(args
->stream
),
363 goto err_submit_cmds
;
366 submit
= submit_create(dev
, gpu
, args
->nr_bos
);
369 goto err_submit_cmds
;
372 ret
= submit_lookup_objects(submit
, file
, bos
, args
->nr_bos
);
374 goto err_submit_objects
;
376 ret
= submit_lock_objects(submit
);
378 goto err_submit_objects
;
380 if (!etnaviv_cmd_validate_one(gpu
, stream
, args
->stream_size
/ 4,
381 relocs
, args
->nr_relocs
)) {
383 goto err_submit_objects
;
386 ret
= submit_fence_sync(submit
);
388 goto err_submit_objects
;
390 ret
= submit_pin_objects(submit
);
394 ret
= submit_reloc(submit
, stream
, args
->stream_size
/ 4,
395 relocs
, args
->nr_relocs
);
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
);
406 args
->fence
= submit
->fence
;
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.
417 flush_workqueue(priv
->wq
);
420 submit_cleanup(submit
);
423 /* if we still own the cmdbuf */
425 etnaviv_gpu_cmdbuf_free(cmdbuf
);
427 drm_free_large(stream
);
431 drm_free_large(relocs
);