GNU Linux-libre 4.14.265-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 dma_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 nouveau_cli *cli, u64 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(cli->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(cli, 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->client.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(drm->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->client.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(cli, 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->client.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 = kvmalloc(size, GFP_KERNEL);
574         if (!mem)
575                 return ERR_PTR(-ENOMEM);
576
577         if (copy_from_user(mem, userptr, size)) {
578                 u_free(mem);
579                 return ERR_PTR(-EFAULT);
580         }
581
582         return mem;
583 }
584
585 static int
586 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
587                                 struct drm_nouveau_gem_pushbuf *req,
588                                 struct drm_nouveau_gem_pushbuf_bo *bo)
589 {
590         struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
591         int ret = 0;
592         unsigned i;
593
594         reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
595         if (IS_ERR(reloc))
596                 return PTR_ERR(reloc);
597
598         for (i = 0; i < req->nr_relocs; i++) {
599                 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
600                 struct drm_nouveau_gem_pushbuf_bo *b;
601                 struct nouveau_bo *nvbo;
602                 uint32_t data;
603
604                 if (unlikely(r->bo_index >= req->nr_buffers)) {
605                         NV_PRINTK(err, cli, "reloc bo index invalid\n");
606                         ret = -EINVAL;
607                         break;
608                 }
609
610                 b = &bo[r->bo_index];
611                 if (b->presumed.valid)
612                         continue;
613
614                 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
615                         NV_PRINTK(err, cli, "reloc container bo index invalid\n");
616                         ret = -EINVAL;
617                         break;
618                 }
619                 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
620
621                 if (unlikely(r->reloc_bo_offset + 4 >
622                              nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
623                         NV_PRINTK(err, cli, "reloc outside of bo\n");
624                         ret = -EINVAL;
625                         break;
626                 }
627
628                 if (!nvbo->kmap.virtual) {
629                         ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
630                                           &nvbo->kmap);
631                         if (ret) {
632                                 NV_PRINTK(err, cli, "failed kmap for reloc\n");
633                                 break;
634                         }
635                         nvbo->validate_mapped = true;
636                 }
637
638                 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
639                         data = b->presumed.offset + r->data;
640                 else
641                 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
642                         data = (b->presumed.offset + r->data) >> 32;
643                 else
644                         data = r->data;
645
646                 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
647                         if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
648                                 data |= r->tor;
649                         else
650                                 data |= r->vor;
651                 }
652
653                 ret = ttm_bo_wait(&nvbo->bo, false, false);
654                 if (ret) {
655                         NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
656                         break;
657                 }
658
659                 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
660         }
661
662         u_free(reloc);
663         return ret;
664 }
665
666 int
667 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
668                           struct drm_file *file_priv)
669 {
670         struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
671         struct nouveau_cli *cli = nouveau_cli(file_priv);
672         struct nouveau_abi16_chan *temp;
673         struct nouveau_drm *drm = nouveau_drm(dev);
674         struct drm_nouveau_gem_pushbuf *req = data;
675         struct drm_nouveau_gem_pushbuf_push *push;
676         struct drm_nouveau_gem_pushbuf_bo *bo;
677         struct nouveau_channel *chan = NULL;
678         struct validate_op op;
679         struct nouveau_fence *fence = NULL;
680         int i, j, ret = 0, do_reloc = 0;
681
682         if (unlikely(!abi16))
683                 return -ENOMEM;
684
685         list_for_each_entry(temp, &abi16->channels, head) {
686                 if (temp->chan->chid == req->channel) {
687                         chan = temp->chan;
688                         break;
689                 }
690         }
691
692         if (!chan)
693                 return nouveau_abi16_put(abi16, -ENOENT);
694
695         req->vram_available = drm->gem.vram_available;
696         req->gart_available = drm->gem.gart_available;
697         if (unlikely(req->nr_push == 0))
698                 goto out_next;
699
700         if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
701                 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
702                          req->nr_push, NOUVEAU_GEM_MAX_PUSH);
703                 return nouveau_abi16_put(abi16, -EINVAL);
704         }
705
706         if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
707                 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
708                          req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
709                 return nouveau_abi16_put(abi16, -EINVAL);
710         }
711
712         if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
713                 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
714                          req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
715                 return nouveau_abi16_put(abi16, -EINVAL);
716         }
717
718         push = u_memcpya(req->push, req->nr_push, sizeof(*push));
719         if (IS_ERR(push))
720                 return nouveau_abi16_put(abi16, PTR_ERR(push));
721
722         bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
723         if (IS_ERR(bo)) {
724                 u_free(push);
725                 return nouveau_abi16_put(abi16, PTR_ERR(bo));
726         }
727
728         /* Ensure all push buffers are on validate list */
729         for (i = 0; i < req->nr_push; i++) {
730                 if (push[i].bo_index >= req->nr_buffers) {
731                         NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
732                         ret = -EINVAL;
733                         goto out_prevalid;
734                 }
735         }
736
737         /* Validate buffer list */
738         ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
739                                            req->nr_buffers, &op, &do_reloc);
740         if (ret) {
741                 if (ret != -ERESTARTSYS)
742                         NV_PRINTK(err, cli, "validate: %d\n", ret);
743                 goto out_prevalid;
744         }
745
746         /* Apply any relocations that are required */
747         if (do_reloc) {
748                 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
749                 if (ret) {
750                         NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
751                         goto out;
752                 }
753         }
754
755         if (chan->dma.ib_max) {
756                 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
757                 if (ret) {
758                         NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
759                         goto out;
760                 }
761
762                 for (i = 0; i < req->nr_push; i++) {
763                         struct nouveau_bo *nvbo = (void *)(unsigned long)
764                                 bo[push[i].bo_index].user_priv;
765
766                         nv50_dma_push(chan, nvbo, push[i].offset,
767                                       push[i].length);
768                 }
769         } else
770         if (drm->client.device.info.chipset >= 0x25) {
771                 ret = RING_SPACE(chan, req->nr_push * 2);
772                 if (ret) {
773                         NV_PRINTK(err, cli, "cal_space: %d\n", ret);
774                         goto out;
775                 }
776
777                 for (i = 0; i < req->nr_push; i++) {
778                         struct nouveau_bo *nvbo = (void *)(unsigned long)
779                                 bo[push[i].bo_index].user_priv;
780
781                         OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
782                         OUT_RING(chan, 0);
783                 }
784         } else {
785                 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
786                 if (ret) {
787                         NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
788                         goto out;
789                 }
790
791                 for (i = 0; i < req->nr_push; i++) {
792                         struct nouveau_bo *nvbo = (void *)(unsigned long)
793                                 bo[push[i].bo_index].user_priv;
794                         uint32_t cmd;
795
796                         cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
797                         cmd |= 0x20000000;
798                         if (unlikely(cmd != req->suffix0)) {
799                                 if (!nvbo->kmap.virtual) {
800                                         ret = ttm_bo_kmap(&nvbo->bo, 0,
801                                                           nvbo->bo.mem.
802                                                           num_pages,
803                                                           &nvbo->kmap);
804                                         if (ret) {
805                                                 WIND_RING(chan);
806                                                 goto out;
807                                         }
808                                         nvbo->validate_mapped = true;
809                                 }
810
811                                 nouveau_bo_wr32(nvbo, (push[i].offset +
812                                                 push[i].length - 8) / 4, cmd);
813                         }
814
815                         OUT_RING(chan, 0x20000000 |
816                                       (nvbo->bo.offset + push[i].offset));
817                         OUT_RING(chan, 0);
818                         for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
819                                 OUT_RING(chan, 0);
820                 }
821         }
822
823         ret = nouveau_fence_new(chan, false, &fence);
824         if (ret) {
825                 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
826                 WIND_RING(chan);
827                 goto out;
828         }
829
830 out:
831         validate_fini(&op, fence, bo);
832         nouveau_fence_unref(&fence);
833
834 out_prevalid:
835         u_free(bo);
836         u_free(push);
837
838 out_next:
839         if (chan->dma.ib_max) {
840                 req->suffix0 = 0x00000000;
841                 req->suffix1 = 0x00000000;
842         } else
843         if (drm->client.device.info.chipset >= 0x25) {
844                 req->suffix0 = 0x00020000;
845                 req->suffix1 = 0x00000000;
846         } else {
847                 req->suffix0 = 0x20000000 |
848                               (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
849                 req->suffix1 = 0x00000000;
850         }
851
852         return nouveau_abi16_put(abi16, ret);
853 }
854
855 int
856 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
857                            struct drm_file *file_priv)
858 {
859         struct drm_nouveau_gem_cpu_prep *req = data;
860         struct drm_gem_object *gem;
861         struct nouveau_bo *nvbo;
862         bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
863         bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
864         long lret;
865         int ret;
866
867         gem = drm_gem_object_lookup(file_priv, req->handle);
868         if (!gem)
869                 return -ENOENT;
870         nvbo = nouveau_gem_object(gem);
871
872         lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
873                                                    no_wait ? 0 : 30 * HZ);
874         if (!lret)
875                 ret = -EBUSY;
876         else if (lret > 0)
877                 ret = 0;
878         else
879                 ret = lret;
880
881         nouveau_bo_sync_for_cpu(nvbo);
882         drm_gem_object_unreference_unlocked(gem);
883
884         return ret;
885 }
886
887 int
888 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
889                            struct drm_file *file_priv)
890 {
891         struct drm_nouveau_gem_cpu_fini *req = data;
892         struct drm_gem_object *gem;
893         struct nouveau_bo *nvbo;
894
895         gem = drm_gem_object_lookup(file_priv, req->handle);
896         if (!gem)
897                 return -ENOENT;
898         nvbo = nouveau_gem_object(gem);
899
900         nouveau_bo_sync_for_device(nvbo);
901         drm_gem_object_unreference_unlocked(gem);
902         return 0;
903 }
904
905 int
906 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
907                        struct drm_file *file_priv)
908 {
909         struct drm_nouveau_gem_info *req = data;
910         struct drm_gem_object *gem;
911         int ret;
912
913         gem = drm_gem_object_lookup(file_priv, req->handle);
914         if (!gem)
915                 return -ENOENT;
916
917         ret = nouveau_gem_info(file_priv, gem, req);
918         drm_gem_object_unreference_unlocked(gem);
919         return ret;
920 }
921