mdpy-fb.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Framebuffer driver for mdpy (mediated virtual pci display device).
  4. *
  5. * See mdpy-defs.h for device specs
  6. *
  7. * (c) Gerd Hoffmann <kraxel@redhat.com>
  8. *
  9. * Using some code snippets from simplefb and cirrusfb.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/fb.h>
  22. #include <linux/io.h>
  23. #include <linux/pci.h>
  24. #include <linux/module.h>
  25. #include <drm/drm_fourcc.h>
  26. #include "mdpy-defs.h"
  27. static const struct fb_fix_screeninfo mdpy_fb_fix = {
  28. .id = "mdpy-fb",
  29. .type = FB_TYPE_PACKED_PIXELS,
  30. .visual = FB_VISUAL_TRUECOLOR,
  31. .accel = FB_ACCEL_NONE,
  32. };
  33. static const struct fb_var_screeninfo mdpy_fb_var = {
  34. .height = -1,
  35. .width = -1,
  36. .activate = FB_ACTIVATE_NOW,
  37. .vmode = FB_VMODE_NONINTERLACED,
  38. .bits_per_pixel = 32,
  39. .transp.offset = 24,
  40. .red.offset = 16,
  41. .green.offset = 8,
  42. .blue.offset = 0,
  43. .transp.length = 8,
  44. .red.length = 8,
  45. .green.length = 8,
  46. .blue.length = 8,
  47. };
  48. #define PSEUDO_PALETTE_SIZE 16
  49. struct mdpy_fb_par {
  50. u32 palette[PSEUDO_PALETTE_SIZE];
  51. };
  52. static int mdpy_fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  53. u_int transp, struct fb_info *info)
  54. {
  55. u32 *pal = info->pseudo_palette;
  56. u32 cr = red >> (16 - info->var.red.length);
  57. u32 cg = green >> (16 - info->var.green.length);
  58. u32 cb = blue >> (16 - info->var.blue.length);
  59. u32 value, mask;
  60. if (regno >= PSEUDO_PALETTE_SIZE)
  61. return -EINVAL;
  62. value = (cr << info->var.red.offset) |
  63. (cg << info->var.green.offset) |
  64. (cb << info->var.blue.offset);
  65. if (info->var.transp.length > 0) {
  66. mask = (1 << info->var.transp.length) - 1;
  67. mask <<= info->var.transp.offset;
  68. value |= mask;
  69. }
  70. pal[regno] = value;
  71. return 0;
  72. }
  73. static void mdpy_fb_destroy(struct fb_info *info)
  74. {
  75. if (info->screen_base)
  76. iounmap(info->screen_base);
  77. }
  78. static struct fb_ops mdpy_fb_ops = {
  79. .owner = THIS_MODULE,
  80. .fb_destroy = mdpy_fb_destroy,
  81. .fb_setcolreg = mdpy_fb_setcolreg,
  82. .fb_fillrect = cfb_fillrect,
  83. .fb_copyarea = cfb_copyarea,
  84. .fb_imageblit = cfb_imageblit,
  85. };
  86. static int mdpy_fb_probe(struct pci_dev *pdev,
  87. const struct pci_device_id *ent)
  88. {
  89. struct fb_info *info;
  90. struct mdpy_fb_par *par;
  91. u32 format, width, height;
  92. int ret;
  93. ret = pci_enable_device(pdev);
  94. if (ret < 0)
  95. return ret;
  96. ret = pci_request_regions(pdev, "mdpy-fb");
  97. if (ret < 0)
  98. return ret;
  99. pci_read_config_dword(pdev, MDPY_FORMAT_OFFSET, &format);
  100. pci_read_config_dword(pdev, MDPY_WIDTH_OFFSET, &width);
  101. pci_read_config_dword(pdev, MDPY_HEIGHT_OFFSET, &height);
  102. if (format != DRM_FORMAT_XRGB8888) {
  103. pci_err(pdev, "format mismatch (0x%x != 0x%x)\n",
  104. format, DRM_FORMAT_XRGB8888);
  105. return -EINVAL;
  106. }
  107. if (width < 100 || width > 10000) {
  108. pci_err(pdev, "width (%d) out of range\n", width);
  109. return -EINVAL;
  110. }
  111. if (height < 100 || height > 10000) {
  112. pci_err(pdev, "height (%d) out of range\n", height);
  113. return -EINVAL;
  114. }
  115. pci_info(pdev, "mdpy found: %dx%d framebuffer\n",
  116. width, height);
  117. info = framebuffer_alloc(sizeof(struct mdpy_fb_par), &pdev->dev);
  118. if (!info)
  119. goto err_release_regions;
  120. pci_set_drvdata(pdev, info);
  121. par = info->par;
  122. info->fix = mdpy_fb_fix;
  123. info->fix.smem_start = pci_resource_start(pdev, 0);
  124. info->fix.smem_len = pci_resource_len(pdev, 0);
  125. info->fix.line_length = width * 4;
  126. info->var = mdpy_fb_var;
  127. info->var.xres = width;
  128. info->var.yres = height;
  129. info->var.xres_virtual = width;
  130. info->var.yres_virtual = height;
  131. info->screen_size = info->fix.smem_len;
  132. info->screen_base = ioremap(info->fix.smem_start,
  133. info->screen_size);
  134. if (!info->screen_base) {
  135. pci_err(pdev, "ioremap(pcibar) failed\n");
  136. ret = -EIO;
  137. goto err_release_fb;
  138. }
  139. info->apertures = alloc_apertures(1);
  140. if (!info->apertures) {
  141. ret = -ENOMEM;
  142. goto err_unmap;
  143. }
  144. info->apertures->ranges[0].base = info->fix.smem_start;
  145. info->apertures->ranges[0].size = info->fix.smem_len;
  146. info->fbops = &mdpy_fb_ops;
  147. info->flags = FBINFO_DEFAULT;
  148. info->pseudo_palette = par->palette;
  149. ret = register_framebuffer(info);
  150. if (ret < 0) {
  151. pci_err(pdev, "mdpy-fb device register failed: %d\n", ret);
  152. goto err_unmap;
  153. }
  154. pci_info(pdev, "fb%d registered\n", info->node);
  155. return 0;
  156. err_unmap:
  157. iounmap(info->screen_base);
  158. err_release_fb:
  159. framebuffer_release(info);
  160. err_release_regions:
  161. pci_release_regions(pdev);
  162. return ret;
  163. }
  164. static void mdpy_fb_remove(struct pci_dev *pdev)
  165. {
  166. struct fb_info *info = pci_get_drvdata(pdev);
  167. unregister_framebuffer(info);
  168. framebuffer_release(info);
  169. }
  170. static struct pci_device_id mdpy_fb_pci_table[] = {
  171. {
  172. .vendor = MDPY_PCI_VENDOR_ID,
  173. .device = MDPY_PCI_DEVICE_ID,
  174. .subvendor = MDPY_PCI_SUBVENDOR_ID,
  175. .subdevice = MDPY_PCI_SUBDEVICE_ID,
  176. }, {
  177. /* end of list */
  178. }
  179. };
  180. static struct pci_driver mdpy_fb_pci_driver = {
  181. .name = "mdpy-fb",
  182. .id_table = mdpy_fb_pci_table,
  183. .probe = mdpy_fb_probe,
  184. .remove = mdpy_fb_remove,
  185. };
  186. static int __init mdpy_fb_init(void)
  187. {
  188. int ret;
  189. ret = pci_register_driver(&mdpy_fb_pci_driver);
  190. if (ret)
  191. return ret;
  192. return 0;
  193. }
  194. module_init(mdpy_fb_init);
  195. MODULE_DEVICE_TABLE(pci, mdpy_fb_pci_table);
  196. MODULE_LICENSE("GPL v2");