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