Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / gpu / drm / msm / msm_gem_submit.c
1 /*
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
26 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
27 #define BO_VALID    0x8000
28 #define BO_LOCKED   0x4000
29 #define BO_PINNED   0x2000
30
31 static inline void __user *to_user_ptr(u64 address)
32 {
33         return (void __user *)(uintptr_t)address;
34 }
35
36 static 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
58 static inline unsigned long __must_check
59 copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
60 {
61         if (access_ok(VERIFY_READ, from, n))
62                 return __copy_from_user_inatomic(to, from, n);
63         return -EFAULT;
64 }
65
66 static int submit_lookup_objects(struct msm_gem_submit *submit,
67                 struct drm_msm_gem_submit *args, struct drm_file *file)
68 {
69         unsigned i;
70         int ret = 0;
71
72         spin_lock(&file->table_lock);
73         pagefault_disable();
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 =
80                         to_user_ptr(args->bos + (i * sizeof(submit_bo)));
81
82                 ret = copy_from_user_inatomic(&submit_bo, userptr, sizeof(submit_bo));
83                 if (unlikely(ret)) {
84                         pagefault_enable();
85                         spin_unlock(&file->table_lock);
86                         ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
87                         if (ret)
88                                 goto out;
89                         spin_lock(&file->table_lock);
90                         pagefault_disable();
91                 }
92
93                 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
94                         DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
95                         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) {
108                         DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
109                         ret = -EINVAL;
110                         goto out_unlock;
111                 }
112
113                 msm_obj = to_msm_bo(obj);
114
115                 if (!list_empty(&msm_obj->submit_entry)) {
116                         DRM_ERROR("handle %u at index %u already on submit list\n",
117                                         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
129 out_unlock:
130         pagefault_enable();
131         spin_unlock(&file->table_lock);
132
133 out:
134         submit->nr_bos = i;
135
136         return ret;
137 }
138
139 static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
140 {
141         struct msm_gem_object *msm_obj = submit->bos[i].obj;
142
143         if (submit->bos[i].flags & BO_PINNED)
144                 msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
145
146         if (submit->bos[i].flags & BO_LOCKED)
147                 ww_mutex_unlock(&msm_obj->resv->lock);
148
149         if (!(submit->bos[i].flags & BO_VALID))
150                 submit->bos[i].iova = 0;
151
152         submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
153 }
154
155 /* This is where we make sure all the bo's are reserved and pin'd: */
156 static int submit_validate_objects(struct msm_gem_submit *submit)
157 {
158         int contended, slow_locked = -1, i, ret = 0;
159
160 retry:
161         submit->valid = true;
162
163         for (i = 0; i < submit->nr_bos; i++) {
164                 struct msm_gem_object *msm_obj = submit->bos[i].obj;
165                 uint32_t iova;
166
167                 if (slow_locked == i)
168                         slow_locked = -1;
169
170                 contended = i;
171
172                 if (!(submit->bos[i].flags & BO_LOCKED)) {
173                         ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
174                                         &submit->ticket);
175                         if (ret)
176                                 goto fail;
177                         submit->bos[i].flags |= BO_LOCKED;
178                 }
179
180
181                 /* if locking succeeded, pin bo: */
182                 ret = msm_gem_get_iova_locked(&msm_obj->base,
183                                 submit->gpu->id, &iova);
184
185                 /* this would break the logic in the fail path.. there is no
186                  * reason for this to happen, but just to be on the safe side
187                  * let's notice if this starts happening in the future:
188                  */
189                 WARN_ON(ret == -EDEADLK);
190
191                 if (ret)
192                         goto fail;
193
194                 submit->bos[i].flags |= BO_PINNED;
195
196                 if (iova == submit->bos[i].iova) {
197                         submit->bos[i].flags |= BO_VALID;
198                 } else {
199                         submit->bos[i].iova = iova;
200                         submit->bos[i].flags &= ~BO_VALID;
201                         submit->valid = false;
202                 }
203         }
204
205         ww_acquire_done(&submit->ticket);
206
207         return 0;
208
209 fail:
210         for (; i >= 0; i--)
211                 submit_unlock_unpin_bo(submit, i);
212
213         if (slow_locked > 0)
214                 submit_unlock_unpin_bo(submit, slow_locked);
215
216         if (ret == -EDEADLK) {
217                 struct msm_gem_object *msm_obj = submit->bos[contended].obj;
218                 /* we lost out in a seqno race, lock and retry.. */
219                 ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
220                                 &submit->ticket);
221                 if (!ret) {
222                         submit->bos[contended].flags |= BO_LOCKED;
223                         slow_locked = contended;
224                         goto retry;
225                 }
226         }
227
228         return ret;
229 }
230
231 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
232                 struct msm_gem_object **obj, uint32_t *iova, bool *valid)
233 {
234         if (idx >= submit->nr_bos) {
235                 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
236                                 idx, submit->nr_bos);
237                 return -EINVAL;
238         }
239
240         if (obj)
241                 *obj = submit->bos[idx].obj;
242         if (iova)
243                 *iova = submit->bos[idx].iova;
244         if (valid)
245                 *valid = !!(submit->bos[idx].flags & BO_VALID);
246
247         return 0;
248 }
249
250 /* process the reloc's and patch up the cmdstream as needed: */
251 static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
252                 uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
253 {
254         uint32_t i, last_offset = 0;
255         uint32_t *ptr;
256         int ret;
257
258         if (offset % 4) {
259                 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
260                 return -EINVAL;
261         }
262
263         /* For now, just map the entire thing.  Eventually we probably
264          * to do it page-by-page, w/ kmap() if not vmap()d..
265          */
266         ptr = msm_gem_vaddr_locked(&obj->base);
267
268         if (IS_ERR(ptr)) {
269                 ret = PTR_ERR(ptr);
270                 DBG("failed to map: %d", ret);
271                 return ret;
272         }
273
274         for (i = 0; i < nr_relocs; i++) {
275                 struct drm_msm_gem_submit_reloc submit_reloc;
276                 void __user *userptr =
277                         to_user_ptr(relocs + (i * sizeof(submit_reloc)));
278                 uint32_t iova, off;
279                 bool valid;
280
281                 ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
282                 if (ret)
283                         return -EFAULT;
284
285                 if (submit_reloc.submit_offset % 4) {
286                         DRM_ERROR("non-aligned reloc offset: %u\n",
287                                         submit_reloc.submit_offset);
288                         return -EINVAL;
289                 }
290
291                 /* offset in dwords: */
292                 off = submit_reloc.submit_offset / 4;
293
294                 if ((off >= (obj->base.size / 4)) ||
295                                 (off < last_offset)) {
296                         DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
297                         return -EINVAL;
298                 }
299
300                 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
301                 if (ret)
302                         return ret;
303
304                 if (valid)
305                         continue;
306
307                 iova += submit_reloc.reloc_offset;
308
309                 if (submit_reloc.shift < 0)
310                         iova >>= -submit_reloc.shift;
311                 else
312                         iova <<= submit_reloc.shift;
313
314                 ptr[off] = iova | submit_reloc.or;
315
316                 last_offset = off;
317         }
318
319         return 0;
320 }
321
322 static void submit_cleanup(struct msm_gem_submit *submit, bool fail)
323 {
324         unsigned i;
325
326         for (i = 0; i < submit->nr_bos; i++) {
327                 struct msm_gem_object *msm_obj = submit->bos[i].obj;
328                 submit_unlock_unpin_bo(submit, i);
329                 list_del_init(&msm_obj->submit_entry);
330                 drm_gem_object_unreference(&msm_obj->base);
331         }
332
333         ww_acquire_fini(&submit->ticket);
334 }
335
336 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
337                 struct drm_file *file)
338 {
339         struct msm_drm_private *priv = dev->dev_private;
340         struct drm_msm_gem_submit *args = data;
341         struct msm_file_private *ctx = file->driver_priv;
342         struct msm_gem_submit *submit;
343         struct msm_gpu *gpu;
344         unsigned i;
345         int ret;
346
347         /* for now, we just have 3d pipe.. eventually this would need to
348          * be more clever to dispatch to appropriate gpu module:
349          */
350         if (args->pipe != MSM_PIPE_3D0)
351                 return -EINVAL;
352
353         gpu = priv->gpu;
354
355         if (args->nr_cmds > MAX_CMDS)
356                 return -EINVAL;
357
358         mutex_lock(&dev->struct_mutex);
359
360         submit = submit_create(dev, gpu, args->nr_bos);
361         if (!submit) {
362                 ret = -ENOMEM;
363                 goto out;
364         }
365
366         ret = submit_lookup_objects(submit, args, file);
367         if (ret)
368                 goto out;
369
370         ret = submit_validate_objects(submit);
371         if (ret)
372                 goto out;
373
374         for (i = 0; i < args->nr_cmds; i++) {
375                 struct drm_msm_gem_submit_cmd submit_cmd;
376                 void __user *userptr =
377                         to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
378                 struct msm_gem_object *msm_obj;
379                 uint32_t iova;
380
381                 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
382                 if (ret) {
383                         ret = -EFAULT;
384                         goto out;
385                 }
386
387                 /* validate input from userspace: */
388                 switch (submit_cmd.type) {
389                 case MSM_SUBMIT_CMD_BUF:
390                 case MSM_SUBMIT_CMD_IB_TARGET_BUF:
391                 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
392                         break;
393                 default:
394                         DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
395                         ret = -EINVAL;
396                         goto out;
397                 }
398
399                 ret = submit_bo(submit, submit_cmd.submit_idx,
400                                 &msm_obj, &iova, NULL);
401                 if (ret)
402                         goto out;
403
404                 if (submit_cmd.size % 4) {
405                         DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
406                                         submit_cmd.size);
407                         ret = -EINVAL;
408                         goto out;
409                 }
410
411                 if ((submit_cmd.size + submit_cmd.submit_offset) >=
412                                 msm_obj->base.size) {
413                         DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
414                         ret = -EINVAL;
415                         goto out;
416                 }
417
418                 submit->cmd[i].type = submit_cmd.type;
419                 submit->cmd[i].size = submit_cmd.size / 4;
420                 submit->cmd[i].iova = iova + submit_cmd.submit_offset;
421                 submit->cmd[i].idx  = submit_cmd.submit_idx;
422
423                 if (submit->valid)
424                         continue;
425
426                 ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
427                                 submit_cmd.nr_relocs, submit_cmd.relocs);
428                 if (ret)
429                         goto out;
430         }
431
432         submit->nr_cmds = i;
433
434         ret = msm_gpu_submit(gpu, submit, ctx);
435
436         args->fence = submit->fence;
437
438 out:
439         if (submit)
440                 submit_cleanup(submit, !!ret);
441         mutex_unlock(&dev->struct_mutex);
442         return ret;
443 }