GNU Linux-libre 4.4.285-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_drm.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, 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, true, 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, 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         struct drm_device *dev = chan->drm->dev;
374         int trycnt = 0;
375         int ret = -EINVAL, i;
376         struct nouveau_bo *res_bo = NULL;
377         LIST_HEAD(gart_list);
378         LIST_HEAD(vram_list);
379         LIST_HEAD(both_list);
380
381         ww_acquire_init(&op->ticket, &reservation_ww_class);
382 retry:
383         if (++trycnt > 100000) {
384                 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
385                 return -EINVAL;
386         }
387
388         for (i = 0; i < nr_buffers; i++) {
389                 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
390                 struct drm_gem_object *gem;
391                 struct nouveau_bo *nvbo;
392
393                 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
394                 if (!gem) {
395                         NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
396                         ret = -ENOENT;
397                         break;
398                 }
399                 nvbo = nouveau_gem_object(gem);
400                 if (nvbo == res_bo) {
401                         res_bo = NULL;
402                         drm_gem_object_unreference_unlocked(gem);
403                         continue;
404                 }
405
406                 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
407                         NV_PRINTK(err, cli, "multiple instances of buffer %d on "
408                                       "validation list\n", b->handle);
409                         drm_gem_object_unreference_unlocked(gem);
410                         ret = -EINVAL;
411                         break;
412                 }
413
414                 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
415                 if (ret) {
416                         list_splice_tail_init(&vram_list, &op->list);
417                         list_splice_tail_init(&gart_list, &op->list);
418                         list_splice_tail_init(&both_list, &op->list);
419                         validate_fini_no_ticket(op, NULL, NULL);
420                         if (unlikely(ret == -EDEADLK)) {
421                                 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
422                                                               &op->ticket);
423                                 if (!ret)
424                                         res_bo = nvbo;
425                         }
426                         if (unlikely(ret)) {
427                                 if (ret != -ERESTARTSYS)
428                                         NV_PRINTK(err, cli, "fail reserve\n");
429                                 break;
430                         }
431                 }
432
433                 b->user_priv = (uint64_t)(unsigned long)nvbo;
434                 nvbo->reserved_by = file_priv;
435                 nvbo->pbbo_index = i;
436                 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
437                     (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
438                         list_add_tail(&nvbo->entry, &both_list);
439                 else
440                 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
441                         list_add_tail(&nvbo->entry, &vram_list);
442                 else
443                 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
444                         list_add_tail(&nvbo->entry, &gart_list);
445                 else {
446                         NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
447                                  b->valid_domains);
448                         list_add_tail(&nvbo->entry, &both_list);
449                         ret = -EINVAL;
450                         break;
451                 }
452                 if (nvbo == res_bo)
453                         goto retry;
454         }
455
456         ww_acquire_done(&op->ticket);
457         list_splice_tail(&vram_list, &op->list);
458         list_splice_tail(&gart_list, &op->list);
459         list_splice_tail(&both_list, &op->list);
460         if (ret)
461                 validate_fini(op, NULL, NULL);
462         return ret;
463
464 }
465
466 static int
467 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
468               struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
469               uint64_t user_pbbo_ptr)
470 {
471         struct nouveau_drm *drm = chan->drm;
472         struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
473                                 (void __force __user *)(uintptr_t)user_pbbo_ptr;
474         struct nouveau_bo *nvbo;
475         int ret, relocs = 0;
476
477         list_for_each_entry(nvbo, list, entry) {
478                 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
479
480                 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
481                                              b->write_domains,
482                                              b->valid_domains);
483                 if (unlikely(ret)) {
484                         NV_PRINTK(err, cli, "fail set_domain\n");
485                         return ret;
486                 }
487
488                 ret = nouveau_bo_validate(nvbo, true, false);
489                 if (unlikely(ret)) {
490                         if (ret != -ERESTARTSYS)
491                                 NV_PRINTK(err, cli, "fail ttm_validate\n");
492                         return ret;
493                 }
494
495                 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
496                 if (unlikely(ret)) {
497                         if (ret != -ERESTARTSYS)
498                                 NV_PRINTK(err, cli, "fail post-validate sync\n");
499                         return ret;
500                 }
501
502                 if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
503                         if (nvbo->bo.offset == b->presumed.offset &&
504                             ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
505                               b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
506                              (nvbo->bo.mem.mem_type == TTM_PL_TT &&
507                               b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
508                                 continue;
509
510                         if (nvbo->bo.mem.mem_type == TTM_PL_TT)
511                                 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
512                         else
513                                 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
514                         b->presumed.offset = nvbo->bo.offset;
515                         b->presumed.valid = 0;
516                         relocs++;
517
518                         if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
519                                              &b->presumed, sizeof(b->presumed)))
520                                 return -EFAULT;
521                 }
522         }
523
524         return relocs;
525 }
526
527 static int
528 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
529                              struct drm_file *file_priv,
530                              struct drm_nouveau_gem_pushbuf_bo *pbbo,
531                              uint64_t user_buffers, int nr_buffers,
532                              struct validate_op *op, int *apply_relocs)
533 {
534         struct nouveau_cli *cli = nouveau_cli(file_priv);
535         int ret;
536
537         INIT_LIST_HEAD(&op->list);
538
539         if (nr_buffers == 0)
540                 return 0;
541
542         ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
543         if (unlikely(ret)) {
544                 if (ret != -ERESTARTSYS)
545                         NV_PRINTK(err, cli, "validate_init\n");
546                 return ret;
547         }
548
549         ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
550         if (unlikely(ret < 0)) {
551                 if (ret != -ERESTARTSYS)
552                         NV_PRINTK(err, cli, "validating bo list\n");
553                 validate_fini(op, NULL, NULL);
554                 return ret;
555         }
556         *apply_relocs = ret;
557         return 0;
558 }
559
560 static inline void
561 u_free(void *addr)
562 {
563         kvfree(addr);
564 }
565
566 static inline void *
567 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
568 {
569         void *mem;
570         void __user *userptr = (void __force __user *)(uintptr_t)user;
571
572         size *= nmemb;
573
574         mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
575         if (!mem)
576                 mem = vmalloc(size);
577         if (!mem)
578                 return ERR_PTR(-ENOMEM);
579
580         if (copy_from_user(mem, userptr, size)) {
581                 u_free(mem);
582                 return ERR_PTR(-EFAULT);
583         }
584
585         return mem;
586 }
587
588 static int
589 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
590                                 struct drm_nouveau_gem_pushbuf *req,
591                                 struct drm_nouveau_gem_pushbuf_bo *bo)
592 {
593         struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
594         int ret = 0;
595         unsigned i;
596
597         reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
598         if (IS_ERR(reloc))
599                 return PTR_ERR(reloc);
600
601         for (i = 0; i < req->nr_relocs; i++) {
602                 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
603                 struct drm_nouveau_gem_pushbuf_bo *b;
604                 struct nouveau_bo *nvbo;
605                 uint32_t data;
606
607                 if (unlikely(r->bo_index >= req->nr_buffers)) {
608                         NV_PRINTK(err, cli, "reloc bo index invalid\n");
609                         ret = -EINVAL;
610                         break;
611                 }
612
613                 b = &bo[r->bo_index];
614                 if (b->presumed.valid)
615                         continue;
616
617                 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
618                         NV_PRINTK(err, cli, "reloc container bo index invalid\n");
619                         ret = -EINVAL;
620                         break;
621                 }
622                 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
623
624                 if (unlikely(r->reloc_bo_offset + 4 >
625                              nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
626                         NV_PRINTK(err, cli, "reloc outside of bo\n");
627                         ret = -EINVAL;
628                         break;
629                 }
630
631                 if (!nvbo->kmap.virtual) {
632                         ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
633                                           &nvbo->kmap);
634                         if (ret) {
635                                 NV_PRINTK(err, cli, "failed kmap for reloc\n");
636                                 break;
637                         }
638                         nvbo->validate_mapped = true;
639                 }
640
641                 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
642                         data = b->presumed.offset + r->data;
643                 else
644                 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
645                         data = (b->presumed.offset + r->data) >> 32;
646                 else
647                         data = r->data;
648
649                 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
650                         if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
651                                 data |= r->tor;
652                         else
653                                 data |= r->vor;
654                 }
655
656                 ret = ttm_bo_wait(&nvbo->bo, true, false, false);
657                 if (ret) {
658                         NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
659                         break;
660                 }
661
662                 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
663         }
664
665         u_free(reloc);
666         return ret;
667 }
668
669 int
670 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
671                           struct drm_file *file_priv)
672 {
673         struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
674         struct nouveau_cli *cli = nouveau_cli(file_priv);
675         struct nouveau_abi16_chan *temp;
676         struct nouveau_drm *drm = nouveau_drm(dev);
677         struct drm_nouveau_gem_pushbuf *req = data;
678         struct drm_nouveau_gem_pushbuf_push *push;
679         struct drm_nouveau_gem_pushbuf_bo *bo;
680         struct nouveau_channel *chan = NULL;
681         struct validate_op op;
682         struct nouveau_fence *fence = NULL;
683         int i, j, ret = 0, do_reloc = 0;
684
685         if (unlikely(!abi16))
686                 return -ENOMEM;
687
688         list_for_each_entry(temp, &abi16->channels, head) {
689                 if (temp->chan->chid == req->channel) {
690                         chan = temp->chan;
691                         break;
692                 }
693         }
694
695         if (!chan)
696                 return nouveau_abi16_put(abi16, -ENOENT);
697
698         req->vram_available = drm->gem.vram_available;
699         req->gart_available = drm->gem.gart_available;
700         if (unlikely(req->nr_push == 0))
701                 goto out_next;
702
703         if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
704                 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
705                          req->nr_push, NOUVEAU_GEM_MAX_PUSH);
706                 return nouveau_abi16_put(abi16, -EINVAL);
707         }
708
709         if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
710                 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
711                          req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
712                 return nouveau_abi16_put(abi16, -EINVAL);
713         }
714
715         if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
716                 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
717                          req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
718                 return nouveau_abi16_put(abi16, -EINVAL);
719         }
720
721         push = u_memcpya(req->push, req->nr_push, sizeof(*push));
722         if (IS_ERR(push))
723                 return nouveau_abi16_put(abi16, PTR_ERR(push));
724
725         bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
726         if (IS_ERR(bo)) {
727                 u_free(push);
728                 return nouveau_abi16_put(abi16, PTR_ERR(bo));
729         }
730
731         /* Ensure all push buffers are on validate list */
732         for (i = 0; i < req->nr_push; i++) {
733                 if (push[i].bo_index >= req->nr_buffers) {
734                         NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
735                         ret = -EINVAL;
736                         goto out_prevalid;
737                 }
738         }
739
740         /* Validate buffer list */
741         ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
742                                            req->nr_buffers, &op, &do_reloc);
743         if (ret) {
744                 if (ret != -ERESTARTSYS)
745                         NV_PRINTK(err, cli, "validate: %d\n", ret);
746                 goto out_prevalid;
747         }
748
749         /* Apply any relocations that are required */
750         if (do_reloc) {
751                 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
752                 if (ret) {
753                         NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
754                         goto out;
755                 }
756         }
757
758         if (chan->dma.ib_max) {
759                 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
760                 if (ret) {
761                         NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
762                         goto out;
763                 }
764
765                 for (i = 0; i < req->nr_push; i++) {
766                         struct nouveau_bo *nvbo = (void *)(unsigned long)
767                                 bo[push[i].bo_index].user_priv;
768
769                         nv50_dma_push(chan, nvbo, push[i].offset,
770                                       push[i].length);
771                 }
772         } else
773         if (drm->device.info.chipset >= 0x25) {
774                 ret = RING_SPACE(chan, req->nr_push * 2);
775                 if (ret) {
776                         NV_PRINTK(err, cli, "cal_space: %d\n", ret);
777                         goto out;
778                 }
779
780                 for (i = 0; i < req->nr_push; i++) {
781                         struct nouveau_bo *nvbo = (void *)(unsigned long)
782                                 bo[push[i].bo_index].user_priv;
783
784                         OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
785                         OUT_RING(chan, 0);
786                 }
787         } else {
788                 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
789                 if (ret) {
790                         NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
791                         goto out;
792                 }
793
794                 for (i = 0; i < req->nr_push; i++) {
795                         struct nouveau_bo *nvbo = (void *)(unsigned long)
796                                 bo[push[i].bo_index].user_priv;
797                         uint32_t cmd;
798
799                         cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
800                         cmd |= 0x20000000;
801                         if (unlikely(cmd != req->suffix0)) {
802                                 if (!nvbo->kmap.virtual) {
803                                         ret = ttm_bo_kmap(&nvbo->bo, 0,
804                                                           nvbo->bo.mem.
805                                                           num_pages,
806                                                           &nvbo->kmap);
807                                         if (ret) {
808                                                 WIND_RING(chan);
809                                                 goto out;
810                                         }
811                                         nvbo->validate_mapped = true;
812                                 }
813
814                                 nouveau_bo_wr32(nvbo, (push[i].offset +
815                                                 push[i].length - 8) / 4, cmd);
816                         }
817
818                         OUT_RING(chan, 0x20000000 |
819                                       (nvbo->bo.offset + push[i].offset));
820                         OUT_RING(chan, 0);
821                         for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
822                                 OUT_RING(chan, 0);
823                 }
824         }
825
826         ret = nouveau_fence_new(chan, false, &fence);
827         if (ret) {
828                 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
829                 WIND_RING(chan);
830                 goto out;
831         }
832
833 out:
834         validate_fini(&op, fence, bo);
835         nouveau_fence_unref(&fence);
836
837 out_prevalid:
838         u_free(bo);
839         u_free(push);
840
841 out_next:
842         if (chan->dma.ib_max) {
843                 req->suffix0 = 0x00000000;
844                 req->suffix1 = 0x00000000;
845         } else
846         if (drm->device.info.chipset >= 0x25) {
847                 req->suffix0 = 0x00020000;
848                 req->suffix1 = 0x00000000;
849         } else {
850                 req->suffix0 = 0x20000000 |
851                               (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
852                 req->suffix1 = 0x00000000;
853         }
854
855         return nouveau_abi16_put(abi16, ret);
856 }
857
858 int
859 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
860                            struct drm_file *file_priv)
861 {
862         struct drm_nouveau_gem_cpu_prep *req = data;
863         struct drm_gem_object *gem;
864         struct nouveau_bo *nvbo;
865         bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
866         bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
867         int ret;
868
869         gem = drm_gem_object_lookup(dev, file_priv, req->handle);
870         if (!gem)
871                 return -ENOENT;
872         nvbo = nouveau_gem_object(gem);
873
874         if (no_wait)
875                 ret = reservation_object_test_signaled_rcu(nvbo->bo.resv, write) ? 0 : -EBUSY;
876         else {
877                 long lret;
878
879                 lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true, 30 * HZ);
880                 if (!lret)
881                         ret = -EBUSY;
882                 else if (lret > 0)
883                         ret = 0;
884                 else
885                         ret = lret;
886         }
887         nouveau_bo_sync_for_cpu(nvbo);
888         drm_gem_object_unreference_unlocked(gem);
889
890         return ret;
891 }
892
893 int
894 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
895                            struct drm_file *file_priv)
896 {
897         struct drm_nouveau_gem_cpu_fini *req = data;
898         struct drm_gem_object *gem;
899         struct nouveau_bo *nvbo;
900
901         gem = drm_gem_object_lookup(dev, file_priv, req->handle);
902         if (!gem)
903                 return -ENOENT;
904         nvbo = nouveau_gem_object(gem);
905
906         nouveau_bo_sync_for_device(nvbo);
907         drm_gem_object_unreference_unlocked(gem);
908         return 0;
909 }
910
911 int
912 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
913                        struct drm_file *file_priv)
914 {
915         struct drm_nouveau_gem_info *req = data;
916         struct drm_gem_object *gem;
917         int ret;
918
919         gem = drm_gem_object_lookup(dev, file_priv, req->handle);
920         if (!gem)
921                 return -ENOENT;
922
923         ret = nouveau_gem_info(file_priv, gem, req);
924         drm_gem_object_unreference_unlocked(gem);
925         return ret;
926 }
927