blob: 2810d413141182e4b6298524612670aa34a54757 [file] [log] [blame]
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +02001/*
2 * drm gem framebuffer helper functions
3 *
4 * Copyright (C) 2017 Noralf Trønnes
5 *
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.
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_fb_helper.h>
20#include <drm/drm_fourcc.h>
21#include <drm/drm_framebuffer.h>
22#include <drm/drm_gem.h>
23#include <drm/drm_gem_framebuffer_helper.h>
24#include <drm/drm_modeset_helper.h>
Daniel Vetterccc3b2b2018-04-05 17:44:42 +020025#include <drm/drm_simple_kms_helper.h>
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +020026
27/**
28 * DOC: overview
29 *
30 * This library provides helpers for drivers that don't subclass
Noralf Trønnes2e187b22017-09-22 17:47:44 +020031 * &drm_framebuffer and use &drm_gem_object for their backing storage.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +020032 *
33 * Drivers without additional needs to validate framebuffers can simply use
Noralf Trønnes2e187b22017-09-22 17:47:44 +020034 * drm_gem_fb_create() and everything is wired up automatically. Other drivers
35 * can use all parts independently.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +020036 */
37
38/**
Noralf Trønnes2e187b22017-09-22 17:47:44 +020039 * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
40 * @fb: Framebuffer
41 * @plane: Plane index
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +020042 *
Noralf Trønnes2e187b22017-09-22 17:47:44 +020043 * No additional reference is taken beyond the one that the &drm_frambuffer
44 * already holds.
45 *
46 * Returns:
47 * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
48 * if it does not exist.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +020049 */
50struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
51 unsigned int plane)
52{
53 if (plane >= 4)
54 return NULL;
55
56 return fb->obj[plane];
57}
58EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
59
60static struct drm_framebuffer *
61drm_gem_fb_alloc(struct drm_device *dev,
62 const struct drm_mode_fb_cmd2 *mode_cmd,
63 struct drm_gem_object **obj, unsigned int num_planes,
64 const struct drm_framebuffer_funcs *funcs)
65{
66 struct drm_framebuffer *fb;
67 int ret, i;
68
69 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
70 if (!fb)
71 return ERR_PTR(-ENOMEM);
72
73 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
74
75 for (i = 0; i < num_planes; i++)
76 fb->obj[i] = obj[i];
77
78 ret = drm_framebuffer_init(dev, fb, funcs);
79 if (ret) {
80 DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n",
81 ret);
82 kfree(fb);
83 return ERR_PTR(ret);
84 }
85
86 return fb;
87}
88
89/**
90 * drm_gem_fb_destroy - Free GEM backed framebuffer
Noralf Trønnes2e187b22017-09-22 17:47:44 +020091 * @fb: Framebuffer
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +020092 *
93 * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
94 * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
95 * callback.
96 */
97void drm_gem_fb_destroy(struct drm_framebuffer *fb)
98{
99 int i;
100
101 for (i = 0; i < 4; i++)
102 drm_gem_object_put_unlocked(fb->obj[i]);
103
104 drm_framebuffer_cleanup(fb);
105 kfree(fb);
106}
107EXPORT_SYMBOL(drm_gem_fb_destroy);
108
109/**
110 * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200111 * @fb: Framebuffer
112 * @file: DRM file to register the handle for
113 * @handle: Pointer to return the created handle
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200114 *
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200115 * This function creates a handle for the GEM object backing the framebuffer.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200116 * Drivers can use this as their &drm_framebuffer_funcs->create_handle
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200117 * callback. The GETFB IOCTL calls into this callback.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200118 *
119 * Returns:
120 * 0 on success or a negative error code on failure.
121 */
122int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
123 unsigned int *handle)
124{
125 return drm_gem_handle_create(file, fb->obj[0], handle);
126}
127EXPORT_SYMBOL(drm_gem_fb_create_handle);
128
129/**
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200130 * drm_gem_fb_create_with_funcs() - Helper function for the
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200131 * &drm_mode_config_funcs.fb_create
132 * callback
133 * @dev: DRM device
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200134 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
135 * @mode_cmd: Metadata from the userspace framebuffer creation request
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200136 * @funcs: vtable to be used for the new framebuffer object
137 *
138 * This can be used to set &drm_framebuffer_funcs for drivers that need the
139 * &drm_framebuffer_funcs.dirty callback. Use drm_gem_fb_create() if you don't
140 * need to change &drm_framebuffer_funcs.
141 * The function does buffer size validation.
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200142 *
143 * Returns:
144 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200145 */
146struct drm_framebuffer *
147drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
148 const struct drm_mode_fb_cmd2 *mode_cmd,
149 const struct drm_framebuffer_funcs *funcs)
150{
151 const struct drm_format_info *info;
152 struct drm_gem_object *objs[4];
153 struct drm_framebuffer *fb;
154 int ret, i;
155
156 info = drm_get_format_info(dev, mode_cmd);
157 if (!info)
158 return ERR_PTR(-EINVAL);
159
160 for (i = 0; i < info->num_planes; i++) {
161 unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
162 unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
163 unsigned int min_size;
164
165 objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
166 if (!objs[i]) {
Noralf Trønnesa5ea8a62017-09-11 18:37:45 +0200167 DRM_DEBUG_KMS("Failed to lookup GEM object\n");
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200168 ret = -ENOENT;
169 goto err_gem_object_put;
170 }
171
172 min_size = (height - 1) * mode_cmd->pitches[i]
173 + width * info->cpp[i]
174 + mode_cmd->offsets[i];
175
176 if (objs[i]->size < min_size) {
177 drm_gem_object_put_unlocked(objs[i]);
178 ret = -EINVAL;
179 goto err_gem_object_put;
180 }
181 }
182
183 fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs);
184 if (IS_ERR(fb)) {
185 ret = PTR_ERR(fb);
186 goto err_gem_object_put;
187 }
188
189 return fb;
190
191err_gem_object_put:
192 for (i--; i >= 0; i--)
193 drm_gem_object_put_unlocked(objs[i]);
194
195 return ERR_PTR(ret);
196}
197EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
198
199static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
200 .destroy = drm_gem_fb_destroy,
201 .create_handle = drm_gem_fb_create_handle,
202};
203
204/**
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200205 * drm_gem_fb_create() - Helper function for the
206 * &drm_mode_config_funcs.fb_create callback
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200207 * @dev: DRM device
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200208 * @file: DRM file that holds the GEM handle(s) backing the framebuffer
209 * @mode_cmd: Metadata from the userspace framebuffer creation request
210 *
211 * This function creates a new framebuffer object described by
212 * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
213 * backing the framebuffer.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200214 *
215 * If your hardware has special alignment or pitch requirements these should be
216 * checked before calling this function. The function does buffer size
217 * validation. Use drm_gem_fb_create_with_funcs() if you need to set
218 * &drm_framebuffer_funcs.dirty.
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200219 *
220 * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
221 * The ADDFB2 IOCTL calls into this callback.
222 *
223 * Returns:
224 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200225 */
226struct drm_framebuffer *
227drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
228 const struct drm_mode_fb_cmd2 *mode_cmd)
229{
230 return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
231 &drm_gem_fb_funcs);
232}
233EXPORT_SYMBOL_GPL(drm_gem_fb_create);
234
235/**
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200236 * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
237 * @plane: Plane
238 * @state: Plane state the fence will be attached to
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200239 *
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200240 * This function prepares a GEM backed framebuffer for scanout by checking if
241 * the plane framebuffer has a DMA-BUF attached. If it does, it extracts the
242 * exclusive fence and attaches it to the plane state for the atomic helper to
243 * wait on. This function can be used as the &drm_plane_helper_funcs.prepare_fb
244 * callback.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200245 *
246 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
247 * gem based framebuffer drivers which have their buffers always pinned in
248 * memory.
249 */
250int drm_gem_fb_prepare_fb(struct drm_plane *plane,
251 struct drm_plane_state *state)
252{
253 struct dma_buf *dma_buf;
254 struct dma_fence *fence;
255
Daniel Vetter9d54fcd2018-04-05 17:44:47 +0200256 if (!state->fb)
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200257 return 0;
258
259 dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf;
260 if (dma_buf) {
261 fence = reservation_object_get_excl_rcu(dma_buf->resv);
262 drm_atomic_set_fence_for_plane(state, fence);
263 }
264
265 return 0;
266}
267EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
268
269/**
Daniel Vetterccc3b2b2018-04-05 17:44:42 +0200270 * drm_gem_fb_simple_display_pipe_prepare_fb - prepare_fb helper for
271 * &drm_simple_display_pipe
272 * @pipe: Simple display pipe
273 * @plane_state: Plane state
274 *
275 * This function uses drm_gem_fb_prepare_fb() to check if the plane FB has a
276 * &dma_buf attached, extracts the exclusive fence and attaches it to plane
277 * state for the atomic helper to wait on. Drivers can use this as their
278 * &drm_simple_display_pipe_funcs.prepare_fb callback.
279 */
280int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
281 struct drm_plane_state *plane_state)
282{
283 return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
284}
285EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb);
286
287/**
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200288 * drm_gem_fbdev_fb_create - Create a GEM backed &drm_framebuffer for fbdev
289 * emulation
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200290 * @dev: DRM device
291 * @sizes: fbdev size description
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200292 * @pitch_align: Optional pitch alignment
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200293 * @obj: GEM object backing the framebuffer
Noralf Trønnese44f0802017-11-15 15:19:40 +0100294 * @funcs: Optional vtable to be used for the new framebuffer object when the
295 * dirty callback is needed.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200296 *
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200297 * This function creates a framebuffer from a &drm_fb_helper_surface_size
298 * description for use in the &drm_fb_helper_funcs.fb_probe callback.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200299 *
300 * Returns:
Noralf Trønnes2e187b22017-09-22 17:47:44 +0200301 * Pointer to a &drm_framebuffer on success or an error pointer on failure.
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200302 */
303struct drm_framebuffer *
304drm_gem_fbdev_fb_create(struct drm_device *dev,
305 struct drm_fb_helper_surface_size *sizes,
306 unsigned int pitch_align, struct drm_gem_object *obj,
307 const struct drm_framebuffer_funcs *funcs)
308{
309 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
310
311 mode_cmd.width = sizes->surface_width;
312 mode_cmd.height = sizes->surface_height;
313 mode_cmd.pitches[0] = sizes->surface_width *
314 DIV_ROUND_UP(sizes->surface_bpp, 8);
315 if (pitch_align)
316 mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0],
317 pitch_align);
318 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
319 sizes->surface_depth);
320 if (obj->size < mode_cmd.pitches[0] * mode_cmd.height)
321 return ERR_PTR(-EINVAL);
322
Noralf Trønnese44f0802017-11-15 15:19:40 +0100323 if (!funcs)
324 funcs = &drm_gem_fb_funcs;
325
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200326 return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
327}
328EXPORT_SYMBOL(drm_gem_fbdev_fb_create);