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