virtgpu_fb.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/drm_fb_helper.h>
  27. #include "virtgpu_drv.h"
  28. #define VIRTIO_GPU_FBCON_POLL_PERIOD (HZ / 60)
  29. struct virtio_gpu_fbdev {
  30. struct drm_fb_helper helper;
  31. struct virtio_gpu_framebuffer vgfb;
  32. struct virtio_gpu_device *vgdev;
  33. struct delayed_work work;
  34. };
  35. static int virtio_gpu_dirty_update(struct virtio_gpu_framebuffer *fb,
  36. bool store, int x, int y,
  37. int width, int height)
  38. {
  39. struct drm_device *dev = fb->base.dev;
  40. struct virtio_gpu_device *vgdev = dev->dev_private;
  41. bool store_for_later = false;
  42. int bpp = fb->base.format->cpp[0];
  43. int x2, y2;
  44. unsigned long flags;
  45. struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(fb->base.obj[0]);
  46. if ((width <= 0) ||
  47. (x + width > fb->base.width) ||
  48. (y + height > fb->base.height)) {
  49. DRM_DEBUG("values out of range %dx%d+%d+%d, fb %dx%d\n",
  50. width, height, x, y,
  51. fb->base.width, fb->base.height);
  52. return -EINVAL;
  53. }
  54. /*
  55. * Can be called with pretty much any context (console output
  56. * path). If we are in atomic just store the dirty rect info
  57. * to send out the update later.
  58. *
  59. * Can't test inside spin lock.
  60. */
  61. if (in_atomic() || store)
  62. store_for_later = true;
  63. x2 = x + width - 1;
  64. y2 = y + height - 1;
  65. spin_lock_irqsave(&fb->dirty_lock, flags);
  66. if (fb->y1 < y)
  67. y = fb->y1;
  68. if (fb->y2 > y2)
  69. y2 = fb->y2;
  70. if (fb->x1 < x)
  71. x = fb->x1;
  72. if (fb->x2 > x2)
  73. x2 = fb->x2;
  74. if (store_for_later) {
  75. fb->x1 = x;
  76. fb->x2 = x2;
  77. fb->y1 = y;
  78. fb->y2 = y2;
  79. spin_unlock_irqrestore(&fb->dirty_lock, flags);
  80. return 0;
  81. }
  82. fb->x1 = fb->y1 = INT_MAX;
  83. fb->x2 = fb->y2 = 0;
  84. spin_unlock_irqrestore(&fb->dirty_lock, flags);
  85. {
  86. uint32_t offset;
  87. uint32_t w = x2 - x + 1;
  88. uint32_t h = y2 - y + 1;
  89. offset = (y * fb->base.pitches[0]) + x * bpp;
  90. virtio_gpu_cmd_transfer_to_host_2d(vgdev, obj->hw_res_handle,
  91. offset,
  92. cpu_to_le32(w),
  93. cpu_to_le32(h),
  94. cpu_to_le32(x),
  95. cpu_to_le32(y),
  96. NULL);
  97. }
  98. virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
  99. x, y, x2 - x + 1, y2 - y + 1);
  100. return 0;
  101. }
  102. int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer *vgfb,
  103. struct drm_clip_rect *clips,
  104. unsigned int num_clips)
  105. {
  106. struct virtio_gpu_device *vgdev = vgfb->base.dev->dev_private;
  107. struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
  108. struct drm_clip_rect norect;
  109. struct drm_clip_rect *clips_ptr;
  110. int left, right, top, bottom;
  111. int i;
  112. int inc = 1;
  113. if (!num_clips) {
  114. num_clips = 1;
  115. clips = &norect;
  116. norect.x1 = norect.y1 = 0;
  117. norect.x2 = vgfb->base.width;
  118. norect.y2 = vgfb->base.height;
  119. }
  120. left = clips->x1;
  121. right = clips->x2;
  122. top = clips->y1;
  123. bottom = clips->y2;
  124. /* skip the first clip rect */
  125. for (i = 1, clips_ptr = clips + inc;
  126. i < num_clips; i++, clips_ptr += inc) {
  127. left = min_t(int, left, (int)clips_ptr->x1);
  128. right = max_t(int, right, (int)clips_ptr->x2);
  129. top = min_t(int, top, (int)clips_ptr->y1);
  130. bottom = max_t(int, bottom, (int)clips_ptr->y2);
  131. }
  132. if (obj->dumb)
  133. return virtio_gpu_dirty_update(vgfb, false, left, top,
  134. right - left, bottom - top);
  135. virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
  136. left, top, right - left, bottom - top);
  137. return 0;
  138. }
  139. static void virtio_gpu_fb_dirty_work(struct work_struct *work)
  140. {
  141. struct delayed_work *delayed_work = to_delayed_work(work);
  142. struct virtio_gpu_fbdev *vfbdev =
  143. container_of(delayed_work, struct virtio_gpu_fbdev, work);
  144. struct virtio_gpu_framebuffer *vgfb = &vfbdev->vgfb;
  145. virtio_gpu_dirty_update(&vfbdev->vgfb, false, vgfb->x1, vgfb->y1,
  146. vgfb->x2 - vgfb->x1, vgfb->y2 - vgfb->y1);
  147. }
  148. static void virtio_gpu_3d_fillrect(struct fb_info *info,
  149. const struct fb_fillrect *rect)
  150. {
  151. struct virtio_gpu_fbdev *vfbdev = info->par;
  152. drm_fb_helper_sys_fillrect(info, rect);
  153. virtio_gpu_dirty_update(&vfbdev->vgfb, true, rect->dx, rect->dy,
  154. rect->width, rect->height);
  155. schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
  156. }
  157. static void virtio_gpu_3d_copyarea(struct fb_info *info,
  158. const struct fb_copyarea *area)
  159. {
  160. struct virtio_gpu_fbdev *vfbdev = info->par;
  161. drm_fb_helper_sys_copyarea(info, area);
  162. virtio_gpu_dirty_update(&vfbdev->vgfb, true, area->dx, area->dy,
  163. area->width, area->height);
  164. schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
  165. }
  166. static void virtio_gpu_3d_imageblit(struct fb_info *info,
  167. const struct fb_image *image)
  168. {
  169. struct virtio_gpu_fbdev *vfbdev = info->par;
  170. drm_fb_helper_sys_imageblit(info, image);
  171. virtio_gpu_dirty_update(&vfbdev->vgfb, true, image->dx, image->dy,
  172. image->width, image->height);
  173. schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
  174. }
  175. static struct fb_ops virtio_gpufb_ops = {
  176. .owner = THIS_MODULE,
  177. DRM_FB_HELPER_DEFAULT_OPS,
  178. .fb_fillrect = virtio_gpu_3d_fillrect,
  179. .fb_copyarea = virtio_gpu_3d_copyarea,
  180. .fb_imageblit = virtio_gpu_3d_imageblit,
  181. };
  182. static int virtio_gpu_vmap_fb(struct virtio_gpu_device *vgdev,
  183. struct virtio_gpu_object *obj)
  184. {
  185. return virtio_gpu_object_kmap(obj, NULL);
  186. }
  187. static int virtio_gpufb_create(struct drm_fb_helper *helper,
  188. struct drm_fb_helper_surface_size *sizes)
  189. {
  190. struct virtio_gpu_fbdev *vfbdev =
  191. container_of(helper, struct virtio_gpu_fbdev, helper);
  192. struct drm_device *dev = helper->dev;
  193. struct virtio_gpu_device *vgdev = dev->dev_private;
  194. struct fb_info *info;
  195. struct drm_framebuffer *fb;
  196. struct drm_mode_fb_cmd2 mode_cmd = {};
  197. struct virtio_gpu_object *obj;
  198. uint32_t resid, format, size;
  199. int ret;
  200. mode_cmd.width = sizes->surface_width;
  201. mode_cmd.height = sizes->surface_height;
  202. mode_cmd.pitches[0] = mode_cmd.width * 4;
  203. mode_cmd.pixel_format = drm_mode_legacy_fb_format(32, 24);
  204. format = virtio_gpu_translate_format(mode_cmd.pixel_format);
  205. if (format == 0)
  206. return -EINVAL;
  207. size = mode_cmd.pitches[0] * mode_cmd.height;
  208. obj = virtio_gpu_alloc_object(dev, size, false, true);
  209. if (IS_ERR(obj))
  210. return PTR_ERR(obj);
  211. virtio_gpu_resource_id_get(vgdev, &resid);
  212. virtio_gpu_cmd_create_resource(vgdev, resid, format,
  213. mode_cmd.width, mode_cmd.height);
  214. ret = virtio_gpu_vmap_fb(vgdev, obj);
  215. if (ret) {
  216. DRM_ERROR("failed to vmap fb %d\n", ret);
  217. goto err_obj_vmap;
  218. }
  219. /* attach the object to the resource */
  220. ret = virtio_gpu_object_attach(vgdev, obj, resid, NULL);
  221. if (ret)
  222. goto err_obj_attach;
  223. info = drm_fb_helper_alloc_fbi(helper);
  224. if (IS_ERR(info)) {
  225. ret = PTR_ERR(info);
  226. goto err_fb_alloc;
  227. }
  228. info->par = helper;
  229. ret = virtio_gpu_framebuffer_init(dev, &vfbdev->vgfb,
  230. &mode_cmd, &obj->gem_base);
  231. if (ret)
  232. goto err_fb_alloc;
  233. fb = &vfbdev->vgfb.base;
  234. vfbdev->helper.fb = fb;
  235. strcpy(info->fix.id, "virtiodrmfb");
  236. info->fbops = &virtio_gpufb_ops;
  237. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  238. info->screen_buffer = obj->vmap;
  239. info->screen_size = obj->gem_base.size;
  240. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  241. drm_fb_helper_fill_var(info, &vfbdev->helper,
  242. sizes->fb_width, sizes->fb_height);
  243. info->fix.mmio_start = 0;
  244. info->fix.mmio_len = 0;
  245. return 0;
  246. err_fb_alloc:
  247. virtio_gpu_cmd_resource_inval_backing(vgdev, resid);
  248. err_obj_attach:
  249. err_obj_vmap:
  250. virtio_gpu_gem_free_object(&obj->gem_base);
  251. return ret;
  252. }
  253. static int virtio_gpu_fbdev_destroy(struct drm_device *dev,
  254. struct virtio_gpu_fbdev *vgfbdev)
  255. {
  256. struct virtio_gpu_framebuffer *vgfb = &vgfbdev->vgfb;
  257. drm_fb_helper_unregister_fbi(&vgfbdev->helper);
  258. if (vgfb->base.obj[0])
  259. vgfb->base.obj[0] = NULL;
  260. drm_fb_helper_fini(&vgfbdev->helper);
  261. drm_framebuffer_cleanup(&vgfb->base);
  262. return 0;
  263. }
  264. static const struct drm_fb_helper_funcs virtio_gpu_fb_helper_funcs = {
  265. .fb_probe = virtio_gpufb_create,
  266. };
  267. int virtio_gpu_fbdev_init(struct virtio_gpu_device *vgdev)
  268. {
  269. struct virtio_gpu_fbdev *vgfbdev;
  270. int bpp_sel = 32; /* TODO: parameter from somewhere? */
  271. int ret;
  272. vgfbdev = kzalloc(sizeof(struct virtio_gpu_fbdev), GFP_KERNEL);
  273. if (!vgfbdev)
  274. return -ENOMEM;
  275. vgfbdev->vgdev = vgdev;
  276. vgdev->vgfbdev = vgfbdev;
  277. INIT_DELAYED_WORK(&vgfbdev->work, virtio_gpu_fb_dirty_work);
  278. drm_fb_helper_prepare(vgdev->ddev, &vgfbdev->helper,
  279. &virtio_gpu_fb_helper_funcs);
  280. ret = drm_fb_helper_init(vgdev->ddev, &vgfbdev->helper,
  281. VIRTIO_GPUFB_CONN_LIMIT);
  282. if (ret) {
  283. kfree(vgfbdev);
  284. return ret;
  285. }
  286. drm_fb_helper_single_add_all_connectors(&vgfbdev->helper);
  287. drm_fb_helper_initial_config(&vgfbdev->helper, bpp_sel);
  288. return 0;
  289. }
  290. void virtio_gpu_fbdev_fini(struct virtio_gpu_device *vgdev)
  291. {
  292. if (!vgdev->vgfbdev)
  293. return;
  294. virtio_gpu_fbdev_destroy(vgdev->ddev, vgdev->vgfbdev);
  295. kfree(vgdev->vgfbdev);
  296. vgdev->vgfbdev = NULL;
  297. }