blob: b231544b3f52dc729d1449080a791be1bc50be63 [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 Clark340faef2016-03-14 13:56:37 -0400187static int submit_pin_objects(struct msm_gem_submit *submit)
188{
189 int i, ret = 0;
190
191 submit->valid = true;
192
193 for (i = 0; i < submit->nr_bos; i++) {
194 struct msm_gem_object *msm_obj = submit->bos[i].obj;
195 uint32_t iova;
196
197 /* if locking succeeded, pin bo: */
198 ret = msm_gem_get_iova_locked(&msm_obj->base,
199 submit->gpu->id, &iova);
200
201 if (ret)
202 break;
203
204 submit->bos[i].flags |= BO_PINNED;
205
206 if (iova == submit->bos[i].iova) {
207 submit->bos[i].flags |= BO_VALID;
208 } else {
209 submit->bos[i].iova = iova;
210 /* iova changed, so address in cmdstream is not valid: */
211 submit->bos[i].flags &= ~BO_VALID;
212 submit->valid = false;
213 }
214 }
215
216 return ret;
217}
218
Rob Clark7198e6b2013-07-19 12:59:32 -0400219static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
220 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
221{
222 if (idx >= submit->nr_bos) {
Rob Clark19872532013-09-06 15:36:40 -0400223 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
224 idx, submit->nr_bos);
225 return -EINVAL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400226 }
227
228 if (obj)
229 *obj = submit->bos[idx].obj;
230 if (iova)
231 *iova = submit->bos[idx].iova;
232 if (valid)
233 *valid = !!(submit->bos[idx].flags & BO_VALID);
234
235 return 0;
236}
237
238/* process the reloc's and patch up the cmdstream as needed: */
239static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
240 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
241{
242 uint32_t i, last_offset = 0;
243 uint32_t *ptr;
244 int ret;
245
246 if (offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400247 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
Rob Clark7198e6b2013-07-19 12:59:32 -0400248 return -EINVAL;
249 }
250
251 /* For now, just map the entire thing. Eventually we probably
252 * to do it page-by-page, w/ kmap() if not vmap()d..
253 */
Rob Clarkc2703b12014-02-06 19:19:20 -0500254 ptr = msm_gem_vaddr_locked(&obj->base);
Rob Clark7198e6b2013-07-19 12:59:32 -0400255
256 if (IS_ERR(ptr)) {
257 ret = PTR_ERR(ptr);
258 DBG("failed to map: %d", ret);
259 return ret;
260 }
261
262 for (i = 0; i < nr_relocs; i++) {
263 struct drm_msm_gem_submit_reloc submit_reloc;
264 void __user *userptr =
265 to_user_ptr(relocs + (i * sizeof(submit_reloc)));
266 uint32_t iova, off;
267 bool valid;
268
269 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
270 if (ret)
271 return -EFAULT;
272
273 if (submit_reloc.submit_offset % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400274 DRM_ERROR("non-aligned reloc offset: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400275 submit_reloc.submit_offset);
276 return -EINVAL;
277 }
278
279 /* offset in dwords: */
280 off = submit_reloc.submit_offset / 4;
281
282 if ((off >= (obj->base.size / 4)) ||
283 (off < last_offset)) {
Rob Clark19872532013-09-06 15:36:40 -0400284 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
Rob Clark7198e6b2013-07-19 12:59:32 -0400285 return -EINVAL;
286 }
287
288 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
289 if (ret)
290 return ret;
291
292 if (valid)
293 continue;
294
295 iova += submit_reloc.reloc_offset;
296
297 if (submit_reloc.shift < 0)
298 iova >>= -submit_reloc.shift;
299 else
300 iova <<= submit_reloc.shift;
301
302 ptr[off] = iova | submit_reloc.or;
303
304 last_offset = off;
305 }
306
307 return 0;
308}
309
310static void submit_cleanup(struct msm_gem_submit *submit, bool fail)
311{
312 unsigned i;
313
Rob Clark7198e6b2013-07-19 12:59:32 -0400314 for (i = 0; i < submit->nr_bos; i++) {
315 struct msm_gem_object *msm_obj = submit->bos[i].obj;
316 submit_unlock_unpin_bo(submit, i);
317 list_del_init(&msm_obj->submit_entry);
318 drm_gem_object_unreference(&msm_obj->base);
319 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400320
321 ww_acquire_fini(&submit->ticket);
Rob Clark7198e6b2013-07-19 12:59:32 -0400322}
323
324int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
325 struct drm_file *file)
326{
327 struct msm_drm_private *priv = dev->dev_private;
328 struct drm_msm_gem_submit *args = data;
329 struct msm_file_private *ctx = file->driver_priv;
330 struct msm_gem_submit *submit;
Rob Clarkc01a9582016-02-03 13:12:31 -0500331 struct msm_gpu *gpu = priv->gpu;
Rob Clark7198e6b2013-07-19 12:59:32 -0400332 unsigned i;
333 int ret;
334
Rob Clarkc01a9582016-02-03 13:12:31 -0500335 if (!gpu)
336 return -ENXIO;
337
Rob Clark7198e6b2013-07-19 12:59:32 -0400338 /* for now, we just have 3d pipe.. eventually this would need to
339 * be more clever to dispatch to appropriate gpu module:
340 */
341 if (args->pipe != MSM_PIPE_3D0)
342 return -EINVAL;
343
Rob Clark7198e6b2013-07-19 12:59:32 -0400344 if (args->nr_cmds > MAX_CMDS)
345 return -EINVAL;
346
347 submit = submit_create(dev, gpu, args->nr_bos);
Rob Clark687f0842016-02-03 13:24:35 -0500348 if (!submit)
349 return -ENOMEM;
350
351 mutex_lock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400352
353 ret = submit_lookup_objects(submit, args, file);
354 if (ret)
355 goto out;
356
Rob Clark340faef2016-03-14 13:56:37 -0400357 ret = submit_lock_objects(submit);
358 if (ret)
359 goto out;
360
361 ret = submit_pin_objects(submit);
Rob Clark7198e6b2013-07-19 12:59:32 -0400362 if (ret)
363 goto out;
364
365 for (i = 0; i < args->nr_cmds; i++) {
366 struct drm_msm_gem_submit_cmd submit_cmd;
367 void __user *userptr =
368 to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
369 struct msm_gem_object *msm_obj;
370 uint32_t iova;
371
372 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
373 if (ret) {
374 ret = -EFAULT;
375 goto out;
376 }
377
Rob Clark93ddb0d2014-03-03 09:42:33 -0500378 /* validate input from userspace: */
379 switch (submit_cmd.type) {
380 case MSM_SUBMIT_CMD_BUF:
381 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
382 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
383 break;
384 default:
385 DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
386 ret = -EINVAL;
387 goto out;
388 }
389
Rob Clark7198e6b2013-07-19 12:59:32 -0400390 ret = submit_bo(submit, submit_cmd.submit_idx,
391 &msm_obj, &iova, NULL);
392 if (ret)
393 goto out;
394
395 if (submit_cmd.size % 4) {
Rob Clark19872532013-09-06 15:36:40 -0400396 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400397 submit_cmd.size);
398 ret = -EINVAL;
399 goto out;
400 }
401
Rob Clark19872532013-09-06 15:36:40 -0400402 if ((submit_cmd.size + submit_cmd.submit_offset) >=
403 msm_obj->base.size) {
404 DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
Rob Clark7198e6b2013-07-19 12:59:32 -0400405 ret = -EINVAL;
406 goto out;
407 }
408
409 submit->cmd[i].type = submit_cmd.type;
410 submit->cmd[i].size = submit_cmd.size / 4;
411 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
Rob Clarka7d3c952014-05-30 14:47:38 -0400412 submit->cmd[i].idx = submit_cmd.submit_idx;
Rob Clark7198e6b2013-07-19 12:59:32 -0400413
414 if (submit->valid)
415 continue;
416
417 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
418 submit_cmd.nr_relocs, submit_cmd.relocs);
419 if (ret)
420 goto out;
421 }
422
423 submit->nr_cmds = i;
424
425 ret = msm_gpu_submit(gpu, submit, ctx);
426
427 args->fence = submit->fence;
428
429out:
Rob Clark687f0842016-02-03 13:24:35 -0500430 submit_cleanup(submit, !!ret);
Rob Clarkc2703b12014-02-06 19:19:20 -0500431 mutex_unlock(&dev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400432 return ret;
433}