drm_gem_atomic_helper.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/dma-resv.h>
  3. #include <linux/dma-fence-chain.h>
  4. #include <linux/export.h>
  5. #include <drm/drm_atomic_state_helper.h>
  6. #include <drm/drm_atomic_uapi.h>
  7. #include <drm/drm_framebuffer.h>
  8. #include <drm/drm_gem.h>
  9. #include <drm/drm_gem_atomic_helper.h>
  10. #include <drm/drm_gem_framebuffer_helper.h>
  11. #include <drm/drm_simple_kms_helper.h>
  12. #include "drm_internal.h"
  13. /**
  14. * DOC: overview
  15. *
  16. * The GEM atomic helpers library implements generic atomic-commit
  17. * functions for drivers that use GEM objects. Currently, it provides
  18. * synchronization helpers, and plane state and framebuffer BO mappings
  19. * for planes with shadow buffers.
  20. *
  21. * Before scanout, a plane's framebuffer needs to be synchronized with
  22. * possible writers that draw into the framebuffer. All drivers should
  23. * call drm_gem_plane_helper_prepare_fb() from their implementation of
  24. * struct &drm_plane_helper.prepare_fb . It sets the plane's fence from
  25. * the framebuffer so that the DRM core can synchronize access automatically.
  26. * drm_gem_plane_helper_prepare_fb() can also be used directly as
  27. * implementation of prepare_fb.
  28. *
  29. * .. code-block:: c
  30. *
  31. * #include <drm/drm_gem_atomic_helper.h>
  32. *
  33. * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
  34. * ...,
  35. * . prepare_fb = drm_gem_plane_helper_prepare_fb,
  36. * };
  37. *
  38. * A driver using a shadow buffer copies the content of the shadow buffers
  39. * into the HW's framebuffer memory during an atomic update. This requires
  40. * a mapping of the shadow buffer into kernel address space. The mappings
  41. * cannot be established by commit-tail functions, such as atomic_update,
  42. * as this would violate locking rules around dma_buf_vmap().
  43. *
  44. * The helpers for shadow-buffered planes establish and release mappings,
  45. * and provide struct drm_shadow_plane_state, which stores the plane's mapping
  46. * for commit-tail functions.
  47. *
  48. * Shadow-buffered planes can easily be enabled by using the provided macros
  49. * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS.
  50. * These macros set up the plane and plane-helper callbacks to point to the
  51. * shadow-buffer helpers.
  52. *
  53. * .. code-block:: c
  54. *
  55. * #include <drm/drm_gem_atomic_helper.h>
  56. *
  57. * struct drm_plane_funcs driver_plane_funcs = {
  58. * ...,
  59. * DRM_GEM_SHADOW_PLANE_FUNCS,
  60. * };
  61. *
  62. * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
  63. * ...,
  64. * DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  65. * };
  66. *
  67. * In the driver's atomic-update function, shadow-buffer mappings are available
  68. * from the plane state. Use to_drm_shadow_plane_state() to upcast from
  69. * struct drm_plane_state.
  70. *
  71. * .. code-block:: c
  72. *
  73. * void driver_plane_atomic_update(struct drm_plane *plane,
  74. * struct drm_plane_state *old_plane_state)
  75. * {
  76. * struct drm_plane_state *plane_state = plane->state;
  77. * struct drm_shadow_plane_state *shadow_plane_state =
  78. * to_drm_shadow_plane_state(plane_state);
  79. *
  80. * // access shadow buffer via shadow_plane_state->map
  81. * }
  82. *
  83. * A mapping address for each of the framebuffer's buffer object is stored in
  84. * struct &drm_shadow_plane_state.map. The mappings are valid while the state
  85. * is being used.
  86. *
  87. * Drivers that use struct drm_simple_display_pipe can use
  88. * %DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS to initialize the rsp
  89. * callbacks. Access to shadow-buffer mappings is similar to regular
  90. * atomic_update.
  91. *
  92. * .. code-block:: c
  93. *
  94. * struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
  95. * ...,
  96. * DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
  97. * };
  98. *
  99. * void driver_pipe_enable(struct drm_simple_display_pipe *pipe,
  100. * struct drm_crtc_state *crtc_state,
  101. * struct drm_plane_state *plane_state)
  102. * {
  103. * struct drm_shadow_plane_state *shadow_plane_state =
  104. * to_drm_shadow_plane_state(plane_state);
  105. *
  106. * // access shadow buffer via shadow_plane_state->map
  107. * }
  108. */
  109. /*
  110. * Plane Helpers
  111. */
  112. /**
  113. * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer
  114. * @plane: Plane
  115. * @state: Plane state the fence will be attached to
  116. *
  117. * This function extracts the exclusive fence from &drm_gem_object.resv and
  118. * attaches it to plane state for the atomic helper to wait on. This is
  119. * necessary to correctly implement implicit synchronization for any buffers
  120. * shared as a struct &dma_buf. This function can be used as the
  121. * &drm_plane_helper_funcs.prepare_fb callback.
  122. *
  123. * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
  124. * GEM based framebuffer drivers which have their buffers always pinned in
  125. * memory.
  126. *
  127. * This function is the default implementation for GEM drivers of
  128. * &drm_plane_helper_funcs.prepare_fb if no callback is provided.
  129. */
  130. int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane,
  131. struct drm_plane_state *state)
  132. {
  133. struct dma_fence *fence = dma_fence_get(state->fence);
  134. enum dma_resv_usage usage;
  135. size_t i;
  136. int ret;
  137. if (!state->fb)
  138. return 0;
  139. /*
  140. * Only add the kernel fences here if there is already a fence set via
  141. * explicit fencing interfaces on the atomic ioctl.
  142. *
  143. * This way explicit fencing can be used to overrule implicit fencing,
  144. * which is important to make explicit fencing use-cases work: One
  145. * example is using one buffer for 2 screens with different refresh
  146. * rates. Implicit fencing will clamp rendering to the refresh rate of
  147. * the slower screen, whereas explicit fence allows 2 independent
  148. * render and display loops on a single buffer. If a driver allows
  149. * obeys both implicit and explicit fences for plane updates, then it
  150. * will break all the benefits of explicit fencing.
  151. */
  152. usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE;
  153. for (i = 0; i < state->fb->format->num_planes; ++i) {
  154. struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i);
  155. struct dma_fence *new;
  156. if (!obj) {
  157. ret = -EINVAL;
  158. goto error;
  159. }
  160. ret = dma_resv_get_singleton(obj->resv, usage, &new);
  161. if (ret)
  162. goto error;
  163. if (new && fence) {
  164. struct dma_fence_chain *chain = dma_fence_chain_alloc();
  165. if (!chain) {
  166. ret = -ENOMEM;
  167. goto error;
  168. }
  169. dma_fence_chain_init(chain, fence, new, 1);
  170. fence = &chain->base;
  171. } else if (new) {
  172. fence = new;
  173. }
  174. }
  175. dma_fence_put(state->fence);
  176. state->fence = fence;
  177. return 0;
  178. error:
  179. dma_fence_put(fence);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb);
  183. /*
  184. * Shadow-buffered Planes
  185. */
  186. /**
  187. * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
  188. * @plane: the plane
  189. * @new_shadow_plane_state: the new shadow-buffered plane state
  190. *
  191. * This function duplicates shadow-buffered plane state. This is helpful for drivers
  192. * that subclass struct drm_shadow_plane_state.
  193. *
  194. * The function does not duplicate existing mappings of the shadow buffers.
  195. * Mappings are maintained during the atomic commit by the plane's prepare_fb
  196. * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
  197. * for corresponding helpers.
  198. */
  199. void
  200. __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane,
  201. struct drm_shadow_plane_state *new_shadow_plane_state)
  202. {
  203. struct drm_plane_state *plane_state = plane->state;
  204. struct drm_shadow_plane_state *shadow_plane_state =
  205. to_drm_shadow_plane_state(plane_state);
  206. __drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base);
  207. drm_format_conv_state_copy(&new_shadow_plane_state->fmtcnv_state,
  208. &shadow_plane_state->fmtcnv_state);
  209. }
  210. EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state);
  211. /**
  212. * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
  213. * @plane: the plane
  214. *
  215. * This function implements struct &drm_plane_funcs.atomic_duplicate_state for
  216. * shadow-buffered planes. It assumes the existing state to be of type
  217. * struct drm_shadow_plane_state and it allocates the new state to be of this
  218. * type.
  219. *
  220. * The function does not duplicate existing mappings of the shadow buffers.
  221. * Mappings are maintained during the atomic commit by the plane's prepare_fb
  222. * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
  223. * for corresponding helpers.
  224. *
  225. * Returns:
  226. * A pointer to a new plane state on success, or NULL otherwise.
  227. */
  228. struct drm_plane_state *
  229. drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane)
  230. {
  231. struct drm_plane_state *plane_state = plane->state;
  232. struct drm_shadow_plane_state *new_shadow_plane_state;
  233. if (!plane_state)
  234. return NULL;
  235. new_shadow_plane_state = kzalloc_obj(*new_shadow_plane_state);
  236. if (!new_shadow_plane_state)
  237. return NULL;
  238. __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
  239. return &new_shadow_plane_state->base;
  240. }
  241. EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state);
  242. /**
  243. * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state
  244. * @shadow_plane_state: the shadow-buffered plane state
  245. *
  246. * This function cleans up shadow-buffered plane state. Helpful for drivers that
  247. * subclass struct drm_shadow_plane_state.
  248. */
  249. void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state)
  250. {
  251. drm_format_conv_state_release(&shadow_plane_state->fmtcnv_state);
  252. __drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base);
  253. }
  254. EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state);
  255. /**
  256. * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state
  257. * @plane: the plane
  258. * @plane_state: the plane state of type struct drm_shadow_plane_state
  259. *
  260. * This function implements struct &drm_plane_funcs.atomic_destroy_state
  261. * for shadow-buffered planes. It expects that mappings of shadow buffers
  262. * have been released already.
  263. */
  264. void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane,
  265. struct drm_plane_state *plane_state)
  266. {
  267. struct drm_shadow_plane_state *shadow_plane_state =
  268. to_drm_shadow_plane_state(plane_state);
  269. __drm_gem_destroy_shadow_plane_state(shadow_plane_state);
  270. kfree(shadow_plane_state);
  271. }
  272. EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state);
  273. /**
  274. * __drm_gem_reset_shadow_plane - resets a shadow-buffered plane
  275. * @plane: the plane
  276. * @shadow_plane_state: the shadow-buffered plane state
  277. *
  278. * This function resets state for shadow-buffered planes. Helpful
  279. * for drivers that subclass struct drm_shadow_plane_state.
  280. */
  281. void __drm_gem_reset_shadow_plane(struct drm_plane *plane,
  282. struct drm_shadow_plane_state *shadow_plane_state)
  283. {
  284. if (shadow_plane_state) {
  285. __drm_atomic_helper_plane_reset(plane, &shadow_plane_state->base);
  286. drm_format_conv_state_init(&shadow_plane_state->fmtcnv_state);
  287. } else {
  288. __drm_atomic_helper_plane_reset(plane, NULL);
  289. }
  290. }
  291. EXPORT_SYMBOL(__drm_gem_reset_shadow_plane);
  292. /**
  293. * drm_gem_reset_shadow_plane - resets a shadow-buffered plane
  294. * @plane: the plane
  295. *
  296. * This function implements struct &drm_plane_funcs.reset_plane for
  297. * shadow-buffered planes. It assumes the current plane state to be
  298. * of type struct drm_shadow_plane and it allocates the new state of
  299. * this type.
  300. */
  301. void drm_gem_reset_shadow_plane(struct drm_plane *plane)
  302. {
  303. struct drm_shadow_plane_state *shadow_plane_state;
  304. if (plane->state) {
  305. drm_gem_destroy_shadow_plane_state(plane, plane->state);
  306. plane->state = NULL; /* must be set to NULL here */
  307. }
  308. shadow_plane_state = kzalloc_obj(*shadow_plane_state);
  309. __drm_gem_reset_shadow_plane(plane, shadow_plane_state);
  310. }
  311. EXPORT_SYMBOL(drm_gem_reset_shadow_plane);
  312. /**
  313. * drm_gem_begin_shadow_fb_access - prepares shadow framebuffers for CPU access
  314. * @plane: the plane
  315. * @plane_state: the plane state of type struct drm_shadow_plane_state
  316. *
  317. * This function implements struct &drm_plane_helper_funcs.begin_fb_access. It
  318. * maps all buffer objects of the plane's framebuffer into kernel address
  319. * space and stores them in struct &drm_shadow_plane_state.map. The first data
  320. * bytes are available in struct &drm_shadow_plane_state.data.
  321. *
  322. * See drm_gem_end_shadow_fb_access() for cleanup.
  323. *
  324. * Returns:
  325. * 0 on success, or a negative errno code otherwise.
  326. */
  327. int drm_gem_begin_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state)
  328. {
  329. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  330. struct drm_framebuffer *fb = plane_state->fb;
  331. if (!fb)
  332. return 0;
  333. return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
  334. }
  335. EXPORT_SYMBOL(drm_gem_begin_shadow_fb_access);
  336. /**
  337. * drm_gem_end_shadow_fb_access - releases shadow framebuffers from CPU access
  338. * @plane: the plane
  339. * @plane_state: the plane state of type struct drm_shadow_plane_state
  340. *
  341. * This function implements struct &drm_plane_helper_funcs.end_fb_access. It
  342. * undoes all effects of drm_gem_begin_shadow_fb_access() in reverse order.
  343. *
  344. * See drm_gem_begin_shadow_fb_access() for more information.
  345. */
  346. void drm_gem_end_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state)
  347. {
  348. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  349. struct drm_framebuffer *fb = plane_state->fb;
  350. if (!fb)
  351. return;
  352. drm_gem_fb_vunmap(fb, shadow_plane_state->map);
  353. }
  354. EXPORT_SYMBOL(drm_gem_end_shadow_fb_access);
  355. /**
  356. * drm_gem_simple_kms_begin_shadow_fb_access - prepares shadow framebuffers for CPU access
  357. * @pipe: the simple display pipe
  358. * @plane_state: the plane state of type struct drm_shadow_plane_state
  359. *
  360. * This function implements struct drm_simple_display_funcs.begin_fb_access.
  361. *
  362. * See drm_gem_begin_shadow_fb_access() for details and
  363. * drm_gem_simple_kms_cleanup_shadow_fb() for cleanup.
  364. *
  365. * Returns:
  366. * 0 on success, or a negative errno code otherwise.
  367. */
  368. int drm_gem_simple_kms_begin_shadow_fb_access(struct drm_simple_display_pipe *pipe,
  369. struct drm_plane_state *plane_state)
  370. {
  371. return drm_gem_begin_shadow_fb_access(&pipe->plane, plane_state);
  372. }
  373. EXPORT_SYMBOL(drm_gem_simple_kms_begin_shadow_fb_access);
  374. /**
  375. * drm_gem_simple_kms_end_shadow_fb_access - releases shadow framebuffers from CPU access
  376. * @pipe: the simple display pipe
  377. * @plane_state: the plane state of type struct drm_shadow_plane_state
  378. *
  379. * This function implements struct drm_simple_display_funcs.end_fb_access.
  380. * It undoes all effects of drm_gem_simple_kms_begin_shadow_fb_access() in
  381. * reverse order.
  382. *
  383. * See drm_gem_simple_kms_begin_shadow_fb_access().
  384. */
  385. void drm_gem_simple_kms_end_shadow_fb_access(struct drm_simple_display_pipe *pipe,
  386. struct drm_plane_state *plane_state)
  387. {
  388. drm_gem_end_shadow_fb_access(&pipe->plane, plane_state);
  389. }
  390. EXPORT_SYMBOL(drm_gem_simple_kms_end_shadow_fb_access);
  391. /**
  392. * drm_gem_simple_kms_reset_shadow_plane - resets a shadow-buffered plane
  393. * @pipe: the simple display pipe
  394. *
  395. * This function implements struct drm_simple_display_funcs.reset_plane
  396. * for shadow-buffered planes.
  397. */
  398. void drm_gem_simple_kms_reset_shadow_plane(struct drm_simple_display_pipe *pipe)
  399. {
  400. drm_gem_reset_shadow_plane(&pipe->plane);
  401. }
  402. EXPORT_SYMBOL(drm_gem_simple_kms_reset_shadow_plane);
  403. /**
  404. * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
  405. * @pipe: the simple display pipe
  406. *
  407. * This function implements struct drm_simple_display_funcs.duplicate_plane_state
  408. * for shadow-buffered planes. It does not duplicate existing mappings of the shadow
  409. * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb
  410. * and cleanup_fb helpers.
  411. *
  412. * Returns:
  413. * A pointer to a new plane state on success, or NULL otherwise.
  414. */
  415. struct drm_plane_state *
  416. drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe)
  417. {
  418. return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
  419. }
  420. EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state);
  421. /**
  422. * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state
  423. * @pipe: the simple display pipe
  424. * @plane_state: the plane state of type struct drm_shadow_plane_state
  425. *
  426. * This function implements struct drm_simple_display_funcs.destroy_plane_state
  427. * for shadow-buffered planes. It expects that mappings of shadow buffers
  428. * have been released already.
  429. */
  430. void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe,
  431. struct drm_plane_state *plane_state)
  432. {
  433. drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
  434. }
  435. EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state);