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