drm/nouveau/gem: fix object reference leak in a failure path
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_gem.c
CommitLineData
6ee73861
BS
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 */
22b33e8e 26#include <linux/dma-buf.h>
6ee73861
BS
27#include "drmP.h"
28#include "drm.h"
29
30#include "nouveau_drv.h"
31#include "nouveau_drm.h"
32#include "nouveau_dma.h"
d375e7d5 33#include "nouveau_fence.h"
6ee73861
BS
34
35#define nouveau_gem_pushbuf_sync(chan) 0
36
37int
38nouveau_gem_object_new(struct drm_gem_object *gem)
39{
40 return 0;
41}
42
43void
44nouveau_gem_object_del(struct drm_gem_object *gem)
45{
46 struct nouveau_bo *nvbo = gem->driver_private;
47 struct ttm_buffer_object *bo = &nvbo->bo;
48
49 if (!nvbo)
50 return;
51 nvbo->gem = NULL;
52
6ee73861
BS
53 if (unlikely(nvbo->pin_refcnt)) {
54 nvbo->pin_refcnt = 1;
55 nouveau_bo_unpin(nvbo);
56 }
57
22b33e8e
DA
58 if (gem->import_attach)
59 drm_prime_gem_destroy(gem, nvbo->bo.sg);
60
6ee73861 61 ttm_bo_unref(&bo);
fd632aa3
DV
62
63 drm_gem_object_release(gem);
64 kfree(gem);
6ee73861
BS
65}
66
639212d0
BS
67int
68nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
69{
70 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
2fd3db6f
BS
71 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
72 struct nouveau_vma *vma;
73 int ret;
639212d0
BS
74
75 if (!fpriv->vm)
76 return 0;
77
2fd3db6f
BS
78 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
79 if (ret)
80 return ret;
81
82 vma = nouveau_bo_vma_find(nvbo, fpriv->vm);
83 if (!vma) {
84 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
85 if (!vma) {
86 ret = -ENOMEM;
87 goto out;
88 }
89
90 ret = nouveau_bo_vma_add(nvbo, fpriv->vm, vma);
91 if (ret) {
92 kfree(vma);
93 goto out;
94 }
95 } else {
96 vma->refcount++;
97 }
98
99out:
100 ttm_bo_unreserve(&nvbo->bo);
101 return ret;
639212d0
BS
102}
103
104void
105nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
106{
107 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
2fd3db6f
BS
108 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
109 struct nouveau_vma *vma;
110 int ret;
639212d0
BS
111
112 if (!fpriv->vm)
113 return;
2fd3db6f
BS
114
115 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
116 if (ret)
117 return;
118
119 vma = nouveau_bo_vma_find(nvbo, fpriv->vm);
120 if (vma) {
8fe198b2 121 if (--vma->refcount == 0) {
2fd3db6f 122 nouveau_bo_vma_del(nvbo, vma);
8fe198b2
MS
123 kfree(vma);
124 }
2fd3db6f
BS
125 }
126 ttm_bo_unreserve(&nvbo->bo);
639212d0
BS
127}
128
6ee73861 129int
f6d4e621
BS
130nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
131 uint32_t tile_mode, uint32_t tile_flags,
132 struct nouveau_bo **pnvbo)
6ee73861 133{
db5c8e29 134 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861 135 struct nouveau_bo *nvbo;
6ba9a683 136 u32 flags = 0;
6ee73861
BS
137 int ret;
138
6ba9a683
BS
139 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
140 flags |= TTM_PL_FLAG_VRAM;
141 if (domain & NOUVEAU_GEM_DOMAIN_GART)
142 flags |= TTM_PL_FLAG_TT;
143 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
144 flags |= TTM_PL_FLAG_SYSTEM;
145
7375c95b 146 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
22b33e8e 147 tile_flags, NULL, pnvbo);
6ee73861
BS
148 if (ret)
149 return ret;
150 nvbo = *pnvbo;
151
db5c8e29
BS
152 /* we restrict allowed domains on nv50+ to only the types
153 * that were requested at creation time. not possibly on
154 * earlier chips without busting the ABI.
155 */
156 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
157 NOUVEAU_GEM_DOMAIN_GART;
158 if (dev_priv->card_type >= NV_50)
159 nvbo->valid_domains &= domain;
160
6ee73861
BS
161 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
162 if (!nvbo->gem) {
163 nouveau_bo_ref(NULL, pnvbo);
164 return -ENOMEM;
165 }
166
5df23979 167 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
6ee73861
BS
168 nvbo->gem->driver_private = nvbo;
169 return 0;
170}
171
172static int
e758a311
BS
173nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
174 struct drm_nouveau_gem_info *rep)
6ee73861 175{
e758a311 176 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
6ee73861 177 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
e758a311 178 struct nouveau_vma *vma;
6ee73861
BS
179
180 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
181 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
182 else
183 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
184
e758a311
BS
185 rep->offset = nvbo->bo.offset;
186 if (fpriv->vm) {
187 vma = nouveau_bo_vma_find(nvbo, fpriv->vm);
188 if (!vma)
189 return -EINVAL;
190
191 rep->offset = vma->offset;
192 }
193
6ee73861 194 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
d550c41e 195 rep->map_handle = nvbo->bo.addr_space_offset;
6ee73861
BS
196 rep->tile_mode = nvbo->tile_mode;
197 rep->tile_flags = nvbo->tile_flags;
198 return 0;
199}
200
6ee73861
BS
201int
202nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
203 struct drm_file *file_priv)
204{
205 struct drm_nouveau_private *dev_priv = dev->dev_private;
206 struct drm_nouveau_gem_new *req = data;
207 struct nouveau_bo *nvbo = NULL;
6ee73861
BS
208 int ret = 0;
209
6ee73861
BS
210 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
211 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
212
60d2a88a
BS
213 if (!dev_priv->engine.vram.flags_valid(dev, req->info.tile_flags)) {
214 NV_ERROR(dev, "bad page flags: 0x%08x\n", req->info.tile_flags);
6ee73861 215 return -EINVAL;
60d2a88a 216 }
6ee73861 217
f6d4e621 218 ret = nouveau_gem_new(dev, req->info.size, req->align,
6ba9a683
BS
219 req->info.domain, req->info.tile_mode,
220 req->info.tile_flags, &nvbo);
6ee73861
BS
221 if (ret)
222 return ret;
223
6ee73861 224 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
e758a311
BS
225 if (ret == 0) {
226 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
227 if (ret)
228 drm_gem_handle_delete(file_priv, req->info.handle);
229 }
230
29d08b3e
DA
231 /* drop reference from allocate - handle holds it now */
232 drm_gem_object_unreference_unlocked(nvbo->gem);
6ee73861
BS
233 return ret;
234}
235
236static int
237nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
238 uint32_t write_domains, uint32_t valid_domains)
239{
240 struct nouveau_bo *nvbo = gem->driver_private;
241 struct ttm_buffer_object *bo = &nvbo->bo;
db5c8e29 242 uint32_t domains = valid_domains & nvbo->valid_domains &
78ad0f7b
FJ
243 (write_domains ? write_domains : read_domains);
244 uint32_t pref_flags = 0, valid_flags = 0;
6ee73861 245
78ad0f7b 246 if (!domains)
6ee73861
BS
247 return -EINVAL;
248
78ad0f7b
FJ
249 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
250 valid_flags |= TTM_PL_FLAG_VRAM;
251
252 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
253 valid_flags |= TTM_PL_FLAG_TT;
254
255 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
256 bo->mem.mem_type == TTM_PL_VRAM)
257 pref_flags |= TTM_PL_FLAG_VRAM;
258
259 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
260 bo->mem.mem_type == TTM_PL_TT)
261 pref_flags |= TTM_PL_FLAG_TT;
262
263 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
264 pref_flags |= TTM_PL_FLAG_VRAM;
265
266 else
267 pref_flags |= TTM_PL_FLAG_TT;
268
269 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
6ee73861 270
6ee73861
BS
271 return 0;
272}
273
274struct validate_op {
6ee73861
BS
275 struct list_head vram_list;
276 struct list_head gart_list;
277 struct list_head both_list;
278};
279
280static void
281validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
282{
283 struct list_head *entry, *tmp;
284 struct nouveau_bo *nvbo;
285
286 list_for_each_safe(entry, tmp, list) {
287 nvbo = list_entry(entry, struct nouveau_bo, entry);
332b242f
FJ
288
289 nouveau_bo_fence(nvbo, fence);
6ee73861 290
a1606a95
BS
291 if (unlikely(nvbo->validate_mapped)) {
292 ttm_bo_kunmap(&nvbo->kmap);
293 nvbo->validate_mapped = false;
294 }
295
6ee73861
BS
296 list_del(&nvbo->entry);
297 nvbo->reserved_by = NULL;
298 ttm_bo_unreserve(&nvbo->bo);
374c3af8 299 drm_gem_object_unreference_unlocked(nvbo->gem);
6ee73861
BS
300 }
301}
302
303static void
234896a7 304validate_fini(struct validate_op *op, struct nouveau_fence* fence)
6ee73861 305{
234896a7
LB
306 validate_fini_list(&op->vram_list, fence);
307 validate_fini_list(&op->gart_list, fence);
308 validate_fini_list(&op->both_list, fence);
6ee73861
BS
309}
310
311static int
312validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
313 struct drm_nouveau_gem_pushbuf_bo *pbbo,
314 int nr_buffers, struct validate_op *op)
315{
316 struct drm_device *dev = chan->dev;
317 struct drm_nouveau_private *dev_priv = dev->dev_private;
318 uint32_t sequence;
319 int trycnt = 0;
320 int ret, i;
321
322 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
323retry:
324 if (++trycnt > 100000) {
325 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
326 return -EINVAL;
327 }
328
329 for (i = 0; i < nr_buffers; i++) {
330 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
331 struct drm_gem_object *gem;
332 struct nouveau_bo *nvbo;
333
334 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
335 if (!gem) {
336 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
337 validate_fini(op, NULL);
bf79cb91 338 return -ENOENT;
6ee73861
BS
339 }
340 nvbo = gem->driver_private;
341
342 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
343 NV_ERROR(dev, "multiple instances of buffer %d on "
344 "validation list\n", b->handle);
5086f69e 345 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
346 validate_fini(op, NULL);
347 return -EINVAL;
348 }
349
938c40ed 350 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
6ee73861
BS
351 if (ret) {
352 validate_fini(op, NULL);
938c40ed
BS
353 if (unlikely(ret == -EAGAIN))
354 ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
374c3af8 355 drm_gem_object_unreference_unlocked(gem);
938c40ed
BS
356 if (unlikely(ret)) {
357 if (ret != -ERESTARTSYS)
358 NV_ERROR(dev, "fail reserve\n");
6ee73861 359 return ret;
a1606a95 360 }
6ee73861
BS
361 goto retry;
362 }
363
a1606a95 364 b->user_priv = (uint64_t)(unsigned long)nvbo;
6ee73861
BS
365 nvbo->reserved_by = file_priv;
366 nvbo->pbbo_index = i;
367 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
368 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
369 list_add_tail(&nvbo->entry, &op->both_list);
370 else
371 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
372 list_add_tail(&nvbo->entry, &op->vram_list);
373 else
374 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
375 list_add_tail(&nvbo->entry, &op->gart_list);
376 else {
377 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
378 b->valid_domains);
0208843d 379 list_add_tail(&nvbo->entry, &op->both_list);
6ee73861
BS
380 validate_fini(op, NULL);
381 return -EINVAL;
382 }
6ee73861
BS
383 }
384
385 return 0;
386}
387
525895ba
BS
388static int
389validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
390{
391 struct nouveau_fence *fence = NULL;
392 int ret = 0;
393
394 spin_lock(&nvbo->bo.bdev->fence_lock);
395 if (nvbo->bo.sync_obj)
396 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
397 spin_unlock(&nvbo->bo.bdev->fence_lock);
398
399 if (fence) {
400 ret = nouveau_fence_sync(fence, chan);
401 nouveau_fence_unref(&fence);
402 }
403
404 return ret;
405}
406
6ee73861
BS
407static int
408validate_list(struct nouveau_channel *chan, struct list_head *list,
409 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
410{
a3fcd0a9 411 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
6ee73861
BS
412 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
413 (void __force __user *)(uintptr_t)user_pbbo_ptr;
a1606a95 414 struct drm_device *dev = chan->dev;
6ee73861
BS
415 struct nouveau_bo *nvbo;
416 int ret, relocs = 0;
417
418 list_for_each_entry(nvbo, list, entry) {
419 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
6ee73861 420
525895ba 421 ret = validate_sync(chan, nvbo);
415e6186
BS
422 if (unlikely(ret)) {
423 NV_ERROR(dev, "fail pre-validate sync\n");
424 return ret;
6ee73861
BS
425 }
426
427 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
428 b->write_domains,
429 b->valid_domains);
a1606a95
BS
430 if (unlikely(ret)) {
431 NV_ERROR(dev, "fail set_domain\n");
6ee73861 432 return ret;
a1606a95 433 }
6ee73861 434
7a45d764 435 ret = nouveau_bo_validate(nvbo, true, false, false);
a1606a95 436 if (unlikely(ret)) {
938c40ed
BS
437 if (ret != -ERESTARTSYS)
438 NV_ERROR(dev, "fail ttm_validate\n");
6ee73861 439 return ret;
a1606a95 440 }
6ee73861 441
525895ba 442 ret = validate_sync(chan, nvbo);
415e6186
BS
443 if (unlikely(ret)) {
444 NV_ERROR(dev, "fail post-validate sync\n");
445 return ret;
446 }
447
a3fcd0a9
BS
448 if (dev_priv->card_type < NV_50) {
449 if (nvbo->bo.offset == b->presumed.offset &&
450 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
451 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
452 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
453 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
454 continue;
455
456 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
457 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
458 else
459 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
460 b->presumed.offset = nvbo->bo.offset;
461 b->presumed.valid = 0;
462 relocs++;
463
464 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
465 &b->presumed, sizeof(b->presumed)))
466 return -EFAULT;
467 }
6ee73861
BS
468 }
469
470 return relocs;
471}
472
473static int
474nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
475 struct drm_file *file_priv,
476 struct drm_nouveau_gem_pushbuf_bo *pbbo,
477 uint64_t user_buffers, int nr_buffers,
478 struct validate_op *op, int *apply_relocs)
479{
a1606a95 480 struct drm_device *dev = chan->dev;
6ee73861
BS
481 int ret, relocs = 0;
482
483 INIT_LIST_HEAD(&op->vram_list);
484 INIT_LIST_HEAD(&op->gart_list);
485 INIT_LIST_HEAD(&op->both_list);
486
6ee73861
BS
487 if (nr_buffers == 0)
488 return 0;
489
490 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
a1606a95 491 if (unlikely(ret)) {
938c40ed
BS
492 if (ret != -ERESTARTSYS)
493 NV_ERROR(dev, "validate_init\n");
6ee73861 494 return ret;
a1606a95 495 }
6ee73861
BS
496
497 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
498 if (unlikely(ret < 0)) {
938c40ed
BS
499 if (ret != -ERESTARTSYS)
500 NV_ERROR(dev, "validate vram_list\n");
6ee73861
BS
501 validate_fini(op, NULL);
502 return ret;
503 }
504 relocs += ret;
505
506 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
507 if (unlikely(ret < 0)) {
938c40ed
BS
508 if (ret != -ERESTARTSYS)
509 NV_ERROR(dev, "validate gart_list\n");
6ee73861
BS
510 validate_fini(op, NULL);
511 return ret;
512 }
513 relocs += ret;
514
515 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
516 if (unlikely(ret < 0)) {
938c40ed
BS
517 if (ret != -ERESTARTSYS)
518 NV_ERROR(dev, "validate both_list\n");
6ee73861
BS
519 validate_fini(op, NULL);
520 return ret;
521 }
522 relocs += ret;
523
524 *apply_relocs = relocs;
525 return 0;
526}
527
528static inline void *
529u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
530{
531 void *mem;
532 void __user *userptr = (void __force __user *)(uintptr_t)user;
533
534 mem = kmalloc(nmemb * size, GFP_KERNEL);
535 if (!mem)
536 return ERR_PTR(-ENOMEM);
537
538 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
539 kfree(mem);
540 return ERR_PTR(-EFAULT);
541 }
542
543 return mem;
544}
545
546static int
a1606a95
BS
547nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
548 struct drm_nouveau_gem_pushbuf *req,
549 struct drm_nouveau_gem_pushbuf_bo *bo)
6ee73861
BS
550{
551 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
12f735b7
LB
552 int ret = 0;
553 unsigned i;
6ee73861 554
a1606a95 555 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
6ee73861
BS
556 if (IS_ERR(reloc))
557 return PTR_ERR(reloc);
558
a1606a95 559 for (i = 0; i < req->nr_relocs; i++) {
6ee73861
BS
560 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
561 struct drm_nouveau_gem_pushbuf_bo *b;
a1606a95 562 struct nouveau_bo *nvbo;
6ee73861
BS
563 uint32_t data;
564
a1606a95
BS
565 if (unlikely(r->bo_index > req->nr_buffers)) {
566 NV_ERROR(dev, "reloc bo index invalid\n");
6ee73861
BS
567 ret = -EINVAL;
568 break;
569 }
570
571 b = &bo[r->bo_index];
a1606a95 572 if (b->presumed.valid)
6ee73861
BS
573 continue;
574
a1606a95
BS
575 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
576 NV_ERROR(dev, "reloc container bo index invalid\n");
577 ret = -EINVAL;
578 break;
579 }
580 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
581
582 if (unlikely(r->reloc_bo_offset + 4 >
583 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
584 NV_ERROR(dev, "reloc outside of bo\n");
585 ret = -EINVAL;
586 break;
587 }
588
589 if (!nvbo->kmap.virtual) {
590 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
591 &nvbo->kmap);
592 if (ret) {
593 NV_ERROR(dev, "failed kmap for reloc\n");
594 break;
595 }
596 nvbo->validate_mapped = true;
597 }
598
6ee73861 599 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
a1606a95 600 data = b->presumed.offset + r->data;
6ee73861
BS
601 else
602 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
a1606a95 603 data = (b->presumed.offset + r->data) >> 32;
6ee73861
BS
604 else
605 data = r->data;
606
607 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
a1606a95 608 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
6ee73861
BS
609 data |= r->tor;
610 else
611 data |= r->vor;
612 }
613
702adba2 614 spin_lock(&nvbo->bo.bdev->fence_lock);
a1606a95 615 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
702adba2 616 spin_unlock(&nvbo->bo.bdev->fence_lock);
a1606a95
BS
617 if (ret) {
618 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
619 break;
620 }
a1606a95
BS
621
622 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
6ee73861
BS
623 }
624
625 kfree(reloc);
626 return ret;
627}
628
629int
630nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
631 struct drm_file *file_priv)
632{
a1606a95 633 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861 634 struct drm_nouveau_gem_pushbuf *req = data;
a1606a95
BS
635 struct drm_nouveau_gem_pushbuf_push *push;
636 struct drm_nouveau_gem_pushbuf_bo *bo;
6ee73861
BS
637 struct nouveau_channel *chan;
638 struct validate_op op;
6e86e041 639 struct nouveau_fence *fence = NULL;
a1606a95 640 int i, j, ret = 0, do_reloc = 0;
6ee73861 641
e8a863c1 642 chan = nouveau_channel_get(file_priv, req->channel);
cff5c133
BS
643 if (IS_ERR(chan))
644 return PTR_ERR(chan);
6ee73861 645
a1606a95
BS
646 req->vram_available = dev_priv->fb_aper_free;
647 req->gart_available = dev_priv->gart_info.aper_free;
648 if (unlikely(req->nr_push == 0))
649 goto out_next;
6ee73861 650
a1606a95
BS
651 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
652 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
653 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
cff5c133 654 nouveau_channel_put(&chan);
a1606a95 655 return -EINVAL;
6ee73861
BS
656 }
657
a1606a95
BS
658 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
659 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
660 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
cff5c133 661 nouveau_channel_put(&chan);
a1606a95 662 return -EINVAL;
6ee73861
BS
663 }
664
a1606a95
BS
665 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
666 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
667 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
cff5c133 668 nouveau_channel_put(&chan);
6ee73861
BS
669 return -EINVAL;
670 }
671
a1606a95 672 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
cff5c133
BS
673 if (IS_ERR(push)) {
674 nouveau_channel_put(&chan);
a1606a95 675 return PTR_ERR(push);
cff5c133 676 }
a1606a95 677
6ee73861 678 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
a1606a95
BS
679 if (IS_ERR(bo)) {
680 kfree(push);
cff5c133 681 nouveau_channel_put(&chan);
6ee73861 682 return PTR_ERR(bo);
a1606a95 683 }
6ee73861 684
accf9496 685 /* Ensure all push buffers are on validate list */
415e6186
BS
686 for (i = 0; i < req->nr_push; i++) {
687 if (push[i].bo_index >= req->nr_buffers) {
688 NV_ERROR(dev, "push %d buffer not in list\n", i);
689 ret = -EINVAL;
7fa0cba2 690 goto out_prevalid;
415e6186 691 }
415e6186
BS
692 }
693
6ee73861
BS
694 /* Validate buffer list */
695 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
696 req->nr_buffers, &op, &do_reloc);
697 if (ret) {
938c40ed
BS
698 if (ret != -ERESTARTSYS)
699 NV_ERROR(dev, "validate: %d\n", ret);
7fa0cba2 700 goto out_prevalid;
6ee73861
BS
701 }
702
6ee73861
BS
703 /* Apply any relocations that are required */
704 if (do_reloc) {
a1606a95 705 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
6ee73861 706 if (ret) {
6ee73861 707 NV_ERROR(dev, "reloc apply: %d\n", ret);
6ee73861
BS
708 goto out;
709 }
6ee73861 710 }
6ee73861 711
9a391ad8 712 if (chan->dma.ib_max) {
5e120f6e 713 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
6ee73861 714 if (ret) {
9a391ad8 715 NV_INFO(dev, "nv50cal_space: %d\n", ret);
6ee73861
BS
716 goto out;
717 }
6ee73861 718
a1606a95
BS
719 for (i = 0; i < req->nr_push; i++) {
720 struct nouveau_bo *nvbo = (void *)(unsigned long)
721 bo[push[i].bo_index].user_priv;
722
723 nv50_dma_push(chan, nvbo, push[i].offset,
724 push[i].length);
725 }
9a391ad8 726 } else
ee508b82 727 if (dev_priv->chipset >= 0x25) {
a1606a95 728 ret = RING_SPACE(chan, req->nr_push * 2);
6ee73861
BS
729 if (ret) {
730 NV_ERROR(dev, "cal_space: %d\n", ret);
731 goto out;
732 }
a1606a95
BS
733
734 for (i = 0; i < req->nr_push; i++) {
735 struct nouveau_bo *nvbo = (void *)(unsigned long)
736 bo[push[i].bo_index].user_priv;
737 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
738
739 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
740 push[i].offset) | 2);
741 OUT_RING(chan, 0);
742 }
6ee73861 743 } else {
a1606a95 744 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
6ee73861
BS
745 if (ret) {
746 NV_ERROR(dev, "jmp_space: %d\n", ret);
747 goto out;
748 }
6ee73861 749
a1606a95
BS
750 for (i = 0; i < req->nr_push; i++) {
751 struct nouveau_bo *nvbo = (void *)(unsigned long)
752 bo[push[i].bo_index].user_priv;
753 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
754 uint32_t cmd;
755
756 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
757 cmd |= 0x20000000;
758 if (unlikely(cmd != req->suffix0)) {
759 if (!nvbo->kmap.virtual) {
760 ret = ttm_bo_kmap(&nvbo->bo, 0,
761 nvbo->bo.mem.
762 num_pages,
763 &nvbo->kmap);
764 if (ret) {
765 WIND_RING(chan);
766 goto out;
767 }
768 nvbo->validate_mapped = true;
769 }
770
771 nouveau_bo_wr32(nvbo, (push[i].offset +
772 push[i].length - 8) / 4, cmd);
773 }
774
775 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
776 push[i].offset) | 0x20000000);
6ee73861 777 OUT_RING(chan, 0);
a1606a95
BS
778 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
779 OUT_RING(chan, 0);
780 }
6ee73861
BS
781 }
782
d375e7d5 783 ret = nouveau_fence_new(chan, &fence);
6ee73861
BS
784 if (ret) {
785 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
786 WIND_RING(chan);
787 goto out;
788 }
789
790out:
234896a7 791 validate_fini(&op, fence);
382d62e5 792 nouveau_fence_unref(&fence);
7fa0cba2
MS
793
794out_prevalid:
6ee73861 795 kfree(bo);
a1606a95 796 kfree(push);
6ee73861
BS
797
798out_next:
9a391ad8
BS
799 if (chan->dma.ib_max) {
800 req->suffix0 = 0x00000000;
801 req->suffix1 = 0x00000000;
802 } else
ee508b82 803 if (dev_priv->chipset >= 0x25) {
6ee73861
BS
804 req->suffix0 = 0x00020000;
805 req->suffix1 = 0x00000000;
806 } else {
807 req->suffix0 = 0x20000000 |
808 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
809 req->suffix1 = 0x00000000;
810 }
811
cff5c133 812 nouveau_channel_put(&chan);
6ee73861
BS
813 return ret;
814}
815
6ee73861
BS
816static inline uint32_t
817domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
818{
819 uint32_t flags = 0;
820
821 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
822 flags |= TTM_PL_FLAG_VRAM;
823 if (domain & NOUVEAU_GEM_DOMAIN_GART)
824 flags |= TTM_PL_FLAG_TT;
825
826 return flags;
827}
828
6ee73861
BS
829int
830nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
831 struct drm_file *file_priv)
832{
833 struct drm_nouveau_gem_cpu_prep *req = data;
834 struct drm_gem_object *gem;
835 struct nouveau_bo *nvbo;
836 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
837 int ret = -EINVAL;
838
6ee73861
BS
839 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
840 if (!gem)
bf79cb91 841 return -ENOENT;
6ee73861
BS
842 nvbo = nouveau_gem_object(gem);
843
21e86c1c
BS
844 spin_lock(&nvbo->bo.bdev->fence_lock);
845 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
846 spin_unlock(&nvbo->bo.bdev->fence_lock);
bc9025bd 847 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
848 return ret;
849}
850
851int
852nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
853 struct drm_file *file_priv)
854{
21e86c1c 855 return 0;
6ee73861
BS
856}
857
858int
859nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
860 struct drm_file *file_priv)
861{
862 struct drm_nouveau_gem_info *req = data;
863 struct drm_gem_object *gem;
864 int ret;
865
6ee73861
BS
866 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
867 if (!gem)
bf79cb91 868 return -ENOENT;
6ee73861 869
e758a311 870 ret = nouveau_gem_info(file_priv, gem, req);
bc9025bd 871 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
872 return ret;
873}
874
This page took 0.185388 seconds and 5 git commands to generate.