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