1 // SPDX-License-Identifier: GPL-2.0
3 * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
4 * Author: Brian Starkey <brian.starkey@arm.com>
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
12 #include <linux/dma-fence.h>
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_device.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_modeset_helper_vtables.h>
18 #include <drm/drm_property.h>
19 #include <drm/drm_writeback.h>
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:
28 * * Writeback connectors don't provide a way to output visually to the user.
30 * * Writeback connectors are visible to userspace only when the client sets
31 * DRM_CLIENT_CAP_WRITEBACK_CONNECTORS.
33 * * Writeback connectors don't have EDID.
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.
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
50 * Writeback connectors have some additional properties, which userspace
51 * can use to query and control them:
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.
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.
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.
81 #define fence_to_wb_connector(x) container_of(x->lock, \
82 struct drm_writeback_connector, \
85 static const char *drm_writeback_fence_get_driver_name(struct dma_fence
*fence
)
87 struct drm_writeback_connector
*wb_connector
=
88 fence_to_wb_connector(fence
);
90 return wb_connector
->base
.dev
->driver
->name
;
94 drm_writeback_fence_get_timeline_name(struct dma_fence
*fence
)
96 struct drm_writeback_connector
*wb_connector
=
97 fence_to_wb_connector(fence
);
99 return wb_connector
->timeline_name
;
102 static bool drm_writeback_fence_enable_signaling(struct dma_fence
*fence
)
107 static const struct dma_fence_ops drm_writeback_fence_ops
= {
108 .get_driver_name
= drm_writeback_fence_get_driver_name
,
109 .get_timeline_name
= drm_writeback_fence_get_timeline_name
,
110 .enable_signaling
= drm_writeback_fence_enable_signaling
,
111 .wait
= dma_fence_default_wait
,
114 static int create_writeback_properties(struct drm_device
*dev
)
116 struct drm_property
*prop
;
118 if (!dev
->mode_config
.writeback_fb_id_property
) {
119 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
124 dev
->mode_config
.writeback_fb_id_property
= prop
;
127 if (!dev
->mode_config
.writeback_pixel_formats_property
) {
128 prop
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
129 DRM_MODE_PROP_ATOMIC
|
130 DRM_MODE_PROP_IMMUTABLE
,
131 "WRITEBACK_PIXEL_FORMATS", 0);
134 dev
->mode_config
.writeback_pixel_formats_property
= prop
;
137 if (!dev
->mode_config
.writeback_out_fence_ptr_property
) {
138 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
139 "WRITEBACK_OUT_FENCE_PTR", 0,
143 dev
->mode_config
.writeback_out_fence_ptr_property
= prop
;
149 static const struct drm_encoder_funcs drm_writeback_encoder_funcs
= {
150 .destroy
= drm_encoder_cleanup
,
154 * drm_writeback_connector_init - Initialize a writeback connector and its properties
156 * @wb_connector: Writeback connector to initialize
157 * @con_funcs: Connector funcs vtable
158 * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder
159 * @formats: Array of supported pixel formats for the writeback engine
160 * @n_formats: Length of the formats array
162 * This function creates the writeback-connector-specific properties if they
163 * have not been already created, initializes the connector as
164 * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
165 * values. It will also create an internal encoder associated with the
166 * drm_writeback_connector and set it to use the @enc_helper_funcs vtable for
167 * the encoder helper.
169 * Drivers should always use this function instead of drm_connector_init() to
170 * set up writeback connectors.
172 * Returns: 0 on success, or a negative error code
174 int drm_writeback_connector_init(struct drm_device
*dev
,
175 struct drm_writeback_connector
*wb_connector
,
176 const struct drm_connector_funcs
*con_funcs
,
177 const struct drm_encoder_helper_funcs
*enc_helper_funcs
,
178 const u32
*formats
, int n_formats
)
180 struct drm_property_blob
*blob
;
181 struct drm_connector
*connector
= &wb_connector
->base
;
182 struct drm_mode_config
*config
= &dev
->mode_config
;
183 int ret
= create_writeback_properties(dev
);
188 blob
= drm_property_create_blob(dev
, n_formats
* sizeof(*formats
),
191 return PTR_ERR(blob
);
193 drm_encoder_helper_add(&wb_connector
->encoder
, enc_helper_funcs
);
194 ret
= drm_encoder_init(dev
, &wb_connector
->encoder
,
195 &drm_writeback_encoder_funcs
,
196 DRM_MODE_ENCODER_VIRTUAL
, NULL
);
200 connector
->interlace_allowed
= 0;
202 ret
= drm_connector_init(dev
, connector
, con_funcs
,
203 DRM_MODE_CONNECTOR_WRITEBACK
);
207 ret
= drm_connector_attach_encoder(connector
,
208 &wb_connector
->encoder
);
212 INIT_LIST_HEAD(&wb_connector
->job_queue
);
213 spin_lock_init(&wb_connector
->job_lock
);
215 wb_connector
->fence_context
= dma_fence_context_alloc(1);
216 spin_lock_init(&wb_connector
->fence_lock
);
217 snprintf(wb_connector
->timeline_name
,
218 sizeof(wb_connector
->timeline_name
),
219 "CONNECTOR:%d-%s", connector
->base
.id
, connector
->name
);
221 drm_object_attach_property(&connector
->base
,
222 config
->writeback_out_fence_ptr_property
, 0);
224 drm_object_attach_property(&connector
->base
,
225 config
->writeback_fb_id_property
, 0);
227 drm_object_attach_property(&connector
->base
,
228 config
->writeback_pixel_formats_property
,
230 wb_connector
->pixel_formats_blob_ptr
= blob
;
235 drm_connector_cleanup(connector
);
237 drm_encoder_cleanup(&wb_connector
->encoder
);
239 drm_property_blob_put(blob
);
242 EXPORT_SYMBOL(drm_writeback_connector_init
);
244 int drm_writeback_set_fb(struct drm_connector_state
*conn_state
,
245 struct drm_framebuffer
*fb
)
247 WARN_ON(conn_state
->connector
->connector_type
!= DRM_MODE_CONNECTOR_WRITEBACK
);
249 if (!conn_state
->writeback_job
) {
250 conn_state
->writeback_job
=
251 kzalloc(sizeof(*conn_state
->writeback_job
), GFP_KERNEL
);
252 if (!conn_state
->writeback_job
)
255 conn_state
->writeback_job
->connector
=
256 drm_connector_to_writeback(conn_state
->connector
);
259 drm_framebuffer_assign(&conn_state
->writeback_job
->fb
, fb
);
263 int drm_writeback_prepare_job(struct drm_writeback_job
*job
)
265 struct drm_writeback_connector
*connector
= job
->connector
;
266 const struct drm_connector_helper_funcs
*funcs
=
267 connector
->base
.helper_private
;
270 if (funcs
->prepare_writeback_job
) {
271 ret
= funcs
->prepare_writeback_job(connector
, job
);
276 job
->prepared
= true;
279 EXPORT_SYMBOL(drm_writeback_prepare_job
);
282 * drm_writeback_queue_job - Queue a writeback job for later signalling
283 * @wb_connector: The writeback connector to queue a job on
284 * @conn_state: The connector state containing the job to queue
286 * This function adds the job contained in @conn_state to the job_queue for a
287 * writeback connector. It takes ownership of the writeback job and sets the
288 * @conn_state->writeback_job to NULL, and so no access to the job may be
289 * performed by the caller after this function returns.
291 * Drivers must ensure that for a given writeback connector, jobs are queued in
292 * exactly the same order as they will be completed by the hardware (and
293 * signaled via drm_writeback_signal_completion).
295 * For every call to drm_writeback_queue_job() there must be exactly one call to
296 * drm_writeback_signal_completion()
298 * See also: drm_writeback_signal_completion()
300 void drm_writeback_queue_job(struct drm_writeback_connector
*wb_connector
,
301 struct drm_connector_state
*conn_state
)
303 struct drm_writeback_job
*job
;
306 job
= conn_state
->writeback_job
;
307 conn_state
->writeback_job
= NULL
;
309 spin_lock_irqsave(&wb_connector
->job_lock
, flags
);
310 list_add_tail(&job
->list_entry
, &wb_connector
->job_queue
);
311 spin_unlock_irqrestore(&wb_connector
->job_lock
, flags
);
313 EXPORT_SYMBOL(drm_writeback_queue_job
);
315 void drm_writeback_cleanup_job(struct drm_writeback_job
*job
)
317 struct drm_writeback_connector
*connector
= job
->connector
;
318 const struct drm_connector_helper_funcs
*funcs
=
319 connector
->base
.helper_private
;
321 if (job
->prepared
&& funcs
->cleanup_writeback_job
)
322 funcs
->cleanup_writeback_job(connector
, job
);
325 drm_framebuffer_put(job
->fb
);
328 dma_fence_put(job
->out_fence
);
332 EXPORT_SYMBOL(drm_writeback_cleanup_job
);
335 * @cleanup_work: deferred cleanup of a writeback job
337 * The job cannot be cleaned up directly in drm_writeback_signal_completion,
338 * because it may be called in interrupt context. Dropping the framebuffer
339 * reference can sleep, and so the cleanup is deferred to a workqueue.
341 static void cleanup_work(struct work_struct
*work
)
343 struct drm_writeback_job
*job
= container_of(work
,
344 struct drm_writeback_job
,
347 drm_writeback_cleanup_job(job
);
351 * drm_writeback_signal_completion - Signal the completion of a writeback job
352 * @wb_connector: The writeback connector whose job is complete
353 * @status: Status code to set in the writeback out_fence (0 for success)
355 * Drivers should call this to signal the completion of a previously queued
356 * writeback job. It should be called as soon as possible after the hardware
357 * has finished writing, and may be called from interrupt context.
358 * It is the driver's responsibility to ensure that for a given connector, the
359 * hardware completes writeback jobs in the same order as they are queued.
361 * Unless the driver is holding its own reference to the framebuffer, it must
362 * not be accessed after calling this function.
364 * See also: drm_writeback_queue_job()
367 drm_writeback_signal_completion(struct drm_writeback_connector
*wb_connector
,
371 struct drm_writeback_job
*job
;
372 struct dma_fence
*out_fence
;
374 spin_lock_irqsave(&wb_connector
->job_lock
, flags
);
375 job
= list_first_entry_or_null(&wb_connector
->job_queue
,
376 struct drm_writeback_job
,
379 list_del(&job
->list_entry
);
381 spin_unlock_irqrestore(&wb_connector
->job_lock
, flags
);
386 out_fence
= job
->out_fence
;
389 dma_fence_set_error(out_fence
, status
);
390 dma_fence_signal(out_fence
);
391 dma_fence_put(out_fence
);
392 job
->out_fence
= NULL
;
395 INIT_WORK(&job
->cleanup_work
, cleanup_work
);
396 queue_work(system_long_wq
, &job
->cleanup_work
);
398 EXPORT_SYMBOL(drm_writeback_signal_completion
);
401 drm_writeback_get_out_fence(struct drm_writeback_connector
*wb_connector
)
403 struct dma_fence
*fence
;
405 if (WARN_ON(wb_connector
->base
.connector_type
!=
406 DRM_MODE_CONNECTOR_WRITEBACK
))
409 fence
= kzalloc(sizeof(*fence
), GFP_KERNEL
);
413 dma_fence_init(fence
, &drm_writeback_fence_ops
,
414 &wb_connector
->fence_lock
, wb_connector
->fence_context
,
415 ++wb_connector
->fence_seqno
);
419 EXPORT_SYMBOL(drm_writeback_get_out_fence
);