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