]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nouveau_gem.c
832cf367f071e6c6d20c705f0bf40978c651936d
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nouveau_gem.c
1 /*
2 * Copyright (C) 2008 Ben Skeggs.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_drv.h"
28 #include "nouveau_dma.h"
29 #include "nouveau_fence.h"
30 #include "nouveau_abi16.h"
31
32 #include "nouveau_ttm.h"
33 #include "nouveau_gem.h"
34 #include "nouveau_mem.h"
35 #include "nouveau_vmm.h"
36
37 #include <nvif/class.h>
38
39 void
40 nouveau_gem_object_del(struct drm_gem_object *gem)
41 {
42 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
43 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
44 struct ttm_buffer_object *bo = &nvbo->bo;
45 struct device *dev = drm->dev->dev;
46 int ret;
47
48 ret = pm_runtime_get_sync(dev);
49 if (WARN_ON(ret < 0 && ret != -EACCES))
50 return;
51
52 if (gem->import_attach)
53 drm_prime_gem_destroy(gem, nvbo->bo.sg);
54
55 drm_gem_object_release(gem);
56
57 /* reset filp so nouveau_bo_del_ttm() can test for it */
58 gem->filp = NULL;
59 ttm_bo_unref(&bo);
60
61 pm_runtime_mark_last_busy(dev);
62 pm_runtime_put_autosuspend(dev);
63 }
64
65 int
66 nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
67 {
68 struct nouveau_cli *cli = nouveau_cli(file_priv);
69 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
70 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
71 struct device *dev = drm->dev->dev;
72 struct nouveau_vma *vma;
73 int ret;
74
75 if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
76 return 0;
77
78 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
79 if (ret)
80 return ret;
81
82 ret = pm_runtime_get_sync(dev);
83 if (ret < 0 && ret != -EACCES)
84 goto out;
85
86 ret = nouveau_vma_new(nvbo, &cli->vmm, &vma);
87 pm_runtime_mark_last_busy(dev);
88 pm_runtime_put_autosuspend(dev);
89 out:
90 ttm_bo_unreserve(&nvbo->bo);
91 return ret;
92 }
93
94 struct nouveau_gem_object_unmap {
95 struct nouveau_cli_work work;
96 struct nouveau_vma *vma;
97 };
98
99 static void
100 nouveau_gem_object_delete(struct nouveau_vma *vma)
101 {
102 nouveau_vma_del(&vma);
103 }
104
105 static void
106 nouveau_gem_object_delete_work(struct nouveau_cli_work *w)
107 {
108 struct nouveau_gem_object_unmap *work =
109 container_of(w, typeof(*work), work);
110 nouveau_gem_object_delete(work->vma);
111 kfree(work);
112 }
113
114 static void
115 nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
116 {
117 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
118 struct reservation_object *resv = nvbo->bo.resv;
119 struct reservation_object_list *fobj;
120 struct nouveau_gem_object_unmap *work;
121 struct dma_fence *fence = NULL;
122
123 fobj = reservation_object_get_list(resv);
124
125 list_del_init(&vma->head);
126
127 if (fobj && fobj->shared_count > 1)
128 ttm_bo_wait(&nvbo->bo, false, false);
129 else if (fobj && fobj->shared_count == 1)
130 fence = rcu_dereference_protected(fobj->shared[0],
131 reservation_object_held(resv));
132 else
133 fence = reservation_object_get_excl(nvbo->bo.resv);
134
135 if (!fence || !mapped) {
136 nouveau_gem_object_delete(vma);
137 return;
138 }
139
140 if (!(work = kmalloc(sizeof(*work), GFP_KERNEL))) {
141 WARN_ON(dma_fence_wait_timeout(fence, false, 2 * HZ) <= 0);
142 nouveau_gem_object_delete(vma);
143 return;
144 }
145
146 work->work.func = nouveau_gem_object_delete_work;
147 work->vma = vma;
148 nouveau_cli_work_queue(vma->vmm->cli, fence, &work->work);
149 }
150
151 void
152 nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
153 {
154 struct nouveau_cli *cli = nouveau_cli(file_priv);
155 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
156 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
157 struct device *dev = drm->dev->dev;
158 struct nouveau_vma *vma;
159 int ret;
160
161 if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
162 return;
163
164 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
165 if (ret)
166 return;
167
168 vma = nouveau_vma_find(nvbo, &cli->vmm);
169 if (vma) {
170 if (--vma->refs == 0) {
171 ret = pm_runtime_get_sync(dev);
172 if (!WARN_ON(ret < 0 && ret != -EACCES)) {
173 nouveau_gem_object_unmap(nvbo, vma);
174 pm_runtime_mark_last_busy(dev);
175 pm_runtime_put_autosuspend(dev);
176 }
177 }
178 }
179 ttm_bo_unreserve(&nvbo->bo);
180 }
181
182 int
183 nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
184 uint32_t tile_mode, uint32_t tile_flags,
185 struct nouveau_bo **pnvbo)
186 {
187 struct nouveau_drm *drm = cli->drm;
188 struct nouveau_bo *nvbo;
189 u32 flags = 0;
190 int ret;
191
192 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
193 flags |= TTM_PL_FLAG_VRAM;
194 if (domain & NOUVEAU_GEM_DOMAIN_GART)
195 flags |= TTM_PL_FLAG_TT;
196 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
197 flags |= TTM_PL_FLAG_SYSTEM;
198
199 if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
200 flags |= TTM_PL_FLAG_UNCACHED;
201
202 ret = nouveau_bo_new(cli, size, align, flags, tile_mode,
203 tile_flags, NULL, NULL, pnvbo);
204 if (ret)
205 return ret;
206 nvbo = *pnvbo;
207
208 /* we restrict allowed domains on nv50+ to only the types
209 * that were requested at creation time. not possibly on
210 * earlier chips without busting the ABI.
211 */
212 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
213 NOUVEAU_GEM_DOMAIN_GART;
214 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
215 nvbo->valid_domains &= domain;
216
217 /* Initialize the embedded gem-object. We return a single gem-reference
218 * to the caller, instead of a normal nouveau_bo ttm reference. */
219 ret = drm_gem_object_init(drm->dev, &nvbo->gem, nvbo->bo.mem.size);
220 if (ret) {
221 nouveau_bo_ref(NULL, pnvbo);
222 return -ENOMEM;
223 }
224
225 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
226 return 0;
227 }
228
229 static int
230 nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
231 struct drm_nouveau_gem_info *rep)
232 {
233 struct nouveau_cli *cli = nouveau_cli(file_priv);
234 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
235 struct nouveau_vma *vma;
236
237 if (is_power_of_2(nvbo->valid_domains))
238 rep->domain = nvbo->valid_domains;
239 else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
240 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
241 else
242 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
243 rep->offset = nvbo->bo.offset;
244 if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
245 vma = nouveau_vma_find(nvbo, &cli->vmm);
246 if (!vma)
247 return -EINVAL;
248
249 rep->offset = vma->addr;
250 }
251
252 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
253 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
254 rep->tile_mode = nvbo->mode;
255 rep->tile_flags = nvbo->contig ? 0 : NOUVEAU_GEM_TILE_NONCONTIG;
256 if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI)
257 rep->tile_flags |= nvbo->kind << 8;
258 else
259 if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
260 rep->tile_flags |= nvbo->kind << 8 | nvbo->comp << 16;
261 else
262 rep->tile_flags |= nvbo->zeta;
263 return 0;
264 }
265
266 int
267 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
268 struct drm_file *file_priv)
269 {
270 struct nouveau_drm *drm = nouveau_drm(dev);
271 struct nouveau_cli *cli = nouveau_cli(file_priv);
272 struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
273 struct drm_nouveau_gem_new *req = data;
274 struct nouveau_bo *nvbo = NULL;
275 int ret = 0;
276
277 if (!nvkm_fb_memtype_valid(fb, req->info.tile_flags)) {
278 NV_PRINTK(err, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
279 return -EINVAL;
280 }
281
282 ret = nouveau_gem_new(cli, req->info.size, req->align,
283 req->info.domain, req->info.tile_mode,
284 req->info.tile_flags, &nvbo);
285 if (ret)
286 return ret;
287
288 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
289 if (ret == 0) {
290 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
291 if (ret)
292 drm_gem_handle_delete(file_priv, req->info.handle);
293 }
294
295 /* drop reference from allocate - handle holds it now */
296 drm_gem_object_unreference_unlocked(&nvbo->gem);
297 return ret;
298 }
299
300 static int
301 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
302 uint32_t write_domains, uint32_t valid_domains)
303 {
304 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
305 struct ttm_buffer_object *bo = &nvbo->bo;
306 uint32_t domains = valid_domains & nvbo->valid_domains &
307 (write_domains ? write_domains : read_domains);
308 uint32_t pref_flags = 0, valid_flags = 0;
309
310 if (!domains)
311 return -EINVAL;
312
313 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
314 valid_flags |= TTM_PL_FLAG_VRAM;
315
316 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
317 valid_flags |= TTM_PL_FLAG_TT;
318
319 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
320 bo->mem.mem_type == TTM_PL_VRAM)
321 pref_flags |= TTM_PL_FLAG_VRAM;
322
323 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
324 bo->mem.mem_type == TTM_PL_TT)
325 pref_flags |= TTM_PL_FLAG_TT;
326
327 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
328 pref_flags |= TTM_PL_FLAG_VRAM;
329
330 else
331 pref_flags |= TTM_PL_FLAG_TT;
332
333 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
334
335 return 0;
336 }
337
338 struct validate_op {
339 struct list_head list;
340 struct ww_acquire_ctx ticket;
341 };
342
343 static void
344 validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
345 struct drm_nouveau_gem_pushbuf_bo *pbbo)
346 {
347 struct nouveau_bo *nvbo;
348 struct drm_nouveau_gem_pushbuf_bo *b;
349
350 while (!list_empty(&op->list)) {
351 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
352 b = &pbbo[nvbo->pbbo_index];
353
354 if (likely(fence))
355 nouveau_bo_fence(nvbo, fence, !!b->write_domains);
356
357 if (unlikely(nvbo->validate_mapped)) {
358 ttm_bo_kunmap(&nvbo->kmap);
359 nvbo->validate_mapped = false;
360 }
361
362 list_del(&nvbo->entry);
363 nvbo->reserved_by = NULL;
364 ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket);
365 drm_gem_object_unreference_unlocked(&nvbo->gem);
366 }
367 }
368
369 static void
370 validate_fini(struct validate_op *op, struct nouveau_fence *fence,
371 struct drm_nouveau_gem_pushbuf_bo *pbbo)
372 {
373 validate_fini_no_ticket(op, fence, pbbo);
374 ww_acquire_fini(&op->ticket);
375 }
376
377 static int
378 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
379 struct drm_nouveau_gem_pushbuf_bo *pbbo,
380 int nr_buffers, struct validate_op *op)
381 {
382 struct nouveau_cli *cli = nouveau_cli(file_priv);
383 int trycnt = 0;
384 int ret = -EINVAL, i;
385 struct nouveau_bo *res_bo = NULL;
386 LIST_HEAD(gart_list);
387 LIST_HEAD(vram_list);
388 LIST_HEAD(both_list);
389
390 ww_acquire_init(&op->ticket, &reservation_ww_class);
391 retry:
392 if (++trycnt > 100000) {
393 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
394 return -EINVAL;
395 }
396
397 for (i = 0; i < nr_buffers; i++) {
398 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
399 struct drm_gem_object *gem;
400 struct nouveau_bo *nvbo;
401
402 gem = drm_gem_object_lookup(file_priv, b->handle);
403 if (!gem) {
404 NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
405 ret = -ENOENT;
406 break;
407 }
408 nvbo = nouveau_gem_object(gem);
409 if (nvbo == res_bo) {
410 res_bo = NULL;
411 drm_gem_object_unreference_unlocked(gem);
412 continue;
413 }
414
415 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
416 NV_PRINTK(err, cli, "multiple instances of buffer %d on "
417 "validation list\n", b->handle);
418 drm_gem_object_unreference_unlocked(gem);
419 ret = -EINVAL;
420 break;
421 }
422
423 ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
424 if (ret) {
425 list_splice_tail_init(&vram_list, &op->list);
426 list_splice_tail_init(&gart_list, &op->list);
427 list_splice_tail_init(&both_list, &op->list);
428 validate_fini_no_ticket(op, NULL, NULL);
429 if (unlikely(ret == -EDEADLK)) {
430 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
431 &op->ticket);
432 if (!ret)
433 res_bo = nvbo;
434 }
435 if (unlikely(ret)) {
436 if (ret != -ERESTARTSYS)
437 NV_PRINTK(err, cli, "fail reserve\n");
438 break;
439 }
440 }
441
442 b->user_priv = (uint64_t)(unsigned long)nvbo;
443 nvbo->reserved_by = file_priv;
444 nvbo->pbbo_index = i;
445 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
446 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
447 list_add_tail(&nvbo->entry, &both_list);
448 else
449 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
450 list_add_tail(&nvbo->entry, &vram_list);
451 else
452 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
453 list_add_tail(&nvbo->entry, &gart_list);
454 else {
455 NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
456 b->valid_domains);
457 list_add_tail(&nvbo->entry, &both_list);
458 ret = -EINVAL;
459 break;
460 }
461 if (nvbo == res_bo)
462 goto retry;
463 }
464
465 ww_acquire_done(&op->ticket);
466 list_splice_tail(&vram_list, &op->list);
467 list_splice_tail(&gart_list, &op->list);
468 list_splice_tail(&both_list, &op->list);
469 if (ret)
470 validate_fini(op, NULL, NULL);
471 return ret;
472
473 }
474
475 static int
476 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
477 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
478 uint64_t user_pbbo_ptr)
479 {
480 struct nouveau_drm *drm = chan->drm;
481 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
482 (void __force __user *)(uintptr_t)user_pbbo_ptr;
483 struct nouveau_bo *nvbo;
484 int ret, relocs = 0;
485
486 list_for_each_entry(nvbo, list, entry) {
487 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
488
489 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
490 b->write_domains,
491 b->valid_domains);
492 if (unlikely(ret)) {
493 NV_PRINTK(err, cli, "fail set_domain\n");
494 return ret;
495 }
496
497 ret = nouveau_bo_validate(nvbo, true, false);
498 if (unlikely(ret)) {
499 if (ret != -ERESTARTSYS)
500 NV_PRINTK(err, cli, "fail ttm_validate\n");
501 return ret;
502 }
503
504 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
505 if (unlikely(ret)) {
506 if (ret != -ERESTARTSYS)
507 NV_PRINTK(err, cli, "fail post-validate sync\n");
508 return ret;
509 }
510
511 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
512 if (nvbo->bo.offset == b->presumed.offset &&
513 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
514 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
515 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
516 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
517 continue;
518
519 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
520 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
521 else
522 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
523 b->presumed.offset = nvbo->bo.offset;
524 b->presumed.valid = 0;
525 relocs++;
526
527 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
528 &b->presumed, sizeof(b->presumed)))
529 return -EFAULT;
530 }
531 }
532
533 return relocs;
534 }
535
536 static int
537 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
538 struct drm_file *file_priv,
539 struct drm_nouveau_gem_pushbuf_bo *pbbo,
540 uint64_t user_buffers, int nr_buffers,
541 struct validate_op *op, int *apply_relocs)
542 {
543 struct nouveau_cli *cli = nouveau_cli(file_priv);
544 int ret;
545
546 INIT_LIST_HEAD(&op->list);
547
548 if (nr_buffers == 0)
549 return 0;
550
551 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
552 if (unlikely(ret)) {
553 if (ret != -ERESTARTSYS)
554 NV_PRINTK(err, cli, "validate_init\n");
555 return ret;
556 }
557
558 ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
559 if (unlikely(ret < 0)) {
560 if (ret != -ERESTARTSYS)
561 NV_PRINTK(err, cli, "validating bo list\n");
562 validate_fini(op, NULL, NULL);
563 return ret;
564 }
565 *apply_relocs = ret;
566 return 0;
567 }
568
569 static inline void
570 u_free(void *addr)
571 {
572 kvfree(addr);
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 size *= nmemb;
582
583 mem = kvmalloc(size, GFP_KERNEL);
584 if (!mem)
585 return ERR_PTR(-ENOMEM);
586
587 if (copy_from_user(mem, userptr, size)) {
588 u_free(mem);
589 return ERR_PTR(-EFAULT);
590 }
591
592 return mem;
593 }
594
595 static int
596 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
597 struct drm_nouveau_gem_pushbuf *req,
598 struct drm_nouveau_gem_pushbuf_bo *bo)
599 {
600 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
601 int ret = 0;
602 unsigned i;
603
604 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
605 if (IS_ERR(reloc))
606 return PTR_ERR(reloc);
607
608 for (i = 0; i < req->nr_relocs; i++) {
609 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
610 struct drm_nouveau_gem_pushbuf_bo *b;
611 struct nouveau_bo *nvbo;
612 uint32_t data;
613
614 if (unlikely(r->bo_index > req->nr_buffers)) {
615 NV_PRINTK(err, cli, "reloc bo index invalid\n");
616 ret = -EINVAL;
617 break;
618 }
619
620 b = &bo[r->bo_index];
621 if (b->presumed.valid)
622 continue;
623
624 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
625 NV_PRINTK(err, cli, "reloc container bo index invalid\n");
626 ret = -EINVAL;
627 break;
628 }
629 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
630
631 if (unlikely(r->reloc_bo_offset + 4 >
632 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
633 NV_PRINTK(err, cli, "reloc outside of bo\n");
634 ret = -EINVAL;
635 break;
636 }
637
638 if (!nvbo->kmap.virtual) {
639 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
640 &nvbo->kmap);
641 if (ret) {
642 NV_PRINTK(err, cli, "failed kmap for reloc\n");
643 break;
644 }
645 nvbo->validate_mapped = true;
646 }
647
648 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
649 data = b->presumed.offset + r->data;
650 else
651 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
652 data = (b->presumed.offset + r->data) >> 32;
653 else
654 data = r->data;
655
656 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
657 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
658 data |= r->tor;
659 else
660 data |= r->vor;
661 }
662
663 ret = ttm_bo_wait(&nvbo->bo, false, false);
664 if (ret) {
665 NV_PRINTK(err, 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 u_free(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);
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->chid == 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_PRINTK(err, 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_PRINTK(err, 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_PRINTK(err, 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 u_free(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_PRINTK(err, 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_PRINTK(err, 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_PRINTK(err, 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_PRINTK(err, 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 (drm->client.device.info.chipset >= 0x25) {
781 ret = RING_SPACE(chan, req->nr_push * 2);
782 if (ret) {
783 NV_PRINTK(err, 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_PRINTK(err, 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.addr + ((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_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
836 WIND_RING(chan);
837 goto out;
838 }
839
840 out:
841 validate_fini(&op, fence, bo);
842 nouveau_fence_unref(&fence);
843
844 out_prevalid:
845 u_free(bo);
846 u_free(push);
847
848 out_next:
849 if (chan->dma.ib_max) {
850 req->suffix0 = 0x00000000;
851 req->suffix1 = 0x00000000;
852 } else
853 if (drm->client.device.info.chipset >= 0x25) {
854 req->suffix0 = 0x00020000;
855 req->suffix1 = 0x00000000;
856 } else {
857 req->suffix0 = 0x20000000 |
858 (chan->push.addr + ((chan->dma.cur + 2) << 2));
859 req->suffix1 = 0x00000000;
860 }
861
862 return nouveau_abi16_put(abi16, ret);
863 }
864
865 int
866 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
867 struct drm_file *file_priv)
868 {
869 struct drm_nouveau_gem_cpu_prep *req = data;
870 struct drm_gem_object *gem;
871 struct nouveau_bo *nvbo;
872 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
873 bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
874 long lret;
875 int ret;
876
877 gem = drm_gem_object_lookup(file_priv, req->handle);
878 if (!gem)
879 return -ENOENT;
880 nvbo = nouveau_gem_object(gem);
881
882 lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
883 no_wait ? 0 : 30 * HZ);
884 if (!lret)
885 ret = -EBUSY;
886 else if (lret > 0)
887 ret = 0;
888 else
889 ret = lret;
890
891 nouveau_bo_sync_for_cpu(nvbo);
892 drm_gem_object_unreference_unlocked(gem);
893
894 return ret;
895 }
896
897 int
898 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
899 struct drm_file *file_priv)
900 {
901 struct drm_nouveau_gem_cpu_fini *req = data;
902 struct drm_gem_object *gem;
903 struct nouveau_bo *nvbo;
904
905 gem = drm_gem_object_lookup(file_priv, req->handle);
906 if (!gem)
907 return -ENOENT;
908 nvbo = nouveau_gem_object(gem);
909
910 nouveau_bo_sync_for_device(nvbo);
911 drm_gem_object_unreference_unlocked(gem);
912 return 0;
913 }
914
915 int
916 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
917 struct drm_file *file_priv)
918 {
919 struct drm_nouveau_gem_info *req = data;
920 struct drm_gem_object *gem;
921 int ret;
922
923 gem = drm_gem_object_lookup(file_priv, req->handle);
924 if (!gem)
925 return -ENOENT;
926
927 ret = nouveau_gem_info(file_priv, gem, req);
928 drm_gem_object_unreference_unlocked(gem);
929 return ret;
930 }
931