1 // SPDX-License-Identifier: GPL-2.0
3 * Mediated virtual PCI display host device driver
5 * See mdpy-defs.h for device specs
7 * (c) Gerd Hoffmann <kraxel@redhat.com>
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>
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.
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"
32 #define MDPY_NAME "mdpy"
33 #define MDPY_CLASS_NAME "mdpy"
35 #define MDPY_CONFIG_SPACE_SIZE 0xff
36 #define MDPY_MEMORY_BAR_OFFSET PAGE_SIZE
37 #define MDPY_DISPLAY_REGION 16
39 #define STORE_LE16(addr, val) (*(u16 *)addr = val)
40 #define STORE_LE32(addr, val) (*(u32 *)addr = val)
43 MODULE_LICENSE("GPL v2");
45 #define MDPY_TYPE_1 "vga"
46 #define MDPY_TYPE_2 "xga"
47 #define MDPY_TYPE_3 "hd"
49 static struct mdpy_type {
50 struct mdev_type type;
57 .type.sysfs_name = MDPY_TYPE_1,
58 .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_1,
59 .format = DRM_FORMAT_XRGB8888,
64 .type.sysfs_name = MDPY_TYPE_2,
65 .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_2,
66 .format = DRM_FORMAT_XRGB8888,
71 .type.sysfs_name = MDPY_TYPE_3,
72 .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_3,
73 .format = DRM_FORMAT_XRGB8888,
80 static struct mdev_type *mdpy_mdev_types[] = {
86 static dev_t mdpy_devt;
87 static struct class *mdpy_class;
88 static struct cdev mdpy_cdev;
89 static struct device mdpy_dev;
90 static struct mdev_parent mdpy_parent;
91 static const struct vfio_device_ops mdpy_dev_ops;
93 /* State of each mdev device */
95 struct vfio_device vdev;
98 struct mutex ops_lock;
99 struct mdev_device *mdev;
100 struct vfio_device_info dev_info;
102 const struct mdpy_type *type;
107 static void mdpy_create_config_space(struct mdev_state *mdev_state)
109 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
111 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
113 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
114 MDPY_PCI_SUBVENDOR_ID);
115 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
116 MDPY_PCI_SUBDEVICE_ID);
118 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
119 PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
120 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_STATUS],
121 PCI_STATUS_CAP_LIST);
122 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
123 PCI_CLASS_DISPLAY_OTHER);
124 mdev_state->vconfig[PCI_CLASS_REVISION] = 0x01;
126 STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
127 PCI_BASE_ADDRESS_SPACE_MEMORY |
128 PCI_BASE_ADDRESS_MEM_TYPE_32 |
129 PCI_BASE_ADDRESS_MEM_PREFETCH);
130 mdev_state->bar_mask = ~(mdev_state->memsize) + 1;
132 /* vendor specific capability for the config registers */
133 mdev_state->vconfig[PCI_CAPABILITY_LIST] = MDPY_VENDORCAP_OFFSET;
134 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 0] = 0x09; /* vendor cap */
135 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 1] = 0x00; /* next ptr */
136 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 2] = MDPY_VENDORCAP_SIZE;
137 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_FORMAT_OFFSET],
138 mdev_state->type->format);
139 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_WIDTH_OFFSET],
140 mdev_state->type->width);
141 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_HEIGHT_OFFSET],
142 mdev_state->type->height);
145 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
146 char *buf, u32 count)
148 struct device *dev = mdev_dev(mdev_state->mdev);
152 case PCI_BASE_ADDRESS_0:
153 cfg_addr = *(u32 *)buf;
155 if (cfg_addr == 0xffffffff) {
156 cfg_addr = (cfg_addr & mdev_state->bar_mask);
158 cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
160 dev_info(dev, "BAR0 @ 0x%x\n", cfg_addr);
163 cfg_addr |= (mdev_state->vconfig[offset] &
164 ~PCI_BASE_ADDRESS_MEM_MASK);
165 STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
170 static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
171 size_t count, loff_t pos, bool is_write)
175 mutex_lock(&mdev_state->ops_lock);
177 if (pos < MDPY_CONFIG_SPACE_SIZE) {
179 handle_pci_cfg_write(mdev_state, pos, buf, count);
181 memcpy(buf, (mdev_state->vconfig + pos), count);
183 } else if ((pos >= MDPY_MEMORY_BAR_OFFSET) &&
185 MDPY_MEMORY_BAR_OFFSET + mdev_state->memsize)) {
186 pos -= MDPY_MEMORY_BAR_OFFSET;
188 memcpy(mdev_state->memblk, buf, count);
190 memcpy(buf, mdev_state->memblk, count);
193 dev_info(mdev_state->vdev.dev,
194 "%s: %s @0x%llx (unhandled)\n", __func__,
195 is_write ? "WR" : "RD", pos);
204 mutex_unlock(&mdev_state->ops_lock);
209 static int mdpy_reset(struct mdev_state *mdev_state)
213 /* initialize with gray gradient */
214 stride = mdev_state->type->width * mdev_state->type->bytepp;
215 for (i = 0; i < mdev_state->type->height; i++)
216 memset(mdev_state->memblk + i * stride,
217 i * 255 / mdev_state->type->height,
222 static int mdpy_init_dev(struct vfio_device *vdev)
224 struct mdev_state *mdev_state =
225 container_of(vdev, struct mdev_state, vdev);
226 struct mdev_device *mdev = to_mdev_device(vdev->dev);
227 const struct mdpy_type *type =
228 container_of(mdev->type, struct mdpy_type, type);
232 mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
233 if (!mdev_state->vconfig)
236 fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
238 mdev_state->memblk = vmalloc_user(fbsize);
239 if (!mdev_state->memblk)
242 mutex_init(&mdev_state->ops_lock);
243 mdev_state->mdev = mdev;
244 mdev_state->type = type;
245 mdev_state->memsize = fbsize;
246 mdpy_create_config_space(mdev_state);
247 mdpy_reset(mdev_state);
249 dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->type.pretty_name,
250 type->width, type->height);
254 kfree(mdev_state->vconfig);
258 static int mdpy_probe(struct mdev_device *mdev)
260 struct mdev_state *mdev_state;
263 mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
265 if (IS_ERR(mdev_state))
266 return PTR_ERR(mdev_state);
268 ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
271 dev_set_drvdata(&mdev->dev, mdev_state);
275 vfio_put_device(&mdev_state->vdev);
279 static void mdpy_release_dev(struct vfio_device *vdev)
281 struct mdev_state *mdev_state =
282 container_of(vdev, struct mdev_state, vdev);
284 vfree(mdev_state->memblk);
285 kfree(mdev_state->vconfig);
288 static void mdpy_remove(struct mdev_device *mdev)
290 struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
292 dev_info(&mdev->dev, "%s\n", __func__);
294 vfio_unregister_group_dev(&mdev_state->vdev);
295 vfio_put_device(&mdev_state->vdev);
298 static ssize_t mdpy_read(struct vfio_device *vdev, char __user *buf,
299 size_t count, loff_t *ppos)
301 struct mdev_state *mdev_state =
302 container_of(vdev, struct mdev_state, vdev);
303 unsigned int done = 0;
309 if (count >= 4 && !(*ppos % 4)) {
312 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
317 if (copy_to_user(buf, &val, sizeof(val)))
321 } else if (count >= 2 && !(*ppos % 2)) {
324 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
329 if (copy_to_user(buf, &val, sizeof(val)))
336 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
341 if (copy_to_user(buf, &val, sizeof(val)))
359 static ssize_t mdpy_write(struct vfio_device *vdev, const char __user *buf,
360 size_t count, loff_t *ppos)
362 struct mdev_state *mdev_state =
363 container_of(vdev, struct mdev_state, vdev);
364 unsigned int done = 0;
370 if (count >= 4 && !(*ppos % 4)) {
373 if (copy_from_user(&val, buf, sizeof(val)))
376 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
382 } else if (count >= 2 && !(*ppos % 2)) {
385 if (copy_from_user(&val, buf, sizeof(val)))
388 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
397 if (copy_from_user(&val, buf, sizeof(val)))
400 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
418 static int mdpy_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
420 struct mdev_state *mdev_state =
421 container_of(vdev, struct mdev_state, vdev);
423 if (vma->vm_pgoff != MDPY_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
425 if (vma->vm_end < vma->vm_start)
427 if (vma->vm_end - vma->vm_start > mdev_state->memsize)
429 if ((vma->vm_flags & VM_SHARED) == 0)
432 return remap_vmalloc_range(vma, mdev_state->memblk, 0);
435 static int mdpy_get_region_info(struct mdev_state *mdev_state,
436 struct vfio_region_info *region_info,
437 u16 *cap_type_id, void **cap_type)
439 if (region_info->index >= VFIO_PCI_NUM_REGIONS &&
440 region_info->index != MDPY_DISPLAY_REGION)
443 switch (region_info->index) {
444 case VFIO_PCI_CONFIG_REGION_INDEX:
445 region_info->offset = 0;
446 region_info->size = MDPY_CONFIG_SPACE_SIZE;
447 region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
448 VFIO_REGION_INFO_FLAG_WRITE);
450 case VFIO_PCI_BAR0_REGION_INDEX:
451 case MDPY_DISPLAY_REGION:
452 region_info->offset = MDPY_MEMORY_BAR_OFFSET;
453 region_info->size = mdev_state->memsize;
454 region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
455 VFIO_REGION_INFO_FLAG_WRITE |
456 VFIO_REGION_INFO_FLAG_MMAP);
459 region_info->size = 0;
460 region_info->offset = 0;
461 region_info->flags = 0;
467 static int mdpy_get_irq_info(struct vfio_irq_info *irq_info)
473 static int mdpy_get_device_info(struct vfio_device_info *dev_info)
475 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
476 dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
477 dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
481 static int mdpy_query_gfx_plane(struct mdev_state *mdev_state,
482 struct vfio_device_gfx_plane_info *plane)
484 if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
485 if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
486 VFIO_GFX_PLANE_TYPE_REGION))
491 if (plane->flags != VFIO_GFX_PLANE_TYPE_REGION)
494 plane->drm_format = mdev_state->type->format;
495 plane->width = mdev_state->type->width;
496 plane->height = mdev_state->type->height;
497 plane->stride = (mdev_state->type->width *
498 mdev_state->type->bytepp);
499 plane->size = mdev_state->memsize;
500 plane->region_index = MDPY_DISPLAY_REGION;
503 plane->drm_format_mod = 0;
512 static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd,
517 struct mdev_state *mdev_state =
518 container_of(vdev, struct mdev_state, vdev);
521 case VFIO_DEVICE_GET_INFO:
523 struct vfio_device_info info;
525 minsz = offsetofend(struct vfio_device_info, num_irqs);
527 if (copy_from_user(&info, (void __user *)arg, minsz))
530 if (info.argsz < minsz)
533 ret = mdpy_get_device_info(&info);
537 memcpy(&mdev_state->dev_info, &info, sizeof(info));
539 if (copy_to_user((void __user *)arg, &info, minsz))
544 case VFIO_DEVICE_GET_REGION_INFO:
546 struct vfio_region_info info;
548 void *cap_type = NULL;
550 minsz = offsetofend(struct vfio_region_info, offset);
552 if (copy_from_user(&info, (void __user *)arg, minsz))
555 if (info.argsz < minsz)
558 ret = mdpy_get_region_info(mdev_state, &info, &cap_type_id,
563 if (copy_to_user((void __user *)arg, &info, minsz))
569 case VFIO_DEVICE_GET_IRQ_INFO:
571 struct vfio_irq_info info;
573 minsz = offsetofend(struct vfio_irq_info, count);
575 if (copy_from_user(&info, (void __user *)arg, minsz))
578 if ((info.argsz < minsz) ||
579 (info.index >= mdev_state->dev_info.num_irqs))
582 ret = mdpy_get_irq_info(&info);
586 if (copy_to_user((void __user *)arg, &info, minsz))
592 case VFIO_DEVICE_QUERY_GFX_PLANE:
594 struct vfio_device_gfx_plane_info plane;
596 minsz = offsetofend(struct vfio_device_gfx_plane_info,
599 if (copy_from_user(&plane, (void __user *)arg, minsz))
602 if (plane.argsz < minsz)
605 ret = mdpy_query_gfx_plane(mdev_state, &plane);
609 if (copy_to_user((void __user *)arg, &plane, minsz))
615 case VFIO_DEVICE_SET_IRQS:
618 case VFIO_DEVICE_RESET:
619 return mdpy_reset(mdev_state);
625 resolution_show(struct device *dev, struct device_attribute *attr,
628 struct mdev_state *mdev_state = dev_get_drvdata(dev);
630 return sprintf(buf, "%dx%d\n",
631 mdev_state->type->width,
632 mdev_state->type->height);
634 static DEVICE_ATTR_RO(resolution);
636 static struct attribute *mdev_dev_attrs[] = {
637 &dev_attr_resolution.attr,
641 static const struct attribute_group mdev_dev_group = {
643 .attrs = mdev_dev_attrs,
646 static const struct attribute_group *mdev_dev_groups[] = {
651 static ssize_t mdpy_show_description(struct mdev_type *mtype, char *buf)
653 struct mdpy_type *type = container_of(mtype, struct mdpy_type, type);
655 return sprintf(buf, "virtual display, %dx%d framebuffer\n",
656 type->width, type->height);
659 static const struct vfio_device_ops mdpy_dev_ops = {
660 .init = mdpy_init_dev,
661 .release = mdpy_release_dev,
666 .bind_iommufd = vfio_iommufd_emulated_bind,
667 .unbind_iommufd = vfio_iommufd_emulated_unbind,
668 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
669 .detach_ioas = vfio_iommufd_emulated_detach_ioas,
672 static struct mdev_driver mdpy_driver = {
673 .device_api = VFIO_DEVICE_API_PCI_STRING,
677 .owner = THIS_MODULE,
678 .mod_name = KBUILD_MODNAME,
679 .dev_groups = mdev_dev_groups,
682 .remove = mdpy_remove,
683 .show_description = mdpy_show_description,
686 static const struct file_operations vd_fops = {
687 .owner = THIS_MODULE,
690 static void mdpy_device_release(struct device *dev)
695 static int __init mdpy_dev_init(void)
699 ret = alloc_chrdev_region(&mdpy_devt, 0, MINORMASK + 1, MDPY_NAME);
701 pr_err("Error: failed to register mdpy_dev, err: %d\n", ret);
704 cdev_init(&mdpy_cdev, &vd_fops);
705 cdev_add(&mdpy_cdev, mdpy_devt, MINORMASK + 1);
706 pr_info("%s: major %d\n", __func__, MAJOR(mdpy_devt));
708 ret = mdev_register_driver(&mdpy_driver);
712 mdpy_class = class_create(MDPY_CLASS_NAME);
713 if (IS_ERR(mdpy_class)) {
714 pr_err("Error: failed to register mdpy_dev class\n");
715 ret = PTR_ERR(mdpy_class);
718 mdpy_dev.class = mdpy_class;
719 mdpy_dev.release = mdpy_device_release;
720 dev_set_name(&mdpy_dev, "%s", MDPY_NAME);
722 ret = device_register(&mdpy_dev);
726 ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
728 ARRAY_SIZE(mdpy_mdev_types));
735 device_del(&mdpy_dev);
737 put_device(&mdpy_dev);
738 class_destroy(mdpy_class);
740 mdev_unregister_driver(&mdpy_driver);
742 cdev_del(&mdpy_cdev);
743 unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
747 static void __exit mdpy_dev_exit(void)
750 mdev_unregister_parent(&mdpy_parent);
752 device_unregister(&mdpy_dev);
753 mdev_unregister_driver(&mdpy_driver);
754 cdev_del(&mdpy_cdev);
755 unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
756 class_destroy(mdpy_class);
760 module_param_named(count, mdpy_driver.max_instances, int, 0444);
761 MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
763 module_init(mdpy_dev_init)
764 module_exit(mdpy_dev_exit)