2 * Copyright (C) 2008 Ben Skeggs.
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:
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.
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.
29 #include "nouveau_drv.h"
30 #include "nouveau_drm.h"
31 #include "nouveau_dma.h"
33 #define nouveau_gem_pushbuf_sync(chan) 0
36 nouveau_gem_object_new(struct drm_gem_object *gem)
42 nouveau_gem_object_del(struct drm_gem_object *gem)
44 struct nouveau_bo *nvbo = gem->driver_private;
45 struct ttm_buffer_object *bo = &nvbo->bo;
51 if (unlikely(nvbo->pin_refcnt)) {
53 nouveau_bo_unpin(nvbo);
58 drm_gem_object_release(gem);
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)
68 struct nouveau_bo *nvbo;
71 ret = nouveau_bo_new(dev, chan, size, align, flags, tile_mode,
72 tile_flags, no_vm, mappable, pnvbo);
77 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
79 nouveau_bo_ref(NULL, pnvbo);
83 nvbo->bo.persistant_swap_storage = nvbo->gem->filp;
84 nvbo->gem->driver_private = nvbo;
89 nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep)
91 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
93 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
94 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
96 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
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;
107 nouveau_gem_tile_flags_valid(struct drm_device *dev, uint32_t tile_flags)
109 struct drm_nouveau_private *dev_priv = dev->dev_private;
111 if (dev_priv->card_type >= NV_50) {
112 switch (tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK) {
124 if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
128 NV_ERROR(dev, "bad page flags: 0x%08x\n", tile_flags);
133 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
134 struct drm_file *file_priv)
136 struct drm_nouveau_private *dev_priv = dev->dev_private;
137 struct drm_nouveau_gem_new *req = data;
138 struct nouveau_bo *nvbo = NULL;
139 struct nouveau_channel *chan = NULL;
143 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
144 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
146 if (req->info.domain & NOUVEAU_GEM_DOMAIN_VRAM)
147 flags |= TTM_PL_FLAG_VRAM;
148 if (req->info.domain & NOUVEAU_GEM_DOMAIN_GART)
149 flags |= TTM_PL_FLAG_TT;
150 if (!flags || req->info.domain & NOUVEAU_GEM_DOMAIN_CPU)
151 flags |= TTM_PL_FLAG_SYSTEM;
153 if (!nouveau_gem_tile_flags_valid(dev, req->info.tile_flags))
156 if (req->channel_hint) {
157 chan = nouveau_channel_get(dev, file_priv, req->channel_hint);
159 return PTR_ERR(chan);
162 ret = nouveau_gem_new(dev, chan, req->info.size, req->align, flags,
163 req->info.tile_mode, req->info.tile_flags, false,
164 (req->info.domain & NOUVEAU_GEM_DOMAIN_MAPPABLE),
167 nouveau_channel_put(&chan);
171 ret = nouveau_gem_info(nvbo->gem, &req->info);
175 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
176 /* drop reference from allocate - handle holds it now */
177 drm_gem_object_unreference_unlocked(nvbo->gem);
183 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
184 uint32_t write_domains, uint32_t valid_domains)
186 struct nouveau_bo *nvbo = gem->driver_private;
187 struct ttm_buffer_object *bo = &nvbo->bo;
188 uint32_t domains = valid_domains &
189 (write_domains ? write_domains : read_domains);
190 uint32_t pref_flags = 0, valid_flags = 0;
195 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
196 valid_flags |= TTM_PL_FLAG_VRAM;
198 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
199 valid_flags |= TTM_PL_FLAG_TT;
201 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
202 bo->mem.mem_type == TTM_PL_VRAM)
203 pref_flags |= TTM_PL_FLAG_VRAM;
205 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
206 bo->mem.mem_type == TTM_PL_TT)
207 pref_flags |= TTM_PL_FLAG_TT;
209 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
210 pref_flags |= TTM_PL_FLAG_VRAM;
213 pref_flags |= TTM_PL_FLAG_TT;
215 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
221 struct list_head vram_list;
222 struct list_head gart_list;
223 struct list_head both_list;
227 validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
229 struct list_head *entry, *tmp;
230 struct nouveau_bo *nvbo;
232 list_for_each_safe(entry, tmp, list) {
233 nvbo = list_entry(entry, struct nouveau_bo, entry);
235 nouveau_bo_fence(nvbo, fence);
237 if (unlikely(nvbo->validate_mapped)) {
238 ttm_bo_kunmap(&nvbo->kmap);
239 nvbo->validate_mapped = false;
242 list_del(&nvbo->entry);
243 nvbo->reserved_by = NULL;
244 ttm_bo_unreserve(&nvbo->bo);
245 drm_gem_object_unreference_unlocked(nvbo->gem);
250 validate_fini(struct validate_op *op, struct nouveau_fence* fence)
252 validate_fini_list(&op->vram_list, fence);
253 validate_fini_list(&op->gart_list, fence);
254 validate_fini_list(&op->both_list, fence);
258 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
259 struct drm_nouveau_gem_pushbuf_bo *pbbo,
260 int nr_buffers, struct validate_op *op)
262 struct drm_device *dev = chan->dev;
263 struct drm_nouveau_private *dev_priv = dev->dev_private;
268 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
270 if (++trycnt > 100000) {
271 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
275 for (i = 0; i < nr_buffers; i++) {
276 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
277 struct drm_gem_object *gem;
278 struct nouveau_bo *nvbo;
280 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
282 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
283 validate_fini(op, NULL);
286 nvbo = gem->driver_private;
288 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
289 NV_ERROR(dev, "multiple instances of buffer %d on "
290 "validation list\n", b->handle);
291 validate_fini(op, NULL);
295 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
297 validate_fini(op, NULL);
298 if (unlikely(ret == -EAGAIN))
299 ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
300 drm_gem_object_unreference_unlocked(gem);
302 if (ret != -ERESTARTSYS)
303 NV_ERROR(dev, "fail reserve\n");
309 b->user_priv = (uint64_t)(unsigned long)nvbo;
310 nvbo->reserved_by = file_priv;
311 nvbo->pbbo_index = i;
312 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
313 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
314 list_add_tail(&nvbo->entry, &op->both_list);
316 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
317 list_add_tail(&nvbo->entry, &op->vram_list);
319 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
320 list_add_tail(&nvbo->entry, &op->gart_list);
322 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
324 list_add_tail(&nvbo->entry, &op->both_list);
325 validate_fini(op, NULL);
334 validate_list(struct nouveau_channel *chan, struct list_head *list,
335 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
337 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
338 (void __force __user *)(uintptr_t)user_pbbo_ptr;
339 struct drm_device *dev = chan->dev;
340 struct nouveau_bo *nvbo;
343 list_for_each_entry(nvbo, list, entry) {
344 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
346 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
348 NV_ERROR(dev, "fail pre-validate sync\n");
352 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
356 NV_ERROR(dev, "fail set_domain\n");
360 nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan;
361 ret = nouveau_bo_validate(nvbo, true, false, false);
362 nvbo->channel = NULL;
364 if (ret != -ERESTARTSYS)
365 NV_ERROR(dev, "fail ttm_validate\n");
369 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
371 NV_ERROR(dev, "fail post-validate sync\n");
375 if (nvbo->bo.offset == b->presumed.offset &&
376 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
377 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
378 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
379 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
382 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
383 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
385 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
386 b->presumed.offset = nvbo->bo.offset;
387 b->presumed.valid = 0;
390 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
391 &b->presumed, sizeof(b->presumed)))
399 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
400 struct drm_file *file_priv,
401 struct drm_nouveau_gem_pushbuf_bo *pbbo,
402 uint64_t user_buffers, int nr_buffers,
403 struct validate_op *op, int *apply_relocs)
405 struct drm_device *dev = chan->dev;
408 INIT_LIST_HEAD(&op->vram_list);
409 INIT_LIST_HEAD(&op->gart_list);
410 INIT_LIST_HEAD(&op->both_list);
415 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
417 if (ret != -ERESTARTSYS)
418 NV_ERROR(dev, "validate_init\n");
422 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
423 if (unlikely(ret < 0)) {
424 if (ret != -ERESTARTSYS)
425 NV_ERROR(dev, "validate vram_list\n");
426 validate_fini(op, NULL);
431 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
432 if (unlikely(ret < 0)) {
433 if (ret != -ERESTARTSYS)
434 NV_ERROR(dev, "validate gart_list\n");
435 validate_fini(op, NULL);
440 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
441 if (unlikely(ret < 0)) {
442 if (ret != -ERESTARTSYS)
443 NV_ERROR(dev, "validate both_list\n");
444 validate_fini(op, NULL);
449 *apply_relocs = relocs;
454 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
457 void __user *userptr = (void __force __user *)(uintptr_t)user;
459 mem = kmalloc(nmemb * size, GFP_KERNEL);
461 return ERR_PTR(-ENOMEM);
463 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
465 return ERR_PTR(-EFAULT);
472 nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
473 struct drm_nouveau_gem_pushbuf *req,
474 struct drm_nouveau_gem_pushbuf_bo *bo)
476 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
480 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
482 return PTR_ERR(reloc);
484 for (i = 0; i < req->nr_relocs; i++) {
485 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
486 struct drm_nouveau_gem_pushbuf_bo *b;
487 struct nouveau_bo *nvbo;
490 if (unlikely(r->bo_index > req->nr_buffers)) {
491 NV_ERROR(dev, "reloc bo index invalid\n");
496 b = &bo[r->bo_index];
497 if (b->presumed.valid)
500 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
501 NV_ERROR(dev, "reloc container bo index invalid\n");
505 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
507 if (unlikely(r->reloc_bo_offset + 4 >
508 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
509 NV_ERROR(dev, "reloc outside of bo\n");
514 if (!nvbo->kmap.virtual) {
515 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
518 NV_ERROR(dev, "failed kmap for reloc\n");
521 nvbo->validate_mapped = true;
524 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
525 data = b->presumed.offset + r->data;
527 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
528 data = (b->presumed.offset + r->data) >> 32;
532 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
533 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
539 spin_lock(&nvbo->bo.bdev->fence_lock);
540 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
541 spin_unlock(&nvbo->bo.bdev->fence_lock);
543 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
547 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
555 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
556 struct drm_file *file_priv)
558 struct drm_nouveau_private *dev_priv = dev->dev_private;
559 struct drm_nouveau_gem_pushbuf *req = data;
560 struct drm_nouveau_gem_pushbuf_push *push;
561 struct drm_nouveau_gem_pushbuf_bo *bo;
562 struct nouveau_channel *chan;
563 struct validate_op op;
564 struct nouveau_fence *fence = NULL;
565 int i, j, ret = 0, do_reloc = 0;
567 chan = nouveau_channel_get(dev, file_priv, req->channel);
569 return PTR_ERR(chan);
571 req->vram_available = dev_priv->fb_aper_free;
572 req->gart_available = dev_priv->gart_info.aper_free;
573 if (unlikely(req->nr_push == 0))
576 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
577 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
578 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
579 nouveau_channel_put(&chan);
583 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
584 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
585 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
586 nouveau_channel_put(&chan);
590 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
591 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
592 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
593 nouveau_channel_put(&chan);
597 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
599 nouveau_channel_put(&chan);
600 return PTR_ERR(push);
603 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
606 nouveau_channel_put(&chan);
610 /* Mark push buffers as being used on PFIFO, the validation code
611 * will then make sure that if the pushbuf bo moves, that they
612 * happen on the kernel channel, which will in turn cause a sync
613 * to happen before we try and submit the push buffer.
615 for (i = 0; i < req->nr_push; i++) {
616 if (push[i].bo_index >= req->nr_buffers) {
617 NV_ERROR(dev, "push %d buffer not in list\n", i);
622 bo[push[i].bo_index].read_domains |= (1 << 31);
625 /* Validate buffer list */
626 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
627 req->nr_buffers, &op, &do_reloc);
629 if (ret != -ERESTARTSYS)
630 NV_ERROR(dev, "validate: %d\n", ret);
634 /* Apply any relocations that are required */
636 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
638 NV_ERROR(dev, "reloc apply: %d\n", ret);
643 if (chan->dma.ib_max) {
644 ret = nouveau_dma_wait(chan, req->nr_push + 1, 6);
646 NV_INFO(dev, "nv50cal_space: %d\n", ret);
650 for (i = 0; i < req->nr_push; i++) {
651 struct nouveau_bo *nvbo = (void *)(unsigned long)
652 bo[push[i].bo_index].user_priv;
654 nv50_dma_push(chan, nvbo, push[i].offset,
658 if (dev_priv->chipset >= 0x25) {
659 ret = RING_SPACE(chan, req->nr_push * 2);
661 NV_ERROR(dev, "cal_space: %d\n", ret);
665 for (i = 0; i < req->nr_push; i++) {
666 struct nouveau_bo *nvbo = (void *)(unsigned long)
667 bo[push[i].bo_index].user_priv;
668 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
670 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
671 push[i].offset) | 2);
675 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
677 NV_ERROR(dev, "jmp_space: %d\n", ret);
681 for (i = 0; i < req->nr_push; i++) {
682 struct nouveau_bo *nvbo = (void *)(unsigned long)
683 bo[push[i].bo_index].user_priv;
684 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
687 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
689 if (unlikely(cmd != req->suffix0)) {
690 if (!nvbo->kmap.virtual) {
691 ret = ttm_bo_kmap(&nvbo->bo, 0,
699 nvbo->validate_mapped = true;
702 nouveau_bo_wr32(nvbo, (push[i].offset +
703 push[i].length - 8) / 4, cmd);
706 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
707 push[i].offset) | 0x20000000);
709 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
714 ret = nouveau_fence_new(chan, &fence, true);
716 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
722 validate_fini(&op, fence);
723 nouveau_fence_unref(&fence);
728 if (chan->dma.ib_max) {
729 req->suffix0 = 0x00000000;
730 req->suffix1 = 0x00000000;
732 if (dev_priv->chipset >= 0x25) {
733 req->suffix0 = 0x00020000;
734 req->suffix1 = 0x00000000;
736 req->suffix0 = 0x20000000 |
737 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
738 req->suffix1 = 0x00000000;
741 nouveau_channel_put(&chan);
745 static inline uint32_t
746 domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
750 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
751 flags |= TTM_PL_FLAG_VRAM;
752 if (domain & NOUVEAU_GEM_DOMAIN_GART)
753 flags |= TTM_PL_FLAG_TT;
759 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
760 struct drm_file *file_priv)
762 struct drm_nouveau_gem_cpu_prep *req = data;
763 struct drm_gem_object *gem;
764 struct nouveau_bo *nvbo;
765 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
768 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
771 nvbo = nouveau_gem_object(gem);
773 spin_lock(&nvbo->bo.bdev->fence_lock);
774 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
775 spin_unlock(&nvbo->bo.bdev->fence_lock);
776 drm_gem_object_unreference_unlocked(gem);
781 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
782 struct drm_file *file_priv)
788 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
789 struct drm_file *file_priv)
791 struct drm_nouveau_gem_info *req = data;
792 struct drm_gem_object *gem;
795 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
799 ret = nouveau_gem_info(gem, req);
800 drm_gem_object_unreference_unlocked(gem);