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/device.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/cdev.h>
25 #include <linux/vfio.h>
26 #include <linux/iommu.h>
27 #include <linux/sysfs.h>
28 #include <linux/mdev.h>
29 #include <linux/pci.h>
30 #include <drm/drm_fourcc.h>
31 #include "mdpy-defs.h"
33 #define MDPY_NAME "mdpy"
34 #define MDPY_CLASS_NAME "mdpy"
36 #define MDPY_CONFIG_SPACE_SIZE 0xff
37 #define MDPY_MEMORY_BAR_OFFSET PAGE_SIZE
38 #define MDPY_DISPLAY_REGION 16
40 #define STORE_LE16(addr, val) (*(u16 *)addr = val)
41 #define STORE_LE32(addr, val) (*(u32 *)addr = val)
44 MODULE_LICENSE("GPL v2");
46 static int max_devices = 4;
47 module_param_named(count, max_devices, int, 0444);
48 MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
51 #define MDPY_TYPE_1 "vga"
52 #define MDPY_TYPE_2 "xga"
53 #define MDPY_TYPE_3 "hd"
55 static const struct mdpy_type {
63 .name = MDPY_CLASS_NAME "-" MDPY_TYPE_1,
64 .format = DRM_FORMAT_XRGB8888,
69 .name = MDPY_CLASS_NAME "-" MDPY_TYPE_2,
70 .format = DRM_FORMAT_XRGB8888,
75 .name = MDPY_CLASS_NAME "-" MDPY_TYPE_3,
76 .format = DRM_FORMAT_XRGB8888,
83 static dev_t mdpy_devt;
84 static struct class *mdpy_class;
85 static struct cdev mdpy_cdev;
86 static struct device mdpy_dev;
87 static u32 mdpy_count;
88 static const struct vfio_device_ops mdpy_dev_ops;
90 /* State of each mdev device */
92 struct vfio_device vdev;
95 struct mutex ops_lock;
96 struct mdev_device *mdev;
97 struct vfio_device_info dev_info;
99 const struct mdpy_type *type;
104 static void mdpy_create_config_space(struct mdev_state *mdev_state)
106 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
108 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
110 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
111 MDPY_PCI_SUBVENDOR_ID);
112 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
113 MDPY_PCI_SUBDEVICE_ID);
115 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
116 PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
117 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_STATUS],
118 PCI_STATUS_CAP_LIST);
119 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
120 PCI_CLASS_DISPLAY_OTHER);
121 mdev_state->vconfig[PCI_CLASS_REVISION] = 0x01;
123 STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
124 PCI_BASE_ADDRESS_SPACE_MEMORY |
125 PCI_BASE_ADDRESS_MEM_TYPE_32 |
126 PCI_BASE_ADDRESS_MEM_PREFETCH);
127 mdev_state->bar_mask = ~(mdev_state->memsize) + 1;
129 /* vendor specific capability for the config registers */
130 mdev_state->vconfig[PCI_CAPABILITY_LIST] = MDPY_VENDORCAP_OFFSET;
131 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 0] = 0x09; /* vendor cap */
132 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 1] = 0x00; /* next ptr */
133 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 2] = MDPY_VENDORCAP_SIZE;
134 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_FORMAT_OFFSET],
135 mdev_state->type->format);
136 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_WIDTH_OFFSET],
137 mdev_state->type->width);
138 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_HEIGHT_OFFSET],
139 mdev_state->type->height);
142 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
143 char *buf, u32 count)
145 struct device *dev = mdev_dev(mdev_state->mdev);
149 case PCI_BASE_ADDRESS_0:
150 cfg_addr = *(u32 *)buf;
152 if (cfg_addr == 0xffffffff) {
153 cfg_addr = (cfg_addr & mdev_state->bar_mask);
155 cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
157 dev_info(dev, "BAR0 @ 0x%x\n", cfg_addr);
160 cfg_addr |= (mdev_state->vconfig[offset] &
161 ~PCI_BASE_ADDRESS_MEM_MASK);
162 STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
167 static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
168 size_t count, loff_t pos, bool is_write)
172 mutex_lock(&mdev_state->ops_lock);
174 if (pos < MDPY_CONFIG_SPACE_SIZE) {
176 handle_pci_cfg_write(mdev_state, pos, buf, count);
178 memcpy(buf, (mdev_state->vconfig + pos), count);
180 } else if ((pos >= MDPY_MEMORY_BAR_OFFSET) &&
182 MDPY_MEMORY_BAR_OFFSET + mdev_state->memsize)) {
183 pos -= MDPY_MEMORY_BAR_OFFSET;
185 memcpy(mdev_state->memblk, buf, count);
187 memcpy(buf, mdev_state->memblk, count);
190 dev_info(mdev_state->vdev.dev,
191 "%s: %s @0x%llx (unhandled)\n", __func__,
192 is_write ? "WR" : "RD", pos);
201 mutex_unlock(&mdev_state->ops_lock);
206 static int mdpy_reset(struct mdev_state *mdev_state)
210 /* initialize with gray gradient */
211 stride = mdev_state->type->width * mdev_state->type->bytepp;
212 for (i = 0; i < mdev_state->type->height; i++)
213 memset(mdev_state->memblk + i * stride,
214 i * 255 / mdev_state->type->height,
219 static int mdpy_probe(struct mdev_device *mdev)
221 const struct mdpy_type *type =
222 &mdpy_types[mdev_get_type_group_id(mdev)];
223 struct device *dev = mdev_dev(mdev);
224 struct mdev_state *mdev_state;
228 if (mdpy_count >= max_devices)
231 mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
232 if (mdev_state == NULL)
234 vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mdpy_dev_ops);
236 mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
237 if (mdev_state->vconfig == NULL) {
242 fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
244 mdev_state->memblk = vmalloc_user(fbsize);
245 if (!mdev_state->memblk) {
246 kfree(mdev_state->vconfig);
250 dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
253 mutex_init(&mdev_state->ops_lock);
254 mdev_state->mdev = mdev;
255 mdev_state->type = type;
256 mdev_state->memsize = fbsize;
257 mdpy_create_config_space(mdev_state);
258 mdpy_reset(mdev_state);
262 ret = vfio_register_group_dev(&mdev_state->vdev);
264 kfree(mdev_state->vconfig);
268 dev_set_drvdata(&mdev->dev, mdev_state);
272 static void mdpy_remove(struct mdev_device *mdev)
274 struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
276 dev_info(&mdev->dev, "%s\n", __func__);
278 vfio_unregister_group_dev(&mdev_state->vdev);
279 vfree(mdev_state->memblk);
280 kfree(mdev_state->vconfig);
286 static ssize_t mdpy_read(struct vfio_device *vdev, char __user *buf,
287 size_t count, loff_t *ppos)
289 struct mdev_state *mdev_state =
290 container_of(vdev, struct mdev_state, vdev);
291 unsigned int done = 0;
297 if (count >= 4 && !(*ppos % 4)) {
300 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
305 if (copy_to_user(buf, &val, sizeof(val)))
309 } else if (count >= 2 && !(*ppos % 2)) {
312 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
317 if (copy_to_user(buf, &val, sizeof(val)))
324 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
329 if (copy_to_user(buf, &val, sizeof(val)))
347 static ssize_t mdpy_write(struct vfio_device *vdev, const char __user *buf,
348 size_t count, loff_t *ppos)
350 struct mdev_state *mdev_state =
351 container_of(vdev, struct mdev_state, vdev);
352 unsigned int done = 0;
358 if (count >= 4 && !(*ppos % 4)) {
361 if (copy_from_user(&val, buf, sizeof(val)))
364 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
370 } else if (count >= 2 && !(*ppos % 2)) {
373 if (copy_from_user(&val, buf, sizeof(val)))
376 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
385 if (copy_from_user(&val, buf, sizeof(val)))
388 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
406 static int mdpy_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
408 struct mdev_state *mdev_state =
409 container_of(vdev, struct mdev_state, vdev);
411 if (vma->vm_pgoff != MDPY_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
413 if (vma->vm_end < vma->vm_start)
415 if (vma->vm_end - vma->vm_start > mdev_state->memsize)
417 if ((vma->vm_flags & VM_SHARED) == 0)
420 return remap_vmalloc_range(vma, mdev_state->memblk, 0);
423 static int mdpy_get_region_info(struct mdev_state *mdev_state,
424 struct vfio_region_info *region_info,
425 u16 *cap_type_id, void **cap_type)
427 if (region_info->index >= VFIO_PCI_NUM_REGIONS &&
428 region_info->index != MDPY_DISPLAY_REGION)
431 switch (region_info->index) {
432 case VFIO_PCI_CONFIG_REGION_INDEX:
433 region_info->offset = 0;
434 region_info->size = MDPY_CONFIG_SPACE_SIZE;
435 region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
436 VFIO_REGION_INFO_FLAG_WRITE);
438 case VFIO_PCI_BAR0_REGION_INDEX:
439 case MDPY_DISPLAY_REGION:
440 region_info->offset = MDPY_MEMORY_BAR_OFFSET;
441 region_info->size = mdev_state->memsize;
442 region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
443 VFIO_REGION_INFO_FLAG_WRITE |
444 VFIO_REGION_INFO_FLAG_MMAP);
447 region_info->size = 0;
448 region_info->offset = 0;
449 region_info->flags = 0;
455 static int mdpy_get_irq_info(struct vfio_irq_info *irq_info)
461 static int mdpy_get_device_info(struct vfio_device_info *dev_info)
463 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
464 dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
465 dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
469 static int mdpy_query_gfx_plane(struct mdev_state *mdev_state,
470 struct vfio_device_gfx_plane_info *plane)
472 if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
473 if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
474 VFIO_GFX_PLANE_TYPE_REGION))
479 if (plane->flags != VFIO_GFX_PLANE_TYPE_REGION)
482 plane->drm_format = mdev_state->type->format;
483 plane->width = mdev_state->type->width;
484 plane->height = mdev_state->type->height;
485 plane->stride = (mdev_state->type->width *
486 mdev_state->type->bytepp);
487 plane->size = mdev_state->memsize;
488 plane->region_index = MDPY_DISPLAY_REGION;
491 plane->drm_format_mod = 0;
500 static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd,
505 struct mdev_state *mdev_state =
506 container_of(vdev, struct mdev_state, vdev);
509 case VFIO_DEVICE_GET_INFO:
511 struct vfio_device_info info;
513 minsz = offsetofend(struct vfio_device_info, num_irqs);
515 if (copy_from_user(&info, (void __user *)arg, minsz))
518 if (info.argsz < minsz)
521 ret = mdpy_get_device_info(&info);
525 memcpy(&mdev_state->dev_info, &info, sizeof(info));
527 if (copy_to_user((void __user *)arg, &info, minsz))
532 case VFIO_DEVICE_GET_REGION_INFO:
534 struct vfio_region_info info;
536 void *cap_type = NULL;
538 minsz = offsetofend(struct vfio_region_info, offset);
540 if (copy_from_user(&info, (void __user *)arg, minsz))
543 if (info.argsz < minsz)
546 ret = mdpy_get_region_info(mdev_state, &info, &cap_type_id,
551 if (copy_to_user((void __user *)arg, &info, minsz))
557 case VFIO_DEVICE_GET_IRQ_INFO:
559 struct vfio_irq_info info;
561 minsz = offsetofend(struct vfio_irq_info, count);
563 if (copy_from_user(&info, (void __user *)arg, minsz))
566 if ((info.argsz < minsz) ||
567 (info.index >= mdev_state->dev_info.num_irqs))
570 ret = mdpy_get_irq_info(&info);
574 if (copy_to_user((void __user *)arg, &info, minsz))
580 case VFIO_DEVICE_QUERY_GFX_PLANE:
582 struct vfio_device_gfx_plane_info plane;
584 minsz = offsetofend(struct vfio_device_gfx_plane_info,
587 if (copy_from_user(&plane, (void __user *)arg, minsz))
590 if (plane.argsz < minsz)
593 ret = mdpy_query_gfx_plane(mdev_state, &plane);
597 if (copy_to_user((void __user *)arg, &plane, minsz))
603 case VFIO_DEVICE_SET_IRQS:
606 case VFIO_DEVICE_RESET:
607 return mdpy_reset(mdev_state);
612 static int mdpy_open(struct vfio_device *vdev)
614 if (!try_module_get(THIS_MODULE))
620 static void mdpy_close(struct vfio_device *vdev)
622 module_put(THIS_MODULE);
626 resolution_show(struct device *dev, struct device_attribute *attr,
629 struct mdev_state *mdev_state = dev_get_drvdata(dev);
631 return sprintf(buf, "%dx%d\n",
632 mdev_state->type->width,
633 mdev_state->type->height);
635 static DEVICE_ATTR_RO(resolution);
637 static struct attribute *mdev_dev_attrs[] = {
638 &dev_attr_resolution.attr,
642 static const struct attribute_group mdev_dev_group = {
644 .attrs = mdev_dev_attrs,
647 static const struct attribute_group *mdev_dev_groups[] = {
652 static ssize_t name_show(struct mdev_type *mtype,
653 struct mdev_type_attribute *attr, char *buf)
655 const struct mdpy_type *type =
656 &mdpy_types[mtype_get_type_group_id(mtype)];
658 return sprintf(buf, "%s\n", type->name);
660 static MDEV_TYPE_ATTR_RO(name);
662 static ssize_t description_show(struct mdev_type *mtype,
663 struct mdev_type_attribute *attr, char *buf)
665 const struct mdpy_type *type =
666 &mdpy_types[mtype_get_type_group_id(mtype)];
668 return sprintf(buf, "virtual display, %dx%d framebuffer\n",
669 type->width, type->height);
671 static MDEV_TYPE_ATTR_RO(description);
673 static ssize_t available_instances_show(struct mdev_type *mtype,
674 struct mdev_type_attribute *attr,
677 return sprintf(buf, "%d\n", max_devices - mdpy_count);
679 static MDEV_TYPE_ATTR_RO(available_instances);
681 static ssize_t device_api_show(struct mdev_type *mtype,
682 struct mdev_type_attribute *attr, char *buf)
684 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
686 static MDEV_TYPE_ATTR_RO(device_api);
688 static struct attribute *mdev_types_attrs[] = {
689 &mdev_type_attr_name.attr,
690 &mdev_type_attr_description.attr,
691 &mdev_type_attr_device_api.attr,
692 &mdev_type_attr_available_instances.attr,
696 static struct attribute_group mdev_type_group1 = {
698 .attrs = mdev_types_attrs,
701 static struct attribute_group mdev_type_group2 = {
703 .attrs = mdev_types_attrs,
706 static struct attribute_group mdev_type_group3 = {
708 .attrs = mdev_types_attrs,
711 static struct attribute_group *mdev_type_groups[] = {
718 static const struct vfio_device_ops mdpy_dev_ops = {
720 .release = mdpy_close,
727 static struct mdev_driver mdpy_driver = {
730 .owner = THIS_MODULE,
731 .mod_name = KBUILD_MODNAME,
732 .dev_groups = mdev_dev_groups,
735 .remove = mdpy_remove,
738 static const struct mdev_parent_ops mdev_fops = {
739 .owner = THIS_MODULE,
740 .device_driver = &mdpy_driver,
741 .supported_type_groups = mdev_type_groups,
744 static const struct file_operations vd_fops = {
745 .owner = THIS_MODULE,
748 static void mdpy_device_release(struct device *dev)
753 static int __init mdpy_dev_init(void)
757 ret = alloc_chrdev_region(&mdpy_devt, 0, MINORMASK + 1, MDPY_NAME);
759 pr_err("Error: failed to register mdpy_dev, err: %d\n", ret);
762 cdev_init(&mdpy_cdev, &vd_fops);
763 cdev_add(&mdpy_cdev, mdpy_devt, MINORMASK + 1);
764 pr_info("%s: major %d\n", __func__, MAJOR(mdpy_devt));
766 ret = mdev_register_driver(&mdpy_driver);
770 mdpy_class = class_create(THIS_MODULE, MDPY_CLASS_NAME);
771 if (IS_ERR(mdpy_class)) {
772 pr_err("Error: failed to register mdpy_dev class\n");
773 ret = PTR_ERR(mdpy_class);
776 mdpy_dev.class = mdpy_class;
777 mdpy_dev.release = mdpy_device_release;
778 dev_set_name(&mdpy_dev, "%s", MDPY_NAME);
780 ret = device_register(&mdpy_dev);
784 ret = mdev_register_device(&mdpy_dev, &mdev_fops);
791 device_unregister(&mdpy_dev);
793 class_destroy(mdpy_class);
795 mdev_unregister_driver(&mdpy_driver);
797 cdev_del(&mdpy_cdev);
798 unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
802 static void __exit mdpy_dev_exit(void)
805 mdev_unregister_device(&mdpy_dev);
807 device_unregister(&mdpy_dev);
808 mdev_unregister_driver(&mdpy_driver);
809 cdev_del(&mdpy_cdev);
810 unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
811 class_destroy(mdpy_class);
815 module_init(mdpy_dev_init)
816 module_exit(mdpy_dev_exit)