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