Merge branch 'v3.10/topic/misc' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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 <subdev/fb.h>
28
29 #include "nouveau_drm.h"
30 #include "nouveau_dma.h"
31 #include "nouveau_fence.h"
32 #include "nouveau_abi16.h"
33
34 #include "nouveau_ttm.h"
35 #include "nouveau_gem.h"
36
37 int
38 nouveau_gem_object_new(struct drm_gem_object *gem)
39 {
40         return 0;
41 }
42
43 void
44 nouveau_gem_object_del(struct drm_gem_object *gem)
45 {
46         struct nouveau_bo *nvbo = gem->driver_private;
47         struct ttm_buffer_object *bo = &nvbo->bo;
48
49         if (!nvbo)
50                 return;
51         nvbo->gem = NULL;
52
53         if (unlikely(nvbo->pin_refcnt)) {
54                 nvbo->pin_refcnt = 1;
55                 nouveau_bo_unpin(nvbo);
56         }
57
58         if (gem->import_attach)
59                 drm_prime_gem_destroy(gem, nvbo->bo.sg);
60
61         ttm_bo_unref(&bo);
62
63         drm_gem_object_release(gem);
64         kfree(gem);
65 }
66
67 int
68 nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
69 {
70         struct nouveau_cli *cli = nouveau_cli(file_priv);
71         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
72         struct nouveau_vma *vma;
73         int ret;
74
75         if (!cli->base.vm)
76                 return 0;
77
78         ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
79         if (ret)
80                 return ret;
81
82         vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
83         if (!vma) {
84                 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
85                 if (!vma) {
86                         ret = -ENOMEM;
87                         goto out;
88                 }
89
90                 ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
91                 if (ret) {
92                         kfree(vma);
93                         goto out;
94                 }
95         } else {
96                 vma->refcount++;
97         }
98
99 out:
100         ttm_bo_unreserve(&nvbo->bo);
101         return ret;
102 }
103
104 void
105 nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
106 {
107         struct nouveau_cli *cli = nouveau_cli(file_priv);
108         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
109         struct nouveau_vma *vma;
110         int ret;
111
112         if (!cli->base.vm)
113                 return;
114
115         ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
116         if (ret)
117                 return;
118
119         vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
120         if (vma) {
121                 if (--vma->refcount == 0) {
122                         nouveau_bo_vma_del(nvbo, vma);
123                         kfree(vma);
124                 }
125         }
126         ttm_bo_unreserve(&nvbo->bo);
127 }
128
129 int
130 nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
131                 uint32_t tile_mode, uint32_t tile_flags,
132                 struct nouveau_bo **pnvbo)
133 {
134         struct nouveau_drm *drm = nouveau_drm(dev);
135         struct nouveau_bo *nvbo;
136         u32 flags = 0;
137         int ret;
138
139         if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
140                 flags |= TTM_PL_FLAG_VRAM;
141         if (domain & NOUVEAU_GEM_DOMAIN_GART)
142                 flags |= TTM_PL_FLAG_TT;
143         if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
144                 flags |= TTM_PL_FLAG_SYSTEM;
145
146         ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
147                              tile_flags, NULL, pnvbo);
148         if (ret)
149                 return ret;
150         nvbo = *pnvbo;
151
152         /* we restrict allowed domains on nv50+ to only the types
153          * that were requested at creation time.  not possibly on
154          * earlier chips without busting the ABI.
155          */
156         nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
157                               NOUVEAU_GEM_DOMAIN_GART;
158         if (nv_device(drm->device)->card_type >= NV_50)
159                 nvbo->valid_domains &= domain;
160
161         nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
162         if (!nvbo->gem) {
163                 nouveau_bo_ref(NULL, pnvbo);
164                 return -ENOMEM;
165         }
166
167         nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
168         nvbo->gem->driver_private = nvbo;
169         return 0;
170 }
171
172 static int
173 nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
174                  struct drm_nouveau_gem_info *rep)
175 {
176         struct nouveau_cli *cli = nouveau_cli(file_priv);
177         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
178         struct nouveau_vma *vma;
179
180         if (nvbo->bo.mem.mem_type == TTM_PL_TT)
181                 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
182         else
183                 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
184
185         rep->offset = nvbo->bo.offset;
186         if (cli->base.vm) {
187                 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
188                 if (!vma)
189                         return -EINVAL;
190
191                 rep->offset = vma->offset;
192         }
193
194         rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
195         rep->map_handle = nvbo->bo.addr_space_offset;
196         rep->tile_mode = nvbo->tile_mode;
197         rep->tile_flags = nvbo->tile_flags;
198         return 0;
199 }
200
201 int
202 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
203                       struct drm_file *file_priv)
204 {
205         struct nouveau_drm *drm = nouveau_drm(dev);
206         struct nouveau_cli *cli = nouveau_cli(file_priv);
207         struct nouveau_fb *pfb = nouveau_fb(drm->device);
208         struct drm_nouveau_gem_new *req = data;
209         struct nouveau_bo *nvbo = NULL;
210         int ret = 0;
211
212         drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
213
214         if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
215                 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
216                 return -EINVAL;
217         }
218
219         ret = nouveau_gem_new(dev, req->info.size, req->align,
220                               req->info.domain, req->info.tile_mode,
221                               req->info.tile_flags, &nvbo);
222         if (ret)
223                 return ret;
224
225         ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
226         if (ret == 0) {
227                 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
228                 if (ret)
229                         drm_gem_handle_delete(file_priv, req->info.handle);
230         }
231
232         /* drop reference from allocate - handle holds it now */
233         drm_gem_object_unreference_unlocked(nvbo->gem);
234         return ret;
235 }
236
237 static int
238 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
239                        uint32_t write_domains, uint32_t valid_domains)
240 {
241         struct nouveau_bo *nvbo = gem->driver_private;
242         struct ttm_buffer_object *bo = &nvbo->bo;
243         uint32_t domains = valid_domains & nvbo->valid_domains &
244                 (write_domains ? write_domains : read_domains);
245         uint32_t pref_flags = 0, valid_flags = 0;
246
247         if (!domains)
248                 return -EINVAL;
249
250         if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
251                 valid_flags |= TTM_PL_FLAG_VRAM;
252
253         if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
254                 valid_flags |= TTM_PL_FLAG_TT;
255
256         if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
257             bo->mem.mem_type == TTM_PL_VRAM)
258                 pref_flags |= TTM_PL_FLAG_VRAM;
259
260         else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
261                  bo->mem.mem_type == TTM_PL_TT)
262                 pref_flags |= TTM_PL_FLAG_TT;
263
264         else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
265                 pref_flags |= TTM_PL_FLAG_VRAM;
266
267         else
268                 pref_flags |= TTM_PL_FLAG_TT;
269
270         nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
271
272         return 0;
273 }
274
275 struct validate_op {
276         struct list_head vram_list;
277         struct list_head gart_list;
278         struct list_head both_list;
279 };
280
281 static void
282 validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
283 {
284         struct list_head *entry, *tmp;
285         struct nouveau_bo *nvbo;
286
287         list_for_each_safe(entry, tmp, list) {
288                 nvbo = list_entry(entry, struct nouveau_bo, entry);
289
290                 if (likely(fence))
291                         nouveau_bo_fence(nvbo, fence);
292
293                 if (unlikely(nvbo->validate_mapped)) {
294                         ttm_bo_kunmap(&nvbo->kmap);
295                         nvbo->validate_mapped = false;
296                 }
297
298                 list_del(&nvbo->entry);
299                 nvbo->reserved_by = NULL;
300                 ttm_bo_unreserve(&nvbo->bo);
301                 drm_gem_object_unreference_unlocked(nvbo->gem);
302         }
303 }
304
305 static void
306 validate_fini(struct validate_op *op, struct nouveau_fence* fence)
307 {
308         validate_fini_list(&op->vram_list, fence);
309         validate_fini_list(&op->gart_list, fence);
310         validate_fini_list(&op->both_list, fence);
311 }
312
313 static int
314 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
315               struct drm_nouveau_gem_pushbuf_bo *pbbo,
316               int nr_buffers, struct validate_op *op)
317 {
318         struct nouveau_cli *cli = nouveau_cli(file_priv);
319         struct drm_device *dev = chan->drm->dev;
320         struct nouveau_drm *drm = nouveau_drm(dev);
321         uint32_t sequence;
322         int trycnt = 0;
323         int ret, i;
324         struct nouveau_bo *res_bo = NULL;
325
326         sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
327 retry:
328         if (++trycnt > 100000) {
329                 NV_ERROR(cli, "%s failed and gave up.\n", __func__);
330                 return -EINVAL;
331         }
332
333         for (i = 0; i < nr_buffers; i++) {
334                 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
335                 struct drm_gem_object *gem;
336                 struct nouveau_bo *nvbo;
337
338                 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
339                 if (!gem) {
340                         NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
341                         validate_fini(op, NULL);
342                         return -ENOENT;
343                 }
344                 nvbo = gem->driver_private;
345                 if (nvbo == res_bo) {
346                         res_bo = NULL;
347                         drm_gem_object_unreference_unlocked(gem);
348                         continue;
349                 }
350
351                 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
352                         NV_ERROR(cli, "multiple instances of buffer %d on "
353                                       "validation list\n", b->handle);
354                         drm_gem_object_unreference_unlocked(gem);
355                         validate_fini(op, NULL);
356                         return -EINVAL;
357                 }
358
359                 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
360                 if (ret) {
361                         validate_fini(op, NULL);
362                         if (unlikely(ret == -EAGAIN)) {
363                                 sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
364                                 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
365                                                               sequence);
366                                 if (!ret)
367                                         res_bo = nvbo;
368                         }
369                         if (unlikely(ret)) {
370                                 drm_gem_object_unreference_unlocked(gem);
371                                 if (ret != -ERESTARTSYS)
372                                         NV_ERROR(cli, "fail reserve\n");
373                                 return ret;
374                         }
375                 }
376
377                 b->user_priv = (uint64_t)(unsigned long)nvbo;
378                 nvbo->reserved_by = file_priv;
379                 nvbo->pbbo_index = i;
380                 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
381                     (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
382                         list_add_tail(&nvbo->entry, &op->both_list);
383                 else
384                 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
385                         list_add_tail(&nvbo->entry, &op->vram_list);
386                 else
387                 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
388                         list_add_tail(&nvbo->entry, &op->gart_list);
389                 else {
390                         NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
391                                  b->valid_domains);
392                         list_add_tail(&nvbo->entry, &op->both_list);
393                         validate_fini(op, NULL);
394                         return -EINVAL;
395                 }
396                 if (nvbo == res_bo)
397                         goto retry;
398         }
399
400         return 0;
401 }
402
403 static int
404 validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
405 {
406         struct nouveau_fence *fence = NULL;
407         int ret = 0;
408
409         spin_lock(&nvbo->bo.bdev->fence_lock);
410         if (nvbo->bo.sync_obj)
411                 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
412         spin_unlock(&nvbo->bo.bdev->fence_lock);
413
414         if (fence) {
415                 ret = nouveau_fence_sync(fence, chan);
416                 nouveau_fence_unref(&fence);
417         }
418
419         return ret;
420 }
421
422 static int
423 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
424               struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
425               uint64_t user_pbbo_ptr)
426 {
427         struct nouveau_drm *drm = chan->drm;
428         struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
429                                 (void __force __user *)(uintptr_t)user_pbbo_ptr;
430         struct nouveau_bo *nvbo;
431         int ret, relocs = 0;
432
433         list_for_each_entry(nvbo, list, entry) {
434                 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
435
436                 ret = validate_sync(chan, nvbo);
437                 if (unlikely(ret)) {
438                         NV_ERROR(cli, "fail pre-validate sync\n");
439                         return ret;
440                 }
441
442                 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
443                                              b->write_domains,
444                                              b->valid_domains);
445                 if (unlikely(ret)) {
446                         NV_ERROR(cli, "fail set_domain\n");
447                         return ret;
448                 }
449
450                 ret = nouveau_bo_validate(nvbo, true, false);
451                 if (unlikely(ret)) {
452                         if (ret != -ERESTARTSYS)
453                                 NV_ERROR(cli, "fail ttm_validate\n");
454                         return ret;
455                 }
456
457                 ret = validate_sync(chan, nvbo);
458                 if (unlikely(ret)) {
459                         NV_ERROR(cli, "fail post-validate sync\n");
460                         return ret;
461                 }
462
463                 if (nv_device(drm->device)->card_type < NV_50) {
464                         if (nvbo->bo.offset == b->presumed.offset &&
465                             ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
466                               b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
467                              (nvbo->bo.mem.mem_type == TTM_PL_TT &&
468                               b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
469                                 continue;
470
471                         if (nvbo->bo.mem.mem_type == TTM_PL_TT)
472                                 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
473                         else
474                                 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
475                         b->presumed.offset = nvbo->bo.offset;
476                         b->presumed.valid = 0;
477                         relocs++;
478
479                         if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
480                                              &b->presumed, sizeof(b->presumed)))
481                                 return -EFAULT;
482                 }
483         }
484
485         return relocs;
486 }
487
488 static int
489 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
490                              struct drm_file *file_priv,
491                              struct drm_nouveau_gem_pushbuf_bo *pbbo,
492                              uint64_t user_buffers, int nr_buffers,
493                              struct validate_op *op, int *apply_relocs)
494 {
495         struct nouveau_cli *cli = nouveau_cli(file_priv);
496         int ret, relocs = 0;
497
498         INIT_LIST_HEAD(&op->vram_list);
499         INIT_LIST_HEAD(&op->gart_list);
500         INIT_LIST_HEAD(&op->both_list);
501
502         if (nr_buffers == 0)
503                 return 0;
504
505         ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
506         if (unlikely(ret)) {
507                 if (ret != -ERESTARTSYS)
508                         NV_ERROR(cli, "validate_init\n");
509                 return ret;
510         }
511
512         ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
513         if (unlikely(ret < 0)) {
514                 if (ret != -ERESTARTSYS)
515                         NV_ERROR(cli, "validate vram_list\n");
516                 validate_fini(op, NULL);
517                 return ret;
518         }
519         relocs += ret;
520
521         ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
522         if (unlikely(ret < 0)) {
523                 if (ret != -ERESTARTSYS)
524                         NV_ERROR(cli, "validate gart_list\n");
525                 validate_fini(op, NULL);
526                 return ret;
527         }
528         relocs += ret;
529
530         ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
531         if (unlikely(ret < 0)) {
532                 if (ret != -ERESTARTSYS)
533                         NV_ERROR(cli, "validate both_list\n");
534                 validate_fini(op, NULL);
535                 return ret;
536         }
537         relocs += ret;
538
539         *apply_relocs = relocs;
540         return 0;
541 }
542
543 static inline void *
544 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
545 {
546         void *mem;
547         void __user *userptr = (void __force __user *)(uintptr_t)user;
548
549         mem = kmalloc(nmemb * size, GFP_KERNEL);
550         if (!mem)
551                 return ERR_PTR(-ENOMEM);
552
553         if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
554                 kfree(mem);
555                 return ERR_PTR(-EFAULT);
556         }
557
558         return mem;
559 }
560
561 static int
562 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
563                                 struct drm_nouveau_gem_pushbuf *req,
564                                 struct drm_nouveau_gem_pushbuf_bo *bo)
565 {
566         struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
567         int ret = 0;
568         unsigned i;
569
570         reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
571         if (IS_ERR(reloc))
572                 return PTR_ERR(reloc);
573
574         for (i = 0; i < req->nr_relocs; i++) {
575                 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
576                 struct drm_nouveau_gem_pushbuf_bo *b;
577                 struct nouveau_bo *nvbo;
578                 uint32_t data;
579
580                 if (unlikely(r->bo_index > req->nr_buffers)) {
581                         NV_ERROR(cli, "reloc bo index invalid\n");
582                         ret = -EINVAL;
583                         break;
584                 }
585
586                 b = &bo[r->bo_index];
587                 if (b->presumed.valid)
588                         continue;
589
590                 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
591                         NV_ERROR(cli, "reloc container bo index invalid\n");
592                         ret = -EINVAL;
593                         break;
594                 }
595                 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
596
597                 if (unlikely(r->reloc_bo_offset + 4 >
598                              nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
599                         NV_ERROR(cli, "reloc outside of bo\n");
600                         ret = -EINVAL;
601                         break;
602                 }
603
604                 if (!nvbo->kmap.virtual) {
605                         ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
606                                           &nvbo->kmap);
607                         if (ret) {
608                                 NV_ERROR(cli, "failed kmap for reloc\n");
609                                 break;
610                         }
611                         nvbo->validate_mapped = true;
612                 }
613
614                 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
615                         data = b->presumed.offset + r->data;
616                 else
617                 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
618                         data = (b->presumed.offset + r->data) >> 32;
619                 else
620                         data = r->data;
621
622                 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
623                         if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
624                                 data |= r->tor;
625                         else
626                                 data |= r->vor;
627                 }
628
629                 spin_lock(&nvbo->bo.bdev->fence_lock);
630                 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
631                 spin_unlock(&nvbo->bo.bdev->fence_lock);
632                 if (ret) {
633                         NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
634                         break;
635                 }
636
637                 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
638         }
639
640         kfree(reloc);
641         return ret;
642 }
643
644 int
645 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
646                           struct drm_file *file_priv)
647 {
648         struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
649         struct nouveau_cli *cli = nouveau_cli(file_priv);
650         struct nouveau_abi16_chan *temp;
651         struct nouveau_drm *drm = nouveau_drm(dev);
652         struct drm_nouveau_gem_pushbuf *req = data;
653         struct drm_nouveau_gem_pushbuf_push *push;
654         struct drm_nouveau_gem_pushbuf_bo *bo;
655         struct nouveau_channel *chan = NULL;
656         struct validate_op op;
657         struct nouveau_fence *fence = NULL;
658         int i, j, ret = 0, do_reloc = 0;
659
660         if (unlikely(!abi16))
661                 return -ENOMEM;
662
663         list_for_each_entry(temp, &abi16->channels, head) {
664                 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
665                         chan = temp->chan;
666                         break;
667                 }
668         }
669
670         if (!chan)
671                 return nouveau_abi16_put(abi16, -ENOENT);
672
673         req->vram_available = drm->gem.vram_available;
674         req->gart_available = drm->gem.gart_available;
675         if (unlikely(req->nr_push == 0))
676                 goto out_next;
677
678         if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
679                 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
680                          req->nr_push, NOUVEAU_GEM_MAX_PUSH);
681                 return nouveau_abi16_put(abi16, -EINVAL);
682         }
683
684         if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
685                 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
686                          req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
687                 return nouveau_abi16_put(abi16, -EINVAL);
688         }
689
690         if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
691                 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
692                          req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
693                 return nouveau_abi16_put(abi16, -EINVAL);
694         }
695
696         push = u_memcpya(req->push, req->nr_push, sizeof(*push));
697         if (IS_ERR(push))
698                 return nouveau_abi16_put(abi16, PTR_ERR(push));
699
700         bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
701         if (IS_ERR(bo)) {
702                 kfree(push);
703                 return nouveau_abi16_put(abi16, PTR_ERR(bo));
704         }
705
706         /* Ensure all push buffers are on validate list */
707         for (i = 0; i < req->nr_push; i++) {
708                 if (push[i].bo_index >= req->nr_buffers) {
709                         NV_ERROR(cli, "push %d buffer not in list\n", i);
710                         ret = -EINVAL;
711                         goto out_prevalid;
712                 }
713         }
714
715         /* Validate buffer list */
716         ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
717                                            req->nr_buffers, &op, &do_reloc);
718         if (ret) {
719                 if (ret != -ERESTARTSYS)
720                         NV_ERROR(cli, "validate: %d\n", ret);
721                 goto out_prevalid;
722         }
723
724         /* Apply any relocations that are required */
725         if (do_reloc) {
726                 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
727                 if (ret) {
728                         NV_ERROR(cli, "reloc apply: %d\n", ret);
729                         goto out;
730                 }
731         }
732
733         if (chan->dma.ib_max) {
734                 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
735                 if (ret) {
736                         NV_ERROR(cli, "nv50cal_space: %d\n", ret);
737                         goto out;
738                 }
739
740                 for (i = 0; i < req->nr_push; i++) {
741                         struct nouveau_bo *nvbo = (void *)(unsigned long)
742                                 bo[push[i].bo_index].user_priv;
743
744                         nv50_dma_push(chan, nvbo, push[i].offset,
745                                       push[i].length);
746                 }
747         } else
748         if (nv_device(drm->device)->chipset >= 0x25) {
749                 ret = RING_SPACE(chan, req->nr_push * 2);
750                 if (ret) {
751                         NV_ERROR(cli, "cal_space: %d\n", ret);
752                         goto out;
753                 }
754
755                 for (i = 0; i < req->nr_push; i++) {
756                         struct nouveau_bo *nvbo = (void *)(unsigned long)
757                                 bo[push[i].bo_index].user_priv;
758
759                         OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
760                         OUT_RING(chan, 0);
761                 }
762         } else {
763                 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
764                 if (ret) {
765                         NV_ERROR(cli, "jmp_space: %d\n", ret);
766                         goto out;
767                 }
768
769                 for (i = 0; i < req->nr_push; i++) {
770                         struct nouveau_bo *nvbo = (void *)(unsigned long)
771                                 bo[push[i].bo_index].user_priv;
772                         uint32_t cmd;
773
774                         cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
775                         cmd |= 0x20000000;
776                         if (unlikely(cmd != req->suffix0)) {
777                                 if (!nvbo->kmap.virtual) {
778                                         ret = ttm_bo_kmap(&nvbo->bo, 0,
779                                                           nvbo->bo.mem.
780                                                           num_pages,
781                                                           &nvbo->kmap);
782                                         if (ret) {
783                                                 WIND_RING(chan);
784                                                 goto out;
785                                         }
786                                         nvbo->validate_mapped = true;
787                                 }
788
789                                 nouveau_bo_wr32(nvbo, (push[i].offset +
790                                                 push[i].length - 8) / 4, cmd);
791                         }
792
793                         OUT_RING(chan, 0x20000000 |
794                                       (nvbo->bo.offset + push[i].offset));
795                         OUT_RING(chan, 0);
796                         for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
797                                 OUT_RING(chan, 0);
798                 }
799         }
800
801         ret = nouveau_fence_new(chan, false, &fence);
802         if (ret) {
803                 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
804                 WIND_RING(chan);
805                 goto out;
806         }
807
808 out:
809         validate_fini(&op, fence);
810         nouveau_fence_unref(&fence);
811
812 out_prevalid:
813         kfree(bo);
814         kfree(push);
815
816 out_next:
817         if (chan->dma.ib_max) {
818                 req->suffix0 = 0x00000000;
819                 req->suffix1 = 0x00000000;
820         } else
821         if (nv_device(drm->device)->chipset >= 0x25) {
822                 req->suffix0 = 0x00020000;
823                 req->suffix1 = 0x00000000;
824         } else {
825                 req->suffix0 = 0x20000000 |
826                               (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
827                 req->suffix1 = 0x00000000;
828         }
829
830         return nouveau_abi16_put(abi16, ret);
831 }
832
833 static inline uint32_t
834 domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
835 {
836         uint32_t flags = 0;
837
838         if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
839                 flags |= TTM_PL_FLAG_VRAM;
840         if (domain & NOUVEAU_GEM_DOMAIN_GART)
841                 flags |= TTM_PL_FLAG_TT;
842
843         return flags;
844 }
845
846 int
847 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
848                            struct drm_file *file_priv)
849 {
850         struct drm_nouveau_gem_cpu_prep *req = data;
851         struct drm_gem_object *gem;
852         struct nouveau_bo *nvbo;
853         bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
854         int ret = -EINVAL;
855
856         gem = drm_gem_object_lookup(dev, file_priv, req->handle);
857         if (!gem)
858                 return -ENOENT;
859         nvbo = nouveau_gem_object(gem);
860
861         spin_lock(&nvbo->bo.bdev->fence_lock);
862         ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
863         spin_unlock(&nvbo->bo.bdev->fence_lock);
864         drm_gem_object_unreference_unlocked(gem);
865         return ret;
866 }
867
868 int
869 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
870                            struct drm_file *file_priv)
871 {
872         return 0;
873 }
874
875 int
876 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
877                        struct drm_file *file_priv)
878 {
879         struct drm_nouveau_gem_info *req = data;
880         struct drm_gem_object *gem;
881         int ret;
882
883         gem = drm_gem_object_lookup(dev, file_priv, req->handle);
884         if (!gem)
885                 return -ENOENT;
886
887         ret = nouveau_gem_info(file_priv, gem, req);
888         drm_gem_object_unreference_unlocked(gem);
889         return ret;
890 }
891