]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nouveau_gem.c
UBUNTU: Ubuntu-4.15.0-96.97
[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_cli *cli = nouveau_cli(file_priv);
271 struct drm_nouveau_gem_new *req = data;
272 struct nouveau_bo *nvbo = NULL;
273 int ret = 0;
274
275 ret = nouveau_gem_new(cli, req->info.size, req->align,
276 req->info.domain, req->info.tile_mode,
277 req->info.tile_flags, &nvbo);
278 if (ret)
279 return ret;
280
281 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
282 if (ret == 0) {
283 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
284 if (ret)
285 drm_gem_handle_delete(file_priv, req->info.handle);
286 }
287
288 /* drop reference from allocate - handle holds it now */
289 drm_gem_object_unreference_unlocked(&nvbo->gem);
290 return ret;
291 }
292
293 static int
294 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
295 uint32_t write_domains, uint32_t valid_domains)
296 {
297 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
298 struct ttm_buffer_object *bo = &nvbo->bo;
299 uint32_t domains = valid_domains & nvbo->valid_domains &
300 (write_domains ? write_domains : read_domains);
301 uint32_t pref_flags = 0, valid_flags = 0;
302
303 if (!domains)
304 return -EINVAL;
305
306 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
307 valid_flags |= TTM_PL_FLAG_VRAM;
308
309 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
310 valid_flags |= TTM_PL_FLAG_TT;
311
312 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
313 bo->mem.mem_type == TTM_PL_VRAM)
314 pref_flags |= TTM_PL_FLAG_VRAM;
315
316 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
317 bo->mem.mem_type == TTM_PL_TT)
318 pref_flags |= TTM_PL_FLAG_TT;
319
320 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
321 pref_flags |= TTM_PL_FLAG_VRAM;
322
323 else
324 pref_flags |= TTM_PL_FLAG_TT;
325
326 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
327
328 return 0;
329 }
330
331 struct validate_op {
332 struct list_head list;
333 struct ww_acquire_ctx ticket;
334 };
335
336 static void
337 validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
338 struct drm_nouveau_gem_pushbuf_bo *pbbo)
339 {
340 struct nouveau_bo *nvbo;
341 struct drm_nouveau_gem_pushbuf_bo *b;
342
343 while (!list_empty(&op->list)) {
344 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
345 b = &pbbo[nvbo->pbbo_index];
346
347 if (likely(fence))
348 nouveau_bo_fence(nvbo, fence, !!b->write_domains);
349
350 if (unlikely(nvbo->validate_mapped)) {
351 ttm_bo_kunmap(&nvbo->kmap);
352 nvbo->validate_mapped = false;
353 }
354
355 list_del(&nvbo->entry);
356 nvbo->reserved_by = NULL;
357 ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket);
358 drm_gem_object_unreference_unlocked(&nvbo->gem);
359 }
360 }
361
362 static void
363 validate_fini(struct validate_op *op, struct nouveau_fence *fence,
364 struct drm_nouveau_gem_pushbuf_bo *pbbo)
365 {
366 validate_fini_no_ticket(op, fence, pbbo);
367 ww_acquire_fini(&op->ticket);
368 }
369
370 static int
371 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
372 struct drm_nouveau_gem_pushbuf_bo *pbbo,
373 int nr_buffers, struct validate_op *op)
374 {
375 struct nouveau_cli *cli = nouveau_cli(file_priv);
376 int trycnt = 0;
377 int ret = -EINVAL, i;
378 struct nouveau_bo *res_bo = NULL;
379 LIST_HEAD(gart_list);
380 LIST_HEAD(vram_list);
381 LIST_HEAD(both_list);
382
383 ww_acquire_init(&op->ticket, &reservation_ww_class);
384 retry:
385 if (++trycnt > 100000) {
386 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
387 return -EINVAL;
388 }
389
390 for (i = 0; i < nr_buffers; i++) {
391 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
392 struct drm_gem_object *gem;
393 struct nouveau_bo *nvbo;
394
395 gem = drm_gem_object_lookup(file_priv, b->handle);
396 if (!gem) {
397 NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
398 ret = -ENOENT;
399 break;
400 }
401 nvbo = nouveau_gem_object(gem);
402 if (nvbo == res_bo) {
403 res_bo = NULL;
404 drm_gem_object_unreference_unlocked(gem);
405 continue;
406 }
407
408 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
409 NV_PRINTK(err, cli, "multiple instances of buffer %d on "
410 "validation list\n", b->handle);
411 drm_gem_object_unreference_unlocked(gem);
412 ret = -EINVAL;
413 break;
414 }
415
416 ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
417 if (ret) {
418 list_splice_tail_init(&vram_list, &op->list);
419 list_splice_tail_init(&gart_list, &op->list);
420 list_splice_tail_init(&both_list, &op->list);
421 validate_fini_no_ticket(op, NULL, NULL);
422 if (unlikely(ret == -EDEADLK)) {
423 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
424 &op->ticket);
425 if (!ret)
426 res_bo = nvbo;
427 }
428 if (unlikely(ret)) {
429 if (ret != -ERESTARTSYS)
430 NV_PRINTK(err, cli, "fail reserve\n");
431 break;
432 }
433 }
434
435 if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
436 struct nouveau_vmm *vmm = &cli->vmm;
437 struct nouveau_vma *vma = nouveau_vma_find(nvbo, vmm);
438 if (!vma) {
439 NV_PRINTK(err, cli, "vma not found!\n");
440 ret = -EINVAL;
441 break;
442 }
443
444 b->user_priv = (uint64_t)(unsigned long)vma;
445 } else {
446 b->user_priv = (uint64_t)(unsigned long)nvbo;
447 }
448
449 nvbo->reserved_by = file_priv;
450 nvbo->pbbo_index = i;
451 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
452 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
453 list_add_tail(&nvbo->entry, &both_list);
454 else
455 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
456 list_add_tail(&nvbo->entry, &vram_list);
457 else
458 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
459 list_add_tail(&nvbo->entry, &gart_list);
460 else {
461 NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
462 b->valid_domains);
463 list_add_tail(&nvbo->entry, &both_list);
464 ret = -EINVAL;
465 break;
466 }
467 if (nvbo == res_bo)
468 goto retry;
469 }
470
471 ww_acquire_done(&op->ticket);
472 list_splice_tail(&vram_list, &op->list);
473 list_splice_tail(&gart_list, &op->list);
474 list_splice_tail(&both_list, &op->list);
475 if (ret)
476 validate_fini(op, NULL, NULL);
477 return ret;
478
479 }
480
481 static int
482 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
483 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
484 uint64_t user_pbbo_ptr)
485 {
486 struct nouveau_drm *drm = chan->drm;
487 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
488 (void __force __user *)(uintptr_t)user_pbbo_ptr;
489 struct nouveau_bo *nvbo;
490 int ret, relocs = 0;
491
492 list_for_each_entry(nvbo, list, entry) {
493 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
494
495 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
496 b->write_domains,
497 b->valid_domains);
498 if (unlikely(ret)) {
499 NV_PRINTK(err, cli, "fail set_domain\n");
500 return ret;
501 }
502
503 ret = nouveau_bo_validate(nvbo, true, false);
504 if (unlikely(ret)) {
505 if (ret != -ERESTARTSYS)
506 NV_PRINTK(err, cli, "fail ttm_validate\n");
507 return ret;
508 }
509
510 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
511 if (unlikely(ret)) {
512 if (ret != -ERESTARTSYS)
513 NV_PRINTK(err, cli, "fail post-validate sync\n");
514 return ret;
515 }
516
517 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
518 if (nvbo->bo.offset == b->presumed.offset &&
519 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
520 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
521 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
522 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
523 continue;
524
525 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
526 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
527 else
528 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
529 b->presumed.offset = nvbo->bo.offset;
530 b->presumed.valid = 0;
531 relocs++;
532
533 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
534 &b->presumed, sizeof(b->presumed)))
535 return -EFAULT;
536 }
537 }
538
539 return relocs;
540 }
541
542 static int
543 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
544 struct drm_file *file_priv,
545 struct drm_nouveau_gem_pushbuf_bo *pbbo,
546 uint64_t user_buffers, int nr_buffers,
547 struct validate_op *op, int *apply_relocs)
548 {
549 struct nouveau_cli *cli = nouveau_cli(file_priv);
550 int ret;
551
552 INIT_LIST_HEAD(&op->list);
553
554 if (nr_buffers == 0)
555 return 0;
556
557 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
558 if (unlikely(ret)) {
559 if (ret != -ERESTARTSYS)
560 NV_PRINTK(err, cli, "validate_init\n");
561 return ret;
562 }
563
564 ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
565 if (unlikely(ret < 0)) {
566 if (ret != -ERESTARTSYS)
567 NV_PRINTK(err, cli, "validating bo list\n");
568 validate_fini(op, NULL, NULL);
569 return ret;
570 }
571 *apply_relocs = ret;
572 return 0;
573 }
574
575 static inline void
576 u_free(void *addr)
577 {
578 kvfree(addr);
579 }
580
581 static inline void *
582 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
583 {
584 void *mem;
585 void __user *userptr = (void __force __user *)(uintptr_t)user;
586
587 size *= nmemb;
588
589 mem = kvmalloc(size, GFP_KERNEL);
590 if (!mem)
591 return ERR_PTR(-ENOMEM);
592
593 if (copy_from_user(mem, userptr, size)) {
594 u_free(mem);
595 return ERR_PTR(-EFAULT);
596 }
597
598 return mem;
599 }
600
601 static int
602 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
603 struct drm_nouveau_gem_pushbuf *req,
604 struct drm_nouveau_gem_pushbuf_bo *bo)
605 {
606 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
607 int ret = 0;
608 unsigned i;
609
610 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
611 if (IS_ERR(reloc))
612 return PTR_ERR(reloc);
613
614 for (i = 0; i < req->nr_relocs; i++) {
615 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
616 struct drm_nouveau_gem_pushbuf_bo *b;
617 struct nouveau_bo *nvbo;
618 uint32_t data;
619
620 if (unlikely(r->bo_index >= req->nr_buffers)) {
621 NV_PRINTK(err, cli, "reloc bo index invalid\n");
622 ret = -EINVAL;
623 break;
624 }
625
626 b = &bo[r->bo_index];
627 if (b->presumed.valid)
628 continue;
629
630 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
631 NV_PRINTK(err, cli, "reloc container bo index invalid\n");
632 ret = -EINVAL;
633 break;
634 }
635 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
636
637 if (unlikely(r->reloc_bo_offset + 4 >
638 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
639 NV_PRINTK(err, cli, "reloc outside of bo\n");
640 ret = -EINVAL;
641 break;
642 }
643
644 if (!nvbo->kmap.virtual) {
645 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
646 &nvbo->kmap);
647 if (ret) {
648 NV_PRINTK(err, cli, "failed kmap for reloc\n");
649 break;
650 }
651 nvbo->validate_mapped = true;
652 }
653
654 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
655 data = b->presumed.offset + r->data;
656 else
657 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
658 data = (b->presumed.offset + r->data) >> 32;
659 else
660 data = r->data;
661
662 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
663 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
664 data |= r->tor;
665 else
666 data |= r->vor;
667 }
668
669 ret = ttm_bo_wait(&nvbo->bo, false, false);
670 if (ret) {
671 NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
672 break;
673 }
674
675 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
676 }
677
678 u_free(reloc);
679 return ret;
680 }
681
682 int
683 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
684 struct drm_file *file_priv)
685 {
686 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
687 struct nouveau_cli *cli = nouveau_cli(file_priv);
688 struct nouveau_abi16_chan *temp;
689 struct nouveau_drm *drm = nouveau_drm(dev);
690 struct drm_nouveau_gem_pushbuf *req = data;
691 struct drm_nouveau_gem_pushbuf_push *push;
692 struct drm_nouveau_gem_pushbuf_bo *bo;
693 struct nouveau_channel *chan = NULL;
694 struct validate_op op;
695 struct nouveau_fence *fence = NULL;
696 int i, j, ret = 0, do_reloc = 0;
697
698 if (unlikely(!abi16))
699 return -ENOMEM;
700
701 list_for_each_entry(temp, &abi16->channels, head) {
702 if (temp->chan->chid == req->channel) {
703 chan = temp->chan;
704 break;
705 }
706 }
707
708 if (!chan)
709 return nouveau_abi16_put(abi16, -ENOENT);
710
711 req->vram_available = drm->gem.vram_available;
712 req->gart_available = drm->gem.gart_available;
713 if (unlikely(req->nr_push == 0))
714 goto out_next;
715
716 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
717 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
718 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
719 return nouveau_abi16_put(abi16, -EINVAL);
720 }
721
722 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
723 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
724 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
725 return nouveau_abi16_put(abi16, -EINVAL);
726 }
727
728 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
729 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
730 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
731 return nouveau_abi16_put(abi16, -EINVAL);
732 }
733
734 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
735 if (IS_ERR(push))
736 return nouveau_abi16_put(abi16, PTR_ERR(push));
737
738 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
739 if (IS_ERR(bo)) {
740 u_free(push);
741 return nouveau_abi16_put(abi16, PTR_ERR(bo));
742 }
743
744 /* Ensure all push buffers are on validate list */
745 for (i = 0; i < req->nr_push; i++) {
746 if (push[i].bo_index >= req->nr_buffers) {
747 NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
748 ret = -EINVAL;
749 goto out_prevalid;
750 }
751 }
752
753 /* Validate buffer list */
754 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
755 req->nr_buffers, &op, &do_reloc);
756 if (ret) {
757 if (ret != -ERESTARTSYS)
758 NV_PRINTK(err, cli, "validate: %d\n", ret);
759 goto out_prevalid;
760 }
761
762 /* Apply any relocations that are required */
763 if (do_reloc) {
764 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
765 if (ret) {
766 NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
767 goto out;
768 }
769 }
770
771 if (chan->dma.ib_max) {
772 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
773 if (ret) {
774 NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
775 goto out;
776 }
777
778 for (i = 0; i < req->nr_push; i++) {
779 struct nouveau_vma *vma = (void *)(unsigned long)
780 bo[push[i].bo_index].user_priv;
781
782 nv50_dma_push(chan, vma->addr + push[i].offset,
783 push[i].length);
784 }
785 } else
786 if (drm->client.device.info.chipset >= 0x25) {
787 ret = RING_SPACE(chan, req->nr_push * 2);
788 if (ret) {
789 NV_PRINTK(err, cli, "cal_space: %d\n", ret);
790 goto out;
791 }
792
793 for (i = 0; i < req->nr_push; i++) {
794 struct nouveau_bo *nvbo = (void *)(unsigned long)
795 bo[push[i].bo_index].user_priv;
796
797 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
798 OUT_RING(chan, 0);
799 }
800 } else {
801 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
802 if (ret) {
803 NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
804 goto out;
805 }
806
807 for (i = 0; i < req->nr_push; i++) {
808 struct nouveau_bo *nvbo = (void *)(unsigned long)
809 bo[push[i].bo_index].user_priv;
810 uint32_t cmd;
811
812 cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
813 cmd |= 0x20000000;
814 if (unlikely(cmd != req->suffix0)) {
815 if (!nvbo->kmap.virtual) {
816 ret = ttm_bo_kmap(&nvbo->bo, 0,
817 nvbo->bo.mem.
818 num_pages,
819 &nvbo->kmap);
820 if (ret) {
821 WIND_RING(chan);
822 goto out;
823 }
824 nvbo->validate_mapped = true;
825 }
826
827 nouveau_bo_wr32(nvbo, (push[i].offset +
828 push[i].length - 8) / 4, cmd);
829 }
830
831 OUT_RING(chan, 0x20000000 |
832 (nvbo->bo.offset + push[i].offset));
833 OUT_RING(chan, 0);
834 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
835 OUT_RING(chan, 0);
836 }
837 }
838
839 ret = nouveau_fence_new(chan, false, &fence);
840 if (ret) {
841 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
842 WIND_RING(chan);
843 goto out;
844 }
845
846 out:
847 validate_fini(&op, fence, bo);
848 nouveau_fence_unref(&fence);
849
850 out_prevalid:
851 u_free(bo);
852 u_free(push);
853
854 out_next:
855 if (chan->dma.ib_max) {
856 req->suffix0 = 0x00000000;
857 req->suffix1 = 0x00000000;
858 } else
859 if (drm->client.device.info.chipset >= 0x25) {
860 req->suffix0 = 0x00020000;
861 req->suffix1 = 0x00000000;
862 } else {
863 req->suffix0 = 0x20000000 |
864 (chan->push.addr + ((chan->dma.cur + 2) << 2));
865 req->suffix1 = 0x00000000;
866 }
867
868 return nouveau_abi16_put(abi16, ret);
869 }
870
871 int
872 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
873 struct drm_file *file_priv)
874 {
875 struct drm_nouveau_gem_cpu_prep *req = data;
876 struct drm_gem_object *gem;
877 struct nouveau_bo *nvbo;
878 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
879 bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
880 long lret;
881 int ret;
882
883 gem = drm_gem_object_lookup(file_priv, req->handle);
884 if (!gem)
885 return -ENOENT;
886 nvbo = nouveau_gem_object(gem);
887
888 lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
889 no_wait ? 0 : 30 * HZ);
890 if (!lret)
891 ret = -EBUSY;
892 else if (lret > 0)
893 ret = 0;
894 else
895 ret = lret;
896
897 nouveau_bo_sync_for_cpu(nvbo);
898 drm_gem_object_unreference_unlocked(gem);
899
900 return ret;
901 }
902
903 int
904 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
905 struct drm_file *file_priv)
906 {
907 struct drm_nouveau_gem_cpu_fini *req = data;
908 struct drm_gem_object *gem;
909 struct nouveau_bo *nvbo;
910
911 gem = drm_gem_object_lookup(file_priv, req->handle);
912 if (!gem)
913 return -ENOENT;
914 nvbo = nouveau_gem_object(gem);
915
916 nouveau_bo_sync_for_device(nvbo);
917 drm_gem_object_unreference_unlocked(gem);
918 return 0;
919 }
920
921 int
922 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
923 struct drm_file *file_priv)
924 {
925 struct drm_nouveau_gem_info *req = data;
926 struct drm_gem_object *gem;
927 int ret;
928
929 gem = drm_gem_object_lookup(file_priv, req->handle);
930 if (!gem)
931 return -ENOENT;
932
933 ret = nouveau_gem_info(file_priv, gem, req);
934 drm_gem_object_unreference_unlocked(gem);
935 return ret;
936 }
937