drm_writeback.c 13 KB

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