This source file includes following definitions.
- drm_writeback_fence_get_driver_name
- drm_writeback_fence_get_timeline_name
- drm_writeback_fence_enable_signaling
- create_writeback_properties
- drm_writeback_connector_init
- drm_writeback_set_fb
- drm_writeback_prepare_job
- drm_writeback_queue_job
- drm_writeback_cleanup_job
- cleanup_work
- drm_writeback_signal_completion
- drm_writeback_get_out_fence
1
2
3
4
5
6
7
8
9
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_modeset_helper_vtables.h>
18 #include <drm/drm_property.h>
19 #include <drm/drm_writeback.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 #define fence_to_wb_connector(x) container_of(x->lock, \
82 struct drm_writeback_connector, \
83 fence_lock)
84
85 static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence)
86 {
87 struct drm_writeback_connector *wb_connector =
88 fence_to_wb_connector(fence);
89
90 return wb_connector->base.dev->driver->name;
91 }
92
93 static const char *
94 drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
95 {
96 struct drm_writeback_connector *wb_connector =
97 fence_to_wb_connector(fence);
98
99 return wb_connector->timeline_name;
100 }
101
102 static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
103 {
104 return true;
105 }
106
107 static const struct dma_fence_ops drm_writeback_fence_ops = {
108 .get_driver_name = drm_writeback_fence_get_driver_name,
109 .get_timeline_name = drm_writeback_fence_get_timeline_name,
110 .enable_signaling = drm_writeback_fence_enable_signaling,
111 .wait = dma_fence_default_wait,
112 };
113
114 static 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
149 static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
150 .destroy = drm_encoder_cleanup,
151 };
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 int drm_writeback_connector_init(struct drm_device *dev,
175 struct drm_writeback_connector *wb_connector,
176 const struct drm_connector_funcs *con_funcs,
177 const struct drm_encoder_helper_funcs *enc_helper_funcs,
178 const u32 *formats, int n_formats)
179 {
180 struct drm_property_blob *blob;
181 struct drm_connector *connector = &wb_connector->base;
182 struct drm_mode_config *config = &dev->mode_config;
183 int ret = create_writeback_properties(dev);
184
185 if (ret != 0)
186 return ret;
187
188 blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
189 formats);
190 if (IS_ERR(blob))
191 return PTR_ERR(blob);
192
193 drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs);
194 ret = drm_encoder_init(dev, &wb_connector->encoder,
195 &drm_writeback_encoder_funcs,
196 DRM_MODE_ENCODER_VIRTUAL, NULL);
197 if (ret)
198 goto fail;
199
200 connector->interlace_allowed = 0;
201
202 ret = drm_connector_init(dev, connector, con_funcs,
203 DRM_MODE_CONNECTOR_WRITEBACK);
204 if (ret)
205 goto connector_fail;
206
207 ret = drm_connector_attach_encoder(connector,
208 &wb_connector->encoder);
209 if (ret)
210 goto attach_fail;
211
212 INIT_LIST_HEAD(&wb_connector->job_queue);
213 spin_lock_init(&wb_connector->job_lock);
214
215 wb_connector->fence_context = dma_fence_context_alloc(1);
216 spin_lock_init(&wb_connector->fence_lock);
217 snprintf(wb_connector->timeline_name,
218 sizeof(wb_connector->timeline_name),
219 "CONNECTOR:%d-%s", connector->base.id, connector->name);
220
221 drm_object_attach_property(&connector->base,
222 config->writeback_out_fence_ptr_property, 0);
223
224 drm_object_attach_property(&connector->base,
225 config->writeback_fb_id_property, 0);
226
227 drm_object_attach_property(&connector->base,
228 config->writeback_pixel_formats_property,
229 blob->base.id);
230 wb_connector->pixel_formats_blob_ptr = blob;
231
232 return 0;
233
234 attach_fail:
235 drm_connector_cleanup(connector);
236 connector_fail:
237 drm_encoder_cleanup(&wb_connector->encoder);
238 fail:
239 drm_property_blob_put(blob);
240 return ret;
241 }
242 EXPORT_SYMBOL(drm_writeback_connector_init);
243
244 int drm_writeback_set_fb(struct drm_connector_state *conn_state,
245 struct drm_framebuffer *fb)
246 {
247 WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
248
249 if (!conn_state->writeback_job) {
250 conn_state->writeback_job =
251 kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
252 if (!conn_state->writeback_job)
253 return -ENOMEM;
254
255 conn_state->writeback_job->connector =
256 drm_connector_to_writeback(conn_state->connector);
257 }
258
259 drm_framebuffer_assign(&conn_state->writeback_job->fb, fb);
260 return 0;
261 }
262
263 int drm_writeback_prepare_job(struct drm_writeback_job *job)
264 {
265 struct drm_writeback_connector *connector = job->connector;
266 const struct drm_connector_helper_funcs *funcs =
267 connector->base.helper_private;
268 int ret;
269
270 if (funcs->prepare_writeback_job) {
271 ret = funcs->prepare_writeback_job(connector, job);
272 if (ret < 0)
273 return ret;
274 }
275
276 job->prepared = true;
277 return 0;
278 }
279 EXPORT_SYMBOL(drm_writeback_prepare_job);
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector,
301 struct drm_connector_state *conn_state)
302 {
303 struct drm_writeback_job *job;
304 unsigned long flags;
305
306 job = conn_state->writeback_job;
307 conn_state->writeback_job = NULL;
308
309 spin_lock_irqsave(&wb_connector->job_lock, flags);
310 list_add_tail(&job->list_entry, &wb_connector->job_queue);
311 spin_unlock_irqrestore(&wb_connector->job_lock, flags);
312 }
313 EXPORT_SYMBOL(drm_writeback_queue_job);
314
315 void drm_writeback_cleanup_job(struct drm_writeback_job *job)
316 {
317 struct drm_writeback_connector *connector = job->connector;
318 const struct drm_connector_helper_funcs *funcs =
319 connector->base.helper_private;
320
321 if (job->prepared && funcs->cleanup_writeback_job)
322 funcs->cleanup_writeback_job(connector, job);
323
324 if (job->fb)
325 drm_framebuffer_put(job->fb);
326
327 if (job->out_fence)
328 dma_fence_put(job->out_fence);
329
330 kfree(job);
331 }
332 EXPORT_SYMBOL(drm_writeback_cleanup_job);
333
334
335
336
337
338
339
340
341 static void cleanup_work(struct work_struct *work)
342 {
343 struct drm_writeback_job *job = container_of(work,
344 struct drm_writeback_job,
345 cleanup_work);
346
347 drm_writeback_cleanup_job(job);
348 }
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 void
367 drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector,
368 int status)
369 {
370 unsigned long flags;
371 struct drm_writeback_job *job;
372 struct dma_fence *out_fence;
373
374 spin_lock_irqsave(&wb_connector->job_lock, flags);
375 job = list_first_entry_or_null(&wb_connector->job_queue,
376 struct drm_writeback_job,
377 list_entry);
378 if (job)
379 list_del(&job->list_entry);
380
381 spin_unlock_irqrestore(&wb_connector->job_lock, flags);
382
383 if (WARN_ON(!job))
384 return;
385
386 out_fence = job->out_fence;
387 if (out_fence) {
388 if (status)
389 dma_fence_set_error(out_fence, status);
390 dma_fence_signal(out_fence);
391 dma_fence_put(out_fence);
392 job->out_fence = NULL;
393 }
394
395 INIT_WORK(&job->cleanup_work, cleanup_work);
396 queue_work(system_long_wq, &job->cleanup_work);
397 }
398 EXPORT_SYMBOL(drm_writeback_signal_completion);
399
400 struct dma_fence *
401 drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector)
402 {
403 struct dma_fence *fence;
404
405 if (WARN_ON(wb_connector->base.connector_type !=
406 DRM_MODE_CONNECTOR_WRITEBACK))
407 return NULL;
408
409 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
410 if (!fence)
411 return NULL;
412
413 dma_fence_init(fence, &drm_writeback_fence_ops,
414 &wb_connector->fence_lock, wb_connector->fence_context,
415 ++wb_connector->fence_seqno);
416
417 return fence;
418 }
419 EXPORT_SYMBOL(drm_writeback_get_out_fence);