xref: /linux/drivers/gpu/drm/drm_writeback.c (revision 57b8280a0a4163545b532ae516c2dd5c9b295ea3)
1935774cdSBrian Starkey // SPDX-License-Identifier: GPL-2.0
2935774cdSBrian Starkey /*
3935774cdSBrian Starkey  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
4935774cdSBrian Starkey  * Author: Brian Starkey <brian.starkey@arm.com>
5935774cdSBrian Starkey  *
6935774cdSBrian Starkey  * This program is free software and is provided to you under the terms of the
7935774cdSBrian Starkey  * GNU General Public License version 2 as published by the Free Software
8935774cdSBrian Starkey  * Foundation, and any use by you of this program is subject to the terms
9935774cdSBrian Starkey  * of such GNU licence.
10935774cdSBrian Starkey  */
11935774cdSBrian Starkey 
120500c04eSSam Ravnborg #include <linux/dma-fence.h>
130500c04eSSam Ravnborg 
14935774cdSBrian Starkey #include <drm/drm_crtc.h>
150500c04eSSam Ravnborg #include <drm/drm_device.h>
160500c04eSSam Ravnborg #include <drm/drm_drv.h>
17935774cdSBrian Starkey #include <drm/drm_modeset_helper_vtables.h>
18935774cdSBrian Starkey #include <drm/drm_property.h>
19935774cdSBrian Starkey #include <drm/drm_writeback.h>
20935774cdSBrian Starkey 
21935774cdSBrian Starkey /**
22935774cdSBrian Starkey  * DOC: overview
23935774cdSBrian Starkey  *
24935774cdSBrian Starkey  * Writeback connectors are used to expose hardware which can write the output
25935774cdSBrian Starkey  * from a CRTC to a memory buffer. They are used and act similarly to other
26935774cdSBrian Starkey  * types of connectors, with some important differences:
2773915b2bSBoris Brezillon  *
2873915b2bSBoris Brezillon  * * Writeback connectors don't provide a way to output visually to the user.
2973915b2bSBoris Brezillon  *
3071a5cb3eSAlexandru Gheorghe  * * Writeback connectors are visible to userspace only when the client sets
3171a5cb3eSAlexandru Gheorghe  *   DRM_CLIENT_CAP_WRITEBACK_CONNECTORS.
3273915b2bSBoris Brezillon  *
3373915b2bSBoris Brezillon  * * Writeback connectors don't have EDID.
34935774cdSBrian Starkey  *
35935774cdSBrian Starkey  * A framebuffer may only be attached to a writeback connector when the
36935774cdSBrian Starkey  * connector is attached to a CRTC. The WRITEBACK_FB_ID property which sets the
37935774cdSBrian Starkey  * framebuffer applies only to a single commit (see below). A framebuffer may
38935774cdSBrian Starkey  * not be attached while the CRTC is off.
39935774cdSBrian Starkey  *
40b13cc8ddSBrian Starkey  * Unlike with planes, when a writeback framebuffer is removed by userspace DRM
41b13cc8ddSBrian Starkey  * makes no attempt to remove it from active use by the connector. This is
42b13cc8ddSBrian Starkey  * because no method is provided to abort a writeback operation, and in any
43b13cc8ddSBrian Starkey  * case making a new commit whilst a writeback is ongoing is undefined (see
44b13cc8ddSBrian Starkey  * WRITEBACK_OUT_FENCE_PTR below). As soon as the current writeback is finished,
45b13cc8ddSBrian Starkey  * the framebuffer will automatically no longer be in active use. As it will
46b13cc8ddSBrian Starkey  * also have already been removed from the framebuffer list, there will be no
47b13cc8ddSBrian Starkey  * way for any userspace application to retrieve a reference to it in the
48b13cc8ddSBrian Starkey  * intervening period.
49b13cc8ddSBrian Starkey  *
50935774cdSBrian Starkey  * Writeback connectors have some additional properties, which userspace
51935774cdSBrian Starkey  * can use to query and control them:
52935774cdSBrian Starkey  *
53935774cdSBrian Starkey  *  "WRITEBACK_FB_ID":
54935774cdSBrian Starkey  *	Write-only object property storing a DRM_MODE_OBJECT_FB: it stores the
55935774cdSBrian Starkey  *	framebuffer to be written by the writeback connector. This property is
56935774cdSBrian Starkey  *	similar to the FB_ID property on planes, but will always read as zero
57935774cdSBrian Starkey  *	and is not preserved across commits.
58935774cdSBrian Starkey  *	Userspace must set this property to an output buffer every time it
59935774cdSBrian Starkey  *	wishes the buffer to get filled.
60935774cdSBrian Starkey  *
61935774cdSBrian Starkey  *  "WRITEBACK_PIXEL_FORMATS":
62935774cdSBrian Starkey  *	Immutable blob property to store the supported pixel formats table. The
63935774cdSBrian Starkey  *	data is an array of u32 DRM_FORMAT_* fourcc values.
64935774cdSBrian Starkey  *	Userspace can use this blob to find out what pixel formats are supported
65935774cdSBrian Starkey  *	by the connector's writeback engine.
66b13cc8ddSBrian Starkey  *
67b13cc8ddSBrian Starkey  *  "WRITEBACK_OUT_FENCE_PTR":
68b13cc8ddSBrian Starkey  *	Userspace can use this property to provide a pointer for the kernel to
69b13cc8ddSBrian Starkey  *	fill with a sync_file file descriptor, which will signal once the
70b13cc8ddSBrian Starkey  *	writeback is finished. The value should be the address of a 32-bit
71b13cc8ddSBrian Starkey  *	signed integer, cast to a u64.
72b13cc8ddSBrian Starkey  *	Userspace should wait for this fence to signal before making another
73b13cc8ddSBrian Starkey  *	commit affecting any of the same CRTCs, Planes or Connectors.
74b13cc8ddSBrian Starkey  *	**Failure to do so will result in undefined behaviour.**
75b13cc8ddSBrian Starkey  *	For this reason it is strongly recommended that all userspace
76b13cc8ddSBrian Starkey  *	applications making use of writeback connectors *always* retrieve an
77b13cc8ddSBrian Starkey  *	out-fence for the commit and use it appropriately.
78b13cc8ddSBrian Starkey  *	From userspace, this property will always read as zero.
79935774cdSBrian Starkey  */
80935774cdSBrian Starkey 
81b13cc8ddSBrian Starkey #define fence_to_wb_connector(x) container_of(x->lock, \
82b13cc8ddSBrian Starkey 					      struct drm_writeback_connector, \
83b13cc8ddSBrian Starkey 					      fence_lock)
84b13cc8ddSBrian Starkey 
85b13cc8ddSBrian Starkey static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence)
86b13cc8ddSBrian Starkey {
87b13cc8ddSBrian Starkey 	struct drm_writeback_connector *wb_connector =
88b13cc8ddSBrian Starkey 		fence_to_wb_connector(fence);
89b13cc8ddSBrian Starkey 
90b13cc8ddSBrian Starkey 	return wb_connector->base.dev->driver->name;
91b13cc8ddSBrian Starkey }
92b13cc8ddSBrian Starkey 
93b13cc8ddSBrian Starkey static const char *
94b13cc8ddSBrian Starkey drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
95b13cc8ddSBrian Starkey {
96b13cc8ddSBrian Starkey 	struct drm_writeback_connector *wb_connector =
97b13cc8ddSBrian Starkey 		fence_to_wb_connector(fence);
98b13cc8ddSBrian Starkey 
99b13cc8ddSBrian Starkey 	return wb_connector->timeline_name;
100b13cc8ddSBrian Starkey }
101b13cc8ddSBrian Starkey 
102b13cc8ddSBrian Starkey static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
103b13cc8ddSBrian Starkey {
104b13cc8ddSBrian Starkey 	return true;
105b13cc8ddSBrian Starkey }
106b13cc8ddSBrian Starkey 
107b13cc8ddSBrian Starkey static const struct dma_fence_ops drm_writeback_fence_ops = {
108b13cc8ddSBrian Starkey 	.get_driver_name = drm_writeback_fence_get_driver_name,
109b13cc8ddSBrian Starkey 	.get_timeline_name = drm_writeback_fence_get_timeline_name,
110b13cc8ddSBrian Starkey 	.enable_signaling = drm_writeback_fence_enable_signaling,
111b13cc8ddSBrian Starkey };
112b13cc8ddSBrian Starkey 
113935774cdSBrian Starkey static int create_writeback_properties(struct drm_device *dev)
114935774cdSBrian Starkey {
115935774cdSBrian Starkey 	struct drm_property *prop;
116935774cdSBrian Starkey 
117935774cdSBrian Starkey 	if (!dev->mode_config.writeback_fb_id_property) {
118935774cdSBrian Starkey 		prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
119935774cdSBrian Starkey 						  "WRITEBACK_FB_ID",
120935774cdSBrian Starkey 						  DRM_MODE_OBJECT_FB);
121935774cdSBrian Starkey 		if (!prop)
122935774cdSBrian Starkey 			return -ENOMEM;
123935774cdSBrian Starkey 		dev->mode_config.writeback_fb_id_property = prop;
124935774cdSBrian Starkey 	}
125935774cdSBrian Starkey 
126935774cdSBrian Starkey 	if (!dev->mode_config.writeback_pixel_formats_property) {
127935774cdSBrian Starkey 		prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
128935774cdSBrian Starkey 					   DRM_MODE_PROP_ATOMIC |
129935774cdSBrian Starkey 					   DRM_MODE_PROP_IMMUTABLE,
130935774cdSBrian Starkey 					   "WRITEBACK_PIXEL_FORMATS", 0);
131935774cdSBrian Starkey 		if (!prop)
132935774cdSBrian Starkey 			return -ENOMEM;
133935774cdSBrian Starkey 		dev->mode_config.writeback_pixel_formats_property = prop;
134935774cdSBrian Starkey 	}
135935774cdSBrian Starkey 
136b13cc8ddSBrian Starkey 	if (!dev->mode_config.writeback_out_fence_ptr_property) {
137b13cc8ddSBrian Starkey 		prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
138b13cc8ddSBrian Starkey 						 "WRITEBACK_OUT_FENCE_PTR", 0,
139b13cc8ddSBrian Starkey 						 U64_MAX);
140b13cc8ddSBrian Starkey 		if (!prop)
141b13cc8ddSBrian Starkey 			return -ENOMEM;
142b13cc8ddSBrian Starkey 		dev->mode_config.writeback_out_fence_ptr_property = prop;
143b13cc8ddSBrian Starkey 	}
144b13cc8ddSBrian Starkey 
145935774cdSBrian Starkey 	return 0;
146935774cdSBrian Starkey }
147935774cdSBrian Starkey 
148935774cdSBrian Starkey static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
149935774cdSBrian Starkey 	.destroy = drm_encoder_cleanup,
150935774cdSBrian Starkey };
151935774cdSBrian Starkey 
152935774cdSBrian Starkey /**
153935774cdSBrian Starkey  * drm_writeback_connector_init - Initialize a writeback connector and its properties
154935774cdSBrian Starkey  * @dev: DRM device
155935774cdSBrian Starkey  * @wb_connector: Writeback connector to initialize
156935774cdSBrian Starkey  * @con_funcs: Connector funcs vtable
157935774cdSBrian Starkey  * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder
158935774cdSBrian Starkey  * @formats: Array of supported pixel formats for the writeback engine
159935774cdSBrian Starkey  * @n_formats: Length of the formats array
160*57b8280aSAbhinav Kumar  * @possible_crtcs: possible crtcs for the internal writeback encoder
161935774cdSBrian Starkey  *
162935774cdSBrian Starkey  * This function creates the writeback-connector-specific properties if they
163935774cdSBrian Starkey  * have not been already created, initializes the connector as
164935774cdSBrian Starkey  * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
165935774cdSBrian Starkey  * values. It will also create an internal encoder associated with the
166935774cdSBrian Starkey  * drm_writeback_connector and set it to use the @enc_helper_funcs vtable for
167935774cdSBrian Starkey  * the encoder helper.
168935774cdSBrian Starkey  *
169935774cdSBrian Starkey  * Drivers should always use this function instead of drm_connector_init() to
170935774cdSBrian Starkey  * set up writeback connectors.
171935774cdSBrian Starkey  *
172935774cdSBrian Starkey  * Returns: 0 on success, or a negative error code
173935774cdSBrian Starkey  */
174935774cdSBrian Starkey int drm_writeback_connector_init(struct drm_device *dev,
175935774cdSBrian Starkey 				 struct drm_writeback_connector *wb_connector,
176935774cdSBrian Starkey 				 const struct drm_connector_funcs *con_funcs,
177935774cdSBrian Starkey 				 const struct drm_encoder_helper_funcs *enc_helper_funcs,
178*57b8280aSAbhinav Kumar 				 const u32 *formats, int n_formats,
179*57b8280aSAbhinav Kumar 				 u32 possible_crtcs)
180935774cdSBrian Starkey {
181935774cdSBrian Starkey 	struct drm_property_blob *blob;
182935774cdSBrian Starkey 	struct drm_connector *connector = &wb_connector->base;
183935774cdSBrian Starkey 	struct drm_mode_config *config = &dev->mode_config;
184935774cdSBrian Starkey 	int ret = create_writeback_properties(dev);
185935774cdSBrian Starkey 
186935774cdSBrian Starkey 	if (ret != 0)
187935774cdSBrian Starkey 		return ret;
188935774cdSBrian Starkey 
189935774cdSBrian Starkey 	blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
190935774cdSBrian Starkey 					formats);
191935774cdSBrian Starkey 	if (IS_ERR(blob))
192935774cdSBrian Starkey 		return PTR_ERR(blob);
193935774cdSBrian Starkey 
194935774cdSBrian Starkey 	drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs);
195*57b8280aSAbhinav Kumar 
196*57b8280aSAbhinav Kumar 	wb_connector->encoder.possible_crtcs = possible_crtcs;
197*57b8280aSAbhinav Kumar 
198935774cdSBrian Starkey 	ret = drm_encoder_init(dev, &wb_connector->encoder,
199935774cdSBrian Starkey 			       &drm_writeback_encoder_funcs,
200935774cdSBrian Starkey 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
201935774cdSBrian Starkey 	if (ret)
202935774cdSBrian Starkey 		goto fail;
203935774cdSBrian Starkey 
204935774cdSBrian Starkey 	connector->interlace_allowed = 0;
205935774cdSBrian Starkey 
206935774cdSBrian Starkey 	ret = drm_connector_init(dev, connector, con_funcs,
207935774cdSBrian Starkey 				 DRM_MODE_CONNECTOR_WRITEBACK);
208935774cdSBrian Starkey 	if (ret)
209935774cdSBrian Starkey 		goto connector_fail;
210935774cdSBrian Starkey 
211cde4c44dSDaniel Vetter 	ret = drm_connector_attach_encoder(connector,
212935774cdSBrian Starkey 						&wb_connector->encoder);
213935774cdSBrian Starkey 	if (ret)
214935774cdSBrian Starkey 		goto attach_fail;
215935774cdSBrian Starkey 
216935774cdSBrian Starkey 	INIT_LIST_HEAD(&wb_connector->job_queue);
217935774cdSBrian Starkey 	spin_lock_init(&wb_connector->job_lock);
218935774cdSBrian Starkey 
219b13cc8ddSBrian Starkey 	wb_connector->fence_context = dma_fence_context_alloc(1);
220b13cc8ddSBrian Starkey 	spin_lock_init(&wb_connector->fence_lock);
221b13cc8ddSBrian Starkey 	snprintf(wb_connector->timeline_name,
222b13cc8ddSBrian Starkey 		 sizeof(wb_connector->timeline_name),
223b13cc8ddSBrian Starkey 		 "CONNECTOR:%d-%s", connector->base.id, connector->name);
224b13cc8ddSBrian Starkey 
225b13cc8ddSBrian Starkey 	drm_object_attach_property(&connector->base,
226b13cc8ddSBrian Starkey 				   config->writeback_out_fence_ptr_property, 0);
227b13cc8ddSBrian Starkey 
228935774cdSBrian Starkey 	drm_object_attach_property(&connector->base,
229935774cdSBrian Starkey 				   config->writeback_fb_id_property, 0);
230935774cdSBrian Starkey 
231935774cdSBrian Starkey 	drm_object_attach_property(&connector->base,
232935774cdSBrian Starkey 				   config->writeback_pixel_formats_property,
233935774cdSBrian Starkey 				   blob->base.id);
234935774cdSBrian Starkey 	wb_connector->pixel_formats_blob_ptr = blob;
235935774cdSBrian Starkey 
236935774cdSBrian Starkey 	return 0;
237935774cdSBrian Starkey 
238935774cdSBrian Starkey attach_fail:
239935774cdSBrian Starkey 	drm_connector_cleanup(connector);
240935774cdSBrian Starkey connector_fail:
241935774cdSBrian Starkey 	drm_encoder_cleanup(&wb_connector->encoder);
242935774cdSBrian Starkey fail:
243935774cdSBrian Starkey 	drm_property_blob_put(blob);
244935774cdSBrian Starkey 	return ret;
245935774cdSBrian Starkey }
246935774cdSBrian Starkey EXPORT_SYMBOL(drm_writeback_connector_init);
247935774cdSBrian Starkey 
2489d2230dcSLaurent Pinchart int drm_writeback_set_fb(struct drm_connector_state *conn_state,
2499d2230dcSLaurent Pinchart 			 struct drm_framebuffer *fb)
2509d2230dcSLaurent Pinchart {
2519d2230dcSLaurent Pinchart 	WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
2529d2230dcSLaurent Pinchart 
2539d2230dcSLaurent Pinchart 	if (!conn_state->writeback_job) {
2549d2230dcSLaurent Pinchart 		conn_state->writeback_job =
2559d2230dcSLaurent Pinchart 			kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
2569d2230dcSLaurent Pinchart 		if (!conn_state->writeback_job)
2579d2230dcSLaurent Pinchart 			return -ENOMEM;
2589d2230dcSLaurent Pinchart 
2599d2230dcSLaurent Pinchart 		conn_state->writeback_job->connector =
2609d2230dcSLaurent Pinchart 			drm_connector_to_writeback(conn_state->connector);
2619d2230dcSLaurent Pinchart 	}
2629d2230dcSLaurent Pinchart 
2639d2230dcSLaurent Pinchart 	drm_framebuffer_assign(&conn_state->writeback_job->fb, fb);
2649d2230dcSLaurent Pinchart 	return 0;
2659d2230dcSLaurent Pinchart }
2669d2230dcSLaurent Pinchart 
2679d2230dcSLaurent Pinchart int drm_writeback_prepare_job(struct drm_writeback_job *job)
2689d2230dcSLaurent Pinchart {
2699d2230dcSLaurent Pinchart 	struct drm_writeback_connector *connector = job->connector;
2709d2230dcSLaurent Pinchart 	const struct drm_connector_helper_funcs *funcs =
2719d2230dcSLaurent Pinchart 		connector->base.helper_private;
2729d2230dcSLaurent Pinchart 	int ret;
2739d2230dcSLaurent Pinchart 
2749d2230dcSLaurent Pinchart 	if (funcs->prepare_writeback_job) {
2759d2230dcSLaurent Pinchart 		ret = funcs->prepare_writeback_job(connector, job);
2769d2230dcSLaurent Pinchart 		if (ret < 0)
2779d2230dcSLaurent Pinchart 			return ret;
2789d2230dcSLaurent Pinchart 	}
2799d2230dcSLaurent Pinchart 
2809d2230dcSLaurent Pinchart 	job->prepared = true;
2819d2230dcSLaurent Pinchart 	return 0;
2829d2230dcSLaurent Pinchart }
2839d2230dcSLaurent Pinchart EXPORT_SYMBOL(drm_writeback_prepare_job);
2849d2230dcSLaurent Pinchart 
285935774cdSBrian Starkey /**
286935774cdSBrian Starkey  * drm_writeback_queue_job - Queue a writeback job for later signalling
287935774cdSBrian Starkey  * @wb_connector: The writeback connector to queue a job on
28897eb9eaeSLaurent Pinchart  * @conn_state: The connector state containing the job to queue
289935774cdSBrian Starkey  *
29097eb9eaeSLaurent Pinchart  * This function adds the job contained in @conn_state to the job_queue for a
29197eb9eaeSLaurent Pinchart  * writeback connector. It takes ownership of the writeback job and sets the
29297eb9eaeSLaurent Pinchart  * @conn_state->writeback_job to NULL, and so no access to the job may be
29397eb9eaeSLaurent Pinchart  * performed by the caller after this function returns.
294935774cdSBrian Starkey  *
295935774cdSBrian Starkey  * Drivers must ensure that for a given writeback connector, jobs are queued in
296935774cdSBrian Starkey  * exactly the same order as they will be completed by the hardware (and
297935774cdSBrian Starkey  * signaled via drm_writeback_signal_completion).
298935774cdSBrian Starkey  *
299935774cdSBrian Starkey  * For every call to drm_writeback_queue_job() there must be exactly one call to
300935774cdSBrian Starkey  * drm_writeback_signal_completion()
301935774cdSBrian Starkey  *
302935774cdSBrian Starkey  * See also: drm_writeback_signal_completion()
303935774cdSBrian Starkey  */
304935774cdSBrian Starkey void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector,
30597eb9eaeSLaurent Pinchart 			     struct drm_connector_state *conn_state)
306935774cdSBrian Starkey {
30797eb9eaeSLaurent Pinchart 	struct drm_writeback_job *job;
308935774cdSBrian Starkey 	unsigned long flags;
309935774cdSBrian Starkey 
31097eb9eaeSLaurent Pinchart 	job = conn_state->writeback_job;
31197eb9eaeSLaurent Pinchart 	conn_state->writeback_job = NULL;
31297eb9eaeSLaurent Pinchart 
313935774cdSBrian Starkey 	spin_lock_irqsave(&wb_connector->job_lock, flags);
314935774cdSBrian Starkey 	list_add_tail(&job->list_entry, &wb_connector->job_queue);
315935774cdSBrian Starkey 	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
316935774cdSBrian Starkey }
317935774cdSBrian Starkey EXPORT_SYMBOL(drm_writeback_queue_job);
318935774cdSBrian Starkey 
319e482ae9bSLaurent Pinchart void drm_writeback_cleanup_job(struct drm_writeback_job *job)
320e482ae9bSLaurent Pinchart {
3219d2230dcSLaurent Pinchart 	struct drm_writeback_connector *connector = job->connector;
3229d2230dcSLaurent Pinchart 	const struct drm_connector_helper_funcs *funcs =
3239d2230dcSLaurent Pinchart 		connector->base.helper_private;
3249d2230dcSLaurent Pinchart 
3259d2230dcSLaurent Pinchart 	if (job->prepared && funcs->cleanup_writeback_job)
3269d2230dcSLaurent Pinchart 		funcs->cleanup_writeback_job(connector, job);
3279d2230dcSLaurent Pinchart 
328e482ae9bSLaurent Pinchart 	if (job->fb)
329e482ae9bSLaurent Pinchart 		drm_framebuffer_put(job->fb);
330e482ae9bSLaurent Pinchart 
331b1066a12SLowry Li (Arm Technology China) 	if (job->out_fence)
332b1066a12SLowry Li (Arm Technology China) 		dma_fence_put(job->out_fence);
333b1066a12SLowry Li (Arm Technology China) 
334e482ae9bSLaurent Pinchart 	kfree(job);
335e482ae9bSLaurent Pinchart }
336e482ae9bSLaurent Pinchart EXPORT_SYMBOL(drm_writeback_cleanup_job);
337e482ae9bSLaurent Pinchart 
338935774cdSBrian Starkey /*
339935774cdSBrian Starkey  * @cleanup_work: deferred cleanup of a writeback job
340935774cdSBrian Starkey  *
341935774cdSBrian Starkey  * The job cannot be cleaned up directly in drm_writeback_signal_completion,
342935774cdSBrian Starkey  * because it may be called in interrupt context. Dropping the framebuffer
343935774cdSBrian Starkey  * reference can sleep, and so the cleanup is deferred to a workqueue.
344935774cdSBrian Starkey  */
345935774cdSBrian Starkey static void cleanup_work(struct work_struct *work)
346935774cdSBrian Starkey {
347935774cdSBrian Starkey 	struct drm_writeback_job *job = container_of(work,
348935774cdSBrian Starkey 						     struct drm_writeback_job,
349935774cdSBrian Starkey 						     cleanup_work);
350935774cdSBrian Starkey 
351e482ae9bSLaurent Pinchart 	drm_writeback_cleanup_job(job);
352e482ae9bSLaurent Pinchart }
353935774cdSBrian Starkey 
354935774cdSBrian Starkey /**
355935774cdSBrian Starkey  * drm_writeback_signal_completion - Signal the completion of a writeback job
356935774cdSBrian Starkey  * @wb_connector: The writeback connector whose job is complete
357b13cc8ddSBrian Starkey  * @status: Status code to set in the writeback out_fence (0 for success)
358935774cdSBrian Starkey  *
359935774cdSBrian Starkey  * Drivers should call this to signal the completion of a previously queued
360935774cdSBrian Starkey  * writeback job. It should be called as soon as possible after the hardware
361935774cdSBrian Starkey  * has finished writing, and may be called from interrupt context.
362935774cdSBrian Starkey  * It is the driver's responsibility to ensure that for a given connector, the
363935774cdSBrian Starkey  * hardware completes writeback jobs in the same order as they are queued.
364935774cdSBrian Starkey  *
365935774cdSBrian Starkey  * Unless the driver is holding its own reference to the framebuffer, it must
366935774cdSBrian Starkey  * not be accessed after calling this function.
367935774cdSBrian Starkey  *
368935774cdSBrian Starkey  * See also: drm_writeback_queue_job()
369935774cdSBrian Starkey  */
370935774cdSBrian Starkey void
371b13cc8ddSBrian Starkey drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector,
372b13cc8ddSBrian Starkey 				int status)
373935774cdSBrian Starkey {
374935774cdSBrian Starkey 	unsigned long flags;
375935774cdSBrian Starkey 	struct drm_writeback_job *job;
376b1066a12SLowry Li (Arm Technology China) 	struct dma_fence *out_fence;
377935774cdSBrian Starkey 
378935774cdSBrian Starkey 	spin_lock_irqsave(&wb_connector->job_lock, flags);
379935774cdSBrian Starkey 	job = list_first_entry_or_null(&wb_connector->job_queue,
380935774cdSBrian Starkey 				       struct drm_writeback_job,
381935774cdSBrian Starkey 				       list_entry);
382b1066a12SLowry Li (Arm Technology China) 	if (job)
383935774cdSBrian Starkey 		list_del(&job->list_entry);
384b1066a12SLowry Li (Arm Technology China) 
385935774cdSBrian Starkey 	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
386935774cdSBrian Starkey 
387935774cdSBrian Starkey 	if (WARN_ON(!job))
388935774cdSBrian Starkey 		return;
389935774cdSBrian Starkey 
390b1066a12SLowry Li (Arm Technology China) 	out_fence = job->out_fence;
391b1066a12SLowry Li (Arm Technology China) 	if (out_fence) {
392b1066a12SLowry Li (Arm Technology China) 		if (status)
393b1066a12SLowry Li (Arm Technology China) 			dma_fence_set_error(out_fence, status);
394b1066a12SLowry Li (Arm Technology China) 		dma_fence_signal(out_fence);
395b1066a12SLowry Li (Arm Technology China) 		dma_fence_put(out_fence);
396b1066a12SLowry Li (Arm Technology China) 		job->out_fence = NULL;
397b1066a12SLowry Li (Arm Technology China) 	}
398b1066a12SLowry Li (Arm Technology China) 
399935774cdSBrian Starkey 	INIT_WORK(&job->cleanup_work, cleanup_work);
400935774cdSBrian Starkey 	queue_work(system_long_wq, &job->cleanup_work);
401935774cdSBrian Starkey }
402935774cdSBrian Starkey EXPORT_SYMBOL(drm_writeback_signal_completion);
403b13cc8ddSBrian Starkey 
404b13cc8ddSBrian Starkey struct dma_fence *
405b13cc8ddSBrian Starkey drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector)
406b13cc8ddSBrian Starkey {
407b13cc8ddSBrian Starkey 	struct dma_fence *fence;
408b13cc8ddSBrian Starkey 
409b13cc8ddSBrian Starkey 	if (WARN_ON(wb_connector->base.connector_type !=
410b13cc8ddSBrian Starkey 		    DRM_MODE_CONNECTOR_WRITEBACK))
411b13cc8ddSBrian Starkey 		return NULL;
412b13cc8ddSBrian Starkey 
413b13cc8ddSBrian Starkey 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
414b13cc8ddSBrian Starkey 	if (!fence)
415b13cc8ddSBrian Starkey 		return NULL;
416b13cc8ddSBrian Starkey 
417b13cc8ddSBrian Starkey 	dma_fence_init(fence, &drm_writeback_fence_ops,
418b13cc8ddSBrian Starkey 		       &wb_connector->fence_lock, wb_connector->fence_context,
419b13cc8ddSBrian Starkey 		       ++wb_connector->fence_seqno);
420b13cc8ddSBrian Starkey 
421b13cc8ddSBrian Starkey 	return fence;
422b13cc8ddSBrian Starkey }
423b13cc8ddSBrian Starkey EXPORT_SYMBOL(drm_writeback_get_out_fence);
424