PageRenderTime 989ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpu/drm/virtio/virtgpu_fb.c

https://github.com/huangrui/linux
C | 416 lines | 329 code | 54 blank | 33 comment | 21 complexity | 54d6139f4034baedf1c20a69b22fb3b5 MD5 | raw file
  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.bits_per_pixel / 8;
  43. int x2, y2;
  44. unsigned long flags;
  45. struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(fb->obj);
  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 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->obj);
  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. .fb_check_var = drm_fb_helper_check_var,
  178. .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
  179. .fb_fillrect = virtio_gpu_3d_fillrect,
  180. .fb_copyarea = virtio_gpu_3d_copyarea,
  181. .fb_imageblit = virtio_gpu_3d_imageblit,
  182. .fb_pan_display = drm_fb_helper_pan_display,
  183. .fb_blank = drm_fb_helper_blank,
  184. .fb_setcmap = drm_fb_helper_setcmap,
  185. .fb_debug_enter = drm_fb_helper_debug_enter,
  186. .fb_debug_leave = drm_fb_helper_debug_leave,
  187. };
  188. static int virtio_gpu_vmap_fb(struct virtio_gpu_device *vgdev,
  189. struct virtio_gpu_object *obj)
  190. {
  191. return virtio_gpu_object_kmap(obj, NULL);
  192. }
  193. static int virtio_gpufb_create(struct drm_fb_helper *helper,
  194. struct drm_fb_helper_surface_size *sizes)
  195. {
  196. struct virtio_gpu_fbdev *vfbdev =
  197. container_of(helper, struct virtio_gpu_fbdev, helper);
  198. struct drm_device *dev = helper->dev;
  199. struct virtio_gpu_device *vgdev = dev->dev_private;
  200. struct fb_info *info;
  201. struct drm_framebuffer *fb;
  202. struct drm_mode_fb_cmd2 mode_cmd = {};
  203. struct virtio_gpu_object *obj;
  204. uint32_t resid, format, size;
  205. int ret;
  206. mode_cmd.width = sizes->surface_width;
  207. mode_cmd.height = sizes->surface_height;
  208. mode_cmd.pitches[0] = mode_cmd.width * 4;
  209. mode_cmd.pixel_format = drm_mode_legacy_fb_format(32, 24);
  210. switch (mode_cmd.pixel_format) {
  211. #ifdef __BIG_ENDIAN
  212. case DRM_FORMAT_XRGB8888:
  213. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  214. break;
  215. case DRM_FORMAT_ARGB8888:
  216. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  217. break;
  218. case DRM_FORMAT_BGRX8888:
  219. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  220. break;
  221. case DRM_FORMAT_BGRA8888:
  222. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  223. break;
  224. case DRM_FORMAT_RGBX8888:
  225. format = VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM;
  226. break;
  227. case DRM_FORMAT_RGBA8888:
  228. format = VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM;
  229. break;
  230. case DRM_FORMAT_XBGR8888:
  231. format = VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM;
  232. break;
  233. case DRM_FORMAT_ABGR8888:
  234. format = VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM;
  235. break;
  236. #else
  237. case DRM_FORMAT_XRGB8888:
  238. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  239. break;
  240. case DRM_FORMAT_ARGB8888:
  241. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  242. break;
  243. case DRM_FORMAT_BGRX8888:
  244. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  245. break;
  246. case DRM_FORMAT_BGRA8888:
  247. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  248. break;
  249. case DRM_FORMAT_RGBX8888:
  250. format = VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM;
  251. break;
  252. case DRM_FORMAT_RGBA8888:
  253. format = VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM;
  254. break;
  255. case DRM_FORMAT_XBGR8888:
  256. format = VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM;
  257. break;
  258. case DRM_FORMAT_ABGR8888:
  259. format = VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM;
  260. break;
  261. #endif
  262. default:
  263. DRM_ERROR("failed to find virtio gpu format for %d\n",
  264. mode_cmd.pixel_format);
  265. return -EINVAL;
  266. }
  267. size = mode_cmd.pitches[0] * mode_cmd.height;
  268. obj = virtio_gpu_alloc_object(dev, size, false, true);
  269. if (IS_ERR(obj))
  270. return PTR_ERR(obj);
  271. virtio_gpu_resource_id_get(vgdev, &resid);
  272. virtio_gpu_cmd_create_resource(vgdev, resid, format,
  273. mode_cmd.width, mode_cmd.height);
  274. ret = virtio_gpu_vmap_fb(vgdev, obj);
  275. if (ret) {
  276. DRM_ERROR("failed to vmap fb %d\n", ret);
  277. goto err_obj_vmap;
  278. }
  279. /* attach the object to the resource */
  280. ret = virtio_gpu_object_attach(vgdev, obj, resid, NULL);
  281. if (ret)
  282. goto err_obj_attach;
  283. info = drm_fb_helper_alloc_fbi(helper);
  284. if (IS_ERR(info)) {
  285. ret = PTR_ERR(info);
  286. goto err_fb_alloc;
  287. }
  288. info->par = helper;
  289. ret = virtio_gpu_framebuffer_init(dev, &vfbdev->vgfb,
  290. &mode_cmd, &obj->gem_base);
  291. if (ret)
  292. goto err_fb_init;
  293. fb = &vfbdev->vgfb.base;
  294. vfbdev->helper.fb = fb;
  295. strcpy(info->fix.id, "virtiodrmfb");
  296. info->flags = FBINFO_DEFAULT;
  297. info->fbops = &virtio_gpufb_ops;
  298. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  299. info->screen_base = obj->vmap;
  300. info->screen_size = obj->gem_base.size;
  301. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  302. drm_fb_helper_fill_var(info, &vfbdev->helper,
  303. sizes->fb_width, sizes->fb_height);
  304. info->fix.mmio_start = 0;
  305. info->fix.mmio_len = 0;
  306. return 0;
  307. err_fb_init:
  308. drm_fb_helper_release_fbi(helper);
  309. err_fb_alloc:
  310. virtio_gpu_cmd_resource_inval_backing(vgdev, resid);
  311. err_obj_attach:
  312. err_obj_vmap:
  313. virtio_gpu_gem_free_object(&obj->gem_base);
  314. return ret;
  315. }
  316. static int virtio_gpu_fbdev_destroy(struct drm_device *dev,
  317. struct virtio_gpu_fbdev *vgfbdev)
  318. {
  319. struct virtio_gpu_framebuffer *vgfb = &vgfbdev->vgfb;
  320. drm_fb_helper_unregister_fbi(&vgfbdev->helper);
  321. drm_fb_helper_release_fbi(&vgfbdev->helper);
  322. if (vgfb->obj)
  323. vgfb->obj = NULL;
  324. drm_fb_helper_fini(&vgfbdev->helper);
  325. drm_framebuffer_cleanup(&vgfb->base);
  326. return 0;
  327. }
  328. static struct drm_fb_helper_funcs virtio_gpu_fb_helper_funcs = {
  329. .fb_probe = virtio_gpufb_create,
  330. };
  331. int virtio_gpu_fbdev_init(struct virtio_gpu_device *vgdev)
  332. {
  333. struct virtio_gpu_fbdev *vgfbdev;
  334. int bpp_sel = 32; /* TODO: parameter from somewhere? */
  335. int ret;
  336. vgfbdev = kzalloc(sizeof(struct virtio_gpu_fbdev), GFP_KERNEL);
  337. if (!vgfbdev)
  338. return -ENOMEM;
  339. vgfbdev->vgdev = vgdev;
  340. vgdev->vgfbdev = vgfbdev;
  341. INIT_DELAYED_WORK(&vgfbdev->work, virtio_gpu_fb_dirty_work);
  342. drm_fb_helper_prepare(vgdev->ddev, &vgfbdev->helper,
  343. &virtio_gpu_fb_helper_funcs);
  344. ret = drm_fb_helper_init(vgdev->ddev, &vgfbdev->helper,
  345. vgdev->num_scanouts,
  346. VIRTIO_GPUFB_CONN_LIMIT);
  347. if (ret) {
  348. kfree(vgfbdev);
  349. return ret;
  350. }
  351. drm_fb_helper_single_add_all_connectors(&vgfbdev->helper);
  352. drm_fb_helper_initial_config(&vgfbdev->helper, bpp_sel);
  353. return 0;
  354. }
  355. void virtio_gpu_fbdev_fini(struct virtio_gpu_device *vgdev)
  356. {
  357. if (!vgdev->vgfbdev)
  358. return;
  359. virtio_gpu_fbdev_destroy(vgdev->ddev, vgdev->vgfbdev);
  360. kfree(vgdev->vgfbdev);
  361. vgdev->vgfbdev = NULL;
  362. }