drm/amdgpu: clean up UVD instance handling v2
[sfrench/cifs-2.6.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  *  - Writeback connectors don't provide a way to output visually to the user.
26  *  - Writeback connectors should always report as "disconnected" (so that
27  *    clients which don't understand them will ignore them).
28  *  - Writeback connectors don't have EDID.
29  *
30  * A framebuffer may only be attached to a writeback connector when the
31  * connector is attached to a CRTC. The WRITEBACK_FB_ID property which sets the
32  * framebuffer applies only to a single commit (see below). A framebuffer may
33  * not be attached while the CRTC is off.
34  *
35  * Unlike with planes, when a writeback framebuffer is removed by userspace DRM
36  * makes no attempt to remove it from active use by the connector. This is
37  * because no method is provided to abort a writeback operation, and in any
38  * case making a new commit whilst a writeback is ongoing is undefined (see
39  * WRITEBACK_OUT_FENCE_PTR below). As soon as the current writeback is finished,
40  * the framebuffer will automatically no longer be in active use. As it will
41  * also have already been removed from the framebuffer list, there will be no
42  * way for any userspace application to retrieve a reference to it in the
43  * intervening period.
44  *
45  * Writeback connectors have some additional properties, which userspace
46  * can use to query and control them:
47  *
48  *  "WRITEBACK_FB_ID":
49  *      Write-only object property storing a DRM_MODE_OBJECT_FB: it stores the
50  *      framebuffer to be written by the writeback connector. This property is
51  *      similar to the FB_ID property on planes, but will always read as zero
52  *      and is not preserved across commits.
53  *      Userspace must set this property to an output buffer every time it
54  *      wishes the buffer to get filled.
55  *
56  *  "WRITEBACK_PIXEL_FORMATS":
57  *      Immutable blob property to store the supported pixel formats table. The
58  *      data is an array of u32 DRM_FORMAT_* fourcc values.
59  *      Userspace can use this blob to find out what pixel formats are supported
60  *      by the connector's writeback engine.
61  *
62  *  "WRITEBACK_OUT_FENCE_PTR":
63  *      Userspace can use this property to provide a pointer for the kernel to
64  *      fill with a sync_file file descriptor, which will signal once the
65  *      writeback is finished. The value should be the address of a 32-bit
66  *      signed integer, cast to a u64.
67  *      Userspace should wait for this fence to signal before making another
68  *      commit affecting any of the same CRTCs, Planes or Connectors.
69  *      **Failure to do so will result in undefined behaviour.**
70  *      For this reason it is strongly recommended that all userspace
71  *      applications making use of writeback connectors *always* retrieve an
72  *      out-fence for the commit and use it appropriately.
73  *      From userspace, this property will always read as zero.
74  */
75
76 #define fence_to_wb_connector(x) container_of(x->lock, \
77                                               struct drm_writeback_connector, \
78                                               fence_lock)
79
80 static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence)
81 {
82         struct drm_writeback_connector *wb_connector =
83                 fence_to_wb_connector(fence);
84
85         return wb_connector->base.dev->driver->name;
86 }
87
88 static const char *
89 drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
90 {
91         struct drm_writeback_connector *wb_connector =
92                 fence_to_wb_connector(fence);
93
94         return wb_connector->timeline_name;
95 }
96
97 static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
98 {
99         return true;
100 }
101
102 static const struct dma_fence_ops drm_writeback_fence_ops = {
103         .get_driver_name = drm_writeback_fence_get_driver_name,
104         .get_timeline_name = drm_writeback_fence_get_timeline_name,
105         .enable_signaling = drm_writeback_fence_enable_signaling,
106         .wait = dma_fence_default_wait,
107 };
108
109 static int create_writeback_properties(struct drm_device *dev)
110 {
111         struct drm_property *prop;
112
113         if (!dev->mode_config.writeback_fb_id_property) {
114                 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
115                                                   "WRITEBACK_FB_ID",
116                                                   DRM_MODE_OBJECT_FB);
117                 if (!prop)
118                         return -ENOMEM;
119                 dev->mode_config.writeback_fb_id_property = prop;
120         }
121
122         if (!dev->mode_config.writeback_pixel_formats_property) {
123                 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
124                                            DRM_MODE_PROP_ATOMIC |
125                                            DRM_MODE_PROP_IMMUTABLE,
126                                            "WRITEBACK_PIXEL_FORMATS", 0);
127                 if (!prop)
128                         return -ENOMEM;
129                 dev->mode_config.writeback_pixel_formats_property = prop;
130         }
131
132         if (!dev->mode_config.writeback_out_fence_ptr_property) {
133                 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
134                                                  "WRITEBACK_OUT_FENCE_PTR", 0,
135                                                  U64_MAX);
136                 if (!prop)
137                         return -ENOMEM;
138                 dev->mode_config.writeback_out_fence_ptr_property = prop;
139         }
140
141         return 0;
142 }
143
144 static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
145         .destroy = drm_encoder_cleanup,
146 };
147
148 /**
149  * drm_writeback_connector_init - Initialize a writeback connector and its properties
150  * @dev: DRM device
151  * @wb_connector: Writeback connector to initialize
152  * @con_funcs: Connector funcs vtable
153  * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder
154  * @formats: Array of supported pixel formats for the writeback engine
155  * @n_formats: Length of the formats array
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  */
169 int 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 {
175         struct drm_property_blob *blob;
176         struct drm_connector *connector = &wb_connector->base;
177         struct drm_mode_config *config = &dev->mode_config;
178         int ret = create_writeback_properties(dev);
179
180         if (ret != 0)
181                 return ret;
182
183         blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
184                                         formats);
185         if (IS_ERR(blob))
186                 return PTR_ERR(blob);
187
188         drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs);
189         ret = drm_encoder_init(dev, &wb_connector->encoder,
190                                &drm_writeback_encoder_funcs,
191                                DRM_MODE_ENCODER_VIRTUAL, NULL);
192         if (ret)
193                 goto fail;
194
195         connector->interlace_allowed = 0;
196
197         ret = drm_connector_init(dev, connector, con_funcs,
198                                  DRM_MODE_CONNECTOR_WRITEBACK);
199         if (ret)
200                 goto connector_fail;
201
202         ret = drm_mode_connector_attach_encoder(connector,
203                                                 &wb_connector->encoder);
204         if (ret)
205                 goto attach_fail;
206
207         INIT_LIST_HEAD(&wb_connector->job_queue);
208         spin_lock_init(&wb_connector->job_lock);
209
210         wb_connector->fence_context = dma_fence_context_alloc(1);
211         spin_lock_init(&wb_connector->fence_lock);
212         snprintf(wb_connector->timeline_name,
213                  sizeof(wb_connector->timeline_name),
214                  "CONNECTOR:%d-%s", connector->base.id, connector->name);
215
216         drm_object_attach_property(&connector->base,
217                                    config->writeback_out_fence_ptr_property, 0);
218
219         drm_object_attach_property(&connector->base,
220                                    config->writeback_fb_id_property, 0);
221
222         drm_object_attach_property(&connector->base,
223                                    config->writeback_pixel_formats_property,
224                                    blob->base.id);
225         wb_connector->pixel_formats_blob_ptr = blob;
226
227         return 0;
228
229 attach_fail:
230         drm_connector_cleanup(connector);
231 connector_fail:
232         drm_encoder_cleanup(&wb_connector->encoder);
233 fail:
234         drm_property_blob_put(blob);
235         return ret;
236 }
237 EXPORT_SYMBOL(drm_writeback_connector_init);
238
239 /**
240  * drm_writeback_queue_job - Queue a writeback job for later signalling
241  * @wb_connector: The writeback connector to queue a job on
242  * @job: The job to queue
243  *
244  * This function adds a job to the job_queue for a writeback connector. It
245  * should be considered to take ownership of the writeback job, and so any other
246  * references to the job must be cleared after calling this function.
247  *
248  * Drivers must ensure that for a given writeback connector, jobs are queued in
249  * exactly the same order as they will be completed by the hardware (and
250  * signaled via drm_writeback_signal_completion).
251  *
252  * For every call to drm_writeback_queue_job() there must be exactly one call to
253  * drm_writeback_signal_completion()
254  *
255  * See also: drm_writeback_signal_completion()
256  */
257 void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector,
258                              struct drm_writeback_job *job)
259 {
260         unsigned long flags;
261
262         spin_lock_irqsave(&wb_connector->job_lock, flags);
263         list_add_tail(&job->list_entry, &wb_connector->job_queue);
264         spin_unlock_irqrestore(&wb_connector->job_lock, flags);
265 }
266 EXPORT_SYMBOL(drm_writeback_queue_job);
267
268 /*
269  * @cleanup_work: deferred cleanup of a writeback job
270  *
271  * The job cannot be cleaned up directly in drm_writeback_signal_completion,
272  * because it may be called in interrupt context. Dropping the framebuffer
273  * reference can sleep, and so the cleanup is deferred to a workqueue.
274  */
275 static void cleanup_work(struct work_struct *work)
276 {
277         struct drm_writeback_job *job = container_of(work,
278                                                      struct drm_writeback_job,
279                                                      cleanup_work);
280         drm_framebuffer_put(job->fb);
281         kfree(job);
282 }
283
284
285 /**
286  * drm_writeback_signal_completion - Signal the completion of a writeback job
287  * @wb_connector: The writeback connector whose job is complete
288  * @status: Status code to set in the writeback out_fence (0 for success)
289  *
290  * Drivers should call this to signal the completion of a previously queued
291  * writeback job. It should be called as soon as possible after the hardware
292  * has finished writing, and may be called from interrupt context.
293  * It is the driver's responsibility to ensure that for a given connector, the
294  * hardware completes writeback jobs in the same order as they are queued.
295  *
296  * Unless the driver is holding its own reference to the framebuffer, it must
297  * not be accessed after calling this function.
298  *
299  * See also: drm_writeback_queue_job()
300  */
301 void
302 drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector,
303                                 int status)
304 {
305         unsigned long flags;
306         struct drm_writeback_job *job;
307
308         spin_lock_irqsave(&wb_connector->job_lock, flags);
309         job = list_first_entry_or_null(&wb_connector->job_queue,
310                                        struct drm_writeback_job,
311                                        list_entry);
312         if (job) {
313                 list_del(&job->list_entry);
314                 if (job->out_fence) {
315                         if (status)
316                                 dma_fence_set_error(job->out_fence, status);
317                         dma_fence_signal(job->out_fence);
318                         dma_fence_put(job->out_fence);
319                 }
320         }
321         spin_unlock_irqrestore(&wb_connector->job_lock, flags);
322
323         if (WARN_ON(!job))
324                 return;
325
326         INIT_WORK(&job->cleanup_work, cleanup_work);
327         queue_work(system_long_wq, &job->cleanup_work);
328 }
329 EXPORT_SYMBOL(drm_writeback_signal_completion);
330
331 struct dma_fence *
332 drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector)
333 {
334         struct dma_fence *fence;
335
336         if (WARN_ON(wb_connector->base.connector_type !=
337                     DRM_MODE_CONNECTOR_WRITEBACK))
338                 return NULL;
339
340         fence = kzalloc(sizeof(*fence), GFP_KERNEL);
341         if (!fence)
342                 return NULL;
343
344         dma_fence_init(fence, &drm_writeback_fence_ops,
345                        &wb_connector->fence_lock, wb_connector->fence_context,
346                        ++wb_connector->fence_seqno);
347
348         return fence;
349 }
350 EXPORT_SYMBOL(drm_writeback_get_out_fence);