0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/init.h>
0019 #include <linux/module.h>
0020 #include <linux/device.h>
0021 #include <linux/kernel.h>
0022 #include <linux/slab.h>
0023 #include <linux/vmalloc.h>
0024 #include <linux/cdev.h>
0025 #include <linux/vfio.h>
0026 #include <linux/iommu.h>
0027 #include <linux/sysfs.h>
0028 #include <linux/mdev.h>
0029 #include <linux/pci.h>
0030 #include <drm/drm_fourcc.h>
0031 #include "mdpy-defs.h"
0032
0033 #define MDPY_NAME "mdpy"
0034 #define MDPY_CLASS_NAME "mdpy"
0035
0036 #define MDPY_CONFIG_SPACE_SIZE 0xff
0037 #define MDPY_MEMORY_BAR_OFFSET PAGE_SIZE
0038 #define MDPY_DISPLAY_REGION 16
0039
0040 #define STORE_LE16(addr, val) (*(u16 *)addr = val)
0041 #define STORE_LE32(addr, val) (*(u32 *)addr = val)
0042
0043
0044 MODULE_LICENSE("GPL v2");
0045
0046 static int max_devices = 4;
0047 module_param_named(count, max_devices, int, 0444);
0048 MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
0049
0050
0051 #define MDPY_TYPE_1 "vga"
0052 #define MDPY_TYPE_2 "xga"
0053 #define MDPY_TYPE_3 "hd"
0054
0055 static const struct mdpy_type {
0056 const char *name;
0057 u32 format;
0058 u32 bytepp;
0059 u32 width;
0060 u32 height;
0061 } mdpy_types[] = {
0062 {
0063 .name = MDPY_CLASS_NAME "-" MDPY_TYPE_1,
0064 .format = DRM_FORMAT_XRGB8888,
0065 .bytepp = 4,
0066 .width = 640,
0067 .height = 480,
0068 }, {
0069 .name = MDPY_CLASS_NAME "-" MDPY_TYPE_2,
0070 .format = DRM_FORMAT_XRGB8888,
0071 .bytepp = 4,
0072 .width = 1024,
0073 .height = 768,
0074 }, {
0075 .name = MDPY_CLASS_NAME "-" MDPY_TYPE_3,
0076 .format = DRM_FORMAT_XRGB8888,
0077 .bytepp = 4,
0078 .width = 1920,
0079 .height = 1080,
0080 },
0081 };
0082
0083 static dev_t mdpy_devt;
0084 static struct class *mdpy_class;
0085 static struct cdev mdpy_cdev;
0086 static struct device mdpy_dev;
0087 static u32 mdpy_count;
0088 static const struct vfio_device_ops mdpy_dev_ops;
0089
0090
0091 struct mdev_state {
0092 struct vfio_device vdev;
0093 u8 *vconfig;
0094 u32 bar_mask;
0095 struct mutex ops_lock;
0096 struct mdev_device *mdev;
0097 struct vfio_device_info dev_info;
0098
0099 const struct mdpy_type *type;
0100 u32 memsize;
0101 void *memblk;
0102 };
0103
0104 static void mdpy_create_config_space(struct mdev_state *mdev_state)
0105 {
0106 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
0107 MDPY_PCI_VENDOR_ID);
0108 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
0109 MDPY_PCI_DEVICE_ID);
0110 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
0111 MDPY_PCI_SUBVENDOR_ID);
0112 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
0113 MDPY_PCI_SUBDEVICE_ID);
0114
0115 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
0116 PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
0117 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_STATUS],
0118 PCI_STATUS_CAP_LIST);
0119 STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
0120 PCI_CLASS_DISPLAY_OTHER);
0121 mdev_state->vconfig[PCI_CLASS_REVISION] = 0x01;
0122
0123 STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
0124 PCI_BASE_ADDRESS_SPACE_MEMORY |
0125 PCI_BASE_ADDRESS_MEM_TYPE_32 |
0126 PCI_BASE_ADDRESS_MEM_PREFETCH);
0127 mdev_state->bar_mask = ~(mdev_state->memsize) + 1;
0128
0129
0130 mdev_state->vconfig[PCI_CAPABILITY_LIST] = MDPY_VENDORCAP_OFFSET;
0131 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 0] = 0x09;
0132 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 1] = 0x00;
0133 mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 2] = MDPY_VENDORCAP_SIZE;
0134 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_FORMAT_OFFSET],
0135 mdev_state->type->format);
0136 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_WIDTH_OFFSET],
0137 mdev_state->type->width);
0138 STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_HEIGHT_OFFSET],
0139 mdev_state->type->height);
0140 }
0141
0142 static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
0143 char *buf, u32 count)
0144 {
0145 struct device *dev = mdev_dev(mdev_state->mdev);
0146 u32 cfg_addr;
0147
0148 switch (offset) {
0149 case PCI_BASE_ADDRESS_0:
0150 cfg_addr = *(u32 *)buf;
0151
0152 if (cfg_addr == 0xffffffff) {
0153 cfg_addr = (cfg_addr & mdev_state->bar_mask);
0154 } else {
0155 cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
0156 if (cfg_addr)
0157 dev_info(dev, "BAR0 @ 0x%x\n", cfg_addr);
0158 }
0159
0160 cfg_addr |= (mdev_state->vconfig[offset] &
0161 ~PCI_BASE_ADDRESS_MEM_MASK);
0162 STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
0163 break;
0164 }
0165 }
0166
0167 static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
0168 size_t count, loff_t pos, bool is_write)
0169 {
0170 int ret = 0;
0171
0172 mutex_lock(&mdev_state->ops_lock);
0173
0174 if (pos < MDPY_CONFIG_SPACE_SIZE) {
0175 if (is_write)
0176 handle_pci_cfg_write(mdev_state, pos, buf, count);
0177 else
0178 memcpy(buf, (mdev_state->vconfig + pos), count);
0179
0180 } else if ((pos >= MDPY_MEMORY_BAR_OFFSET) &&
0181 (pos + count <=
0182 MDPY_MEMORY_BAR_OFFSET + mdev_state->memsize)) {
0183 pos -= MDPY_MEMORY_BAR_OFFSET;
0184 if (is_write)
0185 memcpy(mdev_state->memblk, buf, count);
0186 else
0187 memcpy(buf, mdev_state->memblk, count);
0188
0189 } else {
0190 dev_info(mdev_state->vdev.dev,
0191 "%s: %s @0x%llx (unhandled)\n", __func__,
0192 is_write ? "WR" : "RD", pos);
0193 ret = -1;
0194 goto accessfailed;
0195 }
0196
0197 ret = count;
0198
0199
0200 accessfailed:
0201 mutex_unlock(&mdev_state->ops_lock);
0202
0203 return ret;
0204 }
0205
0206 static int mdpy_reset(struct mdev_state *mdev_state)
0207 {
0208 u32 stride, i;
0209
0210
0211 stride = mdev_state->type->width * mdev_state->type->bytepp;
0212 for (i = 0; i < mdev_state->type->height; i++)
0213 memset(mdev_state->memblk + i * stride,
0214 i * 255 / mdev_state->type->height,
0215 stride);
0216 return 0;
0217 }
0218
0219 static int mdpy_probe(struct mdev_device *mdev)
0220 {
0221 const struct mdpy_type *type =
0222 &mdpy_types[mdev_get_type_group_id(mdev)];
0223 struct device *dev = mdev_dev(mdev);
0224 struct mdev_state *mdev_state;
0225 u32 fbsize;
0226 int ret;
0227
0228 if (mdpy_count >= max_devices)
0229 return -ENOMEM;
0230
0231 mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
0232 if (mdev_state == NULL)
0233 return -ENOMEM;
0234 vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mdpy_dev_ops);
0235
0236 mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
0237 if (mdev_state->vconfig == NULL) {
0238 ret = -ENOMEM;
0239 goto err_state;
0240 }
0241
0242 fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
0243
0244 mdev_state->memblk = vmalloc_user(fbsize);
0245 if (!mdev_state->memblk) {
0246 ret = -ENOMEM;
0247 goto err_vconfig;
0248 }
0249 dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
0250 type->height);
0251
0252 mutex_init(&mdev_state->ops_lock);
0253 mdev_state->mdev = mdev;
0254 mdev_state->type = type;
0255 mdev_state->memsize = fbsize;
0256 mdpy_create_config_space(mdev_state);
0257 mdpy_reset(mdev_state);
0258
0259 mdpy_count++;
0260
0261 ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
0262 if (ret)
0263 goto err_mem;
0264 dev_set_drvdata(&mdev->dev, mdev_state);
0265 return 0;
0266 err_mem:
0267 vfree(mdev_state->memblk);
0268 err_vconfig:
0269 kfree(mdev_state->vconfig);
0270 err_state:
0271 vfio_uninit_group_dev(&mdev_state->vdev);
0272 kfree(mdev_state);
0273 return ret;
0274 }
0275
0276 static void mdpy_remove(struct mdev_device *mdev)
0277 {
0278 struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
0279
0280 dev_info(&mdev->dev, "%s\n", __func__);
0281
0282 vfio_unregister_group_dev(&mdev_state->vdev);
0283 vfree(mdev_state->memblk);
0284 kfree(mdev_state->vconfig);
0285 vfio_uninit_group_dev(&mdev_state->vdev);
0286 kfree(mdev_state);
0287
0288 mdpy_count--;
0289 }
0290
0291 static ssize_t mdpy_read(struct vfio_device *vdev, char __user *buf,
0292 size_t count, loff_t *ppos)
0293 {
0294 struct mdev_state *mdev_state =
0295 container_of(vdev, struct mdev_state, vdev);
0296 unsigned int done = 0;
0297 int ret;
0298
0299 while (count) {
0300 size_t filled;
0301
0302 if (count >= 4 && !(*ppos % 4)) {
0303 u32 val;
0304
0305 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
0306 *ppos, false);
0307 if (ret <= 0)
0308 goto read_err;
0309
0310 if (copy_to_user(buf, &val, sizeof(val)))
0311 goto read_err;
0312
0313 filled = 4;
0314 } else if (count >= 2 && !(*ppos % 2)) {
0315 u16 val;
0316
0317 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
0318 *ppos, false);
0319 if (ret <= 0)
0320 goto read_err;
0321
0322 if (copy_to_user(buf, &val, sizeof(val)))
0323 goto read_err;
0324
0325 filled = 2;
0326 } else {
0327 u8 val;
0328
0329 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
0330 *ppos, false);
0331 if (ret <= 0)
0332 goto read_err;
0333
0334 if (copy_to_user(buf, &val, sizeof(val)))
0335 goto read_err;
0336
0337 filled = 1;
0338 }
0339
0340 count -= filled;
0341 done += filled;
0342 *ppos += filled;
0343 buf += filled;
0344 }
0345
0346 return done;
0347
0348 read_err:
0349 return -EFAULT;
0350 }
0351
0352 static ssize_t mdpy_write(struct vfio_device *vdev, const char __user *buf,
0353 size_t count, loff_t *ppos)
0354 {
0355 struct mdev_state *mdev_state =
0356 container_of(vdev, struct mdev_state, vdev);
0357 unsigned int done = 0;
0358 int ret;
0359
0360 while (count) {
0361 size_t filled;
0362
0363 if (count >= 4 && !(*ppos % 4)) {
0364 u32 val;
0365
0366 if (copy_from_user(&val, buf, sizeof(val)))
0367 goto write_err;
0368
0369 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
0370 *ppos, true);
0371 if (ret <= 0)
0372 goto write_err;
0373
0374 filled = 4;
0375 } else if (count >= 2 && !(*ppos % 2)) {
0376 u16 val;
0377
0378 if (copy_from_user(&val, buf, sizeof(val)))
0379 goto write_err;
0380
0381 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
0382 *ppos, true);
0383 if (ret <= 0)
0384 goto write_err;
0385
0386 filled = 2;
0387 } else {
0388 u8 val;
0389
0390 if (copy_from_user(&val, buf, sizeof(val)))
0391 goto write_err;
0392
0393 ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
0394 *ppos, true);
0395 if (ret <= 0)
0396 goto write_err;
0397
0398 filled = 1;
0399 }
0400 count -= filled;
0401 done += filled;
0402 *ppos += filled;
0403 buf += filled;
0404 }
0405
0406 return done;
0407 write_err:
0408 return -EFAULT;
0409 }
0410
0411 static int mdpy_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
0412 {
0413 struct mdev_state *mdev_state =
0414 container_of(vdev, struct mdev_state, vdev);
0415
0416 if (vma->vm_pgoff != MDPY_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
0417 return -EINVAL;
0418 if (vma->vm_end < vma->vm_start)
0419 return -EINVAL;
0420 if (vma->vm_end - vma->vm_start > mdev_state->memsize)
0421 return -EINVAL;
0422 if ((vma->vm_flags & VM_SHARED) == 0)
0423 return -EINVAL;
0424
0425 return remap_vmalloc_range(vma, mdev_state->memblk, 0);
0426 }
0427
0428 static int mdpy_get_region_info(struct mdev_state *mdev_state,
0429 struct vfio_region_info *region_info,
0430 u16 *cap_type_id, void **cap_type)
0431 {
0432 if (region_info->index >= VFIO_PCI_NUM_REGIONS &&
0433 region_info->index != MDPY_DISPLAY_REGION)
0434 return -EINVAL;
0435
0436 switch (region_info->index) {
0437 case VFIO_PCI_CONFIG_REGION_INDEX:
0438 region_info->offset = 0;
0439 region_info->size = MDPY_CONFIG_SPACE_SIZE;
0440 region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
0441 VFIO_REGION_INFO_FLAG_WRITE);
0442 break;
0443 case VFIO_PCI_BAR0_REGION_INDEX:
0444 case MDPY_DISPLAY_REGION:
0445 region_info->offset = MDPY_MEMORY_BAR_OFFSET;
0446 region_info->size = mdev_state->memsize;
0447 region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
0448 VFIO_REGION_INFO_FLAG_WRITE |
0449 VFIO_REGION_INFO_FLAG_MMAP);
0450 break;
0451 default:
0452 region_info->size = 0;
0453 region_info->offset = 0;
0454 region_info->flags = 0;
0455 }
0456
0457 return 0;
0458 }
0459
0460 static int mdpy_get_irq_info(struct vfio_irq_info *irq_info)
0461 {
0462 irq_info->count = 0;
0463 return 0;
0464 }
0465
0466 static int mdpy_get_device_info(struct vfio_device_info *dev_info)
0467 {
0468 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
0469 dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
0470 dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
0471 return 0;
0472 }
0473
0474 static int mdpy_query_gfx_plane(struct mdev_state *mdev_state,
0475 struct vfio_device_gfx_plane_info *plane)
0476 {
0477 if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
0478 if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
0479 VFIO_GFX_PLANE_TYPE_REGION))
0480 return 0;
0481 return -EINVAL;
0482 }
0483
0484 if (plane->flags != VFIO_GFX_PLANE_TYPE_REGION)
0485 return -EINVAL;
0486
0487 plane->drm_format = mdev_state->type->format;
0488 plane->width = mdev_state->type->width;
0489 plane->height = mdev_state->type->height;
0490 plane->stride = (mdev_state->type->width *
0491 mdev_state->type->bytepp);
0492 plane->size = mdev_state->memsize;
0493 plane->region_index = MDPY_DISPLAY_REGION;
0494
0495
0496 plane->drm_format_mod = 0;
0497 plane->x_pos = 0;
0498 plane->y_pos = 0;
0499 plane->x_hot = 0;
0500 plane->y_hot = 0;
0501
0502 return 0;
0503 }
0504
0505 static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd,
0506 unsigned long arg)
0507 {
0508 int ret = 0;
0509 unsigned long minsz;
0510 struct mdev_state *mdev_state =
0511 container_of(vdev, struct mdev_state, vdev);
0512
0513 switch (cmd) {
0514 case VFIO_DEVICE_GET_INFO:
0515 {
0516 struct vfio_device_info info;
0517
0518 minsz = offsetofend(struct vfio_device_info, num_irqs);
0519
0520 if (copy_from_user(&info, (void __user *)arg, minsz))
0521 return -EFAULT;
0522
0523 if (info.argsz < minsz)
0524 return -EINVAL;
0525
0526 ret = mdpy_get_device_info(&info);
0527 if (ret)
0528 return ret;
0529
0530 memcpy(&mdev_state->dev_info, &info, sizeof(info));
0531
0532 if (copy_to_user((void __user *)arg, &info, minsz))
0533 return -EFAULT;
0534
0535 return 0;
0536 }
0537 case VFIO_DEVICE_GET_REGION_INFO:
0538 {
0539 struct vfio_region_info info;
0540 u16 cap_type_id = 0;
0541 void *cap_type = NULL;
0542
0543 minsz = offsetofend(struct vfio_region_info, offset);
0544
0545 if (copy_from_user(&info, (void __user *)arg, minsz))
0546 return -EFAULT;
0547
0548 if (info.argsz < minsz)
0549 return -EINVAL;
0550
0551 ret = mdpy_get_region_info(mdev_state, &info, &cap_type_id,
0552 &cap_type);
0553 if (ret)
0554 return ret;
0555
0556 if (copy_to_user((void __user *)arg, &info, minsz))
0557 return -EFAULT;
0558
0559 return 0;
0560 }
0561
0562 case VFIO_DEVICE_GET_IRQ_INFO:
0563 {
0564 struct vfio_irq_info info;
0565
0566 minsz = offsetofend(struct vfio_irq_info, count);
0567
0568 if (copy_from_user(&info, (void __user *)arg, minsz))
0569 return -EFAULT;
0570
0571 if ((info.argsz < minsz) ||
0572 (info.index >= mdev_state->dev_info.num_irqs))
0573 return -EINVAL;
0574
0575 ret = mdpy_get_irq_info(&info);
0576 if (ret)
0577 return ret;
0578
0579 if (copy_to_user((void __user *)arg, &info, minsz))
0580 return -EFAULT;
0581
0582 return 0;
0583 }
0584
0585 case VFIO_DEVICE_QUERY_GFX_PLANE:
0586 {
0587 struct vfio_device_gfx_plane_info plane;
0588
0589 minsz = offsetofend(struct vfio_device_gfx_plane_info,
0590 region_index);
0591
0592 if (copy_from_user(&plane, (void __user *)arg, minsz))
0593 return -EFAULT;
0594
0595 if (plane.argsz < minsz)
0596 return -EINVAL;
0597
0598 ret = mdpy_query_gfx_plane(mdev_state, &plane);
0599 if (ret)
0600 return ret;
0601
0602 if (copy_to_user((void __user *)arg, &plane, minsz))
0603 return -EFAULT;
0604
0605 return 0;
0606 }
0607
0608 case VFIO_DEVICE_SET_IRQS:
0609 return -EINVAL;
0610
0611 case VFIO_DEVICE_RESET:
0612 return mdpy_reset(mdev_state);
0613 }
0614 return -ENOTTY;
0615 }
0616
0617 static ssize_t
0618 resolution_show(struct device *dev, struct device_attribute *attr,
0619 char *buf)
0620 {
0621 struct mdev_state *mdev_state = dev_get_drvdata(dev);
0622
0623 return sprintf(buf, "%dx%d\n",
0624 mdev_state->type->width,
0625 mdev_state->type->height);
0626 }
0627 static DEVICE_ATTR_RO(resolution);
0628
0629 static struct attribute *mdev_dev_attrs[] = {
0630 &dev_attr_resolution.attr,
0631 NULL,
0632 };
0633
0634 static const struct attribute_group mdev_dev_group = {
0635 .name = "vendor",
0636 .attrs = mdev_dev_attrs,
0637 };
0638
0639 static const struct attribute_group *mdev_dev_groups[] = {
0640 &mdev_dev_group,
0641 NULL,
0642 };
0643
0644 static ssize_t name_show(struct mdev_type *mtype,
0645 struct mdev_type_attribute *attr, char *buf)
0646 {
0647 const struct mdpy_type *type =
0648 &mdpy_types[mtype_get_type_group_id(mtype)];
0649
0650 return sprintf(buf, "%s\n", type->name);
0651 }
0652 static MDEV_TYPE_ATTR_RO(name);
0653
0654 static ssize_t description_show(struct mdev_type *mtype,
0655 struct mdev_type_attribute *attr, char *buf)
0656 {
0657 const struct mdpy_type *type =
0658 &mdpy_types[mtype_get_type_group_id(mtype)];
0659
0660 return sprintf(buf, "virtual display, %dx%d framebuffer\n",
0661 type->width, type->height);
0662 }
0663 static MDEV_TYPE_ATTR_RO(description);
0664
0665 static ssize_t available_instances_show(struct mdev_type *mtype,
0666 struct mdev_type_attribute *attr,
0667 char *buf)
0668 {
0669 return sprintf(buf, "%d\n", max_devices - mdpy_count);
0670 }
0671 static MDEV_TYPE_ATTR_RO(available_instances);
0672
0673 static ssize_t device_api_show(struct mdev_type *mtype,
0674 struct mdev_type_attribute *attr, char *buf)
0675 {
0676 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
0677 }
0678 static MDEV_TYPE_ATTR_RO(device_api);
0679
0680 static struct attribute *mdev_types_attrs[] = {
0681 &mdev_type_attr_name.attr,
0682 &mdev_type_attr_description.attr,
0683 &mdev_type_attr_device_api.attr,
0684 &mdev_type_attr_available_instances.attr,
0685 NULL,
0686 };
0687
0688 static struct attribute_group mdev_type_group1 = {
0689 .name = MDPY_TYPE_1,
0690 .attrs = mdev_types_attrs,
0691 };
0692
0693 static struct attribute_group mdev_type_group2 = {
0694 .name = MDPY_TYPE_2,
0695 .attrs = mdev_types_attrs,
0696 };
0697
0698 static struct attribute_group mdev_type_group3 = {
0699 .name = MDPY_TYPE_3,
0700 .attrs = mdev_types_attrs,
0701 };
0702
0703 static struct attribute_group *mdev_type_groups[] = {
0704 &mdev_type_group1,
0705 &mdev_type_group2,
0706 &mdev_type_group3,
0707 NULL,
0708 };
0709
0710 static const struct vfio_device_ops mdpy_dev_ops = {
0711 .read = mdpy_read,
0712 .write = mdpy_write,
0713 .ioctl = mdpy_ioctl,
0714 .mmap = mdpy_mmap,
0715 };
0716
0717 static struct mdev_driver mdpy_driver = {
0718 .driver = {
0719 .name = "mdpy",
0720 .owner = THIS_MODULE,
0721 .mod_name = KBUILD_MODNAME,
0722 .dev_groups = mdev_dev_groups,
0723 },
0724 .probe = mdpy_probe,
0725 .remove = mdpy_remove,
0726 .supported_type_groups = mdev_type_groups,
0727 };
0728
0729 static const struct file_operations vd_fops = {
0730 .owner = THIS_MODULE,
0731 };
0732
0733 static void mdpy_device_release(struct device *dev)
0734 {
0735
0736 }
0737
0738 static int __init mdpy_dev_init(void)
0739 {
0740 int ret = 0;
0741
0742 ret = alloc_chrdev_region(&mdpy_devt, 0, MINORMASK + 1, MDPY_NAME);
0743 if (ret < 0) {
0744 pr_err("Error: failed to register mdpy_dev, err: %d\n", ret);
0745 return ret;
0746 }
0747 cdev_init(&mdpy_cdev, &vd_fops);
0748 cdev_add(&mdpy_cdev, mdpy_devt, MINORMASK + 1);
0749 pr_info("%s: major %d\n", __func__, MAJOR(mdpy_devt));
0750
0751 ret = mdev_register_driver(&mdpy_driver);
0752 if (ret)
0753 goto err_cdev;
0754
0755 mdpy_class = class_create(THIS_MODULE, MDPY_CLASS_NAME);
0756 if (IS_ERR(mdpy_class)) {
0757 pr_err("Error: failed to register mdpy_dev class\n");
0758 ret = PTR_ERR(mdpy_class);
0759 goto err_driver;
0760 }
0761 mdpy_dev.class = mdpy_class;
0762 mdpy_dev.release = mdpy_device_release;
0763 dev_set_name(&mdpy_dev, "%s", MDPY_NAME);
0764
0765 ret = device_register(&mdpy_dev);
0766 if (ret)
0767 goto err_class;
0768
0769 ret = mdev_register_device(&mdpy_dev, &mdpy_driver);
0770 if (ret)
0771 goto err_device;
0772
0773 return 0;
0774
0775 err_device:
0776 device_unregister(&mdpy_dev);
0777 err_class:
0778 class_destroy(mdpy_class);
0779 err_driver:
0780 mdev_unregister_driver(&mdpy_driver);
0781 err_cdev:
0782 cdev_del(&mdpy_cdev);
0783 unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
0784 return ret;
0785 }
0786
0787 static void __exit mdpy_dev_exit(void)
0788 {
0789 mdpy_dev.bus = NULL;
0790 mdev_unregister_device(&mdpy_dev);
0791
0792 device_unregister(&mdpy_dev);
0793 mdev_unregister_driver(&mdpy_driver);
0794 cdev_del(&mdpy_cdev);
0795 unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
0796 class_destroy(mdpy_class);
0797 mdpy_class = NULL;
0798 }
0799
0800 module_init(mdpy_dev_init)
0801 module_exit(mdpy_dev_exit)