1*78973132SSergey Zigachev /*
2*78973132SSergey Zigachev * drm gem framebuffer helper functions
3*78973132SSergey Zigachev *
4*78973132SSergey Zigachev * Copyright (C) 2017 Noralf Trønnes
5*78973132SSergey Zigachev *
6*78973132SSergey Zigachev * This program is free software; you can redistribute it and/or modify
7*78973132SSergey Zigachev * it under the terms of the GNU General Public License as published by
8*78973132SSergey Zigachev * the Free Software Foundation; either version 2 of the License, or
9*78973132SSergey Zigachev * (at your option) any later version.
10*78973132SSergey Zigachev */
11*78973132SSergey Zigachev
12*78973132SSergey Zigachev #include <linux/dma-buf.h>
13*78973132SSergey Zigachev #include <linux/dma-fence.h>
14*78973132SSergey Zigachev #include <linux/reservation.h>
15*78973132SSergey Zigachev #include <linux/slab.h>
16*78973132SSergey Zigachev
17*78973132SSergey Zigachev #include <drm/drmP.h>
18*78973132SSergey Zigachev #include <drm/drm_atomic.h>
19*78973132SSergey Zigachev #include <drm/drm_fb_helper.h>
20*78973132SSergey Zigachev #include <drm/drm_fourcc.h>
21*78973132SSergey Zigachev #include <drm/drm_framebuffer.h>
22*78973132SSergey Zigachev #include <drm/drm_gem.h>
23*78973132SSergey Zigachev #include <drm/drm_gem_framebuffer_helper.h>
24*78973132SSergey Zigachev #include <drm/drm_modeset_helper.h>
25*78973132SSergey Zigachev #include <drm/drm_simple_kms_helper.h>
26*78973132SSergey Zigachev
27*78973132SSergey Zigachev /**
28*78973132SSergey Zigachev * DOC: overview
29*78973132SSergey Zigachev *
30*78973132SSergey Zigachev * This library provides helpers for drivers that don't subclass
31*78973132SSergey Zigachev * &drm_framebuffer and use &drm_gem_object for their backing storage.
32*78973132SSergey Zigachev *
33*78973132SSergey Zigachev * Drivers without additional needs to validate framebuffers can simply use
34*78973132SSergey Zigachev * drm_gem_fb_create() and everything is wired up automatically. Other drivers
35*78973132SSergey Zigachev * can use all parts independently.
36*78973132SSergey Zigachev */
37*78973132SSergey Zigachev
38*78973132SSergey Zigachev /**
39*78973132SSergey Zigachev * drm_gem_fb_get_obj() - Get GEM object backing the framebuffer
40*78973132SSergey Zigachev * @fb: Framebuffer
41*78973132SSergey Zigachev * @plane: Plane index
42*78973132SSergey Zigachev *
43*78973132SSergey Zigachev * No additional reference is taken beyond the one that the &drm_frambuffer
44*78973132SSergey Zigachev * already holds.
45*78973132SSergey Zigachev *
46*78973132SSergey Zigachev * Returns:
47*78973132SSergey Zigachev * Pointer to &drm_gem_object for the given framebuffer and plane index or NULL
48*78973132SSergey Zigachev * if it does not exist.
49*78973132SSergey Zigachev */
drm_gem_fb_get_obj(struct drm_framebuffer * fb,unsigned int plane)50*78973132SSergey Zigachev struct drm_gem_object *drm_gem_fb_get_obj(struct drm_framebuffer *fb,
51*78973132SSergey Zigachev unsigned int plane)
52*78973132SSergey Zigachev {
53*78973132SSergey Zigachev if (plane >= 4)
54*78973132SSergey Zigachev return NULL;
55*78973132SSergey Zigachev
56*78973132SSergey Zigachev return fb->obj[plane];
57*78973132SSergey Zigachev }
58*78973132SSergey Zigachev EXPORT_SYMBOL_GPL(drm_gem_fb_get_obj);
59*78973132SSergey Zigachev
60*78973132SSergey Zigachev static struct drm_framebuffer *
drm_gem_fb_alloc(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** obj,unsigned int num_planes,const struct drm_framebuffer_funcs * funcs)61*78973132SSergey Zigachev drm_gem_fb_alloc(struct drm_device *dev,
62*78973132SSergey Zigachev const struct drm_mode_fb_cmd2 *mode_cmd,
63*78973132SSergey Zigachev struct drm_gem_object **obj, unsigned int num_planes,
64*78973132SSergey Zigachev const struct drm_framebuffer_funcs *funcs)
65*78973132SSergey Zigachev {
66*78973132SSergey Zigachev struct drm_framebuffer *fb;
67*78973132SSergey Zigachev int ret, i;
68*78973132SSergey Zigachev
69*78973132SSergey Zigachev fb = kzalloc(sizeof(*fb), GFP_KERNEL);
70*78973132SSergey Zigachev if (!fb)
71*78973132SSergey Zigachev return ERR_PTR(-ENOMEM);
72*78973132SSergey Zigachev
73*78973132SSergey Zigachev drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
74*78973132SSergey Zigachev
75*78973132SSergey Zigachev for (i = 0; i < num_planes; i++)
76*78973132SSergey Zigachev fb->obj[i] = obj[i];
77*78973132SSergey Zigachev
78*78973132SSergey Zigachev ret = drm_framebuffer_init(dev, fb, funcs);
79*78973132SSergey Zigachev if (ret) {
80*78973132SSergey Zigachev DRM_DEV_ERROR(dev->dev, "Failed to init framebuffer: %d\n",
81*78973132SSergey Zigachev ret);
82*78973132SSergey Zigachev kfree(fb);
83*78973132SSergey Zigachev return ERR_PTR(ret);
84*78973132SSergey Zigachev }
85*78973132SSergey Zigachev
86*78973132SSergey Zigachev return fb;
87*78973132SSergey Zigachev }
88*78973132SSergey Zigachev
89*78973132SSergey Zigachev /**
90*78973132SSergey Zigachev * drm_gem_fb_destroy - Free GEM backed framebuffer
91*78973132SSergey Zigachev * @fb: Framebuffer
92*78973132SSergey Zigachev *
93*78973132SSergey Zigachev * Frees a GEM backed framebuffer with its backing buffer(s) and the structure
94*78973132SSergey Zigachev * itself. Drivers can use this as their &drm_framebuffer_funcs->destroy
95*78973132SSergey Zigachev * callback.
96*78973132SSergey Zigachev */
drm_gem_fb_destroy(struct drm_framebuffer * fb)97*78973132SSergey Zigachev void drm_gem_fb_destroy(struct drm_framebuffer *fb)
98*78973132SSergey Zigachev {
99*78973132SSergey Zigachev int i;
100*78973132SSergey Zigachev
101*78973132SSergey Zigachev for (i = 0; i < 4; i++)
102*78973132SSergey Zigachev drm_gem_object_put_unlocked(fb->obj[i]);
103*78973132SSergey Zigachev
104*78973132SSergey Zigachev drm_framebuffer_cleanup(fb);
105*78973132SSergey Zigachev kfree(fb);
106*78973132SSergey Zigachev }
107*78973132SSergey Zigachev EXPORT_SYMBOL(drm_gem_fb_destroy);
108*78973132SSergey Zigachev
109*78973132SSergey Zigachev /**
110*78973132SSergey Zigachev * drm_gem_fb_create_handle - Create handle for GEM backed framebuffer
111*78973132SSergey Zigachev * @fb: Framebuffer
112*78973132SSergey Zigachev * @file: DRM file to register the handle for
113*78973132SSergey Zigachev * @handle: Pointer to return the created handle
114*78973132SSergey Zigachev *
115*78973132SSergey Zigachev * This function creates a handle for the GEM object backing the framebuffer.
116*78973132SSergey Zigachev * Drivers can use this as their &drm_framebuffer_funcs->create_handle
117*78973132SSergey Zigachev * callback. The GETFB IOCTL calls into this callback.
118*78973132SSergey Zigachev *
119*78973132SSergey Zigachev * Returns:
120*78973132SSergey Zigachev * 0 on success or a negative error code on failure.
121*78973132SSergey Zigachev */
drm_gem_fb_create_handle(struct drm_framebuffer * fb,struct drm_file * file,unsigned int * handle)122*78973132SSergey Zigachev int drm_gem_fb_create_handle(struct drm_framebuffer *fb, struct drm_file *file,
123*78973132SSergey Zigachev unsigned int *handle)
124*78973132SSergey Zigachev {
125*78973132SSergey Zigachev return drm_gem_handle_create(file, fb->obj[0], handle);
126*78973132SSergey Zigachev }
127*78973132SSergey Zigachev EXPORT_SYMBOL(drm_gem_fb_create_handle);
128*78973132SSergey Zigachev
129*78973132SSergey Zigachev /**
130*78973132SSergey Zigachev * drm_gem_fb_create_with_funcs() - Helper function for the
131*78973132SSergey Zigachev * &drm_mode_config_funcs.fb_create
132*78973132SSergey Zigachev * callback
133*78973132SSergey Zigachev * @dev: DRM device
134*78973132SSergey Zigachev * @file: DRM file that holds the GEM handle(s) backing the framebuffer
135*78973132SSergey Zigachev * @mode_cmd: Metadata from the userspace framebuffer creation request
136*78973132SSergey Zigachev * @funcs: vtable to be used for the new framebuffer object
137*78973132SSergey Zigachev *
138*78973132SSergey Zigachev * This can be used to set &drm_framebuffer_funcs for drivers that need the
139*78973132SSergey Zigachev * &drm_framebuffer_funcs.dirty callback. Use drm_gem_fb_create() if you don't
140*78973132SSergey Zigachev * need to change &drm_framebuffer_funcs.
141*78973132SSergey Zigachev * The function does buffer size validation.
142*78973132SSergey Zigachev *
143*78973132SSergey Zigachev * Returns:
144*78973132SSergey Zigachev * Pointer to a &drm_framebuffer on success or an error pointer on failure.
145*78973132SSergey Zigachev */
146*78973132SSergey Zigachev struct drm_framebuffer *
drm_gem_fb_create_with_funcs(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd,const struct drm_framebuffer_funcs * funcs)147*78973132SSergey Zigachev drm_gem_fb_create_with_funcs(struct drm_device *dev, struct drm_file *file,
148*78973132SSergey Zigachev const struct drm_mode_fb_cmd2 *mode_cmd,
149*78973132SSergey Zigachev const struct drm_framebuffer_funcs *funcs)
150*78973132SSergey Zigachev {
151*78973132SSergey Zigachev const struct drm_format_info *info;
152*78973132SSergey Zigachev struct drm_gem_object *objs[4];
153*78973132SSergey Zigachev struct drm_framebuffer *fb;
154*78973132SSergey Zigachev int ret, i;
155*78973132SSergey Zigachev
156*78973132SSergey Zigachev info = drm_get_format_info(dev, mode_cmd);
157*78973132SSergey Zigachev if (!info)
158*78973132SSergey Zigachev return ERR_PTR(-EINVAL);
159*78973132SSergey Zigachev
160*78973132SSergey Zigachev for (i = 0; i < info->num_planes; i++) {
161*78973132SSergey Zigachev unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
162*78973132SSergey Zigachev unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
163*78973132SSergey Zigachev unsigned int min_size;
164*78973132SSergey Zigachev
165*78973132SSergey Zigachev objs[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
166*78973132SSergey Zigachev if (!objs[i]) {
167*78973132SSergey Zigachev DRM_DEBUG_KMS("Failed to lookup GEM object\n");
168*78973132SSergey Zigachev ret = -ENOENT;
169*78973132SSergey Zigachev goto err_gem_object_put;
170*78973132SSergey Zigachev }
171*78973132SSergey Zigachev
172*78973132SSergey Zigachev min_size = (height - 1) * mode_cmd->pitches[i]
173*78973132SSergey Zigachev + width * info->cpp[i]
174*78973132SSergey Zigachev + mode_cmd->offsets[i];
175*78973132SSergey Zigachev
176*78973132SSergey Zigachev if (objs[i]->size < min_size) {
177*78973132SSergey Zigachev drm_gem_object_put_unlocked(objs[i]);
178*78973132SSergey Zigachev ret = -EINVAL;
179*78973132SSergey Zigachev goto err_gem_object_put;
180*78973132SSergey Zigachev }
181*78973132SSergey Zigachev }
182*78973132SSergey Zigachev
183*78973132SSergey Zigachev fb = drm_gem_fb_alloc(dev, mode_cmd, objs, i, funcs);
184*78973132SSergey Zigachev if (IS_ERR(fb)) {
185*78973132SSergey Zigachev ret = PTR_ERR(fb);
186*78973132SSergey Zigachev goto err_gem_object_put;
187*78973132SSergey Zigachev }
188*78973132SSergey Zigachev
189*78973132SSergey Zigachev return fb;
190*78973132SSergey Zigachev
191*78973132SSergey Zigachev err_gem_object_put:
192*78973132SSergey Zigachev for (i--; i >= 0; i--)
193*78973132SSergey Zigachev drm_gem_object_put_unlocked(objs[i]);
194*78973132SSergey Zigachev
195*78973132SSergey Zigachev return ERR_PTR(ret);
196*78973132SSergey Zigachev }
197*78973132SSergey Zigachev EXPORT_SYMBOL_GPL(drm_gem_fb_create_with_funcs);
198*78973132SSergey Zigachev
199*78973132SSergey Zigachev static const struct drm_framebuffer_funcs drm_gem_fb_funcs = {
200*78973132SSergey Zigachev .destroy = drm_gem_fb_destroy,
201*78973132SSergey Zigachev .create_handle = drm_gem_fb_create_handle,
202*78973132SSergey Zigachev };
203*78973132SSergey Zigachev
204*78973132SSergey Zigachev /**
205*78973132SSergey Zigachev * drm_gem_fb_create() - Helper function for the
206*78973132SSergey Zigachev * &drm_mode_config_funcs.fb_create callback
207*78973132SSergey Zigachev * @dev: DRM device
208*78973132SSergey Zigachev * @file: DRM file that holds the GEM handle(s) backing the framebuffer
209*78973132SSergey Zigachev * @mode_cmd: Metadata from the userspace framebuffer creation request
210*78973132SSergey Zigachev *
211*78973132SSergey Zigachev * This function creates a new framebuffer object described by
212*78973132SSergey Zigachev * &drm_mode_fb_cmd2. This description includes handles for the buffer(s)
213*78973132SSergey Zigachev * backing the framebuffer.
214*78973132SSergey Zigachev *
215*78973132SSergey Zigachev * If your hardware has special alignment or pitch requirements these should be
216*78973132SSergey Zigachev * checked before calling this function. The function does buffer size
217*78973132SSergey Zigachev * validation. Use drm_gem_fb_create_with_funcs() if you need to set
218*78973132SSergey Zigachev * &drm_framebuffer_funcs.dirty.
219*78973132SSergey Zigachev *
220*78973132SSergey Zigachev * Drivers can use this as their &drm_mode_config_funcs.fb_create callback.
221*78973132SSergey Zigachev * The ADDFB2 IOCTL calls into this callback.
222*78973132SSergey Zigachev *
223*78973132SSergey Zigachev * Returns:
224*78973132SSergey Zigachev * Pointer to a &drm_framebuffer on success or an error pointer on failure.
225*78973132SSergey Zigachev */
226*78973132SSergey Zigachev struct drm_framebuffer *
drm_gem_fb_create(struct drm_device * dev,struct drm_file * file,const struct drm_mode_fb_cmd2 * mode_cmd)227*78973132SSergey Zigachev drm_gem_fb_create(struct drm_device *dev, struct drm_file *file,
228*78973132SSergey Zigachev const struct drm_mode_fb_cmd2 *mode_cmd)
229*78973132SSergey Zigachev {
230*78973132SSergey Zigachev return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
231*78973132SSergey Zigachev &drm_gem_fb_funcs);
232*78973132SSergey Zigachev }
233*78973132SSergey Zigachev EXPORT_SYMBOL_GPL(drm_gem_fb_create);
234*78973132SSergey Zigachev
235*78973132SSergey Zigachev /**
236*78973132SSergey Zigachev * drm_gem_fb_prepare_fb() - Prepare a GEM backed framebuffer
237*78973132SSergey Zigachev * @plane: Plane
238*78973132SSergey Zigachev * @state: Plane state the fence will be attached to
239*78973132SSergey Zigachev *
240*78973132SSergey Zigachev * This function prepares a GEM backed framebuffer for scanout by checking if
241*78973132SSergey Zigachev * the plane framebuffer has a DMA-BUF attached. If it does, it extracts the
242*78973132SSergey Zigachev * exclusive fence and attaches it to the plane state for the atomic helper to
243*78973132SSergey Zigachev * wait on. This function can be used as the &drm_plane_helper_funcs.prepare_fb
244*78973132SSergey Zigachev * callback.
245*78973132SSergey Zigachev *
246*78973132SSergey Zigachev * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
247*78973132SSergey Zigachev * gem based framebuffer drivers which have their buffers always pinned in
248*78973132SSergey Zigachev * memory.
249*78973132SSergey Zigachev */
drm_gem_fb_prepare_fb(struct drm_plane * plane,struct drm_plane_state * state)250*78973132SSergey Zigachev int drm_gem_fb_prepare_fb(struct drm_plane *plane,
251*78973132SSergey Zigachev struct drm_plane_state *state)
252*78973132SSergey Zigachev {
253*78973132SSergey Zigachev struct dma_buf *dma_buf;
254*78973132SSergey Zigachev struct dma_fence *fence;
255*78973132SSergey Zigachev
256*78973132SSergey Zigachev if (!state->fb)
257*78973132SSergey Zigachev return 0;
258*78973132SSergey Zigachev
259*78973132SSergey Zigachev dma_buf = drm_gem_fb_get_obj(state->fb, 0)->dma_buf;
260*78973132SSergey Zigachev if (dma_buf) {
261*78973132SSergey Zigachev fence = reservation_object_get_excl_rcu(dma_buf->resv);
262*78973132SSergey Zigachev drm_atomic_set_fence_for_plane(state, fence);
263*78973132SSergey Zigachev }
264*78973132SSergey Zigachev
265*78973132SSergey Zigachev return 0;
266*78973132SSergey Zigachev }
267*78973132SSergey Zigachev EXPORT_SYMBOL_GPL(drm_gem_fb_prepare_fb);
268*78973132SSergey Zigachev
269*78973132SSergey Zigachev /**
270*78973132SSergey Zigachev * drm_gem_fb_simple_display_pipe_prepare_fb - prepare_fb helper for
271*78973132SSergey Zigachev * &drm_simple_display_pipe
272*78973132SSergey Zigachev * @pipe: Simple display pipe
273*78973132SSergey Zigachev * @plane_state: Plane state
274*78973132SSergey Zigachev *
275*78973132SSergey Zigachev * This function uses drm_gem_fb_prepare_fb() to check if the plane FB has a
276*78973132SSergey Zigachev * &dma_buf attached, extracts the exclusive fence and attaches it to plane
277*78973132SSergey Zigachev * state for the atomic helper to wait on. Drivers can use this as their
278*78973132SSergey Zigachev * &drm_simple_display_pipe_funcs.prepare_fb callback.
279*78973132SSergey Zigachev */
drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe * pipe,struct drm_plane_state * plane_state)280*78973132SSergey Zigachev int drm_gem_fb_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
281*78973132SSergey Zigachev struct drm_plane_state *plane_state)
282*78973132SSergey Zigachev {
283*78973132SSergey Zigachev return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
284*78973132SSergey Zigachev }
285*78973132SSergey Zigachev EXPORT_SYMBOL(drm_gem_fb_simple_display_pipe_prepare_fb);
286*78973132SSergey Zigachev
287*78973132SSergey Zigachev /**
288*78973132SSergey Zigachev * drm_gem_fbdev_fb_create - Create a GEM backed &drm_framebuffer for fbdev
289*78973132SSergey Zigachev * emulation
290*78973132SSergey Zigachev * @dev: DRM device
291*78973132SSergey Zigachev * @sizes: fbdev size description
292*78973132SSergey Zigachev * @pitch_align: Optional pitch alignment
293*78973132SSergey Zigachev * @obj: GEM object backing the framebuffer
294*78973132SSergey Zigachev * @funcs: Optional vtable to be used for the new framebuffer object when the
295*78973132SSergey Zigachev * dirty callback is needed.
296*78973132SSergey Zigachev *
297*78973132SSergey Zigachev * This function creates a framebuffer from a &drm_fb_helper_surface_size
298*78973132SSergey Zigachev * description for use in the &drm_fb_helper_funcs.fb_probe callback.
299*78973132SSergey Zigachev *
300*78973132SSergey Zigachev * Returns:
301*78973132SSergey Zigachev * Pointer to a &drm_framebuffer on success or an error pointer on failure.
302*78973132SSergey Zigachev */
303*78973132SSergey Zigachev struct drm_framebuffer *
drm_gem_fbdev_fb_create(struct drm_device * dev,struct drm_fb_helper_surface_size * sizes,unsigned int pitch_align,struct drm_gem_object * obj,const struct drm_framebuffer_funcs * funcs)304*78973132SSergey Zigachev drm_gem_fbdev_fb_create(struct drm_device *dev,
305*78973132SSergey Zigachev struct drm_fb_helper_surface_size *sizes,
306*78973132SSergey Zigachev unsigned int pitch_align, struct drm_gem_object *obj,
307*78973132SSergey Zigachev const struct drm_framebuffer_funcs *funcs)
308*78973132SSergey Zigachev {
309*78973132SSergey Zigachev struct drm_mode_fb_cmd2 mode_cmd = { 0 };
310*78973132SSergey Zigachev
311*78973132SSergey Zigachev mode_cmd.width = sizes->surface_width;
312*78973132SSergey Zigachev mode_cmd.height = sizes->surface_height;
313*78973132SSergey Zigachev mode_cmd.pitches[0] = sizes->surface_width *
314*78973132SSergey Zigachev DIV_ROUND_UP(sizes->surface_bpp, 8);
315*78973132SSergey Zigachev if (pitch_align)
316*78973132SSergey Zigachev mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0],
317*78973132SSergey Zigachev pitch_align);
318*78973132SSergey Zigachev mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
319*78973132SSergey Zigachev sizes->surface_depth);
320*78973132SSergey Zigachev if (obj->size < mode_cmd.pitches[0] * mode_cmd.height)
321*78973132SSergey Zigachev return ERR_PTR(-EINVAL);
322*78973132SSergey Zigachev
323*78973132SSergey Zigachev if (!funcs)
324*78973132SSergey Zigachev funcs = &drm_gem_fb_funcs;
325*78973132SSergey Zigachev
326*78973132SSergey Zigachev return drm_gem_fb_alloc(dev, &mode_cmd, &obj, 1, funcs);
327*78973132SSergey Zigachev }
328*78973132SSergey Zigachev EXPORT_SYMBOL(drm_gem_fbdev_fb_create);
329