mdpy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 <[email protected]>
  8. *
  9. * based on mtty driver which is:
  10. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  11. * Author: Neo Jia <[email protected]>
  12. * Kirti Wankhede <[email protected]>
  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. #define MDPY_NAME "mdpy"
  32. #define MDPY_CLASS_NAME "mdpy"
  33. #define MDPY_CONFIG_SPACE_SIZE 0xff
  34. #define MDPY_MEMORY_BAR_OFFSET PAGE_SIZE
  35. #define MDPY_DISPLAY_REGION 16
  36. #define STORE_LE16(addr, val) (*(u16 *)addr = val)
  37. #define STORE_LE32(addr, val) (*(u32 *)addr = val)
  38. MODULE_LICENSE("GPL v2");
  39. #define MDPY_TYPE_1 "vga"
  40. #define MDPY_TYPE_2 "xga"
  41. #define MDPY_TYPE_3 "hd"
  42. static struct mdpy_type {
  43. struct mdev_type type;
  44. u32 format;
  45. u32 bytepp;
  46. u32 width;
  47. u32 height;
  48. } mdpy_types[] = {
  49. {
  50. .type.sysfs_name = MDPY_TYPE_1,
  51. .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_1,
  52. .format = DRM_FORMAT_XRGB8888,
  53. .bytepp = 4,
  54. .width = 640,
  55. .height = 480,
  56. }, {
  57. .type.sysfs_name = MDPY_TYPE_2,
  58. .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_2,
  59. .format = DRM_FORMAT_XRGB8888,
  60. .bytepp = 4,
  61. .width = 1024,
  62. .height = 768,
  63. }, {
  64. .type.sysfs_name = MDPY_TYPE_3,
  65. .type.pretty_name = MDPY_CLASS_NAME "-" MDPY_TYPE_3,
  66. .format = DRM_FORMAT_XRGB8888,
  67. .bytepp = 4,
  68. .width = 1920,
  69. .height = 1080,
  70. },
  71. };
  72. static struct mdev_type *mdpy_mdev_types[] = {
  73. &mdpy_types[0].type,
  74. &mdpy_types[1].type,
  75. &mdpy_types[2].type,
  76. };
  77. static dev_t mdpy_devt;
  78. static struct class *mdpy_class;
  79. static struct cdev mdpy_cdev;
  80. static struct device mdpy_dev;
  81. static struct mdev_parent mdpy_parent;
  82. static const struct vfio_device_ops mdpy_dev_ops;
  83. /* State of each mdev device */
  84. struct mdev_state {
  85. struct vfio_device vdev;
  86. u8 *vconfig;
  87. u32 bar_mask;
  88. struct mutex ops_lock;
  89. struct mdev_device *mdev;
  90. struct vfio_device_info dev_info;
  91. const struct mdpy_type *type;
  92. u32 memsize;
  93. void *memblk;
  94. };
  95. static void mdpy_create_config_space(struct mdev_state *mdev_state)
  96. {
  97. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
  98. MDPY_PCI_VENDOR_ID);
  99. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
  100. MDPY_PCI_DEVICE_ID);
  101. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
  102. MDPY_PCI_SUBVENDOR_ID);
  103. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
  104. MDPY_PCI_SUBDEVICE_ID);
  105. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
  106. PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  107. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_STATUS],
  108. PCI_STATUS_CAP_LIST);
  109. STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
  110. PCI_CLASS_DISPLAY_OTHER);
  111. mdev_state->vconfig[PCI_CLASS_REVISION] = 0x01;
  112. STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
  113. PCI_BASE_ADDRESS_SPACE_MEMORY |
  114. PCI_BASE_ADDRESS_MEM_TYPE_32 |
  115. PCI_BASE_ADDRESS_MEM_PREFETCH);
  116. mdev_state->bar_mask = ~(mdev_state->memsize) + 1;
  117. /* vendor specific capability for the config registers */
  118. mdev_state->vconfig[PCI_CAPABILITY_LIST] = MDPY_VENDORCAP_OFFSET;
  119. mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 0] = 0x09; /* vendor cap */
  120. mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 1] = 0x00; /* next ptr */
  121. mdev_state->vconfig[MDPY_VENDORCAP_OFFSET + 2] = MDPY_VENDORCAP_SIZE;
  122. STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_FORMAT_OFFSET],
  123. mdev_state->type->format);
  124. STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_WIDTH_OFFSET],
  125. mdev_state->type->width);
  126. STORE_LE32((u32 *) &mdev_state->vconfig[MDPY_HEIGHT_OFFSET],
  127. mdev_state->type->height);
  128. }
  129. static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
  130. char *buf, u32 count)
  131. {
  132. struct device *dev = mdev_dev(mdev_state->mdev);
  133. u32 cfg_addr;
  134. switch (offset) {
  135. case PCI_BASE_ADDRESS_0:
  136. cfg_addr = *(u32 *)buf;
  137. if (cfg_addr == 0xffffffff) {
  138. cfg_addr = (cfg_addr & mdev_state->bar_mask);
  139. } else {
  140. cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
  141. if (cfg_addr)
  142. dev_info(dev, "BAR0 @ 0x%x\n", cfg_addr);
  143. }
  144. cfg_addr |= (mdev_state->vconfig[offset] &
  145. ~PCI_BASE_ADDRESS_MEM_MASK);
  146. STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
  147. break;
  148. }
  149. }
  150. static ssize_t mdev_access(struct mdev_state *mdev_state, char *buf,
  151. size_t count, loff_t pos, bool is_write)
  152. {
  153. int ret = 0;
  154. mutex_lock(&mdev_state->ops_lock);
  155. if (pos < MDPY_CONFIG_SPACE_SIZE) {
  156. if (is_write)
  157. handle_pci_cfg_write(mdev_state, pos, buf, count);
  158. else
  159. memcpy(buf, (mdev_state->vconfig + pos), count);
  160. } else if ((pos >= MDPY_MEMORY_BAR_OFFSET) &&
  161. (pos + count <=
  162. MDPY_MEMORY_BAR_OFFSET + mdev_state->memsize)) {
  163. pos -= MDPY_MEMORY_BAR_OFFSET;
  164. if (is_write)
  165. memcpy(mdev_state->memblk, buf, count);
  166. else
  167. memcpy(buf, mdev_state->memblk, count);
  168. } else {
  169. dev_info(mdev_state->vdev.dev,
  170. "%s: %s @0x%llx (unhandled)\n", __func__,
  171. is_write ? "WR" : "RD", pos);
  172. ret = -1;
  173. goto accessfailed;
  174. }
  175. ret = count;
  176. accessfailed:
  177. mutex_unlock(&mdev_state->ops_lock);
  178. return ret;
  179. }
  180. static int mdpy_reset(struct mdev_state *mdev_state)
  181. {
  182. u32 stride, i;
  183. /* initialize with gray gradient */
  184. stride = mdev_state->type->width * mdev_state->type->bytepp;
  185. for (i = 0; i < mdev_state->type->height; i++)
  186. memset(mdev_state->memblk + i * stride,
  187. i * 255 / mdev_state->type->height,
  188. stride);
  189. return 0;
  190. }
  191. static int mdpy_init_dev(struct vfio_device *vdev)
  192. {
  193. struct mdev_state *mdev_state =
  194. container_of(vdev, struct mdev_state, vdev);
  195. struct mdev_device *mdev = to_mdev_device(vdev->dev);
  196. const struct mdpy_type *type =
  197. container_of(mdev->type, struct mdpy_type, type);
  198. u32 fbsize;
  199. int ret = -ENOMEM;
  200. mdev_state->vconfig = kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL);
  201. if (!mdev_state->vconfig)
  202. return ret;
  203. fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
  204. mdev_state->memblk = vmalloc_user(fbsize);
  205. if (!mdev_state->memblk)
  206. goto out_vconfig;
  207. mutex_init(&mdev_state->ops_lock);
  208. mdev_state->mdev = mdev;
  209. mdev_state->type = type;
  210. mdev_state->memsize = fbsize;
  211. mdpy_create_config_space(mdev_state);
  212. mdpy_reset(mdev_state);
  213. dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->type.pretty_name,
  214. type->width, type->height);
  215. return 0;
  216. out_vconfig:
  217. kfree(mdev_state->vconfig);
  218. return ret;
  219. }
  220. static int mdpy_probe(struct mdev_device *mdev)
  221. {
  222. struct mdev_state *mdev_state;
  223. int ret;
  224. mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
  225. &mdpy_dev_ops);
  226. if (IS_ERR(mdev_state))
  227. return PTR_ERR(mdev_state);
  228. ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
  229. if (ret)
  230. goto err_put_vdev;
  231. dev_set_drvdata(&mdev->dev, mdev_state);
  232. return 0;
  233. err_put_vdev:
  234. vfio_put_device(&mdev_state->vdev);
  235. return ret;
  236. }
  237. static void mdpy_release_dev(struct vfio_device *vdev)
  238. {
  239. struct mdev_state *mdev_state =
  240. container_of(vdev, struct mdev_state, vdev);
  241. vfree(mdev_state->memblk);
  242. kfree(mdev_state->vconfig);
  243. vfio_free_device(vdev);
  244. }
  245. static void mdpy_remove(struct mdev_device *mdev)
  246. {
  247. struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
  248. dev_info(&mdev->dev, "%s\n", __func__);
  249. vfio_unregister_group_dev(&mdev_state->vdev);
  250. vfio_put_device(&mdev_state->vdev);
  251. }
  252. static ssize_t mdpy_read(struct vfio_device *vdev, char __user *buf,
  253. size_t count, loff_t *ppos)
  254. {
  255. struct mdev_state *mdev_state =
  256. container_of(vdev, struct mdev_state, vdev);
  257. unsigned int done = 0;
  258. int ret;
  259. while (count) {
  260. size_t filled;
  261. if (count >= 4 && !(*ppos % 4)) {
  262. u32 val;
  263. ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
  264. *ppos, false);
  265. if (ret <= 0)
  266. goto read_err;
  267. if (copy_to_user(buf, &val, sizeof(val)))
  268. goto read_err;
  269. filled = 4;
  270. } else if (count >= 2 && !(*ppos % 2)) {
  271. u16 val;
  272. ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
  273. *ppos, false);
  274. if (ret <= 0)
  275. goto read_err;
  276. if (copy_to_user(buf, &val, sizeof(val)))
  277. goto read_err;
  278. filled = 2;
  279. } else {
  280. u8 val;
  281. ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
  282. *ppos, false);
  283. if (ret <= 0)
  284. goto read_err;
  285. if (copy_to_user(buf, &val, sizeof(val)))
  286. goto read_err;
  287. filled = 1;
  288. }
  289. count -= filled;
  290. done += filled;
  291. *ppos += filled;
  292. buf += filled;
  293. }
  294. return done;
  295. read_err:
  296. return -EFAULT;
  297. }
  298. static ssize_t mdpy_write(struct vfio_device *vdev, const char __user *buf,
  299. size_t count, loff_t *ppos)
  300. {
  301. struct mdev_state *mdev_state =
  302. container_of(vdev, struct mdev_state, vdev);
  303. unsigned int done = 0;
  304. int ret;
  305. while (count) {
  306. size_t filled;
  307. if (count >= 4 && !(*ppos % 4)) {
  308. u32 val;
  309. if (copy_from_user(&val, buf, sizeof(val)))
  310. goto write_err;
  311. ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
  312. *ppos, true);
  313. if (ret <= 0)
  314. goto write_err;
  315. filled = 4;
  316. } else if (count >= 2 && !(*ppos % 2)) {
  317. u16 val;
  318. if (copy_from_user(&val, buf, sizeof(val)))
  319. goto write_err;
  320. ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
  321. *ppos, true);
  322. if (ret <= 0)
  323. goto write_err;
  324. filled = 2;
  325. } else {
  326. u8 val;
  327. if (copy_from_user(&val, buf, sizeof(val)))
  328. goto write_err;
  329. ret = mdev_access(mdev_state, (char *)&val, sizeof(val),
  330. *ppos, true);
  331. if (ret <= 0)
  332. goto write_err;
  333. filled = 1;
  334. }
  335. count -= filled;
  336. done += filled;
  337. *ppos += filled;
  338. buf += filled;
  339. }
  340. return done;
  341. write_err:
  342. return -EFAULT;
  343. }
  344. static int mdpy_mmap(struct vfio_device *vdev, struct vm_area_struct *vma)
  345. {
  346. struct mdev_state *mdev_state =
  347. container_of(vdev, struct mdev_state, vdev);
  348. if (vma->vm_pgoff != MDPY_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
  349. return -EINVAL;
  350. if (vma->vm_end < vma->vm_start)
  351. return -EINVAL;
  352. if (vma->vm_end - vma->vm_start > mdev_state->memsize)
  353. return -EINVAL;
  354. if ((vma->vm_flags & VM_SHARED) == 0)
  355. return -EINVAL;
  356. return remap_vmalloc_range(vma, mdev_state->memblk, 0);
  357. }
  358. static int mdpy_get_region_info(struct mdev_state *mdev_state,
  359. struct vfio_region_info *region_info,
  360. u16 *cap_type_id, void **cap_type)
  361. {
  362. if (region_info->index >= VFIO_PCI_NUM_REGIONS &&
  363. region_info->index != MDPY_DISPLAY_REGION)
  364. return -EINVAL;
  365. switch (region_info->index) {
  366. case VFIO_PCI_CONFIG_REGION_INDEX:
  367. region_info->offset = 0;
  368. region_info->size = MDPY_CONFIG_SPACE_SIZE;
  369. region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
  370. VFIO_REGION_INFO_FLAG_WRITE);
  371. break;
  372. case VFIO_PCI_BAR0_REGION_INDEX:
  373. case MDPY_DISPLAY_REGION:
  374. region_info->offset = MDPY_MEMORY_BAR_OFFSET;
  375. region_info->size = mdev_state->memsize;
  376. region_info->flags = (VFIO_REGION_INFO_FLAG_READ |
  377. VFIO_REGION_INFO_FLAG_WRITE |
  378. VFIO_REGION_INFO_FLAG_MMAP);
  379. break;
  380. default:
  381. region_info->size = 0;
  382. region_info->offset = 0;
  383. region_info->flags = 0;
  384. }
  385. return 0;
  386. }
  387. static int mdpy_get_irq_info(struct vfio_irq_info *irq_info)
  388. {
  389. irq_info->count = 0;
  390. return 0;
  391. }
  392. static int mdpy_get_device_info(struct vfio_device_info *dev_info)
  393. {
  394. dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
  395. dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
  396. dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
  397. return 0;
  398. }
  399. static int mdpy_query_gfx_plane(struct mdev_state *mdev_state,
  400. struct vfio_device_gfx_plane_info *plane)
  401. {
  402. if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
  403. if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
  404. VFIO_GFX_PLANE_TYPE_REGION))
  405. return 0;
  406. return -EINVAL;
  407. }
  408. if (plane->flags != VFIO_GFX_PLANE_TYPE_REGION)
  409. return -EINVAL;
  410. plane->drm_format = mdev_state->type->format;
  411. plane->width = mdev_state->type->width;
  412. plane->height = mdev_state->type->height;
  413. plane->stride = (mdev_state->type->width *
  414. mdev_state->type->bytepp);
  415. plane->size = mdev_state->memsize;
  416. plane->region_index = MDPY_DISPLAY_REGION;
  417. /* unused */
  418. plane->drm_format_mod = 0;
  419. plane->x_pos = 0;
  420. plane->y_pos = 0;
  421. plane->x_hot = 0;
  422. plane->y_hot = 0;
  423. return 0;
  424. }
  425. static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd,
  426. unsigned long arg)
  427. {
  428. int ret = 0;
  429. unsigned long minsz;
  430. struct mdev_state *mdev_state =
  431. container_of(vdev, struct mdev_state, vdev);
  432. switch (cmd) {
  433. case VFIO_DEVICE_GET_INFO:
  434. {
  435. struct vfio_device_info info;
  436. minsz = offsetofend(struct vfio_device_info, num_irqs);
  437. if (copy_from_user(&info, (void __user *)arg, minsz))
  438. return -EFAULT;
  439. if (info.argsz < minsz)
  440. return -EINVAL;
  441. ret = mdpy_get_device_info(&info);
  442. if (ret)
  443. return ret;
  444. memcpy(&mdev_state->dev_info, &info, sizeof(info));
  445. if (copy_to_user((void __user *)arg, &info, minsz))
  446. return -EFAULT;
  447. return 0;
  448. }
  449. case VFIO_DEVICE_GET_REGION_INFO:
  450. {
  451. struct vfio_region_info info;
  452. u16 cap_type_id = 0;
  453. void *cap_type = NULL;
  454. minsz = offsetofend(struct vfio_region_info, offset);
  455. if (copy_from_user(&info, (void __user *)arg, minsz))
  456. return -EFAULT;
  457. if (info.argsz < minsz)
  458. return -EINVAL;
  459. ret = mdpy_get_region_info(mdev_state, &info, &cap_type_id,
  460. &cap_type);
  461. if (ret)
  462. return ret;
  463. if (copy_to_user((void __user *)arg, &info, minsz))
  464. return -EFAULT;
  465. return 0;
  466. }
  467. case VFIO_DEVICE_GET_IRQ_INFO:
  468. {
  469. struct vfio_irq_info info;
  470. minsz = offsetofend(struct vfio_irq_info, count);
  471. if (copy_from_user(&info, (void __user *)arg, minsz))
  472. return -EFAULT;
  473. if ((info.argsz < minsz) ||
  474. (info.index >= mdev_state->dev_info.num_irqs))
  475. return -EINVAL;
  476. ret = mdpy_get_irq_info(&info);
  477. if (ret)
  478. return ret;
  479. if (copy_to_user((void __user *)arg, &info, minsz))
  480. return -EFAULT;
  481. return 0;
  482. }
  483. case VFIO_DEVICE_QUERY_GFX_PLANE:
  484. {
  485. struct vfio_device_gfx_plane_info plane;
  486. minsz = offsetofend(struct vfio_device_gfx_plane_info,
  487. region_index);
  488. if (copy_from_user(&plane, (void __user *)arg, minsz))
  489. return -EFAULT;
  490. if (plane.argsz < minsz)
  491. return -EINVAL;
  492. ret = mdpy_query_gfx_plane(mdev_state, &plane);
  493. if (ret)
  494. return ret;
  495. if (copy_to_user((void __user *)arg, &plane, minsz))
  496. return -EFAULT;
  497. return 0;
  498. }
  499. case VFIO_DEVICE_SET_IRQS:
  500. return -EINVAL;
  501. case VFIO_DEVICE_RESET:
  502. return mdpy_reset(mdev_state);
  503. }
  504. return -ENOTTY;
  505. }
  506. static ssize_t
  507. resolution_show(struct device *dev, struct device_attribute *attr,
  508. char *buf)
  509. {
  510. struct mdev_state *mdev_state = dev_get_drvdata(dev);
  511. return sprintf(buf, "%dx%d\n",
  512. mdev_state->type->width,
  513. mdev_state->type->height);
  514. }
  515. static DEVICE_ATTR_RO(resolution);
  516. static struct attribute *mdev_dev_attrs[] = {
  517. &dev_attr_resolution.attr,
  518. NULL,
  519. };
  520. static const struct attribute_group mdev_dev_group = {
  521. .name = "vendor",
  522. .attrs = mdev_dev_attrs,
  523. };
  524. static const struct attribute_group *mdev_dev_groups[] = {
  525. &mdev_dev_group,
  526. NULL,
  527. };
  528. static ssize_t mdpy_show_description(struct mdev_type *mtype, char *buf)
  529. {
  530. struct mdpy_type *type = container_of(mtype, struct mdpy_type, type);
  531. return sprintf(buf, "virtual display, %dx%d framebuffer\n",
  532. type->width, type->height);
  533. }
  534. static const struct vfio_device_ops mdpy_dev_ops = {
  535. .init = mdpy_init_dev,
  536. .release = mdpy_release_dev,
  537. .read = mdpy_read,
  538. .write = mdpy_write,
  539. .ioctl = mdpy_ioctl,
  540. .mmap = mdpy_mmap,
  541. };
  542. static struct mdev_driver mdpy_driver = {
  543. .device_api = VFIO_DEVICE_API_PCI_STRING,
  544. .max_instances = 4,
  545. .driver = {
  546. .name = "mdpy",
  547. .owner = THIS_MODULE,
  548. .mod_name = KBUILD_MODNAME,
  549. .dev_groups = mdev_dev_groups,
  550. },
  551. .probe = mdpy_probe,
  552. .remove = mdpy_remove,
  553. .show_description = mdpy_show_description,
  554. };
  555. static const struct file_operations vd_fops = {
  556. .owner = THIS_MODULE,
  557. };
  558. static void mdpy_device_release(struct device *dev)
  559. {
  560. /* nothing */
  561. }
  562. static int __init mdpy_dev_init(void)
  563. {
  564. int ret = 0;
  565. ret = alloc_chrdev_region(&mdpy_devt, 0, MINORMASK + 1, MDPY_NAME);
  566. if (ret < 0) {
  567. pr_err("Error: failed to register mdpy_dev, err: %d\n", ret);
  568. return ret;
  569. }
  570. cdev_init(&mdpy_cdev, &vd_fops);
  571. cdev_add(&mdpy_cdev, mdpy_devt, MINORMASK + 1);
  572. pr_info("%s: major %d\n", __func__, MAJOR(mdpy_devt));
  573. ret = mdev_register_driver(&mdpy_driver);
  574. if (ret)
  575. goto err_cdev;
  576. mdpy_class = class_create(THIS_MODULE, MDPY_CLASS_NAME);
  577. if (IS_ERR(mdpy_class)) {
  578. pr_err("Error: failed to register mdpy_dev class\n");
  579. ret = PTR_ERR(mdpy_class);
  580. goto err_driver;
  581. }
  582. mdpy_dev.class = mdpy_class;
  583. mdpy_dev.release = mdpy_device_release;
  584. dev_set_name(&mdpy_dev, "%s", MDPY_NAME);
  585. ret = device_register(&mdpy_dev);
  586. if (ret)
  587. goto err_class;
  588. ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
  589. mdpy_mdev_types,
  590. ARRAY_SIZE(mdpy_mdev_types));
  591. if (ret)
  592. goto err_device;
  593. return 0;
  594. err_device:
  595. device_unregister(&mdpy_dev);
  596. err_class:
  597. class_destroy(mdpy_class);
  598. err_driver:
  599. mdev_unregister_driver(&mdpy_driver);
  600. err_cdev:
  601. cdev_del(&mdpy_cdev);
  602. unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
  603. return ret;
  604. }
  605. static void __exit mdpy_dev_exit(void)
  606. {
  607. mdpy_dev.bus = NULL;
  608. mdev_unregister_parent(&mdpy_parent);
  609. device_unregister(&mdpy_dev);
  610. mdev_unregister_driver(&mdpy_driver);
  611. cdev_del(&mdpy_cdev);
  612. unregister_chrdev_region(mdpy_devt, MINORMASK + 1);
  613. class_destroy(mdpy_class);
  614. mdpy_class = NULL;
  615. }
  616. module_param_named(count, mdpy_driver.max_instances, int, 0444);
  617. MODULE_PARM_DESC(count, "number of " MDPY_NAME " devices");
  618. module_init(mdpy_dev_init)
  619. module_exit(mdpy_dev_exit)