nouveau_gem.c revision 1.5 1 /* $NetBSD: nouveau_gem.c,v 1.5 2018/08/27 04:58:24 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2008 Ben Skeggs.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nouveau_gem.c,v 1.5 2018/08/27 04:58:24 riastradh Exp $");
31
32 #include <linux/err.h> /* XXX */
33
34 #include "nouveau_drm.h"
35 #include "nouveau_dma.h"
36 #include "nouveau_fence.h"
37 #include "nouveau_abi16.h"
38
39 #include "nouveau_ttm.h"
40 #include "nouveau_gem.h"
41
42 void
43 nouveau_gem_object_del(struct drm_gem_object *gem)
44 {
45 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
46 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
47 struct ttm_buffer_object *bo = &nvbo->bo;
48 struct device *dev = drm->dev->dev;
49 int ret;
50
51 ret = pm_runtime_get_sync(dev);
52 if (WARN_ON(ret < 0 && ret != -EACCES))
53 return;
54
55 #ifndef __NetBSD__ /* XXX drm prime */
56 if (gem->import_attach)
57 drm_prime_gem_destroy(gem, nvbo->bo.sg);
58 #endif
59
60 drm_gem_object_release(gem);
61
62 /* reset filp so nouveau_bo_del_ttm() can test for it */
63 #ifdef __NetBSD__
64 /* XXX Whattakludge! */
65 gem->gemo_shm_uao = NULL;
66 #else
67 gem->filp = NULL;
68 #endif
69 ttm_bo_unref(&bo);
70
71 pm_runtime_mark_last_busy(dev);
72 pm_runtime_put_autosuspend(dev);
73 }
74
75 int
76 nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
77 {
78 struct nouveau_cli *cli = nouveau_cli(file_priv);
79 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
80 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
81 struct nvkm_vma *vma;
82 struct device *dev = drm->dev->dev;
83 int ret;
84
85 if (!cli->vm)
86 return 0;
87
88 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
89 if (ret)
90 return ret;
91
92 vma = nouveau_bo_vma_find(nvbo, cli->vm);
93 if (!vma) {
94 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
95 if (!vma) {
96 ret = -ENOMEM;
97 goto out;
98 }
99
100 ret = pm_runtime_get_sync(dev);
101 if (ret < 0 && ret != -EACCES) {
102 kfree(vma);
103 goto out;
104 }
105
106 ret = nouveau_bo_vma_add(nvbo, cli->vm, vma);
107 if (ret)
108 kfree(vma);
109
110 pm_runtime_mark_last_busy(dev);
111 pm_runtime_put_autosuspend(dev);
112 } else {
113 vma->refcount++;
114 }
115
116 out:
117 ttm_bo_unreserve(&nvbo->bo);
118 return ret;
119 }
120
121 static void
122 nouveau_gem_object_delete(void *data)
123 {
124 struct nvkm_vma *vma = data;
125 nvkm_vm_unmap(vma);
126 nvkm_vm_put(vma);
127 kfree(vma);
128 }
129
130 static void
131 nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nvkm_vma *vma)
132 {
133 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
134 struct reservation_object *resv = nvbo->bo.resv;
135 struct reservation_object_list *fobj;
136 struct fence *fence = NULL;
137
138 fobj = reservation_object_get_list(resv);
139
140 list_del(&vma->head);
141
142 if (fobj && fobj->shared_count > 1)
143 ttm_bo_wait(&nvbo->bo, true, false, false);
144 else if (fobj && fobj->shared_count == 1)
145 fence = rcu_dereference_protected(fobj->shared[0],
146 reservation_object_held(resv));
147 else
148 fence = reservation_object_get_excl(nvbo->bo.resv);
149
150 if (fence && mapped) {
151 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
152 } else {
153 if (mapped)
154 nvkm_vm_unmap(vma);
155 nvkm_vm_put(vma);
156 kfree(vma);
157 }
158 }
159
160 void
161 nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
162 {
163 struct nouveau_cli *cli = nouveau_cli(file_priv);
164 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
165 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
166 struct device *dev = drm->dev->dev;
167 struct nvkm_vma *vma;
168 int ret;
169
170 if (!cli->vm)
171 return;
172
173 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
174 if (ret)
175 return;
176
177 vma = nouveau_bo_vma_find(nvbo, cli->vm);
178 if (vma) {
179 if (--vma->refcount == 0) {
180 ret = pm_runtime_get_sync(dev);
181 if (!WARN_ON(ret < 0 && ret != -EACCES)) {
182 nouveau_gem_object_unmap(nvbo, vma);
183 pm_runtime_mark_last_busy(dev);
184 pm_runtime_put_autosuspend(dev);
185 }
186 }
187 }
188 ttm_bo_unreserve(&nvbo->bo);
189 }
190
191 int
192 nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
193 uint32_t tile_mode, uint32_t tile_flags,
194 struct nouveau_bo **pnvbo)
195 {
196 struct nouveau_drm *drm = nouveau_drm(dev);
197 struct nouveau_bo *nvbo;
198 u32 flags = 0;
199 int ret;
200
201 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
202 flags |= TTM_PL_FLAG_VRAM;
203 if (domain & NOUVEAU_GEM_DOMAIN_GART)
204 flags |= TTM_PL_FLAG_TT;
205 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
206 flags |= TTM_PL_FLAG_SYSTEM;
207
208 if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
209 flags |= TTM_PL_FLAG_UNCACHED;
210
211 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
212 tile_flags, NULL, NULL, pnvbo);
213 if (ret)
214 return ret;
215 nvbo = *pnvbo;
216
217 /* we restrict allowed domains on nv50+ to only the types
218 * that were requested at creation time. not possibly on
219 * earlier chips without busting the ABI.
220 */
221 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
222 NOUVEAU_GEM_DOMAIN_GART;
223 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
224 nvbo->valid_domains &= domain;
225
226 /* Initialize the embedded gem-object. We return a single gem-reference
227 * to the caller, instead of a normal nouveau_bo ttm reference. */
228 ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
229 if (ret) {
230 nouveau_bo_ref(NULL, pnvbo);
231 return -ENOMEM;
232 }
233
234 #ifndef __NetBSD__ /* XXX Let TTM swap; skip GEM like radeon. */
235 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
236 #endif
237 return 0;
238 }
239
240 static int
241 nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
242 struct drm_nouveau_gem_info *rep)
243 {
244 struct nouveau_cli *cli = nouveau_cli(file_priv);
245 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
246 struct nvkm_vma *vma;
247
248 if (is_power_of_2(nvbo->valid_domains))
249 rep->domain = nvbo->valid_domains;
250 else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
251 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
252 else
253 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
254 rep->offset = nvbo->bo.offset;
255 if (cli->vm) {
256 vma = nouveau_bo_vma_find(nvbo, cli->vm);
257 if (!vma)
258 return -EINVAL;
259
260 rep->offset = vma->offset;
261 }
262
263 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
264 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
265 rep->tile_mode = nvbo->tile_mode;
266 rep->tile_flags = nvbo->tile_flags;
267 return 0;
268 }
269
270 int
271 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
272 struct drm_file *file_priv)
273 {
274 struct nouveau_drm *drm = nouveau_drm(dev);
275 struct nouveau_cli *cli = nouveau_cli(file_priv);
276 struct nvkm_fb *fb = nvxx_fb(&drm->device);
277 struct drm_nouveau_gem_new *req = data;
278 struct nouveau_bo *nvbo = NULL;
279 int ret = 0;
280
281 if (!nvkm_fb_memtype_valid(fb, req->info.tile_flags)) {
282 NV_PRINTK(err, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
283 return -EINVAL;
284 }
285
286 ret = nouveau_gem_new(dev, req->info.size, req->align,
287 req->info.domain, req->info.tile_mode,
288 req->info.tile_flags, &nvbo);
289 if (ret)
290 return ret;
291
292 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
293 if (ret == 0) {
294 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
295 if (ret)
296 drm_gem_handle_delete(file_priv, req->info.handle);
297 }
298
299 /* drop reference from allocate - handle holds it now */
300 drm_gem_object_unreference_unlocked(&nvbo->gem);
301 return ret;
302 }
303
304 static int
305 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
306 uint32_t write_domains, uint32_t valid_domains)
307 {
308 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
309 struct ttm_buffer_object *bo = &nvbo->bo;
310 uint32_t domains = valid_domains & nvbo->valid_domains &
311 (write_domains ? write_domains : read_domains);
312 uint32_t pref_flags = 0, valid_flags = 0;
313
314 if (!domains)
315 return -EINVAL;
316
317 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
318 valid_flags |= TTM_PL_FLAG_VRAM;
319
320 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
321 valid_flags |= TTM_PL_FLAG_TT;
322
323 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
324 bo->mem.mem_type == TTM_PL_VRAM)
325 pref_flags |= TTM_PL_FLAG_VRAM;
326
327 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
328 bo->mem.mem_type == TTM_PL_TT)
329 pref_flags |= TTM_PL_FLAG_TT;
330
331 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
332 pref_flags |= TTM_PL_FLAG_VRAM;
333
334 else
335 pref_flags |= TTM_PL_FLAG_TT;
336
337 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
338
339 return 0;
340 }
341
342 struct validate_op {
343 struct list_head list;
344 struct ww_acquire_ctx ticket;
345 };
346
347 static void
348 validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
349 struct drm_nouveau_gem_pushbuf_bo *pbbo)
350 {
351 struct nouveau_bo *nvbo;
352 struct drm_nouveau_gem_pushbuf_bo *b;
353
354 while (!list_empty(&op->list)) {
355 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
356 b = &pbbo[nvbo->pbbo_index];
357
358 if (likely(fence))
359 nouveau_bo_fence(nvbo, fence, !!b->write_domains);
360
361 if (unlikely(nvbo->validate_mapped)) {
362 ttm_bo_kunmap(&nvbo->kmap);
363 nvbo->validate_mapped = false;
364 }
365
366 list_del(&nvbo->entry);
367 nvbo->reserved_by = NULL;
368 ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket);
369 drm_gem_object_unreference_unlocked(&nvbo->gem);
370 }
371 }
372
373 static void
374 validate_fini(struct validate_op *op, struct nouveau_fence *fence,
375 struct drm_nouveau_gem_pushbuf_bo *pbbo)
376 {
377 validate_fini_no_ticket(op, fence, pbbo);
378 ww_acquire_fini(&op->ticket);
379 }
380
381 static int
382 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
383 struct drm_nouveau_gem_pushbuf_bo *pbbo,
384 int nr_buffers, struct validate_op *op)
385 {
386 struct nouveau_cli *cli = nouveau_cli(file_priv);
387 struct drm_device *dev = chan->drm->dev;
388 int trycnt = 0;
389 int ret = -EINVAL, i;
390 struct nouveau_bo *res_bo = NULL;
391 LIST_HEAD(gart_list);
392 LIST_HEAD(vram_list);
393 LIST_HEAD(both_list);
394
395 ww_acquire_init(&op->ticket, &reservation_ww_class);
396 retry:
397 if (++trycnt > 100000) {
398 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
399 return -EINVAL;
400 }
401
402 for (i = 0; i < nr_buffers; i++) {
403 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
404 struct drm_gem_object *gem;
405 struct nouveau_bo *nvbo;
406
407 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
408 if (!gem) {
409 NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
410 ret = -ENOENT;
411 break;
412 }
413 nvbo = nouveau_gem_object(gem);
414 if (nvbo == res_bo) {
415 res_bo = NULL;
416 drm_gem_object_unreference_unlocked(gem);
417 continue;
418 }
419
420 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
421 NV_PRINTK(err, cli, "multiple instances of buffer %d on "
422 "validation list\n", b->handle);
423 drm_gem_object_unreference_unlocked(gem);
424 ret = -EINVAL;
425 break;
426 }
427
428 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
429 if (ret) {
430 list_splice_tail_init(&vram_list, &op->list);
431 list_splice_tail_init(&gart_list, &op->list);
432 list_splice_tail_init(&both_list, &op->list);
433 validate_fini_no_ticket(op, NULL, NULL);
434 if (unlikely(ret == -EDEADLK)) {
435 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
436 &op->ticket);
437 if (!ret)
438 res_bo = nvbo;
439 }
440 if (unlikely(ret)) {
441 if (ret != -ERESTARTSYS)
442 NV_PRINTK(err, cli, "fail reserve\n");
443 break;
444 }
445 }
446
447 b->user_priv = (uint64_t)(unsigned long)nvbo;
448 nvbo->reserved_by = file_priv;
449 nvbo->pbbo_index = i;
450 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
451 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
452 list_add_tail(&nvbo->entry, &both_list);
453 else
454 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
455 list_add_tail(&nvbo->entry, &vram_list);
456 else
457 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
458 list_add_tail(&nvbo->entry, &gart_list);
459 else {
460 NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
461 b->valid_domains);
462 list_add_tail(&nvbo->entry, &both_list);
463 ret = -EINVAL;
464 break;
465 }
466 if (nvbo == res_bo)
467 goto retry;
468 }
469
470 ww_acquire_done(&op->ticket);
471 list_splice_tail(&vram_list, &op->list);
472 list_splice_tail(&gart_list, &op->list);
473 list_splice_tail(&both_list, &op->list);
474 if (ret)
475 validate_fini(op, NULL, NULL);
476 return ret;
477
478 }
479
480 #ifdef __NetBSD__ /* XXX yargleblargh */
481 # define __force
482 #endif
483
484 static int
485 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
486 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
487 uint64_t user_pbbo_ptr)
488 {
489 struct nouveau_drm *drm = chan->drm;
490 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
491 (void __force __user *)(uintptr_t)user_pbbo_ptr;
492 struct nouveau_bo *nvbo;
493 int ret, relocs = 0;
494
495 list_for_each_entry(nvbo, list, entry) {
496 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
497
498 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
499 b->write_domains,
500 b->valid_domains);
501 if (unlikely(ret)) {
502 NV_PRINTK(err, cli, "fail set_domain\n");
503 return ret;
504 }
505
506 ret = nouveau_bo_validate(nvbo, true, false);
507 if (unlikely(ret)) {
508 if (ret != -ERESTARTSYS)
509 NV_PRINTK(err, cli, "fail ttm_validate\n");
510 return ret;
511 }
512
513 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
514 if (unlikely(ret)) {
515 if (ret != -ERESTARTSYS)
516 NV_PRINTK(err, cli, "fail post-validate sync\n");
517 return ret;
518 }
519
520 if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
521 if (nvbo->bo.offset == b->presumed.offset &&
522 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
523 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
524 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
525 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
526 continue;
527
528 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
529 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
530 else
531 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
532 b->presumed.offset = nvbo->bo.offset;
533 b->presumed.valid = 0;
534 relocs++;
535
536 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
537 &b->presumed, sizeof(b->presumed)))
538 return -EFAULT;
539 }
540 }
541
542 return relocs;
543 }
544
545 static int
546 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
547 struct drm_file *file_priv,
548 struct drm_nouveau_gem_pushbuf_bo *pbbo,
549 uint64_t user_buffers, int nr_buffers,
550 struct validate_op *op, int *apply_relocs)
551 {
552 struct nouveau_cli *cli = nouveau_cli(file_priv);
553 int ret;
554
555 INIT_LIST_HEAD(&op->list);
556
557 if (nr_buffers == 0)
558 return 0;
559
560 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
561 if (unlikely(ret)) {
562 if (ret != -ERESTARTSYS)
563 NV_PRINTK(err, cli, "validate_init\n");
564 return ret;
565 }
566
567 ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
568 if (unlikely(ret < 0)) {
569 if (ret != -ERESTARTSYS)
570 NV_PRINTK(err, cli, "validating bo list\n");
571 validate_fini(op, NULL, NULL);
572 return ret;
573 }
574 *apply_relocs = ret;
575 return 0;
576 }
577
578 static inline void
579 u_free(void *addr)
580 {
581 kvfree(addr);
582 }
583
584 static inline void *
585 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
586 {
587 void *mem;
588 void __user *userptr = (void __force __user *)(uintptr_t)user;
589
590 size *= nmemb;
591
592 mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
593 if (!mem)
594 mem = vmalloc(size);
595 if (!mem)
596 return ERR_PTR(-ENOMEM);
597
598 if (copy_from_user(mem, userptr, size)) {
599 u_free(mem);
600 return ERR_PTR(-EFAULT);
601 }
602
603 return mem;
604 }
605
606 static int
607 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
608 struct drm_nouveau_gem_pushbuf *req,
609 struct drm_nouveau_gem_pushbuf_bo *bo)
610 {
611 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
612 int ret = 0;
613 unsigned i;
614
615 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
616 if (IS_ERR(reloc))
617 return PTR_ERR(reloc);
618
619 for (i = 0; i < req->nr_relocs; i++) {
620 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
621 struct drm_nouveau_gem_pushbuf_bo *b;
622 struct nouveau_bo *nvbo;
623 uint32_t data;
624
625 if (unlikely(r->bo_index > req->nr_buffers)) {
626 NV_PRINTK(err, cli, "reloc bo index invalid\n");
627 ret = -EINVAL;
628 break;
629 }
630
631 b = &bo[r->bo_index];
632 if (b->presumed.valid)
633 continue;
634
635 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
636 NV_PRINTK(err, cli, "reloc container bo index invalid\n");
637 ret = -EINVAL;
638 break;
639 }
640 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
641
642 if (unlikely(r->reloc_bo_offset + 4 >
643 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
644 NV_PRINTK(err, cli, "reloc outside of bo\n");
645 ret = -EINVAL;
646 break;
647 }
648
649 if (!nvbo->kmap.virtual) {
650 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
651 &nvbo->kmap);
652 if (ret) {
653 NV_PRINTK(err, cli, "failed kmap for reloc\n");
654 break;
655 }
656 nvbo->validate_mapped = true;
657 }
658
659 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
660 data = b->presumed.offset + r->data;
661 else
662 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
663 data = (b->presumed.offset + r->data) >> 32;
664 else
665 data = r->data;
666
667 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
668 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
669 data |= r->tor;
670 else
671 data |= r->vor;
672 }
673
674 ret = ttm_bo_wait(&nvbo->bo, true, false, false);
675 if (ret) {
676 NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
677 break;
678 }
679
680 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
681 }
682
683 u_free(reloc);
684 return ret;
685 }
686
687 int
688 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
689 struct drm_file *file_priv)
690 {
691 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
692 struct nouveau_cli *cli = nouveau_cli(file_priv);
693 struct nouveau_abi16_chan *temp;
694 struct nouveau_drm *drm = nouveau_drm(dev);
695 struct drm_nouveau_gem_pushbuf *req = data;
696 struct drm_nouveau_gem_pushbuf_push *push;
697 struct drm_nouveau_gem_pushbuf_bo *bo;
698 struct nouveau_channel *chan = NULL;
699 struct validate_op op;
700 struct nouveau_fence *fence = NULL;
701 int i, j, ret = 0, do_reloc = 0;
702
703 if (unlikely(!abi16))
704 return -ENOMEM;
705
706 list_for_each_entry(temp, &abi16->channels, head) {
707 if (temp->chan->chid == req->channel) {
708 chan = temp->chan;
709 break;
710 }
711 }
712
713 if (!chan)
714 return nouveau_abi16_put(abi16, -ENOENT);
715
716 req->vram_available = drm->gem.vram_available;
717 req->gart_available = drm->gem.gart_available;
718 if (unlikely(req->nr_push == 0))
719 goto out_next;
720
721 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
722 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
723 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
724 return nouveau_abi16_put(abi16, -EINVAL);
725 }
726
727 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
728 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
729 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
730 return nouveau_abi16_put(abi16, -EINVAL);
731 }
732
733 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
734 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
735 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
736 return nouveau_abi16_put(abi16, -EINVAL);
737 }
738
739 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
740 if (IS_ERR(push))
741 return nouveau_abi16_put(abi16, PTR_ERR(push));
742
743 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
744 if (IS_ERR(bo)) {
745 u_free(push);
746 return nouveau_abi16_put(abi16, PTR_ERR(bo));
747 }
748
749 /* Ensure all push buffers are on validate list */
750 for (i = 0; i < req->nr_push; i++) {
751 if (push[i].bo_index >= req->nr_buffers) {
752 NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
753 ret = -EINVAL;
754 goto out_prevalid;
755 }
756 }
757
758 /* Validate buffer list */
759 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
760 req->nr_buffers, &op, &do_reloc);
761 if (ret) {
762 if (ret != -ERESTARTSYS)
763 NV_PRINTK(err, cli, "validate: %d\n", ret);
764 goto out_prevalid;
765 }
766
767 /* Apply any relocations that are required */
768 if (do_reloc) {
769 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
770 if (ret) {
771 NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
772 goto out;
773 }
774 }
775
776 if (chan->dma.ib_max) {
777 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
778 if (ret) {
779 NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
780 goto out;
781 }
782
783 for (i = 0; i < req->nr_push; i++) {
784 struct nouveau_bo *nvbo = (void *)(unsigned long)
785 bo[push[i].bo_index].user_priv;
786
787 nv50_dma_push(chan, nvbo, push[i].offset,
788 push[i].length);
789 }
790 } else
791 if (drm->device.info.chipset >= 0x25) {
792 ret = RING_SPACE(chan, req->nr_push * 2);
793 if (ret) {
794 NV_PRINTK(err, cli, "cal_space: %d\n", ret);
795 goto out;
796 }
797
798 for (i = 0; i < req->nr_push; i++) {
799 struct nouveau_bo *nvbo = (void *)(unsigned long)
800 bo[push[i].bo_index].user_priv;
801
802 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
803 OUT_RING(chan, 0);
804 }
805 } else {
806 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
807 if (ret) {
808 NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
809 goto out;
810 }
811
812 for (i = 0; i < req->nr_push; i++) {
813 struct nouveau_bo *nvbo = (void *)(unsigned long)
814 bo[push[i].bo_index].user_priv;
815 uint32_t cmd;
816
817 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
818 cmd |= 0x20000000;
819 if (unlikely(cmd != req->suffix0)) {
820 if (!nvbo->kmap.virtual) {
821 ret = ttm_bo_kmap(&nvbo->bo, 0,
822 nvbo->bo.mem.
823 num_pages,
824 &nvbo->kmap);
825 if (ret) {
826 WIND_RING(chan);
827 goto out;
828 }
829 nvbo->validate_mapped = true;
830 }
831
832 nouveau_bo_wr32(nvbo, (push[i].offset +
833 push[i].length - 8) / 4, cmd);
834 }
835
836 OUT_RING(chan, 0x20000000 |
837 (nvbo->bo.offset + push[i].offset));
838 OUT_RING(chan, 0);
839 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
840 OUT_RING(chan, 0);
841 }
842 }
843
844 ret = nouveau_fence_new(chan, false, &fence);
845 if (ret) {
846 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
847 WIND_RING(chan);
848 goto out;
849 }
850
851 out:
852 validate_fini(&op, fence, bo);
853 nouveau_fence_unref(&fence);
854
855 out_prevalid:
856 u_free(bo);
857 u_free(push);
858
859 out_next:
860 if (chan->dma.ib_max) {
861 req->suffix0 = 0x00000000;
862 req->suffix1 = 0x00000000;
863 } else
864 if (drm->device.info.chipset >= 0x25) {
865 req->suffix0 = 0x00020000;
866 req->suffix1 = 0x00000000;
867 } else {
868 req->suffix0 = 0x20000000 |
869 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
870 req->suffix1 = 0x00000000;
871 }
872
873 return nouveau_abi16_put(abi16, ret);
874 }
875
876 int
877 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
878 struct drm_file *file_priv)
879 {
880 struct drm_nouveau_gem_cpu_prep *req = data;
881 struct drm_gem_object *gem;
882 struct nouveau_bo *nvbo;
883 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
884 bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
885 int ret;
886
887 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
888 if (!gem)
889 return -ENOENT;
890 nvbo = nouveau_gem_object(gem);
891
892 if (no_wait)
893 ret = reservation_object_test_signaled_rcu(nvbo->bo.resv, write) ? 0 : -EBUSY;
894 else {
895 long lret;
896
897 lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true, 30 * HZ);
898 if (!lret)
899 ret = -EBUSY;
900 else if (lret > 0)
901 ret = 0;
902 else
903 ret = lret;
904 }
905 nouveau_bo_sync_for_cpu(nvbo);
906 drm_gem_object_unreference_unlocked(gem);
907
908 return ret;
909 }
910
911 int
912 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
913 struct drm_file *file_priv)
914 {
915 struct drm_nouveau_gem_cpu_fini *req = data;
916 struct drm_gem_object *gem;
917 struct nouveau_bo *nvbo;
918
919 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
920 if (!gem)
921 return -ENOENT;
922 nvbo = nouveau_gem_object(gem);
923
924 nouveau_bo_sync_for_device(nvbo);
925 drm_gem_object_unreference_unlocked(gem);
926 return 0;
927 }
928
929 int
930 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
931 struct drm_file *file_priv)
932 {
933 struct drm_nouveau_gem_info *req = data;
934 struct drm_gem_object *gem;
935 int ret;
936
937 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
938 if (!gem)
939 return -ENOENT;
940
941 ret = nouveau_gem_info(file_priv, gem, req);
942 drm_gem_object_unreference_unlocked(gem);
943 return ret;
944 }
945
946