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