dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / drm_gem_framebuffer_helper.c
blob65edb1ccb185f01840e2395436ab51159ee67dce
1 /*
2 * drm gem framebuffer helper functions
4 * Copyright (C) 2017 Noralf Trønnes
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/dma-buf.h>
13 #include <linux/dma-fence.h>
14 #include <linux/reservation.h>
15 #include <linux/slab.h>
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>
29 /**
30 * DOC: overview
32 * This library provides helpers for drivers that don't subclass
33 * &drm_framebuffer and use &drm_gem_object for their backing storage.
35 * Drivers without additional needs to validate framebuffers can simply use
36 * drm_gem_fb_create() and everything is wired up automatically. Other drivers
37 * can use all parts independently.
40 /**
41 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
42 * @fb: Framebuffer
43 * @plane: Plane index
45 * No additional reference is taken beyond the one that the &drm_frambuffer
46 * already holds.
48 * Returns:
49 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
50 * if it does not exist.
52 struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
53 unsigned int plane)
55 if (plane >= 4)
56 return NULL;
58 return fb->obj[plane];
60 EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
62 static struct drm_framebuffer *
63 drm_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)
68 struct drm_framebuffer *fb;
69 int ret, i;
71 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
72 if (!fb)
73 return ERR_PTR(-ENOMEM);
75 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
77 for (i = 0; i < num_planes; i++)
78 fb->obj[i] = obj[i];
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);
88 return fb;
91 /**
92 * drm_gem_fb_destroy - Free GEM backed framebuffer
93 * @fb: Framebuffer
95 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
96 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
97 * callback.
99 void drm_gem_fb_destroy(struct drm_framebuffer *fb)
101 int i;
103 for (i = 0; i < 4; i++)
104 drm_gem_object_put_unlocked(fb->obj[i]);
106 drm_framebuffer_cleanup(fb);
107 kfree(fb);
109 EXPORT_SYMBOL(drm_gem_fb_destroy);
112 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
113 * @fb: Framebuffer
114 * @file: DRM file to register the handle for
115 * @handle: Pointer to return the created handle
117 * This function creates a handle for the GEM object backing the framebuffer.
118 * Drivers can use this as their &drm_framebuffer_funcs->create_handle
119 * callback. The GETFB IOCTL calls into this callback.
121 * Returns:
122 * 0 on success or a negative error code on failure.
124 int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
125 unsigned int *handle)
127 return drm_gem_handle_create(file, fb->obj[0], handle);
129 EXPORT_SYMBOL(drm_gem_fb_create_handle);
132 * drm_gem_fb_create_with_funcs() - Helper function for the
133 * &drm_mode_config_funcs.fb_create
134 * callback
135 * @dev: DRM device
136 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
137 * @mode_cmd: Metadata from the userspace framebuffer creation request
138 * @funcs: vtable to be used for the new framebuffer object
140 * This function can be used to set &drm_framebuffer_funcs for drivers that need
141 * custom framebuffer callbacks. Use drm_gem_fb_create() if you don't need to
142 * change &drm_framebuffer_funcs. The function does buffer size validation.
144 * Returns:
145 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
147 struct drm_framebuffer *
148 drm_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)
152 const struct drm_format_info *info;
153 struct drm_gem_object *objs[4];
154 struct drm_framebuffer *fb;
155 int ret, i;
157 info = drm_get_format_info(dev, mode_cmd);
158 if (!info)
159 return ERR_PTR(-EINVAL);
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;
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;
173 min_size = (height - 1) * mode_cmd->pitches[i]
174 + drm_format_info_min_pitch(info, i, width)
175 + mode_cmd->offsets[i];
177 if (objs[i]->size < min_size) {
178 drm_gem_object_put_unlocked(objs[i]);
179 ret = -EINVAL;
180 goto err_gem_object_put;
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;
190 return fb;
192 err_gem_object_put:
193 for (i--; i >= 0; i--)
194 drm_gem_object_put_unlocked(objs[i]);
196 return ERR_PTR(ret);
198 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
200 static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
201 .destroy = drm_gem_fb_destroy,
202 .create_handle = drm_gem_fb_create_handle,
206 * drm_gem_fb_create() - Helper function for the
207 * &drm_mode_config_funcs.fb_create callback
208 * @dev: DRM device
209 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
210 * @mode_cmd: Metadata from the userspace framebuffer creation request
212 * This function creates a new framebuffer object described by
213 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
214 * backing the framebuffer.
216 * If your hardware has special alignment or pitch requirements these should be
217 * checked before calling this function. The function does buffer size
218 * validation. Use drm_gem_fb_create_with_dirty() if you need framebuffer
219 * flushing.
221 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
222 * The ADDFB2 IOCTL calls into this callback.
224 * Returns:
225 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
227 struct drm_framebuffer *
228 drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
229 const struct drm_mode_fb_cmd2 *mode_cmd)
231 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
232 &drm_gem_fb_funcs);
234 EXPORT_SYMBOL_GPL(drm_gem_fb_create);
236 static 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,
243 * drm_gem_fb_create_with_dirty() - Helper function for the
244 * &drm_mode_config_funcs.fb_create callback
245 * @dev: DRM device
246 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
247 * @mode_cmd: Metadata from the userspace framebuffer creation request
249 * This function creates a new framebuffer object described by
250 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
251 * backing the framebuffer. drm_atomic_helper_dirtyfb() is used for the dirty
252 * callback giving framebuffer flushing through the atomic machinery. Use
253 * drm_gem_fb_create() if you don't need the dirty callback.
254 * The function does buffer size validation.
256 * Drivers should also call drm_plane_enable_fb_damage_clips() on all planes
257 * to enable userspace to use damage clips also with the ATOMIC IOCTL.
259 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
260 * The ADDFB2 IOCTL calls into this callback.
262 * Returns:
263 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
265 struct drm_framebuffer *
266 drm_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
267 const struct drm_mode_fb_cmd2 *mode_cmd)
269 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
270 &drm_gem_fb_funcs_dirtyfb);
272 EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_dirty);
275 * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
276 * @plane: Plane
277 * @state: Plane state the fence will be attached to
279 * This function prepares a GEM backed framebuffer for scanout by checking if
280 * the plane framebuffer has a DMA-BUF attached. If it does, it extracts the
281 * exclusive fence and attaches it to the plane state for the atomic helper to
282 * wait on. This function can be used as the &drm_plane_helper_funcs.prepare_fb
283 * callback.
285 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
286 * gem based framebuffer drivers which have their buffers always pinned in
287 * memory.
289 int drm_gem_fb_prepare_fb(struct drm_plane *plane,
290 struct drm_plane_state *state)
292 struct dma_buf *dma_buf;
293 struct dma_fence *fence;
295 if (!state->fb)
296 return 0;
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);
304 return 0;
306 EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
309 * drm_gem_fb_simple_display_pipe_prepare_fb - prepare_fb helper for
310 * &drm_simple_display_pipe
311 * @pipe: Simple display pipe
312 * @plane_state: Plane state
314 * This function uses drm_gem_fb_prepare_fb() to check if the plane FB has a
315 * &dma_buf attached, extracts the exclusive fence and attaches it to plane
316 * state for the atomic helper to wait on. Drivers can use this as their
317 * &drm_simple_display_pipe_funcs.prepare_fb callback.
319 int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
320 struct drm_plane_state *plane_state)
322 return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
324 EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb);
327 * drm_gem_fbdev_fb_create - Create a GEM backed &drm_framebuffer for fbdev
328 * emulation
329 * @dev: DRM device
330 * @sizes: fbdev size description
331 * @pitch_align: Optional pitch alignment
332 * @obj: GEM object backing the framebuffer
333 * @funcs: Optional vtable to be used for the new framebuffer object when the
334 * dirty callback is needed.
336 * This function creates a framebuffer from a &drm_fb_helper_surface_size
337 * description for use in the &drm_fb_helper_funcs.fb_probe callback.
339 * Returns:
340 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
342 struct drm_framebuffer *
343 drm_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)
348 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
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);
362 if (!funcs)
363 funcs = &drm_gem_fb_funcs;
365 return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
367 EXPORT_SYMBOL(drm_gem_fbdev_fb_create);