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