1
2
3
4
5
6
7
8
9
10
11
12#include <linux/dma-buf.h>
13#include <linux/dma-fence.h>
14#include <linux/reservation.h>
15#include <linux/slab.h>
16
17#include <drm/drmP.h>
18#include <drm/drm_atomic.h>
19#include <drm/drm_atomic_uapi.h>
20#include <drm/drm_damage_helper.h>
21#include <drm/drm_fb_helper.h>
22#include <drm/drm_fourcc.h>
23#include <drm/drm_framebuffer.h>
24#include <drm/drm_gem.h>
25#include <drm/drm_gem_framebuffer_helper.h>
26#include <drm/drm_modeset_helper.h>
27#include <drm/drm_simple_kms_helper.h>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
53 unsigned int plane)
54{
55 if (plane >= 4)
56 return NULL;
57
58 return fb->obj[plane];
59}
60EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
61
62static struct drm_framebuffer *
63drm_gem_fb_alloc(struct drm_device *dev,
64 const struct drm_mode_fb_cmd2 *mode_cmd,
65 struct drm_gem_object **obj, unsigned int num_planes,
66 const struct drm_framebuffer_funcs *funcs)
67{
68 struct drm_framebuffer *fb;
69 int ret, i;
70
71 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
72 if (!fb)
73 return ERR_PTR(-ENOMEM);
74
75 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
76
77 for (i = 0; i < num_planes; i++)
78 fb->obj[i] = obj[i];
79
80 ret = drm_framebuffer_init(dev, fb, funcs);
81 if (ret) {
82 DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n",
83 ret);
84 kfree(fb);
85 return ERR_PTR(ret);
86 }
87
88 return fb;
89}
90
91
92
93
94
95
96
97
98
99void drm_gem_fb_destroy(struct drm_framebuffer *fb)
100{
101 int i;
102
103 for (i = 0; i < 4; i++)
104 drm_gem_object_put_unlocked(fb->obj[i]);
105
106 drm_framebuffer_cleanup(fb);
107 kfree(fb);
108}
109EXPORT_SYMBOL(drm_gem_fb_destroy);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
125 unsigned int *handle)
126{
127 return drm_gem_handle_create(file, fb->obj[0], handle);
128}
129EXPORT_SYMBOL(drm_gem_fb_create_handle);
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147struct drm_framebuffer *
148drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
149 const struct drm_mode_fb_cmd2 *mode_cmd,
150 const struct drm_framebuffer_funcs *funcs)
151{
152 const struct drm_format_info *info;
153 struct drm_gem_object *objs[4];
154 struct drm_framebuffer *fb;
155 int ret, i;
156
157 info = drm_get_format_info(dev, mode_cmd);
158 if (!info)
159 return ERR_PTR(-EINVAL);
160
161 for (i = 0; i < info->num_planes; i++) {
162 unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
163 unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
164 unsigned int min_size;
165
166 objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
167 if (!objs[i]) {
168 DRM_DEBUG_KMS("Failed to lookup GEM object\n");
169 ret = -ENOENT;
170 goto err_gem_object_put;
171 }
172
173 min_size = (height - 1) * mode_cmd->pitches[i]
174 + drm_format_info_min_pitch(info, i, width)
175 + mode_cmd->offsets[i];
176
177 if (objs[i]->size < min_size) {
178 drm_gem_object_put_unlocked(objs[i]);
179 ret = -EINVAL;
180 goto err_gem_object_put;
181 }
182 }
183
184 fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs);
185 if (IS_ERR(fb)) {
186 ret = PTR_ERR(fb);
187 goto err_gem_object_put;
188 }
189
190 return fb;
191
192err_gem_object_put:
193 for (i--; i >= 0; i--)
194 drm_gem_object_put_unlocked(objs[i]);
195
196 return ERR_PTR(ret);
197}
198EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
199
200static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
201 .destroy = drm_gem_fb_destroy,
202 .create_handle = drm_gem_fb_create_handle,
203};
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227struct drm_framebuffer *
228drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
229 const struct drm_mode_fb_cmd2 *mode_cmd)
230{
231 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
232 &drm_gem_fb_funcs);
233}
234EXPORT_SYMBOL_GPL(drm_gem_fb_create);
235
236static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb = {
237 .destroy = drm_gem_fb_destroy,
238 .create_handle = drm_gem_fb_create_handle,
239 .dirty = drm_atomic_helper_dirtyfb,
240};
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265struct drm_framebuffer *
266drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
267 const struct drm_mode_fb_cmd2 *mode_cmd)
268{
269 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
270 &drm_gem_fb_funcs_dirtyfb);
271}
272EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289int drm_gem_fb_prepare_fb(struct drm_plane *plane,
290 struct drm_plane_state *state)
291{
292 struct dma_buf *dma_buf;
293 struct dma_fence *fence;
294
295 if (!state->fb)
296 return 0;
297
298 dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf;
299 if (dma_buf) {
300 fence = reservation_object_get_excl_rcu(dma_buf->resv);
301 drm_atomic_set_fence_for_plane(state, fence);
302 }
303
304 return 0;
305}
306EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
307
308
309
310
311
312
313
314
315
316
317
318
319int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
320 struct drm_plane_state *plane_state)
321{
322 return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
323}
324EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb);
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342struct drm_framebuffer *
343drm_gem_fbdev_fb_create(struct drm_device *dev,
344 struct drm_fb_helper_surface_size *sizes,
345 unsigned int pitch_align, struct drm_gem_object *obj,
346 const struct drm_framebuffer_funcs *funcs)
347{
348 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
349
350 mode_cmd.width = sizes->surface_width;
351 mode_cmd.height = sizes->surface_height;
352 mode_cmd.pitches[0] = sizes->surface_width *
353 DIV_ROUND_UP(sizes->surface_bpp, 8);
354 if (pitch_align)
355 mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0],
356 pitch_align);
357 mode_cmd.pixel_format = drm_driver_legacy_fb_format(dev, sizes->surface_bpp,
358 sizes->surface_depth);
359 if (obj->size < mode_cmd.pitches[0] * mode_cmd.height)
360 return ERR_PTR(-EINVAL);
361
362 if (!funcs)
363 funcs = &drm_gem_fb_funcs;
364
365 return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
366}
367EXPORT_SYMBOL(drm_gem_fbdev_fb_create);
368