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