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