xref: /linux/samples/vfio-mdev/mbochs.c (revision bdef2b7896df293736330eb6eb0f43947049b828)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Mediated virtual PCI display host device driver
4  *
5  * Emulate enough of qemu stdvga to make bochs-drm.ko happy.  That is
6  * basically the vram memory bar and the bochs dispi interface vbe
7  * registers in the mmio register bar.	Specifically it does *not*
8  * include any legacy vga stuff.  Device looks a lot like "qemu -device
9  * secondary-vga".
10  *
11  *   (c) Gerd Hoffmann <kraxel@redhat.com>
12  *
13  * based on mtty driver which is:
14  *   Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
15  *	 Author: Neo Jia <cjia@nvidia.com>
16  *		 Kirti Wankhede <kwankhede@nvidia.com>
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2 as
20  * published by the Free Software Foundation.
21  */
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/cdev.h>
28 #include <linux/vfio.h>
29 #include <linux/iommu.h>
30 #include <linux/sysfs.h>
31 #include <linux/mdev.h>
32 #include <linux/pci.h>
33 #include <linux/dma-buf.h>
34 #include <linux/highmem.h>
35 #include <drm/drm_fourcc.h>
36 #include <drm/drm_rect.h>
37 #include <drm/drm_modeset_lock.h>
38 #include <drm/drm_property.h>
39 #include <drm/drm_plane.h>
40 
41 
42 #define VBE_DISPI_INDEX_ID		0x0
43 #define VBE_DISPI_INDEX_XRES		0x1
44 #define VBE_DISPI_INDEX_YRES		0x2
45 #define VBE_DISPI_INDEX_BPP		0x3
46 #define VBE_DISPI_INDEX_ENABLE		0x4
47 #define VBE_DISPI_INDEX_BANK		0x5
48 #define VBE_DISPI_INDEX_VIRT_WIDTH	0x6
49 #define VBE_DISPI_INDEX_VIRT_HEIGHT	0x7
50 #define VBE_DISPI_INDEX_X_OFFSET	0x8
51 #define VBE_DISPI_INDEX_Y_OFFSET	0x9
52 #define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
53 #define VBE_DISPI_INDEX_COUNT		0xb
54 
55 #define VBE_DISPI_ID0			0xB0C0
56 #define VBE_DISPI_ID1			0xB0C1
57 #define VBE_DISPI_ID2			0xB0C2
58 #define VBE_DISPI_ID3			0xB0C3
59 #define VBE_DISPI_ID4			0xB0C4
60 #define VBE_DISPI_ID5			0xB0C5
61 
62 #define VBE_DISPI_DISABLED		0x00
63 #define VBE_DISPI_ENABLED		0x01
64 #define VBE_DISPI_GETCAPS		0x02
65 #define VBE_DISPI_8BIT_DAC		0x20
66 #define VBE_DISPI_LFB_ENABLED		0x40
67 #define VBE_DISPI_NOCLEARMEM		0x80
68 
69 
70 #define MBOCHS_NAME		  "mbochs"
71 #define MBOCHS_CLASS_NAME	  "mbochs"
72 
73 #define MBOCHS_EDID_REGION_INDEX  VFIO_PCI_NUM_REGIONS
74 #define MBOCHS_NUM_REGIONS        (MBOCHS_EDID_REGION_INDEX+1)
75 
76 #define MBOCHS_CONFIG_SPACE_SIZE  0xff
77 #define MBOCHS_MMIO_BAR_OFFSET	  PAGE_SIZE
78 #define MBOCHS_MMIO_BAR_SIZE	  PAGE_SIZE
79 #define MBOCHS_EDID_OFFSET	  (MBOCHS_MMIO_BAR_OFFSET +	\
80 				   MBOCHS_MMIO_BAR_SIZE)
81 #define MBOCHS_EDID_SIZE	  PAGE_SIZE
82 #define MBOCHS_MEMORY_BAR_OFFSET  (MBOCHS_EDID_OFFSET + \
83 				   MBOCHS_EDID_SIZE)
84 
85 #define MBOCHS_EDID_BLOB_OFFSET   (MBOCHS_EDID_SIZE/2)
86 
87 #define STORE_LE16(addr, val)	(*(u16 *)addr = val)
88 #define STORE_LE32(addr, val)	(*(u32 *)addr = val)
89 
90 
91 MODULE_LICENSE("GPL v2");
92 
93 static int max_mbytes = 256;
94 module_param_named(count, max_mbytes, int, 0444);
95 MODULE_PARM_DESC(mem, "megabytes available to " MBOCHS_NAME " devices");
96 
97 
98 #define MBOCHS_TYPE_1 "small"
99 #define MBOCHS_TYPE_2 "medium"
100 #define MBOCHS_TYPE_3 "large"
101 
102 static const struct mbochs_type {
103 	const char *name;
104 	u32 mbytes;
105 	u32 max_x;
106 	u32 max_y;
107 } mbochs_types[] = {
108 	{
109 		.name	= MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_1,
110 		.mbytes = 4,
111 		.max_x  = 800,
112 		.max_y  = 600,
113 	}, {
114 		.name	= MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_2,
115 		.mbytes = 16,
116 		.max_x  = 1920,
117 		.max_y  = 1440,
118 	}, {
119 		.name	= MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_3,
120 		.mbytes = 64,
121 		.max_x  = 0,
122 		.max_y  = 0,
123 	},
124 };
125 
126 
127 static dev_t		mbochs_devt;
128 static struct class	*mbochs_class;
129 static struct cdev	mbochs_cdev;
130 static struct device	mbochs_dev;
131 static atomic_t mbochs_avail_mbytes;
132 static const struct vfio_device_ops mbochs_dev_ops;
133 
134 struct vfio_region_info_ext {
135 	struct vfio_region_info          base;
136 	struct vfio_region_info_cap_type type;
137 };
138 
139 struct mbochs_mode {
140 	u32 drm_format;
141 	u32 bytepp;
142 	u32 width;
143 	u32 height;
144 	u32 stride;
145 	u32 __pad;
146 	u64 offset;
147 	u64 size;
148 };
149 
150 struct mbochs_dmabuf {
151 	struct mbochs_mode mode;
152 	u32 id;
153 	struct page **pages;
154 	pgoff_t pagecount;
155 	struct dma_buf *buf;
156 	struct mdev_state *mdev_state;
157 	struct list_head next;
158 	bool unlinked;
159 };
160 
161 /* State of each mdev device */
162 struct mdev_state {
163 	struct vfio_device vdev;
164 	u8 *vconfig;
165 	u64 bar_mask[3];
166 	u32 memory_bar_mask;
167 	struct mutex ops_lock;
168 	struct mdev_device *mdev;
169 
170 	const struct mbochs_type *type;
171 	u16 vbe[VBE_DISPI_INDEX_COUNT];
172 	u64 memsize;
173 	struct page **pages;
174 	pgoff_t pagecount;
175 	struct vfio_region_gfx_edid edid_regs;
176 	u8 edid_blob[0x400];
177 
178 	struct list_head dmabufs;
179 	u32 active_id;
180 	u32 next_id;
181 };
182 
183 static const char *vbe_name_list[VBE_DISPI_INDEX_COUNT] = {
184 	[VBE_DISPI_INDEX_ID]               = "id",
185 	[VBE_DISPI_INDEX_XRES]             = "xres",
186 	[VBE_DISPI_INDEX_YRES]             = "yres",
187 	[VBE_DISPI_INDEX_BPP]              = "bpp",
188 	[VBE_DISPI_INDEX_ENABLE]           = "enable",
189 	[VBE_DISPI_INDEX_BANK]             = "bank",
190 	[VBE_DISPI_INDEX_VIRT_WIDTH]       = "virt-width",
191 	[VBE_DISPI_INDEX_VIRT_HEIGHT]      = "virt-height",
192 	[VBE_DISPI_INDEX_X_OFFSET]         = "x-offset",
193 	[VBE_DISPI_INDEX_Y_OFFSET]         = "y-offset",
194 	[VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = "video-mem",
195 };
196 
197 static const char *vbe_name(u32 index)
198 {
199 	if (index < ARRAY_SIZE(vbe_name_list))
200 		return vbe_name_list[index];
201 	return "(invalid)";
202 }
203 
204 static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
205 				      pgoff_t pgoff);
206 static struct page *mbochs_get_page(struct mdev_state *mdev_state,
207 				    pgoff_t pgoff);
208 
209 static void mbochs_create_config_space(struct mdev_state *mdev_state)
210 {
211 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
212 		   0x1234);
213 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
214 		   0x1111);
215 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
216 		   PCI_SUBVENDOR_ID_REDHAT_QUMRANET);
217 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
218 		   PCI_SUBDEVICE_ID_QEMU);
219 
220 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
221 		   PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
222 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
223 		   PCI_CLASS_DISPLAY_OTHER);
224 	mdev_state->vconfig[PCI_CLASS_REVISION] =  0x01;
225 
226 	STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
227 		   PCI_BASE_ADDRESS_SPACE_MEMORY |
228 		   PCI_BASE_ADDRESS_MEM_TYPE_32	 |
229 		   PCI_BASE_ADDRESS_MEM_PREFETCH);
230 	mdev_state->bar_mask[0] = ~(mdev_state->memsize) + 1;
231 
232 	STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_2],
233 		   PCI_BASE_ADDRESS_SPACE_MEMORY |
234 		   PCI_BASE_ADDRESS_MEM_TYPE_32);
235 	mdev_state->bar_mask[2] = ~(MBOCHS_MMIO_BAR_SIZE) + 1;
236 }
237 
238 static int mbochs_check_framebuffer(struct mdev_state *mdev_state,
239 				    struct mbochs_mode *mode)
240 {
241 	struct device *dev = mdev_dev(mdev_state->mdev);
242 	u16 *vbe = mdev_state->vbe;
243 	u32 virt_width;
244 
245 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
246 
247 	if (!(vbe[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED))
248 		goto nofb;
249 
250 	memset(mode, 0, sizeof(*mode));
251 	switch (vbe[VBE_DISPI_INDEX_BPP]) {
252 	case 32:
253 		mode->drm_format = DRM_FORMAT_XRGB8888;
254 		mode->bytepp = 4;
255 		break;
256 	default:
257 		dev_info_ratelimited(dev, "%s: bpp %d not supported\n",
258 				     __func__, vbe[VBE_DISPI_INDEX_BPP]);
259 		goto nofb;
260 	}
261 
262 	mode->width  = vbe[VBE_DISPI_INDEX_XRES];
263 	mode->height = vbe[VBE_DISPI_INDEX_YRES];
264 	virt_width  = vbe[VBE_DISPI_INDEX_VIRT_WIDTH];
265 	if (virt_width < mode->width)
266 		virt_width = mode->width;
267 	mode->stride = virt_width * mode->bytepp;
268 	mode->size   = (u64)mode->stride * mode->height;
269 	mode->offset = ((u64)vbe[VBE_DISPI_INDEX_X_OFFSET] * mode->bytepp +
270 		       (u64)vbe[VBE_DISPI_INDEX_Y_OFFSET] * mode->stride);
271 
272 	if (mode->width < 64 || mode->height < 64) {
273 		dev_info_ratelimited(dev, "%s: invalid resolution %dx%d\n",
274 				     __func__, mode->width, mode->height);
275 		goto nofb;
276 	}
277 	if (mode->offset + mode->size > mdev_state->memsize) {
278 		dev_info_ratelimited(dev, "%s: framebuffer memory overflow\n",
279 				     __func__);
280 		goto nofb;
281 	}
282 
283 	return 0;
284 
285 nofb:
286 	memset(mode, 0, sizeof(*mode));
287 	return -EINVAL;
288 }
289 
290 static bool mbochs_modes_equal(struct mbochs_mode *mode1,
291 			       struct mbochs_mode *mode2)
292 {
293 	return memcmp(mode1, mode2, sizeof(struct mbochs_mode)) == 0;
294 }
295 
296 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
297 				 char *buf, u32 count)
298 {
299 	struct device *dev = mdev_dev(mdev_state->mdev);
300 	int index = (offset - PCI_BASE_ADDRESS_0) / 0x04;
301 	u32 cfg_addr;
302 
303 	switch (offset) {
304 	case PCI_BASE_ADDRESS_0:
305 	case PCI_BASE_ADDRESS_2:
306 		cfg_addr = *(u32 *)buf;
307 
308 		if (cfg_addr == 0xffffffff) {
309 			cfg_addr = (cfg_addr & mdev_state->bar_mask[index]);
310 		} else {
311 			cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
312 			if (cfg_addr)
313 				dev_info(dev, "BAR #%d @ 0x%x\n",
314 					 index, cfg_addr);
315 		}
316 
317 		cfg_addr |= (mdev_state->vconfig[offset] &
318 			     ~PCI_BASE_ADDRESS_MEM_MASK);
319 		STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
320 		break;
321 	}
322 }
323 
324 static void handle_mmio_write(struct mdev_state *mdev_state, u16 offset,
325 			      char *buf, u32 count)
326 {
327 	struct device *dev = mdev_dev(mdev_state->mdev);
328 	int index;
329 	u16 reg16;
330 
331 	switch (offset) {
332 	case 0x400 ... 0x41f: /* vga ioports remapped */
333 		goto unhandled;
334 	case 0x500 ... 0x515: /* bochs dispi interface */
335 		if (count != 2)
336 			goto unhandled;
337 		index = (offset - 0x500) / 2;
338 		reg16 = *(u16 *)buf;
339 		if (index < ARRAY_SIZE(mdev_state->vbe))
340 			mdev_state->vbe[index] = reg16;
341 		dev_dbg(dev, "%s: vbe write %d = %d (%s)\n",
342 			__func__, index, reg16, vbe_name(index));
343 		break;
344 	case 0x600 ... 0x607: /* qemu extended regs */
345 		goto unhandled;
346 	default:
347 unhandled:
348 		dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n",
349 			__func__, offset, count);
350 		break;
351 	}
352 }
353 
354 static void handle_mmio_read(struct mdev_state *mdev_state, u16 offset,
355 			     char *buf, u32 count)
356 {
357 	struct device *dev = mdev_dev(mdev_state->mdev);
358 	struct vfio_region_gfx_edid *edid;
359 	u16 reg16 = 0;
360 	int index;
361 
362 	switch (offset) {
363 	case 0x000 ... 0x3ff: /* edid block */
364 		edid = &mdev_state->edid_regs;
365 		if (edid->link_state != VFIO_DEVICE_GFX_LINK_STATE_UP ||
366 		    offset >= edid->edid_size) {
367 			memset(buf, 0, count);
368 			break;
369 		}
370 		memcpy(buf, mdev_state->edid_blob + offset, count);
371 		break;
372 	case 0x500 ... 0x515: /* bochs dispi interface */
373 		if (count != 2)
374 			goto unhandled;
375 		index = (offset - 0x500) / 2;
376 		if (index < ARRAY_SIZE(mdev_state->vbe))
377 			reg16 = mdev_state->vbe[index];
378 		dev_dbg(dev, "%s: vbe read %d = %d (%s)\n",
379 			__func__, index, reg16, vbe_name(index));
380 		*(u16 *)buf = reg16;
381 		break;
382 	default:
383 unhandled:
384 		dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n",
385 			__func__, offset, count);
386 		memset(buf, 0, count);
387 		break;
388 	}
389 }
390 
391 static void handle_edid_regs(struct mdev_state *mdev_state, u16 offset,
392 			     char *buf, u32 count, bool is_write)
393 {
394 	char *regs = (void *)&mdev_state->edid_regs;
395 
396 	if (offset + count > sizeof(mdev_state->edid_regs))
397 		return;
398 	if (count != 4)
399 		return;
400 	if (offset % 4)
401 		return;
402 
403 	if (is_write) {
404 		switch (offset) {
405 		case offsetof(struct vfio_region_gfx_edid, link_state):
406 		case offsetof(struct vfio_region_gfx_edid, edid_size):
407 			memcpy(regs + offset, buf, count);
408 			break;
409 		default:
410 			/* read-only regs */
411 			break;
412 		}
413 	} else {
414 		memcpy(buf, regs + offset, count);
415 	}
416 }
417 
418 static void handle_edid_blob(struct mdev_state *mdev_state, u16 offset,
419 			     char *buf, u32 count, bool is_write)
420 {
421 	if (offset + count > mdev_state->edid_regs.edid_max_size)
422 		return;
423 	if (is_write)
424 		memcpy(mdev_state->edid_blob + offset, buf, count);
425 	else
426 		memcpy(buf, mdev_state->edid_blob + offset, count);
427 }
428 
429 static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
430 			   size_t count, loff_t pos, bool is_write)
431 {
432 	struct page *pg;
433 	loff_t poff;
434 	char *map;
435 	int ret = 0;
436 
437 	mutex_lock(&mdev_state->ops_lock);
438 
439 	if (pos < MBOCHS_CONFIG_SPACE_SIZE) {
440 		if (is_write)
441 			handle_pci_cfg_write(mdev_state, pos, buf, count);
442 		else
443 			memcpy(buf, (mdev_state->vconfig + pos), count);
444 
445 	} else if (pos >= MBOCHS_MMIO_BAR_OFFSET &&
446 		   pos + count <= (MBOCHS_MMIO_BAR_OFFSET +
447 				   MBOCHS_MMIO_BAR_SIZE)) {
448 		pos -= MBOCHS_MMIO_BAR_OFFSET;
449 		if (is_write)
450 			handle_mmio_write(mdev_state, pos, buf, count);
451 		else
452 			handle_mmio_read(mdev_state, pos, buf, count);
453 
454 	} else if (pos >= MBOCHS_EDID_OFFSET &&
455 		   pos + count <= (MBOCHS_EDID_OFFSET +
456 				   MBOCHS_EDID_SIZE)) {
457 		pos -= MBOCHS_EDID_OFFSET;
458 		if (pos < MBOCHS_EDID_BLOB_OFFSET) {
459 			handle_edid_regs(mdev_state, pos, buf, count, is_write);
460 		} else {
461 			pos -= MBOCHS_EDID_BLOB_OFFSET;
462 			handle_edid_blob(mdev_state, pos, buf, count, is_write);
463 		}
464 
465 	} else if (pos >= MBOCHS_MEMORY_BAR_OFFSET &&
466 		   pos + count <=
467 		   MBOCHS_MEMORY_BAR_OFFSET + mdev_state->memsize) {
468 		pos -= MBOCHS_MMIO_BAR_OFFSET;
469 		poff = pos & ~PAGE_MASK;
470 		pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
471 		map = kmap(pg);
472 		if (is_write)
473 			memcpy(map + poff, buf, count);
474 		else
475 			memcpy(buf, map + poff, count);
476 		kunmap(pg);
477 		put_page(pg);
478 
479 	} else {
480 		dev_dbg(mdev_state->vdev.dev, "%s: %s @0x%llx (unhandled)\n",
481 			__func__, is_write ? "WR" : "RD", pos);
482 		ret = -1;
483 		goto accessfailed;
484 	}
485 
486 	ret = count;
487 
488 
489 accessfailed:
490 	mutex_unlock(&mdev_state->ops_lock);
491 
492 	return ret;
493 }
494 
495 static int mbochs_reset(struct mdev_state *mdev_state)
496 {
497 	u32 size64k = mdev_state->memsize / (64 * 1024);
498 	int i;
499 
500 	for (i = 0; i < ARRAY_SIZE(mdev_state->vbe); i++)
501 		mdev_state->vbe[i] = 0;
502 	mdev_state->vbe[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
503 	mdev_state->vbe[VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = size64k;
504 	return 0;
505 }
506 
507 static int mbochs_init_dev(struct vfio_device *vdev)
508 {
509 	struct mdev_state *mdev_state =
510 		container_of(vdev, struct mdev_state, vdev);
511 	struct mdev_device *mdev = to_mdev_device(vdev->dev);
512 	const struct mbochs_type *type =
513 		&mbochs_types[mdev_get_type_group_id(mdev)];
514 	int avail_mbytes = atomic_read(&mbochs_avail_mbytes);
515 	int ret = -ENOMEM;
516 
517 	do {
518 		if (avail_mbytes < type->mbytes)
519 			return -ENOSPC;
520 	} while (!atomic_try_cmpxchg(&mbochs_avail_mbytes, &avail_mbytes,
521 				     avail_mbytes - type->mbytes));
522 
523 	mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
524 	if (!mdev_state->vconfig)
525 		goto err_avail;
526 
527 	mdev_state->memsize = type->mbytes * 1024 * 1024;
528 	mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT;
529 	mdev_state->pages = kcalloc(mdev_state->pagecount,
530 				    sizeof(struct page *),
531 				    GFP_KERNEL);
532 	if (!mdev_state->pages)
533 		goto err_vconfig;
534 
535 	mutex_init(&mdev_state->ops_lock);
536 	mdev_state->mdev = mdev;
537 	INIT_LIST_HEAD(&mdev_state->dmabufs);
538 	mdev_state->next_id = 1;
539 
540 	mdev_state->type = type;
541 	mdev_state->edid_regs.max_xres = type->max_x;
542 	mdev_state->edid_regs.max_yres = type->max_y;
543 	mdev_state->edid_regs.edid_offset = MBOCHS_EDID_BLOB_OFFSET;
544 	mdev_state->edid_regs.edid_max_size = sizeof(mdev_state->edid_blob);
545 	mbochs_create_config_space(mdev_state);
546 	mbochs_reset(mdev_state);
547 
548 	dev_info(vdev->dev, "%s: %s, %d MB, %ld pages\n", __func__,
549 		 type->name, type->mbytes, mdev_state->pagecount);
550 	return 0;
551 
552 err_vconfig:
553 	kfree(mdev_state->vconfig);
554 err_avail:
555 	atomic_add(type->mbytes, &mbochs_avail_mbytes);
556 	return ret;
557 }
558 
559 static int mbochs_probe(struct mdev_device *mdev)
560 {
561 	struct mdev_state *mdev_state;
562 	int ret = -ENOMEM;
563 
564 	mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
565 				       &mbochs_dev_ops);
566 	if (IS_ERR(mdev_state))
567 		return PTR_ERR(mdev_state);
568 
569 	ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
570 	if (ret)
571 		goto err_put_vdev;
572 	dev_set_drvdata(&mdev->dev, mdev_state);
573 	return 0;
574 
575 err_put_vdev:
576 	vfio_put_device(&mdev_state->vdev);
577 	return ret;
578 }
579 
580 static void mbochs_release_dev(struct vfio_device *vdev)
581 {
582 	struct mdev_state *mdev_state =
583 		container_of(vdev, struct mdev_state, vdev);
584 
585 	atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
586 	kfree(mdev_state->pages);
587 	kfree(mdev_state->vconfig);
588 	vfio_free_device(vdev);
589 }
590 
591 static void mbochs_remove(struct mdev_device *mdev)
592 {
593 	struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
594 
595 	vfio_unregister_group_dev(&mdev_state->vdev);
596 	vfio_put_device(&mdev_state->vdev);
597 }
598 
599 static ssize_t mbochs_read(struct vfio_device *vdev, char __user *buf,
600 			   size_t count, loff_t *ppos)
601 {
602 	struct mdev_state *mdev_state =
603 		container_of(vdev, struct mdev_state, vdev);
604 	unsigned int done = 0;
605 	int ret;
606 
607 	while (count) {
608 		size_t filled;
609 
610 		if (count >= 4 && !(*ppos % 4)) {
611 			u32 val;
612 
613 			ret =  mdev_access(mdev_state, (char *)&val, sizeof(val),
614 					   *ppos, false);
615 			if (ret <= 0)
616 				goto read_err;
617 
618 			if (copy_to_user(buf, &val, sizeof(val)))
619 				goto read_err;
620 
621 			filled = 4;
622 		} else if (count >= 2 && !(*ppos % 2)) {
623 			u16 val;
624 
625 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
626 					  *ppos, false);
627 			if (ret <= 0)
628 				goto read_err;
629 
630 			if (copy_to_user(buf, &val, sizeof(val)))
631 				goto read_err;
632 
633 			filled = 2;
634 		} else {
635 			u8 val;
636 
637 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
638 					  *ppos, false);
639 			if (ret <= 0)
640 				goto read_err;
641 
642 			if (copy_to_user(buf, &val, sizeof(val)))
643 				goto read_err;
644 
645 			filled = 1;
646 		}
647 
648 		count -= filled;
649 		done += filled;
650 		*ppos += filled;
651 		buf += filled;
652 	}
653 
654 	return done;
655 
656 read_err:
657 	return -EFAULT;
658 }
659 
660 static ssize_t mbochs_write(struct vfio_device *vdev, const char __user *buf,
661 			    size_t count, loff_t *ppos)
662 {
663 	struct mdev_state *mdev_state =
664 		container_of(vdev, struct mdev_state, vdev);
665 	unsigned int done = 0;
666 	int ret;
667 
668 	while (count) {
669 		size_t filled;
670 
671 		if (count >= 4 && !(*ppos % 4)) {
672 			u32 val;
673 
674 			if (copy_from_user(&val, buf, sizeof(val)))
675 				goto write_err;
676 
677 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
678 					  *ppos, true);
679 			if (ret <= 0)
680 				goto write_err;
681 
682 			filled = 4;
683 		} else if (count >= 2 && !(*ppos % 2)) {
684 			u16 val;
685 
686 			if (copy_from_user(&val, buf, sizeof(val)))
687 				goto write_err;
688 
689 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
690 					  *ppos, true);
691 			if (ret <= 0)
692 				goto write_err;
693 
694 			filled = 2;
695 		} else {
696 			u8 val;
697 
698 			if (copy_from_user(&val, buf, sizeof(val)))
699 				goto write_err;
700 
701 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
702 					  *ppos, true);
703 			if (ret <= 0)
704 				goto write_err;
705 
706 			filled = 1;
707 		}
708 		count -= filled;
709 		done += filled;
710 		*ppos += filled;
711 		buf += filled;
712 	}
713 
714 	return done;
715 write_err:
716 	return -EFAULT;
717 }
718 
719 static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
720 				      pgoff_t pgoff)
721 {
722 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
723 
724 	if (!mdev_state->pages[pgoff]) {
725 		mdev_state->pages[pgoff] =
726 			alloc_pages(GFP_HIGHUSER | __GFP_ZERO, 0);
727 		if (!mdev_state->pages[pgoff])
728 			return NULL;
729 	}
730 
731 	get_page(mdev_state->pages[pgoff]);
732 	return mdev_state->pages[pgoff];
733 }
734 
735 static struct page *mbochs_get_page(struct mdev_state *mdev_state,
736 				    pgoff_t pgoff)
737 {
738 	struct page *page;
739 
740 	if (WARN_ON(pgoff >= mdev_state->pagecount))
741 		return NULL;
742 
743 	mutex_lock(&mdev_state->ops_lock);
744 	page = __mbochs_get_page(mdev_state, pgoff);
745 	mutex_unlock(&mdev_state->ops_lock);
746 
747 	return page;
748 }
749 
750 static void mbochs_put_pages(struct mdev_state *mdev_state)
751 {
752 	struct device *dev = mdev_dev(mdev_state->mdev);
753 	int i, count = 0;
754 
755 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
756 
757 	for (i = 0; i < mdev_state->pagecount; i++) {
758 		if (!mdev_state->pages[i])
759 			continue;
760 		put_page(mdev_state->pages[i]);
761 		mdev_state->pages[i] = NULL;
762 		count++;
763 	}
764 	dev_dbg(dev, "%s: %d pages released\n", __func__, count);
765 }
766 
767 static vm_fault_t mbochs_region_vm_fault(struct vm_fault *vmf)
768 {
769 	struct vm_area_struct *vma = vmf->vma;
770 	struct mdev_state *mdev_state = vma->vm_private_data;
771 	pgoff_t page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
772 
773 	if (page_offset >= mdev_state->pagecount)
774 		return VM_FAULT_SIGBUS;
775 
776 	vmf->page = mbochs_get_page(mdev_state, page_offset);
777 	if (!vmf->page)
778 		return VM_FAULT_SIGBUS;
779 
780 	return 0;
781 }
782 
783 static const struct vm_operations_struct mbochs_region_vm_ops = {
784 	.fault = mbochs_region_vm_fault,
785 };
786 
787 static int mbochs_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
788 {
789 	struct mdev_state *mdev_state =
790 		container_of(vdev, struct mdev_state, vdev);
791 
792 	if (vma->vm_pgoff != MBOCHS_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
793 		return -EINVAL;
794 	if (vma->vm_end < vma->vm_start)
795 		return -EINVAL;
796 	if (vma->vm_end - vma->vm_start > mdev_state->memsize)
797 		return -EINVAL;
798 	if ((vma->vm_flags & VM_SHARED) == 0)
799 		return -EINVAL;
800 
801 	vma->vm_ops = &mbochs_region_vm_ops;
802 	vma->vm_private_data = mdev_state;
803 	return 0;
804 }
805 
806 static vm_fault_t mbochs_dmabuf_vm_fault(struct vm_fault *vmf)
807 {
808 	struct vm_area_struct *vma = vmf->vma;
809 	struct mbochs_dmabuf *dmabuf = vma->vm_private_data;
810 
811 	if (WARN_ON(vmf->pgoff >= dmabuf->pagecount))
812 		return VM_FAULT_SIGBUS;
813 
814 	vmf->page = dmabuf->pages[vmf->pgoff];
815 	get_page(vmf->page);
816 	return 0;
817 }
818 
819 static const struct vm_operations_struct mbochs_dmabuf_vm_ops = {
820 	.fault = mbochs_dmabuf_vm_fault,
821 };
822 
823 static int mbochs_mmap_dmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
824 {
825 	struct mbochs_dmabuf *dmabuf = buf->priv;
826 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
827 
828 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
829 
830 	if ((vma->vm_flags & VM_SHARED) == 0)
831 		return -EINVAL;
832 
833 	vma->vm_ops = &mbochs_dmabuf_vm_ops;
834 	vma->vm_private_data = dmabuf;
835 	return 0;
836 }
837 
838 static void mbochs_print_dmabuf(struct mbochs_dmabuf *dmabuf,
839 				const char *prefix)
840 {
841 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
842 	u32 fourcc = dmabuf->mode.drm_format;
843 
844 	dev_dbg(dev, "%s/%d: %c%c%c%c, %dx%d, stride %d, off 0x%llx, size 0x%llx, pages %ld\n",
845 		prefix, dmabuf->id,
846 		fourcc ? ((fourcc >>  0) & 0xff) : '-',
847 		fourcc ? ((fourcc >>  8) & 0xff) : '-',
848 		fourcc ? ((fourcc >> 16) & 0xff) : '-',
849 		fourcc ? ((fourcc >> 24) & 0xff) : '-',
850 		dmabuf->mode.width, dmabuf->mode.height, dmabuf->mode.stride,
851 		dmabuf->mode.offset, dmabuf->mode.size, dmabuf->pagecount);
852 }
853 
854 static struct sg_table *mbochs_map_dmabuf(struct dma_buf_attachment *at,
855 					  enum dma_data_direction direction)
856 {
857 	struct mbochs_dmabuf *dmabuf = at->dmabuf->priv;
858 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
859 	struct sg_table *sg;
860 
861 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
862 
863 	sg = kzalloc(sizeof(*sg), GFP_KERNEL);
864 	if (!sg)
865 		goto err1;
866 	if (sg_alloc_table_from_pages(sg, dmabuf->pages, dmabuf->pagecount,
867 				      0, dmabuf->mode.size, GFP_KERNEL) < 0)
868 		goto err2;
869 	if (dma_map_sgtable(at->dev, sg, direction, 0))
870 		goto err3;
871 
872 	return sg;
873 
874 err3:
875 	sg_free_table(sg);
876 err2:
877 	kfree(sg);
878 err1:
879 	return ERR_PTR(-ENOMEM);
880 }
881 
882 static void mbochs_unmap_dmabuf(struct dma_buf_attachment *at,
883 				struct sg_table *sg,
884 				enum dma_data_direction direction)
885 {
886 	struct mbochs_dmabuf *dmabuf = at->dmabuf->priv;
887 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
888 
889 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
890 
891 	dma_unmap_sgtable(at->dev, sg, direction, 0);
892 	sg_free_table(sg);
893 	kfree(sg);
894 }
895 
896 static void mbochs_release_dmabuf(struct dma_buf *buf)
897 {
898 	struct mbochs_dmabuf *dmabuf = buf->priv;
899 	struct mdev_state *mdev_state = dmabuf->mdev_state;
900 	struct device *dev = mdev_dev(mdev_state->mdev);
901 	pgoff_t pg;
902 
903 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
904 
905 	for (pg = 0; pg < dmabuf->pagecount; pg++)
906 		put_page(dmabuf->pages[pg]);
907 
908 	mutex_lock(&mdev_state->ops_lock);
909 	dmabuf->buf = NULL;
910 	if (dmabuf->unlinked)
911 		kfree(dmabuf);
912 	mutex_unlock(&mdev_state->ops_lock);
913 }
914 
915 static struct dma_buf_ops mbochs_dmabuf_ops = {
916 	.map_dma_buf	  = mbochs_map_dmabuf,
917 	.unmap_dma_buf	  = mbochs_unmap_dmabuf,
918 	.release	  = mbochs_release_dmabuf,
919 	.mmap		  = mbochs_mmap_dmabuf,
920 };
921 
922 static struct mbochs_dmabuf *mbochs_dmabuf_alloc(struct mdev_state *mdev_state,
923 						 struct mbochs_mode *mode)
924 {
925 	struct mbochs_dmabuf *dmabuf;
926 	pgoff_t page_offset, pg;
927 
928 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
929 
930 	dmabuf = kzalloc(sizeof(struct mbochs_dmabuf), GFP_KERNEL);
931 	if (!dmabuf)
932 		return NULL;
933 
934 	dmabuf->mode = *mode;
935 	dmabuf->id = mdev_state->next_id++;
936 	dmabuf->pagecount = DIV_ROUND_UP(mode->size, PAGE_SIZE);
937 	dmabuf->pages = kcalloc(dmabuf->pagecount, sizeof(struct page *),
938 				GFP_KERNEL);
939 	if (!dmabuf->pages)
940 		goto err_free_dmabuf;
941 
942 	page_offset = dmabuf->mode.offset >> PAGE_SHIFT;
943 	for (pg = 0; pg < dmabuf->pagecount; pg++) {
944 		dmabuf->pages[pg] = __mbochs_get_page(mdev_state,
945 						      page_offset + pg);
946 		if (!dmabuf->pages[pg])
947 			goto err_free_pages;
948 	}
949 
950 	dmabuf->mdev_state = mdev_state;
951 	list_add(&dmabuf->next, &mdev_state->dmabufs);
952 
953 	mbochs_print_dmabuf(dmabuf, __func__);
954 	return dmabuf;
955 
956 err_free_pages:
957 	while (pg > 0)
958 		put_page(dmabuf->pages[--pg]);
959 	kfree(dmabuf->pages);
960 err_free_dmabuf:
961 	kfree(dmabuf);
962 	return NULL;
963 }
964 
965 static struct mbochs_dmabuf *
966 mbochs_dmabuf_find_by_mode(struct mdev_state *mdev_state,
967 			   struct mbochs_mode *mode)
968 {
969 	struct mbochs_dmabuf *dmabuf;
970 
971 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
972 
973 	list_for_each_entry(dmabuf, &mdev_state->dmabufs, next)
974 		if (mbochs_modes_equal(&dmabuf->mode, mode))
975 			return dmabuf;
976 
977 	return NULL;
978 }
979 
980 static struct mbochs_dmabuf *
981 mbochs_dmabuf_find_by_id(struct mdev_state *mdev_state, u32 id)
982 {
983 	struct mbochs_dmabuf *dmabuf;
984 
985 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
986 
987 	list_for_each_entry(dmabuf, &mdev_state->dmabufs, next)
988 		if (dmabuf->id == id)
989 			return dmabuf;
990 
991 	return NULL;
992 }
993 
994 static int mbochs_dmabuf_export(struct mbochs_dmabuf *dmabuf)
995 {
996 	struct mdev_state *mdev_state = dmabuf->mdev_state;
997 	struct device *dev = mdev_state->vdev.dev;
998 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
999 	struct dma_buf *buf;
1000 
1001 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
1002 
1003 	if (!IS_ALIGNED(dmabuf->mode.offset, PAGE_SIZE)) {
1004 		dev_info_ratelimited(dev, "%s: framebuffer not page-aligned\n",
1005 				     __func__);
1006 		return -EINVAL;
1007 	}
1008 
1009 	exp_info.ops = &mbochs_dmabuf_ops;
1010 	exp_info.size = dmabuf->mode.size;
1011 	exp_info.priv = dmabuf;
1012 
1013 	buf = dma_buf_export(&exp_info);
1014 	if (IS_ERR(buf)) {
1015 		dev_info_ratelimited(dev, "%s: dma_buf_export failed: %ld\n",
1016 				     __func__, PTR_ERR(buf));
1017 		return PTR_ERR(buf);
1018 	}
1019 
1020 	dmabuf->buf = buf;
1021 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
1022 	return 0;
1023 }
1024 
1025 static int mbochs_get_region_info(struct mdev_state *mdev_state,
1026 				  struct vfio_region_info_ext *ext)
1027 {
1028 	struct vfio_region_info *region_info = &ext->base;
1029 
1030 	if (region_info->index >= MBOCHS_NUM_REGIONS)
1031 		return -EINVAL;
1032 
1033 	switch (region_info->index) {
1034 	case VFIO_PCI_CONFIG_REGION_INDEX:
1035 		region_info->offset = 0;
1036 		region_info->size   = MBOCHS_CONFIG_SPACE_SIZE;
1037 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ |
1038 				       VFIO_REGION_INFO_FLAG_WRITE);
1039 		break;
1040 	case VFIO_PCI_BAR0_REGION_INDEX:
1041 		region_info->offset = MBOCHS_MEMORY_BAR_OFFSET;
1042 		region_info->size   = mdev_state->memsize;
1043 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
1044 				       VFIO_REGION_INFO_FLAG_WRITE |
1045 				       VFIO_REGION_INFO_FLAG_MMAP);
1046 		break;
1047 	case VFIO_PCI_BAR2_REGION_INDEX:
1048 		region_info->offset = MBOCHS_MMIO_BAR_OFFSET;
1049 		region_info->size   = MBOCHS_MMIO_BAR_SIZE;
1050 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
1051 				       VFIO_REGION_INFO_FLAG_WRITE);
1052 		break;
1053 	case MBOCHS_EDID_REGION_INDEX:
1054 		ext->base.argsz = sizeof(*ext);
1055 		ext->base.offset = MBOCHS_EDID_OFFSET;
1056 		ext->base.size = MBOCHS_EDID_SIZE;
1057 		ext->base.flags = (VFIO_REGION_INFO_FLAG_READ  |
1058 				   VFIO_REGION_INFO_FLAG_WRITE |
1059 				   VFIO_REGION_INFO_FLAG_CAPS);
1060 		ext->base.cap_offset = offsetof(typeof(*ext), type);
1061 		ext->type.header.id = VFIO_REGION_INFO_CAP_TYPE;
1062 		ext->type.header.version = 1;
1063 		ext->type.header.next = 0;
1064 		ext->type.type = VFIO_REGION_TYPE_GFX;
1065 		ext->type.subtype = VFIO_REGION_SUBTYPE_GFX_EDID;
1066 		break;
1067 	default:
1068 		region_info->size   = 0;
1069 		region_info->offset = 0;
1070 		region_info->flags  = 0;
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 static int mbochs_get_irq_info(struct vfio_irq_info *irq_info)
1077 {
1078 	irq_info->count = 0;
1079 	return 0;
1080 }
1081 
1082 static int mbochs_get_device_info(struct vfio_device_info *dev_info)
1083 {
1084 	dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
1085 	dev_info->num_regions = MBOCHS_NUM_REGIONS;
1086 	dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
1087 	return 0;
1088 }
1089 
1090 static int mbochs_query_gfx_plane(struct mdev_state *mdev_state,
1091 				  struct vfio_device_gfx_plane_info *plane)
1092 {
1093 	struct mbochs_dmabuf *dmabuf;
1094 	struct mbochs_mode mode;
1095 	int ret;
1096 
1097 	if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
1098 		if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
1099 				     VFIO_GFX_PLANE_TYPE_DMABUF))
1100 			return 0;
1101 		return -EINVAL;
1102 	}
1103 
1104 	if (plane->flags != VFIO_GFX_PLANE_TYPE_DMABUF)
1105 		return -EINVAL;
1106 
1107 	plane->drm_format_mod = 0;
1108 	plane->x_pos	      = 0;
1109 	plane->y_pos	      = 0;
1110 	plane->x_hot	      = 0;
1111 	plane->y_hot	      = 0;
1112 
1113 	mutex_lock(&mdev_state->ops_lock);
1114 
1115 	ret = -EINVAL;
1116 	if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY)
1117 		ret = mbochs_check_framebuffer(mdev_state, &mode);
1118 	if (ret < 0) {
1119 		plane->drm_format     = 0;
1120 		plane->width	      = 0;
1121 		plane->height	      = 0;
1122 		plane->stride	      = 0;
1123 		plane->size	      = 0;
1124 		plane->dmabuf_id      = 0;
1125 		goto done;
1126 	}
1127 
1128 	dmabuf = mbochs_dmabuf_find_by_mode(mdev_state, &mode);
1129 	if (!dmabuf)
1130 		mbochs_dmabuf_alloc(mdev_state, &mode);
1131 	if (!dmabuf) {
1132 		mutex_unlock(&mdev_state->ops_lock);
1133 		return -ENOMEM;
1134 	}
1135 
1136 	plane->drm_format     = dmabuf->mode.drm_format;
1137 	plane->width	      = dmabuf->mode.width;
1138 	plane->height	      = dmabuf->mode.height;
1139 	plane->stride	      = dmabuf->mode.stride;
1140 	plane->size	      = dmabuf->mode.size;
1141 	plane->dmabuf_id      = dmabuf->id;
1142 
1143 done:
1144 	if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY &&
1145 	    mdev_state->active_id != plane->dmabuf_id) {
1146 		dev_dbg(mdev_state->vdev.dev, "%s: primary: %d => %d\n",
1147 			__func__, mdev_state->active_id, plane->dmabuf_id);
1148 		mdev_state->active_id = plane->dmabuf_id;
1149 	}
1150 	mutex_unlock(&mdev_state->ops_lock);
1151 	return 0;
1152 }
1153 
1154 static int mbochs_get_gfx_dmabuf(struct mdev_state *mdev_state, u32 id)
1155 {
1156 	struct mbochs_dmabuf *dmabuf;
1157 
1158 	mutex_lock(&mdev_state->ops_lock);
1159 
1160 	dmabuf = mbochs_dmabuf_find_by_id(mdev_state, id);
1161 	if (!dmabuf) {
1162 		mutex_unlock(&mdev_state->ops_lock);
1163 		return -ENOENT;
1164 	}
1165 
1166 	if (!dmabuf->buf)
1167 		mbochs_dmabuf_export(dmabuf);
1168 
1169 	mutex_unlock(&mdev_state->ops_lock);
1170 
1171 	if (!dmabuf->buf)
1172 		return -EINVAL;
1173 
1174 	return dma_buf_fd(dmabuf->buf, 0);
1175 }
1176 
1177 static long mbochs_ioctl(struct vfio_device *vdev, unsigned int cmd,
1178 			 unsigned long arg)
1179 {
1180 	struct mdev_state *mdev_state =
1181 		container_of(vdev, struct mdev_state, vdev);
1182 	int ret = 0;
1183 	unsigned long minsz, outsz;
1184 
1185 	switch (cmd) {
1186 	case VFIO_DEVICE_GET_INFO:
1187 	{
1188 		struct vfio_device_info info;
1189 
1190 		minsz = offsetofend(struct vfio_device_info, num_irqs);
1191 
1192 		if (copy_from_user(&info, (void __user *)arg, minsz))
1193 			return -EFAULT;
1194 
1195 		if (info.argsz < minsz)
1196 			return -EINVAL;
1197 
1198 		ret = mbochs_get_device_info(&info);
1199 		if (ret)
1200 			return ret;
1201 
1202 		if (copy_to_user((void __user *)arg, &info, minsz))
1203 			return -EFAULT;
1204 
1205 		return 0;
1206 	}
1207 	case VFIO_DEVICE_GET_REGION_INFO:
1208 	{
1209 		struct vfio_region_info_ext info;
1210 
1211 		minsz = offsetofend(typeof(info), base.offset);
1212 
1213 		if (copy_from_user(&info, (void __user *)arg, minsz))
1214 			return -EFAULT;
1215 
1216 		outsz = info.base.argsz;
1217 		if (outsz < minsz)
1218 			return -EINVAL;
1219 		if (outsz > sizeof(info))
1220 			return -EINVAL;
1221 
1222 		ret = mbochs_get_region_info(mdev_state, &info);
1223 		if (ret)
1224 			return ret;
1225 
1226 		if (copy_to_user((void __user *)arg, &info, outsz))
1227 			return -EFAULT;
1228 
1229 		return 0;
1230 	}
1231 
1232 	case VFIO_DEVICE_GET_IRQ_INFO:
1233 	{
1234 		struct vfio_irq_info info;
1235 
1236 		minsz = offsetofend(struct vfio_irq_info, count);
1237 
1238 		if (copy_from_user(&info, (void __user *)arg, minsz))
1239 			return -EFAULT;
1240 
1241 		if ((info.argsz < minsz) ||
1242 		    (info.index >= VFIO_PCI_NUM_IRQS))
1243 			return -EINVAL;
1244 
1245 		ret = mbochs_get_irq_info(&info);
1246 		if (ret)
1247 			return ret;
1248 
1249 		if (copy_to_user((void __user *)arg, &info, minsz))
1250 			return -EFAULT;
1251 
1252 		return 0;
1253 	}
1254 
1255 	case VFIO_DEVICE_QUERY_GFX_PLANE:
1256 	{
1257 		struct vfio_device_gfx_plane_info plane;
1258 
1259 		minsz = offsetofend(struct vfio_device_gfx_plane_info,
1260 				    region_index);
1261 
1262 		if (copy_from_user(&plane, (void __user *)arg, minsz))
1263 			return -EFAULT;
1264 
1265 		if (plane.argsz < minsz)
1266 			return -EINVAL;
1267 
1268 		ret = mbochs_query_gfx_plane(mdev_state, &plane);
1269 		if (ret)
1270 			return ret;
1271 
1272 		if (copy_to_user((void __user *)arg, &plane, minsz))
1273 			return -EFAULT;
1274 
1275 		return 0;
1276 	}
1277 
1278 	case VFIO_DEVICE_GET_GFX_DMABUF:
1279 	{
1280 		u32 dmabuf_id;
1281 
1282 		if (get_user(dmabuf_id, (__u32 __user *)arg))
1283 			return -EFAULT;
1284 
1285 		return mbochs_get_gfx_dmabuf(mdev_state, dmabuf_id);
1286 	}
1287 
1288 	case VFIO_DEVICE_SET_IRQS:
1289 		return -EINVAL;
1290 
1291 	case VFIO_DEVICE_RESET:
1292 		return mbochs_reset(mdev_state);
1293 	}
1294 	return -ENOTTY;
1295 }
1296 
1297 static void mbochs_close_device(struct vfio_device *vdev)
1298 {
1299 	struct mdev_state *mdev_state =
1300 		container_of(vdev, struct mdev_state, vdev);
1301 	struct mbochs_dmabuf *dmabuf, *tmp;
1302 
1303 	mutex_lock(&mdev_state->ops_lock);
1304 
1305 	list_for_each_entry_safe(dmabuf, tmp, &mdev_state->dmabufs, next) {
1306 		list_del(&dmabuf->next);
1307 		if (dmabuf->buf) {
1308 			/* free in mbochs_release_dmabuf() */
1309 			dmabuf->unlinked = true;
1310 		} else {
1311 			kfree(dmabuf);
1312 		}
1313 	}
1314 	mbochs_put_pages(mdev_state);
1315 
1316 	mutex_unlock(&mdev_state->ops_lock);
1317 }
1318 
1319 static ssize_t
1320 memory_show(struct device *dev, struct device_attribute *attr,
1321 	    char *buf)
1322 {
1323 	struct mdev_state *mdev_state = dev_get_drvdata(dev);
1324 
1325 	return sprintf(buf, "%d MB\n", mdev_state->type->mbytes);
1326 }
1327 static DEVICE_ATTR_RO(memory);
1328 
1329 static struct attribute *mdev_dev_attrs[] = {
1330 	&dev_attr_memory.attr,
1331 	NULL,
1332 };
1333 
1334 static const struct attribute_group mdev_dev_group = {
1335 	.name  = "vendor",
1336 	.attrs = mdev_dev_attrs,
1337 };
1338 
1339 static const struct attribute_group *mdev_dev_groups[] = {
1340 	&mdev_dev_group,
1341 	NULL,
1342 };
1343 
1344 static ssize_t name_show(struct mdev_type *mtype,
1345 			 struct mdev_type_attribute *attr, char *buf)
1346 {
1347 	const struct mbochs_type *type =
1348 		&mbochs_types[mtype_get_type_group_id(mtype)];
1349 
1350 	return sprintf(buf, "%s\n", type->name);
1351 }
1352 static MDEV_TYPE_ATTR_RO(name);
1353 
1354 static ssize_t description_show(struct mdev_type *mtype,
1355 				struct mdev_type_attribute *attr, char *buf)
1356 {
1357 	const struct mbochs_type *type =
1358 		&mbochs_types[mtype_get_type_group_id(mtype)];
1359 
1360 	return sprintf(buf, "virtual display, %d MB video memory\n",
1361 		       type ? type->mbytes  : 0);
1362 }
1363 static MDEV_TYPE_ATTR_RO(description);
1364 
1365 static ssize_t available_instances_show(struct mdev_type *mtype,
1366 					struct mdev_type_attribute *attr,
1367 					char *buf)
1368 {
1369 	const struct mbochs_type *type =
1370 		&mbochs_types[mtype_get_type_group_id(mtype)];
1371 	int count = atomic_read(&mbochs_avail_mbytes) / type->mbytes;
1372 
1373 	return sprintf(buf, "%d\n", count);
1374 }
1375 static MDEV_TYPE_ATTR_RO(available_instances);
1376 
1377 static ssize_t device_api_show(struct mdev_type *mtype,
1378 			       struct mdev_type_attribute *attr, char *buf)
1379 {
1380 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
1381 }
1382 static MDEV_TYPE_ATTR_RO(device_api);
1383 
1384 static struct attribute *mdev_types_attrs[] = {
1385 	&mdev_type_attr_name.attr,
1386 	&mdev_type_attr_description.attr,
1387 	&mdev_type_attr_device_api.attr,
1388 	&mdev_type_attr_available_instances.attr,
1389 	NULL,
1390 };
1391 
1392 static struct attribute_group mdev_type_group1 = {
1393 	.name  = MBOCHS_TYPE_1,
1394 	.attrs = mdev_types_attrs,
1395 };
1396 
1397 static struct attribute_group mdev_type_group2 = {
1398 	.name  = MBOCHS_TYPE_2,
1399 	.attrs = mdev_types_attrs,
1400 };
1401 
1402 static struct attribute_group mdev_type_group3 = {
1403 	.name  = MBOCHS_TYPE_3,
1404 	.attrs = mdev_types_attrs,
1405 };
1406 
1407 static struct attribute_group *mdev_type_groups[] = {
1408 	&mdev_type_group1,
1409 	&mdev_type_group2,
1410 	&mdev_type_group3,
1411 	NULL,
1412 };
1413 
1414 static const struct vfio_device_ops mbochs_dev_ops = {
1415 	.close_device = mbochs_close_device,
1416 	.init = mbochs_init_dev,
1417 	.release = mbochs_release_dev,
1418 	.read = mbochs_read,
1419 	.write = mbochs_write,
1420 	.ioctl = mbochs_ioctl,
1421 	.mmap = mbochs_mmap,
1422 };
1423 
1424 static struct mdev_driver mbochs_driver = {
1425 	.driver = {
1426 		.name = "mbochs",
1427 		.owner = THIS_MODULE,
1428 		.mod_name = KBUILD_MODNAME,
1429 		.dev_groups = mdev_dev_groups,
1430 	},
1431 	.probe = mbochs_probe,
1432 	.remove	= mbochs_remove,
1433 	.supported_type_groups = mdev_type_groups,
1434 };
1435 
1436 static const struct file_operations vd_fops = {
1437 	.owner		= THIS_MODULE,
1438 };
1439 
1440 static void mbochs_device_release(struct device *dev)
1441 {
1442 	/* nothing */
1443 }
1444 
1445 static int __init mbochs_dev_init(void)
1446 {
1447 	int ret = 0;
1448 
1449 	atomic_set(&mbochs_avail_mbytes, max_mbytes);
1450 
1451 	ret = alloc_chrdev_region(&mbochs_devt, 0, MINORMASK + 1, MBOCHS_NAME);
1452 	if (ret < 0) {
1453 		pr_err("Error: failed to register mbochs_dev, err: %d\n", ret);
1454 		return ret;
1455 	}
1456 	cdev_init(&mbochs_cdev, &vd_fops);
1457 	cdev_add(&mbochs_cdev, mbochs_devt, MINORMASK + 1);
1458 	pr_info("%s: major %d\n", __func__, MAJOR(mbochs_devt));
1459 
1460 	ret = mdev_register_driver(&mbochs_driver);
1461 	if (ret)
1462 		goto err_cdev;
1463 
1464 	mbochs_class = class_create(THIS_MODULE, MBOCHS_CLASS_NAME);
1465 	if (IS_ERR(mbochs_class)) {
1466 		pr_err("Error: failed to register mbochs_dev class\n");
1467 		ret = PTR_ERR(mbochs_class);
1468 		goto err_driver;
1469 	}
1470 	mbochs_dev.class = mbochs_class;
1471 	mbochs_dev.release = mbochs_device_release;
1472 	dev_set_name(&mbochs_dev, "%s", MBOCHS_NAME);
1473 
1474 	ret = device_register(&mbochs_dev);
1475 	if (ret)
1476 		goto err_class;
1477 
1478 	ret = mdev_register_device(&mbochs_dev, &mbochs_driver);
1479 	if (ret)
1480 		goto err_device;
1481 
1482 	return 0;
1483 
1484 err_device:
1485 	device_unregister(&mbochs_dev);
1486 err_class:
1487 	class_destroy(mbochs_class);
1488 err_driver:
1489 	mdev_unregister_driver(&mbochs_driver);
1490 err_cdev:
1491 	cdev_del(&mbochs_cdev);
1492 	unregister_chrdev_region(mbochs_devt, MINORMASK + 1);
1493 	return ret;
1494 }
1495 
1496 static void __exit mbochs_dev_exit(void)
1497 {
1498 	mbochs_dev.bus = NULL;
1499 	mdev_unregister_device(&mbochs_dev);
1500 
1501 	device_unregister(&mbochs_dev);
1502 	mdev_unregister_driver(&mbochs_driver);
1503 	cdev_del(&mbochs_cdev);
1504 	unregister_chrdev_region(mbochs_devt, MINORMASK + 1);
1505 	class_destroy(mbochs_class);
1506 	mbochs_class = NULL;
1507 }
1508 
1509 MODULE_IMPORT_NS(DMA_BUF);
1510 module_init(mbochs_dev_init)
1511 module_exit(mbochs_dev_exit)
1512