virtgpu_vq.c 32.8 KB
Newer Older
D
Dave Airlie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (C) 2015 Red Hat, Inc.
 * All Rights Reserved.
 *
 * Authors:
 *    Dave Airlie <airlied@redhat.com>
 *    Gerd Hoffmann <kraxel@redhat.com>
 *
 * 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, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS 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.
 */

S
Sam Ravnborg 已提交
29
#include <linux/dma-mapping.h>
D
Dave Airlie 已提交
30 31 32 33
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ring.h>

S
Sam Ravnborg 已提交
34 35 36
#include "virtgpu_drv.h"
#include "virtgpu_trace.h"

D
Dave Airlie 已提交
37 38 39 40 41 42
#define MAX_INLINE_CMD_SIZE   96
#define MAX_INLINE_RESP_SIZE  24
#define VBUFFER_SIZE          (sizeof(struct virtio_gpu_vbuffer) \
			       + MAX_INLINE_CMD_SIZE		 \
			       + MAX_INLINE_RESP_SIZE)

43 44 45 46 47 48 49 50 51 52 53
static void convert_to_hw_box(struct virtio_gpu_box *dst,
			      const struct drm_virtgpu_3d_box *src)
{
	dst->x = cpu_to_le32(src->x);
	dst->y = cpu_to_le32(src->y);
	dst->z = cpu_to_le32(src->z);
	dst->w = cpu_to_le32(src->w);
	dst->h = cpu_to_le32(src->h);
	dst->d = cpu_to_le32(src->d);
}

D
Dave Airlie 已提交
54 55 56 57
void virtio_gpu_ctrl_ack(struct virtqueue *vq)
{
	struct drm_device *dev = vq->vdev->priv;
	struct virtio_gpu_device *vgdev = dev->dev_private;
58

D
Dave Airlie 已提交
59 60 61 62 63 64 65
	schedule_work(&vgdev->ctrlq.dequeue_work);
}

void virtio_gpu_cursor_ack(struct virtqueue *vq)
{
	struct drm_device *dev = vq->vdev->priv;
	struct virtio_gpu_device *vgdev = dev->dev_private;
66

D
Dave Airlie 已提交
67 68 69 70 71
	schedule_work(&vgdev->cursorq.dequeue_work);
}

int virtio_gpu_alloc_vbufs(struct virtio_gpu_device *vgdev)
{
G
Gerd Hoffmann 已提交
72 73 74 75
	vgdev->vbufs = kmem_cache_create("virtio-gpu-vbufs",
					 VBUFFER_SIZE,
					 __alignof__(struct virtio_gpu_vbuffer),
					 0, NULL);
D
Dave Airlie 已提交
76 77 78 79 80 81 82
	if (!vgdev->vbufs)
		return -ENOMEM;
	return 0;
}

void virtio_gpu_free_vbufs(struct virtio_gpu_device *vgdev)
{
G
Gerd Hoffmann 已提交
83 84
	kmem_cache_destroy(vgdev->vbufs);
	vgdev->vbufs = NULL;
D
Dave Airlie 已提交
85 86 87 88 89 90 91 92 93
}

static struct virtio_gpu_vbuffer*
virtio_gpu_get_vbuf(struct virtio_gpu_device *vgdev,
		    int size, int resp_size, void *resp_buf,
		    virtio_gpu_resp_cb resp_cb)
{
	struct virtio_gpu_vbuffer *vbuf;

94
	vbuf = kmem_cache_zalloc(vgdev->vbufs, GFP_KERNEL);
95 96
	if (!vbuf)
		return ERR_PTR(-ENOMEM);
D
Dave Airlie 已提交
97

98 99
	BUG_ON(size > MAX_INLINE_CMD_SIZE ||
	       size < sizeof(struct virtio_gpu_ctrl_hdr));
D
Dave Airlie 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
	vbuf->buf = (void *)vbuf + sizeof(*vbuf);
	vbuf->size = size;

	vbuf->resp_cb = resp_cb;
	vbuf->resp_size = resp_size;
	if (resp_size <= MAX_INLINE_RESP_SIZE)
		vbuf->resp_buf = (void *)vbuf->buf + size;
	else
		vbuf->resp_buf = resp_buf;
	BUG_ON(!vbuf->resp_buf);
	return vbuf;
}

113 114 115 116 117 118 119 120 121 122
static struct virtio_gpu_ctrl_hdr *
virtio_gpu_vbuf_ctrl_hdr(struct virtio_gpu_vbuffer *vbuf)
{
	/* this assumes a vbuf contains a command that starts with a
	 * virtio_gpu_ctrl_hdr, which is true for both ctrl and cursor
	 * virtqueues.
	 */
	return (struct virtio_gpu_ctrl_hdr *)vbuf->buf;
}

D
Dave Airlie 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static void *virtio_gpu_alloc_cmd(struct virtio_gpu_device *vgdev,
				  struct virtio_gpu_vbuffer **vbuffer_p,
				  int size)
{
	struct virtio_gpu_vbuffer *vbuf;

	vbuf = virtio_gpu_get_vbuf(vgdev, size,
				   sizeof(struct virtio_gpu_ctrl_hdr),
				   NULL, NULL);
	if (IS_ERR(vbuf)) {
		*vbuffer_p = NULL;
		return ERR_CAST(vbuf);
	}
	*vbuffer_p = vbuf;
	return vbuf->buf;
}

static struct virtio_gpu_update_cursor*
virtio_gpu_alloc_cursor(struct virtio_gpu_device *vgdev,
			struct virtio_gpu_vbuffer **vbuffer_p)
{
	struct virtio_gpu_vbuffer *vbuf;

	vbuf = virtio_gpu_get_vbuf
		(vgdev, sizeof(struct virtio_gpu_update_cursor),
		 0, NULL, NULL);
	if (IS_ERR(vbuf)) {
		*vbuffer_p = NULL;
		return ERR_CAST(vbuf);
	}
	*vbuffer_p = vbuf;
	return (struct virtio_gpu_update_cursor *)vbuf->buf;
}

static void *virtio_gpu_alloc_cmd_resp(struct virtio_gpu_device *vgdev,
				       virtio_gpu_resp_cb cb,
				       struct virtio_gpu_vbuffer **vbuffer_p,
				       int cmd_size, int resp_size,
				       void *resp_buf)
{
	struct virtio_gpu_vbuffer *vbuf;

	vbuf = virtio_gpu_get_vbuf(vgdev, cmd_size,
				   resp_size, resp_buf, cb);
	if (IS_ERR(vbuf)) {
		*vbuffer_p = NULL;
		return ERR_CAST(vbuf);
	}
	*vbuffer_p = vbuf;
	return (struct virtio_gpu_command *)vbuf->buf;
}

static void free_vbuf(struct virtio_gpu_device *vgdev,
		      struct virtio_gpu_vbuffer *vbuf)
{
	if (vbuf->resp_size > MAX_INLINE_RESP_SIZE)
		kfree(vbuf->resp_buf);
180
	kvfree(vbuf->data_buf);
G
Gerd Hoffmann 已提交
181
	kmem_cache_free(vgdev->vbufs, vbuf);
D
Dave Airlie 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

static void reclaim_vbufs(struct virtqueue *vq, struct list_head *reclaim_list)
{
	struct virtio_gpu_vbuffer *vbuf;
	unsigned int len;
	int freed = 0;

	while ((vbuf = virtqueue_get_buf(vq, &len))) {
		list_add_tail(&vbuf->list, reclaim_list);
		freed++;
	}
	if (freed == 0)
		DRM_DEBUG("Huh? zero vbufs reclaimed");
}

void virtio_gpu_dequeue_ctrl_func(struct work_struct *work)
{
	struct virtio_gpu_device *vgdev =
		container_of(work, struct virtio_gpu_device,
			     ctrlq.dequeue_work);
	struct list_head reclaim_list;
	struct virtio_gpu_vbuffer *entry, *tmp;
	struct virtio_gpu_ctrl_hdr *resp;
	u64 fence_id = 0;

	INIT_LIST_HEAD(&reclaim_list);
	spin_lock(&vgdev->ctrlq.qlock);
	do {
		virtqueue_disable_cb(vgdev->ctrlq.vq);
		reclaim_vbufs(vgdev->ctrlq.vq, &reclaim_list);

	} while (!virtqueue_enable_cb(vgdev->ctrlq.vq));
	spin_unlock(&vgdev->ctrlq.qlock);

217
	list_for_each_entry(entry, &reclaim_list, list) {
D
Dave Airlie 已提交
218
		resp = (struct virtio_gpu_ctrl_hdr *)entry->resp_buf;
219 220 221

		trace_virtio_gpu_cmd_response(vgdev->ctrlq.vq, resp);

G
Gerd Hoffmann 已提交
222 223 224
		if (resp->type != cpu_to_le32(VIRTIO_GPU_RESP_OK_NODATA)) {
			if (resp->type >= cpu_to_le32(VIRTIO_GPU_RESP_ERR_UNSPEC)) {
				struct virtio_gpu_ctrl_hdr *cmd;
225
				cmd = virtio_gpu_vbuf_ctrl_hdr(entry);
226 227 228
				DRM_ERROR_RATELIMITED("response 0x%x (command 0x%x)\n",
						      le32_to_cpu(resp->type),
						      le32_to_cpu(cmd->type));
G
Gerd Hoffmann 已提交
229 230 231
			} else
				DRM_DEBUG("response 0x%x\n", le32_to_cpu(resp->type));
		}
D
Dave Airlie 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		if (resp->flags & cpu_to_le32(VIRTIO_GPU_FLAG_FENCE)) {
			u64 f = le64_to_cpu(resp->fence_id);

			if (fence_id > f) {
				DRM_ERROR("%s: Oops: fence %llx -> %llx\n",
					  __func__, fence_id, f);
			} else {
				fence_id = f;
			}
		}
		if (entry->resp_cb)
			entry->resp_cb(vgdev, entry);
	}
	wake_up(&vgdev->ctrlq.ack_queue);

	if (fence_id)
		virtio_gpu_fence_event_process(vgdev, fence_id);
249 250 251

	list_for_each_entry_safe(entry, tmp, &reclaim_list, list) {
		if (entry->objs)
252
			virtio_gpu_array_put_free_delayed(vgdev, entry->objs);
253 254 255
		list_del(&entry->list);
		free_vbuf(vgdev, entry);
	}
D
Dave Airlie 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
}

void virtio_gpu_dequeue_cursor_func(struct work_struct *work)
{
	struct virtio_gpu_device *vgdev =
		container_of(work, struct virtio_gpu_device,
			     cursorq.dequeue_work);
	struct list_head reclaim_list;
	struct virtio_gpu_vbuffer *entry, *tmp;

	INIT_LIST_HEAD(&reclaim_list);
	spin_lock(&vgdev->cursorq.qlock);
	do {
		virtqueue_disable_cb(vgdev->cursorq.vq);
		reclaim_vbufs(vgdev->cursorq.vq, &reclaim_list);
	} while (!virtqueue_enable_cb(vgdev->cursorq.vq));
	spin_unlock(&vgdev->cursorq.qlock);

	list_for_each_entry_safe(entry, tmp, &reclaim_list, list) {
		list_del(&entry->list);
		free_vbuf(vgdev, entry);
	}
	wake_up(&vgdev->cursorq.ack_queue);
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/* Create sg_table from a vmalloc'd buffer. */
static struct sg_table *vmalloc_to_sgt(char *data, uint32_t size, int *sg_ents)
{
	int ret, s, i;
	struct sg_table *sgt;
	struct scatterlist *sg;
	struct page *pg;

	if (WARN_ON(!PAGE_ALIGNED(data)))
		return NULL;

	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
	if (!sgt)
		return NULL;

	*sg_ents = DIV_ROUND_UP(size, PAGE_SIZE);
	ret = sg_alloc_table(sgt, *sg_ents, GFP_KERNEL);
	if (ret) {
		kfree(sgt);
		return NULL;
	}

	for_each_sg(sgt->sgl, sg, *sg_ents, i) {
		pg = vmalloc_to_page(data);
		if (!pg) {
			sg_free_table(sgt);
			kfree(sgt);
			return NULL;
		}

		s = min_t(int, PAGE_SIZE, size);
		sg_set_page(sg, pg, s, 0);

		size -= s;
		data += s;
	}

	return sgt;
}

321 322 323 324 325
static bool virtio_gpu_queue_ctrl_sgs_locked(struct virtio_gpu_device *vgdev,
					     struct virtio_gpu_vbuffer *vbuf,
					     struct scatterlist **sgs,
					     int outcnt,
					     int incnt)
D
Dave Airlie 已提交
326 327
{
	struct virtqueue *vq = vgdev->ctrlq.vq;
328
	bool notify = false;
D
Dave Airlie 已提交
329 330 331
	int ret;

	if (!vgdev->vqs_ready)
332
		return notify;
D
Dave Airlie 已提交
333 334

	ret = virtqueue_add_sgs(vq, sgs, outcnt, incnt, vbuf, GFP_ATOMIC);
335 336
	WARN_ON(ret);

337
	trace_virtio_gpu_cmd_queue(vq, virtio_gpu_vbuf_ctrl_hdr(vbuf));
338 339 340

	notify = virtqueue_kick_prepare(vq);

341
	return notify;
D
Dave Airlie 已提交
342 343
}

344 345 346
static void virtio_gpu_queue_fenced_ctrl_buffer(struct virtio_gpu_device *vgdev,
						struct virtio_gpu_vbuffer *vbuf,
						struct virtio_gpu_fence *fence)
347 348
{
	struct virtqueue *vq = vgdev->ctrlq.vq;
349
	struct scatterlist *sgs[3], vcmd, vout, vresp;
350
	struct sg_table *sgt = NULL;
351
	int elemcnt = 0, outcnt = 0, incnt = 0;
352
	bool notify;
353

354 355 356 357 358 359 360
	/* set up vcmd */
	sg_init_one(&vcmd, vbuf->buf, vbuf->size);
	elemcnt++;
	sgs[outcnt] = &vcmd;
	outcnt++;

	/* set up vout */
361 362
	if (vbuf->data_size) {
		if (is_vmalloc_addr(vbuf->data_buf)) {
363
			int sg_ents;
364
			sgt = vmalloc_to_sgt(vbuf->data_buf, vbuf->data_size,
365
					     &sg_ents);
366 367 368
			if (!sgt) {
				if (fence && vbuf->objs)
					virtio_gpu_array_unlock_resv(vbuf->objs);
369
				return;
370
			}
371 372 373

			elemcnt += sg_ents;
			sgs[outcnt] = sgt->sgl;
374
		} else {
375 376 377
			sg_init_one(&vout, vbuf->data_buf, vbuf->data_size);
			elemcnt++;
			sgs[outcnt] = &vout;
378
		}
379 380 381 382 383 384 385 386 387
		outcnt++;
	}

	/* set up vresp */
	if (vbuf->resp_size) {
		sg_init_one(&vresp, vbuf->resp_buf, vbuf->resp_size);
		elemcnt++;
		sgs[outcnt + incnt] = &vresp;
		incnt++;
388
	}
389 390 391 392 393 394 395 396 397 398 399 400

again:
	spin_lock(&vgdev->ctrlq.qlock);

	/*
	 * Make sure we have enouth space in the virtqueue.  If not
	 * wait here until we have.
	 *
	 * Without that virtio_gpu_queue_ctrl_buffer_nolock might have
	 * to wait for free space, which can result in fence ids being
	 * submitted out-of-order.
	 */
401
	if (vq->num_free < elemcnt) {
402
		spin_unlock(&vgdev->ctrlq.qlock);
403
		wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= elemcnt);
404 405 406
		goto again;
	}

407 408 409
	if (fence) {
		virtio_gpu_fence_emit(vgdev, virtio_gpu_vbuf_ctrl_hdr(vbuf),
				      fence);
410 411 412 413
		if (vbuf->objs) {
			virtio_gpu_array_add_fence(vbuf->objs, &fence->f);
			virtio_gpu_array_unlock_resv(vbuf->objs);
		}
414
	}
415 416
	notify = virtio_gpu_queue_ctrl_sgs_locked(vgdev, vbuf, sgs, outcnt,
						  incnt);
417
	spin_unlock(&vgdev->ctrlq.qlock);
418 419 420 421 422 423
	if (notify) {
		if (vgdev->disable_notify)
			vgdev->pending_notify = true;
		else
			virtqueue_notify(vgdev->ctrlq.vq);
	}
424 425 426 427 428

	if (sgt) {
		sg_free_table(sgt);
		kfree(sgt);
	}
429 430
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
void virtio_gpu_disable_notify(struct virtio_gpu_device *vgdev)
{
	vgdev->disable_notify = true;
}

void virtio_gpu_enable_notify(struct virtio_gpu_device *vgdev)
{
	vgdev->disable_notify = false;

	if (!vgdev->pending_notify)
		return;
	vgdev->pending_notify = false;
	virtqueue_notify(vgdev->ctrlq.vq);
}

446 447 448
static void virtio_gpu_queue_ctrl_buffer(struct virtio_gpu_device *vgdev,
					 struct virtio_gpu_vbuffer *vbuf)
{
449
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, NULL);
450 451
}

452 453
static void virtio_gpu_queue_cursor(struct virtio_gpu_device *vgdev,
				    struct virtio_gpu_vbuffer *vbuf)
D
Dave Airlie 已提交
454 455 456
{
	struct virtqueue *vq = vgdev->cursorq.vq;
	struct scatterlist *sgs[1], ccmd;
457
	bool notify;
D
Dave Airlie 已提交
458 459 460 461
	int ret;
	int outcnt;

	if (!vgdev->vqs_ready)
462
		return;
D
Dave Airlie 已提交
463 464 465 466 467 468 469 470 471 472

	sg_init_one(&ccmd, vbuf->buf, vbuf->size);
	sgs[0] = &ccmd;
	outcnt = 1;

	spin_lock(&vgdev->cursorq.qlock);
retry:
	ret = virtqueue_add_sgs(vq, sgs, outcnt, 0, vbuf, GFP_ATOMIC);
	if (ret == -ENOSPC) {
		spin_unlock(&vgdev->cursorq.qlock);
473
		wait_event(vgdev->cursorq.ack_queue, vq->num_free >= outcnt);
D
Dave Airlie 已提交
474 475 476
		spin_lock(&vgdev->cursorq.qlock);
		goto retry;
	} else {
477
		trace_virtio_gpu_cmd_queue(vq,
478
			virtio_gpu_vbuf_ctrl_hdr(vbuf));
479

480
		notify = virtqueue_kick_prepare(vq);
D
Dave Airlie 已提交
481 482 483
	}

	spin_unlock(&vgdev->cursorq.qlock);
484 485 486

	if (notify)
		virtqueue_notify(vq);
D
Dave Airlie 已提交
487 488 489
}

/* just create gem objects for userspace and long lived objects,
490 491
 * just use dma_alloced pages for the queue objects?
 */
D
Dave Airlie 已提交
492 493 494

/* create a basic resource */
void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
495
				    struct virtio_gpu_object *bo,
496
				    struct virtio_gpu_object_params *params,
497
				    struct virtio_gpu_object_array *objs,
498
				    struct virtio_gpu_fence *fence)
D
Dave Airlie 已提交
499 500 501 502 503 504
{
	struct virtio_gpu_resource_create_2d *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));
505
	vbuf->objs = objs;
D
Dave Airlie 已提交
506 507

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_CREATE_2D);
508
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
509 510 511
	cmd_p->format = cpu_to_le32(params->format);
	cmd_p->width = cpu_to_le32(params->width);
	cmd_p->height = cpu_to_le32(params->height);
D
Dave Airlie 已提交
512

513
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
514
	bo->created = true;
D
Dave Airlie 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
}

void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
				   uint32_t resource_id)
{
	struct virtio_gpu_resource_unref *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_UNREF);
	cmd_p->resource_id = cpu_to_le32(resource_id);

	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
}

532
static void virtio_gpu_cmd_resource_inval_backing(struct virtio_gpu_device *vgdev,
G
Gerd Hoffmann 已提交
533
						  uint32_t resource_id,
534
						  struct virtio_gpu_fence *fence)
D
Dave Airlie 已提交
535 536 537 538 539 540 541 542 543 544
{
	struct virtio_gpu_resource_detach_backing *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING);
	cmd_p->resource_id = cpu_to_le32(resource_id);

545
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
D
Dave Airlie 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
}

void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
				uint32_t scanout_id, uint32_t resource_id,
				uint32_t width, uint32_t height,
				uint32_t x, uint32_t y)
{
	struct virtio_gpu_set_scanout *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_SET_SCANOUT);
	cmd_p->resource_id = cpu_to_le32(resource_id);
	cmd_p->scanout_id = cpu_to_le32(scanout_id);
	cmd_p->r.width = cpu_to_le32(width);
	cmd_p->r.height = cpu_to_le32(height);
	cmd_p->r.x = cpu_to_le32(x);
	cmd_p->r.y = cpu_to_le32(y);

	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
}

void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device *vgdev,
				   uint32_t resource_id,
				   uint32_t x, uint32_t y,
				   uint32_t width, uint32_t height)
{
	struct virtio_gpu_resource_flush *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_FLUSH);
	cmd_p->resource_id = cpu_to_le32(resource_id);
	cmd_p->r.width = cpu_to_le32(width);
	cmd_p->r.height = cpu_to_le32(height);
	cmd_p->r.x = cpu_to_le32(x);
	cmd_p->r.y = cpu_to_le32(y);

	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
}

void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
592
					uint64_t offset,
593 594
					uint32_t width, uint32_t height,
					uint32_t x, uint32_t y,
595
					struct virtio_gpu_object_array *objs,
596
					struct virtio_gpu_fence *fence)
D
Dave Airlie 已提交
597
{
598
	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(objs->objs[0]);
D
Dave Airlie 已提交
599 600
	struct virtio_gpu_transfer_to_host_2d *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;
601 602 603 604
	bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);

	if (use_dma_api)
		dma_sync_sg_for_device(vgdev->vdev->dev.parent,
605
				       bo->pages->sgl, bo->pages->nents,
606
				       DMA_TO_DEVICE);
D
Dave Airlie 已提交
607 608 609

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));
610
	vbuf->objs = objs;
D
Dave Airlie 已提交
611 612

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D);
613
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
D
Dave Airlie 已提交
614
	cmd_p->offset = cpu_to_le64(offset);
615 616 617 618
	cmd_p->r.width = cpu_to_le32(width);
	cmd_p->r.height = cpu_to_le32(height);
	cmd_p->r.x = cpu_to_le32(x);
	cmd_p->r.y = cpu_to_le32(y);
D
Dave Airlie 已提交
619

620
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
D
Dave Airlie 已提交
621 622 623 624 625 626 627
}

static void
virtio_gpu_cmd_resource_attach_backing(struct virtio_gpu_device *vgdev,
				       uint32_t resource_id,
				       struct virtio_gpu_mem_entry *ents,
				       uint32_t nents,
628
				       struct virtio_gpu_fence *fence)
D
Dave Airlie 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642
{
	struct virtio_gpu_resource_attach_backing *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING);
	cmd_p->resource_id = cpu_to_le32(resource_id);
	cmd_p->nr_entries = cpu_to_le32(nents);

	vbuf->data_buf = ents;
	vbuf->data_size = sizeof(*ents) * nents;

643
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
D
Dave Airlie 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
}

static void virtio_gpu_cmd_get_display_info_cb(struct virtio_gpu_device *vgdev,
					       struct virtio_gpu_vbuffer *vbuf)
{
	struct virtio_gpu_resp_display_info *resp =
		(struct virtio_gpu_resp_display_info *)vbuf->resp_buf;
	int i;

	spin_lock(&vgdev->display_info_lock);
	for (i = 0; i < vgdev->num_scanouts; i++) {
		vgdev->outputs[i].info = resp->pmodes[i];
		if (resp->pmodes[i].enabled) {
			DRM_DEBUG("output %d: %dx%d+%d+%d", i,
				  le32_to_cpu(resp->pmodes[i].r.width),
				  le32_to_cpu(resp->pmodes[i].r.height),
				  le32_to_cpu(resp->pmodes[i].r.x),
				  le32_to_cpu(resp->pmodes[i].r.y));
		} else {
			DRM_DEBUG("output %d: disabled", i);
		}
	}

667
	vgdev->display_info_pending = false;
D
Dave Airlie 已提交
668 669 670 671 672 673 674
	spin_unlock(&vgdev->display_info_lock);
	wake_up(&vgdev->resp_wq);

	if (!drm_helper_hpd_irq_event(vgdev->ddev))
		drm_kms_helper_hotplug_event(vgdev->ddev);
}

G
Gerd Hoffmann 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
static void virtio_gpu_cmd_get_capset_info_cb(struct virtio_gpu_device *vgdev,
					      struct virtio_gpu_vbuffer *vbuf)
{
	struct virtio_gpu_get_capset_info *cmd =
		(struct virtio_gpu_get_capset_info *)vbuf->buf;
	struct virtio_gpu_resp_capset_info *resp =
		(struct virtio_gpu_resp_capset_info *)vbuf->resp_buf;
	int i = le32_to_cpu(cmd->capset_index);

	spin_lock(&vgdev->display_info_lock);
	vgdev->capsets[i].id = le32_to_cpu(resp->capset_id);
	vgdev->capsets[i].max_version = le32_to_cpu(resp->capset_max_version);
	vgdev->capsets[i].max_size = le32_to_cpu(resp->capset_max_size);
	spin_unlock(&vgdev->display_info_lock);
	wake_up(&vgdev->resp_wq);
}

static void virtio_gpu_cmd_capset_cb(struct virtio_gpu_device *vgdev,
				     struct virtio_gpu_vbuffer *vbuf)
{
	struct virtio_gpu_get_capset *cmd =
		(struct virtio_gpu_get_capset *)vbuf->buf;
	struct virtio_gpu_resp_capset *resp =
		(struct virtio_gpu_resp_capset *)vbuf->resp_buf;
	struct virtio_gpu_drv_cap_cache *cache_ent;

	spin_lock(&vgdev->display_info_lock);
	list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
		if (cache_ent->version == le32_to_cpu(cmd->capset_version) &&
		    cache_ent->id == le32_to_cpu(cmd->capset_id)) {
			memcpy(cache_ent->caps_cache, resp->capset_data,
			       cache_ent->size);
707 708
			/* Copy must occur before is_valid is signalled. */
			smp_wmb();
G
Gerd Hoffmann 已提交
709 710 711 712 713
			atomic_set(&cache_ent->is_valid, 1);
			break;
		}
	}
	spin_unlock(&vgdev->display_info_lock);
714
	wake_up_all(&vgdev->resp_wq);
G
Gerd Hoffmann 已提交
715 716
}

G
Gerd Hoffmann 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
static int virtio_get_edid_block(void *data, u8 *buf,
				 unsigned int block, size_t len)
{
	struct virtio_gpu_resp_edid *resp = data;
	size_t start = block * EDID_LENGTH;

	if (start + len > le32_to_cpu(resp->size))
		return -1;
	memcpy(buf, resp->edid + start, len);
	return 0;
}

static void virtio_gpu_cmd_get_edid_cb(struct virtio_gpu_device *vgdev,
				       struct virtio_gpu_vbuffer *vbuf)
{
	struct virtio_gpu_cmd_get_edid *cmd =
		(struct virtio_gpu_cmd_get_edid *)vbuf->buf;
	struct virtio_gpu_resp_edid *resp =
		(struct virtio_gpu_resp_edid *)vbuf->resp_buf;
	uint32_t scanout = le32_to_cpu(cmd->scanout);
	struct virtio_gpu_output *output;
	struct edid *new_edid, *old_edid;

	if (scanout >= vgdev->num_scanouts)
		return;
	output = vgdev->outputs + scanout;

	new_edid = drm_do_get_edid(&output->conn, virtio_get_edid_block, resp);
745
	drm_connector_update_edid_property(&output->conn, new_edid);
G
Gerd Hoffmann 已提交
746 747 748 749 750 751 752 753 754 755

	spin_lock(&vgdev->display_info_lock);
	old_edid = output->edid;
	output->edid = new_edid;
	spin_unlock(&vgdev->display_info_lock);

	kfree(old_edid);
	wake_up(&vgdev->resp_wq);
}

D
Dave Airlie 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev)
{
	struct virtio_gpu_ctrl_hdr *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;
	void *resp_buf;

	resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_display_info),
			   GFP_KERNEL);
	if (!resp_buf)
		return -ENOMEM;

	cmd_p = virtio_gpu_alloc_cmd_resp
		(vgdev, &virtio_gpu_cmd_get_display_info_cb, &vbuf,
		 sizeof(*cmd_p), sizeof(struct virtio_gpu_resp_display_info),
		 resp_buf);
	memset(cmd_p, 0, sizeof(*cmd_p));

773
	vgdev->display_info_pending = true;
D
Dave Airlie 已提交
774 775 776 777 778
	cmd_p->type = cpu_to_le32(VIRTIO_GPU_CMD_GET_DISPLAY_INFO);
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
	return 0;
}

G
Gerd Hoffmann 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
int virtio_gpu_cmd_get_capset_info(struct virtio_gpu_device *vgdev, int idx)
{
	struct virtio_gpu_get_capset_info *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;
	void *resp_buf;

	resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_capset_info),
			   GFP_KERNEL);
	if (!resp_buf)
		return -ENOMEM;

	cmd_p = virtio_gpu_alloc_cmd_resp
		(vgdev, &virtio_gpu_cmd_get_capset_info_cb, &vbuf,
		 sizeof(*cmd_p), sizeof(struct virtio_gpu_resp_capset_info),
		 resp_buf);
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_GET_CAPSET_INFO);
	cmd_p->capset_index = cpu_to_le32(idx);
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
	return 0;
}

int virtio_gpu_cmd_get_capset(struct virtio_gpu_device *vgdev,
			      int idx, int version,
			      struct virtio_gpu_drv_cap_cache **cache_p)
{
	struct virtio_gpu_get_capset *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;
808
	int max_size;
G
Gerd Hoffmann 已提交
809
	struct virtio_gpu_drv_cap_cache *cache_ent;
810
	struct virtio_gpu_drv_cap_cache *search_ent;
G
Gerd Hoffmann 已提交
811 812
	void *resp_buf;

813 814
	*cache_p = NULL;

815
	if (idx >= vgdev->num_capsets)
G
Gerd Hoffmann 已提交
816 817 818 819 820 821 822 823 824
		return -EINVAL;

	if (version > vgdev->capsets[idx].max_version)
		return -EINVAL;

	cache_ent = kzalloc(sizeof(*cache_ent), GFP_KERNEL);
	if (!cache_ent)
		return -ENOMEM;

825
	max_size = vgdev->capsets[idx].max_size;
G
Gerd Hoffmann 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	cache_ent->caps_cache = kmalloc(max_size, GFP_KERNEL);
	if (!cache_ent->caps_cache) {
		kfree(cache_ent);
		return -ENOMEM;
	}

	resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_capset) + max_size,
			   GFP_KERNEL);
	if (!resp_buf) {
		kfree(cache_ent->caps_cache);
		kfree(cache_ent);
		return -ENOMEM;
	}

	cache_ent->version = version;
	cache_ent->id = vgdev->capsets[idx].id;
	atomic_set(&cache_ent->is_valid, 0);
	cache_ent->size = max_size;
	spin_lock(&vgdev->display_info_lock);
845 846 847 848 849 850 851 852 853 854
	/* Search while under lock in case it was added by another task. */
	list_for_each_entry(search_ent, &vgdev->cap_cache, head) {
		if (search_ent->id == vgdev->capsets[idx].id &&
		    search_ent->version == version) {
			*cache_p = search_ent;
			break;
		}
	}
	if (!*cache_p)
		list_add_tail(&cache_ent->head, &vgdev->cap_cache);
G
Gerd Hoffmann 已提交
855 856
	spin_unlock(&vgdev->display_info_lock);

857 858 859 860 861 862 863 864
	if (*cache_p) {
		/* Entry was found, so free everything that was just created. */
		kfree(resp_buf);
		kfree(cache_ent->caps_cache);
		kfree(cache_ent);
		return 0;
	}

G
Gerd Hoffmann 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877
	cmd_p = virtio_gpu_alloc_cmd_resp
		(vgdev, &virtio_gpu_cmd_capset_cb, &vbuf, sizeof(*cmd_p),
		 sizeof(struct virtio_gpu_resp_capset) + max_size,
		 resp_buf);
	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_GET_CAPSET);
	cmd_p->capset_id = cpu_to_le32(vgdev->capsets[idx].id);
	cmd_p->capset_version = cpu_to_le32(version);
	*cache_p = cache_ent;
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);

	return 0;
}

G
Gerd Hoffmann 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
int virtio_gpu_cmd_get_edids(struct virtio_gpu_device *vgdev)
{
	struct virtio_gpu_cmd_get_edid *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;
	void *resp_buf;
	int scanout;

	if (WARN_ON(!vgdev->has_edid))
		return -EINVAL;

	for (scanout = 0; scanout < vgdev->num_scanouts; scanout++) {
		resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_edid),
				   GFP_KERNEL);
		if (!resp_buf)
			return -ENOMEM;

		cmd_p = virtio_gpu_alloc_cmd_resp
			(vgdev, &virtio_gpu_cmd_get_edid_cb, &vbuf,
			 sizeof(*cmd_p), sizeof(struct virtio_gpu_resp_edid),
			 resp_buf);
		cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_GET_EDID);
		cmd_p->scanout = cpu_to_le32(scanout);
		virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
	}

	return 0;
}

G
Gerd Hoffmann 已提交
906 907 908 909 910 911 912 913 914 915 916 917
void virtio_gpu_cmd_context_create(struct virtio_gpu_device *vgdev, uint32_t id,
				   uint32_t nlen, const char *name)
{
	struct virtio_gpu_ctx_create *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_CREATE);
	cmd_p->hdr.ctx_id = cpu_to_le32(id);
	cmd_p->nlen = cpu_to_le32(nlen);
918 919
	strncpy(cmd_p->debug_name, name, sizeof(cmd_p->debug_name) - 1);
	cmd_p->debug_name[sizeof(cmd_p->debug_name) - 1] = 0;
G
Gerd Hoffmann 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
}

void virtio_gpu_cmd_context_destroy(struct virtio_gpu_device *vgdev,
				    uint32_t id)
{
	struct virtio_gpu_ctx_destroy *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_DESTROY);
	cmd_p->hdr.ctx_id = cpu_to_le32(id);
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
}

void virtio_gpu_cmd_context_attach_resource(struct virtio_gpu_device *vgdev,
					    uint32_t ctx_id,
939
					    struct virtio_gpu_object_array *objs)
G
Gerd Hoffmann 已提交
940
{
941
	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(objs->objs[0]);
G
Gerd Hoffmann 已提交
942 943 944 945 946
	struct virtio_gpu_ctx_resource *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));
947
	vbuf->objs = objs;
G
Gerd Hoffmann 已提交
948 949 950

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE);
	cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
951
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
G
Gerd Hoffmann 已提交
952 953 954 955 956 957
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);

}

void virtio_gpu_cmd_context_detach_resource(struct virtio_gpu_device *vgdev,
					    uint32_t ctx_id,
958
					    struct virtio_gpu_object_array *objs)
G
Gerd Hoffmann 已提交
959
{
960
	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(objs->objs[0]);
G
Gerd Hoffmann 已提交
961 962 963 964 965
	struct virtio_gpu_ctx_resource *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));
966
	vbuf->objs = objs;
G
Gerd Hoffmann 已提交
967 968 969

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE);
	cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
970
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
G
Gerd Hoffmann 已提交
971 972 973 974 975
	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
}

void
virtio_gpu_cmd_resource_create_3d(struct virtio_gpu_device *vgdev,
976
				  struct virtio_gpu_object *bo,
977
				  struct virtio_gpu_object_params *params,
978
				  struct virtio_gpu_object_array *objs,
979
				  struct virtio_gpu_fence *fence)
G
Gerd Hoffmann 已提交
980 981 982 983 984 985
{
	struct virtio_gpu_resource_create_3d *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));
986
	vbuf->objs = objs;
G
Gerd Hoffmann 已提交
987 988

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_CREATE_3D);
989 990 991 992 993 994 995 996 997 998 999 1000
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
	cmd_p->format = cpu_to_le32(params->format);
	cmd_p->width = cpu_to_le32(params->width);
	cmd_p->height = cpu_to_le32(params->height);

	cmd_p->target = cpu_to_le32(params->target);
	cmd_p->bind = cpu_to_le32(params->bind);
	cmd_p->depth = cpu_to_le32(params->depth);
	cmd_p->array_size = cpu_to_le32(params->array_size);
	cmd_p->last_level = cpu_to_le32(params->last_level);
	cmd_p->nr_samples = cpu_to_le32(params->nr_samples);
	cmd_p->flags = cpu_to_le32(params->flags);
G
Gerd Hoffmann 已提交
1001

1002
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
1003
	bo->created = true;
G
Gerd Hoffmann 已提交
1004 1005 1006
}

void virtio_gpu_cmd_transfer_to_host_3d(struct virtio_gpu_device *vgdev,
1007
					uint32_t ctx_id,
G
Gerd Hoffmann 已提交
1008
					uint64_t offset, uint32_t level,
1009
					struct drm_virtgpu_3d_box *box,
1010
					struct virtio_gpu_object_array *objs,
1011
					struct virtio_gpu_fence *fence)
G
Gerd Hoffmann 已提交
1012
{
1013
	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(objs->objs[0]);
G
Gerd Hoffmann 已提交
1014 1015
	struct virtio_gpu_transfer_host_3d *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;
1016 1017 1018 1019
	bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);

	if (use_dma_api)
		dma_sync_sg_for_device(vgdev->vdev->dev.parent,
1020
				       bo->pages->sgl, bo->pages->nents,
1021
				       DMA_TO_DEVICE);
G
Gerd Hoffmann 已提交
1022 1023 1024 1025

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

1026 1027
	vbuf->objs = objs;

G
Gerd Hoffmann 已提交
1028 1029
	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D);
	cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
1030
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
1031
	convert_to_hw_box(&cmd_p->box, box);
G
Gerd Hoffmann 已提交
1032 1033 1034
	cmd_p->offset = cpu_to_le64(offset);
	cmd_p->level = cpu_to_le32(level);

1035
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
G
Gerd Hoffmann 已提交
1036 1037 1038
}

void virtio_gpu_cmd_transfer_from_host_3d(struct virtio_gpu_device *vgdev,
1039
					  uint32_t ctx_id,
G
Gerd Hoffmann 已提交
1040
					  uint64_t offset, uint32_t level,
1041
					  struct drm_virtgpu_3d_box *box,
1042
					  struct virtio_gpu_object_array *objs,
1043
					  struct virtio_gpu_fence *fence)
G
Gerd Hoffmann 已提交
1044
{
1045
	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(objs->objs[0]);
G
Gerd Hoffmann 已提交
1046 1047 1048 1049 1050 1051
	struct virtio_gpu_transfer_host_3d *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

1052 1053
	vbuf->objs = objs;

G
Gerd Hoffmann 已提交
1054 1055
	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D);
	cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
1056
	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
1057
	convert_to_hw_box(&cmd_p->box, box);
G
Gerd Hoffmann 已提交
1058 1059 1060
	cmd_p->offset = cpu_to_le64(offset);
	cmd_p->level = cpu_to_le32(level);

1061
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
G
Gerd Hoffmann 已提交
1062 1063 1064 1065
}

void virtio_gpu_cmd_submit(struct virtio_gpu_device *vgdev,
			   void *data, uint32_t data_size,
1066 1067 1068
			   uint32_t ctx_id,
			   struct virtio_gpu_object_array *objs,
			   struct virtio_gpu_fence *fence)
G
Gerd Hoffmann 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077
{
	struct virtio_gpu_cmd_submit *cmd_p;
	struct virtio_gpu_vbuffer *vbuf;

	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
	memset(cmd_p, 0, sizeof(*cmd_p));

	vbuf->data_buf = data;
	vbuf->data_size = data_size;
1078
	vbuf->objs = objs;
G
Gerd Hoffmann 已提交
1079 1080 1081 1082 1083

	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_SUBMIT_3D);
	cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
	cmd_p->size = cpu_to_le32(data_size);

1084
	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
G
Gerd Hoffmann 已提交
1085 1086
}

D
Dave Airlie 已提交
1087 1088
int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
			     struct virtio_gpu_object *obj,
1089
			     struct virtio_gpu_fence *fence)
D
Dave Airlie 已提交
1090
{
G
Gerd Hoffmann 已提交
1091
	bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
D
Dave Airlie 已提交
1092 1093
	struct virtio_gpu_mem_entry *ents;
	struct scatterlist *sg;
1094
	int si, nents, ret;
D
Dave Airlie 已提交
1095

1096 1097
	if (WARN_ON_ONCE(!obj->created))
		return -EINVAL;
1098 1099
	if (WARN_ON_ONCE(obj->pages))
		return -EINVAL;
1100

1101 1102 1103
	ret = drm_gem_shmem_pin(&obj->base.base);
	if (ret < 0)
		return -EINVAL;
1104

1105 1106 1107 1108
	obj->pages = drm_gem_shmem_get_sg_table(&obj->base.base);
	if (obj->pages == NULL) {
		drm_gem_shmem_unpin(&obj->base.base);
		return -EINVAL;
D
Dave Airlie 已提交
1109 1110
	}

G
Gerd Hoffmann 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119
	if (use_dma_api) {
		obj->mapped = dma_map_sg(vgdev->vdev->dev.parent,
					 obj->pages->sgl, obj->pages->nents,
					 DMA_TO_DEVICE);
		nents = obj->mapped;
	} else {
		nents = obj->pages->nents;
	}

D
Dave Airlie 已提交
1120
	/* gets freed when the ring has consumed it */
G
Gerd Hoffmann 已提交
1121
	ents = kmalloc_array(nents, sizeof(struct virtio_gpu_mem_entry),
D
Dave Airlie 已提交
1122 1123 1124 1125 1126 1127
			     GFP_KERNEL);
	if (!ents) {
		DRM_ERROR("failed to allocate ent list\n");
		return -ENOMEM;
	}

G
Gerd Hoffmann 已提交
1128 1129 1130 1131
	for_each_sg(obj->pages->sgl, sg, nents, si) {
		ents[si].addr = cpu_to_le64(use_dma_api
					    ? sg_dma_address(sg)
					    : sg_phys(sg));
D
Dave Airlie 已提交
1132 1133 1134 1135
		ents[si].length = cpu_to_le32(sg->length);
		ents[si].padding = 0;
	}

1136
	virtio_gpu_cmd_resource_attach_backing(vgdev, obj->hw_res_handle,
G
Gerd Hoffmann 已提交
1137
					       ents, nents,
D
Dave Airlie 已提交
1138 1139 1140 1141
					       fence);
	return 0;
}

1142 1143 1144
void virtio_gpu_object_detach(struct virtio_gpu_device *vgdev,
			      struct virtio_gpu_object *obj)
{
G
Gerd Hoffmann 已提交
1145 1146
	bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);

1147 1148 1149
	if (WARN_ON_ONCE(!obj->pages))
		return;

G
Gerd Hoffmann 已提交
1150
	if (use_dma_api && obj->mapped) {
1151
		struct virtio_gpu_fence *fence = virtio_gpu_fence_alloc(vgdev);
G
Gerd Hoffmann 已提交
1152
		/* detach backing and wait for the host process it ... */
1153
		virtio_gpu_cmd_resource_inval_backing(vgdev, obj->hw_res_handle, fence);
G
Gerd Hoffmann 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
		dma_fence_wait(&fence->f, true);
		dma_fence_put(&fence->f);

		/* ... then tear down iommu mappings */
		dma_unmap_sg(vgdev->vdev->dev.parent,
			     obj->pages->sgl, obj->mapped,
			     DMA_TO_DEVICE);
		obj->mapped = 0;
	} else {
		virtio_gpu_cmd_resource_inval_backing(vgdev, obj->hw_res_handle, NULL);
	}
1165 1166 1167 1168 1169

	sg_free_table(obj->pages);
	obj->pages = NULL;

	drm_gem_shmem_unpin(&obj->base.base);
1170 1171
}

D
Dave Airlie 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
			    struct virtio_gpu_output *output)
{
	struct virtio_gpu_vbuffer *vbuf;
	struct virtio_gpu_update_cursor *cur_p;

	output->cursor.pos.scanout_id = cpu_to_le32(output->index);
	cur_p = virtio_gpu_alloc_cursor(vgdev, &vbuf);
	memcpy(cur_p, &output->cursor, sizeof(output->cursor));
	virtio_gpu_queue_cursor(vgdev, vbuf);
}