drm_writeback.c 15 KB

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