xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c (revision d6667f0d)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright (c) 2011-2024 Broadcom. All Rights Reserved. The term
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * 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, sub license, 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 portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 
29 #include "vmwgfx_bo.h"
30 #include "vmwgfx_drv.h"
31 #include "vmwgfx_resource_priv.h"
32 
33 #include <drm/ttm/ttm_placement.h>
34 
vmw_bo_release(struct vmw_bo * vbo)35 static void vmw_bo_release(struct vmw_bo *vbo)
36 {
37 	struct vmw_resource *res;
38 
39 	WARN_ON(vbo->tbo.base.funcs &&
40 		kref_read(&vbo->tbo.base.refcount) != 0);
41 	vmw_bo_unmap(vbo);
42 
43 	xa_destroy(&vbo->detached_resources);
44 	WARN_ON(vbo->is_dumb && !vbo->dumb_surface);
45 	if (vbo->is_dumb && vbo->dumb_surface) {
46 		res = &vbo->dumb_surface->res;
47 		WARN_ON(vbo != res->guest_memory_bo);
48 		WARN_ON(!res->guest_memory_bo);
49 		if (res->guest_memory_bo) {
50 			/* Reserve and switch the backing mob. */
51 			mutex_lock(&res->dev_priv->cmdbuf_mutex);
52 			(void)vmw_resource_reserve(res, false, true);
53 			vmw_resource_mob_detach(res);
54 			if (res->coherent)
55 				vmw_bo_dirty_release(res->guest_memory_bo);
56 			res->guest_memory_bo = NULL;
57 			res->guest_memory_offset = 0;
58 			vmw_resource_unreserve(res, false, false, false, NULL,
59 					       0);
60 			mutex_unlock(&res->dev_priv->cmdbuf_mutex);
61 		}
62 		vmw_surface_unreference(&vbo->dumb_surface);
63 	}
64 	drm_gem_object_release(&vbo->tbo.base);
65 }
66 
67 /**
68  * vmw_bo_free - vmw_bo destructor
69  *
70  * @bo: Pointer to the embedded struct ttm_buffer_object
71  */
vmw_bo_free(struct ttm_buffer_object * bo)72 static void vmw_bo_free(struct ttm_buffer_object *bo)
73 {
74 	struct vmw_bo *vbo = to_vmw_bo(&bo->base);
75 
76 	WARN_ON(vbo->dirty);
77 	WARN_ON(!RB_EMPTY_ROOT(&vbo->res_tree));
78 	vmw_bo_release(vbo);
79 	kfree(vbo);
80 }
81 
82 /**
83  * vmw_bo_pin_in_placement - Validate a buffer to placement.
84  *
85  * @dev_priv:  Driver private.
86  * @buf:  DMA buffer to move.
87  * @placement:  The placement to pin it.
88  * @interruptible:  Use interruptible wait.
89  * Return: Zero on success, Negative error code on failure. In particular
90  * -ERESTARTSYS if interrupted by a signal
91  */
vmw_bo_pin_in_placement(struct vmw_private * dev_priv,struct vmw_bo * buf,struct ttm_placement * placement,bool interruptible)92 static int vmw_bo_pin_in_placement(struct vmw_private *dev_priv,
93 				   struct vmw_bo *buf,
94 				   struct ttm_placement *placement,
95 				   bool interruptible)
96 {
97 	struct ttm_operation_ctx ctx = {interruptible, false };
98 	struct ttm_buffer_object *bo = &buf->tbo;
99 	int ret;
100 
101 	vmw_execbuf_release_pinned_bo(dev_priv);
102 
103 	ret = ttm_bo_reserve(bo, interruptible, false, NULL);
104 	if (unlikely(ret != 0))
105 		goto err;
106 
107 	ret = ttm_bo_validate(bo, placement, &ctx);
108 	if (!ret)
109 		vmw_bo_pin_reserved(buf, true);
110 
111 	ttm_bo_unreserve(bo);
112 err:
113 	return ret;
114 }
115 
116 
117 /**
118  * vmw_bo_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
119  *
120  * This function takes the reservation_sem in write mode.
121  * Flushes and unpins the query bo to avoid failures.
122  *
123  * @dev_priv:  Driver private.
124  * @buf:  DMA buffer to move.
125  * @interruptible:  Use interruptible wait.
126  * Return: Zero on success, Negative error code on failure. In particular
127  * -ERESTARTSYS if interrupted by a signal
128  */
vmw_bo_pin_in_vram_or_gmr(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)129 int vmw_bo_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
130 			      struct vmw_bo *buf,
131 			      bool interruptible)
132 {
133 	struct ttm_operation_ctx ctx = {interruptible, false };
134 	struct ttm_buffer_object *bo = &buf->tbo;
135 	int ret;
136 
137 	vmw_execbuf_release_pinned_bo(dev_priv);
138 
139 	ret = ttm_bo_reserve(bo, interruptible, false, NULL);
140 	if (unlikely(ret != 0))
141 		goto err;
142 
143 	vmw_bo_placement_set(buf,
144 			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
145 			     VMW_BO_DOMAIN_GMR);
146 	ret = ttm_bo_validate(bo, &buf->placement, &ctx);
147 	if (likely(ret == 0) || ret == -ERESTARTSYS)
148 		goto out_unreserve;
149 
150 	vmw_bo_placement_set(buf,
151 			     VMW_BO_DOMAIN_VRAM,
152 			     VMW_BO_DOMAIN_VRAM);
153 	ret = ttm_bo_validate(bo, &buf->placement, &ctx);
154 
155 out_unreserve:
156 	if (!ret)
157 		vmw_bo_pin_reserved(buf, true);
158 
159 	ttm_bo_unreserve(bo);
160 err:
161 	return ret;
162 }
163 
164 
165 /**
166  * vmw_bo_pin_in_vram - Move a buffer to vram.
167  *
168  * This function takes the reservation_sem in write mode.
169  * Flushes and unpins the query bo to avoid failures.
170  *
171  * @dev_priv:  Driver private.
172  * @buf:  DMA buffer to move.
173  * @interruptible:  Use interruptible wait.
174  * Return: Zero on success, Negative error code on failure. In particular
175  * -ERESTARTSYS if interrupted by a signal
176  */
vmw_bo_pin_in_vram(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)177 int vmw_bo_pin_in_vram(struct vmw_private *dev_priv,
178 		       struct vmw_bo *buf,
179 		       bool interruptible)
180 {
181 	return vmw_bo_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
182 				       interruptible);
183 }
184 
185 
186 /**
187  * vmw_bo_pin_in_start_of_vram - Move a buffer to start of vram.
188  *
189  * This function takes the reservation_sem in write mode.
190  * Flushes and unpins the query bo to avoid failures.
191  *
192  * @dev_priv:  Driver private.
193  * @buf:  DMA buffer to pin.
194  * @interruptible:  Use interruptible wait.
195  * Return: Zero on success, Negative error code on failure. In particular
196  * -ERESTARTSYS if interrupted by a signal
197  */
vmw_bo_pin_in_start_of_vram(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)198 int vmw_bo_pin_in_start_of_vram(struct vmw_private *dev_priv,
199 				struct vmw_bo *buf,
200 				bool interruptible)
201 {
202 	struct ttm_operation_ctx ctx = {interruptible, false };
203 	struct ttm_buffer_object *bo = &buf->tbo;
204 	int ret = 0;
205 
206 	vmw_execbuf_release_pinned_bo(dev_priv);
207 	ret = ttm_bo_reserve(bo, interruptible, false, NULL);
208 	if (unlikely(ret != 0))
209 		goto err_unlock;
210 
211 	/*
212 	 * Is this buffer already in vram but not at the start of it?
213 	 * In that case, evict it first because TTM isn't good at handling
214 	 * that situation.
215 	 */
216 	if (bo->resource->mem_type == TTM_PL_VRAM &&
217 	    bo->resource->start < PFN_UP(bo->resource->size) &&
218 	    bo->resource->start > 0 &&
219 	    buf->tbo.pin_count == 0) {
220 		ctx.interruptible = false;
221 		vmw_bo_placement_set(buf,
222 				     VMW_BO_DOMAIN_SYS,
223 				     VMW_BO_DOMAIN_SYS);
224 		(void)ttm_bo_validate(bo, &buf->placement, &ctx);
225 	}
226 
227 	vmw_bo_placement_set(buf,
228 			     VMW_BO_DOMAIN_VRAM,
229 			     VMW_BO_DOMAIN_VRAM);
230 	buf->places[0].lpfn = PFN_UP(bo->resource->size);
231 	buf->busy_places[0].lpfn = PFN_UP(bo->resource->size);
232 	ret = ttm_bo_validate(bo, &buf->placement, &ctx);
233 
234 	/* For some reason we didn't end up at the start of vram */
235 	WARN_ON(ret == 0 && bo->resource->start != 0);
236 	if (!ret)
237 		vmw_bo_pin_reserved(buf, true);
238 
239 	ttm_bo_unreserve(bo);
240 err_unlock:
241 
242 	return ret;
243 }
244 
245 
246 /**
247  * vmw_bo_unpin - Unpin the buffer given buffer, does not move the buffer.
248  *
249  * This function takes the reservation_sem in write mode.
250  *
251  * @dev_priv:  Driver private.
252  * @buf:  DMA buffer to unpin.
253  * @interruptible:  Use interruptible wait.
254  * Return: Zero on success, Negative error code on failure. In particular
255  * -ERESTARTSYS if interrupted by a signal
256  */
vmw_bo_unpin(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)257 int vmw_bo_unpin(struct vmw_private *dev_priv,
258 		 struct vmw_bo *buf,
259 		 bool interruptible)
260 {
261 	struct ttm_buffer_object *bo = &buf->tbo;
262 	int ret;
263 
264 	ret = ttm_bo_reserve(bo, interruptible, false, NULL);
265 	if (unlikely(ret != 0))
266 		goto err;
267 
268 	vmw_bo_pin_reserved(buf, false);
269 
270 	ttm_bo_unreserve(bo);
271 
272 err:
273 	return ret;
274 }
275 
276 /**
277  * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
278  * of a buffer.
279  *
280  * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
281  * @ptr: SVGAGuestPtr returning the result.
282  */
vmw_bo_get_guest_ptr(const struct ttm_buffer_object * bo,SVGAGuestPtr * ptr)283 void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
284 			  SVGAGuestPtr *ptr)
285 {
286 	if (bo->resource->mem_type == TTM_PL_VRAM) {
287 		ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
288 		ptr->offset = bo->resource->start << PAGE_SHIFT;
289 	} else {
290 		ptr->gmrId = bo->resource->start;
291 		ptr->offset = 0;
292 	}
293 }
294 
295 
296 /**
297  * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
298  *
299  * @vbo: The buffer object. Must be reserved.
300  * @pin: Whether to pin or unpin.
301  *
302  */
vmw_bo_pin_reserved(struct vmw_bo * vbo,bool pin)303 void vmw_bo_pin_reserved(struct vmw_bo *vbo, bool pin)
304 {
305 	struct ttm_operation_ctx ctx = { false, true };
306 	struct ttm_place pl;
307 	struct ttm_placement placement;
308 	struct ttm_buffer_object *bo = &vbo->tbo;
309 	uint32_t old_mem_type = bo->resource->mem_type;
310 	int ret;
311 
312 	dma_resv_assert_held(bo->base.resv);
313 
314 	if (pin == !!bo->pin_count)
315 		return;
316 
317 	pl.fpfn = 0;
318 	pl.lpfn = 0;
319 	pl.mem_type = bo->resource->mem_type;
320 	pl.flags = bo->resource->placement;
321 
322 	memset(&placement, 0, sizeof(placement));
323 	placement.num_placement = 1;
324 	placement.placement = &pl;
325 
326 	ret = ttm_bo_validate(bo, &placement, &ctx);
327 
328 	BUG_ON(ret != 0 || bo->resource->mem_type != old_mem_type);
329 
330 	if (pin)
331 		ttm_bo_pin(bo);
332 	else
333 		ttm_bo_unpin(bo);
334 }
335 
336 /**
337  * vmw_bo_map_and_cache - Map a buffer object and cache the map
338  *
339  * @vbo: The buffer object to map
340  * Return: A kernel virtual address or NULL if mapping failed.
341  *
342  * This function maps a buffer object into the kernel address space, or
343  * returns the virtual kernel address of an already existing map. The virtual
344  * address remains valid as long as the buffer object is pinned or reserved.
345  * The cached map is torn down on either
346  * 1) Buffer object move
347  * 2) Buffer object swapout
348  * 3) Buffer object destruction
349  *
350  */
vmw_bo_map_and_cache(struct vmw_bo * vbo)351 void *vmw_bo_map_and_cache(struct vmw_bo *vbo)
352 {
353 	return vmw_bo_map_and_cache_size(vbo, vbo->tbo.base.size);
354 }
355 
vmw_bo_map_and_cache_size(struct vmw_bo * vbo,size_t size)356 void *vmw_bo_map_and_cache_size(struct vmw_bo *vbo, size_t size)
357 {
358 	struct ttm_buffer_object *bo = &vbo->tbo;
359 	bool not_used;
360 	void *virtual;
361 	int ret;
362 
363 	virtual = ttm_kmap_obj_virtual(&vbo->map, &not_used);
364 	if (virtual)
365 		return virtual;
366 
367 	ret = ttm_bo_kmap(bo, 0, PFN_UP(size), &vbo->map);
368 	if (ret)
369 		DRM_ERROR("Buffer object map failed: %d (size: bo = %zu, map = %zu).\n",
370 			  ret, bo->base.size, size);
371 
372 	return ttm_kmap_obj_virtual(&vbo->map, &not_used);
373 }
374 
375 
376 /**
377  * vmw_bo_unmap - Tear down a cached buffer object map.
378  *
379  * @vbo: The buffer object whose map we are tearing down.
380  *
381  * This function tears down a cached map set up using
382  * vmw_bo_map_and_cache().
383  */
vmw_bo_unmap(struct vmw_bo * vbo)384 void vmw_bo_unmap(struct vmw_bo *vbo)
385 {
386 	if (vbo->map.bo == NULL)
387 		return;
388 
389 	ttm_bo_kunmap(&vbo->map);
390 	vbo->map.bo = NULL;
391 }
392 
393 
394 /**
395  * vmw_bo_init - Initialize a vmw buffer object
396  *
397  * @dev_priv: Pointer to the device private struct
398  * @vmw_bo: Buffer object to initialize
399  * @params: Parameters used to initialize the buffer object
400  * @destroy: The function used to delete the buffer object
401  * Returns: Zero on success, negative error code on error.
402  *
403  */
vmw_bo_init(struct vmw_private * dev_priv,struct vmw_bo * vmw_bo,struct vmw_bo_params * params,void (* destroy)(struct ttm_buffer_object *))404 static int vmw_bo_init(struct vmw_private *dev_priv,
405 		       struct vmw_bo *vmw_bo,
406 		       struct vmw_bo_params *params,
407 		       void (*destroy)(struct ttm_buffer_object *))
408 {
409 	struct ttm_operation_ctx ctx = {
410 		.interruptible = params->bo_type != ttm_bo_type_kernel,
411 		.no_wait_gpu = false,
412 		.resv = params->resv,
413 	};
414 	struct ttm_device *bdev = &dev_priv->bdev;
415 	struct drm_device *vdev = &dev_priv->drm;
416 	int ret;
417 
418 	memset(vmw_bo, 0, sizeof(*vmw_bo));
419 
420 	BUILD_BUG_ON(TTM_MAX_BO_PRIORITY <= 3);
421 	vmw_bo->tbo.priority = 3;
422 	vmw_bo->res_tree = RB_ROOT;
423 	xa_init(&vmw_bo->detached_resources);
424 
425 	params->size = ALIGN(params->size, PAGE_SIZE);
426 	drm_gem_private_object_init(vdev, &vmw_bo->tbo.base, params->size);
427 
428 	vmw_bo_placement_set(vmw_bo, params->domain, params->busy_domain);
429 	ret = ttm_bo_init_reserved(bdev, &vmw_bo->tbo, params->bo_type,
430 				   &vmw_bo->placement, 0, &ctx,
431 				   params->sg, params->resv, destroy);
432 	if (unlikely(ret))
433 		return ret;
434 
435 	if (params->pin)
436 		ttm_bo_pin(&vmw_bo->tbo);
437 	ttm_bo_unreserve(&vmw_bo->tbo);
438 
439 	return 0;
440 }
441 
vmw_bo_create(struct vmw_private * vmw,struct vmw_bo_params * params,struct vmw_bo ** p_bo)442 int vmw_bo_create(struct vmw_private *vmw,
443 		  struct vmw_bo_params *params,
444 		  struct vmw_bo **p_bo)
445 {
446 	int ret;
447 
448 	*p_bo = kmalloc(sizeof(**p_bo), GFP_KERNEL);
449 	if (unlikely(!*p_bo)) {
450 		DRM_ERROR("Failed to allocate a buffer.\n");
451 		return -ENOMEM;
452 	}
453 
454 	/*
455 	 * vmw_bo_init will delete the *p_bo object if it fails
456 	 */
457 	ret = vmw_bo_init(vmw, *p_bo, params, vmw_bo_free);
458 	if (unlikely(ret != 0))
459 		goto out_error;
460 
461 	return ret;
462 out_error:
463 	*p_bo = NULL;
464 	return ret;
465 }
466 
467 /**
468  * vmw_user_bo_synccpu_grab - Grab a struct vmw_bo for cpu
469  * access, idling previous GPU operations on the buffer and optionally
470  * blocking it for further command submissions.
471  *
472  * @vmw_bo: Pointer to the buffer object being grabbed for CPU access
473  * @flags: Flags indicating how the grab should be performed.
474  * Return: Zero on success, Negative error code on error. In particular,
475  * -EBUSY will be returned if a dontblock operation is requested and the
476  * buffer object is busy, and -ERESTARTSYS will be returned if a wait is
477  * interrupted by a signal.
478  *
479  * A blocking grab will be automatically released when @tfile is closed.
480  */
vmw_user_bo_synccpu_grab(struct vmw_bo * vmw_bo,uint32_t flags)481 static int vmw_user_bo_synccpu_grab(struct vmw_bo *vmw_bo,
482 				    uint32_t flags)
483 {
484 	bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
485 	struct ttm_buffer_object *bo = &vmw_bo->tbo;
486 	int ret;
487 
488 	if (flags & drm_vmw_synccpu_allow_cs) {
489 		long lret;
490 
491 		lret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_READ,
492 					     true, nonblock ? 0 :
493 					     MAX_SCHEDULE_TIMEOUT);
494 		if (!lret)
495 			return -EBUSY;
496 		else if (lret < 0)
497 			return lret;
498 		return 0;
499 	}
500 
501 	ret = ttm_bo_reserve(bo, true, nonblock, NULL);
502 	if (unlikely(ret != 0))
503 		return ret;
504 
505 	ret = ttm_bo_wait(bo, true, nonblock);
506 	if (likely(ret == 0))
507 		atomic_inc(&vmw_bo->cpu_writers);
508 
509 	ttm_bo_unreserve(bo);
510 	if (unlikely(ret != 0))
511 		return ret;
512 
513 	return ret;
514 }
515 
516 /**
517  * vmw_user_bo_synccpu_release - Release a previous grab for CPU access,
518  * and unblock command submission on the buffer if blocked.
519  *
520  * @filp: Identifying the caller.
521  * @handle: Handle identifying the buffer object.
522  * @flags: Flags indicating the type of release.
523  */
vmw_user_bo_synccpu_release(struct drm_file * filp,uint32_t handle,uint32_t flags)524 static int vmw_user_bo_synccpu_release(struct drm_file *filp,
525 				       uint32_t handle,
526 				       uint32_t flags)
527 {
528 	struct vmw_bo *vmw_bo;
529 	int ret = vmw_user_bo_lookup(filp, handle, &vmw_bo);
530 
531 	if (!ret) {
532 		if (!(flags & drm_vmw_synccpu_allow_cs)) {
533 			atomic_dec(&vmw_bo->cpu_writers);
534 		}
535 		vmw_user_bo_unref(&vmw_bo);
536 	}
537 
538 	return ret;
539 }
540 
541 
542 /**
543  * vmw_user_bo_synccpu_ioctl - ioctl function implementing the synccpu
544  * functionality.
545  *
546  * @dev: Identifies the drm device.
547  * @data: Pointer to the ioctl argument.
548  * @file_priv: Identifies the caller.
549  * Return: Zero on success, negative error code on error.
550  *
551  * This function checks the ioctl arguments for validity and calls the
552  * relevant synccpu functions.
553  */
vmw_user_bo_synccpu_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)554 int vmw_user_bo_synccpu_ioctl(struct drm_device *dev, void *data,
555 			      struct drm_file *file_priv)
556 {
557 	struct drm_vmw_synccpu_arg *arg =
558 		(struct drm_vmw_synccpu_arg *) data;
559 	struct vmw_bo *vbo;
560 	int ret;
561 
562 	if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
563 	    || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
564 			       drm_vmw_synccpu_dontblock |
565 			       drm_vmw_synccpu_allow_cs)) != 0) {
566 		DRM_ERROR("Illegal synccpu flags.\n");
567 		return -EINVAL;
568 	}
569 
570 	switch (arg->op) {
571 	case drm_vmw_synccpu_grab:
572 		ret = vmw_user_bo_lookup(file_priv, arg->handle, &vbo);
573 		if (unlikely(ret != 0))
574 			return ret;
575 
576 		ret = vmw_user_bo_synccpu_grab(vbo, arg->flags);
577 		vmw_user_bo_unref(&vbo);
578 		if (unlikely(ret != 0)) {
579 			if (ret == -ERESTARTSYS || ret == -EBUSY)
580 				return -EBUSY;
581 			DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
582 				  (unsigned int) arg->handle);
583 			return ret;
584 		}
585 		break;
586 	case drm_vmw_synccpu_release:
587 		ret = vmw_user_bo_synccpu_release(file_priv,
588 						  arg->handle,
589 						  arg->flags);
590 		if (unlikely(ret != 0)) {
591 			DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
592 				  (unsigned int) arg->handle);
593 			return ret;
594 		}
595 		break;
596 	default:
597 		DRM_ERROR("Invalid synccpu operation.\n");
598 		return -EINVAL;
599 	}
600 
601 	return 0;
602 }
603 
604 /**
605  * vmw_bo_unref_ioctl - Generic handle close ioctl.
606  *
607  * @dev: Identifies the drm device.
608  * @data: Pointer to the ioctl argument.
609  * @file_priv: Identifies the caller.
610  * Return: Zero on success, negative error code on error.
611  *
612  * This function checks the ioctl arguments for validity and closes a
613  * handle to a TTM base object, optionally freeing the object.
614  */
vmw_bo_unref_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)615 int vmw_bo_unref_ioctl(struct drm_device *dev, void *data,
616 		       struct drm_file *file_priv)
617 {
618 	struct drm_vmw_unref_dmabuf_arg *arg =
619 	    (struct drm_vmw_unref_dmabuf_arg *)data;
620 
621 	return drm_gem_handle_delete(file_priv, arg->handle);
622 }
623 
624 
625 /**
626  * vmw_user_bo_lookup - Look up a vmw user buffer object from a handle.
627  *
628  * @filp: The file the handle is registered with.
629  * @handle: The user buffer object handle
630  * @out: Pointer to a where a pointer to the embedded
631  * struct vmw_bo should be placed.
632  * Return: Zero on success, Negative error code on error.
633  *
634  * The vmw buffer object pointer will be refcounted (both ttm and gem)
635  */
vmw_user_bo_lookup(struct drm_file * filp,u32 handle,struct vmw_bo ** out)636 int vmw_user_bo_lookup(struct drm_file *filp,
637 		       u32 handle,
638 		       struct vmw_bo **out)
639 {
640 	struct drm_gem_object *gobj;
641 
642 	gobj = drm_gem_object_lookup(filp, handle);
643 	if (!gobj) {
644 		DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
645 			  (unsigned long)handle);
646 		return -ESRCH;
647 	}
648 
649 	*out = to_vmw_bo(gobj);
650 
651 	return 0;
652 }
653 
654 /**
655  * vmw_bo_fence_single - Utility function to fence a single TTM buffer
656  *                       object without unreserving it.
657  *
658  * @bo:             Pointer to the struct ttm_buffer_object to fence.
659  * @fence:          Pointer to the fence. If NULL, this function will
660  *                  insert a fence into the command stream..
661  *
662  * Contrary to the ttm_eu version of this function, it takes only
663  * a single buffer object instead of a list, and it also doesn't
664  * unreserve the buffer object, which needs to be done separately.
665  */
vmw_bo_fence_single(struct ttm_buffer_object * bo,struct vmw_fence_obj * fence)666 void vmw_bo_fence_single(struct ttm_buffer_object *bo,
667 			 struct vmw_fence_obj *fence)
668 {
669 	struct ttm_device *bdev = bo->bdev;
670 	struct vmw_private *dev_priv = vmw_priv_from_ttm(bdev);
671 	int ret;
672 
673 	if (fence == NULL)
674 		vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
675 	else
676 		dma_fence_get(&fence->base);
677 
678 	ret = dma_resv_reserve_fences(bo->base.resv, 1);
679 	if (!ret)
680 		dma_resv_add_fence(bo->base.resv, &fence->base,
681 				   DMA_RESV_USAGE_KERNEL);
682 	else
683 		/* Last resort fallback when we are OOM */
684 		dma_fence_wait(&fence->base, false);
685 	dma_fence_put(&fence->base);
686 }
687 
688 /**
689  * vmw_bo_swap_notify - swapout notify callback.
690  *
691  * @bo: The buffer object to be swapped out.
692  */
vmw_bo_swap_notify(struct ttm_buffer_object * bo)693 void vmw_bo_swap_notify(struct ttm_buffer_object *bo)
694 {
695 	/* Kill any cached kernel maps before swapout */
696 	vmw_bo_unmap(to_vmw_bo(&bo->base));
697 }
698 
699 
700 /**
701  * vmw_bo_move_notify - TTM move_notify_callback
702  *
703  * @bo: The TTM buffer object about to move.
704  * @mem: The struct ttm_resource indicating to what memory
705  *       region the move is taking place.
706  *
707  * Detaches cached maps and device bindings that require that the
708  * buffer doesn't move.
709  */
vmw_bo_move_notify(struct ttm_buffer_object * bo,struct ttm_resource * mem)710 void vmw_bo_move_notify(struct ttm_buffer_object *bo,
711 			struct ttm_resource *mem)
712 {
713 	struct vmw_bo *vbo = to_vmw_bo(&bo->base);
714 
715 	/*
716 	 * Kill any cached kernel maps before move to or from VRAM.
717 	 * With other types of moves, the underlying pages stay the same,
718 	 * and the map can be kept.
719 	 */
720 	if (mem->mem_type == TTM_PL_VRAM || bo->resource->mem_type == TTM_PL_VRAM)
721 		vmw_bo_unmap(vbo);
722 
723 	/*
724 	 * If we're moving a backup MOB out of MOB placement, then make sure we
725 	 * read back all resource content first, and unbind the MOB from
726 	 * the resource.
727 	 */
728 	if (mem->mem_type != VMW_PL_MOB && bo->resource->mem_type == VMW_PL_MOB)
729 		vmw_resource_unbind_list(vbo);
730 }
731 
placement_flags(u32 domain,u32 desired,u32 fallback)732 static u32 placement_flags(u32 domain, u32 desired, u32 fallback)
733 {
734 	if (desired & fallback & domain)
735 		return 0;
736 
737 	if (desired & domain)
738 		return TTM_PL_FLAG_DESIRED;
739 
740 	return TTM_PL_FLAG_FALLBACK;
741 }
742 
743 static u32
set_placement_list(struct ttm_place * pl,u32 desired,u32 fallback)744 set_placement_list(struct ttm_place *pl, u32 desired, u32 fallback)
745 {
746 	u32 domain = desired | fallback;
747 	u32 n = 0;
748 
749 	/*
750 	 * The placements are ordered according to our preferences
751 	 */
752 	if (domain & VMW_BO_DOMAIN_MOB) {
753 		pl[n].mem_type = VMW_PL_MOB;
754 		pl[n].flags = placement_flags(VMW_BO_DOMAIN_MOB, desired,
755 					      fallback);
756 		pl[n].fpfn = 0;
757 		pl[n].lpfn = 0;
758 		n++;
759 	}
760 	if (domain & VMW_BO_DOMAIN_GMR) {
761 		pl[n].mem_type = VMW_PL_GMR;
762 		pl[n].flags = placement_flags(VMW_BO_DOMAIN_GMR, desired,
763 					      fallback);
764 		pl[n].fpfn = 0;
765 		pl[n].lpfn = 0;
766 		n++;
767 	}
768 	if (domain & VMW_BO_DOMAIN_VRAM) {
769 		pl[n].mem_type = TTM_PL_VRAM;
770 		pl[n].flags = placement_flags(VMW_BO_DOMAIN_VRAM, desired,
771 					      fallback);
772 		pl[n].fpfn = 0;
773 		pl[n].lpfn = 0;
774 		n++;
775 	}
776 	if (domain & VMW_BO_DOMAIN_WAITABLE_SYS) {
777 		pl[n].mem_type = VMW_PL_SYSTEM;
778 		pl[n].flags = placement_flags(VMW_BO_DOMAIN_WAITABLE_SYS,
779 					      desired, fallback);
780 		pl[n].fpfn = 0;
781 		pl[n].lpfn = 0;
782 		n++;
783 	}
784 	if (domain & VMW_BO_DOMAIN_SYS) {
785 		pl[n].mem_type = TTM_PL_SYSTEM;
786 		pl[n].flags = placement_flags(VMW_BO_DOMAIN_SYS, desired,
787 					      fallback);
788 		pl[n].fpfn = 0;
789 		pl[n].lpfn = 0;
790 		n++;
791 	}
792 
793 	WARN_ON(!n);
794 	if (!n) {
795 		pl[n].mem_type = TTM_PL_SYSTEM;
796 		pl[n].flags = 0;
797 		pl[n].fpfn = 0;
798 		pl[n].lpfn = 0;
799 		n++;
800 	}
801 	return n;
802 }
803 
vmw_bo_placement_set(struct vmw_bo * bo,u32 domain,u32 busy_domain)804 void vmw_bo_placement_set(struct vmw_bo *bo, u32 domain, u32 busy_domain)
805 {
806 	struct ttm_device *bdev = bo->tbo.bdev;
807 	struct vmw_private *vmw = vmw_priv_from_ttm(bdev);
808 	struct ttm_placement *pl = &bo->placement;
809 	bool mem_compatible = false;
810 	u32 i;
811 
812 	pl->placement = bo->places;
813 	pl->num_placement = set_placement_list(bo->places, domain, busy_domain);
814 
815 	if (drm_debug_enabled(DRM_UT_DRIVER) && bo->tbo.resource) {
816 		for (i = 0; i < pl->num_placement; ++i) {
817 			if (bo->tbo.resource->mem_type == TTM_PL_SYSTEM ||
818 			    bo->tbo.resource->mem_type == pl->placement[i].mem_type)
819 				mem_compatible = true;
820 		}
821 		if (!mem_compatible)
822 			drm_warn(&vmw->drm,
823 				 "%s: Incompatible transition from "
824 				 "bo->base.resource->mem_type = %u to domain = %u\n",
825 				 __func__, bo->tbo.resource->mem_type, domain);
826 	}
827 
828 }
829 
vmw_bo_placement_set_default_accelerated(struct vmw_bo * bo)830 void vmw_bo_placement_set_default_accelerated(struct vmw_bo *bo)
831 {
832 	struct ttm_device *bdev = bo->tbo.bdev;
833 	struct vmw_private *vmw = vmw_priv_from_ttm(bdev);
834 	u32 domain = VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM;
835 
836 	if (vmw->has_mob)
837 		domain = VMW_BO_DOMAIN_MOB;
838 
839 	vmw_bo_placement_set(bo, domain, domain);
840 }
841 
vmw_bo_add_detached_resource(struct vmw_bo * vbo,struct vmw_resource * res)842 void vmw_bo_add_detached_resource(struct vmw_bo *vbo, struct vmw_resource *res)
843 {
844 	xa_store(&vbo->detached_resources, (unsigned long)res, res, GFP_KERNEL);
845 }
846 
vmw_bo_del_detached_resource(struct vmw_bo * vbo,struct vmw_resource * res)847 void vmw_bo_del_detached_resource(struct vmw_bo *vbo, struct vmw_resource *res)
848 {
849 	xa_erase(&vbo->detached_resources, (unsigned long)res);
850 }
851 
vmw_bo_surface(struct vmw_bo * vbo)852 struct vmw_surface *vmw_bo_surface(struct vmw_bo *vbo)
853 {
854 	unsigned long index;
855 	struct vmw_resource *res = NULL;
856 	struct vmw_surface *surf = NULL;
857 	struct rb_node *rb_itr = vbo->res_tree.rb_node;
858 
859 	if (vbo->is_dumb && vbo->dumb_surface) {
860 		res = &vbo->dumb_surface->res;
861 		goto out;
862 	}
863 
864 	xa_for_each(&vbo->detached_resources, index, res) {
865 		if (res->func->res_type == vmw_res_surface)
866 			goto out;
867 	}
868 
869 	for (rb_itr = rb_first(&vbo->res_tree); rb_itr;
870 	     rb_itr = rb_next(rb_itr)) {
871 		res = rb_entry(rb_itr, struct vmw_resource, mob_node);
872 		if (res->func->res_type == vmw_res_surface)
873 			goto out;
874 	}
875 
876 out:
877 	if (res)
878 		surf = vmw_res_to_srf(res);
879 	return surf;
880 }
881