GNU Linux-libre 4.9.332-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nouveau_gem.c
1 /*
2  * Copyright (C) 2008 Ben Skeggs.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include "nouveau_drv.h"
28 #include "nouveau_dma.h"
29 #include "nouveau_fence.h"
30 #include "nouveau_abi16.h"
31
32 #include "nouveau_ttm.h"
33 #include "nouveau_gem.h"
34
35 void
36 nouveau_gem_object_del(struct drm_gem_object *gem)
37 {
38         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
39         struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
40         struct ttm_buffer_object *bo = &nvbo->bo;
41         struct device *dev = drm->dev->dev;
42         int ret;
43
44         ret = pm_runtime_get_sync(dev);
45         if (WARN_ON(ret < 0 && ret != -EACCES)) {
46                 pm_runtime_put_autosuspend(dev);
47                 return;
48         }
49
50         if (gem->import_attach)
51                 drm_prime_gem_destroy(gem, nvbo->bo.sg);
52
53         drm_gem_object_release(gem);
54
55         /* reset filp so nouveau_bo_del_ttm() can test for it */
56         gem->filp = NULL;
57         ttm_bo_unref(&bo);
58
59         pm_runtime_mark_last_busy(dev);
60         pm_runtime_put_autosuspend(dev);
61 }
62
63 int
64 nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
65 {
66         struct nouveau_cli *cli = nouveau_cli(file_priv);
67         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
68         struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
69         struct nvkm_vma *vma;
70         struct device *dev = drm->dev->dev;
71         int ret;
72
73         if (!cli->vm)
74                 return 0;
75
76         ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
77         if (ret)
78                 return ret;
79
80         vma = nouveau_bo_vma_find(nvbo, cli->vm);
81         if (!vma) {
82                 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
83                 if (!vma) {
84                         ret = -ENOMEM;
85                         goto out;
86                 }
87
88                 ret = pm_runtime_get_sync(dev);
89                 if (ret < 0 && ret != -EACCES) {
90                         kfree(vma);
91                         goto out;
92                 }
93
94                 ret = nouveau_bo_vma_add(nvbo, cli->vm, vma);
95                 if (ret)
96                         kfree(vma);
97
98                 pm_runtime_mark_last_busy(dev);
99                 pm_runtime_put_autosuspend(dev);
100         } else {
101                 vma->refcount++;
102         }
103
104 out:
105         ttm_bo_unreserve(&nvbo->bo);
106         return ret;
107 }
108
109 static void
110 nouveau_gem_object_delete(void *data)
111 {
112         struct nvkm_vma *vma = data;
113         nvkm_vm_unmap(vma);
114         nvkm_vm_put(vma);
115         kfree(vma);
116 }
117
118 static void
119 nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nvkm_vma *vma)
120 {
121         const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
122         struct reservation_object *resv = nvbo->bo.resv;
123         struct reservation_object_list *fobj;
124         struct fence *fence = NULL;
125
126         fobj = reservation_object_get_list(resv);
127
128         list_del(&vma->head);
129
130         if (fobj && fobj->shared_count > 1)
131                 ttm_bo_wait(&nvbo->bo, false, false);
132         else if (fobj && fobj->shared_count == 1)
133                 fence = rcu_dereference_protected(fobj->shared[0],
134                                                 reservation_object_held(resv));
135         else
136                 fence = reservation_object_get_excl(nvbo->bo.resv);
137
138         if (fence && mapped) {
139                 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
140         } else {
141                 if (mapped)
142                         nvkm_vm_unmap(vma);
143                 nvkm_vm_put(vma);
144                 kfree(vma);
145         }
146 }
147
148 void
149 nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
150 {
151         struct nouveau_cli *cli = nouveau_cli(file_priv);
152         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
153         struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
154         struct device *dev = drm->dev->dev;
155         struct nvkm_vma *vma;
156         int ret;
157
158         if (!cli->vm)
159                 return;
160
161         ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
162         if (ret)
163                 return;
164
165         vma = nouveau_bo_vma_find(nvbo, cli->vm);
166         if (vma) {
167                 if (--vma->refcount == 0) {
168                         ret = pm_runtime_get_sync(dev);
169                         if (!WARN_ON(ret < 0 && ret != -EACCES)) {
170                                 nouveau_gem_object_unmap(nvbo, vma);
171                                 pm_runtime_mark_last_busy(dev);
172                                 pm_runtime_put_autosuspend(dev);
173                         }
174                 }
175         }
176         ttm_bo_unreserve(&nvbo->bo);
177 }
178
179 int
180 nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
181                 uint32_t tile_mode, uint32_t tile_flags,
182                 struct nouveau_bo **pnvbo)
183 {
184         struct nouveau_drm *drm = nouveau_drm(dev);
185         struct nouveau_bo *nvbo;
186         u32 flags = 0;
187         int ret;
188
189         if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
190                 flags |= TTM_PL_FLAG_VRAM;
191         if (domain & NOUVEAU_GEM_DOMAIN_GART)
192                 flags |= TTM_PL_FLAG_TT;
193         if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
194                 flags |= TTM_PL_FLAG_SYSTEM;
195
196         if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
197                 flags |= TTM_PL_FLAG_UNCACHED;
198
199         ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
200                              tile_flags, NULL, NULL, pnvbo);
201         if (ret)
202                 return ret;
203         nvbo = *pnvbo;
204
205         /* we restrict allowed domains on nv50+ to only the types
206          * that were requested at creation time.  not possibly on
207          * earlier chips without busting the ABI.
208          */
209         nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
210                               NOUVEAU_GEM_DOMAIN_GART;
211         if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
212                 nvbo->valid_domains &= domain;
213
214         /* Initialize the embedded gem-object. We return a single gem-reference
215          * to the caller, instead of a normal nouveau_bo ttm reference. */
216         ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
217         if (ret) {
218                 nouveau_bo_ref(NULL, pnvbo);
219                 return -ENOMEM;
220         }
221
222         nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
223         return 0;
224 }
225
226 static int
227 nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
228                  struct drm_nouveau_gem_info *rep)
229 {
230         struct nouveau_cli *cli = nouveau_cli(file_priv);
231         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
232         struct nvkm_vma *vma;
233
234         if (is_power_of_2(nvbo->valid_domains))
235                 rep->domain = nvbo->valid_domains;
236         else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
237                 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
238         else
239                 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
240         rep->offset = nvbo->bo.offset;
241         if (cli->vm) {
242                 vma = nouveau_bo_vma_find(nvbo, cli->vm);
243                 if (!vma)
244                         return -EINVAL;
245
246                 rep->offset = vma->offset;
247         }
248
249         rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
250         rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
251         rep->tile_mode = nvbo->tile_mode;
252         rep->tile_flags = nvbo->tile_flags;
253         return 0;
254 }
255
256 int
257 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
258                       struct drm_file *file_priv)
259 {
260         struct nouveau_drm *drm = nouveau_drm(dev);
261         struct nouveau_cli *cli = nouveau_cli(file_priv);
262         struct nvkm_fb *fb = nvxx_fb(&drm->device);
263         struct drm_nouveau_gem_new *req = data;
264         struct nouveau_bo *nvbo = NULL;
265         int ret = 0;
266
267         if (!nvkm_fb_memtype_valid(fb, req->info.tile_flags)) {
268                 NV_PRINTK(err, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
269                 return -EINVAL;
270         }
271
272         ret = nouveau_gem_new(dev, req->info.size, req->align,
273                               req->info.domain, req->info.tile_mode,
274                               req->info.tile_flags, &nvbo);
275         if (ret)
276                 return ret;
277
278         ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
279         if (ret == 0) {
280                 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
281                 if (ret)
282                         drm_gem_handle_delete(file_priv, req->info.handle);
283         }
284
285         /* drop reference from allocate - handle holds it now */
286         drm_gem_object_unreference_unlocked(&nvbo->gem);
287         return ret;
288 }
289
290 static int
291 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
292                        uint32_t write_domains, uint32_t valid_domains)
293 {
294         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
295         struct ttm_buffer_object *bo = &nvbo->bo;
296         uint32_t domains = valid_domains & nvbo->valid_domains &
297                 (write_domains ? write_domains : read_domains);
298         uint32_t pref_flags = 0, valid_flags = 0;
299
300         if (!domains)
301                 return -EINVAL;
302
303         if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
304                 valid_flags |= TTM_PL_FLAG_VRAM;
305
306         if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
307                 valid_flags |= TTM_PL_FLAG_TT;
308
309         if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
310             bo->mem.mem_type == TTM_PL_VRAM)
311                 pref_flags |= TTM_PL_FLAG_VRAM;
312
313         else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
314                  bo->mem.mem_type == TTM_PL_TT)
315                 pref_flags |= TTM_PL_FLAG_TT;
316
317         else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
318                 pref_flags |= TTM_PL_FLAG_VRAM;
319
320         else
321                 pref_flags |= TTM_PL_FLAG_TT;
322
323         nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
324
325         return 0;
326 }
327
328 struct validate_op {
329         struct list_head list;
330         struct ww_acquire_ctx ticket;
331 };
332
333 static void
334 validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
335                         struct drm_nouveau_gem_pushbuf_bo *pbbo)
336 {
337         struct nouveau_bo *nvbo;
338         struct drm_nouveau_gem_pushbuf_bo *b;
339
340         while (!list_empty(&op->list)) {
341                 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
342                 b = &pbbo[nvbo->pbbo_index];
343
344                 if (likely(fence))
345                         nouveau_bo_fence(nvbo, fence, !!b->write_domains);
346
347                 if (unlikely(nvbo->validate_mapped)) {
348                         ttm_bo_kunmap(&nvbo->kmap);
349                         nvbo->validate_mapped = false;
350                 }
351
352                 list_del(&nvbo->entry);
353                 nvbo->reserved_by = NULL;
354                 ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket);
355                 drm_gem_object_unreference_unlocked(&nvbo->gem);
356         }
357 }
358
359 static void
360 validate_fini(struct validate_op *op, struct nouveau_fence *fence,
361               struct drm_nouveau_gem_pushbuf_bo *pbbo)
362 {
363         validate_fini_no_ticket(op, fence, pbbo);
364         ww_acquire_fini(&op->ticket);
365 }
366
367 static int
368 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
369               struct drm_nouveau_gem_pushbuf_bo *pbbo,
370               int nr_buffers, struct validate_op *op)
371 {
372         struct nouveau_cli *cli = nouveau_cli(file_priv);
373         int trycnt = 0;
374         int ret = -EINVAL, i;
375         struct nouveau_bo *res_bo = NULL;
376         LIST_HEAD(gart_list);
377         LIST_HEAD(vram_list);
378         LIST_HEAD(both_list);
379
380         ww_acquire_init(&op->ticket, &reservation_ww_class);
381 retry:
382         if (++trycnt > 100000) {
383                 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
384                 return -EINVAL;
385         }
386
387         for (i = 0; i < nr_buffers; i++) {
388                 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
389                 struct drm_gem_object *gem;
390                 struct nouveau_bo *nvbo;
391
392                 gem = drm_gem_object_lookup(file_priv, b->handle);
393                 if (!gem) {
394                         NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
395                         ret = -ENOENT;
396                         break;
397                 }
398                 nvbo = nouveau_gem_object(gem);
399                 if (nvbo == res_bo) {
400                         res_bo = NULL;
401                         drm_gem_object_unreference_unlocked(gem);
402                         continue;
403                 }
404
405                 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
406                         NV_PRINTK(err, cli, "multiple instances of buffer %d on "
407                                       "validation list\n", b->handle);
408                         drm_gem_object_unreference_unlocked(gem);
409                         ret = -EINVAL;
410                         break;
411                 }
412
413                 ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
414                 if (ret) {
415                         list_splice_tail_init(&vram_list, &op->list);
416                         list_splice_tail_init(&gart_list, &op->list);
417                         list_splice_tail_init(&both_list, &op->list);
418                         validate_fini_no_ticket(op, NULL, NULL);
419                         if (unlikely(ret == -EDEADLK)) {
420                                 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
421                                                               &op->ticket);
422                                 if (!ret)
423                                         res_bo = nvbo;
424                         }
425                         if (unlikely(ret)) {
426                                 if (ret != -ERESTARTSYS)
427                                         NV_PRINTK(err, cli, "fail reserve\n");
428                                 break;
429                         }
430                 }
431
432                 b->user_priv = (uint64_t)(unsigned long)nvbo;
433                 nvbo->reserved_by = file_priv;
434                 nvbo->pbbo_index = i;
435                 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
436                     (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
437                         list_add_tail(&nvbo->entry, &both_list);
438                 else
439                 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
440                         list_add_tail(&nvbo->entry, &vram_list);
441                 else
442                 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
443                         list_add_tail(&nvbo->entry, &gart_list);
444                 else {
445                         NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
446                                  b->valid_domains);
447                         list_add_tail(&nvbo->entry, &both_list);
448                         ret = -EINVAL;
449                         break;
450                 }
451                 if (nvbo == res_bo)
452                         goto retry;
453         }
454
455         ww_acquire_done(&op->ticket);
456         list_splice_tail(&vram_list, &op->list);
457         list_splice_tail(&gart_list, &op->list);
458         list_splice_tail(&both_list, &op->list);
459         if (ret)
460                 validate_fini(op, NULL, NULL);
461         return ret;
462
463 }
464
465 static int
466 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
467               struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
468               uint64_t user_pbbo_ptr)
469 {
470         struct nouveau_drm *drm = chan->drm;
471         struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
472                                 (void __force __user *)(uintptr_t)user_pbbo_ptr;
473         struct nouveau_bo *nvbo;
474         int ret, relocs = 0;
475
476         list_for_each_entry(nvbo, list, entry) {
477                 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
478
479                 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
480                                              b->write_domains,
481                                              b->valid_domains);
482                 if (unlikely(ret)) {
483                         NV_PRINTK(err, cli, "fail set_domain\n");
484                         return ret;
485                 }
486
487                 ret = nouveau_bo_validate(nvbo, true, false);
488                 if (unlikely(ret)) {
489                         if (ret != -ERESTARTSYS)
490                                 NV_PRINTK(err, cli, "fail ttm_validate\n");
491                         return ret;
492                 }
493
494                 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
495                 if (unlikely(ret)) {
496                         if (ret != -ERESTARTSYS)
497                                 NV_PRINTK(err, cli, "fail post-validate sync\n");
498                         return ret;
499                 }
500
501                 if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
502                         if (nvbo->bo.offset == b->presumed.offset &&
503                             ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
504                               b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
505                              (nvbo->bo.mem.mem_type == TTM_PL_TT &&
506                               b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
507                                 continue;
508
509                         if (nvbo->bo.mem.mem_type == TTM_PL_TT)
510                                 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
511                         else
512                                 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
513                         b->presumed.offset = nvbo->bo.offset;
514                         b->presumed.valid = 0;
515                         relocs++;
516
517                         if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
518                                              &b->presumed, sizeof(b->presumed)))
519                                 return -EFAULT;
520                 }
521         }
522
523         return relocs;
524 }
525
526 static int
527 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
528                              struct drm_file *file_priv,
529                              struct drm_nouveau_gem_pushbuf_bo *pbbo,
530                              uint64_t user_buffers, int nr_buffers,
531                              struct validate_op *op, int *apply_relocs)
532 {
533         struct nouveau_cli *cli = nouveau_cli(file_priv);
534         int ret;
535
536         INIT_LIST_HEAD(&op->list);
537
538         if (nr_buffers == 0)
539                 return 0;
540
541         ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
542         if (unlikely(ret)) {
543                 if (ret != -ERESTARTSYS)
544                         NV_PRINTK(err, cli, "validate_init\n");
545                 return ret;
546         }
547
548         ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
549         if (unlikely(ret < 0)) {
550                 if (ret != -ERESTARTSYS)
551                         NV_PRINTK(err, cli, "validating bo list\n");
552                 validate_fini(op, NULL, NULL);
553                 return ret;
554         }
555         *apply_relocs = ret;
556         return 0;
557 }
558
559 static inline void
560 u_free(void *addr)
561 {
562         kvfree(addr);
563 }
564
565 static inline void *
566 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
567 {
568         void *mem;
569         void __user *userptr = (void __force __user *)(uintptr_t)user;
570
571         size *= nmemb;
572
573         mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
574         if (!mem)
575                 mem = vmalloc(size);
576         if (!mem)
577                 return ERR_PTR(-ENOMEM);
578
579         if (copy_from_user(mem, userptr, size)) {
580                 u_free(mem);
581                 return ERR_PTR(-EFAULT);
582         }
583
584         return mem;
585 }
586
587 static int
588 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
589                                 struct drm_nouveau_gem_pushbuf *req,
590                                 struct drm_nouveau_gem_pushbuf_bo *bo)
591 {
592         struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
593         int ret = 0;
594         unsigned i;
595
596         reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
597         if (IS_ERR(reloc))
598                 return PTR_ERR(reloc);
599
600         for (i = 0; i < req->nr_relocs; i++) {
601                 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
602                 struct drm_nouveau_gem_pushbuf_bo *b;
603                 struct nouveau_bo *nvbo;
604                 uint32_t data;
605
606                 if (unlikely(r->bo_index >= req->nr_buffers)) {
607                         NV_PRINTK(err, cli, "reloc bo index invalid\n");
608                         ret = -EINVAL;
609                         break;
610                 }
611
612                 b = &bo[r->bo_index];
613                 if (b->presumed.valid)
614                         continue;
615
616                 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
617                         NV_PRINTK(err, cli, "reloc container bo index invalid\n");
618                         ret = -EINVAL;
619                         break;
620                 }
621                 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
622
623                 if (unlikely(r->reloc_bo_offset + 4 >
624                              nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
625                         NV_PRINTK(err, cli, "reloc outside of bo\n");
626                         ret = -EINVAL;
627                         break;
628                 }
629
630                 if (!nvbo->kmap.virtual) {
631                         ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
632                                           &nvbo->kmap);
633                         if (ret) {
634                                 NV_PRINTK(err, cli, "failed kmap for reloc\n");
635                                 break;
636                         }
637                         nvbo->validate_mapped = true;
638                 }
639
640                 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
641                         data = b->presumed.offset + r->data;
642                 else
643                 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
644                         data = (b->presumed.offset + r->data) >> 32;
645                 else
646                         data = r->data;
647
648                 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
649                         if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
650                                 data |= r->tor;
651                         else
652                                 data |= r->vor;
653                 }
654
655                 ret = ttm_bo_wait(&nvbo->bo, false, false);
656                 if (ret) {
657                         NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
658                         break;
659                 }
660
661                 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
662         }
663
664         u_free(reloc);
665         return ret;
666 }
667
668 int
669 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
670                           struct drm_file *file_priv)
671 {
672         struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
673         struct nouveau_cli *cli = nouveau_cli(file_priv);
674         struct nouveau_abi16_chan *temp;
675         struct nouveau_drm *drm = nouveau_drm(dev);
676         struct drm_nouveau_gem_pushbuf *req = data;
677         struct drm_nouveau_gem_pushbuf_push *push;
678         struct drm_nouveau_gem_pushbuf_bo *bo;
679         struct nouveau_channel *chan = NULL;
680         struct validate_op op;
681         struct nouveau_fence *fence = NULL;
682         int i, j, ret = 0, do_reloc = 0;
683
684         if (unlikely(!abi16))
685                 return -ENOMEM;
686
687         list_for_each_entry(temp, &abi16->channels, head) {
688                 if (temp->chan->chid == req->channel) {
689                         chan = temp->chan;
690                         break;
691                 }
692         }
693
694         if (!chan)
695                 return nouveau_abi16_put(abi16, -ENOENT);
696
697         req->vram_available = drm->gem.vram_available;
698         req->gart_available = drm->gem.gart_available;
699         if (unlikely(req->nr_push == 0))
700                 goto out_next;
701
702         if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
703                 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
704                          req->nr_push, NOUVEAU_GEM_MAX_PUSH);
705                 return nouveau_abi16_put(abi16, -EINVAL);
706         }
707
708         if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
709                 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
710                          req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
711                 return nouveau_abi16_put(abi16, -EINVAL);
712         }
713
714         if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
715                 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
716                          req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
717                 return nouveau_abi16_put(abi16, -EINVAL);
718         }
719
720         push = u_memcpya(req->push, req->nr_push, sizeof(*push));
721         if (IS_ERR(push))
722                 return nouveau_abi16_put(abi16, PTR_ERR(push));
723
724         bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
725         if (IS_ERR(bo)) {
726                 u_free(push);
727                 return nouveau_abi16_put(abi16, PTR_ERR(bo));
728         }
729
730         /* Ensure all push buffers are on validate list */
731         for (i = 0; i < req->nr_push; i++) {
732                 if (push[i].bo_index >= req->nr_buffers) {
733                         NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
734                         ret = -EINVAL;
735                         goto out_prevalid;
736                 }
737         }
738
739         /* Validate buffer list */
740         ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
741                                            req->nr_buffers, &op, &do_reloc);
742         if (ret) {
743                 if (ret != -ERESTARTSYS)
744                         NV_PRINTK(err, cli, "validate: %d\n", ret);
745                 goto out_prevalid;
746         }
747
748         /* Apply any relocations that are required */
749         if (do_reloc) {
750                 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
751                 if (ret) {
752                         NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
753                         goto out;
754                 }
755         }
756
757         if (chan->dma.ib_max) {
758                 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
759                 if (ret) {
760                         NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
761                         goto out;
762                 }
763
764                 for (i = 0; i < req->nr_push; i++) {
765                         struct nouveau_bo *nvbo = (void *)(unsigned long)
766                                 bo[push[i].bo_index].user_priv;
767
768                         nv50_dma_push(chan, nvbo, push[i].offset,
769                                       push[i].length);
770                 }
771         } else
772         if (drm->device.info.chipset >= 0x25) {
773                 ret = RING_SPACE(chan, req->nr_push * 2);
774                 if (ret) {
775                         NV_PRINTK(err, cli, "cal_space: %d\n", ret);
776                         goto out;
777                 }
778
779                 for (i = 0; i < req->nr_push; i++) {
780                         struct nouveau_bo *nvbo = (void *)(unsigned long)
781                                 bo[push[i].bo_index].user_priv;
782
783                         OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
784                         OUT_RING(chan, 0);
785                 }
786         } else {
787                 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
788                 if (ret) {
789                         NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
790                         goto out;
791                 }
792
793                 for (i = 0; i < req->nr_push; i++) {
794                         struct nouveau_bo *nvbo = (void *)(unsigned long)
795                                 bo[push[i].bo_index].user_priv;
796                         uint32_t cmd;
797
798                         cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
799                         cmd |= 0x20000000;
800                         if (unlikely(cmd != req->suffix0)) {
801                                 if (!nvbo->kmap.virtual) {
802                                         ret = ttm_bo_kmap(&nvbo->bo, 0,
803                                                           nvbo->bo.mem.
804                                                           num_pages,
805                                                           &nvbo->kmap);
806                                         if (ret) {
807                                                 WIND_RING(chan);
808                                                 goto out;
809                                         }
810                                         nvbo->validate_mapped = true;
811                                 }
812
813                                 nouveau_bo_wr32(nvbo, (push[i].offset +
814                                                 push[i].length - 8) / 4, cmd);
815                         }
816
817                         OUT_RING(chan, 0x20000000 |
818                                       (nvbo->bo.offset + push[i].offset));
819                         OUT_RING(chan, 0);
820                         for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
821                                 OUT_RING(chan, 0);
822                 }
823         }
824
825         ret = nouveau_fence_new(chan, false, &fence);
826         if (ret) {
827                 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
828                 WIND_RING(chan);
829                 goto out;
830         }
831
832 out:
833         validate_fini(&op, fence, bo);
834         nouveau_fence_unref(&fence);
835
836 out_prevalid:
837         u_free(bo);
838         u_free(push);
839
840 out_next:
841         if (chan->dma.ib_max) {
842                 req->suffix0 = 0x00000000;
843                 req->suffix1 = 0x00000000;
844         } else
845         if (drm->device.info.chipset >= 0x25) {
846                 req->suffix0 = 0x00020000;
847                 req->suffix1 = 0x00000000;
848         } else {
849                 req->suffix0 = 0x20000000 |
850                               (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
851                 req->suffix1 = 0x00000000;
852         }
853
854         return nouveau_abi16_put(abi16, ret);
855 }
856
857 int
858 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
859                            struct drm_file *file_priv)
860 {
861         struct drm_nouveau_gem_cpu_prep *req = data;
862         struct drm_gem_object *gem;
863         struct nouveau_bo *nvbo;
864         bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
865         bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
866         int ret;
867
868         gem = drm_gem_object_lookup(file_priv, req->handle);
869         if (!gem)
870                 return -ENOENT;
871         nvbo = nouveau_gem_object(gem);
872
873         if (no_wait)
874                 ret = reservation_object_test_signaled_rcu(nvbo->bo.resv, write) ? 0 : -EBUSY;
875         else {
876                 long lret;
877
878                 lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true, 30 * HZ);
879                 if (!lret)
880                         ret = -EBUSY;
881                 else if (lret > 0)
882                         ret = 0;
883                 else
884                         ret = lret;
885         }
886         nouveau_bo_sync_for_cpu(nvbo);
887         drm_gem_object_unreference_unlocked(gem);
888
889         return ret;
890 }
891
892 int
893 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
894                            struct drm_file *file_priv)
895 {
896         struct drm_nouveau_gem_cpu_fini *req = data;
897         struct drm_gem_object *gem;
898         struct nouveau_bo *nvbo;
899
900         gem = drm_gem_object_lookup(file_priv, req->handle);
901         if (!gem)
902                 return -ENOENT;
903         nvbo = nouveau_gem_object(gem);
904
905         nouveau_bo_sync_for_device(nvbo);
906         drm_gem_object_unreference_unlocked(gem);
907         return 0;
908 }
909
910 int
911 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
912                        struct drm_file *file_priv)
913 {
914         struct drm_nouveau_gem_info *req = data;
915         struct drm_gem_object *gem;
916         int ret;
917
918         gem = drm_gem_object_lookup(file_priv, req->handle);
919         if (!gem)
920                 return -ENOENT;
921
922         ret = nouveau_gem_info(file_priv, gem, req);
923         drm_gem_object_unreference_unlocked(gem);
924         return ret;
925 }
926