Use drm_gem_object_[handle_]unreference_unlocked where possible
[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 #include "drmP.h"
27 #include "drm.h"
28
29 #include "nouveau_drv.h"
30 #include "nouveau_drm.h"
31 #include "nouveau_dma.h"
32
33 #define nouveau_gem_pushbuf_sync(chan) 0
34
35 int
36 nouveau_gem_object_new(struct drm_gem_object *gem)
37 {
38 return 0;
39 }
40
41 void
42 nouveau_gem_object_del(struct drm_gem_object *gem)
43 {
44 struct nouveau_bo *nvbo = gem->driver_private;
45 struct ttm_buffer_object *bo = &nvbo->bo;
46
47 if (!nvbo)
48 return;
49 nvbo->gem = NULL;
50
51 if (unlikely(nvbo->cpu_filp))
52 ttm_bo_synccpu_write_release(bo);
53
54 if (unlikely(nvbo->pin_refcnt)) {
55 nvbo->pin_refcnt = 1;
56 nouveau_bo_unpin(nvbo);
57 }
58
59 ttm_bo_unref(&bo);
60 }
61
62 int
63 nouveau_gem_new(struct drm_device *dev, struct nouveau_channel *chan,
64 int size, int align, uint32_t flags, uint32_t tile_mode,
65 uint32_t tile_flags, bool no_vm, bool mappable,
66 struct nouveau_bo **pnvbo)
67 {
68 struct nouveau_bo *nvbo;
69 int ret;
70
71 ret = nouveau_bo_new(dev, chan, size, align, flags, tile_mode,
72 tile_flags, no_vm, mappable, pnvbo);
73 if (ret)
74 return ret;
75 nvbo = *pnvbo;
76
77 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
78 if (!nvbo->gem) {
79 nouveau_bo_ref(NULL, pnvbo);
80 return -ENOMEM;
81 }
82
83 nvbo->bo.persistant_swap_storage = nvbo->gem->filp;
84 nvbo->gem->driver_private = nvbo;
85 return 0;
86 }
87
88 static int
89 nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep)
90 {
91 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
92
93 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
94 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
95 else
96 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
97
98 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
99 rep->offset = nvbo->bo.offset;
100 rep->map_handle = nvbo->mappable ? nvbo->bo.addr_space_offset : 0;
101 rep->tile_mode = nvbo->tile_mode;
102 rep->tile_flags = nvbo->tile_flags;
103 return 0;
104 }
105
106 static bool
107 nouveau_gem_tile_flags_valid(struct drm_device *dev, uint32_t tile_flags) {
108 switch (tile_flags) {
109 case 0x0000:
110 case 0x1800:
111 case 0x2800:
112 case 0x4800:
113 case 0x7000:
114 case 0x7400:
115 case 0x7a00:
116 case 0xe000:
117 break;
118 default:
119 NV_ERROR(dev, "bad page flags: 0x%08x\n", tile_flags);
120 return false;
121 }
122
123 return true;
124 }
125
126 int
127 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
128 struct drm_file *file_priv)
129 {
130 struct drm_nouveau_private *dev_priv = dev->dev_private;
131 struct drm_nouveau_gem_new *req = data;
132 struct nouveau_bo *nvbo = NULL;
133 struct nouveau_channel *chan = NULL;
134 uint32_t flags = 0;
135 int ret = 0;
136
137 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
138
139 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
140 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
141
142 if (req->channel_hint) {
143 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel_hint,
144 file_priv, chan);
145 }
146
147 if (req->info.domain & NOUVEAU_GEM_DOMAIN_VRAM)
148 flags |= TTM_PL_FLAG_VRAM;
149 if (req->info.domain & NOUVEAU_GEM_DOMAIN_GART)
150 flags |= TTM_PL_FLAG_TT;
151 if (!flags || req->info.domain & NOUVEAU_GEM_DOMAIN_CPU)
152 flags |= TTM_PL_FLAG_SYSTEM;
153
154 if (!nouveau_gem_tile_flags_valid(dev, req->info.tile_flags))
155 return -EINVAL;
156
157 ret = nouveau_gem_new(dev, chan, req->info.size, req->align, flags,
158 req->info.tile_mode, req->info.tile_flags, false,
159 (req->info.domain & NOUVEAU_GEM_DOMAIN_MAPPABLE),
160 &nvbo);
161 if (ret)
162 return ret;
163
164 ret = nouveau_gem_info(nvbo->gem, &req->info);
165 if (ret)
166 goto out;
167
168 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
169 out:
170 drm_gem_object_handle_unreference_unlocked(nvbo->gem);
171
172 if (ret)
173 drm_gem_object_unreference_unlocked(nvbo->gem);
174 return ret;
175 }
176
177 static int
178 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
179 uint32_t write_domains, uint32_t valid_domains)
180 {
181 struct nouveau_bo *nvbo = gem->driver_private;
182 struct ttm_buffer_object *bo = &nvbo->bo;
183 uint64_t flags;
184
185 if (!valid_domains || (!read_domains && !write_domains))
186 return -EINVAL;
187
188 if (write_domains) {
189 if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
190 (write_domains & NOUVEAU_GEM_DOMAIN_VRAM))
191 flags = TTM_PL_FLAG_VRAM;
192 else
193 if ((valid_domains & NOUVEAU_GEM_DOMAIN_GART) &&
194 (write_domains & NOUVEAU_GEM_DOMAIN_GART))
195 flags = TTM_PL_FLAG_TT;
196 else
197 return -EINVAL;
198 } else {
199 if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
200 (read_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
201 bo->mem.mem_type == TTM_PL_VRAM)
202 flags = TTM_PL_FLAG_VRAM;
203 else
204 if ((valid_domains & NOUVEAU_GEM_DOMAIN_GART) &&
205 (read_domains & NOUVEAU_GEM_DOMAIN_GART) &&
206 bo->mem.mem_type == TTM_PL_TT)
207 flags = TTM_PL_FLAG_TT;
208 else
209 if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
210 (read_domains & NOUVEAU_GEM_DOMAIN_VRAM))
211 flags = TTM_PL_FLAG_VRAM;
212 else
213 flags = TTM_PL_FLAG_TT;
214 }
215
216 nouveau_bo_placement_set(nvbo, flags);
217 return 0;
218 }
219
220 struct validate_op {
221 struct list_head vram_list;
222 struct list_head gart_list;
223 struct list_head both_list;
224 };
225
226 static void
227 validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
228 {
229 struct list_head *entry, *tmp;
230 struct nouveau_bo *nvbo;
231
232 list_for_each_safe(entry, tmp, list) {
233 nvbo = list_entry(entry, struct nouveau_bo, entry);
234 if (likely(fence)) {
235 struct nouveau_fence *prev_fence;
236
237 spin_lock(&nvbo->bo.lock);
238 prev_fence = nvbo->bo.sync_obj;
239 nvbo->bo.sync_obj = nouveau_fence_ref(fence);
240 spin_unlock(&nvbo->bo.lock);
241 nouveau_fence_unref((void *)&prev_fence);
242 }
243
244 list_del(&nvbo->entry);
245 nvbo->reserved_by = NULL;
246 ttm_bo_unreserve(&nvbo->bo);
247 drm_gem_object_unreference(nvbo->gem);
248 }
249 }
250
251 static void
252 validate_fini(struct validate_op *op, struct nouveau_fence* fence)
253 {
254 validate_fini_list(&op->vram_list, fence);
255 validate_fini_list(&op->gart_list, fence);
256 validate_fini_list(&op->both_list, fence);
257 }
258
259 static int
260 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
261 struct drm_nouveau_gem_pushbuf_bo *pbbo,
262 int nr_buffers, struct validate_op *op)
263 {
264 struct drm_device *dev = chan->dev;
265 struct drm_nouveau_private *dev_priv = dev->dev_private;
266 uint32_t sequence;
267 int trycnt = 0;
268 int ret, i;
269
270 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
271 retry:
272 if (++trycnt > 100000) {
273 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
274 return -EINVAL;
275 }
276
277 for (i = 0; i < nr_buffers; i++) {
278 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
279 struct drm_gem_object *gem;
280 struct nouveau_bo *nvbo;
281
282 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
283 if (!gem) {
284 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
285 validate_fini(op, NULL);
286 return -EINVAL;
287 }
288 nvbo = gem->driver_private;
289
290 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
291 NV_ERROR(dev, "multiple instances of buffer %d on "
292 "validation list\n", b->handle);
293 validate_fini(op, NULL);
294 return -EINVAL;
295 }
296
297 ret = ttm_bo_reserve(&nvbo->bo, false, false, true, sequence);
298 if (ret) {
299 validate_fini(op, NULL);
300 if (ret == -EAGAIN)
301 ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
302 drm_gem_object_unreference(gem);
303 if (ret)
304 return ret;
305 goto retry;
306 }
307
308 nvbo->reserved_by = file_priv;
309 nvbo->pbbo_index = i;
310 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
311 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
312 list_add_tail(&nvbo->entry, &op->both_list);
313 else
314 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
315 list_add_tail(&nvbo->entry, &op->vram_list);
316 else
317 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
318 list_add_tail(&nvbo->entry, &op->gart_list);
319 else {
320 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
321 b->valid_domains);
322 list_add_tail(&nvbo->entry, &op->both_list);
323 validate_fini(op, NULL);
324 return -EINVAL;
325 }
326
327 if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) {
328 validate_fini(op, NULL);
329
330 if (nvbo->cpu_filp == file_priv) {
331 NV_ERROR(dev, "bo %p mapped by process trying "
332 "to validate it!\n", nvbo);
333 return -EINVAL;
334 }
335
336 ret = ttm_bo_wait_cpu(&nvbo->bo, false);
337 if (ret)
338 return ret;
339 goto retry;
340 }
341 }
342
343 return 0;
344 }
345
346 static int
347 validate_list(struct nouveau_channel *chan, struct list_head *list,
348 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
349 {
350 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
351 (void __force __user *)(uintptr_t)user_pbbo_ptr;
352 struct nouveau_bo *nvbo;
353 int ret, relocs = 0;
354
355 list_for_each_entry(nvbo, list, entry) {
356 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
357 struct nouveau_fence *prev_fence = nvbo->bo.sync_obj;
358
359 if (prev_fence && nouveau_fence_channel(prev_fence) != chan) {
360 spin_lock(&nvbo->bo.lock);
361 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
362 spin_unlock(&nvbo->bo.lock);
363 if (unlikely(ret))
364 return ret;
365 }
366
367 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
368 b->write_domains,
369 b->valid_domains);
370 if (unlikely(ret))
371 return ret;
372
373 nvbo->channel = chan;
374 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
375 false, false);
376 nvbo->channel = NULL;
377 if (unlikely(ret))
378 return ret;
379
380 if (nvbo->bo.offset == b->presumed_offset &&
381 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
382 b->presumed_domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
383 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
384 b->presumed_domain & NOUVEAU_GEM_DOMAIN_GART)))
385 continue;
386
387 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
388 b->presumed_domain = NOUVEAU_GEM_DOMAIN_GART;
389 else
390 b->presumed_domain = NOUVEAU_GEM_DOMAIN_VRAM;
391 b->presumed_offset = nvbo->bo.offset;
392 b->presumed_ok = 0;
393 relocs++;
394
395 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index], b, sizeof(*b)))
396 return -EFAULT;
397 }
398
399 return relocs;
400 }
401
402 static int
403 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
404 struct drm_file *file_priv,
405 struct drm_nouveau_gem_pushbuf_bo *pbbo,
406 uint64_t user_buffers, int nr_buffers,
407 struct validate_op *op, int *apply_relocs)
408 {
409 int ret, relocs = 0;
410
411 INIT_LIST_HEAD(&op->vram_list);
412 INIT_LIST_HEAD(&op->gart_list);
413 INIT_LIST_HEAD(&op->both_list);
414
415 if (nr_buffers == 0)
416 return 0;
417
418 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
419 if (unlikely(ret))
420 return ret;
421
422 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
423 if (unlikely(ret < 0)) {
424 validate_fini(op, NULL);
425 return ret;
426 }
427 relocs += ret;
428
429 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
430 if (unlikely(ret < 0)) {
431 validate_fini(op, NULL);
432 return ret;
433 }
434 relocs += ret;
435
436 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
437 if (unlikely(ret < 0)) {
438 validate_fini(op, NULL);
439 return ret;
440 }
441 relocs += ret;
442
443 *apply_relocs = relocs;
444 return 0;
445 }
446
447 static inline void *
448 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
449 {
450 void *mem;
451 void __user *userptr = (void __force __user *)(uintptr_t)user;
452
453 mem = kmalloc(nmemb * size, GFP_KERNEL);
454 if (!mem)
455 return ERR_PTR(-ENOMEM);
456
457 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
458 kfree(mem);
459 return ERR_PTR(-EFAULT);
460 }
461
462 return mem;
463 }
464
465 static int
466 nouveau_gem_pushbuf_reloc_apply(struct nouveau_channel *chan, int nr_bo,
467 struct drm_nouveau_gem_pushbuf_bo *bo,
468 unsigned nr_relocs, uint64_t ptr_relocs,
469 unsigned nr_dwords, unsigned first_dword,
470 uint32_t *pushbuf, bool is_iomem)
471 {
472 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
473 struct drm_device *dev = chan->dev;
474 int ret = 0;
475 unsigned i;
476
477 reloc = u_memcpya(ptr_relocs, nr_relocs, sizeof(*reloc));
478 if (IS_ERR(reloc))
479 return PTR_ERR(reloc);
480
481 for (i = 0; i < nr_relocs; i++) {
482 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
483 struct drm_nouveau_gem_pushbuf_bo *b;
484 uint32_t data;
485
486 if (r->bo_index >= nr_bo || r->reloc_index < first_dword ||
487 r->reloc_index >= first_dword + nr_dwords) {
488 NV_ERROR(dev, "Bad relocation %d\n", i);
489 NV_ERROR(dev, " bo: %d max %d\n", r->bo_index, nr_bo);
490 NV_ERROR(dev, " id: %d max %d\n", r->reloc_index, nr_dwords);
491 ret = -EINVAL;
492 break;
493 }
494
495 b = &bo[r->bo_index];
496 if (b->presumed_ok)
497 continue;
498
499 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
500 data = b->presumed_offset + r->data;
501 else
502 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
503 data = (b->presumed_offset + r->data) >> 32;
504 else
505 data = r->data;
506
507 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
508 if (b->presumed_domain == NOUVEAU_GEM_DOMAIN_GART)
509 data |= r->tor;
510 else
511 data |= r->vor;
512 }
513
514 if (is_iomem)
515 iowrite32_native(data, (void __force __iomem *)
516 &pushbuf[r->reloc_index]);
517 else
518 pushbuf[r->reloc_index] = data;
519 }
520
521 kfree(reloc);
522 return ret;
523 }
524
525 int
526 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
527 struct drm_file *file_priv)
528 {
529 struct drm_nouveau_gem_pushbuf *req = data;
530 struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
531 struct nouveau_channel *chan;
532 struct validate_op op;
533 struct nouveau_fence* fence = 0;
534 uint32_t *pushbuf = NULL;
535 int ret = 0, do_reloc = 0, i;
536
537 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
538 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
539
540 if (req->nr_dwords >= chan->dma.max ||
541 req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
542 req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
543 NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
544 NV_ERROR(dev, " dwords : %d max %d\n", req->nr_dwords,
545 chan->dma.max - 1);
546 NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
547 NOUVEAU_GEM_MAX_BUFFERS);
548 NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
549 NOUVEAU_GEM_MAX_RELOCS);
550 return -EINVAL;
551 }
552
553 pushbuf = u_memcpya(req->dwords, req->nr_dwords, sizeof(uint32_t));
554 if (IS_ERR(pushbuf))
555 return PTR_ERR(pushbuf);
556
557 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
558 if (IS_ERR(bo)) {
559 kfree(pushbuf);
560 return PTR_ERR(bo);
561 }
562
563 mutex_lock(&dev->struct_mutex);
564
565 /* Validate buffer list */
566 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
567 req->nr_buffers, &op, &do_reloc);
568 if (ret)
569 goto out;
570
571 /* Apply any relocations that are required */
572 if (do_reloc) {
573 ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers,
574 bo, req->nr_relocs,
575 req->relocs,
576 req->nr_dwords, 0,
577 pushbuf, false);
578 if (ret)
579 goto out;
580 }
581
582 /* Emit push buffer to the hw
583 */
584 ret = RING_SPACE(chan, req->nr_dwords);
585 if (ret)
586 goto out;
587
588 OUT_RINGp(chan, pushbuf, req->nr_dwords);
589
590 ret = nouveau_fence_new(chan, &fence, true);
591 if (ret) {
592 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
593 WIND_RING(chan);
594 goto out;
595 }
596
597 if (nouveau_gem_pushbuf_sync(chan)) {
598 ret = nouveau_fence_wait(fence, NULL, false, false);
599 if (ret) {
600 for (i = 0; i < req->nr_dwords; i++)
601 NV_ERROR(dev, "0x%08x\n", pushbuf[i]);
602 NV_ERROR(dev, "^^ above push buffer is fail :(\n");
603 }
604 }
605
606 out:
607 validate_fini(&op, fence);
608 nouveau_fence_unref((void**)&fence);
609 mutex_unlock(&dev->struct_mutex);
610 kfree(pushbuf);
611 kfree(bo);
612 return ret;
613 }
614
615 #define PUSHBUF_CAL (dev_priv->card_type >= NV_20)
616
617 int
618 nouveau_gem_ioctl_pushbuf_call(struct drm_device *dev, void *data,
619 struct drm_file *file_priv)
620 {
621 struct drm_nouveau_private *dev_priv = dev->dev_private;
622 struct drm_nouveau_gem_pushbuf_call *req = data;
623 struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
624 struct nouveau_channel *chan;
625 struct drm_gem_object *gem;
626 struct nouveau_bo *pbbo;
627 struct validate_op op;
628 struct nouveau_fence* fence = 0;
629 int i, ret = 0, do_reloc = 0;
630
631 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
632 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
633
634 if (unlikely(req->handle == 0))
635 goto out_next;
636
637 if (req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
638 req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
639 NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
640 NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
641 NOUVEAU_GEM_MAX_BUFFERS);
642 NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
643 NOUVEAU_GEM_MAX_RELOCS);
644 return -EINVAL;
645 }
646
647 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
648 if (IS_ERR(bo))
649 return PTR_ERR(bo);
650
651 mutex_lock(&dev->struct_mutex);
652
653 /* Validate buffer list */
654 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
655 req->nr_buffers, &op, &do_reloc);
656 if (ret) {
657 NV_ERROR(dev, "validate: %d\n", ret);
658 goto out;
659 }
660
661 /* Validate DMA push buffer */
662 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
663 if (!gem) {
664 NV_ERROR(dev, "Unknown pb handle 0x%08x\n", req->handle);
665 ret = -EINVAL;
666 goto out;
667 }
668 pbbo = nouveau_gem_object(gem);
669
670 if ((req->offset & 3) || req->nr_dwords < 2 ||
671 (unsigned long)req->offset > (unsigned long)pbbo->bo.mem.size ||
672 (unsigned long)req->nr_dwords >
673 ((unsigned long)(pbbo->bo.mem.size - req->offset ) >> 2)) {
674 NV_ERROR(dev, "pb call misaligned or out of bounds: "
675 "%d + %d * 4 > %ld\n",
676 req->offset, req->nr_dwords, pbbo->bo.mem.size);
677 ret = -EINVAL;
678 drm_gem_object_unreference(gem);
679 goto out;
680 }
681
682 ret = ttm_bo_reserve(&pbbo->bo, false, false, true,
683 chan->fence.sequence);
684 if (ret) {
685 NV_ERROR(dev, "resv pb: %d\n", ret);
686 drm_gem_object_unreference(gem);
687 goto out;
688 }
689
690 nouveau_bo_placement_set(pbbo, 1 << chan->pushbuf_bo->bo.mem.mem_type);
691 ret = ttm_bo_validate(&pbbo->bo, &pbbo->placement, false, false);
692 if (ret) {
693 NV_ERROR(dev, "validate pb: %d\n", ret);
694 ttm_bo_unreserve(&pbbo->bo);
695 drm_gem_object_unreference(gem);
696 goto out;
697 }
698
699 list_add_tail(&pbbo->entry, &op.both_list);
700
701 /* If presumed return address doesn't match, we need to map the
702 * push buffer and fix it..
703 */
704 if (!PUSHBUF_CAL) {
705 uint32_t retaddy;
706
707 if (chan->dma.free < 4 + NOUVEAU_DMA_SKIPS) {
708 ret = nouveau_dma_wait(chan, 4 + NOUVEAU_DMA_SKIPS);
709 if (ret) {
710 NV_ERROR(dev, "jmp_space: %d\n", ret);
711 goto out;
712 }
713 }
714
715 retaddy = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
716 retaddy |= 0x20000000;
717 if (retaddy != req->suffix0) {
718 req->suffix0 = retaddy;
719 do_reloc = 1;
720 }
721 }
722
723 /* Apply any relocations that are required */
724 if (do_reloc) {
725 void *pbvirt;
726 bool is_iomem;
727 ret = ttm_bo_kmap(&pbbo->bo, 0, pbbo->bo.mem.num_pages,
728 &pbbo->kmap);
729 if (ret) {
730 NV_ERROR(dev, "kmap pb: %d\n", ret);
731 goto out;
732 }
733
734 pbvirt = ttm_kmap_obj_virtual(&pbbo->kmap, &is_iomem);
735 ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers, bo,
736 req->nr_relocs,
737 req->relocs,
738 req->nr_dwords,
739 req->offset / 4,
740 pbvirt, is_iomem);
741
742 if (!PUSHBUF_CAL) {
743 nouveau_bo_wr32(pbbo,
744 req->offset / 4 + req->nr_dwords - 2,
745 req->suffix0);
746 }
747
748 ttm_bo_kunmap(&pbbo->kmap);
749 if (ret) {
750 NV_ERROR(dev, "reloc apply: %d\n", ret);
751 goto out;
752 }
753 }
754
755 if (PUSHBUF_CAL) {
756 ret = RING_SPACE(chan, 2);
757 if (ret) {
758 NV_ERROR(dev, "cal_space: %d\n", ret);
759 goto out;
760 }
761 OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
762 req->offset) | 2);
763 OUT_RING(chan, 0);
764 } else {
765 ret = RING_SPACE(chan, 2 + NOUVEAU_DMA_SKIPS);
766 if (ret) {
767 NV_ERROR(dev, "jmp_space: %d\n", ret);
768 goto out;
769 }
770 OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
771 req->offset) | 0x20000000);
772 OUT_RING(chan, 0);
773
774 /* Space the jumps apart with NOPs. */
775 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
776 OUT_RING(chan, 0);
777 }
778
779 ret = nouveau_fence_new(chan, &fence, true);
780 if (ret) {
781 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
782 WIND_RING(chan);
783 goto out;
784 }
785
786 out:
787 validate_fini(&op, fence);
788 nouveau_fence_unref((void**)&fence);
789 mutex_unlock(&dev->struct_mutex);
790 kfree(bo);
791
792 out_next:
793 if (PUSHBUF_CAL) {
794 req->suffix0 = 0x00020000;
795 req->suffix1 = 0x00000000;
796 } else {
797 req->suffix0 = 0x20000000 |
798 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
799 req->suffix1 = 0x00000000;
800 }
801
802 return ret;
803 }
804
805 int
806 nouveau_gem_ioctl_pushbuf_call2(struct drm_device *dev, void *data,
807 struct drm_file *file_priv)
808 {
809 struct drm_nouveau_private *dev_priv = dev->dev_private;
810 struct drm_nouveau_gem_pushbuf_call *req = data;
811
812 req->vram_available = dev_priv->fb_aper_free;
813 req->gart_available = dev_priv->gart_info.aper_free;
814
815 return nouveau_gem_ioctl_pushbuf_call(dev, data, file_priv);
816 }
817
818 static inline uint32_t
819 domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
820 {
821 uint32_t flags = 0;
822
823 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
824 flags |= TTM_PL_FLAG_VRAM;
825 if (domain & NOUVEAU_GEM_DOMAIN_GART)
826 flags |= TTM_PL_FLAG_TT;
827
828 return flags;
829 }
830
831 int
832 nouveau_gem_ioctl_pin(struct drm_device *dev, void *data,
833 struct drm_file *file_priv)
834 {
835 struct drm_nouveau_gem_pin *req = data;
836 struct drm_gem_object *gem;
837 struct nouveau_bo *nvbo;
838 int ret = 0;
839
840 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
841
842 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
843 NV_ERROR(dev, "pin only allowed without kernel modesetting\n");
844 return -EINVAL;
845 }
846
847 if (!DRM_SUSER(DRM_CURPROC))
848 return -EPERM;
849
850 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
851 if (!gem)
852 return -EINVAL;
853 nvbo = nouveau_gem_object(gem);
854
855 ret = nouveau_bo_pin(nvbo, domain_to_ttm(nvbo, req->domain));
856 if (ret)
857 goto out;
858
859 req->offset = nvbo->bo.offset;
860 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
861 req->domain = NOUVEAU_GEM_DOMAIN_GART;
862 else
863 req->domain = NOUVEAU_GEM_DOMAIN_VRAM;
864
865 out:
866 drm_gem_object_unreference_unlocked(gem);
867
868 return ret;
869 }
870
871 int
872 nouveau_gem_ioctl_unpin(struct drm_device *dev, void *data,
873 struct drm_file *file_priv)
874 {
875 struct drm_nouveau_gem_pin *req = data;
876 struct drm_gem_object *gem;
877 int ret;
878
879 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
880
881 if (drm_core_check_feature(dev, DRIVER_MODESET))
882 return -EINVAL;
883
884 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
885 if (!gem)
886 return -EINVAL;
887
888 ret = nouveau_bo_unpin(nouveau_gem_object(gem));
889
890 drm_gem_object_unreference_unlocked(gem);
891
892 return ret;
893 }
894
895 int
896 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
897 struct drm_file *file_priv)
898 {
899 struct drm_nouveau_gem_cpu_prep *req = data;
900 struct drm_gem_object *gem;
901 struct nouveau_bo *nvbo;
902 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
903 int ret = -EINVAL;
904
905 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
906
907 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
908 if (!gem)
909 return ret;
910 nvbo = nouveau_gem_object(gem);
911
912 if (nvbo->cpu_filp) {
913 if (nvbo->cpu_filp == file_priv)
914 goto out;
915
916 ret = ttm_bo_wait_cpu(&nvbo->bo, no_wait);
917 if (ret)
918 goto out;
919 }
920
921 if (req->flags & NOUVEAU_GEM_CPU_PREP_NOBLOCK) {
922 spin_lock(&nvbo->bo.lock);
923 ret = ttm_bo_wait(&nvbo->bo, false, false, no_wait);
924 spin_unlock(&nvbo->bo.lock);
925 } else {
926 ret = ttm_bo_synccpu_write_grab(&nvbo->bo, no_wait);
927 if (ret == 0)
928 nvbo->cpu_filp = file_priv;
929 }
930
931 out:
932 drm_gem_object_unreference_unlocked(gem);
933 return ret;
934 }
935
936 int
937 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
938 struct drm_file *file_priv)
939 {
940 struct drm_nouveau_gem_cpu_prep *req = data;
941 struct drm_gem_object *gem;
942 struct nouveau_bo *nvbo;
943 int ret = -EINVAL;
944
945 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
946
947 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
948 if (!gem)
949 return ret;
950 nvbo = nouveau_gem_object(gem);
951
952 if (nvbo->cpu_filp != file_priv)
953 goto out;
954 nvbo->cpu_filp = NULL;
955
956 ttm_bo_synccpu_write_release(&nvbo->bo);
957 ret = 0;
958
959 out:
960 drm_gem_object_unreference_unlocked(gem);
961 return ret;
962 }
963
964 int
965 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
966 struct drm_file *file_priv)
967 {
968 struct drm_nouveau_gem_info *req = data;
969 struct drm_gem_object *gem;
970 int ret;
971
972 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
973
974 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
975 if (!gem)
976 return -EINVAL;
977
978 ret = nouveau_gem_info(gem, req);
979 drm_gem_object_unreference_unlocked(gem);
980 return ret;
981 }
982
This page took 0.081295 seconds and 5 git commands to generate.