blob: ea5f709e2e2780e41356c5233e5e2288e1706755 [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,
32 struct msm_gpu *gpu, int nr)
33{
34 struct msm_gem_submit *submit;
35 int sz = sizeof(*submit) + (nr * sizeof(submit->bos[0]));
36
37 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Rob Clark6860b562016-05-03 09:49:40 -040038 if (!submit)
39 return NULL;
Rob Clark7198e6b2013-07-19 12:59:32 -040040
Rob Clark6860b562016-05-03 09:49:40 -040041 submit->dev = dev;
42 submit->gpu = gpu;
Rob Clarkba344af2016-05-24 18:43:26 -040043 submit->fence = NULL;
Rob Clark4816b622016-05-03 10:10:15 -040044 submit->pid = get_pid(task_pid(current));
Rob Clark7198e6b2013-07-19 12:59:32 -040045
Rob Clark6860b562016-05-03 09:49:40 -040046 /* initially, until copy_from_user() and bo lookup succeeds: */
47 submit->nr_bos = 0;
48 submit->nr_cmds = 0;
49
Rob Clarkba344af2016-05-24 18:43:26 -040050 INIT_LIST_HEAD(&submit->node);
Rob Clark6860b562016-05-03 09:49:40 -040051 INIT_LIST_HEAD(&submit->bo_list);
52 ww_acquire_init(&submit->ticket, &reservation_ww_class);
Rob Clark7198e6b2013-07-19 12:59:32 -040053
54 return submit;
55}
56
Rob Clark40e68152016-05-03 09:50:26 -040057void msm_gem_submit_free(struct msm_gem_submit *submit)
58{
59 fence_put(submit->fence);
60 list_del(&submit->node);
Rob Clark4816b622016-05-03 10:10:15 -040061 put_pid(submit->pid);
Rob Clark40e68152016-05-03 09:50:26 -040062 kfree(submit);
63}
64
Rob Clark7198e6b2013-07-19 12:59:32 -040065static int submit_lookup_objects(struct msm_gem_submit *submit,
66 struct drm_msm_gem_submit *args, struct drm_file *file)
67{
68 unsigned i;
69 int ret = 0;
70
71 spin_lock(&file->table_lock);
72
73 for (i = 0; i < args->nr_bos; i++) {
74 struct drm_msm_gem_submit_bo submit_bo;
75 struct drm_gem_object *obj;
76 struct msm_gem_object *msm_obj;
77 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -030078 u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
Rob Clark7198e6b2013-07-19 12:59:32 -040079
Rob Clarka9e26ca2016-06-01 14:02:51 -040080 /* make sure we don't have garbage flags, in case we hit
81 * error path before flags is initialized:
82 */
83 submit->bos[i].flags = 0;
84
Rob Clark7198e6b2013-07-19 12:59:32 -040085 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
86 if (ret) {
87 ret = -EFAULT;
88 goto out_unlock;
89 }
90
Rob Clark93ddb0d2014-03-03 09:42:33 -050091 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
Rob Clark19872532013-09-06 15:36:40 -040092 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
Rob Clark7198e6b2013-07-19 12:59:32 -040093 ret = -EINVAL;
94 goto out_unlock;
95 }
96
97 submit->bos[i].flags = submit_bo.flags;
98 /* in validate_objects() we figure out if this is true: */
99 submit->bos[i].iova = submit_bo.presumed;
100
101 /* normally use drm_gem_object_lookup(), but for bulk lookup
102 * all under single table_lock just hit object_idr directly:
103 */
104 obj = idr_find(&file->object_idr, submit_bo.handle);
105 if (!obj) {
Rob Clark19872532013-09-06 15:36:40 -0400106 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400107 ret = -EINVAL;
108 goto out_unlock;
109 }
110
111 msm_obj = to_msm_bo(obj);
112
113 if (!list_empty(&msm_obj->submit_entry)) {
Rob Clark19872532013-09-06 15:36:40 -0400114 DRM_ERROR("handle %u at index %u already on submit list\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400115 submit_bo.handle, i);
116 ret = -EINVAL;
117 goto out_unlock;
118 }
119
120 drm_gem_object_reference(obj);
121
122 submit->bos[i].obj = msm_obj;
123
124 list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
125 }
126
127out_unlock:
128 submit->nr_bos = i;
129 spin_unlock(&file->table_lock);
130
131 return ret;
132}
133
134static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
135{
136 struct msm_gem_object *msm_obj = submit->bos[i].obj;
137
138 if (submit->bos[i].flags & BO_PINNED)
139 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
140
141 if (submit->bos[i].flags & BO_LOCKED)
142 ww_mutex_unlock(&msm_obj->resv->lock);
143
144 if (!(submit->bos[i].flags & BO_VALID))
145 submit->bos[i].iova = 0;
146
147 submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
148}
149
150/* This is where we make sure all the bo's are reserved and pin'd: */
Rob Clark340faef2016-03-14 13:56:37 -0400151static int submit_lock_objects(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400152{
153 int contended, slow_locked = -1, i, ret = 0;
154
155retry:
Rob Clark7198e6b2013-07-19 12:59:32 -0400156 for (i = 0; i < submit->nr_bos; i++) {
157 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7198e6b2013-07-19 12:59:32 -0400158
159 if (slow_locked == i)
160 slow_locked = -1;
161
162 contended = i;
163
164 if (!(submit->bos[i].flags & BO_LOCKED)) {
165 ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
166 &submit->ticket);
167 if (ret)
168 goto fail;
169 submit->bos[i].flags |= BO_LOCKED;
170 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400171 }
172
173 ww_acquire_done(&submit->ticket);
174
175 return 0;
176
177fail:
178 for (; i >= 0; i--)
179 submit_unlock_unpin_bo(submit, i);
180
181 if (slow_locked > 0)
182 submit_unlock_unpin_bo(submit, slow_locked);
183
184 if (ret == -EDEADLK) {
185 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
186 /* we lost out in a seqno race, lock and retry.. */
187 ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
188 &submit->ticket);
189 if (!ret) {
190 submit->bos[contended].flags |= BO_LOCKED;
191 slow_locked = contended;
192 goto retry;
193 }
194 }
195
196 return ret;
197}
198
Rob Clarkb6295f92016-03-15 18:26:28 -0400199static int submit_fence_sync(struct msm_gem_submit *submit)
200{
201 int i, ret = 0;
202
203 for (i = 0; i < submit->nr_bos; i++) {
204 struct msm_gem_object *msm_obj = submit->bos[i].obj;
205 bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
206
207 ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
208 if (ret)
209 break;
210 }
211
212 return ret;
213}
214
Rob Clark340faef2016-03-14 13:56:37 -0400215static int submit_pin_objects(struct msm_gem_submit *submit)
216{
217 int i, ret = 0;
218
219 submit->valid = true;
220
221 for (i = 0; i < submit->nr_bos; i++) {
222 struct msm_gem_object *msm_obj = submit->bos[i].obj;
223 uint32_t iova;
224
225 /* if locking succeeded, pin bo: */
226 ret = msm_gem_get_iova_locked(&msm_obj->base,
227 submit->gpu->id, &iova);
228
229 if (ret)
230 break;
231
232 submit->bos[i].flags |= BO_PINNED;
233
234 if (iova == submit->bos[i].iova) {
235 submit->bos[i].flags |= BO_VALID;
236 } else {
237 submit->bos[i].iova = iova;
238 /* iova changed, so address in cmdstream is not valid: */
239 submit->bos[i].flags &= ~BO_VALID;
240 submit->valid = false;
241 }
242 }
243
244 return ret;
245}
246
Rob Clark7198e6b2013-07-19 12:59:32 -0400247static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
248 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
249{
250 if (idx >= submit->nr_bos) {
Rob Clark19872532013-09-06 15:36:40 -0400251 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
252 idx, submit->nr_bos);
253 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400254 }
255
256 if (obj)
257 *obj = submit->bos[idx].obj;
258 if (iova)
259 *iova = submit->bos[idx].iova;
260 if (valid)
261 *valid = !!(submit->bos[idx].flags & BO_VALID);
262
263 return 0;
264}
265
266/* process the reloc's and patch up the cmdstream as needed: */
267static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
268 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
269{
270 uint32_t i, last_offset = 0;
271 uint32_t *ptr;
272 int ret;
273
274 if (offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400275 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
Rob Clark7198e6b2013-07-19 12:59:32 -0400276 return -EINVAL;
277 }
278
279 /* For now, just map the entire thing. Eventually we probably
280 * to do it page-by-page, w/ kmap() if not vmap()d..
281 */
Rob Clarkc2703b12014-02-06 19:19:20 -0500282 ptr = msm_gem_vaddr_locked(&obj->base);
Rob Clark7198e6b2013-07-19 12:59:32 -0400283
284 if (IS_ERR(ptr)) {
285 ret = PTR_ERR(ptr);
286 DBG("failed to map: %d", ret);
287 return ret;
288 }
289
290 for (i = 0; i < nr_relocs; i++) {
291 struct drm_msm_gem_submit_reloc submit_reloc;
292 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300293 u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400294 uint32_t iova, off;
295 bool valid;
296
297 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
298 if (ret)
299 return -EFAULT;
300
301 if (submit_reloc.submit_offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400302 DRM_ERROR("non-aligned reloc offset: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400303 submit_reloc.submit_offset);
304 return -EINVAL;
305 }
306
307 /* offset in dwords: */
308 off = submit_reloc.submit_offset / 4;
309
310 if ((off >= (obj->base.size / 4)) ||
311 (off < last_offset)) {
Rob Clark19872532013-09-06 15:36:40 -0400312 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400313 return -EINVAL;
314 }
315
316 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
317 if (ret)
318 return ret;
319
320 if (valid)
321 continue;
322
323 iova += submit_reloc.reloc_offset;
324
325 if (submit_reloc.shift < 0)
326 iova >>= -submit_reloc.shift;
327 else
328 iova <<= submit_reloc.shift;
329
330 ptr[off] = iova | submit_reloc.or;
331
332 last_offset = off;
333 }
334
335 return 0;
336}
337
Rob Clark40e68152016-05-03 09:50:26 -0400338static void submit_cleanup(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400339{
340 unsigned i;
341
Rob Clark7198e6b2013-07-19 12:59:32 -0400342 for (i = 0; i < submit->nr_bos; i++) {
343 struct msm_gem_object *msm_obj = submit->bos[i].obj;
344 submit_unlock_unpin_bo(submit, i);
345 list_del_init(&msm_obj->submit_entry);
346 drm_gem_object_unreference(&msm_obj->base);
347 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400348
349 ww_acquire_fini(&submit->ticket);
Rob Clark7198e6b2013-07-19 12:59:32 -0400350}
351
352int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
353 struct drm_file *file)
354{
355 struct msm_drm_private *priv = dev->dev_private;
356 struct drm_msm_gem_submit *args = data;
357 struct msm_file_private *ctx = file->driver_priv;
358 struct msm_gem_submit *submit;
Rob Clarkc01a9582016-02-03 13:12:31 -0500359 struct msm_gpu *gpu = priv->gpu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400360 unsigned i;
361 int ret;
362
Rob Clarkc01a9582016-02-03 13:12:31 -0500363 if (!gpu)
364 return -ENXIO;
365
Rob Clark7198e6b2013-07-19 12:59:32 -0400366 /* for now, we just have 3d pipe.. eventually this would need to
367 * be more clever to dispatch to appropriate gpu module:
368 */
369 if (args->pipe != MSM_PIPE_3D0)
370 return -EINVAL;
371
Rob Clark7198e6b2013-07-19 12:59:32 -0400372 if (args->nr_cmds > MAX_CMDS)
373 return -EINVAL;
374
Rob Clarkb5b4c262016-05-17 15:43:35 -0400375 ret = mutex_lock_interruptible(&dev->struct_mutex);
376 if (ret)
377 return ret;
Rob Clark687f0842016-02-03 13:24:35 -0500378
Rob Clarkb5b4c262016-05-17 15:43:35 -0400379 submit = submit_create(dev, gpu, args->nr_bos);
380 if (!submit) {
381 ret = -ENOMEM;
382 goto out_unlock;
383 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400384
385 ret = submit_lookup_objects(submit, args, file);
386 if (ret)
387 goto out;
388
Rob Clark340faef2016-03-14 13:56:37 -0400389 ret = submit_lock_objects(submit);
390 if (ret)
391 goto out;
392
Rob Clarkb6295f92016-03-15 18:26:28 -0400393 ret = submit_fence_sync(submit);
394 if (ret)
395 goto out;
396
Rob Clark340faef2016-03-14 13:56:37 -0400397 ret = submit_pin_objects(submit);
Rob Clark7198e6b2013-07-19 12:59:32 -0400398 if (ret)
399 goto out;
400
401 for (i = 0; i < args->nr_cmds; i++) {
402 struct drm_msm_gem_submit_cmd submit_cmd;
403 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300404 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400405 struct msm_gem_object *msm_obj;
406 uint32_t iova;
407
408 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
409 if (ret) {
410 ret = -EFAULT;
411 goto out;
412 }
413
Rob Clark93ddb0d2014-03-03 09:42:33 -0500414 /* validate input from userspace: */
415 switch (submit_cmd.type) {
416 case MSM_SUBMIT_CMD_BUF:
417 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
418 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
419 break;
420 default:
421 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
422 ret = -EINVAL;
423 goto out;
424 }
425
Rob Clark7198e6b2013-07-19 12:59:32 -0400426 ret = submit_bo(submit, submit_cmd.submit_idx,
427 &msm_obj, &iova, NULL);
428 if (ret)
429 goto out;
430
431 if (submit_cmd.size % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400432 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400433 submit_cmd.size);
434 ret = -EINVAL;
435 goto out;
436 }
437
Rob Clark19872532013-09-06 15:36:40 -0400438 if ((submit_cmd.size + submit_cmd.submit_offset) >=
439 msm_obj->base.size) {
440 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
Rob Clark7198e6b2013-07-19 12:59:32 -0400441 ret = -EINVAL;
442 goto out;
443 }
444
445 submit->cmd[i].type = submit_cmd.type;
446 submit->cmd[i].size = submit_cmd.size / 4;
447 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
Rob Clarka7d3c952014-05-30 14:47:38 -0400448 submit->cmd[i].idx = submit_cmd.submit_idx;
Rob Clark7198e6b2013-07-19 12:59:32 -0400449
450 if (submit->valid)
451 continue;
452
453 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
454 submit_cmd.nr_relocs, submit_cmd.relocs);
455 if (ret)
456 goto out;
457 }
458
459 submit->nr_cmds = i;
460
461 ret = msm_gpu_submit(gpu, submit, ctx);
462
Rob Clarkb6295f92016-03-15 18:26:28 -0400463 args->fence = submit->fence->seqno;
Rob Clark7198e6b2013-07-19 12:59:32 -0400464
465out:
Rob Clark40e68152016-05-03 09:50:26 -0400466 submit_cleanup(submit);
467 if (ret)
468 msm_gem_submit_free(submit);
Rob Clarkb5b4c262016-05-17 15:43:35 -0400469out_unlock:
Rob Clarkc2703b12014-02-06 19:19:20 -0500470 mutex_unlock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400471 return ret;
472}