Linux Audio

Check our new training course

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