xref: /linux/samples/vfio-mdev/mdpy.c (revision 290aac5df88a83e264b3a73ec146e5e5b3c45793)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Mediated virtual PCI display host device driver
4  *
5  * See mdpy-defs.h for device specs
6  *
7  *   (c) Gerd Hoffmann <kraxel@redhat.com>
8  *
9  * based on mtty driver which is:
10  *   Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
11  *	 Author: Neo Jia <cjia@nvidia.com>
12  *		 Kirti Wankhede <kwankhede@nvidia.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 #include <linux/cdev.h>
24 #include <linux/vfio.h>
25 #include <linux/iommu.h>
26 #include <linux/sysfs.h>
27 #include <linux/mdev.h>
28 #include <linux/pci.h>
29 #include <drm/drm_fourcc.h>
30 #include "mdpy-defs.h"
31 
32 #define MDPY_NAME		"mdpy"
33 #define MDPY_CLASS_NAME		"mdpy"
34 
35 #define MDPY_CONFIG_SPACE_SIZE	0xff
36 #define MDPY_MEMORY_BAR_OFFSET	PAGE_SIZE
37 #define MDPY_DISPLAY_REGION	16
38 
39 #define STORE_LE16(addr, val)	(*(u16 *)addr = val)
40 #define STORE_LE32(addr, val)	(*(u32 *)addr = val)
41 
42 
43 MODULE_LICENSE("GPL v2");
44 
45 static int max_devices = 4;
46 module_param_named(count, max_devices, int, 0444);
47 MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
48 
49 
50 #define MDPY_TYPE_1 "vga"
51 #define MDPY_TYPE_2 "xga"
52 #define MDPY_TYPE_3 "hd"
53 
54 static struct mdpy_type {
55 	struct mdev_type type;
56 	const char *name;
57 	u32 format;
58 	u32 bytepp;
59 	u32 width;
60 	u32 height;
61 } mdpy_types[] = {
62 	{
63 		.type.sysfs_name 	= MDPY_TYPE_1,
64 		.name	= MDPY_CLASS_NAME "-" MDPY_TYPE_1,
65 		.format = DRM_FORMAT_XRGB8888,
66 		.bytepp = 4,
67 		.width	= 640,
68 		.height = 480,
69 	}, {
70 		.type.sysfs_name 	= MDPY_TYPE_2,
71 		.name	= MDPY_CLASS_NAME "-" MDPY_TYPE_2,
72 		.format = DRM_FORMAT_XRGB8888,
73 		.bytepp = 4,
74 		.width	= 1024,
75 		.height = 768,
76 	}, {
77 		.type.sysfs_name 	= MDPY_TYPE_3,
78 		.name	= MDPY_CLASS_NAME "-" MDPY_TYPE_3,
79 		.format = DRM_FORMAT_XRGB8888,
80 		.bytepp = 4,
81 		.width	= 1920,
82 		.height = 1080,
83 	},
84 };
85 
86 static struct mdev_type *mdpy_mdev_types[] = {
87 	&mdpy_types[0].type,
88 	&mdpy_types[1].type,
89 	&mdpy_types[2].type,
90 };
91 
92 static dev_t		mdpy_devt;
93 static struct class	*mdpy_class;
94 static struct cdev	mdpy_cdev;
95 static struct device	mdpy_dev;
96 static struct mdev_parent mdpy_parent;
97 static u32		mdpy_count;
98 static const struct vfio_device_ops mdpy_dev_ops;
99 
100 /* State of each mdev device */
101 struct mdev_state {
102 	struct vfio_device vdev;
103 	u8 *vconfig;
104 	u32 bar_mask;
105 	struct mutex ops_lock;
106 	struct mdev_device *mdev;
107 	struct vfio_device_info dev_info;
108 
109 	const struct mdpy_type *type;
110 	u32 memsize;
111 	void *memblk;
112 };
113 
114 static void mdpy_create_config_space(struct mdev_state *mdev_state)
115 {
116 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
117 		   MDPY_PCI_VENDOR_ID);
118 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
119 		   MDPY_PCI_DEVICE_ID);
120 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
121 		   MDPY_PCI_SUBVENDOR_ID);
122 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
123 		   MDPY_PCI_SUBDEVICE_ID);
124 
125 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
126 		   PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
127 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_STATUS],
128 		   PCI_STATUS_CAP_LIST);
129 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
130 		   PCI_CLASS_DISPLAY_OTHER);
131 	mdev_state->vconfig[PCI_CLASS_REVISION] =  0x01;
132 
133 	STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
134 		   PCI_BASE_ADDRESS_SPACE_MEMORY |
135 		   PCI_BASE_ADDRESS_MEM_TYPE_32	 |
136 		   PCI_BASE_ADDRESS_MEM_PREFETCH);
137 	mdev_state->bar_mask = ~(mdev_state->memsize) + 1;
138 
139 	/* vendor specific capability for the config registers */
140 	mdev_state->vconfig[PCI_CAPABILITY_LIST]       = MDPY_VENDORCAP_OFFSET;
141 	mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 0] = 0x09; /* vendor cap */
142 	mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 1] = 0x00; /* next ptr */
143 	mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 2] = MDPY_VENDORCAP_SIZE;
144 	STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_FORMAT_OFFSET],
145 		   mdev_state->type->format);
146 	STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_WIDTH_OFFSET],
147 		   mdev_state->type->width);
148 	STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_HEIGHT_OFFSET],
149 		   mdev_state->type->height);
150 }
151 
152 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
153 				 char *buf, u32 count)
154 {
155 	struct device *dev = mdev_dev(mdev_state->mdev);
156 	u32 cfg_addr;
157 
158 	switch (offset) {
159 	case PCI_BASE_ADDRESS_0:
160 		cfg_addr = *(u32 *)buf;
161 
162 		if (cfg_addr == 0xffffffff) {
163 			cfg_addr = (cfg_addr & mdev_state->bar_mask);
164 		} else {
165 			cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
166 			if (cfg_addr)
167 				dev_info(dev, "BAR0 @ 0x%x\n", cfg_addr);
168 		}
169 
170 		cfg_addr |= (mdev_state->vconfig[offset] &
171 			     ~PCI_BASE_ADDRESS_MEM_MASK);
172 		STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
173 		break;
174 	}
175 }
176 
177 static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
178 			   size_t count, loff_t pos, bool is_write)
179 {
180 	int ret = 0;
181 
182 	mutex_lock(&mdev_state->ops_lock);
183 
184 	if (pos < MDPY_CONFIG_SPACE_SIZE) {
185 		if (is_write)
186 			handle_pci_cfg_write(mdev_state, pos, buf, count);
187 		else
188 			memcpy(buf, (mdev_state->vconfig + pos), count);
189 
190 	} else if ((pos >= MDPY_MEMORY_BAR_OFFSET) &&
191 		   (pos + count <=
192 		    MDPY_MEMORY_BAR_OFFSET + mdev_state->memsize)) {
193 		pos -= MDPY_MEMORY_BAR_OFFSET;
194 		if (is_write)
195 			memcpy(mdev_state->memblk, buf, count);
196 		else
197 			memcpy(buf, mdev_state->memblk, count);
198 
199 	} else {
200 		dev_info(mdev_state->vdev.dev,
201 			 "%s: %s @0x%llx (unhandled)\n", __func__,
202 			 is_write ? "WR" : "RD", pos);
203 		ret = -1;
204 		goto accessfailed;
205 	}
206 
207 	ret = count;
208 
209 
210 accessfailed:
211 	mutex_unlock(&mdev_state->ops_lock);
212 
213 	return ret;
214 }
215 
216 static int mdpy_reset(struct mdev_state *mdev_state)
217 {
218 	u32 stride, i;
219 
220 	/* initialize with gray gradient */
221 	stride = mdev_state->type->width * mdev_state->type->bytepp;
222 	for (i = 0; i < mdev_state->type->height; i++)
223 		memset(mdev_state->memblk + i * stride,
224 		       i * 255 / mdev_state->type->height,
225 		       stride);
226 	return 0;
227 }
228 
229 static int mdpy_init_dev(struct vfio_device *vdev)
230 {
231 	struct mdev_state *mdev_state =
232 		container_of(vdev, struct mdev_state, vdev);
233 	struct mdev_device *mdev = to_mdev_device(vdev->dev);
234 	const struct mdpy_type *type =
235 		container_of(mdev->type, struct mdpy_type, type);
236 	u32 fbsize;
237 	int ret = -ENOMEM;
238 
239 	if (mdpy_count >= max_devices)
240 		return ret;
241 
242 	mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
243 	if (!mdev_state->vconfig)
244 		return ret;
245 
246 	fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
247 
248 	mdev_state->memblk = vmalloc_user(fbsize);
249 	if (!mdev_state->memblk)
250 		goto out_vconfig;
251 
252 	mutex_init(&mdev_state->ops_lock);
253 	mdev_state->mdev = mdev;
254 	mdev_state->type = type;
255 	mdev_state->memsize = fbsize;
256 	mdpy_create_config_space(mdev_state);
257 	mdpy_reset(mdev_state);
258 
259 	dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
260 		 type->height);
261 
262 	mdpy_count++;
263 	return 0;
264 
265 out_vconfig:
266 	kfree(mdev_state->vconfig);
267 	return ret;
268 }
269 
270 static int mdpy_probe(struct mdev_device *mdev)
271 {
272 	struct mdev_state *mdev_state;
273 	int ret;
274 
275 	mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
276 				       &mdpy_dev_ops);
277 	if (IS_ERR(mdev_state))
278 		return PTR_ERR(mdev_state);
279 
280 	ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
281 	if (ret)
282 		goto err_put_vdev;
283 	dev_set_drvdata(&mdev->dev, mdev_state);
284 	return 0;
285 
286 err_put_vdev:
287 	vfio_put_device(&mdev_state->vdev);
288 	return ret;
289 }
290 
291 static void mdpy_release_dev(struct vfio_device *vdev)
292 {
293 	struct mdev_state *mdev_state =
294 		container_of(vdev, struct mdev_state, vdev);
295 
296 	mdpy_count--;
297 	vfree(mdev_state->memblk);
298 	kfree(mdev_state->vconfig);
299 	vfio_free_device(vdev);
300 }
301 
302 static void mdpy_remove(struct mdev_device *mdev)
303 {
304 	struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
305 
306 	dev_info(&mdev->dev, "%s\n", __func__);
307 
308 	vfio_unregister_group_dev(&mdev_state->vdev);
309 	vfio_put_device(&mdev_state->vdev);
310 }
311 
312 static ssize_t mdpy_read(struct vfio_device *vdev, char __user *buf,
313 			 size_t count, loff_t *ppos)
314 {
315 	struct mdev_state *mdev_state =
316 		container_of(vdev, struct mdev_state, vdev);
317 	unsigned int done = 0;
318 	int ret;
319 
320 	while (count) {
321 		size_t filled;
322 
323 		if (count >= 4 && !(*ppos % 4)) {
324 			u32 val;
325 
326 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
327 					  *ppos, false);
328 			if (ret <= 0)
329 				goto read_err;
330 
331 			if (copy_to_user(buf, &val, sizeof(val)))
332 				goto read_err;
333 
334 			filled = 4;
335 		} else if (count >= 2 && !(*ppos % 2)) {
336 			u16 val;
337 
338 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
339 					  *ppos, false);
340 			if (ret <= 0)
341 				goto read_err;
342 
343 			if (copy_to_user(buf, &val, sizeof(val)))
344 				goto read_err;
345 
346 			filled = 2;
347 		} else {
348 			u8 val;
349 
350 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
351 					  *ppos, false);
352 			if (ret <= 0)
353 				goto read_err;
354 
355 			if (copy_to_user(buf, &val, sizeof(val)))
356 				goto read_err;
357 
358 			filled = 1;
359 		}
360 
361 		count -= filled;
362 		done += filled;
363 		*ppos += filled;
364 		buf += filled;
365 	}
366 
367 	return done;
368 
369 read_err:
370 	return -EFAULT;
371 }
372 
373 static ssize_t mdpy_write(struct vfio_device *vdev, const char __user *buf,
374 			  size_t count, loff_t *ppos)
375 {
376 	struct mdev_state *mdev_state =
377 		container_of(vdev, struct mdev_state, vdev);
378 	unsigned int done = 0;
379 	int ret;
380 
381 	while (count) {
382 		size_t filled;
383 
384 		if (count >= 4 && !(*ppos % 4)) {
385 			u32 val;
386 
387 			if (copy_from_user(&val, buf, sizeof(val)))
388 				goto write_err;
389 
390 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
391 					  *ppos, true);
392 			if (ret <= 0)
393 				goto write_err;
394 
395 			filled = 4;
396 		} else if (count >= 2 && !(*ppos % 2)) {
397 			u16 val;
398 
399 			if (copy_from_user(&val, buf, sizeof(val)))
400 				goto write_err;
401 
402 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
403 					  *ppos, true);
404 			if (ret <= 0)
405 				goto write_err;
406 
407 			filled = 2;
408 		} else {
409 			u8 val;
410 
411 			if (copy_from_user(&val, buf, sizeof(val)))
412 				goto write_err;
413 
414 			ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
415 					  *ppos, true);
416 			if (ret <= 0)
417 				goto write_err;
418 
419 			filled = 1;
420 		}
421 		count -= filled;
422 		done += filled;
423 		*ppos += filled;
424 		buf += filled;
425 	}
426 
427 	return done;
428 write_err:
429 	return -EFAULT;
430 }
431 
432 static int mdpy_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
433 {
434 	struct mdev_state *mdev_state =
435 		container_of(vdev, struct mdev_state, vdev);
436 
437 	if (vma->vm_pgoff != MDPY_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
438 		return -EINVAL;
439 	if (vma->vm_end < vma->vm_start)
440 		return -EINVAL;
441 	if (vma->vm_end - vma->vm_start > mdev_state->memsize)
442 		return -EINVAL;
443 	if ((vma->vm_flags & VM_SHARED) == 0)
444 		return -EINVAL;
445 
446 	return remap_vmalloc_range(vma, mdev_state->memblk, 0);
447 }
448 
449 static int mdpy_get_region_info(struct mdev_state *mdev_state,
450 				struct vfio_region_info *region_info,
451 				u16 *cap_type_id, void **cap_type)
452 {
453 	if (region_info->index >= VFIO_PCI_NUM_REGIONS &&
454 	    region_info->index != MDPY_DISPLAY_REGION)
455 		return -EINVAL;
456 
457 	switch (region_info->index) {
458 	case VFIO_PCI_CONFIG_REGION_INDEX:
459 		region_info->offset = 0;
460 		region_info->size   = MDPY_CONFIG_SPACE_SIZE;
461 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ |
462 				       VFIO_REGION_INFO_FLAG_WRITE);
463 		break;
464 	case VFIO_PCI_BAR0_REGION_INDEX:
465 	case MDPY_DISPLAY_REGION:
466 		region_info->offset = MDPY_MEMORY_BAR_OFFSET;
467 		region_info->size   = mdev_state->memsize;
468 		region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
469 				       VFIO_REGION_INFO_FLAG_WRITE |
470 				       VFIO_REGION_INFO_FLAG_MMAP);
471 		break;
472 	default:
473 		region_info->size   = 0;
474 		region_info->offset = 0;
475 		region_info->flags  = 0;
476 	}
477 
478 	return 0;
479 }
480 
481 static int mdpy_get_irq_info(struct vfio_irq_info *irq_info)
482 {
483 	irq_info->count = 0;
484 	return 0;
485 }
486 
487 static int mdpy_get_device_info(struct vfio_device_info *dev_info)
488 {
489 	dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
490 	dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
491 	dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
492 	return 0;
493 }
494 
495 static int mdpy_query_gfx_plane(struct mdev_state *mdev_state,
496 				struct vfio_device_gfx_plane_info *plane)
497 {
498 	if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
499 		if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
500 				     VFIO_GFX_PLANE_TYPE_REGION))
501 			return 0;
502 		return -EINVAL;
503 	}
504 
505 	if (plane->flags != VFIO_GFX_PLANE_TYPE_REGION)
506 		return -EINVAL;
507 
508 	plane->drm_format     = mdev_state->type->format;
509 	plane->width	      = mdev_state->type->width;
510 	plane->height	      = mdev_state->type->height;
511 	plane->stride	      = (mdev_state->type->width *
512 				 mdev_state->type->bytepp);
513 	plane->size	      = mdev_state->memsize;
514 	plane->region_index   = MDPY_DISPLAY_REGION;
515 
516 	/* unused */
517 	plane->drm_format_mod = 0;
518 	plane->x_pos	      = 0;
519 	plane->y_pos	      = 0;
520 	plane->x_hot	      = 0;
521 	plane->y_hot	      = 0;
522 
523 	return 0;
524 }
525 
526 static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd,
527 		       unsigned long arg)
528 {
529 	int ret = 0;
530 	unsigned long minsz;
531 	struct mdev_state *mdev_state =
532 		container_of(vdev, struct mdev_state, vdev);
533 
534 	switch (cmd) {
535 	case VFIO_DEVICE_GET_INFO:
536 	{
537 		struct vfio_device_info info;
538 
539 		minsz = offsetofend(struct vfio_device_info, num_irqs);
540 
541 		if (copy_from_user(&info, (void __user *)arg, minsz))
542 			return -EFAULT;
543 
544 		if (info.argsz < minsz)
545 			return -EINVAL;
546 
547 		ret = mdpy_get_device_info(&info);
548 		if (ret)
549 			return ret;
550 
551 		memcpy(&mdev_state->dev_info, &info, sizeof(info));
552 
553 		if (copy_to_user((void __user *)arg, &info, minsz))
554 			return -EFAULT;
555 
556 		return 0;
557 	}
558 	case VFIO_DEVICE_GET_REGION_INFO:
559 	{
560 		struct vfio_region_info info;
561 		u16 cap_type_id = 0;
562 		void *cap_type = NULL;
563 
564 		minsz = offsetofend(struct vfio_region_info, offset);
565 
566 		if (copy_from_user(&info, (void __user *)arg, minsz))
567 			return -EFAULT;
568 
569 		if (info.argsz < minsz)
570 			return -EINVAL;
571 
572 		ret = mdpy_get_region_info(mdev_state, &info, &cap_type_id,
573 					   &cap_type);
574 		if (ret)
575 			return ret;
576 
577 		if (copy_to_user((void __user *)arg, &info, minsz))
578 			return -EFAULT;
579 
580 		return 0;
581 	}
582 
583 	case VFIO_DEVICE_GET_IRQ_INFO:
584 	{
585 		struct vfio_irq_info info;
586 
587 		minsz = offsetofend(struct vfio_irq_info, count);
588 
589 		if (copy_from_user(&info, (void __user *)arg, minsz))
590 			return -EFAULT;
591 
592 		if ((info.argsz < minsz) ||
593 		    (info.index >= mdev_state->dev_info.num_irqs))
594 			return -EINVAL;
595 
596 		ret = mdpy_get_irq_info(&info);
597 		if (ret)
598 			return ret;
599 
600 		if (copy_to_user((void __user *)arg, &info, minsz))
601 			return -EFAULT;
602 
603 		return 0;
604 	}
605 
606 	case VFIO_DEVICE_QUERY_GFX_PLANE:
607 	{
608 		struct vfio_device_gfx_plane_info plane;
609 
610 		minsz = offsetofend(struct vfio_device_gfx_plane_info,
611 				    region_index);
612 
613 		if (copy_from_user(&plane, (void __user *)arg, minsz))
614 			return -EFAULT;
615 
616 		if (plane.argsz < minsz)
617 			return -EINVAL;
618 
619 		ret = mdpy_query_gfx_plane(mdev_state, &plane);
620 		if (ret)
621 			return ret;
622 
623 		if (copy_to_user((void __user *)arg, &plane, minsz))
624 			return -EFAULT;
625 
626 		return 0;
627 	}
628 
629 	case VFIO_DEVICE_SET_IRQS:
630 		return -EINVAL;
631 
632 	case VFIO_DEVICE_RESET:
633 		return mdpy_reset(mdev_state);
634 	}
635 	return -ENOTTY;
636 }
637 
638 static ssize_t
639 resolution_show(struct device *dev, struct device_attribute *attr,
640 		char *buf)
641 {
642 	struct mdev_state *mdev_state = dev_get_drvdata(dev);
643 
644 	return sprintf(buf, "%dx%d\n",
645 		       mdev_state->type->width,
646 		       mdev_state->type->height);
647 }
648 static DEVICE_ATTR_RO(resolution);
649 
650 static struct attribute *mdev_dev_attrs[] = {
651 	&dev_attr_resolution.attr,
652 	NULL,
653 };
654 
655 static const struct attribute_group mdev_dev_group = {
656 	.name  = "vendor",
657 	.attrs = mdev_dev_attrs,
658 };
659 
660 static const struct attribute_group *mdev_dev_groups[] = {
661 	&mdev_dev_group,
662 	NULL,
663 };
664 
665 static ssize_t name_show(struct mdev_type *mtype,
666 			 struct mdev_type_attribute *attr, char *buf)
667 {
668 	struct mdpy_type *type = container_of(mtype, struct mdpy_type, type);
669 
670 	return sprintf(buf, "%s\n", type->name);
671 }
672 static MDEV_TYPE_ATTR_RO(name);
673 
674 static ssize_t description_show(struct mdev_type *mtype,
675 				struct mdev_type_attribute *attr, char *buf)
676 {
677 	struct mdpy_type *type = container_of(mtype, struct mdpy_type, type);
678 
679 	return sprintf(buf, "virtual display, %dx%d framebuffer\n",
680 		       type->width, type->height);
681 }
682 static MDEV_TYPE_ATTR_RO(description);
683 
684 static ssize_t available_instances_show(struct mdev_type *mtype,
685 					struct mdev_type_attribute *attr,
686 					char *buf)
687 {
688 	return sprintf(buf, "%d\n", max_devices - mdpy_count);
689 }
690 static MDEV_TYPE_ATTR_RO(available_instances);
691 
692 static const struct attribute *mdev_types_attrs[] = {
693 	&mdev_type_attr_name.attr,
694 	&mdev_type_attr_description.attr,
695 	&mdev_type_attr_available_instances.attr,
696 	NULL,
697 };
698 
699 static const struct vfio_device_ops mdpy_dev_ops = {
700 	.init = mdpy_init_dev,
701 	.release = mdpy_release_dev,
702 	.read = mdpy_read,
703 	.write = mdpy_write,
704 	.ioctl = mdpy_ioctl,
705 	.mmap = mdpy_mmap,
706 };
707 
708 static struct mdev_driver mdpy_driver = {
709 	.device_api = VFIO_DEVICE_API_PCI_STRING,
710 	.driver = {
711 		.name = "mdpy",
712 		.owner = THIS_MODULE,
713 		.mod_name = KBUILD_MODNAME,
714 		.dev_groups = mdev_dev_groups,
715 	},
716 	.probe = mdpy_probe,
717 	.remove	= mdpy_remove,
718 	.types_attrs = mdev_types_attrs,
719 };
720 
721 static const struct file_operations vd_fops = {
722 	.owner		= THIS_MODULE,
723 };
724 
725 static void mdpy_device_release(struct device *dev)
726 {
727 	/* nothing */
728 }
729 
730 static int __init mdpy_dev_init(void)
731 {
732 	int ret = 0;
733 
734 	ret = alloc_chrdev_region(&mdpy_devt, 0, MINORMASK + 1, MDPY_NAME);
735 	if (ret < 0) {
736 		pr_err("Error: failed to register mdpy_dev, err: %d\n", ret);
737 		return ret;
738 	}
739 	cdev_init(&mdpy_cdev, &vd_fops);
740 	cdev_add(&mdpy_cdev, mdpy_devt, MINORMASK + 1);
741 	pr_info("%s: major %d\n", __func__, MAJOR(mdpy_devt));
742 
743 	ret = mdev_register_driver(&mdpy_driver);
744 	if (ret)
745 		goto err_cdev;
746 
747 	mdpy_class = class_create(THIS_MODULE, MDPY_CLASS_NAME);
748 	if (IS_ERR(mdpy_class)) {
749 		pr_err("Error: failed to register mdpy_dev class\n");
750 		ret = PTR_ERR(mdpy_class);
751 		goto err_driver;
752 	}
753 	mdpy_dev.class = mdpy_class;
754 	mdpy_dev.release = mdpy_device_release;
755 	dev_set_name(&mdpy_dev, "%s", MDPY_NAME);
756 
757 	ret = device_register(&mdpy_dev);
758 	if (ret)
759 		goto err_class;
760 
761 	ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
762 				   mdpy_mdev_types,
763 				   ARRAY_SIZE(mdpy_mdev_types));
764 	if (ret)
765 		goto err_device;
766 
767 	return 0;
768 
769 err_device:
770 	device_unregister(&mdpy_dev);
771 err_class:
772 	class_destroy(mdpy_class);
773 err_driver:
774 	mdev_unregister_driver(&mdpy_driver);
775 err_cdev:
776 	cdev_del(&mdpy_cdev);
777 	unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
778 	return ret;
779 }
780 
781 static void __exit mdpy_dev_exit(void)
782 {
783 	mdpy_dev.bus = NULL;
784 	mdev_unregister_parent(&mdpy_parent);
785 
786 	device_unregister(&mdpy_dev);
787 	mdev_unregister_driver(&mdpy_driver);
788 	cdev_del(&mdpy_cdev);
789 	unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
790 	class_destroy(mdpy_class);
791 	mdpy_class = NULL;
792 }
793 
794 module_init(mdpy_dev_init)
795 module_exit(mdpy_dev_exit)
796