blob: 0b96d85d99f95f5b645b6e15b55627d835e38dbe [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
Rob Clark7198e6b2013-07-19 12:59:32 -040031static struct msm_gem_submit *submit_create(struct drm_device *dev,
Rob Clark6b597ce2016-06-01 14:17:40 -040032 struct msm_gpu *gpu, int nr_bos, int nr_cmds)
Rob Clark7198e6b2013-07-19 12:59:32 -040033{
34 struct msm_gem_submit *submit;
Rob Clark6b597ce2016-06-01 14:17:40 -040035 int sz = sizeof(*submit) + (nr_bos * sizeof(submit->bos[0])) +
36 (nr_cmds * sizeof(*submit->cmd));
Rob Clark7198e6b2013-07-19 12:59:32 -040037
38 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Rob Clark6860b562016-05-03 09:49:40 -040039 if (!submit)
40 return NULL;
Rob Clark7198e6b2013-07-19 12:59:32 -040041
Rob Clark6860b562016-05-03 09:49:40 -040042 submit->dev = dev;
43 submit->gpu = gpu;
Rob Clarkba344af2016-05-24 18:43:26 -040044 submit->fence = NULL;
Rob Clark4816b622016-05-03 10:10:15 -040045 submit->pid = get_pid(task_pid(current));
Rob Clark6b597ce2016-06-01 14:17:40 -040046 submit->cmd = (void *)&submit->bos[nr_bos];
Rob Clark7198e6b2013-07-19 12:59:32 -040047
Rob Clark6860b562016-05-03 09:49:40 -040048 /* initially, until copy_from_user() and bo lookup succeeds: */
49 submit->nr_bos = 0;
50 submit->nr_cmds = 0;
51
Rob Clarkba344af2016-05-24 18:43:26 -040052 INIT_LIST_HEAD(&submit->node);
Rob Clark6860b562016-05-03 09:49:40 -040053 INIT_LIST_HEAD(&submit->bo_list);
54 ww_acquire_init(&submit->ticket, &reservation_ww_class);
Rob Clark7198e6b2013-07-19 12:59:32 -040055
56 return submit;
57}
58
Rob Clark40e68152016-05-03 09:50:26 -040059void msm_gem_submit_free(struct msm_gem_submit *submit)
60{
61 fence_put(submit->fence);
62 list_del(&submit->node);
Rob Clark4816b622016-05-03 10:10:15 -040063 put_pid(submit->pid);
Rob Clark40e68152016-05-03 09:50:26 -040064 kfree(submit);
65}
66
Rob Clark7198e6b2013-07-19 12:59:32 -040067static int submit_lookup_objects(struct msm_gem_submit *submit,
68 struct drm_msm_gem_submit *args, struct drm_file *file)
69{
70 unsigned i;
71 int ret = 0;
72
73 spin_lock(&file->table_lock);
74
75 for (i = 0; i < args->nr_bos; i++) {
76 struct drm_msm_gem_submit_bo submit_bo;
77 struct drm_gem_object *obj;
78 struct msm_gem_object *msm_obj;
79 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -030080 u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
Rob Clark7198e6b2013-07-19 12:59:32 -040081
Rob Clarka9e26ca2016-06-01 14:02:51 -040082 /* make sure we don't have garbage flags, in case we hit
83 * error path before flags is initialized:
84 */
85 submit->bos[i].flags = 0;
86
Rob Clark7198e6b2013-07-19 12:59:32 -040087 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
88 if (ret) {
89 ret = -EFAULT;
90 goto out_unlock;
91 }
92
Rob Clark93ddb0d2014-03-03 09:42:33 -050093 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
Rob Clark19872532013-09-06 15:36:40 -040094 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
Rob Clark7198e6b2013-07-19 12:59:32 -040095 ret = -EINVAL;
96 goto out_unlock;
97 }
98
99 submit->bos[i].flags = submit_bo.flags;
100 /* in validate_objects() we figure out if this is true: */
101 submit->bos[i].iova = submit_bo.presumed;
102
103 /* normally use drm_gem_object_lookup(), but for bulk lookup
104 * all under single table_lock just hit object_idr directly:
105 */
106 obj = idr_find(&file->object_idr, submit_bo.handle);
107 if (!obj) {
Rob Clark19872532013-09-06 15:36:40 -0400108 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400109 ret = -EINVAL;
110 goto out_unlock;
111 }
112
113 msm_obj = to_msm_bo(obj);
114
115 if (!list_empty(&msm_obj->submit_entry)) {
Rob Clark19872532013-09-06 15:36:40 -0400116 DRM_ERROR("handle %u at index %u already on submit list\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400117 submit_bo.handle, i);
118 ret = -EINVAL;
119 goto out_unlock;
120 }
121
122 drm_gem_object_reference(obj);
123
124 submit->bos[i].obj = msm_obj;
125
126 list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
127 }
128
129out_unlock:
130 submit->nr_bos = i;
131 spin_unlock(&file->table_lock);
132
133 return ret;
134}
135
136static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
137{
138 struct msm_gem_object *msm_obj = submit->bos[i].obj;
139
140 if (submit->bos[i].flags & BO_PINNED)
141 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
142
143 if (submit->bos[i].flags & BO_LOCKED)
144 ww_mutex_unlock(&msm_obj->resv->lock);
145
146 if (!(submit->bos[i].flags & BO_VALID))
147 submit->bos[i].iova = 0;
148
149 submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
150}
151
152/* This is where we make sure all the bo's are reserved and pin'd: */
Rob Clark340faef2016-03-14 13:56:37 -0400153static int submit_lock_objects(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400154{
155 int contended, slow_locked = -1, i, ret = 0;
156
157retry:
Rob Clark7198e6b2013-07-19 12:59:32 -0400158 for (i = 0; i < submit->nr_bos; i++) {
159 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7198e6b2013-07-19 12:59:32 -0400160
161 if (slow_locked == i)
162 slow_locked = -1;
163
164 contended = i;
165
166 if (!(submit->bos[i].flags & BO_LOCKED)) {
167 ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
168 &submit->ticket);
169 if (ret)
170 goto fail;
171 submit->bos[i].flags |= BO_LOCKED;
172 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400173 }
174
175 ww_acquire_done(&submit->ticket);
176
177 return 0;
178
179fail:
180 for (; i >= 0; i--)
181 submit_unlock_unpin_bo(submit, i);
182
183 if (slow_locked > 0)
184 submit_unlock_unpin_bo(submit, slow_locked);
185
186 if (ret == -EDEADLK) {
187 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
188 /* we lost out in a seqno race, lock and retry.. */
189 ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
190 &submit->ticket);
191 if (!ret) {
192 submit->bos[contended].flags |= BO_LOCKED;
193 slow_locked = contended;
194 goto retry;
195 }
196 }
197
198 return ret;
199}
200
Rob Clarkb6295f92016-03-15 18:26:28 -0400201static int submit_fence_sync(struct msm_gem_submit *submit)
202{
203 int i, ret = 0;
204
205 for (i = 0; i < submit->nr_bos; i++) {
206 struct msm_gem_object *msm_obj = submit->bos[i].obj;
207 bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
208
209 ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
210 if (ret)
211 break;
212 }
213
214 return ret;
215}
216
Rob Clark340faef2016-03-14 13:56:37 -0400217static int submit_pin_objects(struct msm_gem_submit *submit)
218{
219 int i, ret = 0;
220
221 submit->valid = true;
222
223 for (i = 0; i < submit->nr_bos; i++) {
224 struct msm_gem_object *msm_obj = submit->bos[i].obj;
225 uint32_t iova;
226
227 /* if locking succeeded, pin bo: */
228 ret = msm_gem_get_iova_locked(&msm_obj->base,
229 submit->gpu->id, &iova);
230
231 if (ret)
232 break;
233
234 submit->bos[i].flags |= BO_PINNED;
235
236 if (iova == submit->bos[i].iova) {
237 submit->bos[i].flags |= BO_VALID;
238 } else {
239 submit->bos[i].iova = iova;
240 /* iova changed, so address in cmdstream is not valid: */
241 submit->bos[i].flags &= ~BO_VALID;
242 submit->valid = false;
243 }
244 }
245
246 return ret;
247}
248
Rob Clark7198e6b2013-07-19 12:59:32 -0400249static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
250 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
251{
252 if (idx >= submit->nr_bos) {
Rob Clark19872532013-09-06 15:36:40 -0400253 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
254 idx, submit->nr_bos);
255 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400256 }
257
258 if (obj)
259 *obj = submit->bos[idx].obj;
260 if (iova)
261 *iova = submit->bos[idx].iova;
262 if (valid)
263 *valid = !!(submit->bos[idx].flags & BO_VALID);
264
265 return 0;
266}
267
268/* process the reloc's and patch up the cmdstream as needed: */
269static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
270 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
271{
272 uint32_t i, last_offset = 0;
273 uint32_t *ptr;
274 int ret;
275
276 if (offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400277 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
Rob Clark7198e6b2013-07-19 12:59:32 -0400278 return -EINVAL;
279 }
280
281 /* For now, just map the entire thing. Eventually we probably
282 * to do it page-by-page, w/ kmap() if not vmap()d..
283 */
Rob Clark18f23042016-05-26 16:24:35 -0400284 ptr = msm_gem_get_vaddr_locked(&obj->base);
Rob Clark7198e6b2013-07-19 12:59:32 -0400285
286 if (IS_ERR(ptr)) {
287 ret = PTR_ERR(ptr);
288 DBG("failed to map: %d", ret);
289 return ret;
290 }
291
292 for (i = 0; i < nr_relocs; i++) {
293 struct drm_msm_gem_submit_reloc submit_reloc;
294 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300295 u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400296 uint32_t iova, off;
297 bool valid;
298
299 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
300 if (ret)
301 return -EFAULT;
302
303 if (submit_reloc.submit_offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400304 DRM_ERROR("non-aligned reloc offset: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400305 submit_reloc.submit_offset);
306 return -EINVAL;
307 }
308
309 /* offset in dwords: */
310 off = submit_reloc.submit_offset / 4;
311
312 if ((off >= (obj->base.size / 4)) ||
313 (off < last_offset)) {
Rob Clark19872532013-09-06 15:36:40 -0400314 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400315 return -EINVAL;
316 }
317
318 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
319 if (ret)
320 return ret;
321
322 if (valid)
323 continue;
324
325 iova += submit_reloc.reloc_offset;
326
327 if (submit_reloc.shift < 0)
328 iova >>= -submit_reloc.shift;
329 else
330 iova <<= submit_reloc.shift;
331
332 ptr[off] = iova | submit_reloc.or;
333
334 last_offset = off;
335 }
336
Rob Clark18f23042016-05-26 16:24:35 -0400337 msm_gem_put_vaddr_locked(&obj->base);
338
Rob Clark7198e6b2013-07-19 12:59:32 -0400339 return 0;
340}
341
Rob Clark40e68152016-05-03 09:50:26 -0400342static void submit_cleanup(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400343{
344 unsigned i;
345
Rob Clark7198e6b2013-07-19 12:59:32 -0400346 for (i = 0; i < submit->nr_bos; i++) {
347 struct msm_gem_object *msm_obj = submit->bos[i].obj;
348 submit_unlock_unpin_bo(submit, i);
349 list_del_init(&msm_obj->submit_entry);
350 drm_gem_object_unreference(&msm_obj->base);
351 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400352
353 ww_acquire_fini(&submit->ticket);
Rob Clark7198e6b2013-07-19 12:59:32 -0400354}
355
356int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
357 struct drm_file *file)
358{
359 struct msm_drm_private *priv = dev->dev_private;
360 struct drm_msm_gem_submit *args = data;
361 struct msm_file_private *ctx = file->driver_priv;
362 struct msm_gem_submit *submit;
Rob Clarkc01a9582016-02-03 13:12:31 -0500363 struct msm_gpu *gpu = priv->gpu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400364 unsigned i;
365 int ret;
366
Rob Clarkc01a9582016-02-03 13:12:31 -0500367 if (!gpu)
368 return -ENXIO;
369
Rob Clark7198e6b2013-07-19 12:59:32 -0400370 /* for now, we just have 3d pipe.. eventually this would need to
371 * be more clever to dispatch to appropriate gpu module:
372 */
Rob Clarkd9c181e2016-04-23 10:08:59 -0400373 if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
374 return -EINVAL;
375
376 if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
Rob Clark7198e6b2013-07-19 12:59:32 -0400377 return -EINVAL;
378
Rob Clarkb5b4c262016-05-17 15:43:35 -0400379 ret = mutex_lock_interruptible(&dev->struct_mutex);
380 if (ret)
381 return ret;
Rob Clark687f0842016-02-03 13:24:35 -0500382
Rob Clark6b597ce2016-06-01 14:17:40 -0400383 submit = submit_create(dev, gpu, args->nr_bos, args->nr_cmds);
Rob Clarkb5b4c262016-05-17 15:43:35 -0400384 if (!submit) {
385 ret = -ENOMEM;
386 goto out_unlock;
387 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400388
389 ret = submit_lookup_objects(submit, args, file);
390 if (ret)
391 goto out;
392
Rob Clark340faef2016-03-14 13:56:37 -0400393 ret = submit_lock_objects(submit);
394 if (ret)
395 goto out;
396
Rob Clarkb6295f92016-03-15 18:26:28 -0400397 ret = submit_fence_sync(submit);
398 if (ret)
399 goto out;
400
Rob Clark340faef2016-03-14 13:56:37 -0400401 ret = submit_pin_objects(submit);
Rob Clark7198e6b2013-07-19 12:59:32 -0400402 if (ret)
403 goto out;
404
405 for (i = 0; i < args->nr_cmds; i++) {
406 struct drm_msm_gem_submit_cmd submit_cmd;
407 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300408 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400409 struct msm_gem_object *msm_obj;
410 uint32_t iova;
411
412 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
413 if (ret) {
414 ret = -EFAULT;
415 goto out;
416 }
417
Rob Clark93ddb0d2014-03-03 09:42:33 -0500418 /* validate input from userspace: */
419 switch (submit_cmd.type) {
420 case MSM_SUBMIT_CMD_BUF:
421 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
422 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
423 break;
424 default:
425 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
426 ret = -EINVAL;
427 goto out;
428 }
429
Rob Clark7198e6b2013-07-19 12:59:32 -0400430 ret = submit_bo(submit, submit_cmd.submit_idx,
431 &msm_obj, &iova, NULL);
432 if (ret)
433 goto out;
434
435 if (submit_cmd.size % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400436 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400437 submit_cmd.size);
438 ret = -EINVAL;
439 goto out;
440 }
441
Rob Clark19872532013-09-06 15:36:40 -0400442 if ((submit_cmd.size + submit_cmd.submit_offset) >=
443 msm_obj->base.size) {
444 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
Rob Clark7198e6b2013-07-19 12:59:32 -0400445 ret = -EINVAL;
446 goto out;
447 }
448
449 submit->cmd[i].type = submit_cmd.type;
450 submit->cmd[i].size = submit_cmd.size / 4;
451 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
Rob Clarka7d3c952014-05-30 14:47:38 -0400452 submit->cmd[i].idx = submit_cmd.submit_idx;
Rob Clark7198e6b2013-07-19 12:59:32 -0400453
454 if (submit->valid)
455 continue;
456
457 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
458 submit_cmd.nr_relocs, submit_cmd.relocs);
459 if (ret)
460 goto out;
461 }
462
463 submit->nr_cmds = i;
464
465 ret = msm_gpu_submit(gpu, submit, ctx);
466
Rob Clarkb6295f92016-03-15 18:26:28 -0400467 args->fence = submit->fence->seqno;
Rob Clark7198e6b2013-07-19 12:59:32 -0400468
469out:
Rob Clark40e68152016-05-03 09:50:26 -0400470 submit_cleanup(submit);
471 if (ret)
472 msm_gem_submit_free(submit);
Rob Clarkb5b4c262016-05-17 15:43:35 -0400473out_unlock:
Rob Clarkc2703b12014-02-06 19:19:20 -0500474 mutex_unlock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400475 return ret;
476}