blob: 2661a7cb23c8df6a7201b23b514ee69913b6c19f [file] [log] [blame]
Rob Clark7198e6b2013-07-19 12:59:32 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "msm_drv.h"
19#include "msm_gpu.h"
20#include "msm_gem.h"
21
22/*
23 * Cmdstream submission:
24 */
25
Rob Clark7198e6b2013-07-19 12:59:32 -040026/* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
Rob Clark340faef2016-03-14 13:56:37 -040027#define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */
Rob Clark7198e6b2013-07-19 12:59:32 -040028#define BO_LOCKED 0x4000
29#define BO_PINNED 0x2000
30
31static inline void __user *to_user_ptr(u64 address)
32{
33 return (void __user *)(uintptr_t)address;
34}
35
36static struct msm_gem_submit *submit_create(struct drm_device *dev,
37 struct msm_gpu *gpu, int nr)
38{
39 struct msm_gem_submit *submit;
40 int sz = sizeof(*submit) + (nr * sizeof(submit->bos[0]));
41
42 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
43 if (submit) {
44 submit->dev = dev;
45 submit->gpu = gpu;
46
47 /* initially, until copy_from_user() and bo lookup succeeds: */
48 submit->nr_bos = 0;
49 submit->nr_cmds = 0;
50
51 INIT_LIST_HEAD(&submit->bo_list);
52 ww_acquire_init(&submit->ticket, &reservation_ww_class);
53 }
54
55 return submit;
56}
57
58static int submit_lookup_objects(struct msm_gem_submit *submit,
59 struct drm_msm_gem_submit *args, struct drm_file *file)
60{
61 unsigned i;
62 int ret = 0;
63
64 spin_lock(&file->table_lock);
65
66 for (i = 0; i < args->nr_bos; i++) {
67 struct drm_msm_gem_submit_bo submit_bo;
68 struct drm_gem_object *obj;
69 struct msm_gem_object *msm_obj;
70 void __user *userptr =
71 to_user_ptr(args->bos + (i * sizeof(submit_bo)));
72
73 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
74 if (ret) {
75 ret = -EFAULT;
76 goto out_unlock;
77 }
78
Rob Clark93ddb0d2014-03-03 09:42:33 -050079 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
Rob Clark19872532013-09-06 15:36:40 -040080 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
Rob Clark7198e6b2013-07-19 12:59:32 -040081 ret = -EINVAL;
82 goto out_unlock;
83 }
84
85 submit->bos[i].flags = submit_bo.flags;
86 /* in validate_objects() we figure out if this is true: */
87 submit->bos[i].iova = submit_bo.presumed;
88
89 /* normally use drm_gem_object_lookup(), but for bulk lookup
90 * all under single table_lock just hit object_idr directly:
91 */
92 obj = idr_find(&file->object_idr, submit_bo.handle);
93 if (!obj) {
Rob Clark19872532013-09-06 15:36:40 -040094 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
Rob Clark7198e6b2013-07-19 12:59:32 -040095 ret = -EINVAL;
96 goto out_unlock;
97 }
98
99 msm_obj = to_msm_bo(obj);
100
101 if (!list_empty(&msm_obj->submit_entry)) {
Rob Clark19872532013-09-06 15:36:40 -0400102 DRM_ERROR("handle %u at index %u already on submit list\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400103 submit_bo.handle, i);
104 ret = -EINVAL;
105 goto out_unlock;
106 }
107
108 drm_gem_object_reference(obj);
109
110 submit->bos[i].obj = msm_obj;
111
112 list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
113 }
114
115out_unlock:
116 submit->nr_bos = i;
117 spin_unlock(&file->table_lock);
118
119 return ret;
120}
121
122static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
123{
124 struct msm_gem_object *msm_obj = submit->bos[i].obj;
125
126 if (submit->bos[i].flags & BO_PINNED)
127 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
128
129 if (submit->bos[i].flags & BO_LOCKED)
130 ww_mutex_unlock(&msm_obj->resv->lock);
131
132 if (!(submit->bos[i].flags & BO_VALID))
133 submit->bos[i].iova = 0;
134
135 submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
136}
137
138/* This is where we make sure all the bo's are reserved and pin'd: */
Rob Clark340faef2016-03-14 13:56:37 -0400139static int submit_lock_objects(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400140{
141 int contended, slow_locked = -1, i, ret = 0;
142
143retry:
Rob Clark7198e6b2013-07-19 12:59:32 -0400144 for (i = 0; i < submit->nr_bos; i++) {
145 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7198e6b2013-07-19 12:59:32 -0400146
147 if (slow_locked == i)
148 slow_locked = -1;
149
150 contended = i;
151
152 if (!(submit->bos[i].flags & BO_LOCKED)) {
153 ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
154 &submit->ticket);
155 if (ret)
156 goto fail;
157 submit->bos[i].flags |= BO_LOCKED;
158 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400159 }
160
161 ww_acquire_done(&submit->ticket);
162
163 return 0;
164
165fail:
166 for (; i >= 0; i--)
167 submit_unlock_unpin_bo(submit, i);
168
169 if (slow_locked > 0)
170 submit_unlock_unpin_bo(submit, slow_locked);
171
172 if (ret == -EDEADLK) {
173 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
174 /* we lost out in a seqno race, lock and retry.. */
175 ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
176 &submit->ticket);
177 if (!ret) {
178 submit->bos[contended].flags |= BO_LOCKED;
179 slow_locked = contended;
180 goto retry;
181 }
182 }
183
184 return ret;
185}
186
Rob Clarkb6295f92016-03-15 18:26:28 -0400187static int submit_fence_sync(struct msm_gem_submit *submit)
188{
189 int i, ret = 0;
190
191 for (i = 0; i < submit->nr_bos; i++) {
192 struct msm_gem_object *msm_obj = submit->bos[i].obj;
193 bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
194
195 ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
196 if (ret)
197 break;
198 }
199
200 return ret;
201}
202
Rob Clark340faef2016-03-14 13:56:37 -0400203static int submit_pin_objects(struct msm_gem_submit *submit)
204{
205 int i, ret = 0;
206
207 submit->valid = true;
208
209 for (i = 0; i < submit->nr_bos; i++) {
210 struct msm_gem_object *msm_obj = submit->bos[i].obj;
211 uint32_t iova;
212
213 /* if locking succeeded, pin bo: */
214 ret = msm_gem_get_iova_locked(&msm_obj->base,
215 submit->gpu->id, &iova);
216
217 if (ret)
218 break;
219
220 submit->bos[i].flags |= BO_PINNED;
221
222 if (iova == submit->bos[i].iova) {
223 submit->bos[i].flags |= BO_VALID;
224 } else {
225 submit->bos[i].iova = iova;
226 /* iova changed, so address in cmdstream is not valid: */
227 submit->bos[i].flags &= ~BO_VALID;
228 submit->valid = false;
229 }
230 }
231
232 return ret;
233}
234
Rob Clark7198e6b2013-07-19 12:59:32 -0400235static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
236 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
237{
238 if (idx >= submit->nr_bos) {
Rob Clark19872532013-09-06 15:36:40 -0400239 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
240 idx, submit->nr_bos);
241 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400242 }
243
244 if (obj)
245 *obj = submit->bos[idx].obj;
246 if (iova)
247 *iova = submit->bos[idx].iova;
248 if (valid)
249 *valid = !!(submit->bos[idx].flags & BO_VALID);
250
251 return 0;
252}
253
254/* process the reloc's and patch up the cmdstream as needed: */
255static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
256 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
257{
258 uint32_t i, last_offset = 0;
259 uint32_t *ptr;
260 int ret;
261
262 if (offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400263 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
Rob Clark7198e6b2013-07-19 12:59:32 -0400264 return -EINVAL;
265 }
266
267 /* For now, just map the entire thing. Eventually we probably
268 * to do it page-by-page, w/ kmap() if not vmap()d..
269 */
Rob Clarkc2703b12014-02-06 19:19:20 -0500270 ptr = msm_gem_vaddr_locked(&obj->base);
Rob Clark7198e6b2013-07-19 12:59:32 -0400271
272 if (IS_ERR(ptr)) {
273 ret = PTR_ERR(ptr);
274 DBG("failed to map: %d", ret);
275 return ret;
276 }
277
278 for (i = 0; i < nr_relocs; i++) {
279 struct drm_msm_gem_submit_reloc submit_reloc;
280 void __user *userptr =
281 to_user_ptr(relocs + (i * sizeof(submit_reloc)));
282 uint32_t iova, off;
283 bool valid;
284
285 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
286 if (ret)
287 return -EFAULT;
288
289 if (submit_reloc.submit_offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400290 DRM_ERROR("non-aligned reloc offset: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400291 submit_reloc.submit_offset);
292 return -EINVAL;
293 }
294
295 /* offset in dwords: */
296 off = submit_reloc.submit_offset / 4;
297
298 if ((off >= (obj->base.size / 4)) ||
299 (off < last_offset)) {
Rob Clark19872532013-09-06 15:36:40 -0400300 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400301 return -EINVAL;
302 }
303
304 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
305 if (ret)
306 return ret;
307
308 if (valid)
309 continue;
310
311 iova += submit_reloc.reloc_offset;
312
313 if (submit_reloc.shift < 0)
314 iova >>= -submit_reloc.shift;
315 else
316 iova <<= submit_reloc.shift;
317
318 ptr[off] = iova | submit_reloc.or;
319
320 last_offset = off;
321 }
322
323 return 0;
324}
325
326static void submit_cleanup(struct msm_gem_submit *submit, bool fail)
327{
328 unsigned i;
329
Rob Clark7198e6b2013-07-19 12:59:32 -0400330 for (i = 0; i < submit->nr_bos; i++) {
331 struct msm_gem_object *msm_obj = submit->bos[i].obj;
332 submit_unlock_unpin_bo(submit, i);
333 list_del_init(&msm_obj->submit_entry);
334 drm_gem_object_unreference(&msm_obj->base);
335 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400336
337 ww_acquire_fini(&submit->ticket);
Rob Clark7198e6b2013-07-19 12:59:32 -0400338}
339
340int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
341 struct drm_file *file)
342{
343 struct msm_drm_private *priv = dev->dev_private;
344 struct drm_msm_gem_submit *args = data;
345 struct msm_file_private *ctx = file->driver_priv;
346 struct msm_gem_submit *submit;
Rob Clarkc01a9582016-02-03 13:12:31 -0500347 struct msm_gpu *gpu = priv->gpu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400348 unsigned i;
349 int ret;
350
Rob Clarkc01a9582016-02-03 13:12:31 -0500351 if (!gpu)
352 return -ENXIO;
353
Rob Clark7198e6b2013-07-19 12:59:32 -0400354 /* for now, we just have 3d pipe.. eventually this would need to
355 * be more clever to dispatch to appropriate gpu module:
356 */
357 if (args->pipe != MSM_PIPE_3D0)
358 return -EINVAL;
359
Rob Clark7198e6b2013-07-19 12:59:32 -0400360 if (args->nr_cmds > MAX_CMDS)
361 return -EINVAL;
362
363 submit = submit_create(dev, gpu, args->nr_bos);
Rob Clark687f0842016-02-03 13:24:35 -0500364 if (!submit)
365 return -ENOMEM;
366
367 mutex_lock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400368
369 ret = submit_lookup_objects(submit, args, file);
370 if (ret)
371 goto out;
372
Rob Clark340faef2016-03-14 13:56:37 -0400373 ret = submit_lock_objects(submit);
374 if (ret)
375 goto out;
376
Rob Clarkb6295f92016-03-15 18:26:28 -0400377 ret = submit_fence_sync(submit);
378 if (ret)
379 goto out;
380
Rob Clark340faef2016-03-14 13:56:37 -0400381 ret = submit_pin_objects(submit);
Rob Clark7198e6b2013-07-19 12:59:32 -0400382 if (ret)
383 goto out;
384
385 for (i = 0; i < args->nr_cmds; i++) {
386 struct drm_msm_gem_submit_cmd submit_cmd;
387 void __user *userptr =
388 to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
389 struct msm_gem_object *msm_obj;
390 uint32_t iova;
391
392 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
393 if (ret) {
394 ret = -EFAULT;
395 goto out;
396 }
397
Rob Clark93ddb0d2014-03-03 09:42:33 -0500398 /* validate input from userspace: */
399 switch (submit_cmd.type) {
400 case MSM_SUBMIT_CMD_BUF:
401 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
402 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
403 break;
404 default:
405 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
406 ret = -EINVAL;
407 goto out;
408 }
409
Rob Clark7198e6b2013-07-19 12:59:32 -0400410 ret = submit_bo(submit, submit_cmd.submit_idx,
411 &msm_obj, &iova, NULL);
412 if (ret)
413 goto out;
414
415 if (submit_cmd.size % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400416 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400417 submit_cmd.size);
418 ret = -EINVAL;
419 goto out;
420 }
421
Rob Clark19872532013-09-06 15:36:40 -0400422 if ((submit_cmd.size + submit_cmd.submit_offset) >=
423 msm_obj->base.size) {
424 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
Rob Clark7198e6b2013-07-19 12:59:32 -0400425 ret = -EINVAL;
426 goto out;
427 }
428
429 submit->cmd[i].type = submit_cmd.type;
430 submit->cmd[i].size = submit_cmd.size / 4;
431 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
Rob Clarka7d3c952014-05-30 14:47:38 -0400432 submit->cmd[i].idx = submit_cmd.submit_idx;
Rob Clark7198e6b2013-07-19 12:59:32 -0400433
434 if (submit->valid)
435 continue;
436
437 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
438 submit_cmd.nr_relocs, submit_cmd.relocs);
439 if (ret)
440 goto out;
441 }
442
443 submit->nr_cmds = i;
444
445 ret = msm_gpu_submit(gpu, submit, ctx);
446
Rob Clarkb6295f92016-03-15 18:26:28 -0400447 args->fence = submit->fence->seqno;
Rob Clark7198e6b2013-07-19 12:59:32 -0400448
449out:
Rob Clark687f0842016-02-03 13:24:35 -0500450 submit_cleanup(submit, !!ret);
Rob Clarkc2703b12014-02-06 19:19:20 -0500451 mutex_unlock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400452 return ret;
453}