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