drm_writeback.c 20 KB

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