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