xref: /linux/samples/vfio-mdev/mbochs.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1a5e6e650SGerd Hoffmann // SPDX-License-Identifier: GPL-2.0
2a5e6e650SGerd Hoffmann /*
3a5e6e650SGerd Hoffmann  * Mediated virtual PCI display host device driver
4a5e6e650SGerd Hoffmann  *
5a5e6e650SGerd Hoffmann  * Emulate enough of qemu stdvga to make bochs-drm.ko happy.  That is
6a5e6e650SGerd Hoffmann  * basically the vram memory bar and the bochs dispi interface vbe
7a5e6e650SGerd Hoffmann  * registers in the mmio register bar.	Specifically it does *not*
8a5e6e650SGerd Hoffmann  * include any legacy vga stuff.  Device looks a lot like "qemu -device
9a5e6e650SGerd Hoffmann  * secondary-vga".
10a5e6e650SGerd Hoffmann  *
11a5e6e650SGerd Hoffmann  *   (c) Gerd Hoffmann <kraxel@redhat.com>
12a5e6e650SGerd Hoffmann  *
13a5e6e650SGerd Hoffmann  * based on mtty driver which is:
14a5e6e650SGerd Hoffmann  *   Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
15a5e6e650SGerd Hoffmann  *	 Author: Neo Jia <cjia@nvidia.com>
16a5e6e650SGerd Hoffmann  *		 Kirti Wankhede <kwankhede@nvidia.com>
17a5e6e650SGerd Hoffmann  *
18a5e6e650SGerd Hoffmann  * This program is free software; you can redistribute it and/or modify
19a5e6e650SGerd Hoffmann  * it under the terms of the GNU General Public License version 2 as
20a5e6e650SGerd Hoffmann  * published by the Free Software Foundation.
21a5e6e650SGerd Hoffmann  */
22a5e6e650SGerd Hoffmann #include <linux/init.h>
23a5e6e650SGerd Hoffmann #include <linux/module.h>
24a5e6e650SGerd Hoffmann #include <linux/kernel.h>
25a5e6e650SGerd Hoffmann #include <linux/slab.h>
26a5e6e650SGerd Hoffmann #include <linux/vmalloc.h>
27a5e6e650SGerd Hoffmann #include <linux/cdev.h>
28a5e6e650SGerd Hoffmann #include <linux/vfio.h>
29a5e6e650SGerd Hoffmann #include <linux/iommu.h>
30a5e6e650SGerd Hoffmann #include <linux/sysfs.h>
31a5e6e650SGerd Hoffmann #include <linux/mdev.h>
32a5e6e650SGerd Hoffmann #include <linux/pci.h>
33a5e6e650SGerd Hoffmann #include <linux/dma-buf.h>
34a5e6e650SGerd Hoffmann #include <linux/highmem.h>
35a5e6e650SGerd Hoffmann #include <drm/drm_fourcc.h>
36a5e6e650SGerd Hoffmann #include <drm/drm_rect.h>
37a5e6e650SGerd Hoffmann #include <drm/drm_modeset_lock.h>
38a5e6e650SGerd Hoffmann #include <drm/drm_property.h>
39a5e6e650SGerd Hoffmann #include <drm/drm_plane.h>
40a5e6e650SGerd Hoffmann 
41a5e6e650SGerd Hoffmann 
42a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_ID		0x0
43a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_XRES		0x1
44a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_YRES		0x2
45a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_BPP		0x3
46a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_ENABLE		0x4
47a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_BANK		0x5
48a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_VIRT_WIDTH	0x6
49a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_VIRT_HEIGHT	0x7
50a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_X_OFFSET	0x8
51a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_Y_OFFSET	0x9
52a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
53a5e6e650SGerd Hoffmann #define VBE_DISPI_INDEX_COUNT		0xb
54a5e6e650SGerd Hoffmann 
55a5e6e650SGerd Hoffmann #define VBE_DISPI_ID0			0xB0C0
56a5e6e650SGerd Hoffmann #define VBE_DISPI_ID1			0xB0C1
57a5e6e650SGerd Hoffmann #define VBE_DISPI_ID2			0xB0C2
58a5e6e650SGerd Hoffmann #define VBE_DISPI_ID3			0xB0C3
59a5e6e650SGerd Hoffmann #define VBE_DISPI_ID4			0xB0C4
60a5e6e650SGerd Hoffmann #define VBE_DISPI_ID5			0xB0C5
61a5e6e650SGerd Hoffmann 
62a5e6e650SGerd Hoffmann #define VBE_DISPI_DISABLED		0x00
63a5e6e650SGerd Hoffmann #define VBE_DISPI_ENABLED		0x01
64a5e6e650SGerd Hoffmann #define VBE_DISPI_GETCAPS		0x02
65a5e6e650SGerd Hoffmann #define VBE_DISPI_8BIT_DAC		0x20
66a5e6e650SGerd Hoffmann #define VBE_DISPI_LFB_ENABLED		0x40
67a5e6e650SGerd Hoffmann #define VBE_DISPI_NOCLEARMEM		0x80
68a5e6e650SGerd Hoffmann 
69a5e6e650SGerd Hoffmann 
70a5e6e650SGerd Hoffmann #define MBOCHS_NAME		  "mbochs"
71a5e6e650SGerd Hoffmann #define MBOCHS_CLASS_NAME	  "mbochs"
72a5e6e650SGerd Hoffmann 
73104c7405SGerd Hoffmann #define MBOCHS_EDID_REGION_INDEX  VFIO_PCI_NUM_REGIONS
74104c7405SGerd Hoffmann #define MBOCHS_NUM_REGIONS        (MBOCHS_EDID_REGION_INDEX+1)
75104c7405SGerd Hoffmann 
76a5e6e650SGerd Hoffmann #define MBOCHS_CONFIG_SPACE_SIZE  0xff
77a5e6e650SGerd Hoffmann #define MBOCHS_MMIO_BAR_OFFSET	  PAGE_SIZE
78a5e6e650SGerd Hoffmann #define MBOCHS_MMIO_BAR_SIZE	  PAGE_SIZE
79104c7405SGerd Hoffmann #define MBOCHS_EDID_OFFSET	  (MBOCHS_MMIO_BAR_OFFSET +	\
80a5e6e650SGerd Hoffmann 				   MBOCHS_MMIO_BAR_SIZE)
81104c7405SGerd Hoffmann #define MBOCHS_EDID_SIZE	  PAGE_SIZE
82104c7405SGerd Hoffmann #define MBOCHS_MEMORY_BAR_OFFSET  (MBOCHS_EDID_OFFSET + \
83104c7405SGerd Hoffmann 				   MBOCHS_EDID_SIZE)
84104c7405SGerd Hoffmann 
85104c7405SGerd Hoffmann #define MBOCHS_EDID_BLOB_OFFSET   (MBOCHS_EDID_SIZE/2)
86a5e6e650SGerd Hoffmann 
87a5e6e650SGerd Hoffmann #define STORE_LE16(addr, val)	(*(u16 *)addr = val)
88a5e6e650SGerd Hoffmann #define STORE_LE32(addr, val)	(*(u32 *)addr = val)
89a5e6e650SGerd Hoffmann 
90a5e6e650SGerd Hoffmann 
91*0756bec2SJeff Johnson MODULE_DESCRIPTION("Mediated virtual PCI display host device driver");
92a5e6e650SGerd Hoffmann MODULE_LICENSE("GPL v2");
93a5e6e650SGerd Hoffmann 
94a5e6e650SGerd Hoffmann static int max_mbytes = 256;
95a5e6e650SGerd Hoffmann module_param_named(count, max_mbytes, int, 0444);
96a5e6e650SGerd Hoffmann MODULE_PARM_DESC(mem, "megabytes available to " MBOCHS_NAME " devices");
97a5e6e650SGerd Hoffmann 
98a5e6e650SGerd Hoffmann 
99a5e6e650SGerd Hoffmann #define MBOCHS_TYPE_1 "small"
100a5e6e650SGerd Hoffmann #define MBOCHS_TYPE_2 "medium"
101a5e6e650SGerd Hoffmann #define MBOCHS_TYPE_3 "large"
102a5e6e650SGerd Hoffmann 
103da44c340SChristoph Hellwig static struct mbochs_type {
104da44c340SChristoph Hellwig 	struct mdev_type type;
105a5e6e650SGerd Hoffmann 	u32 mbytes;
106104c7405SGerd Hoffmann 	u32 max_x;
107104c7405SGerd Hoffmann 	u32 max_y;
108a5e6e650SGerd Hoffmann } mbochs_types[] = {
109a5e6e650SGerd Hoffmann 	{
110da44c340SChristoph Hellwig 		.type.sysfs_name	= MBOCHS_TYPE_1,
1110bc79069SChristoph Hellwig 		.type.pretty_name	= MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_1,
112a5e6e650SGerd Hoffmann 		.mbytes = 4,
113104c7405SGerd Hoffmann 		.max_x  = 800,
114104c7405SGerd Hoffmann 		.max_y  = 600,
115a5e6e650SGerd Hoffmann 	}, {
116da44c340SChristoph Hellwig 		.type.sysfs_name	= MBOCHS_TYPE_2,
1170bc79069SChristoph Hellwig 		.type.pretty_name	= MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_2,
118a5e6e650SGerd Hoffmann 		.mbytes = 16,
119104c7405SGerd Hoffmann 		.max_x  = 1920,
120104c7405SGerd Hoffmann 		.max_y  = 1440,
121a5e6e650SGerd Hoffmann 	}, {
122da44c340SChristoph Hellwig 		.type.sysfs_name	= MBOCHS_TYPE_3,
1230bc79069SChristoph Hellwig 		.type.pretty_name	= MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_3,
124a5e6e650SGerd Hoffmann 		.mbytes = 64,
125104c7405SGerd Hoffmann 		.max_x  = 0,
126104c7405SGerd Hoffmann 		.max_y  = 0,
127a5e6e650SGerd Hoffmann 	},
128a5e6e650SGerd Hoffmann };
129a5e6e650SGerd Hoffmann 
130da44c340SChristoph Hellwig static struct mdev_type *mbochs_mdev_types[] = {
131da44c340SChristoph Hellwig 	&mbochs_types[0].type,
132da44c340SChristoph Hellwig 	&mbochs_types[1].type,
133da44c340SChristoph Hellwig 	&mbochs_types[2].type,
134da44c340SChristoph Hellwig };
135a5e6e650SGerd Hoffmann 
136a5e6e650SGerd Hoffmann static dev_t		mbochs_devt;
137626f534dSRicardo B. Marliere static const struct class mbochs_class = {
138626f534dSRicardo B. Marliere 	.name = MBOCHS_CLASS_NAME,
139626f534dSRicardo B. Marliere };
140a5e6e650SGerd Hoffmann static struct cdev	mbochs_cdev;
141a5e6e650SGerd Hoffmann static struct device	mbochs_dev;
14289345d51SChristoph Hellwig static struct mdev_parent mbochs_parent;
143de5494afSJason Gunthorpe static atomic_t mbochs_avail_mbytes;
144681c1615SJason Gunthorpe static const struct vfio_device_ops mbochs_dev_ops;
145a5e6e650SGerd Hoffmann 
146104c7405SGerd Hoffmann struct vfio_region_info_ext {
147104c7405SGerd Hoffmann 	struct vfio_region_info          base;
148104c7405SGerd Hoffmann 	struct vfio_region_info_cap_type type;
149104c7405SGerd Hoffmann };
150104c7405SGerd Hoffmann 
151a5e6e650SGerd Hoffmann struct mbochs_mode {
152a5e6e650SGerd Hoffmann 	u32 drm_format;
153a5e6e650SGerd Hoffmann 	u32 bytepp;
154a5e6e650SGerd Hoffmann 	u32 width;
155a5e6e650SGerd Hoffmann 	u32 height;
156a5e6e650SGerd Hoffmann 	u32 stride;
157a5e6e650SGerd Hoffmann 	u32 __pad;
158a5e6e650SGerd Hoffmann 	u64 offset;
159a5e6e650SGerd Hoffmann 	u64 size;
160a5e6e650SGerd Hoffmann };
161a5e6e650SGerd Hoffmann 
162a5e6e650SGerd Hoffmann struct mbochs_dmabuf {
163a5e6e650SGerd Hoffmann 	struct mbochs_mode mode;
164a5e6e650SGerd Hoffmann 	u32 id;
165a5e6e650SGerd Hoffmann 	struct page **pages;
166a5e6e650SGerd Hoffmann 	pgoff_t pagecount;
167a5e6e650SGerd Hoffmann 	struct dma_buf *buf;
168a5e6e650SGerd Hoffmann 	struct mdev_state *mdev_state;
169a5e6e650SGerd Hoffmann 	struct list_head next;
170a5e6e650SGerd Hoffmann 	bool unlinked;
171a5e6e650SGerd Hoffmann };
172a5e6e650SGerd Hoffmann 
173a5e6e650SGerd Hoffmann /* State of each mdev device */
174a5e6e650SGerd Hoffmann struct mdev_state {
175681c1615SJason Gunthorpe 	struct vfio_device vdev;
176a5e6e650SGerd Hoffmann 	u8 *vconfig;
177a5e6e650SGerd Hoffmann 	u64 bar_mask[3];
178a5e6e650SGerd Hoffmann 	u32 memory_bar_mask;
179a5e6e650SGerd Hoffmann 	struct mutex ops_lock;
180a5e6e650SGerd Hoffmann 	struct mdev_device *mdev;
181a5e6e650SGerd Hoffmann 
182a5e6e650SGerd Hoffmann 	const struct mbochs_type *type;
183a5e6e650SGerd Hoffmann 	u16 vbe[VBE_DISPI_INDEX_COUNT];
184a5e6e650SGerd Hoffmann 	u64 memsize;
185a5e6e650SGerd Hoffmann 	struct page **pages;
186a5e6e650SGerd Hoffmann 	pgoff_t pagecount;
187104c7405SGerd Hoffmann 	struct vfio_region_gfx_edid edid_regs;
188104c7405SGerd Hoffmann 	u8 edid_blob[0x400];
189a5e6e650SGerd Hoffmann 
190a5e6e650SGerd Hoffmann 	struct list_head dmabufs;
191a5e6e650SGerd Hoffmann 	u32 active_id;
192a5e6e650SGerd Hoffmann 	u32 next_id;
193a5e6e650SGerd Hoffmann };
194a5e6e650SGerd Hoffmann 
195a5e6e650SGerd Hoffmann static const char *vbe_name_list[VBE_DISPI_INDEX_COUNT] = {
196a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_ID]               = "id",
197a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_XRES]             = "xres",
198a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_YRES]             = "yres",
199a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_BPP]              = "bpp",
200a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_ENABLE]           = "enable",
201a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_BANK]             = "bank",
202a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_VIRT_WIDTH]       = "virt-width",
203a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_VIRT_HEIGHT]      = "virt-height",
204a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_X_OFFSET]         = "x-offset",
205a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_Y_OFFSET]         = "y-offset",
206a5e6e650SGerd Hoffmann 	[VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = "video-mem",
207a5e6e650SGerd Hoffmann };
208a5e6e650SGerd Hoffmann 
vbe_name(u32 index)209a5e6e650SGerd Hoffmann static const char *vbe_name(u32 index)
210a5e6e650SGerd Hoffmann {
211a5e6e650SGerd Hoffmann 	if (index < ARRAY_SIZE(vbe_name_list))
212a5e6e650SGerd Hoffmann 		return vbe_name_list[index];
213a5e6e650SGerd Hoffmann 	return "(invalid)";
214a5e6e650SGerd Hoffmann }
215a5e6e650SGerd Hoffmann 
216498e8bf5SAlexey Khoroshilov static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
217498e8bf5SAlexey Khoroshilov 				      pgoff_t pgoff);
218a5e6e650SGerd Hoffmann static struct page *mbochs_get_page(struct mdev_state *mdev_state,
219a5e6e650SGerd Hoffmann 				    pgoff_t pgoff);
220a5e6e650SGerd Hoffmann 
mbochs_create_config_space(struct mdev_state * mdev_state)221a5e6e650SGerd Hoffmann static void mbochs_create_config_space(struct mdev_state *mdev_state)
222a5e6e650SGerd Hoffmann {
223a5e6e650SGerd Hoffmann 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
224a5e6e650SGerd Hoffmann 		   0x1234);
225a5e6e650SGerd Hoffmann 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
226a5e6e650SGerd Hoffmann 		   0x1111);
227a5e6e650SGerd Hoffmann 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
228a5e6e650SGerd Hoffmann 		   PCI_SUBVENDOR_ID_REDHAT_QUMRANET);
229a5e6e650SGerd Hoffmann 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
230a5e6e650SGerd Hoffmann 		   PCI_SUBDEVICE_ID_QEMU);
231a5e6e650SGerd Hoffmann 
232a5e6e650SGerd Hoffmann 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
233a5e6e650SGerd Hoffmann 		   PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
234a5e6e650SGerd Hoffmann 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
235a5e6e650SGerd Hoffmann 		   PCI_CLASS_DISPLAY_OTHER);
236a5e6e650SGerd Hoffmann 	mdev_state->vconfig[PCI_CLASS_REVISION] =  0x01;
237a5e6e650SGerd Hoffmann 
238a5e6e650SGerd Hoffmann 	STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
239a5e6e650SGerd Hoffmann 		   PCI_BASE_ADDRESS_SPACE_MEMORY |
240a5e6e650SGerd Hoffmann 		   PCI_BASE_ADDRESS_MEM_TYPE_32	 |
241a5e6e650SGerd Hoffmann 		   PCI_BASE_ADDRESS_MEM_PREFETCH);
242a5e6e650SGerd Hoffmann 	mdev_state->bar_mask[0] = ~(mdev_state->memsize) + 1;
243a5e6e650SGerd Hoffmann 
244a5e6e650SGerd Hoffmann 	STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_2],
245a5e6e650SGerd Hoffmann 		   PCI_BASE_ADDRESS_SPACE_MEMORY |
246a5e6e650SGerd Hoffmann 		   PCI_BASE_ADDRESS_MEM_TYPE_32);
247a5e6e650SGerd Hoffmann 	mdev_state->bar_mask[2] = ~(MBOCHS_MMIO_BAR_SIZE) + 1;
248a5e6e650SGerd Hoffmann }
249a5e6e650SGerd Hoffmann 
mbochs_check_framebuffer(struct mdev_state * mdev_state,struct mbochs_mode * mode)250a5e6e650SGerd Hoffmann static int mbochs_check_framebuffer(struct mdev_state *mdev_state,
251a5e6e650SGerd Hoffmann 				    struct mbochs_mode *mode)
252a5e6e650SGerd Hoffmann {
253a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(mdev_state->mdev);
254a5e6e650SGerd Hoffmann 	u16 *vbe = mdev_state->vbe;
255a5e6e650SGerd Hoffmann 	u32 virt_width;
256a5e6e650SGerd Hoffmann 
257a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
258a5e6e650SGerd Hoffmann 
259a5e6e650SGerd Hoffmann 	if (!(vbe[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED))
260a5e6e650SGerd Hoffmann 		goto nofb;
261a5e6e650SGerd Hoffmann 
262a5e6e650SGerd Hoffmann 	memset(mode, 0, sizeof(*mode));
263a5e6e650SGerd Hoffmann 	switch (vbe[VBE_DISPI_INDEX_BPP]) {
264a5e6e650SGerd Hoffmann 	case 32:
265a5e6e650SGerd Hoffmann 		mode->drm_format = DRM_FORMAT_XRGB8888;
266a5e6e650SGerd Hoffmann 		mode->bytepp = 4;
267a5e6e650SGerd Hoffmann 		break;
268a5e6e650SGerd Hoffmann 	default:
269a5e6e650SGerd Hoffmann 		dev_info_ratelimited(dev, "%s: bpp %d not supported\n",
270a5e6e650SGerd Hoffmann 				     __func__, vbe[VBE_DISPI_INDEX_BPP]);
271a5e6e650SGerd Hoffmann 		goto nofb;
272a5e6e650SGerd Hoffmann 	}
273a5e6e650SGerd Hoffmann 
274a5e6e650SGerd Hoffmann 	mode->width  = vbe[VBE_DISPI_INDEX_XRES];
275a5e6e650SGerd Hoffmann 	mode->height = vbe[VBE_DISPI_INDEX_YRES];
276a5e6e650SGerd Hoffmann 	virt_width  = vbe[VBE_DISPI_INDEX_VIRT_WIDTH];
277a5e6e650SGerd Hoffmann 	if (virt_width < mode->width)
278a5e6e650SGerd Hoffmann 		virt_width = mode->width;
279a5e6e650SGerd Hoffmann 	mode->stride = virt_width * mode->bytepp;
280a5e6e650SGerd Hoffmann 	mode->size   = (u64)mode->stride * mode->height;
281a5e6e650SGerd Hoffmann 	mode->offset = ((u64)vbe[VBE_DISPI_INDEX_X_OFFSET] * mode->bytepp +
282a5e6e650SGerd Hoffmann 		       (u64)vbe[VBE_DISPI_INDEX_Y_OFFSET] * mode->stride);
283a5e6e650SGerd Hoffmann 
284a5e6e650SGerd Hoffmann 	if (mode->width < 64 || mode->height < 64) {
285a5e6e650SGerd Hoffmann 		dev_info_ratelimited(dev, "%s: invalid resolution %dx%d\n",
286a5e6e650SGerd Hoffmann 				     __func__, mode->width, mode->height);
287a5e6e650SGerd Hoffmann 		goto nofb;
288a5e6e650SGerd Hoffmann 	}
289a5e6e650SGerd Hoffmann 	if (mode->offset + mode->size > mdev_state->memsize) {
290a5e6e650SGerd Hoffmann 		dev_info_ratelimited(dev, "%s: framebuffer memory overflow\n",
291a5e6e650SGerd Hoffmann 				     __func__);
292a5e6e650SGerd Hoffmann 		goto nofb;
293a5e6e650SGerd Hoffmann 	}
294a5e6e650SGerd Hoffmann 
295a5e6e650SGerd Hoffmann 	return 0;
296a5e6e650SGerd Hoffmann 
297a5e6e650SGerd Hoffmann nofb:
298a5e6e650SGerd Hoffmann 	memset(mode, 0, sizeof(*mode));
299a5e6e650SGerd Hoffmann 	return -EINVAL;
300a5e6e650SGerd Hoffmann }
301a5e6e650SGerd Hoffmann 
mbochs_modes_equal(struct mbochs_mode * mode1,struct mbochs_mode * mode2)302a5e6e650SGerd Hoffmann static bool mbochs_modes_equal(struct mbochs_mode *mode1,
303a5e6e650SGerd Hoffmann 			       struct mbochs_mode *mode2)
304a5e6e650SGerd Hoffmann {
305a5e6e650SGerd Hoffmann 	return memcmp(mode1, mode2, sizeof(struct mbochs_mode)) == 0;
306a5e6e650SGerd Hoffmann }
307a5e6e650SGerd Hoffmann 
handle_pci_cfg_write(struct mdev_state * mdev_state,u16 offset,char * buf,u32 count)308a5e6e650SGerd Hoffmann static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
309a5e6e650SGerd Hoffmann 				 char *buf, u32 count)
310a5e6e650SGerd Hoffmann {
311a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(mdev_state->mdev);
312a5e6e650SGerd Hoffmann 	int index = (offset - PCI_BASE_ADDRESS_0) / 0x04;
313a5e6e650SGerd Hoffmann 	u32 cfg_addr;
314a5e6e650SGerd Hoffmann 
315a5e6e650SGerd Hoffmann 	switch (offset) {
316a5e6e650SGerd Hoffmann 	case PCI_BASE_ADDRESS_0:
317a5e6e650SGerd Hoffmann 	case PCI_BASE_ADDRESS_2:
318a5e6e650SGerd Hoffmann 		cfg_addr = *(u32 *)buf;
319a5e6e650SGerd Hoffmann 
320a5e6e650SGerd Hoffmann 		if (cfg_addr == 0xffffffff) {
321a5e6e650SGerd Hoffmann 			cfg_addr = (cfg_addr & mdev_state->bar_mask[index]);
322a5e6e650SGerd Hoffmann 		} else {
323a5e6e650SGerd Hoffmann 			cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
324a5e6e650SGerd Hoffmann 			if (cfg_addr)
325a5e6e650SGerd Hoffmann 				dev_info(dev, "BAR #%d @ 0x%x\n",
326a5e6e650SGerd Hoffmann 					 index, cfg_addr);
327a5e6e650SGerd Hoffmann 		}
328a5e6e650SGerd Hoffmann 
329a5e6e650SGerd Hoffmann 		cfg_addr |= (mdev_state->vconfig[offset] &
330a5e6e650SGerd Hoffmann 			     ~PCI_BASE_ADDRESS_MEM_MASK);
331a5e6e650SGerd Hoffmann 		STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
332a5e6e650SGerd Hoffmann 		break;
333a5e6e650SGerd Hoffmann 	}
334a5e6e650SGerd Hoffmann }
335a5e6e650SGerd Hoffmann 
handle_mmio_write(struct mdev_state * mdev_state,u16 offset,char * buf,u32 count)336a5e6e650SGerd Hoffmann static void handle_mmio_write(struct mdev_state *mdev_state, u16 offset,
337a5e6e650SGerd Hoffmann 			      char *buf, u32 count)
338a5e6e650SGerd Hoffmann {
339a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(mdev_state->mdev);
340a5e6e650SGerd Hoffmann 	int index;
341a5e6e650SGerd Hoffmann 	u16 reg16;
342a5e6e650SGerd Hoffmann 
343a5e6e650SGerd Hoffmann 	switch (offset) {
344a5e6e650SGerd Hoffmann 	case 0x400 ... 0x41f: /* vga ioports remapped */
345a5e6e650SGerd Hoffmann 		goto unhandled;
346a5e6e650SGerd Hoffmann 	case 0x500 ... 0x515: /* bochs dispi interface */
347a5e6e650SGerd Hoffmann 		if (count != 2)
348a5e6e650SGerd Hoffmann 			goto unhandled;
349a5e6e650SGerd Hoffmann 		index = (offset - 0x500) / 2;
350a5e6e650SGerd Hoffmann 		reg16 = *(u16 *)buf;
351a5e6e650SGerd Hoffmann 		if (index < ARRAY_SIZE(mdev_state->vbe))
352a5e6e650SGerd Hoffmann 			mdev_state->vbe[index] = reg16;
353a5e6e650SGerd Hoffmann 		dev_dbg(dev, "%s: vbe write %d = %d (%s)\n",
354a5e6e650SGerd Hoffmann 			__func__, index, reg16, vbe_name(index));
355a5e6e650SGerd Hoffmann 		break;
356a5e6e650SGerd Hoffmann 	case 0x600 ... 0x607: /* qemu extended regs */
357a5e6e650SGerd Hoffmann 		goto unhandled;
358a5e6e650SGerd Hoffmann 	default:
359a5e6e650SGerd Hoffmann unhandled:
360a5e6e650SGerd Hoffmann 		dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n",
361a5e6e650SGerd Hoffmann 			__func__, offset, count);
362a5e6e650SGerd Hoffmann 		break;
363a5e6e650SGerd Hoffmann 	}
364a5e6e650SGerd Hoffmann }
365a5e6e650SGerd Hoffmann 
handle_mmio_read(struct mdev_state * mdev_state,u16 offset,char * buf,u32 count)366a5e6e650SGerd Hoffmann static void handle_mmio_read(struct mdev_state *mdev_state, u16 offset,
367a5e6e650SGerd Hoffmann 			     char *buf, u32 count)
368a5e6e650SGerd Hoffmann {
369a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(mdev_state->mdev);
370104c7405SGerd Hoffmann 	struct vfio_region_gfx_edid *edid;
371a5e6e650SGerd Hoffmann 	u16 reg16 = 0;
372a5e6e650SGerd Hoffmann 	int index;
373a5e6e650SGerd Hoffmann 
374a5e6e650SGerd Hoffmann 	switch (offset) {
375104c7405SGerd Hoffmann 	case 0x000 ... 0x3ff: /* edid block */
376104c7405SGerd Hoffmann 		edid = &mdev_state->edid_regs;
377104c7405SGerd Hoffmann 		if (edid->link_state != VFIO_DEVICE_GFX_LINK_STATE_UP ||
378104c7405SGerd Hoffmann 		    offset >= edid->edid_size) {
379104c7405SGerd Hoffmann 			memset(buf, 0, count);
380104c7405SGerd Hoffmann 			break;
381104c7405SGerd Hoffmann 		}
382104c7405SGerd Hoffmann 		memcpy(buf, mdev_state->edid_blob + offset, count);
383104c7405SGerd Hoffmann 		break;
384a5e6e650SGerd Hoffmann 	case 0x500 ... 0x515: /* bochs dispi interface */
385a5e6e650SGerd Hoffmann 		if (count != 2)
386a5e6e650SGerd Hoffmann 			goto unhandled;
387a5e6e650SGerd Hoffmann 		index = (offset - 0x500) / 2;
388a5e6e650SGerd Hoffmann 		if (index < ARRAY_SIZE(mdev_state->vbe))
389a5e6e650SGerd Hoffmann 			reg16 = mdev_state->vbe[index];
390a5e6e650SGerd Hoffmann 		dev_dbg(dev, "%s: vbe read %d = %d (%s)\n",
391a5e6e650SGerd Hoffmann 			__func__, index, reg16, vbe_name(index));
392a5e6e650SGerd Hoffmann 		*(u16 *)buf = reg16;
393a5e6e650SGerd Hoffmann 		break;
394a5e6e650SGerd Hoffmann 	default:
395a5e6e650SGerd Hoffmann unhandled:
396a5e6e650SGerd Hoffmann 		dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n",
397a5e6e650SGerd Hoffmann 			__func__, offset, count);
398a5e6e650SGerd Hoffmann 		memset(buf, 0, count);
399a5e6e650SGerd Hoffmann 		break;
400a5e6e650SGerd Hoffmann 	}
401a5e6e650SGerd Hoffmann }
402a5e6e650SGerd Hoffmann 
handle_edid_regs(struct mdev_state * mdev_state,u16 offset,char * buf,u32 count,bool is_write)403104c7405SGerd Hoffmann static void handle_edid_regs(struct mdev_state *mdev_state, u16 offset,
404104c7405SGerd Hoffmann 			     char *buf, u32 count, bool is_write)
405104c7405SGerd Hoffmann {
406104c7405SGerd Hoffmann 	char *regs = (void *)&mdev_state->edid_regs;
407104c7405SGerd Hoffmann 
408104c7405SGerd Hoffmann 	if (offset + count > sizeof(mdev_state->edid_regs))
409104c7405SGerd Hoffmann 		return;
410104c7405SGerd Hoffmann 	if (count != 4)
411104c7405SGerd Hoffmann 		return;
412104c7405SGerd Hoffmann 	if (offset % 4)
413104c7405SGerd Hoffmann 		return;
414104c7405SGerd Hoffmann 
415104c7405SGerd Hoffmann 	if (is_write) {
416104c7405SGerd Hoffmann 		switch (offset) {
417104c7405SGerd Hoffmann 		case offsetof(struct vfio_region_gfx_edid, link_state):
418104c7405SGerd Hoffmann 		case offsetof(struct vfio_region_gfx_edid, edid_size):
419104c7405SGerd Hoffmann 			memcpy(regs + offset, buf, count);
420104c7405SGerd Hoffmann 			break;
421104c7405SGerd Hoffmann 		default:
422104c7405SGerd Hoffmann 			/* read-only regs */
423104c7405SGerd Hoffmann 			break;
424104c7405SGerd Hoffmann 		}
425104c7405SGerd Hoffmann 	} else {
426104c7405SGerd Hoffmann 		memcpy(buf, regs + offset, count);
427104c7405SGerd Hoffmann 	}
428104c7405SGerd Hoffmann }
429104c7405SGerd Hoffmann 
handle_edid_blob(struct mdev_state * mdev_state,u16 offset,char * buf,u32 count,bool is_write)430104c7405SGerd Hoffmann static void handle_edid_blob(struct mdev_state *mdev_state, u16 offset,
431104c7405SGerd Hoffmann 			     char *buf, u32 count, bool is_write)
432104c7405SGerd Hoffmann {
433104c7405SGerd Hoffmann 	if (offset + count > mdev_state->edid_regs.edid_max_size)
434104c7405SGerd Hoffmann 		return;
435104c7405SGerd Hoffmann 	if (is_write)
436104c7405SGerd Hoffmann 		memcpy(mdev_state->edid_blob + offset, buf, count);
437104c7405SGerd Hoffmann 	else
438104c7405SGerd Hoffmann 		memcpy(buf, mdev_state->edid_blob + offset, count);
439104c7405SGerd Hoffmann }
440104c7405SGerd Hoffmann 
mdev_access(struct mdev_state * mdev_state,char * buf,size_t count,loff_t pos,bool is_write)441681c1615SJason Gunthorpe static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
442681c1615SJason Gunthorpe 			   size_t count, loff_t pos, bool is_write)
443a5e6e650SGerd Hoffmann {
444a5e6e650SGerd Hoffmann 	struct page *pg;
445a5e6e650SGerd Hoffmann 	loff_t poff;
446a5e6e650SGerd Hoffmann 	char *map;
447a5e6e650SGerd Hoffmann 	int ret = 0;
448a5e6e650SGerd Hoffmann 
449a5e6e650SGerd Hoffmann 	mutex_lock(&mdev_state->ops_lock);
450a5e6e650SGerd Hoffmann 
451a5e6e650SGerd Hoffmann 	if (pos < MBOCHS_CONFIG_SPACE_SIZE) {
452a5e6e650SGerd Hoffmann 		if (is_write)
453a5e6e650SGerd Hoffmann 			handle_pci_cfg_write(mdev_state, pos, buf, count);
454a5e6e650SGerd Hoffmann 		else
455a5e6e650SGerd Hoffmann 			memcpy(buf, (mdev_state->vconfig + pos), count);
456a5e6e650SGerd Hoffmann 
457a5e6e650SGerd Hoffmann 	} else if (pos >= MBOCHS_MMIO_BAR_OFFSET &&
458104c7405SGerd Hoffmann 		   pos + count <= (MBOCHS_MMIO_BAR_OFFSET +
459104c7405SGerd Hoffmann 				   MBOCHS_MMIO_BAR_SIZE)) {
460a5e6e650SGerd Hoffmann 		pos -= MBOCHS_MMIO_BAR_OFFSET;
461a5e6e650SGerd Hoffmann 		if (is_write)
462a5e6e650SGerd Hoffmann 			handle_mmio_write(mdev_state, pos, buf, count);
463a5e6e650SGerd Hoffmann 		else
464a5e6e650SGerd Hoffmann 			handle_mmio_read(mdev_state, pos, buf, count);
465a5e6e650SGerd Hoffmann 
466104c7405SGerd Hoffmann 	} else if (pos >= MBOCHS_EDID_OFFSET &&
467104c7405SGerd Hoffmann 		   pos + count <= (MBOCHS_EDID_OFFSET +
468104c7405SGerd Hoffmann 				   MBOCHS_EDID_SIZE)) {
469104c7405SGerd Hoffmann 		pos -= MBOCHS_EDID_OFFSET;
470104c7405SGerd Hoffmann 		if (pos < MBOCHS_EDID_BLOB_OFFSET) {
471104c7405SGerd Hoffmann 			handle_edid_regs(mdev_state, pos, buf, count, is_write);
472104c7405SGerd Hoffmann 		} else {
473104c7405SGerd Hoffmann 			pos -= MBOCHS_EDID_BLOB_OFFSET;
474104c7405SGerd Hoffmann 			handle_edid_blob(mdev_state, pos, buf, count, is_write);
475104c7405SGerd Hoffmann 		}
476104c7405SGerd Hoffmann 
477a5e6e650SGerd Hoffmann 	} else if (pos >= MBOCHS_MEMORY_BAR_OFFSET &&
478a5e6e650SGerd Hoffmann 		   pos + count <=
479a5e6e650SGerd Hoffmann 		   MBOCHS_MEMORY_BAR_OFFSET + mdev_state->memsize) {
480a5e6e650SGerd Hoffmann 		pos -= MBOCHS_MMIO_BAR_OFFSET;
481a5e6e650SGerd Hoffmann 		poff = pos & ~PAGE_MASK;
482498e8bf5SAlexey Khoroshilov 		pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
483a5e6e650SGerd Hoffmann 		map = kmap(pg);
484a5e6e650SGerd Hoffmann 		if (is_write)
485a5e6e650SGerd Hoffmann 			memcpy(map + poff, buf, count);
486a5e6e650SGerd Hoffmann 		else
487a5e6e650SGerd Hoffmann 			memcpy(buf, map + poff, count);
488a5e6e650SGerd Hoffmann 		kunmap(pg);
489a5e6e650SGerd Hoffmann 		put_page(pg);
490a5e6e650SGerd Hoffmann 
491a5e6e650SGerd Hoffmann 	} else {
492681c1615SJason Gunthorpe 		dev_dbg(mdev_state->vdev.dev, "%s: %s @0x%llx (unhandled)\n",
493a5e6e650SGerd Hoffmann 			__func__, is_write ? "WR" : "RD", pos);
494a5e6e650SGerd Hoffmann 		ret = -1;
495a5e6e650SGerd Hoffmann 		goto accessfailed;
496a5e6e650SGerd Hoffmann 	}
497a5e6e650SGerd Hoffmann 
498a5e6e650SGerd Hoffmann 	ret = count;
499a5e6e650SGerd Hoffmann 
500a5e6e650SGerd Hoffmann 
501a5e6e650SGerd Hoffmann accessfailed:
502a5e6e650SGerd Hoffmann 	mutex_unlock(&mdev_state->ops_lock);
503a5e6e650SGerd Hoffmann 
504a5e6e650SGerd Hoffmann 	return ret;
505a5e6e650SGerd Hoffmann }
506a5e6e650SGerd Hoffmann 
mbochs_reset(struct mdev_state * mdev_state)507681c1615SJason Gunthorpe static int mbochs_reset(struct mdev_state *mdev_state)
508a5e6e650SGerd Hoffmann {
509a5e6e650SGerd Hoffmann 	u32 size64k = mdev_state->memsize / (64 * 1024);
510a5e6e650SGerd Hoffmann 	int i;
511a5e6e650SGerd Hoffmann 
512a5e6e650SGerd Hoffmann 	for (i = 0; i < ARRAY_SIZE(mdev_state->vbe); i++)
513a5e6e650SGerd Hoffmann 		mdev_state->vbe[i] = 0;
514a5e6e650SGerd Hoffmann 	mdev_state->vbe[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
515a5e6e650SGerd Hoffmann 	mdev_state->vbe[VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = size64k;
516a5e6e650SGerd Hoffmann 	return 0;
517a5e6e650SGerd Hoffmann }
518a5e6e650SGerd Hoffmann 
mbochs_init_dev(struct vfio_device * vdev)5193d5d18e1SYi Liu static int mbochs_init_dev(struct vfio_device *vdev)
520a5e6e650SGerd Hoffmann {
5213d5d18e1SYi Liu 	struct mdev_state *mdev_state =
5223d5d18e1SYi Liu 		container_of(vdev, struct mdev_state, vdev);
5233d5d18e1SYi Liu 	struct mdev_device *mdev = to_mdev_device(vdev->dev);
524da44c340SChristoph Hellwig 	struct mbochs_type *type =
525da44c340SChristoph Hellwig 		container_of(mdev->type, struct mbochs_type, type);
5263d5d18e1SYi Liu 	int avail_mbytes = atomic_read(&mbochs_avail_mbytes);
527681c1615SJason Gunthorpe 	int ret = -ENOMEM;
528a5e6e650SGerd Hoffmann 
529de5494afSJason Gunthorpe 	do {
530de5494afSJason Gunthorpe 		if (avail_mbytes < type->mbytes)
531de5494afSJason Gunthorpe 			return -ENOSPC;
532de5494afSJason Gunthorpe 	} while (!atomic_try_cmpxchg(&mbochs_avail_mbytes, &avail_mbytes,
533de5494afSJason Gunthorpe 				     avail_mbytes - type->mbytes));
534a5e6e650SGerd Hoffmann 
535a5e6e650SGerd Hoffmann 	mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
5363d5d18e1SYi Liu 	if (!mdev_state->vconfig)
5373d5d18e1SYi Liu 		goto err_avail;
538a5e6e650SGerd Hoffmann 
539a5e6e650SGerd Hoffmann 	mdev_state->memsize = type->mbytes * 1024 * 1024;
540a5e6e650SGerd Hoffmann 	mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT;
541a5e6e650SGerd Hoffmann 	mdev_state->pages = kcalloc(mdev_state->pagecount,
542a5e6e650SGerd Hoffmann 				    sizeof(struct page *),
543a5e6e650SGerd Hoffmann 				    GFP_KERNEL);
544a5e6e650SGerd Hoffmann 	if (!mdev_state->pages)
5453d5d18e1SYi Liu 		goto err_vconfig;
546a5e6e650SGerd Hoffmann 
547a5e6e650SGerd Hoffmann 	mutex_init(&mdev_state->ops_lock);
548a5e6e650SGerd Hoffmann 	mdev_state->mdev = mdev;
549a5e6e650SGerd Hoffmann 	INIT_LIST_HEAD(&mdev_state->dmabufs);
550a5e6e650SGerd Hoffmann 	mdev_state->next_id = 1;
551a5e6e650SGerd Hoffmann 
552a5e6e650SGerd Hoffmann 	mdev_state->type = type;
553104c7405SGerd Hoffmann 	mdev_state->edid_regs.max_xres = type->max_x;
554104c7405SGerd Hoffmann 	mdev_state->edid_regs.max_yres = type->max_y;
555104c7405SGerd Hoffmann 	mdev_state->edid_regs.edid_offset = MBOCHS_EDID_BLOB_OFFSET;
556104c7405SGerd Hoffmann 	mdev_state->edid_regs.edid_max_size = sizeof(mdev_state->edid_blob);
557a5e6e650SGerd Hoffmann 	mbochs_create_config_space(mdev_state);
558681c1615SJason Gunthorpe 	mbochs_reset(mdev_state);
559a5e6e650SGerd Hoffmann 
5603d5d18e1SYi Liu 	dev_info(vdev->dev, "%s: %s, %d MB, %ld pages\n", __func__,
5610bc79069SChristoph Hellwig 		 type->type.pretty_name, type->mbytes, mdev_state->pagecount);
562a5e6e650SGerd Hoffmann 	return 0;
5633d5d18e1SYi Liu 
5643d5d18e1SYi Liu err_vconfig:
565a5e6e650SGerd Hoffmann 	kfree(mdev_state->vconfig);
566de5494afSJason Gunthorpe err_avail:
567de5494afSJason Gunthorpe 	atomic_add(type->mbytes, &mbochs_avail_mbytes);
568681c1615SJason Gunthorpe 	return ret;
569a5e6e650SGerd Hoffmann }
570a5e6e650SGerd Hoffmann 
mbochs_probe(struct mdev_device * mdev)5713d5d18e1SYi Liu static int mbochs_probe(struct mdev_device *mdev)
5723d5d18e1SYi Liu {
5733d5d18e1SYi Liu 	struct mdev_state *mdev_state;
5743d5d18e1SYi Liu 	int ret = -ENOMEM;
5753d5d18e1SYi Liu 
5763d5d18e1SYi Liu 	mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
5773d5d18e1SYi Liu 				       &mbochs_dev_ops);
5783d5d18e1SYi Liu 	if (IS_ERR(mdev_state))
5793d5d18e1SYi Liu 		return PTR_ERR(mdev_state);
5803d5d18e1SYi Liu 
5813d5d18e1SYi Liu 	ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
5823d5d18e1SYi Liu 	if (ret)
5833d5d18e1SYi Liu 		goto err_put_vdev;
5843d5d18e1SYi Liu 	dev_set_drvdata(&mdev->dev, mdev_state);
5853d5d18e1SYi Liu 	return 0;
5863d5d18e1SYi Liu 
5873d5d18e1SYi Liu err_put_vdev:
5883d5d18e1SYi Liu 	vfio_put_device(&mdev_state->vdev);
5893d5d18e1SYi Liu 	return ret;
5903d5d18e1SYi Liu }
5913d5d18e1SYi Liu 
mbochs_release_dev(struct vfio_device * vdev)5923d5d18e1SYi Liu static void mbochs_release_dev(struct vfio_device *vdev)
5933d5d18e1SYi Liu {
5943d5d18e1SYi Liu 	struct mdev_state *mdev_state =
5953d5d18e1SYi Liu 		container_of(vdev, struct mdev_state, vdev);
5963d5d18e1SYi Liu 
5973d5d18e1SYi Liu 	atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
5983d5d18e1SYi Liu 	kfree(mdev_state->pages);
5993d5d18e1SYi Liu 	kfree(mdev_state->vconfig);
6003d5d18e1SYi Liu }
6013d5d18e1SYi Liu 
mbochs_remove(struct mdev_device * mdev)602681c1615SJason Gunthorpe static void mbochs_remove(struct mdev_device *mdev)
603a5e6e650SGerd Hoffmann {
604681c1615SJason Gunthorpe 	struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
605a5e6e650SGerd Hoffmann 
606681c1615SJason Gunthorpe 	vfio_unregister_group_dev(&mdev_state->vdev);
6073d5d18e1SYi Liu 	vfio_put_device(&mdev_state->vdev);
608a5e6e650SGerd Hoffmann }
609a5e6e650SGerd Hoffmann 
mbochs_read(struct vfio_device * vdev,char __user * buf,size_t count,loff_t * ppos)610681c1615SJason Gunthorpe static ssize_t mbochs_read(struct vfio_device *vdev, char __user *buf,
611a5e6e650SGerd Hoffmann 			   size_t count, loff_t *ppos)
612a5e6e650SGerd Hoffmann {
613681c1615SJason Gunthorpe 	struct mdev_state *mdev_state =
614681c1615SJason Gunthorpe 		container_of(vdev, struct mdev_state, vdev);
615a5e6e650SGerd Hoffmann 	unsigned int done = 0;
616a5e6e650SGerd Hoffmann 	int ret;
617a5e6e650SGerd Hoffmann 
618a5e6e650SGerd Hoffmann 	while (count) {
619a5e6e650SGerd Hoffmann 		size_t filled;
620a5e6e650SGerd Hoffmann 
621a5e6e650SGerd Hoffmann 		if (count >= 4 && !(*ppos % 4)) {
622a5e6e650SGerd Hoffmann 			u32 val;
623a5e6e650SGerd Hoffmann 
624681c1615SJason Gunthorpe 			ret =  mdev_access(mdev_state, (char *)&val, sizeof(val),
625a5e6e650SGerd Hoffmann 					   *ppos, false);
626a5e6e650SGerd Hoffmann 			if (ret <= 0)
627a5e6e650SGerd Hoffmann 				goto read_err;
628a5e6e650SGerd Hoffmann 
629a5e6e650SGerd Hoffmann 			if (copy_to_user(buf, &val, sizeof(val)))
630a5e6e650SGerd Hoffmann 				goto read_err;
631a5e6e650SGerd Hoffmann 
632a5e6e650SGerd Hoffmann 			filled = 4;
633a5e6e650SGerd Hoffmann 		} else if (count >= 2 && !(*ppos % 2)) {
634a5e6e650SGerd Hoffmann 			u16 val;
635a5e6e650SGerd Hoffmann 
636681c1615SJason Gunthorpe 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
637a5e6e650SGerd Hoffmann 					  *ppos, false);
638a5e6e650SGerd Hoffmann 			if (ret <= 0)
639a5e6e650SGerd Hoffmann 				goto read_err;
640a5e6e650SGerd Hoffmann 
641a5e6e650SGerd Hoffmann 			if (copy_to_user(buf, &val, sizeof(val)))
642a5e6e650SGerd Hoffmann 				goto read_err;
643a5e6e650SGerd Hoffmann 
644a5e6e650SGerd Hoffmann 			filled = 2;
645a5e6e650SGerd Hoffmann 		} else {
646a5e6e650SGerd Hoffmann 			u8 val;
647a5e6e650SGerd Hoffmann 
648681c1615SJason Gunthorpe 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
649a5e6e650SGerd Hoffmann 					  *ppos, false);
650a5e6e650SGerd Hoffmann 			if (ret <= 0)
651a5e6e650SGerd Hoffmann 				goto read_err;
652a5e6e650SGerd Hoffmann 
653a5e6e650SGerd Hoffmann 			if (copy_to_user(buf, &val, sizeof(val)))
654a5e6e650SGerd Hoffmann 				goto read_err;
655a5e6e650SGerd Hoffmann 
656a5e6e650SGerd Hoffmann 			filled = 1;
657a5e6e650SGerd Hoffmann 		}
658a5e6e650SGerd Hoffmann 
659a5e6e650SGerd Hoffmann 		count -= filled;
660a5e6e650SGerd Hoffmann 		done += filled;
661a5e6e650SGerd Hoffmann 		*ppos += filled;
662a5e6e650SGerd Hoffmann 		buf += filled;
663a5e6e650SGerd Hoffmann 	}
664a5e6e650SGerd Hoffmann 
665a5e6e650SGerd Hoffmann 	return done;
666a5e6e650SGerd Hoffmann 
667a5e6e650SGerd Hoffmann read_err:
668a5e6e650SGerd Hoffmann 	return -EFAULT;
669a5e6e650SGerd Hoffmann }
670a5e6e650SGerd Hoffmann 
mbochs_write(struct vfio_device * vdev,const char __user * buf,size_t count,loff_t * ppos)671681c1615SJason Gunthorpe static ssize_t mbochs_write(struct vfio_device *vdev, const char __user *buf,
672a5e6e650SGerd Hoffmann 			    size_t count, loff_t *ppos)
673a5e6e650SGerd Hoffmann {
674681c1615SJason Gunthorpe 	struct mdev_state *mdev_state =
675681c1615SJason Gunthorpe 		container_of(vdev, struct mdev_state, vdev);
676a5e6e650SGerd Hoffmann 	unsigned int done = 0;
677a5e6e650SGerd Hoffmann 	int ret;
678a5e6e650SGerd Hoffmann 
679a5e6e650SGerd Hoffmann 	while (count) {
680a5e6e650SGerd Hoffmann 		size_t filled;
681a5e6e650SGerd Hoffmann 
682a5e6e650SGerd Hoffmann 		if (count >= 4 && !(*ppos % 4)) {
683a5e6e650SGerd Hoffmann 			u32 val;
684a5e6e650SGerd Hoffmann 
685a5e6e650SGerd Hoffmann 			if (copy_from_user(&val, buf, sizeof(val)))
686a5e6e650SGerd Hoffmann 				goto write_err;
687a5e6e650SGerd Hoffmann 
688681c1615SJason Gunthorpe 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
689a5e6e650SGerd Hoffmann 					  *ppos, true);
690a5e6e650SGerd Hoffmann 			if (ret <= 0)
691a5e6e650SGerd Hoffmann 				goto write_err;
692a5e6e650SGerd Hoffmann 
693a5e6e650SGerd Hoffmann 			filled = 4;
694a5e6e650SGerd Hoffmann 		} else if (count >= 2 && !(*ppos % 2)) {
695a5e6e650SGerd Hoffmann 			u16 val;
696a5e6e650SGerd Hoffmann 
697a5e6e650SGerd Hoffmann 			if (copy_from_user(&val, buf, sizeof(val)))
698a5e6e650SGerd Hoffmann 				goto write_err;
699a5e6e650SGerd Hoffmann 
700681c1615SJason Gunthorpe 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
701a5e6e650SGerd Hoffmann 					  *ppos, true);
702a5e6e650SGerd Hoffmann 			if (ret <= 0)
703a5e6e650SGerd Hoffmann 				goto write_err;
704a5e6e650SGerd Hoffmann 
705a5e6e650SGerd Hoffmann 			filled = 2;
706a5e6e650SGerd Hoffmann 		} else {
707a5e6e650SGerd Hoffmann 			u8 val;
708a5e6e650SGerd Hoffmann 
709a5e6e650SGerd Hoffmann 			if (copy_from_user(&val, buf, sizeof(val)))
710a5e6e650SGerd Hoffmann 				goto write_err;
711a5e6e650SGerd Hoffmann 
712681c1615SJason Gunthorpe 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
713a5e6e650SGerd Hoffmann 					  *ppos, true);
714a5e6e650SGerd Hoffmann 			if (ret <= 0)
715a5e6e650SGerd Hoffmann 				goto write_err;
716a5e6e650SGerd Hoffmann 
717a5e6e650SGerd Hoffmann 			filled = 1;
718a5e6e650SGerd Hoffmann 		}
719a5e6e650SGerd Hoffmann 		count -= filled;
720a5e6e650SGerd Hoffmann 		done += filled;
721a5e6e650SGerd Hoffmann 		*ppos += filled;
722a5e6e650SGerd Hoffmann 		buf += filled;
723a5e6e650SGerd Hoffmann 	}
724a5e6e650SGerd Hoffmann 
725a5e6e650SGerd Hoffmann 	return done;
726a5e6e650SGerd Hoffmann write_err:
727a5e6e650SGerd Hoffmann 	return -EFAULT;
728a5e6e650SGerd Hoffmann }
729a5e6e650SGerd Hoffmann 
__mbochs_get_page(struct mdev_state * mdev_state,pgoff_t pgoff)730a5e6e650SGerd Hoffmann static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
731a5e6e650SGerd Hoffmann 				      pgoff_t pgoff)
732a5e6e650SGerd Hoffmann {
733a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
734a5e6e650SGerd Hoffmann 
735a5e6e650SGerd Hoffmann 	if (!mdev_state->pages[pgoff]) {
736a5e6e650SGerd Hoffmann 		mdev_state->pages[pgoff] =
737a5e6e650SGerd Hoffmann 			alloc_pages(GFP_HIGHUSER | __GFP_ZERO, 0);
738a5e6e650SGerd Hoffmann 		if (!mdev_state->pages[pgoff])
739a5e6e650SGerd Hoffmann 			return NULL;
740a5e6e650SGerd Hoffmann 	}
741a5e6e650SGerd Hoffmann 
742a5e6e650SGerd Hoffmann 	get_page(mdev_state->pages[pgoff]);
743a5e6e650SGerd Hoffmann 	return mdev_state->pages[pgoff];
744a5e6e650SGerd Hoffmann }
745a5e6e650SGerd Hoffmann 
mbochs_get_page(struct mdev_state * mdev_state,pgoff_t pgoff)746a5e6e650SGerd Hoffmann static struct page *mbochs_get_page(struct mdev_state *mdev_state,
747a5e6e650SGerd Hoffmann 				    pgoff_t pgoff)
748a5e6e650SGerd Hoffmann {
749a5e6e650SGerd Hoffmann 	struct page *page;
750a5e6e650SGerd Hoffmann 
751a5e6e650SGerd Hoffmann 	if (WARN_ON(pgoff >= mdev_state->pagecount))
752a5e6e650SGerd Hoffmann 		return NULL;
753a5e6e650SGerd Hoffmann 
754a5e6e650SGerd Hoffmann 	mutex_lock(&mdev_state->ops_lock);
755a5e6e650SGerd Hoffmann 	page = __mbochs_get_page(mdev_state, pgoff);
756a5e6e650SGerd Hoffmann 	mutex_unlock(&mdev_state->ops_lock);
757a5e6e650SGerd Hoffmann 
758a5e6e650SGerd Hoffmann 	return page;
759a5e6e650SGerd Hoffmann }
760a5e6e650SGerd Hoffmann 
mbochs_put_pages(struct mdev_state * mdev_state)761a5e6e650SGerd Hoffmann static void mbochs_put_pages(struct mdev_state *mdev_state)
762a5e6e650SGerd Hoffmann {
763a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(mdev_state->mdev);
764a5e6e650SGerd Hoffmann 	int i, count = 0;
765a5e6e650SGerd Hoffmann 
766a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
767a5e6e650SGerd Hoffmann 
768a5e6e650SGerd Hoffmann 	for (i = 0; i < mdev_state->pagecount; i++) {
769a5e6e650SGerd Hoffmann 		if (!mdev_state->pages[i])
770a5e6e650SGerd Hoffmann 			continue;
771a5e6e650SGerd Hoffmann 		put_page(mdev_state->pages[i]);
772a5e6e650SGerd Hoffmann 		mdev_state->pages[i] = NULL;
773a5e6e650SGerd Hoffmann 		count++;
774a5e6e650SGerd Hoffmann 	}
775a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s: %d pages released\n", __func__, count);
776a5e6e650SGerd Hoffmann }
777a5e6e650SGerd Hoffmann 
mbochs_region_vm_fault(struct vm_fault * vmf)778d7ef4899SSouptick Joarder static vm_fault_t mbochs_region_vm_fault(struct vm_fault *vmf)
779a5e6e650SGerd Hoffmann {
780a5e6e650SGerd Hoffmann 	struct vm_area_struct *vma = vmf->vma;
781a5e6e650SGerd Hoffmann 	struct mdev_state *mdev_state = vma->vm_private_data;
782a5e6e650SGerd Hoffmann 	pgoff_t page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
783a5e6e650SGerd Hoffmann 
784a5e6e650SGerd Hoffmann 	if (page_offset >= mdev_state->pagecount)
785a5e6e650SGerd Hoffmann 		return VM_FAULT_SIGBUS;
786a5e6e650SGerd Hoffmann 
787a5e6e650SGerd Hoffmann 	vmf->page = mbochs_get_page(mdev_state, page_offset);
788a5e6e650SGerd Hoffmann 	if (!vmf->page)
789a5e6e650SGerd Hoffmann 		return VM_FAULT_SIGBUS;
790a5e6e650SGerd Hoffmann 
791a5e6e650SGerd Hoffmann 	return 0;
792a5e6e650SGerd Hoffmann }
793a5e6e650SGerd Hoffmann 
794a5e6e650SGerd Hoffmann static const struct vm_operations_struct mbochs_region_vm_ops = {
795a5e6e650SGerd Hoffmann 	.fault = mbochs_region_vm_fault,
796a5e6e650SGerd Hoffmann };
797a5e6e650SGerd Hoffmann 
mbochs_mmap(struct vfio_device * vdev,struct vm_area_struct * vma)798681c1615SJason Gunthorpe static int mbochs_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
799a5e6e650SGerd Hoffmann {
800681c1615SJason Gunthorpe 	struct mdev_state *mdev_state =
801681c1615SJason Gunthorpe 		container_of(vdev, struct mdev_state, vdev);
802a5e6e650SGerd Hoffmann 
803a5e6e650SGerd Hoffmann 	if (vma->vm_pgoff != MBOCHS_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
804a5e6e650SGerd Hoffmann 		return -EINVAL;
805a5e6e650SGerd Hoffmann 	if (vma->vm_end < vma->vm_start)
806a5e6e650SGerd Hoffmann 		return -EINVAL;
807a5e6e650SGerd Hoffmann 	if (vma->vm_end - vma->vm_start > mdev_state->memsize)
808a5e6e650SGerd Hoffmann 		return -EINVAL;
809a5e6e650SGerd Hoffmann 	if ((vma->vm_flags & VM_SHARED) == 0)
810a5e6e650SGerd Hoffmann 		return -EINVAL;
811a5e6e650SGerd Hoffmann 
812a5e6e650SGerd Hoffmann 	vma->vm_ops = &mbochs_region_vm_ops;
813a5e6e650SGerd Hoffmann 	vma->vm_private_data = mdev_state;
814a5e6e650SGerd Hoffmann 	return 0;
815a5e6e650SGerd Hoffmann }
816a5e6e650SGerd Hoffmann 
mbochs_dmabuf_vm_fault(struct vm_fault * vmf)817d7ef4899SSouptick Joarder static vm_fault_t mbochs_dmabuf_vm_fault(struct vm_fault *vmf)
818a5e6e650SGerd Hoffmann {
819a5e6e650SGerd Hoffmann 	struct vm_area_struct *vma = vmf->vma;
820a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf = vma->vm_private_data;
821a5e6e650SGerd Hoffmann 
822a5e6e650SGerd Hoffmann 	if (WARN_ON(vmf->pgoff >= dmabuf->pagecount))
823a5e6e650SGerd Hoffmann 		return VM_FAULT_SIGBUS;
824a5e6e650SGerd Hoffmann 
825a5e6e650SGerd Hoffmann 	vmf->page = dmabuf->pages[vmf->pgoff];
826a5e6e650SGerd Hoffmann 	get_page(vmf->page);
827a5e6e650SGerd Hoffmann 	return 0;
828a5e6e650SGerd Hoffmann }
829a5e6e650SGerd Hoffmann 
830a5e6e650SGerd Hoffmann static const struct vm_operations_struct mbochs_dmabuf_vm_ops = {
831a5e6e650SGerd Hoffmann 	.fault = mbochs_dmabuf_vm_fault,
832a5e6e650SGerd Hoffmann };
833a5e6e650SGerd Hoffmann 
mbochs_mmap_dmabuf(struct dma_buf * buf,struct vm_area_struct * vma)834a5e6e650SGerd Hoffmann static int mbochs_mmap_dmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
835a5e6e650SGerd Hoffmann {
836a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf = buf->priv;
837a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
838a5e6e650SGerd Hoffmann 
839a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
840a5e6e650SGerd Hoffmann 
841a5e6e650SGerd Hoffmann 	if ((vma->vm_flags & VM_SHARED) == 0)
842a5e6e650SGerd Hoffmann 		return -EINVAL;
843a5e6e650SGerd Hoffmann 
844a5e6e650SGerd Hoffmann 	vma->vm_ops = &mbochs_dmabuf_vm_ops;
845a5e6e650SGerd Hoffmann 	vma->vm_private_data = dmabuf;
846a5e6e650SGerd Hoffmann 	return 0;
847a5e6e650SGerd Hoffmann }
848a5e6e650SGerd Hoffmann 
mbochs_print_dmabuf(struct mbochs_dmabuf * dmabuf,const char * prefix)849a5e6e650SGerd Hoffmann static void mbochs_print_dmabuf(struct mbochs_dmabuf *dmabuf,
850a5e6e650SGerd Hoffmann 				const char *prefix)
851a5e6e650SGerd Hoffmann {
852a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
853a5e6e650SGerd Hoffmann 	u32 fourcc = dmabuf->mode.drm_format;
854a5e6e650SGerd Hoffmann 
855a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s/%d: %c%c%c%c, %dx%d, stride %d, off 0x%llx, size 0x%llx, pages %ld\n",
856a5e6e650SGerd Hoffmann 		prefix, dmabuf->id,
857a5e6e650SGerd Hoffmann 		fourcc ? ((fourcc >>  0) & 0xff) : '-',
858a5e6e650SGerd Hoffmann 		fourcc ? ((fourcc >>  8) & 0xff) : '-',
859a5e6e650SGerd Hoffmann 		fourcc ? ((fourcc >> 16) & 0xff) : '-',
860a5e6e650SGerd Hoffmann 		fourcc ? ((fourcc >> 24) & 0xff) : '-',
861a5e6e650SGerd Hoffmann 		dmabuf->mode.width, dmabuf->mode.height, dmabuf->mode.stride,
862a5e6e650SGerd Hoffmann 		dmabuf->mode.offset, dmabuf->mode.size, dmabuf->pagecount);
863a5e6e650SGerd Hoffmann }
864a5e6e650SGerd Hoffmann 
mbochs_map_dmabuf(struct dma_buf_attachment * at,enum dma_data_direction direction)865a5e6e650SGerd Hoffmann static struct sg_table *mbochs_map_dmabuf(struct dma_buf_attachment *at,
866a5e6e650SGerd Hoffmann 					  enum dma_data_direction direction)
867a5e6e650SGerd Hoffmann {
868a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf = at->dmabuf->priv;
869a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
870a5e6e650SGerd Hoffmann 	struct sg_table *sg;
871a5e6e650SGerd Hoffmann 
872a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
873a5e6e650SGerd Hoffmann 
874a5e6e650SGerd Hoffmann 	sg = kzalloc(sizeof(*sg), GFP_KERNEL);
875a5e6e650SGerd Hoffmann 	if (!sg)
876a5e6e650SGerd Hoffmann 		goto err1;
877a5e6e650SGerd Hoffmann 	if (sg_alloc_table_from_pages(sg, dmabuf->pages, dmabuf->pagecount,
878a5e6e650SGerd Hoffmann 				      0, dmabuf->mode.size, GFP_KERNEL) < 0)
879a5e6e650SGerd Hoffmann 		goto err2;
880be0704beSMarek Szyprowski 	if (dma_map_sgtable(at->dev, sg, direction, 0))
881a5e6e650SGerd Hoffmann 		goto err3;
882a5e6e650SGerd Hoffmann 
883a5e6e650SGerd Hoffmann 	return sg;
884a5e6e650SGerd Hoffmann 
885a5e6e650SGerd Hoffmann err3:
886a5e6e650SGerd Hoffmann 	sg_free_table(sg);
887a5e6e650SGerd Hoffmann err2:
888a5e6e650SGerd Hoffmann 	kfree(sg);
889a5e6e650SGerd Hoffmann err1:
890a5e6e650SGerd Hoffmann 	return ERR_PTR(-ENOMEM);
891a5e6e650SGerd Hoffmann }
892a5e6e650SGerd Hoffmann 
mbochs_unmap_dmabuf(struct dma_buf_attachment * at,struct sg_table * sg,enum dma_data_direction direction)893a5e6e650SGerd Hoffmann static void mbochs_unmap_dmabuf(struct dma_buf_attachment *at,
894a5e6e650SGerd Hoffmann 				struct sg_table *sg,
895a5e6e650SGerd Hoffmann 				enum dma_data_direction direction)
896a5e6e650SGerd Hoffmann {
897a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf = at->dmabuf->priv;
898a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
899a5e6e650SGerd Hoffmann 
900a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
901a5e6e650SGerd Hoffmann 
902be0704beSMarek Szyprowski 	dma_unmap_sgtable(at->dev, sg, direction, 0);
903a5e6e650SGerd Hoffmann 	sg_free_table(sg);
904a5e6e650SGerd Hoffmann 	kfree(sg);
905a5e6e650SGerd Hoffmann }
906a5e6e650SGerd Hoffmann 
mbochs_release_dmabuf(struct dma_buf * buf)907a5e6e650SGerd Hoffmann static void mbochs_release_dmabuf(struct dma_buf *buf)
908a5e6e650SGerd Hoffmann {
909a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf = buf->priv;
910a5e6e650SGerd Hoffmann 	struct mdev_state *mdev_state = dmabuf->mdev_state;
911a5e6e650SGerd Hoffmann 	struct device *dev = mdev_dev(mdev_state->mdev);
912a5e6e650SGerd Hoffmann 	pgoff_t pg;
913a5e6e650SGerd Hoffmann 
914a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
915a5e6e650SGerd Hoffmann 
916a5e6e650SGerd Hoffmann 	for (pg = 0; pg < dmabuf->pagecount; pg++)
917a5e6e650SGerd Hoffmann 		put_page(dmabuf->pages[pg]);
918a5e6e650SGerd Hoffmann 
919a5e6e650SGerd Hoffmann 	mutex_lock(&mdev_state->ops_lock);
920a5e6e650SGerd Hoffmann 	dmabuf->buf = NULL;
921a5e6e650SGerd Hoffmann 	if (dmabuf->unlinked)
922a5e6e650SGerd Hoffmann 		kfree(dmabuf);
923a5e6e650SGerd Hoffmann 	mutex_unlock(&mdev_state->ops_lock);
924a5e6e650SGerd Hoffmann }
925a5e6e650SGerd Hoffmann 
926a5e6e650SGerd Hoffmann static struct dma_buf_ops mbochs_dmabuf_ops = {
927a5e6e650SGerd Hoffmann 	.map_dma_buf	  = mbochs_map_dmabuf,
928a5e6e650SGerd Hoffmann 	.unmap_dma_buf	  = mbochs_unmap_dmabuf,
929a5e6e650SGerd Hoffmann 	.release	  = mbochs_release_dmabuf,
930a5e6e650SGerd Hoffmann 	.mmap		  = mbochs_mmap_dmabuf,
931a5e6e650SGerd Hoffmann };
932a5e6e650SGerd Hoffmann 
mbochs_dmabuf_alloc(struct mdev_state * mdev_state,struct mbochs_mode * mode)933a5e6e650SGerd Hoffmann static struct mbochs_dmabuf *mbochs_dmabuf_alloc(struct mdev_state *mdev_state,
934a5e6e650SGerd Hoffmann 						 struct mbochs_mode *mode)
935a5e6e650SGerd Hoffmann {
936a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf;
937a5e6e650SGerd Hoffmann 	pgoff_t page_offset, pg;
938a5e6e650SGerd Hoffmann 
939a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
940a5e6e650SGerd Hoffmann 
941a5e6e650SGerd Hoffmann 	dmabuf = kzalloc(sizeof(struct mbochs_dmabuf), GFP_KERNEL);
942a5e6e650SGerd Hoffmann 	if (!dmabuf)
943a5e6e650SGerd Hoffmann 		return NULL;
944a5e6e650SGerd Hoffmann 
945a5e6e650SGerd Hoffmann 	dmabuf->mode = *mode;
946a5e6e650SGerd Hoffmann 	dmabuf->id = mdev_state->next_id++;
947a5e6e650SGerd Hoffmann 	dmabuf->pagecount = DIV_ROUND_UP(mode->size, PAGE_SIZE);
948a5e6e650SGerd Hoffmann 	dmabuf->pages = kcalloc(dmabuf->pagecount, sizeof(struct page *),
949a5e6e650SGerd Hoffmann 				GFP_KERNEL);
950a5e6e650SGerd Hoffmann 	if (!dmabuf->pages)
951a5e6e650SGerd Hoffmann 		goto err_free_dmabuf;
952a5e6e650SGerd Hoffmann 
953a5e6e650SGerd Hoffmann 	page_offset = dmabuf->mode.offset >> PAGE_SHIFT;
954a5e6e650SGerd Hoffmann 	for (pg = 0; pg < dmabuf->pagecount; pg++) {
955a5e6e650SGerd Hoffmann 		dmabuf->pages[pg] = __mbochs_get_page(mdev_state,
956a5e6e650SGerd Hoffmann 						      page_offset + pg);
957a5e6e650SGerd Hoffmann 		if (!dmabuf->pages[pg])
958a5e6e650SGerd Hoffmann 			goto err_free_pages;
959a5e6e650SGerd Hoffmann 	}
960a5e6e650SGerd Hoffmann 
961a5e6e650SGerd Hoffmann 	dmabuf->mdev_state = mdev_state;
962a5e6e650SGerd Hoffmann 	list_add(&dmabuf->next, &mdev_state->dmabufs);
963a5e6e650SGerd Hoffmann 
964a5e6e650SGerd Hoffmann 	mbochs_print_dmabuf(dmabuf, __func__);
965a5e6e650SGerd Hoffmann 	return dmabuf;
966a5e6e650SGerd Hoffmann 
967a5e6e650SGerd Hoffmann err_free_pages:
968a5e6e650SGerd Hoffmann 	while (pg > 0)
969a5e6e650SGerd Hoffmann 		put_page(dmabuf->pages[--pg]);
970a5e6e650SGerd Hoffmann 	kfree(dmabuf->pages);
971a5e6e650SGerd Hoffmann err_free_dmabuf:
972a5e6e650SGerd Hoffmann 	kfree(dmabuf);
973a5e6e650SGerd Hoffmann 	return NULL;
974a5e6e650SGerd Hoffmann }
975a5e6e650SGerd Hoffmann 
976a5e6e650SGerd Hoffmann static struct mbochs_dmabuf *
mbochs_dmabuf_find_by_mode(struct mdev_state * mdev_state,struct mbochs_mode * mode)977a5e6e650SGerd Hoffmann mbochs_dmabuf_find_by_mode(struct mdev_state *mdev_state,
978a5e6e650SGerd Hoffmann 			   struct mbochs_mode *mode)
979a5e6e650SGerd Hoffmann {
980a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf;
981a5e6e650SGerd Hoffmann 
982a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
983a5e6e650SGerd Hoffmann 
984a5e6e650SGerd Hoffmann 	list_for_each_entry(dmabuf, &mdev_state->dmabufs, next)
985a5e6e650SGerd Hoffmann 		if (mbochs_modes_equal(&dmabuf->mode, mode))
986a5e6e650SGerd Hoffmann 			return dmabuf;
987a5e6e650SGerd Hoffmann 
988a5e6e650SGerd Hoffmann 	return NULL;
989a5e6e650SGerd Hoffmann }
990a5e6e650SGerd Hoffmann 
991a5e6e650SGerd Hoffmann static struct mbochs_dmabuf *
mbochs_dmabuf_find_by_id(struct mdev_state * mdev_state,u32 id)992a5e6e650SGerd Hoffmann mbochs_dmabuf_find_by_id(struct mdev_state *mdev_state, u32 id)
993a5e6e650SGerd Hoffmann {
994a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf;
995a5e6e650SGerd Hoffmann 
996a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
997a5e6e650SGerd Hoffmann 
998a5e6e650SGerd Hoffmann 	list_for_each_entry(dmabuf, &mdev_state->dmabufs, next)
999a5e6e650SGerd Hoffmann 		if (dmabuf->id == id)
1000a5e6e650SGerd Hoffmann 			return dmabuf;
1001a5e6e650SGerd Hoffmann 
1002a5e6e650SGerd Hoffmann 	return NULL;
1003a5e6e650SGerd Hoffmann }
1004a5e6e650SGerd Hoffmann 
mbochs_dmabuf_export(struct mbochs_dmabuf * dmabuf)1005a5e6e650SGerd Hoffmann static int mbochs_dmabuf_export(struct mbochs_dmabuf *dmabuf)
1006a5e6e650SGerd Hoffmann {
1007a5e6e650SGerd Hoffmann 	struct mdev_state *mdev_state = dmabuf->mdev_state;
1008681c1615SJason Gunthorpe 	struct device *dev = mdev_state->vdev.dev;
1009a5e6e650SGerd Hoffmann 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1010a5e6e650SGerd Hoffmann 	struct dma_buf *buf;
1011a5e6e650SGerd Hoffmann 
1012a5e6e650SGerd Hoffmann 	WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
1013a5e6e650SGerd Hoffmann 
1014a5e6e650SGerd Hoffmann 	if (!IS_ALIGNED(dmabuf->mode.offset, PAGE_SIZE)) {
1015a5e6e650SGerd Hoffmann 		dev_info_ratelimited(dev, "%s: framebuffer not page-aligned\n",
1016a5e6e650SGerd Hoffmann 				     __func__);
1017a5e6e650SGerd Hoffmann 		return -EINVAL;
1018a5e6e650SGerd Hoffmann 	}
1019a5e6e650SGerd Hoffmann 
1020a5e6e650SGerd Hoffmann 	exp_info.ops = &mbochs_dmabuf_ops;
1021a5e6e650SGerd Hoffmann 	exp_info.size = dmabuf->mode.size;
1022a5e6e650SGerd Hoffmann 	exp_info.priv = dmabuf;
1023a5e6e650SGerd Hoffmann 
1024a5e6e650SGerd Hoffmann 	buf = dma_buf_export(&exp_info);
1025a5e6e650SGerd Hoffmann 	if (IS_ERR(buf)) {
1026a5e6e650SGerd Hoffmann 		dev_info_ratelimited(dev, "%s: dma_buf_export failed: %ld\n",
1027a5e6e650SGerd Hoffmann 				     __func__, PTR_ERR(buf));
1028a5e6e650SGerd Hoffmann 		return PTR_ERR(buf);
1029a5e6e650SGerd Hoffmann 	}
1030a5e6e650SGerd Hoffmann 
1031a5e6e650SGerd Hoffmann 	dmabuf->buf = buf;
1032a5e6e650SGerd Hoffmann 	dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
1033a5e6e650SGerd Hoffmann 	return 0;
1034a5e6e650SGerd Hoffmann }
1035a5e6e650SGerd Hoffmann 
mbochs_get_region_info(struct mdev_state * mdev_state,struct vfio_region_info_ext * ext)1036681c1615SJason Gunthorpe static int mbochs_get_region_info(struct mdev_state *mdev_state,
1037104c7405SGerd Hoffmann 				  struct vfio_region_info_ext *ext)
1038a5e6e650SGerd Hoffmann {
1039104c7405SGerd Hoffmann 	struct vfio_region_info *region_info = &ext->base;
1040a5e6e650SGerd Hoffmann 
1041104c7405SGerd Hoffmann 	if (region_info->index >= MBOCHS_NUM_REGIONS)
1042a5e6e650SGerd Hoffmann 		return -EINVAL;
1043a5e6e650SGerd Hoffmann 
1044a5e6e650SGerd Hoffmann 	switch (region_info->index) {
1045a5e6e650SGerd Hoffmann 	case VFIO_PCI_CONFIG_REGION_INDEX:
1046a5e6e650SGerd Hoffmann 		region_info->offset = 0;
1047a5e6e650SGerd Hoffmann 		region_info->size   = MBOCHS_CONFIG_SPACE_SIZE;
1048a5e6e650SGerd Hoffmann 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ |
1049a5e6e650SGerd Hoffmann 				       VFIO_REGION_INFO_FLAG_WRITE);
1050a5e6e650SGerd Hoffmann 		break;
1051a5e6e650SGerd Hoffmann 	case VFIO_PCI_BAR0_REGION_INDEX:
1052a5e6e650SGerd Hoffmann 		region_info->offset = MBOCHS_MEMORY_BAR_OFFSET;
1053a5e6e650SGerd Hoffmann 		region_info->size   = mdev_state->memsize;
1054a5e6e650SGerd Hoffmann 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
1055a5e6e650SGerd Hoffmann 				       VFIO_REGION_INFO_FLAG_WRITE |
1056a5e6e650SGerd Hoffmann 				       VFIO_REGION_INFO_FLAG_MMAP);
1057a5e6e650SGerd Hoffmann 		break;
1058a5e6e650SGerd Hoffmann 	case VFIO_PCI_BAR2_REGION_INDEX:
1059a5e6e650SGerd Hoffmann 		region_info->offset = MBOCHS_MMIO_BAR_OFFSET;
1060a5e6e650SGerd Hoffmann 		region_info->size   = MBOCHS_MMIO_BAR_SIZE;
1061a5e6e650SGerd Hoffmann 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
1062a5e6e650SGerd Hoffmann 				       VFIO_REGION_INFO_FLAG_WRITE);
1063a5e6e650SGerd Hoffmann 		break;
1064104c7405SGerd Hoffmann 	case MBOCHS_EDID_REGION_INDEX:
1065104c7405SGerd Hoffmann 		ext->base.argsz = sizeof(*ext);
1066104c7405SGerd Hoffmann 		ext->base.offset = MBOCHS_EDID_OFFSET;
1067104c7405SGerd Hoffmann 		ext->base.size = MBOCHS_EDID_SIZE;
1068104c7405SGerd Hoffmann 		ext->base.flags = (VFIO_REGION_INFO_FLAG_READ  |
1069104c7405SGerd Hoffmann 				   VFIO_REGION_INFO_FLAG_WRITE |
1070104c7405SGerd Hoffmann 				   VFIO_REGION_INFO_FLAG_CAPS);
1071104c7405SGerd Hoffmann 		ext->base.cap_offset = offsetof(typeof(*ext), type);
1072104c7405SGerd Hoffmann 		ext->type.header.id = VFIO_REGION_INFO_CAP_TYPE;
1073104c7405SGerd Hoffmann 		ext->type.header.version = 1;
1074104c7405SGerd Hoffmann 		ext->type.header.next = 0;
1075104c7405SGerd Hoffmann 		ext->type.type = VFIO_REGION_TYPE_GFX;
1076104c7405SGerd Hoffmann 		ext->type.subtype = VFIO_REGION_SUBTYPE_GFX_EDID;
1077104c7405SGerd Hoffmann 		break;
1078a5e6e650SGerd Hoffmann 	default:
1079a5e6e650SGerd Hoffmann 		region_info->size   = 0;
1080a5e6e650SGerd Hoffmann 		region_info->offset = 0;
1081a5e6e650SGerd Hoffmann 		region_info->flags  = 0;
1082a5e6e650SGerd Hoffmann 	}
1083a5e6e650SGerd Hoffmann 
1084a5e6e650SGerd Hoffmann 	return 0;
1085a5e6e650SGerd Hoffmann }
1086a5e6e650SGerd Hoffmann 
mbochs_get_irq_info(struct vfio_irq_info * irq_info)1087681c1615SJason Gunthorpe static int mbochs_get_irq_info(struct vfio_irq_info *irq_info)
1088a5e6e650SGerd Hoffmann {
1089a5e6e650SGerd Hoffmann 	irq_info->count = 0;
1090a5e6e650SGerd Hoffmann 	return 0;
1091a5e6e650SGerd Hoffmann }
1092a5e6e650SGerd Hoffmann 
mbochs_get_device_info(struct vfio_device_info * dev_info)1093681c1615SJason Gunthorpe static int mbochs_get_device_info(struct vfio_device_info *dev_info)
1094a5e6e650SGerd Hoffmann {
1095a5e6e650SGerd Hoffmann 	dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
1096104c7405SGerd Hoffmann 	dev_info->num_regions = MBOCHS_NUM_REGIONS;
1097a5e6e650SGerd Hoffmann 	dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
1098a5e6e650SGerd Hoffmann 	return 0;
1099a5e6e650SGerd Hoffmann }
1100a5e6e650SGerd Hoffmann 
mbochs_query_gfx_plane(struct mdev_state * mdev_state,struct vfio_device_gfx_plane_info * plane)1101681c1615SJason Gunthorpe static int mbochs_query_gfx_plane(struct mdev_state *mdev_state,
1102a5e6e650SGerd Hoffmann 				  struct vfio_device_gfx_plane_info *plane)
1103a5e6e650SGerd Hoffmann {
1104a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf;
1105a5e6e650SGerd Hoffmann 	struct mbochs_mode mode;
1106a5e6e650SGerd Hoffmann 	int ret;
1107a5e6e650SGerd Hoffmann 
1108a5e6e650SGerd Hoffmann 	if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
1109a5e6e650SGerd Hoffmann 		if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
1110a5e6e650SGerd Hoffmann 				     VFIO_GFX_PLANE_TYPE_DMABUF))
1111a5e6e650SGerd Hoffmann 			return 0;
1112a5e6e650SGerd Hoffmann 		return -EINVAL;
1113a5e6e650SGerd Hoffmann 	}
1114a5e6e650SGerd Hoffmann 
1115a5e6e650SGerd Hoffmann 	if (plane->flags != VFIO_GFX_PLANE_TYPE_DMABUF)
1116a5e6e650SGerd Hoffmann 		return -EINVAL;
1117a5e6e650SGerd Hoffmann 
1118a5e6e650SGerd Hoffmann 	plane->drm_format_mod = 0;
1119a5e6e650SGerd Hoffmann 	plane->x_pos	      = 0;
1120a5e6e650SGerd Hoffmann 	plane->y_pos	      = 0;
1121a5e6e650SGerd Hoffmann 	plane->x_hot	      = 0;
1122a5e6e650SGerd Hoffmann 	plane->y_hot	      = 0;
1123a5e6e650SGerd Hoffmann 
1124a5e6e650SGerd Hoffmann 	mutex_lock(&mdev_state->ops_lock);
1125a5e6e650SGerd Hoffmann 
1126a5e6e650SGerd Hoffmann 	ret = -EINVAL;
1127a5e6e650SGerd Hoffmann 	if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY)
1128a5e6e650SGerd Hoffmann 		ret = mbochs_check_framebuffer(mdev_state, &mode);
1129a5e6e650SGerd Hoffmann 	if (ret < 0) {
1130a5e6e650SGerd Hoffmann 		plane->drm_format     = 0;
1131a5e6e650SGerd Hoffmann 		plane->width	      = 0;
1132a5e6e650SGerd Hoffmann 		plane->height	      = 0;
1133a5e6e650SGerd Hoffmann 		plane->stride	      = 0;
1134a5e6e650SGerd Hoffmann 		plane->size	      = 0;
1135a5e6e650SGerd Hoffmann 		plane->dmabuf_id      = 0;
1136a5e6e650SGerd Hoffmann 		goto done;
1137a5e6e650SGerd Hoffmann 	}
1138a5e6e650SGerd Hoffmann 
1139a5e6e650SGerd Hoffmann 	dmabuf = mbochs_dmabuf_find_by_mode(mdev_state, &mode);
1140a5e6e650SGerd Hoffmann 	if (!dmabuf)
1141a5e6e650SGerd Hoffmann 		mbochs_dmabuf_alloc(mdev_state, &mode);
1142a5e6e650SGerd Hoffmann 	if (!dmabuf) {
1143a5e6e650SGerd Hoffmann 		mutex_unlock(&mdev_state->ops_lock);
1144a5e6e650SGerd Hoffmann 		return -ENOMEM;
1145a5e6e650SGerd Hoffmann 	}
1146a5e6e650SGerd Hoffmann 
1147a5e6e650SGerd Hoffmann 	plane->drm_format     = dmabuf->mode.drm_format;
1148a5e6e650SGerd Hoffmann 	plane->width	      = dmabuf->mode.width;
1149a5e6e650SGerd Hoffmann 	plane->height	      = dmabuf->mode.height;
1150a5e6e650SGerd Hoffmann 	plane->stride	      = dmabuf->mode.stride;
1151a5e6e650SGerd Hoffmann 	plane->size	      = dmabuf->mode.size;
1152a5e6e650SGerd Hoffmann 	plane->dmabuf_id      = dmabuf->id;
1153a5e6e650SGerd Hoffmann 
1154a5e6e650SGerd Hoffmann done:
1155a5e6e650SGerd Hoffmann 	if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY &&
1156a5e6e650SGerd Hoffmann 	    mdev_state->active_id != plane->dmabuf_id) {
1157681c1615SJason Gunthorpe 		dev_dbg(mdev_state->vdev.dev, "%s: primary: %d => %d\n",
1158681c1615SJason Gunthorpe 			__func__, mdev_state->active_id, plane->dmabuf_id);
1159a5e6e650SGerd Hoffmann 		mdev_state->active_id = plane->dmabuf_id;
1160a5e6e650SGerd Hoffmann 	}
1161a5e6e650SGerd Hoffmann 	mutex_unlock(&mdev_state->ops_lock);
1162a5e6e650SGerd Hoffmann 	return 0;
1163a5e6e650SGerd Hoffmann }
1164a5e6e650SGerd Hoffmann 
mbochs_get_gfx_dmabuf(struct mdev_state * mdev_state,u32 id)1165681c1615SJason Gunthorpe static int mbochs_get_gfx_dmabuf(struct mdev_state *mdev_state, u32 id)
1166a5e6e650SGerd Hoffmann {
1167a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf;
1168a5e6e650SGerd Hoffmann 
1169a5e6e650SGerd Hoffmann 	mutex_lock(&mdev_state->ops_lock);
1170a5e6e650SGerd Hoffmann 
1171a5e6e650SGerd Hoffmann 	dmabuf = mbochs_dmabuf_find_by_id(mdev_state, id);
1172a5e6e650SGerd Hoffmann 	if (!dmabuf) {
1173a5e6e650SGerd Hoffmann 		mutex_unlock(&mdev_state->ops_lock);
1174a5e6e650SGerd Hoffmann 		return -ENOENT;
1175a5e6e650SGerd Hoffmann 	}
1176a5e6e650SGerd Hoffmann 
1177a5e6e650SGerd Hoffmann 	if (!dmabuf->buf)
1178a5e6e650SGerd Hoffmann 		mbochs_dmabuf_export(dmabuf);
1179a5e6e650SGerd Hoffmann 
1180a5e6e650SGerd Hoffmann 	mutex_unlock(&mdev_state->ops_lock);
1181a5e6e650SGerd Hoffmann 
1182a5e6e650SGerd Hoffmann 	if (!dmabuf->buf)
1183a5e6e650SGerd Hoffmann 		return -EINVAL;
1184a5e6e650SGerd Hoffmann 
1185a5e6e650SGerd Hoffmann 	return dma_buf_fd(dmabuf->buf, 0);
1186a5e6e650SGerd Hoffmann }
1187a5e6e650SGerd Hoffmann 
mbochs_ioctl(struct vfio_device * vdev,unsigned int cmd,unsigned long arg)1188681c1615SJason Gunthorpe static long mbochs_ioctl(struct vfio_device *vdev, unsigned int cmd,
1189a5e6e650SGerd Hoffmann 			 unsigned long arg)
1190a5e6e650SGerd Hoffmann {
1191681c1615SJason Gunthorpe 	struct mdev_state *mdev_state =
1192681c1615SJason Gunthorpe 		container_of(vdev, struct mdev_state, vdev);
1193a5e6e650SGerd Hoffmann 	int ret = 0;
1194104c7405SGerd Hoffmann 	unsigned long minsz, outsz;
1195a5e6e650SGerd Hoffmann 
1196a5e6e650SGerd Hoffmann 	switch (cmd) {
1197a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_GET_INFO:
1198a5e6e650SGerd Hoffmann 	{
1199a5e6e650SGerd Hoffmann 		struct vfio_device_info info;
1200a5e6e650SGerd Hoffmann 
1201a5e6e650SGerd Hoffmann 		minsz = offsetofend(struct vfio_device_info, num_irqs);
1202a5e6e650SGerd Hoffmann 
1203a5e6e650SGerd Hoffmann 		if (copy_from_user(&info, (void __user *)arg, minsz))
1204a5e6e650SGerd Hoffmann 			return -EFAULT;
1205a5e6e650SGerd Hoffmann 
1206a5e6e650SGerd Hoffmann 		if (info.argsz < minsz)
1207a5e6e650SGerd Hoffmann 			return -EINVAL;
1208a5e6e650SGerd Hoffmann 
1209681c1615SJason Gunthorpe 		ret = mbochs_get_device_info(&info);
1210a5e6e650SGerd Hoffmann 		if (ret)
1211a5e6e650SGerd Hoffmann 			return ret;
1212a5e6e650SGerd Hoffmann 
1213a5e6e650SGerd Hoffmann 		if (copy_to_user((void __user *)arg, &info, minsz))
1214a5e6e650SGerd Hoffmann 			return -EFAULT;
1215a5e6e650SGerd Hoffmann 
1216a5e6e650SGerd Hoffmann 		return 0;
1217a5e6e650SGerd Hoffmann 	}
1218a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_GET_REGION_INFO:
1219a5e6e650SGerd Hoffmann 	{
1220104c7405SGerd Hoffmann 		struct vfio_region_info_ext info;
1221a5e6e650SGerd Hoffmann 
1222104c7405SGerd Hoffmann 		minsz = offsetofend(typeof(info), base.offset);
1223a5e6e650SGerd Hoffmann 
1224a5e6e650SGerd Hoffmann 		if (copy_from_user(&info, (void __user *)arg, minsz))
1225a5e6e650SGerd Hoffmann 			return -EFAULT;
1226a5e6e650SGerd Hoffmann 
1227104c7405SGerd Hoffmann 		outsz = info.base.argsz;
1228104c7405SGerd Hoffmann 		if (outsz < minsz)
1229104c7405SGerd Hoffmann 			return -EINVAL;
1230104c7405SGerd Hoffmann 		if (outsz > sizeof(info))
1231a5e6e650SGerd Hoffmann 			return -EINVAL;
1232a5e6e650SGerd Hoffmann 
1233681c1615SJason Gunthorpe 		ret = mbochs_get_region_info(mdev_state, &info);
1234a5e6e650SGerd Hoffmann 		if (ret)
1235a5e6e650SGerd Hoffmann 			return ret;
1236a5e6e650SGerd Hoffmann 
1237104c7405SGerd Hoffmann 		if (copy_to_user((void __user *)arg, &info, outsz))
1238a5e6e650SGerd Hoffmann 			return -EFAULT;
1239a5e6e650SGerd Hoffmann 
1240a5e6e650SGerd Hoffmann 		return 0;
1241a5e6e650SGerd Hoffmann 	}
1242a5e6e650SGerd Hoffmann 
1243a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_GET_IRQ_INFO:
1244a5e6e650SGerd Hoffmann 	{
1245a5e6e650SGerd Hoffmann 		struct vfio_irq_info info;
1246a5e6e650SGerd Hoffmann 
1247a5e6e650SGerd Hoffmann 		minsz = offsetofend(struct vfio_irq_info, count);
1248a5e6e650SGerd Hoffmann 
1249a5e6e650SGerd Hoffmann 		if (copy_from_user(&info, (void __user *)arg, minsz))
1250a5e6e650SGerd Hoffmann 			return -EFAULT;
1251a5e6e650SGerd Hoffmann 
1252a5e6e650SGerd Hoffmann 		if ((info.argsz < minsz) ||
1253104c7405SGerd Hoffmann 		    (info.index >= VFIO_PCI_NUM_IRQS))
1254a5e6e650SGerd Hoffmann 			return -EINVAL;
1255a5e6e650SGerd Hoffmann 
1256681c1615SJason Gunthorpe 		ret = mbochs_get_irq_info(&info);
1257a5e6e650SGerd Hoffmann 		if (ret)
1258a5e6e650SGerd Hoffmann 			return ret;
1259a5e6e650SGerd Hoffmann 
1260a5e6e650SGerd Hoffmann 		if (copy_to_user((void __user *)arg, &info, minsz))
1261a5e6e650SGerd Hoffmann 			return -EFAULT;
1262a5e6e650SGerd Hoffmann 
1263a5e6e650SGerd Hoffmann 		return 0;
1264a5e6e650SGerd Hoffmann 	}
1265a5e6e650SGerd Hoffmann 
1266a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_QUERY_GFX_PLANE:
1267a5e6e650SGerd Hoffmann 	{
1268a7bea9f4SStefan Hajnoczi 		struct vfio_device_gfx_plane_info plane = {};
1269a5e6e650SGerd Hoffmann 
1270a5e6e650SGerd Hoffmann 		minsz = offsetofend(struct vfio_device_gfx_plane_info,
1271a5e6e650SGerd Hoffmann 				    region_index);
1272a5e6e650SGerd Hoffmann 
1273a5e6e650SGerd Hoffmann 		if (copy_from_user(&plane, (void __user *)arg, minsz))
1274a5e6e650SGerd Hoffmann 			return -EFAULT;
1275a5e6e650SGerd Hoffmann 
1276a5e6e650SGerd Hoffmann 		if (plane.argsz < minsz)
1277a5e6e650SGerd Hoffmann 			return -EINVAL;
1278a5e6e650SGerd Hoffmann 
1279681c1615SJason Gunthorpe 		ret = mbochs_query_gfx_plane(mdev_state, &plane);
1280a5e6e650SGerd Hoffmann 		if (ret)
1281a5e6e650SGerd Hoffmann 			return ret;
1282a5e6e650SGerd Hoffmann 
1283a5e6e650SGerd Hoffmann 		if (copy_to_user((void __user *)arg, &plane, minsz))
1284a5e6e650SGerd Hoffmann 			return -EFAULT;
1285a5e6e650SGerd Hoffmann 
1286a5e6e650SGerd Hoffmann 		return 0;
1287a5e6e650SGerd Hoffmann 	}
1288a5e6e650SGerd Hoffmann 
1289a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_GET_GFX_DMABUF:
1290a5e6e650SGerd Hoffmann 	{
1291a5e6e650SGerd Hoffmann 		u32 dmabuf_id;
1292a5e6e650SGerd Hoffmann 
1293a5e6e650SGerd Hoffmann 		if (get_user(dmabuf_id, (__u32 __user *)arg))
1294a5e6e650SGerd Hoffmann 			return -EFAULT;
1295a5e6e650SGerd Hoffmann 
1296681c1615SJason Gunthorpe 		return mbochs_get_gfx_dmabuf(mdev_state, dmabuf_id);
1297a5e6e650SGerd Hoffmann 	}
1298a5e6e650SGerd Hoffmann 
1299a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_SET_IRQS:
1300a5e6e650SGerd Hoffmann 		return -EINVAL;
1301a5e6e650SGerd Hoffmann 
1302a5e6e650SGerd Hoffmann 	case VFIO_DEVICE_RESET:
1303681c1615SJason Gunthorpe 		return mbochs_reset(mdev_state);
1304a5e6e650SGerd Hoffmann 	}
1305a5e6e650SGerd Hoffmann 	return -ENOTTY;
1306a5e6e650SGerd Hoffmann }
1307a5e6e650SGerd Hoffmann 
mbochs_close_device(struct vfio_device * vdev)13083cb24827SJason Gunthorpe static void mbochs_close_device(struct vfio_device *vdev)
1309a5e6e650SGerd Hoffmann {
1310681c1615SJason Gunthorpe 	struct mdev_state *mdev_state =
1311681c1615SJason Gunthorpe 		container_of(vdev, struct mdev_state, vdev);
1312a5e6e650SGerd Hoffmann 	struct mbochs_dmabuf *dmabuf, *tmp;
1313a5e6e650SGerd Hoffmann 
1314a5e6e650SGerd Hoffmann 	mutex_lock(&mdev_state->ops_lock);
1315a5e6e650SGerd Hoffmann 
1316a5e6e650SGerd Hoffmann 	list_for_each_entry_safe(dmabuf, tmp, &mdev_state->dmabufs, next) {
1317a5e6e650SGerd Hoffmann 		list_del(&dmabuf->next);
1318a5e6e650SGerd Hoffmann 		if (dmabuf->buf) {
1319a5e6e650SGerd Hoffmann 			/* free in mbochs_release_dmabuf() */
1320a5e6e650SGerd Hoffmann 			dmabuf->unlinked = true;
1321a5e6e650SGerd Hoffmann 		} else {
1322a5e6e650SGerd Hoffmann 			kfree(dmabuf);
1323a5e6e650SGerd Hoffmann 		}
1324a5e6e650SGerd Hoffmann 	}
1325a5e6e650SGerd Hoffmann 	mbochs_put_pages(mdev_state);
1326a5e6e650SGerd Hoffmann 
1327a5e6e650SGerd Hoffmann 	mutex_unlock(&mdev_state->ops_lock);
1328a5e6e650SGerd Hoffmann }
1329a5e6e650SGerd Hoffmann 
1330a5e6e650SGerd Hoffmann static ssize_t
memory_show(struct device * dev,struct device_attribute * attr,char * buf)1331a5e6e650SGerd Hoffmann memory_show(struct device *dev, struct device_attribute *attr,
1332a5e6e650SGerd Hoffmann 	    char *buf)
1333a5e6e650SGerd Hoffmann {
1334681c1615SJason Gunthorpe 	struct mdev_state *mdev_state = dev_get_drvdata(dev);
1335a5e6e650SGerd Hoffmann 
1336a5e6e650SGerd Hoffmann 	return sprintf(buf, "%d MB\n", mdev_state->type->mbytes);
1337a5e6e650SGerd Hoffmann }
1338a5e6e650SGerd Hoffmann static DEVICE_ATTR_RO(memory);
1339a5e6e650SGerd Hoffmann 
1340a5e6e650SGerd Hoffmann static struct attribute *mdev_dev_attrs[] = {
1341a5e6e650SGerd Hoffmann 	&dev_attr_memory.attr,
1342a5e6e650SGerd Hoffmann 	NULL,
1343a5e6e650SGerd Hoffmann };
1344a5e6e650SGerd Hoffmann 
1345a5e6e650SGerd Hoffmann static const struct attribute_group mdev_dev_group = {
1346a5e6e650SGerd Hoffmann 	.name  = "vendor",
1347a5e6e650SGerd Hoffmann 	.attrs = mdev_dev_attrs,
1348a5e6e650SGerd Hoffmann };
1349a5e6e650SGerd Hoffmann 
13506cbf507fSJason Gunthorpe static const struct attribute_group *mdev_dev_groups[] = {
1351a5e6e650SGerd Hoffmann 	&mdev_dev_group,
1352a5e6e650SGerd Hoffmann 	NULL,
1353a5e6e650SGerd Hoffmann };
1354a5e6e650SGerd Hoffmann 
mbochs_show_description(struct mdev_type * mtype,char * buf)1355685a1537SChristoph Hellwig static ssize_t mbochs_show_description(struct mdev_type *mtype, char *buf)
1356a5e6e650SGerd Hoffmann {
1357da44c340SChristoph Hellwig 	struct mbochs_type *type =
1358da44c340SChristoph Hellwig 		container_of(mtype, struct mbochs_type, type);
1359a5e6e650SGerd Hoffmann 
1360a5e6e650SGerd Hoffmann 	return sprintf(buf, "virtual display, %d MB video memory\n",
1361a5e6e650SGerd Hoffmann 		       type ? type->mbytes  : 0);
1362a5e6e650SGerd Hoffmann }
1363a5e6e650SGerd Hoffmann 
mbochs_get_available(struct mdev_type * mtype)1364f2fbc72eSChristoph Hellwig static unsigned int mbochs_get_available(struct mdev_type *mtype)
1365a5e6e650SGerd Hoffmann {
1366da44c340SChristoph Hellwig 	struct mbochs_type *type =
1367da44c340SChristoph Hellwig 		container_of(mtype, struct mbochs_type, type);
1368a5e6e650SGerd Hoffmann 
1369f2fbc72eSChristoph Hellwig 	return atomic_read(&mbochs_avail_mbytes) / type->mbytes;
1370a5e6e650SGerd Hoffmann }
1371a5e6e650SGerd Hoffmann 
1372681c1615SJason Gunthorpe static const struct vfio_device_ops mbochs_dev_ops = {
13733cb24827SJason Gunthorpe 	.close_device = mbochs_close_device,
13743d5d18e1SYi Liu 	.init = mbochs_init_dev,
13753d5d18e1SYi Liu 	.release = mbochs_release_dev,
1376a5e6e650SGerd Hoffmann 	.read = mbochs_read,
1377a5e6e650SGerd Hoffmann 	.write = mbochs_write,
1378a5e6e650SGerd Hoffmann 	.ioctl = mbochs_ioctl,
1379a5e6e650SGerd Hoffmann 	.mmap = mbochs_mmap,
13800a782d15SYi Liu 	.bind_iommufd	= vfio_iommufd_emulated_bind,
13810a782d15SYi Liu 	.unbind_iommufd	= vfio_iommufd_emulated_unbind,
13820a782d15SYi Liu 	.attach_ioas	= vfio_iommufd_emulated_attach_ioas,
13838cfa7186SYi Liu 	.detach_ioas	= vfio_iommufd_emulated_detach_ioas,
1384a5e6e650SGerd Hoffmann };
1385a5e6e650SGerd Hoffmann 
1386681c1615SJason Gunthorpe static struct mdev_driver mbochs_driver = {
1387290aac5dSJason Gunthorpe 	.device_api = VFIO_DEVICE_API_PCI_STRING,
1388681c1615SJason Gunthorpe 	.driver = {
1389681c1615SJason Gunthorpe 		.name = "mbochs",
1390681c1615SJason Gunthorpe 		.owner = THIS_MODULE,
1391681c1615SJason Gunthorpe 		.mod_name = KBUILD_MODNAME,
1392681c1615SJason Gunthorpe 		.dev_groups = mdev_dev_groups,
1393681c1615SJason Gunthorpe 	},
1394681c1615SJason Gunthorpe 	.probe = mbochs_probe,
1395681c1615SJason Gunthorpe 	.remove	= mbochs_remove,
1396f2fbc72eSChristoph Hellwig 	.get_available = mbochs_get_available,
1397685a1537SChristoph Hellwig 	.show_description = mbochs_show_description,
1398681c1615SJason Gunthorpe };
1399681c1615SJason Gunthorpe 
1400a5e6e650SGerd Hoffmann static const struct file_operations vd_fops = {
1401a5e6e650SGerd Hoffmann 	.owner		= THIS_MODULE,
1402a5e6e650SGerd Hoffmann };
1403a5e6e650SGerd Hoffmann 
mbochs_device_release(struct device * dev)1404a5e6e650SGerd Hoffmann static void mbochs_device_release(struct device *dev)
1405a5e6e650SGerd Hoffmann {
1406a5e6e650SGerd Hoffmann 	/* nothing */
1407a5e6e650SGerd Hoffmann }
1408a5e6e650SGerd Hoffmann 
mbochs_dev_init(void)1409a5e6e650SGerd Hoffmann static int __init mbochs_dev_init(void)
1410a5e6e650SGerd Hoffmann {
1411a5e6e650SGerd Hoffmann 	int ret = 0;
1412a5e6e650SGerd Hoffmann 
1413de5494afSJason Gunthorpe 	atomic_set(&mbochs_avail_mbytes, max_mbytes);
1414de5494afSJason Gunthorpe 
141516355214SChengguang Xu 	ret = alloc_chrdev_region(&mbochs_devt, 0, MINORMASK + 1, MBOCHS_NAME);
1416a5e6e650SGerd Hoffmann 	if (ret < 0) {
1417a5e6e650SGerd Hoffmann 		pr_err("Error: failed to register mbochs_dev, err: %d\n", ret);
1418a5e6e650SGerd Hoffmann 		return ret;
1419a5e6e650SGerd Hoffmann 	}
1420a5e6e650SGerd Hoffmann 	cdev_init(&mbochs_cdev, &vd_fops);
142116355214SChengguang Xu 	cdev_add(&mbochs_cdev, mbochs_devt, MINORMASK + 1);
1422a5e6e650SGerd Hoffmann 	pr_info("%s: major %d\n", __func__, MAJOR(mbochs_devt));
1423a5e6e650SGerd Hoffmann 
1424681c1615SJason Gunthorpe 	ret = mdev_register_driver(&mbochs_driver);
1425681c1615SJason Gunthorpe 	if (ret)
1426681c1615SJason Gunthorpe 		goto err_cdev;
1427681c1615SJason Gunthorpe 
1428626f534dSRicardo B. Marliere 	ret = class_register(&mbochs_class);
1429626f534dSRicardo B. Marliere 	if (ret)
1430681c1615SJason Gunthorpe 		goto err_driver;
1431626f534dSRicardo B. Marliere 	mbochs_dev.class = &mbochs_class;
1432a5e6e650SGerd Hoffmann 	mbochs_dev.release = mbochs_device_release;
1433a5e6e650SGerd Hoffmann 	dev_set_name(&mbochs_dev, "%s", MBOCHS_NAME);
1434a5e6e650SGerd Hoffmann 
1435a5e6e650SGerd Hoffmann 	ret = device_register(&mbochs_dev);
1436a5e6e650SGerd Hoffmann 	if (ret)
1437ce389573SAlex Williamson 		goto err_put;
1438a5e6e650SGerd Hoffmann 
1439da44c340SChristoph Hellwig 	ret = mdev_register_parent(&mbochs_parent, &mbochs_dev, &mbochs_driver,
1440da44c340SChristoph Hellwig 				   mbochs_mdev_types,
1441da44c340SChristoph Hellwig 				   ARRAY_SIZE(mbochs_mdev_types));
1442a5e6e650SGerd Hoffmann 	if (ret)
1443681c1615SJason Gunthorpe 		goto err_device;
1444a5e6e650SGerd Hoffmann 
1445a5e6e650SGerd Hoffmann 	return 0;
1446a5e6e650SGerd Hoffmann 
1447681c1615SJason Gunthorpe err_device:
1448ce389573SAlex Williamson 	device_del(&mbochs_dev);
1449ce389573SAlex Williamson err_put:
1450ce389573SAlex Williamson 	put_device(&mbochs_dev);
1451626f534dSRicardo B. Marliere 	class_unregister(&mbochs_class);
1452681c1615SJason Gunthorpe err_driver:
1453681c1615SJason Gunthorpe 	mdev_unregister_driver(&mbochs_driver);
1454681c1615SJason Gunthorpe err_cdev:
1455a5e6e650SGerd Hoffmann 	cdev_del(&mbochs_cdev);
145616355214SChengguang Xu 	unregister_chrdev_region(mbochs_devt, MINORMASK + 1);
1457a5e6e650SGerd Hoffmann 	return ret;
1458a5e6e650SGerd Hoffmann }
1459a5e6e650SGerd Hoffmann 
mbochs_dev_exit(void)1460a5e6e650SGerd Hoffmann static void __exit mbochs_dev_exit(void)
1461a5e6e650SGerd Hoffmann {
1462a5e6e650SGerd Hoffmann 	mbochs_dev.bus = NULL;
146389345d51SChristoph Hellwig 	mdev_unregister_parent(&mbochs_parent);
1464a5e6e650SGerd Hoffmann 
1465a5e6e650SGerd Hoffmann 	device_unregister(&mbochs_dev);
1466681c1615SJason Gunthorpe 	mdev_unregister_driver(&mbochs_driver);
1467a5e6e650SGerd Hoffmann 	cdev_del(&mbochs_cdev);
146816355214SChengguang Xu 	unregister_chrdev_region(mbochs_devt, MINORMASK + 1);
1469626f534dSRicardo B. Marliere 	class_unregister(&mbochs_class);
1470a5e6e650SGerd Hoffmann }
1471a5e6e650SGerd Hoffmann 
147216b0314aSGreg Kroah-Hartman MODULE_IMPORT_NS(DMA_BUF);
1473a5e6e650SGerd Hoffmann module_init(mbochs_dev_init)
1474a5e6e650SGerd Hoffmann module_exit(mbochs_dev_exit)
1475