drm_gem_framebuffer_helper.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * drm gem framebuffer helper functions
  3. *
  4. * Copyright (C) 2017 Noralf Trønnes
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/dma-buf.h>
  12. #include <linux/dma-fence.h>
  13. #include <linux/reservation.h>
  14. #include <linux/slab.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_uapi.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_fourcc.h>
  20. #include <drm/drm_framebuffer.h>
  21. #include <drm/drm_gem.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include <drm/drm_modeset_helper.h>
  24. #include <drm/drm_simple_kms_helper.h>
  25. /**
  26. * DOC: overview
  27. *
  28. * This library provides helpers for drivers that don't subclass
  29. * &drm_framebuffer and use &drm_gem_object for their backing storage.
  30. *
  31. * Drivers without additional needs to validate framebuffers can simply use
  32. * drm_gem_fb_create() and everything is wired up automatically. Other drivers
  33. * can use all parts independently.
  34. */
  35. /**
  36. * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
  37. * @fb: Framebuffer
  38. * @plane: Plane index
  39. *
  40. * No additional reference is taken beyond the one that the &drm_frambuffer
  41. * already holds.
  42. *
  43. * Returns:
  44. * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
  45. * if it does not exist.
  46. */
  47. struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
  48. unsigned int plane)
  49. {
  50. if (plane >= 4)
  51. return NULL;
  52. return fb->obj[plane];
  53. }
  54. EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
  55. static struct drm_framebuffer *
  56. drm_gem_fb_alloc(struct drm_device *dev,
  57. const struct drm_mode_fb_cmd2 *mode_cmd,
  58. struct drm_gem_object **obj, unsigned int num_planes,
  59. const struct drm_framebuffer_funcs *funcs)
  60. {
  61. struct drm_framebuffer *fb;
  62. int ret, i;
  63. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  64. if (!fb)
  65. return ERR_PTR(-ENOMEM);
  66. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  67. for (i = 0; i < num_planes; i++)
  68. fb->obj[i] = obj[i];
  69. ret = drm_framebuffer_init(dev, fb, funcs);
  70. if (ret) {
  71. DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n",
  72. ret);
  73. kfree(fb);
  74. return ERR_PTR(ret);
  75. }
  76. return fb;
  77. }
  78. /**
  79. * drm_gem_fb_destroy - Free GEM backed framebuffer
  80. * @fb: Framebuffer
  81. *
  82. * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
  83. * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
  84. * callback.
  85. */
  86. void drm_gem_fb_destroy(struct drm_framebuffer *fb)
  87. {
  88. int i;
  89. for (i = 0; i < 4; i++)
  90. drm_gem_object_put_unlocked(fb->obj[i]);
  91. drm_framebuffer_cleanup(fb);
  92. kfree(fb);
  93. }
  94. EXPORT_SYMBOL(drm_gem_fb_destroy);
  95. /**
  96. * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
  97. * @fb: Framebuffer
  98. * @file: DRM file to register the handle for
  99. * @handle: Pointer to return the created handle
  100. *
  101. * This function creates a handle for the GEM object backing the framebuffer.
  102. * Drivers can use this as their &drm_framebuffer_funcs->create_handle
  103. * callback. The GETFB IOCTL calls into this callback.
  104. *
  105. * Returns:
  106. * 0 on success or a negative error code on failure.
  107. */
  108. int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
  109. unsigned int *handle)
  110. {
  111. return drm_gem_handle_create(file, fb->obj[0], handle);
  112. }
  113. EXPORT_SYMBOL(drm_gem_fb_create_handle);
  114. /**
  115. * drm_gem_fb_create_with_funcs() - Helper function for the
  116. * &drm_mode_config_funcs.fb_create
  117. * callback
  118. * @dev: DRM device
  119. * @file: DRM file that holds the GEM handle(s) backing the framebuffer
  120. * @mode_cmd: Metadata from the userspace framebuffer creation request
  121. * @funcs: vtable to be used for the new framebuffer object
  122. *
  123. * This can be used to set &drm_framebuffer_funcs for drivers that need the
  124. * &drm_framebuffer_funcs.dirty callback. Use drm_gem_fb_create() if you don't
  125. * need to change &drm_framebuffer_funcs.
  126. * The function does buffer size validation.
  127. *
  128. * Returns:
  129. * Pointer to a &drm_framebuffer on success or an error pointer on failure.
  130. */
  131. struct drm_framebuffer *
  132. drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
  133. const struct drm_mode_fb_cmd2 *mode_cmd,
  134. const struct drm_framebuffer_funcs *funcs)
  135. {
  136. const struct drm_format_info *info;
  137. struct drm_gem_object *objs[4];
  138. struct drm_framebuffer *fb;
  139. int ret, i;
  140. info = drm_get_format_info(dev, mode_cmd);
  141. if (!info)
  142. return ERR_PTR(-EINVAL);
  143. for (i = 0; i < info->num_planes; i++) {
  144. unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
  145. unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
  146. unsigned int min_size;
  147. objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  148. if (!objs[i]) {
  149. DRM_DEBUG_KMS("Failed to lookup GEM object\n");
  150. ret = -ENOENT;
  151. goto err_gem_object_put;
  152. }
  153. min_size = (height - 1) * mode_cmd->pitches[i]
  154. + width * info->cpp[i]
  155. + mode_cmd->offsets[i];
  156. if (objs[i]->size < min_size) {
  157. drm_gem_object_put_unlocked(objs[i]);
  158. ret = -EINVAL;
  159. goto err_gem_object_put;
  160. }
  161. }
  162. fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs);
  163. if (IS_ERR(fb)) {
  164. ret = PTR_ERR(fb);
  165. goto err_gem_object_put;
  166. }
  167. return fb;
  168. err_gem_object_put:
  169. for (i--; i >= 0; i--)
  170. drm_gem_object_put_unlocked(objs[i]);
  171. return ERR_PTR(ret);
  172. }
  173. EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
  174. static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
  175. .destroy = drm_gem_fb_destroy,
  176. .create_handle = drm_gem_fb_create_handle,
  177. };
  178. /**
  179. * drm_gem_fb_create() - Helper function for the
  180. * &drm_mode_config_funcs.fb_create callback
  181. * @dev: DRM device
  182. * @file: DRM file that holds the GEM handle(s) backing the framebuffer
  183. * @mode_cmd: Metadata from the userspace framebuffer creation request
  184. *
  185. * This function creates a new framebuffer object described by
  186. * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
  187. * backing the framebuffer.
  188. *
  189. * If your hardware has special alignment or pitch requirements these should be
  190. * checked before calling this function. The function does buffer size
  191. * validation. Use drm_gem_fb_create_with_funcs() if you need to set
  192. * &drm_framebuffer_funcs.dirty.
  193. *
  194. * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
  195. * The ADDFB2 IOCTL calls into this callback.
  196. *
  197. * Returns:
  198. * Pointer to a &drm_framebuffer on success or an error pointer on failure.
  199. */
  200. struct drm_framebuffer *
  201. drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
  202. const struct drm_mode_fb_cmd2 *mode_cmd)
  203. {
  204. return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
  205. &drm_gem_fb_funcs);
  206. }
  207. EXPORT_SYMBOL_GPL(drm_gem_fb_create);
  208. /**
  209. * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
  210. * @plane: Plane
  211. * @state: Plane state the fence will be attached to
  212. *
  213. * This function prepares a GEM backed framebuffer for scanout by checking if
  214. * the plane framebuffer has a DMA-BUF attached. If it does, it extracts the
  215. * exclusive fence and attaches it to the plane state for the atomic helper to
  216. * wait on. This function can be used as the &drm_plane_helper_funcs.prepare_fb
  217. * callback.
  218. *
  219. * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
  220. * gem based framebuffer drivers which have their buffers always pinned in
  221. * memory.
  222. */
  223. int drm_gem_fb_prepare_fb(struct drm_plane *plane,
  224. struct drm_plane_state *state)
  225. {
  226. struct dma_buf *dma_buf;
  227. struct dma_fence *fence;
  228. if (!state->fb)
  229. return 0;
  230. dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf;
  231. if (dma_buf) {
  232. fence = reservation_object_get_excl_rcu(dma_buf->resv);
  233. drm_atomic_set_fence_for_plane(state, fence);
  234. }
  235. return 0;
  236. }
  237. EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
  238. /**
  239. * drm_gem_fb_simple_display_pipe_prepare_fb - prepare_fb helper for
  240. * &drm_simple_display_pipe
  241. * @pipe: Simple display pipe
  242. * @plane_state: Plane state
  243. *
  244. * This function uses drm_gem_fb_prepare_fb() to check if the plane FB has a
  245. * &dma_buf attached, extracts the exclusive fence and attaches it to plane
  246. * state for the atomic helper to wait on. Drivers can use this as their
  247. * &drm_simple_display_pipe_funcs.prepare_fb callback.
  248. */
  249. int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
  250. struct drm_plane_state *plane_state)
  251. {
  252. return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
  253. }
  254. EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb);
  255. /**
  256. * drm_gem_fbdev_fb_create - Create a GEM backed &drm_framebuffer for fbdev
  257. * emulation
  258. * @dev: DRM device
  259. * @sizes: fbdev size description
  260. * @pitch_align: Optional pitch alignment
  261. * @obj: GEM object backing the framebuffer
  262. * @funcs: Optional vtable to be used for the new framebuffer object when the
  263. * dirty callback is needed.
  264. *
  265. * This function creates a framebuffer from a &drm_fb_helper_surface_size
  266. * description for use in the &drm_fb_helper_funcs.fb_probe callback.
  267. *
  268. * Returns:
  269. * Pointer to a &drm_framebuffer on success or an error pointer on failure.
  270. */
  271. struct drm_framebuffer *
  272. drm_gem_fbdev_fb_create(struct drm_device *dev,
  273. struct drm_fb_helper_surface_size *sizes,
  274. unsigned int pitch_align, struct drm_gem_object *obj,
  275. const struct drm_framebuffer_funcs *funcs)
  276. {
  277. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  278. mode_cmd.width = sizes->surface_width;
  279. mode_cmd.height = sizes->surface_height;
  280. mode_cmd.pitches[0] = sizes->surface_width *
  281. DIV_ROUND_UP(sizes->surface_bpp, 8);
  282. if (pitch_align)
  283. mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0],
  284. pitch_align);
  285. mode_cmd.pixel_format = drm_driver_legacy_fb_format(dev, sizes->surface_bpp,
  286. sizes->surface_depth);
  287. if (obj->size < mode_cmd.pitches[0] * mode_cmd.height)
  288. return ERR_PTR(-EINVAL);
  289. if (!funcs)
  290. funcs = &drm_gem_fb_funcs;
  291. return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
  292. }
  293. EXPORT_SYMBOL(drm_gem_fbdev_fb_create);