virtgpu_fb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 list_head fbdev_list;
  33. struct virtio_gpu_device *vgdev;
  34. struct delayed_work work;
  35. };
  36. static int virtio_gpu_dirty_update(struct virtio_gpu_framebuffer *fb,
  37. bool store, int x, int y,
  38. int width, int height)
  39. {
  40. struct drm_device *dev = fb->base.dev;
  41. struct virtio_gpu_device *vgdev = dev->dev_private;
  42. bool store_for_later = false;
  43. int bpp = fb->base.bits_per_pixel / 8;
  44. int x2, y2;
  45. unsigned long flags;
  46. struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(fb->obj);
  47. if ((width <= 0) ||
  48. (x + width > fb->base.width) ||
  49. (y + height > fb->base.height)) {
  50. DRM_DEBUG("values out of range %dx%d+%d+%d, fb %dx%d\n",
  51. width, height, x, y,
  52. fb->base.width, fb->base.height);
  53. return -EINVAL;
  54. }
  55. /*
  56. * Can be called with pretty much any context (console output
  57. * path). If we are in atomic just store the dirty rect info
  58. * to send out the update later.
  59. *
  60. * Can't test inside spin lock.
  61. */
  62. if (in_atomic() || store)
  63. store_for_later = true;
  64. x2 = x + width - 1;
  65. y2 = y + height - 1;
  66. spin_lock_irqsave(&fb->dirty_lock, flags);
  67. if (fb->y1 < y)
  68. y = fb->y1;
  69. if (fb->y2 > y2)
  70. y2 = fb->y2;
  71. if (fb->x1 < x)
  72. x = fb->x1;
  73. if (fb->x2 > x2)
  74. x2 = fb->x2;
  75. if (store_for_later) {
  76. fb->x1 = x;
  77. fb->x2 = x2;
  78. fb->y1 = y;
  79. fb->y2 = y2;
  80. spin_unlock_irqrestore(&fb->dirty_lock, flags);
  81. return 0;
  82. }
  83. fb->x1 = fb->y1 = INT_MAX;
  84. fb->x2 = fb->y2 = 0;
  85. spin_unlock_irqrestore(&fb->dirty_lock, flags);
  86. {
  87. uint32_t offset;
  88. uint32_t w = x2 - x + 1;
  89. uint32_t h = y2 - y + 1;
  90. offset = (y * fb->base.pitches[0]) + x * bpp;
  91. virtio_gpu_cmd_transfer_to_host_2d(vgdev, obj->hw_res_handle,
  92. offset,
  93. cpu_to_le32(w),
  94. cpu_to_le32(h),
  95. cpu_to_le32(x),
  96. cpu_to_le32(y),
  97. NULL);
  98. }
  99. virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
  100. x, y, x2 - x + 1, y2 - y + 1);
  101. return 0;
  102. }
  103. int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer *vgfb,
  104. struct drm_clip_rect *clips,
  105. unsigned num_clips)
  106. {
  107. struct virtio_gpu_device *vgdev = vgfb->base.dev->dev_private;
  108. struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(vgfb->obj);
  109. struct drm_clip_rect norect;
  110. struct drm_clip_rect *clips_ptr;
  111. int left, right, top, bottom;
  112. int i;
  113. int inc = 1;
  114. if (!num_clips) {
  115. num_clips = 1;
  116. clips = &norect;
  117. norect.x1 = norect.y1 = 0;
  118. norect.x2 = vgfb->base.width;
  119. norect.y2 = vgfb->base.height;
  120. }
  121. left = clips->x1;
  122. right = clips->x2;
  123. top = clips->y1;
  124. bottom = clips->y2;
  125. /* skip the first clip rect */
  126. for (i = 1, clips_ptr = clips + inc;
  127. i < num_clips; i++, clips_ptr += inc) {
  128. left = min_t(int, left, (int)clips_ptr->x1);
  129. right = max_t(int, right, (int)clips_ptr->x2);
  130. top = min_t(int, top, (int)clips_ptr->y1);
  131. bottom = max_t(int, bottom, (int)clips_ptr->y2);
  132. }
  133. if (obj->dumb)
  134. return virtio_gpu_dirty_update(vgfb, false, left, top,
  135. right - left, bottom - top);
  136. virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
  137. left, top, right - left, bottom - top);
  138. return 0;
  139. }
  140. static void virtio_gpu_fb_dirty_work(struct work_struct *work)
  141. {
  142. struct delayed_work *delayed_work = to_delayed_work(work);
  143. struct virtio_gpu_fbdev *vfbdev =
  144. container_of(delayed_work, struct virtio_gpu_fbdev, work);
  145. struct virtio_gpu_framebuffer *vgfb = &vfbdev->vgfb;
  146. virtio_gpu_dirty_update(&vfbdev->vgfb, false, vgfb->x1, vgfb->y1,
  147. vgfb->x2 - vgfb->x1, vgfb->y2 - vgfb->y1);
  148. }
  149. static void virtio_gpu_3d_fillrect(struct fb_info *info,
  150. const struct fb_fillrect *rect)
  151. {
  152. struct virtio_gpu_fbdev *vfbdev = info->par;
  153. drm_fb_helper_sys_fillrect(info, rect);
  154. virtio_gpu_dirty_update(&vfbdev->vgfb, true, rect->dx, rect->dy,
  155. rect->width, rect->height);
  156. schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
  157. }
  158. static void virtio_gpu_3d_copyarea(struct fb_info *info,
  159. const struct fb_copyarea *area)
  160. {
  161. struct virtio_gpu_fbdev *vfbdev = info->par;
  162. drm_fb_helper_sys_copyarea(info, area);
  163. virtio_gpu_dirty_update(&vfbdev->vgfb, true, area->dx, area->dy,
  164. area->width, area->height);
  165. schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
  166. }
  167. static void virtio_gpu_3d_imageblit(struct fb_info *info,
  168. const struct fb_image *image)
  169. {
  170. struct virtio_gpu_fbdev *vfbdev = info->par;
  171. drm_fb_helper_sys_imageblit(info, image);
  172. virtio_gpu_dirty_update(&vfbdev->vgfb, true, image->dx, image->dy,
  173. image->width, image->height);
  174. schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
  175. }
  176. static struct fb_ops virtio_gpufb_ops = {
  177. .owner = THIS_MODULE,
  178. .fb_check_var = drm_fb_helper_check_var,
  179. .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
  180. .fb_fillrect = virtio_gpu_3d_fillrect,
  181. .fb_copyarea = virtio_gpu_3d_copyarea,
  182. .fb_imageblit = virtio_gpu_3d_imageblit,
  183. .fb_pan_display = drm_fb_helper_pan_display,
  184. .fb_blank = drm_fb_helper_blank,
  185. .fb_setcmap = drm_fb_helper_setcmap,
  186. .fb_debug_enter = drm_fb_helper_debug_enter,
  187. .fb_debug_leave = drm_fb_helper_debug_leave,
  188. };
  189. static int virtio_gpu_vmap_fb(struct virtio_gpu_device *vgdev,
  190. struct virtio_gpu_object *obj)
  191. {
  192. return virtio_gpu_object_kmap(obj, NULL);
  193. }
  194. static int virtio_gpufb_create(struct drm_fb_helper *helper,
  195. struct drm_fb_helper_surface_size *sizes)
  196. {
  197. struct virtio_gpu_fbdev *vfbdev =
  198. container_of(helper, struct virtio_gpu_fbdev, helper);
  199. struct drm_device *dev = helper->dev;
  200. struct virtio_gpu_device *vgdev = dev->dev_private;
  201. struct fb_info *info;
  202. struct drm_framebuffer *fb;
  203. struct drm_mode_fb_cmd2 mode_cmd = {};
  204. struct virtio_gpu_object *obj;
  205. uint32_t resid, format, size;
  206. int ret;
  207. mode_cmd.width = sizes->surface_width;
  208. mode_cmd.height = sizes->surface_height;
  209. mode_cmd.pitches[0] = mode_cmd.width * 4;
  210. mode_cmd.pixel_format = drm_mode_legacy_fb_format(32, 24);
  211. switch (mode_cmd.pixel_format) {
  212. #ifdef __BIG_ENDIAN
  213. case DRM_FORMAT_XRGB8888:
  214. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  215. break;
  216. case DRM_FORMAT_ARGB8888:
  217. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  218. break;
  219. case DRM_FORMAT_BGRX8888:
  220. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  221. break;
  222. case DRM_FORMAT_BGRA8888:
  223. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  224. break;
  225. case DRM_FORMAT_RGBX8888:
  226. format = VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM;
  227. break;
  228. case DRM_FORMAT_RGBA8888:
  229. format = VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM;
  230. break;
  231. case DRM_FORMAT_XBGR8888:
  232. format = VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM;
  233. break;
  234. case DRM_FORMAT_ABGR8888:
  235. format = VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM;
  236. break;
  237. #else
  238. case DRM_FORMAT_XRGB8888:
  239. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  240. break;
  241. case DRM_FORMAT_ARGB8888:
  242. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  243. break;
  244. case DRM_FORMAT_BGRX8888:
  245. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  246. break;
  247. case DRM_FORMAT_BGRA8888:
  248. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  249. break;
  250. case DRM_FORMAT_RGBX8888:
  251. format = VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM;
  252. break;
  253. case DRM_FORMAT_RGBA8888:
  254. format = VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM;
  255. break;
  256. case DRM_FORMAT_XBGR8888:
  257. format = VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM;
  258. break;
  259. case DRM_FORMAT_ABGR8888:
  260. format = VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM;
  261. break;
  262. #endif
  263. default:
  264. DRM_ERROR("failed to find virtio gpu format for %d\n",
  265. mode_cmd.pixel_format);
  266. return -EINVAL;
  267. }
  268. size = mode_cmd.pitches[0] * mode_cmd.height;
  269. obj = virtio_gpu_alloc_object(dev, size, false, true);
  270. if (IS_ERR(obj))
  271. return PTR_ERR(obj);
  272. virtio_gpu_resource_id_get(vgdev, &resid);
  273. virtio_gpu_cmd_create_resource(vgdev, resid, format,
  274. mode_cmd.width, mode_cmd.height);
  275. ret = virtio_gpu_vmap_fb(vgdev, obj);
  276. if (ret) {
  277. DRM_ERROR("failed to vmap fb %d\n", ret);
  278. goto err_obj_vmap;
  279. }
  280. /* attach the object to the resource */
  281. ret = virtio_gpu_object_attach(vgdev, obj, resid, NULL);
  282. if (ret)
  283. goto err_obj_attach;
  284. info = drm_fb_helper_alloc_fbi(helper);
  285. if (IS_ERR(info)) {
  286. ret = PTR_ERR(info);
  287. goto err_fb_alloc;
  288. }
  289. info->par = helper;
  290. ret = virtio_gpu_framebuffer_init(dev, &vfbdev->vgfb,
  291. &mode_cmd, &obj->gem_base);
  292. if (ret)
  293. goto err_fb_init;
  294. fb = &vfbdev->vgfb.base;
  295. vfbdev->helper.fb = fb;
  296. strcpy(info->fix.id, "virtiodrmfb");
  297. info->flags = FBINFO_DEFAULT;
  298. info->fbops = &virtio_gpufb_ops;
  299. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  300. info->screen_base = obj->vmap;
  301. info->screen_size = obj->gem_base.size;
  302. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  303. drm_fb_helper_fill_var(info, &vfbdev->helper,
  304. sizes->fb_width, sizes->fb_height);
  305. info->fix.mmio_start = 0;
  306. info->fix.mmio_len = 0;
  307. return 0;
  308. err_fb_init:
  309. drm_fb_helper_release_fbi(helper);
  310. err_fb_alloc:
  311. virtio_gpu_cmd_resource_inval_backing(vgdev, resid);
  312. err_obj_attach:
  313. err_obj_vmap:
  314. virtio_gpu_gem_free_object(&obj->gem_base);
  315. return ret;
  316. }
  317. static int virtio_gpu_fbdev_destroy(struct drm_device *dev,
  318. struct virtio_gpu_fbdev *vgfbdev)
  319. {
  320. struct virtio_gpu_framebuffer *vgfb = &vgfbdev->vgfb;
  321. drm_fb_helper_unregister_fbi(&vgfbdev->helper);
  322. drm_fb_helper_release_fbi(&vgfbdev->helper);
  323. if (vgfb->obj)
  324. vgfb->obj = NULL;
  325. drm_fb_helper_fini(&vgfbdev->helper);
  326. drm_framebuffer_cleanup(&vgfb->base);
  327. return 0;
  328. }
  329. static struct drm_fb_helper_funcs virtio_gpu_fb_helper_funcs = {
  330. .fb_probe = virtio_gpufb_create,
  331. };
  332. int virtio_gpu_fbdev_init(struct virtio_gpu_device *vgdev)
  333. {
  334. struct virtio_gpu_fbdev *vgfbdev;
  335. int bpp_sel = 32; /* TODO: parameter from somewhere? */
  336. int ret;
  337. vgfbdev = kzalloc(sizeof(struct virtio_gpu_fbdev), GFP_KERNEL);
  338. if (!vgfbdev)
  339. return -ENOMEM;
  340. vgfbdev->vgdev = vgdev;
  341. vgdev->vgfbdev = vgfbdev;
  342. INIT_DELAYED_WORK(&vgfbdev->work, virtio_gpu_fb_dirty_work);
  343. drm_fb_helper_prepare(vgdev->ddev, &vgfbdev->helper,
  344. &virtio_gpu_fb_helper_funcs);
  345. ret = drm_fb_helper_init(vgdev->ddev, &vgfbdev->helper,
  346. vgdev->num_scanouts,
  347. VIRTIO_GPUFB_CONN_LIMIT);
  348. if (ret) {
  349. kfree(vgfbdev);
  350. return ret;
  351. }
  352. drm_fb_helper_single_add_all_connectors(&vgfbdev->helper);
  353. drm_fb_helper_initial_config(&vgfbdev->helper, bpp_sel);
  354. return 0;
  355. }
  356. void virtio_gpu_fbdev_fini(struct virtio_gpu_device *vgdev)
  357. {
  358. if (!vgdev->vgfbdev)
  359. return;
  360. virtio_gpu_fbdev_destroy(vgdev->ddev, vgdev->vgfbdev);
  361. kfree(vgdev->vgfbdev);
  362. vgdev->vgfbdev = NULL;
  363. }