drm_writeback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  4. * Author: Brian Starkey <brian.starkey@arm.com>
  5. *
  6. * This program is free software and is provided to you under the terms of the
  7. * GNU General Public License version 2 as published by the Free Software
  8. * Foundation, and any use by you of this program is subject to the terms
  9. * of such GNU licence.
  10. */
  11. #include <drm/drm_crtc.h>
  12. #include <drm/drm_modeset_helper_vtables.h>
  13. #include <drm/drm_property.h>
  14. #include <drm/drm_writeback.h>
  15. #include <drm/drmP.h>
  16. #include <linux/dma-fence.h>
  17. /**
  18. * DOC: overview
  19. *
  20. * Writeback connectors are used to expose hardware which can write the output
  21. * from a CRTC to a memory buffer. They are used and act similarly to other
  22. * types of connectors, with some important differences:
  23. * - Writeback connectors don't provide a way to output visually to the user.
  24. * - Writeback connectors should always report as "disconnected" (so that
  25. * clients which don't understand them will ignore them).
  26. * - Writeback connectors don't have EDID.
  27. *
  28. * A framebuffer may only be attached to a writeback connector when the
  29. * connector is attached to a CRTC. The WRITEBACK_FB_ID property which sets the
  30. * framebuffer applies only to a single commit (see below). A framebuffer may
  31. * not be attached while the CRTC is off.
  32. *
  33. * Unlike with planes, when a writeback framebuffer is removed by userspace DRM
  34. * makes no attempt to remove it from active use by the connector. This is
  35. * because no method is provided to abort a writeback operation, and in any
  36. * case making a new commit whilst a writeback is ongoing is undefined (see
  37. * WRITEBACK_OUT_FENCE_PTR below). As soon as the current writeback is finished,
  38. * the framebuffer will automatically no longer be in active use. As it will
  39. * also have already been removed from the framebuffer list, there will be no
  40. * way for any userspace application to retrieve a reference to it in the
  41. * intervening period.
  42. *
  43. * Writeback connectors have some additional properties, which userspace
  44. * can use to query and control them:
  45. *
  46. * "WRITEBACK_FB_ID":
  47. * Write-only object property storing a DRM_MODE_OBJECT_FB: it stores the
  48. * framebuffer to be written by the writeback connector. This property is
  49. * similar to the FB_ID property on planes, but will always read as zero
  50. * and is not preserved across commits.
  51. * Userspace must set this property to an output buffer every time it
  52. * wishes the buffer to get filled.
  53. *
  54. * "WRITEBACK_PIXEL_FORMATS":
  55. * Immutable blob property to store the supported pixel formats table. The
  56. * data is an array of u32 DRM_FORMAT_* fourcc values.
  57. * Userspace can use this blob to find out what pixel formats are supported
  58. * by the connector's writeback engine.
  59. *
  60. * "WRITEBACK_OUT_FENCE_PTR":
  61. * Userspace can use this property to provide a pointer for the kernel to
  62. * fill with a sync_file file descriptor, which will signal once the
  63. * writeback is finished. The value should be the address of a 32-bit
  64. * signed integer, cast to a u64.
  65. * Userspace should wait for this fence to signal before making another
  66. * commit affecting any of the same CRTCs, Planes or Connectors.
  67. * **Failure to do so will result in undefined behaviour.**
  68. * For this reason it is strongly recommended that all userspace
  69. * applications making use of writeback connectors *always* retrieve an
  70. * out-fence for the commit and use it appropriately.
  71. * From userspace, this property will always read as zero.
  72. */
  73. #define fence_to_wb_connector(x) container_of(x->lock, \
  74. struct drm_writeback_connector, \
  75. fence_lock)
  76. static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence)
  77. {
  78. struct drm_writeback_connector *wb_connector =
  79. fence_to_wb_connector(fence);
  80. return wb_connector->base.dev->driver->name;
  81. }
  82. static const char *
  83. drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
  84. {
  85. struct drm_writeback_connector *wb_connector =
  86. fence_to_wb_connector(fence);
  87. return wb_connector->timeline_name;
  88. }
  89. static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
  90. {
  91. return true;
  92. }
  93. static const struct dma_fence_ops drm_writeback_fence_ops = {
  94. .get_driver_name = drm_writeback_fence_get_driver_name,
  95. .get_timeline_name = drm_writeback_fence_get_timeline_name,
  96. .enable_signaling = drm_writeback_fence_enable_signaling,
  97. .wait = dma_fence_default_wait,
  98. };
  99. static int create_writeback_properties(struct drm_device *dev)
  100. {
  101. struct drm_property *prop;
  102. if (!dev->mode_config.writeback_fb_id_property) {
  103. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  104. "WRITEBACK_FB_ID",
  105. DRM_MODE_OBJECT_FB);
  106. if (!prop)
  107. return -ENOMEM;
  108. dev->mode_config.writeback_fb_id_property = prop;
  109. }
  110. if (!dev->mode_config.writeback_pixel_formats_property) {
  111. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  112. DRM_MODE_PROP_ATOMIC |
  113. DRM_MODE_PROP_IMMUTABLE,
  114. "WRITEBACK_PIXEL_FORMATS", 0);
  115. if (!prop)
  116. return -ENOMEM;
  117. dev->mode_config.writeback_pixel_formats_property = prop;
  118. }
  119. if (!dev->mode_config.writeback_out_fence_ptr_property) {
  120. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  121. "WRITEBACK_OUT_FENCE_PTR", 0,
  122. U64_MAX);
  123. if (!prop)
  124. return -ENOMEM;
  125. dev->mode_config.writeback_out_fence_ptr_property = prop;
  126. }
  127. return 0;
  128. }
  129. static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
  130. .destroy = drm_encoder_cleanup,
  131. };
  132. /**
  133. * drm_writeback_connector_init - Initialize a writeback connector and its properties
  134. * @dev: DRM device
  135. * @wb_connector: Writeback connector to initialize
  136. * @con_funcs: Connector funcs vtable
  137. * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder
  138. * @formats: Array of supported pixel formats for the writeback engine
  139. * @n_formats: Length of the formats array
  140. *
  141. * This function creates the writeback-connector-specific properties if they
  142. * have not been already created, initializes the connector as
  143. * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
  144. * values. It will also create an internal encoder associated with the
  145. * drm_writeback_connector and set it to use the @enc_helper_funcs vtable for
  146. * the encoder helper.
  147. *
  148. * Drivers should always use this function instead of drm_connector_init() to
  149. * set up writeback connectors.
  150. *
  151. * Returns: 0 on success, or a negative error code
  152. */
  153. int drm_writeback_connector_init(struct drm_device *dev,
  154. struct drm_writeback_connector *wb_connector,
  155. const struct drm_connector_funcs *con_funcs,
  156. const struct drm_encoder_helper_funcs *enc_helper_funcs,
  157. const u32 *formats, int n_formats)
  158. {
  159. struct drm_property_blob *blob;
  160. struct drm_connector *connector = &wb_connector->base;
  161. struct drm_mode_config *config = &dev->mode_config;
  162. int ret = create_writeback_properties(dev);
  163. if (ret != 0)
  164. return ret;
  165. blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
  166. formats);
  167. if (IS_ERR(blob))
  168. return PTR_ERR(blob);
  169. drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs);
  170. ret = drm_encoder_init(dev, &wb_connector->encoder,
  171. &drm_writeback_encoder_funcs,
  172. DRM_MODE_ENCODER_VIRTUAL, NULL);
  173. if (ret)
  174. goto fail;
  175. connector->interlace_allowed = 0;
  176. ret = drm_connector_init(dev, connector, con_funcs,
  177. DRM_MODE_CONNECTOR_WRITEBACK);
  178. if (ret)
  179. goto connector_fail;
  180. ret = drm_mode_connector_attach_encoder(connector,
  181. &wb_connector->encoder);
  182. if (ret)
  183. goto attach_fail;
  184. INIT_LIST_HEAD(&wb_connector->job_queue);
  185. spin_lock_init(&wb_connector->job_lock);
  186. wb_connector->fence_context = dma_fence_context_alloc(1);
  187. spin_lock_init(&wb_connector->fence_lock);
  188. snprintf(wb_connector->timeline_name,
  189. sizeof(wb_connector->timeline_name),
  190. "CONNECTOR:%d-%s", connector->base.id, connector->name);
  191. drm_object_attach_property(&connector->base,
  192. config->writeback_out_fence_ptr_property, 0);
  193. drm_object_attach_property(&connector->base,
  194. config->writeback_fb_id_property, 0);
  195. drm_object_attach_property(&connector->base,
  196. config->writeback_pixel_formats_property,
  197. blob->base.id);
  198. wb_connector->pixel_formats_blob_ptr = blob;
  199. return 0;
  200. attach_fail:
  201. drm_connector_cleanup(connector);
  202. connector_fail:
  203. drm_encoder_cleanup(&wb_connector->encoder);
  204. fail:
  205. drm_property_blob_put(blob);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL(drm_writeback_connector_init);
  209. /**
  210. * drm_writeback_queue_job - Queue a writeback job for later signalling
  211. * @wb_connector: The writeback connector to queue a job on
  212. * @job: The job to queue
  213. *
  214. * This function adds a job to the job_queue for a writeback connector. It
  215. * should be considered to take ownership of the writeback job, and so any other
  216. * references to the job must be cleared after calling this function.
  217. *
  218. * Drivers must ensure that for a given writeback connector, jobs are queued in
  219. * exactly the same order as they will be completed by the hardware (and
  220. * signaled via drm_writeback_signal_completion).
  221. *
  222. * For every call to drm_writeback_queue_job() there must be exactly one call to
  223. * drm_writeback_signal_completion()
  224. *
  225. * See also: drm_writeback_signal_completion()
  226. */
  227. void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector,
  228. struct drm_writeback_job *job)
  229. {
  230. unsigned long flags;
  231. spin_lock_irqsave(&wb_connector->job_lock, flags);
  232. list_add_tail(&job->list_entry, &wb_connector->job_queue);
  233. spin_unlock_irqrestore(&wb_connector->job_lock, flags);
  234. }
  235. EXPORT_SYMBOL(drm_writeback_queue_job);
  236. /*
  237. * @cleanup_work: deferred cleanup of a writeback job
  238. *
  239. * The job cannot be cleaned up directly in drm_writeback_signal_completion,
  240. * because it may be called in interrupt context. Dropping the framebuffer
  241. * reference can sleep, and so the cleanup is deferred to a workqueue.
  242. */
  243. static void cleanup_work(struct work_struct *work)
  244. {
  245. struct drm_writeback_job *job = container_of(work,
  246. struct drm_writeback_job,
  247. cleanup_work);
  248. drm_framebuffer_put(job->fb);
  249. kfree(job);
  250. }
  251. /**
  252. * drm_writeback_signal_completion - Signal the completion of a writeback job
  253. * @wb_connector: The writeback connector whose job is complete
  254. * @status: Status code to set in the writeback out_fence (0 for success)
  255. *
  256. * Drivers should call this to signal the completion of a previously queued
  257. * writeback job. It should be called as soon as possible after the hardware
  258. * has finished writing, and may be called from interrupt context.
  259. * It is the driver's responsibility to ensure that for a given connector, the
  260. * hardware completes writeback jobs in the same order as they are queued.
  261. *
  262. * Unless the driver is holding its own reference to the framebuffer, it must
  263. * not be accessed after calling this function.
  264. *
  265. * See also: drm_writeback_queue_job()
  266. */
  267. void
  268. drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector,
  269. int status)
  270. {
  271. unsigned long flags;
  272. struct drm_writeback_job *job;
  273. spin_lock_irqsave(&wb_connector->job_lock, flags);
  274. job = list_first_entry_or_null(&wb_connector->job_queue,
  275. struct drm_writeback_job,
  276. list_entry);
  277. if (job) {
  278. list_del(&job->list_entry);
  279. if (job->out_fence) {
  280. if (status)
  281. dma_fence_set_error(job->out_fence, status);
  282. dma_fence_signal(job->out_fence);
  283. dma_fence_put(job->out_fence);
  284. }
  285. }
  286. spin_unlock_irqrestore(&wb_connector->job_lock, flags);
  287. if (WARN_ON(!job))
  288. return;
  289. INIT_WORK(&job->cleanup_work, cleanup_work);
  290. queue_work(system_long_wq, &job->cleanup_work);
  291. }
  292. EXPORT_SYMBOL(drm_writeback_signal_completion);
  293. struct dma_fence *
  294. drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector)
  295. {
  296. struct dma_fence *fence;
  297. if (WARN_ON(wb_connector->base.connector_type !=
  298. DRM_MODE_CONNECTOR_WRITEBACK))
  299. return NULL;
  300. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  301. if (!fence)
  302. return NULL;
  303. dma_fence_init(fence, &drm_writeback_fence_ops,
  304. &wb_connector->fence_lock, wb_connector->fence_context,
  305. ++wb_connector->fence_seqno);
  306. return fence;
  307. }
  308. EXPORT_SYMBOL(drm_writeback_get_out_fence);