vmwgfx_dmabuf.c 7.9 KB
Newer Older
1 2
/**************************************************************************
 *
S
Sinclair Yeh 已提交
3
 * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

28
#include <drm/ttm/ttm_placement.h>
29

30
#include <drm/drmP.h>
31 32 33 34
#include "vmwgfx_drv.h"


/**
35
 * vmw_dmabuf_pin_in_placement - Validate a buffer to placement.
36
 *
37 38
 * @dev_priv:  Driver private.
 * @buf:  DMA buffer to move.
39
 * @placement:  The placement to pin it.
40 41
 * @interruptible:  Use interruptible wait.
 *
42 43 44
 * Returns
 *  -ERESTARTSYS if interrupted by a signal.
 */
45 46 47 48
int vmw_dmabuf_pin_in_placement(struct vmw_private *dev_priv,
				struct vmw_dma_buffer *buf,
				struct ttm_placement *placement,
				bool interruptible)
49 50 51 52
{
	struct ttm_buffer_object *bo = &buf->base;
	int ret;

53
	ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
54 55 56
	if (unlikely(ret != 0))
		return ret;

57
	vmw_execbuf_release_pinned_bo(dev_priv);
T
Thomas Hellstrom 已提交
58

59
	ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
60 61 62
	if (unlikely(ret != 0))
		goto err;

63
	ret = ttm_bo_validate(bo, placement, interruptible, false);
64 65
	if (!ret)
		vmw_bo_pin_reserved(buf, true);
66 67 68 69

	ttm_bo_unreserve(bo);

err:
70
	ttm_write_unlock(&dev_priv->reservation_sem);
71 72 73 74
	return ret;
}

/**
75
 * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
76
 *
77 78
 * This function takes the reservation_sem in write mode.
 * Flushes and unpins the query bo to avoid failures.
79 80 81 82 83 84 85 86 87
 *
 * @dev_priv:  Driver private.
 * @buf:  DMA buffer to move.
 * @pin:  Pin buffer if true.
 * @interruptible:  Use interruptible wait.
 *
 * Returns
 * -ERESTARTSYS if interrupted by a signal.
 */
88 89 90
int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
				  struct vmw_dma_buffer *buf,
				  bool interruptible)
91 92 93 94
{
	struct ttm_buffer_object *bo = &buf->base;
	int ret;

95
	ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
96 97 98
	if (unlikely(ret != 0))
		return ret;

99
	vmw_execbuf_release_pinned_bo(dev_priv);
T
Thomas Hellstrom 已提交
100

101
	ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
102 103 104
	if (unlikely(ret != 0))
		goto err;

105 106
	ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible,
			      false);
107
	if (likely(ret == 0) || ret == -ERESTARTSYS)
108
		goto out_unreserve;
109

110
	ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false);
111

112 113 114
out_unreserve:
	if (!ret)
		vmw_bo_pin_reserved(buf, true);
115 116 117

	ttm_bo_unreserve(bo);
err:
118
	ttm_write_unlock(&dev_priv->reservation_sem);
119 120 121 122
	return ret;
}

/**
123
 * vmw_dmabuf_pin_in_vram - Move a buffer to vram.
124
 *
125 126
 * This function takes the reservation_sem in write mode.
 * Flushes and unpins the query bo to avoid failures.
127 128 129 130 131 132 133 134
 *
 * @dev_priv:  Driver private.
 * @buf:  DMA buffer to move.
 * @interruptible:  Use interruptible wait.
 *
 * Returns
 * -ERESTARTSYS if interrupted by a signal.
 */
135 136 137
int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv,
			   struct vmw_dma_buffer *buf,
			   bool interruptible)
138
{
139 140
	return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
					   interruptible);
141 142 143
}

/**
144
 * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram.
145
 *
146 147
 * This function takes the reservation_sem in write mode.
 * Flushes and unpins the query bo to avoid failures.
148 149
 *
 * @dev_priv:  Driver private.
150
 * @buf:  DMA buffer to pin.
151 152 153 154 155
 * @interruptible:  Use interruptible wait.
 *
 * Returns
 * -ERESTARTSYS if interrupted by a signal.
 */
156 157 158
int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *dev_priv,
				    struct vmw_dma_buffer *buf,
				    bool interruptible)
159 160 161
{
	struct ttm_buffer_object *bo = &buf->base;
	struct ttm_placement placement;
162
	struct ttm_place place;
163 164
	int ret = 0;

165
	place = vmw_vram_placement.placement[0];
166 167 168 169 170
	place.lpfn = bo->num_pages;
	placement.num_placement = 1;
	placement.placement = &place;
	placement.num_busy_placement = 1;
	placement.busy_placement = &place;
171

172
	ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
173 174 175
	if (unlikely(ret != 0))
		return ret;

176
	vmw_execbuf_release_pinned_bo(dev_priv);
177
	ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
178 179 180
	if (unlikely(ret != 0))
		goto err_unlock;

181 182 183 184 185
	/*
	 * Is this buffer already in vram but not at the start of it?
	 * In that case, evict it first because TTM isn't good at handling
	 * that situation.
	 */
186 187 188
	if (bo->mem.mem_type == TTM_PL_VRAM &&
	    bo->mem.start < bo->num_pages &&
	    bo->mem.start > 0)
189
		(void) ttm_bo_validate(bo, &vmw_sys_placement, false, false);
190

191
	ret = ttm_bo_validate(bo, &placement, interruptible, false);
192

193
	/* For some reason we didn't end up at the start of vram */
194
	WARN_ON(ret == 0 && bo->offset != 0);
195 196
	if (!ret)
		vmw_bo_pin_reserved(buf, true);
197 198 199

	ttm_bo_unreserve(bo);
err_unlock:
200
	ttm_write_unlock(&dev_priv->reservation_sem);
201 202 203 204 205

	return ret;
}

/**
206
 * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer.
207
 *
208
 * This function takes the reservation_sem in write mode.
209 210 211 212 213 214 215 216 217 218 219 220
 *
 * @dev_priv:  Driver private.
 * @buf:  DMA buffer to unpin.
 * @interruptible:  Use interruptible wait.
 *
 * Returns
 * -ERESTARTSYS if interrupted by a signal.
 */
int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
		     struct vmw_dma_buffer *buf,
		     bool interruptible)
{
221 222
	struct ttm_buffer_object *bo = &buf->base;
	int ret;
223

224 225 226 227
	ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible);
	if (unlikely(ret != 0))
		return ret;

228
	ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
229 230 231 232 233 234 235 236 237 238 239
	if (unlikely(ret != 0))
		goto err;

	vmw_bo_pin_reserved(buf, false);

	ttm_bo_unreserve(bo);

err:
	ttm_read_unlock(&dev_priv->reservation_sem);
	return ret;
}
240

241
/**
242 243
 * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
 * of a buffer.
244
 *
245 246
 * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
 * @ptr: SVGAGuestPtr returning the result.
247
 */
248 249
void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
			  SVGAGuestPtr *ptr)
250
{
251
	if (bo->mem.mem_type == TTM_PL_VRAM) {
252
		ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
253
		ptr->offset = bo->offset;
254
	} else {
255
		ptr->gmrId = bo->mem.start;
256 257 258
		ptr->offset = 0;
	}
}
T
Thomas Hellstrom 已提交
259 260 261


/**
262
 * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
T
Thomas Hellstrom 已提交
263
 *
264
 * @vbo: The buffer object. Must be reserved.
T
Thomas Hellstrom 已提交
265 266 267
 * @pin: Whether to pin or unpin.
 *
 */
268
void vmw_bo_pin_reserved(struct vmw_dma_buffer *vbo, bool pin)
T
Thomas Hellstrom 已提交
269
{
270
	struct ttm_place pl;
T
Thomas Hellstrom 已提交
271
	struct ttm_placement placement;
272
	struct ttm_buffer_object *bo = &vbo->base;
T
Thomas Hellstrom 已提交
273 274 275
	uint32_t old_mem_type = bo->mem.mem_type;
	int ret;

276
	lockdep_assert_held(&bo->resv->lock.base);
T
Thomas Hellstrom 已提交
277

278 279 280 281 282 283 284 285 286
	if (pin) {
		if (vbo->pin_count++ > 0)
			return;
	} else {
		WARN_ON(vbo->pin_count <= 0);
		if (--vbo->pin_count > 0)
			return;
	}

287 288 289
	pl.fpfn = 0;
	pl.lpfn = 0;
	pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB
290
		| TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
T
Thomas Hellstrom 已提交
291
	if (pin)
292
		pl.flags |= TTM_PL_FLAG_NO_EVICT;
T
Thomas Hellstrom 已提交
293 294 295

	memset(&placement, 0, sizeof(placement));
	placement.num_placement = 1;
296
	placement.placement = &pl;
T
Thomas Hellstrom 已提交
297

298
	ret = ttm_bo_validate(bo, &placement, false, true);
T
Thomas Hellstrom 已提交
299 300 301

	BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type);
}