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