blob: 635eff17c3d116545c75d3e1a4d5cd9d652a0fca [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
80 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
81 if (ret) {
82 ret = -EFAULT;
83 goto out_unlock;
84 }
85
Rob Clark93ddb0d2014-03-03 09:42:33 -050086 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
Rob Clark19872532013-09-06 15:36:40 -040087 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
Rob Clark7198e6b2013-07-19 12:59:32 -040088 ret = -EINVAL;
89 goto out_unlock;
90 }
91
92 submit->bos[i].flags = submit_bo.flags;
93 /* in validate_objects() we figure out if this is true: */
94 submit->bos[i].iova = submit_bo.presumed;
95
96 /* normally use drm_gem_object_lookup(), but for bulk lookup
97 * all under single table_lock just hit object_idr directly:
98 */
99 obj = idr_find(&file->object_idr, submit_bo.handle);
100 if (!obj) {
Rob Clark19872532013-09-06 15:36:40 -0400101 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400102 ret = -EINVAL;
103 goto out_unlock;
104 }
105
106 msm_obj = to_msm_bo(obj);
107
108 if (!list_empty(&msm_obj->submit_entry)) {
Rob Clark19872532013-09-06 15:36:40 -0400109 DRM_ERROR("handle %u at index %u already on submit list\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400110 submit_bo.handle, i);
111 ret = -EINVAL;
112 goto out_unlock;
113 }
114
115 drm_gem_object_reference(obj);
116
117 submit->bos[i].obj = msm_obj;
118
119 list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
120 }
121
122out_unlock:
123 submit->nr_bos = i;
124 spin_unlock(&file->table_lock);
125
126 return ret;
127}
128
129static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
130{
131 struct msm_gem_object *msm_obj = submit->bos[i].obj;
132
133 if (submit->bos[i].flags & BO_PINNED)
134 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
135
136 if (submit->bos[i].flags & BO_LOCKED)
137 ww_mutex_unlock(&msm_obj->resv->lock);
138
139 if (!(submit->bos[i].flags & BO_VALID))
140 submit->bos[i].iova = 0;
141
142 submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
143}
144
145/* This is where we make sure all the bo's are reserved and pin'd: */
Rob Clark340faef2016-03-14 13:56:37 -0400146static int submit_lock_objects(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400147{
148 int contended, slow_locked = -1, i, ret = 0;
149
150retry:
Rob Clark7198e6b2013-07-19 12:59:32 -0400151 for (i = 0; i < submit->nr_bos; i++) {
152 struct msm_gem_object *msm_obj = submit->bos[i].obj;
Rob Clark7198e6b2013-07-19 12:59:32 -0400153
154 if (slow_locked == i)
155 slow_locked = -1;
156
157 contended = i;
158
159 if (!(submit->bos[i].flags & BO_LOCKED)) {
160 ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
161 &submit->ticket);
162 if (ret)
163 goto fail;
164 submit->bos[i].flags |= BO_LOCKED;
165 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400166 }
167
168 ww_acquire_done(&submit->ticket);
169
170 return 0;
171
172fail:
173 for (; i >= 0; i--)
174 submit_unlock_unpin_bo(submit, i);
175
176 if (slow_locked > 0)
177 submit_unlock_unpin_bo(submit, slow_locked);
178
179 if (ret == -EDEADLK) {
180 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
181 /* we lost out in a seqno race, lock and retry.. */
182 ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
183 &submit->ticket);
184 if (!ret) {
185 submit->bos[contended].flags |= BO_LOCKED;
186 slow_locked = contended;
187 goto retry;
188 }
189 }
190
191 return ret;
192}
193
Rob Clarkb6295f92016-03-15 18:26:28 -0400194static int submit_fence_sync(struct msm_gem_submit *submit)
195{
196 int i, ret = 0;
197
198 for (i = 0; i < submit->nr_bos; i++) {
199 struct msm_gem_object *msm_obj = submit->bos[i].obj;
200 bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE;
201
202 ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write);
203 if (ret)
204 break;
205 }
206
207 return ret;
208}
209
Rob Clark340faef2016-03-14 13:56:37 -0400210static int submit_pin_objects(struct msm_gem_submit *submit)
211{
212 int i, ret = 0;
213
214 submit->valid = true;
215
216 for (i = 0; i < submit->nr_bos; i++) {
217 struct msm_gem_object *msm_obj = submit->bos[i].obj;
218 uint32_t iova;
219
220 /* if locking succeeded, pin bo: */
221 ret = msm_gem_get_iova_locked(&msm_obj->base,
222 submit->gpu->id, &iova);
223
224 if (ret)
225 break;
226
227 submit->bos[i].flags |= BO_PINNED;
228
229 if (iova == submit->bos[i].iova) {
230 submit->bos[i].flags |= BO_VALID;
231 } else {
232 submit->bos[i].iova = iova;
233 /* iova changed, so address in cmdstream is not valid: */
234 submit->bos[i].flags &= ~BO_VALID;
235 submit->valid = false;
236 }
237 }
238
239 return ret;
240}
241
Rob Clark7198e6b2013-07-19 12:59:32 -0400242static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
243 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
244{
245 if (idx >= submit->nr_bos) {
Rob Clark19872532013-09-06 15:36:40 -0400246 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
247 idx, submit->nr_bos);
248 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400249 }
250
251 if (obj)
252 *obj = submit->bos[idx].obj;
253 if (iova)
254 *iova = submit->bos[idx].iova;
255 if (valid)
256 *valid = !!(submit->bos[idx].flags & BO_VALID);
257
258 return 0;
259}
260
261/* process the reloc's and patch up the cmdstream as needed: */
262static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
263 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
264{
265 uint32_t i, last_offset = 0;
266 uint32_t *ptr;
267 int ret;
268
269 if (offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400270 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
Rob Clark7198e6b2013-07-19 12:59:32 -0400271 return -EINVAL;
272 }
273
274 /* For now, just map the entire thing. Eventually we probably
275 * to do it page-by-page, w/ kmap() if not vmap()d..
276 */
Rob Clarkc2703b12014-02-06 19:19:20 -0500277 ptr = msm_gem_vaddr_locked(&obj->base);
Rob Clark7198e6b2013-07-19 12:59:32 -0400278
279 if (IS_ERR(ptr)) {
280 ret = PTR_ERR(ptr);
281 DBG("failed to map: %d", ret);
282 return ret;
283 }
284
285 for (i = 0; i < nr_relocs; i++) {
286 struct drm_msm_gem_submit_reloc submit_reloc;
287 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300288 u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400289 uint32_t iova, off;
290 bool valid;
291
292 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
293 if (ret)
294 return -EFAULT;
295
296 if (submit_reloc.submit_offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400297 DRM_ERROR("non-aligned reloc offset: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400298 submit_reloc.submit_offset);
299 return -EINVAL;
300 }
301
302 /* offset in dwords: */
303 off = submit_reloc.submit_offset / 4;
304
305 if ((off >= (obj->base.size / 4)) ||
306 (off < last_offset)) {
Rob Clark19872532013-09-06 15:36:40 -0400307 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400308 return -EINVAL;
309 }
310
311 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
312 if (ret)
313 return ret;
314
315 if (valid)
316 continue;
317
318 iova += submit_reloc.reloc_offset;
319
320 if (submit_reloc.shift < 0)
321 iova >>= -submit_reloc.shift;
322 else
323 iova <<= submit_reloc.shift;
324
325 ptr[off] = iova | submit_reloc.or;
326
327 last_offset = off;
328 }
329
330 return 0;
331}
332
Rob Clark40e68152016-05-03 09:50:26 -0400333static void submit_cleanup(struct msm_gem_submit *submit)
Rob Clark7198e6b2013-07-19 12:59:32 -0400334{
335 unsigned i;
336
Rob Clark7198e6b2013-07-19 12:59:32 -0400337 for (i = 0; i < submit->nr_bos; i++) {
338 struct msm_gem_object *msm_obj = submit->bos[i].obj;
339 submit_unlock_unpin_bo(submit, i);
340 list_del_init(&msm_obj->submit_entry);
341 drm_gem_object_unreference(&msm_obj->base);
342 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400343
344 ww_acquire_fini(&submit->ticket);
Rob Clark7198e6b2013-07-19 12:59:32 -0400345}
346
347int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
348 struct drm_file *file)
349{
350 struct msm_drm_private *priv = dev->dev_private;
351 struct drm_msm_gem_submit *args = data;
352 struct msm_file_private *ctx = file->driver_priv;
353 struct msm_gem_submit *submit;
Rob Clarkc01a9582016-02-03 13:12:31 -0500354 struct msm_gpu *gpu = priv->gpu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400355 unsigned i;
356 int ret;
357
Rob Clarkc01a9582016-02-03 13:12:31 -0500358 if (!gpu)
359 return -ENXIO;
360
Rob Clark7198e6b2013-07-19 12:59:32 -0400361 /* for now, we just have 3d pipe.. eventually this would need to
362 * be more clever to dispatch to appropriate gpu module:
363 */
364 if (args->pipe != MSM_PIPE_3D0)
365 return -EINVAL;
366
Rob Clark7198e6b2013-07-19 12:59:32 -0400367 if (args->nr_cmds > MAX_CMDS)
368 return -EINVAL;
369
370 submit = submit_create(dev, gpu, args->nr_bos);
Rob Clark687f0842016-02-03 13:24:35 -0500371 if (!submit)
372 return -ENOMEM;
373
374 mutex_lock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400375
376 ret = submit_lookup_objects(submit, args, file);
377 if (ret)
378 goto out;
379
Rob Clark340faef2016-03-14 13:56:37 -0400380 ret = submit_lock_objects(submit);
381 if (ret)
382 goto out;
383
Rob Clarkb6295f92016-03-15 18:26:28 -0400384 ret = submit_fence_sync(submit);
385 if (ret)
386 goto out;
387
Rob Clark340faef2016-03-14 13:56:37 -0400388 ret = submit_pin_objects(submit);
Rob Clark7198e6b2013-07-19 12:59:32 -0400389 if (ret)
390 goto out;
391
392 for (i = 0; i < args->nr_cmds; i++) {
393 struct drm_msm_gem_submit_cmd submit_cmd;
394 void __user *userptr =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300395 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
Rob Clark7198e6b2013-07-19 12:59:32 -0400396 struct msm_gem_object *msm_obj;
397 uint32_t iova;
398
399 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
400 if (ret) {
401 ret = -EFAULT;
402 goto out;
403 }
404
Rob Clark93ddb0d2014-03-03 09:42:33 -0500405 /* validate input from userspace: */
406 switch (submit_cmd.type) {
407 case MSM_SUBMIT_CMD_BUF:
408 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
409 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
410 break;
411 default:
412 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
413 ret = -EINVAL;
414 goto out;
415 }
416
Rob Clark7198e6b2013-07-19 12:59:32 -0400417 ret = submit_bo(submit, submit_cmd.submit_idx,
418 &msm_obj, &iova, NULL);
419 if (ret)
420 goto out;
421
422 if (submit_cmd.size % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400423 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400424 submit_cmd.size);
425 ret = -EINVAL;
426 goto out;
427 }
428
Rob Clark19872532013-09-06 15:36:40 -0400429 if ((submit_cmd.size + submit_cmd.submit_offset) >=
430 msm_obj->base.size) {
431 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
Rob Clark7198e6b2013-07-19 12:59:32 -0400432 ret = -EINVAL;
433 goto out;
434 }
435
436 submit->cmd[i].type = submit_cmd.type;
437 submit->cmd[i].size = submit_cmd.size / 4;
438 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
Rob Clarka7d3c952014-05-30 14:47:38 -0400439 submit->cmd[i].idx = submit_cmd.submit_idx;
Rob Clark7198e6b2013-07-19 12:59:32 -0400440
441 if (submit->valid)
442 continue;
443
444 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
445 submit_cmd.nr_relocs, submit_cmd.relocs);
446 if (ret)
447 goto out;
448 }
449
450 submit->nr_cmds = i;
451
452 ret = msm_gpu_submit(gpu, submit, ctx);
453
Rob Clarkb6295f92016-03-15 18:26:28 -0400454 args->fence = submit->fence->seqno;
Rob Clark7198e6b2013-07-19 12:59:32 -0400455
456out:
Rob Clark40e68152016-05-03 09:50:26 -0400457 submit_cleanup(submit);
458 if (ret)
459 msm_gem_submit_free(submit);
Rob Clarkc2703b12014-02-06 19:19:20 -0500460 mutex_unlock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400461 return ret;
462}