1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drm gem framebuffer helper functions
5 * Copyright (C) 2017 Noralf Trønnes
8 #include <linux/dma-buf.h>
9 #include <linux/dma-fence.h>
10 #include <linux/dma-resv.h>
11 #include <linux/slab.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_uapi.h>
15 #include <drm/drm_damage_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_gem.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_modeset_helper.h>
22 #include <drm/drm_simple_kms_helper.h>
27 * This library provides helpers for drivers that don't subclass
28 * &drm_framebuffer and use &drm_gem_object for their backing storage.
30 * Drivers without additional needs to validate framebuffers can simply use
31 * drm_gem_fb_create() and everything is wired up automatically. Other drivers
32 * can use all parts independently.
36 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
40 * No additional reference is taken beyond the one that the &drm_frambuffer
44 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
45 * if it does not exist.
47 struct drm_gem_object
*drm_gem_fb_get_obj(struct drm_framebuffer
*fb
,
53 return fb
->obj
[plane
];
55 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj
);
57 static struct drm_framebuffer
*
58 drm_gem_fb_alloc(struct drm_device
*dev
,
59 const struct drm_mode_fb_cmd2
*mode_cmd
,
60 struct drm_gem_object
**obj
, unsigned int num_planes
,
61 const struct drm_framebuffer_funcs
*funcs
)
63 struct drm_framebuffer
*fb
;
66 fb
= kzalloc(sizeof(*fb
), GFP_KERNEL
);
68 return ERR_PTR(-ENOMEM
);
70 drm_helper_mode_fill_fb_struct(dev
, fb
, mode_cmd
);
72 for (i
= 0; i
< num_planes
; i
++)
75 ret
= drm_framebuffer_init(dev
, fb
, funcs
);
77 drm_err(dev
, "Failed to init framebuffer: %d\n", ret
);
86 * drm_gem_fb_destroy - Free GEM backed framebuffer
89 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
90 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
93 void drm_gem_fb_destroy(struct drm_framebuffer
*fb
)
97 for (i
= 0; i
< 4; i
++)
98 drm_gem_object_put_unlocked(fb
->obj
[i
]);
100 drm_framebuffer_cleanup(fb
);
103 EXPORT_SYMBOL(drm_gem_fb_destroy
);
106 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
108 * @file: DRM file to register the handle for
109 * @handle: Pointer to return the created handle
111 * This function creates a handle for the GEM object backing the framebuffer.
112 * Drivers can use this as their &drm_framebuffer_funcs->create_handle
113 * callback. The GETFB IOCTL calls into this callback.
116 * 0 on success or a negative error code on failure.
118 int drm_gem_fb_create_handle(struct drm_framebuffer
*fb
, struct drm_file
*file
,
119 unsigned int *handle
)
121 return drm_gem_handle_create(file
, fb
->obj
[0], handle
);
123 EXPORT_SYMBOL(drm_gem_fb_create_handle
);
126 * drm_gem_fb_create_with_funcs() - Helper function for the
127 * &drm_mode_config_funcs.fb_create
130 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
131 * @mode_cmd: Metadata from the userspace framebuffer creation request
132 * @funcs: vtable to be used for the new framebuffer object
134 * This function can be used to set &drm_framebuffer_funcs for drivers that need
135 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
136 * change &drm_framebuffer_funcs. The function does buffer size validation.
139 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
141 struct drm_framebuffer
*
142 drm_gem_fb_create_with_funcs(struct drm_device
*dev
, struct drm_file
*file
,
143 const struct drm_mode_fb_cmd2
*mode_cmd
,
144 const struct drm_framebuffer_funcs
*funcs
)
146 const struct drm_format_info
*info
;
147 struct drm_gem_object
*objs
[4];
148 struct drm_framebuffer
*fb
;
151 info
= drm_get_format_info(dev
, mode_cmd
);
153 return ERR_PTR(-EINVAL
);
155 for (i
= 0; i
< info
->num_planes
; i
++) {
156 unsigned int width
= mode_cmd
->width
/ (i
? info
->hsub
: 1);
157 unsigned int height
= mode_cmd
->height
/ (i
? info
->vsub
: 1);
158 unsigned int min_size
;
160 objs
[i
] = drm_gem_object_lookup(file
, mode_cmd
->handles
[i
]);
162 drm_dbg_kms(dev
, "Failed to lookup GEM object\n");
164 goto err_gem_object_put
;
167 min_size
= (height
- 1) * mode_cmd
->pitches
[i
]
168 + drm_format_info_min_pitch(info
, i
, width
)
169 + mode_cmd
->offsets
[i
];
171 if (objs
[i
]->size
< min_size
) {
172 drm_gem_object_put_unlocked(objs
[i
]);
174 goto err_gem_object_put
;
178 fb
= drm_gem_fb_alloc(dev
, mode_cmd
, objs
, i
, funcs
);
181 goto err_gem_object_put
;
187 for (i
--; i
>= 0; i
--)
188 drm_gem_object_put_unlocked(objs
[i
]);
192 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs
);
194 static const struct drm_framebuffer_funcs drm_gem_fb_funcs
= {
195 .destroy
= drm_gem_fb_destroy
,
196 .create_handle
= drm_gem_fb_create_handle
,
200 * drm_gem_fb_create() - Helper function for the
201 * &drm_mode_config_funcs.fb_create callback
203 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
204 * @mode_cmd: Metadata from the userspace framebuffer creation request
206 * This function creates a new framebuffer object described by
207 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
208 * backing the framebuffer.
210 * If your hardware has special alignment or pitch requirements these should be
211 * checked before calling this function. The function does buffer size
212 * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
215 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
216 * The ADDFB2 IOCTL calls into this callback.
219 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
221 struct drm_framebuffer
*
222 drm_gem_fb_create(struct drm_device
*dev
, struct drm_file
*file
,
223 const struct drm_mode_fb_cmd2
*mode_cmd
)
225 return drm_gem_fb_create_with_funcs(dev
, file
, mode_cmd
,
228 EXPORT_SYMBOL_GPL(drm_gem_fb_create
);
230 static const struct drm_framebuffer_funcs drm_gem_fb_funcs_dirtyfb
= {
231 .destroy
= drm_gem_fb_destroy
,
232 .create_handle
= drm_gem_fb_create_handle
,
233 .dirty
= drm_atomic_helper_dirtyfb
,
237 * drm_gem_fb_create_with_dirty() - Helper function for the
238 * &drm_mode_config_funcs.fb_create callback
240 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
241 * @mode_cmd: Metadata from the userspace framebuffer creation request
243 * This function creates a new framebuffer object described by
244 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
245 * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
246 * callback giving framebuffer flushing through the atomic machinery. Use
247 * drm_gem_fb_create() if you don't need the dirty callback.
248 * The function does buffer size validation.
250 * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
251 * to enable userspace to use damage clips also with the ATOMIC IOCTL.
253 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
254 * The ADDFB2 IOCTL calls into this callback.
257 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
259 struct drm_framebuffer
*
260 drm_gem_fb_create_with_dirty(struct drm_device
*dev
, struct drm_file
*file
,
261 const struct drm_mode_fb_cmd2
*mode_cmd
)
263 return drm_gem_fb_create_with_funcs(dev
, file
, mode_cmd
,
264 &drm_gem_fb_funcs_dirtyfb
);
266 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty
);
269 * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
271 * @state: Plane state the fence will be attached to
273 * This function extracts the exclusive fence from &drm_gem_object.resv and
274 * attaches it to plane state for the atomic helper to wait on. This is
275 * necessary to correctly implement implicit synchronization for any buffers
276 * shared as a struct &dma_buf. This function can be used as the
277 * &drm_plane_helper_funcs.prepare_fb callback.
279 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
280 * gem based framebuffer drivers which have their buffers always pinned in
283 * See drm_atomic_set_fence_for_plane() for a discussion of implicit and
284 * explicit fencing in atomic modeset updates.
286 int drm_gem_fb_prepare_fb(struct drm_plane
*plane
,
287 struct drm_plane_state
*state
)
289 struct drm_gem_object
*obj
;
290 struct dma_fence
*fence
;
295 obj
= drm_gem_fb_get_obj(state
->fb
, 0);
296 fence
= dma_resv_get_excl_rcu(obj
->resv
);
297 drm_atomic_set_fence_for_plane(state
, fence
);
301 EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb
);
304 * drm_gem_fb_simple_display_pipe_prepare_fb - prepare_fb helper for
305 * &drm_simple_display_pipe
306 * @pipe: Simple display pipe
307 * @plane_state: Plane state
309 * This function uses drm_gem_fb_prepare_fb() to extract the exclusive fence
310 * from &drm_gem_object.resv and attaches it to plane state for the atomic
311 * helper to wait on. This is necessary to correctly implement implicit
312 * synchronization for any buffers shared as a struct &dma_buf. Drivers can use
313 * this as their &drm_simple_display_pipe_funcs.prepare_fb callback.
315 * See drm_atomic_set_fence_for_plane() for a discussion of implicit and
316 * explicit fencing in atomic modeset updates.
318 int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe
*pipe
,
319 struct drm_plane_state
*plane_state
)
321 return drm_gem_fb_prepare_fb(&pipe
->plane
, plane_state
);
323 EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb
);