blob: 9766f9ae4b7d9a2593550bf7d013f988b61de46d [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 */
373 if (args->pipe != MSM_PIPE_3D0)
374 return -EINVAL;
375
Rob Clarkb5b4c262016-05-17 15:43:35 -0400376 ret = mutex_lock_interruptible(&dev->struct_mutex);
377 if (ret)
378 return ret;
Rob Clark687f0842016-02-03 13:24:35 -0500379
Rob Clark6b597ce2016-06-01 14:17:40 -0400380 submit = submit_create(dev, gpu, args->nr_bos, args->nr_cmds);
Rob Clarkb5b4c262016-05-17 15:43:35 -0400381 if (!submit) {
382 ret = -ENOMEM;
383 goto out_unlock;
384 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400385
386 ret = submit_lookup_objects(submit, args, file);
387 if (ret)
388 goto out;
389
Rob Clark340faef2016-03-14 13:56:37 -0400390 ret = submit_lock_objects(submit);
391 if (ret)
392 goto out;
393
Rob Clarkb6295f92016-03-15 18:26:28 -0400394 ret = submit_fence_sync(submit);
395 if (ret)
396 goto out;
397
Rob Clark340faef2016-03-14 13:56:37 -0400398 ret = submit_pin_objects(submit);
Rob Clark7198e6b2013-07-19 12:59:32 -0400399 if (ret)
400 goto out;
401
402 for (i = 0; i < args->nr_cmds; i++) {
403 struct drm_msm_gem_submit_cmd submit_cmd;
404 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300405 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400406 struct msm_gem_object *msm_obj;
407 uint32_t iova;
408
409 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
410 if (ret) {
411 ret = -EFAULT;
412 goto out;
413 }
414
Rob Clark93ddb0d2014-03-03 09:42:33 -0500415 /* validate input from userspace: */
416 switch (submit_cmd.type) {
417 case MSM_SUBMIT_CMD_BUF:
418 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
419 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
420 break;
421 default:
422 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
423 ret = -EINVAL;
424 goto out;
425 }
426
Rob Clark7198e6b2013-07-19 12:59:32 -0400427 ret = submit_bo(submit, submit_cmd.submit_idx,
428 &msm_obj, &iova, NULL);
429 if (ret)
430 goto out;
431
432 if (submit_cmd.size % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400433 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400434 submit_cmd.size);
435 ret = -EINVAL;
436 goto out;
437 }
438
Rob Clark19872532013-09-06 15:36:40 -0400439 if ((submit_cmd.size + submit_cmd.submit_offset) >=
440 msm_obj->base.size) {
441 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
Rob Clark7198e6b2013-07-19 12:59:32 -0400442 ret = -EINVAL;
443 goto out;
444 }
445
446 submit->cmd[i].type = submit_cmd.type;
447 submit->cmd[i].size = submit_cmd.size / 4;
448 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
Rob Clarka7d3c952014-05-30 14:47:38 -0400449 submit->cmd[i].idx = submit_cmd.submit_idx;
Rob Clark7198e6b2013-07-19 12:59:32 -0400450
451 if (submit->valid)
452 continue;
453
454 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
455 submit_cmd.nr_relocs, submit_cmd.relocs);
456 if (ret)
457 goto out;
458 }
459
460 submit->nr_cmds = i;
461
462 ret = msm_gpu_submit(gpu, submit, ctx);
463
Rob Clarkb6295f92016-03-15 18:26:28 -0400464 args->fence = submit->fence->seqno;
Rob Clark7198e6b2013-07-19 12:59:32 -0400465
466out:
Rob Clark40e68152016-05-03 09:50:26 -0400467 submit_cleanup(submit);
468 if (ret)
469 msm_gem_submit_free(submit);
Rob Clarkb5b4c262016-05-17 15:43:35 -0400470out_unlock:
Rob Clarkc2703b12014-02-06 19:19:20 -0500471 mutex_unlock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400472 return ret;
473}