blob: bb88e3df925712e52af66869e4a23563221dc36c [file] [log] [blame]
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +02001/*
2 * drm kms/fb cma (contiguous memory allocator) helper functions
3 *
4 * Copyright (C) 2012 Analog Device Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 *
7 * Based on udl_fbdev.c
8 * Copyright (C) 2012 Red Hat
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <drm/drmP.h>
21#include <drm/drm_crtc.h>
22#include <drm/drm_fb_helper.h>
23#include <drm/drm_crtc_helper.h>
24#include <drm/drm_gem_cma_helper.h>
25#include <drm/drm_fb_cma_helper.h>
26#include <linux/module.h>
27
28struct drm_fb_cma {
29 struct drm_framebuffer fb;
30 struct drm_gem_cma_object *obj[4];
31};
32
33struct drm_fbdev_cma {
34 struct drm_fb_helper fb_helper;
35 struct drm_fb_cma *fb;
36};
37
38static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
39{
40 return container_of(helper, struct drm_fbdev_cma, fb_helper);
41}
42
43static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb)
44{
45 return container_of(fb, struct drm_fb_cma, fb);
46}
47
48static void drm_fb_cma_destroy(struct drm_framebuffer *fb)
49{
50 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
51 int i;
52
53 for (i = 0; i < 4; i++) {
54 if (fb_cma->obj[i])
55 drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base);
56 }
57
58 drm_framebuffer_cleanup(fb);
59 kfree(fb_cma);
60}
61
62static int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
63 struct drm_file *file_priv, unsigned int *handle)
64{
65 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
66
67 return drm_gem_handle_create(file_priv,
68 &fb_cma->obj[0]->base, handle);
69}
70
71static struct drm_framebuffer_funcs drm_fb_cma_funcs = {
72 .destroy = drm_fb_cma_destroy,
73 .create_handle = drm_fb_cma_create_handle,
74};
75
76static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev,
Colin Ian King70c06162016-01-20 10:59:34 +000077 const struct drm_mode_fb_cmd2 *mode_cmd,
78 struct drm_gem_cma_object **obj,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020079 unsigned int num_planes)
80{
81 struct drm_fb_cma *fb_cma;
82 int ret;
83 int i;
84
85 fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL);
86 if (!fb_cma)
87 return ERR_PTR(-ENOMEM);
88
Daniel Vetterc7d73f62012-12-13 23:38:38 +010089 drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd);
90
91 for (i = 0; i < num_planes; i++)
92 fb_cma->obj[i] = obj[i];
93
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020094 ret = drm_framebuffer_init(dev, &fb_cma->fb, &drm_fb_cma_funcs);
95 if (ret) {
Masanari Iida8b513d02013-05-21 23:13:12 +090096 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020097 kfree(fb_cma);
98 return ERR_PTR(ret);
99 }
100
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200101 return fb_cma;
102}
103
104/**
105 * drm_fb_cma_create() - (struct drm_mode_config_funcs *)->fb_create callback function
106 *
107 * If your hardware has special alignment or pitch requirements these should be
108 * checked before calling this function.
109 */
110struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200111 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200112{
113 struct drm_fb_cma *fb_cma;
114 struct drm_gem_cma_object *objs[4];
115 struct drm_gem_object *obj;
116 unsigned int hsub;
117 unsigned int vsub;
118 int ret;
119 int i;
120
121 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
122 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
123
124 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
125 unsigned int width = mode_cmd->width / (i ? hsub : 1);
126 unsigned int height = mode_cmd->height / (i ? vsub : 1);
127 unsigned int min_size;
128
129 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[i]);
130 if (!obj) {
131 dev_err(dev->dev, "Failed to lookup GEM object\n");
132 ret = -ENXIO;
133 goto err_gem_object_unreference;
134 }
135
136 min_size = (height - 1) * mode_cmd->pitches[i]
137 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
138 + mode_cmd->offsets[i];
139
140 if (obj->size < min_size) {
141 drm_gem_object_unreference_unlocked(obj);
142 ret = -EINVAL;
143 goto err_gem_object_unreference;
144 }
145 objs[i] = to_drm_gem_cma_obj(obj);
146 }
147
148 fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i);
149 if (IS_ERR(fb_cma)) {
150 ret = PTR_ERR(fb_cma);
151 goto err_gem_object_unreference;
152 }
153
154 return &fb_cma->fb;
155
156err_gem_object_unreference:
157 for (i--; i >= 0; i--)
158 drm_gem_object_unreference_unlocked(&objs[i]->base);
159 return ERR_PTR(ret);
160}
161EXPORT_SYMBOL_GPL(drm_fb_cma_create);
162
163/**
164 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
165 * @fb: The framebuffer
166 * @plane: Which plane
167 *
168 * Return the CMA GEM object for given framebuffer.
169 *
170 * This function will usually be called from the CRTC callback functions.
171 */
172struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
173 unsigned int plane)
174{
175 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
176
177 if (plane >= 4)
178 return NULL;
179
180 return fb_cma->obj[plane];
181}
182EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
183
Rob Clark6f646092012-12-10 10:46:43 -0600184#ifdef CONFIG_DEBUG_FS
Lespiau, Damien2c9c52e2013-08-20 00:53:08 +0100185/*
Rob Clark6f646092012-12-10 10:46:43 -0600186 * drm_fb_cma_describe() - Helper to dump information about a single
187 * CMA framebuffer object
188 */
Lespiau, Damien2c9c52e2013-08-20 00:53:08 +0100189static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
Rob Clark6f646092012-12-10 10:46:43 -0600190{
191 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
192 int i, n = drm_format_num_planes(fb->pixel_format);
193
194 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
195 (char *)&fb->pixel_format);
196
197 for (i = 0; i < n; i++) {
198 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
199 i, fb->offsets[i], fb->pitches[i]);
200 drm_gem_cma_describe(fb_cma->obj[i], m);
201 }
202}
Rob Clark6f646092012-12-10 10:46:43 -0600203
204/**
205 * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
206 * in debugfs.
207 */
208int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
209{
210 struct drm_info_node *node = (struct drm_info_node *) m->private;
211 struct drm_device *dev = node->minor->dev;
212 struct drm_framebuffer *fb;
Rob Clark6f646092012-12-10 10:46:43 -0600213
Daniel Vetter9e75c0e2015-07-09 23:32:34 +0200214 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vettere4f62542015-07-09 23:44:35 +0200215 drm_for_each_fb(fb, dev)
Rob Clark6f646092012-12-10 10:46:43 -0600216 drm_fb_cma_describe(fb, m);
Daniel Vetter9e75c0e2015-07-09 23:32:34 +0200217 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clark6f646092012-12-10 10:46:43 -0600218
219 return 0;
220}
221EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
222#endif
223
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200224static struct fb_ops drm_fbdev_cma_ops = {
225 .owner = THIS_MODULE,
Archit Taneja85f2edf2015-07-22 14:58:20 +0530226 .fb_fillrect = drm_fb_helper_sys_fillrect,
227 .fb_copyarea = drm_fb_helper_sys_copyarea,
228 .fb_imageblit = drm_fb_helper_sys_imageblit,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200229 .fb_check_var = drm_fb_helper_check_var,
230 .fb_set_par = drm_fb_helper_set_par,
231 .fb_blank = drm_fb_helper_blank,
232 .fb_pan_display = drm_fb_helper_pan_display,
233 .fb_setcmap = drm_fb_helper_setcmap,
234};
235
236static int drm_fbdev_cma_create(struct drm_fb_helper *helper,
237 struct drm_fb_helper_surface_size *sizes)
238{
239 struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
240 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
241 struct drm_device *dev = helper->dev;
242 struct drm_gem_cma_object *obj;
243 struct drm_framebuffer *fb;
244 unsigned int bytes_per_pixel;
245 unsigned long offset;
246 struct fb_info *fbi;
247 size_t size;
248 int ret;
249
Thierry Redinge0d78d02012-10-20 10:32:46 +0000250 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200251 sizes->surface_width, sizes->surface_height,
252 sizes->surface_bpp);
253
254 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
255
256 mode_cmd.width = sizes->surface_width;
257 mode_cmd.height = sizes->surface_height;
258 mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
259 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
260 sizes->surface_depth);
261
262 size = mode_cmd.pitches[0] * mode_cmd.height;
263 obj = drm_gem_cma_create(dev, size);
Thierry Reding02813242012-10-20 10:32:47 +0000264 if (IS_ERR(obj))
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200265 return -ENOMEM;
266
Archit Taneja85f2edf2015-07-22 14:58:20 +0530267 fbi = drm_fb_helper_alloc_fbi(helper);
268 if (IS_ERR(fbi)) {
269 ret = PTR_ERR(fbi);
Eric Anholt50cbc132015-12-14 16:26:26 -0800270 goto err_gem_free_object;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200271 }
272
273 fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1);
274 if (IS_ERR(fbdev_cma->fb)) {
275 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
276 ret = PTR_ERR(fbdev_cma->fb);
Archit Taneja85f2edf2015-07-22 14:58:20 +0530277 goto err_fb_info_destroy;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200278 }
279
280 fb = &fbdev_cma->fb->fb;
281 helper->fb = fb;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200282
283 fbi->par = helper;
284 fbi->flags = FBINFO_FLAG_DEFAULT;
285 fbi->fbops = &drm_fbdev_cma_ops;
286
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200287 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
Rob Clark8d766122015-03-11 10:23:10 -0400288 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200289
290 offset = fbi->var.xoffset * bytes_per_pixel;
291 offset += fbi->var.yoffset * fb->pitches[0];
292
293 dev->mode_config.fb_base = (resource_size_t)obj->paddr;
294 fbi->screen_base = obj->vaddr + offset;
295 fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
296 fbi->screen_size = size;
297 fbi->fix.smem_len = size;
298
299 return 0;
300
Archit Taneja85f2edf2015-07-22 14:58:20 +0530301err_fb_info_destroy:
302 drm_fb_helper_release_fbi(helper);
Eric Anholt50cbc132015-12-14 16:26:26 -0800303err_gem_free_object:
304 dev->driver->gem_free_object(&obj->base);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200305 return ret;
306}
307
Thierry Reding3a493872014-06-27 17:19:23 +0200308static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
Daniel Vettercd5428a2013-01-21 23:42:49 +0100309 .fb_probe = drm_fbdev_cma_create,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200310};
311
312/**
313 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
314 * @dev: DRM device
315 * @preferred_bpp: Preferred bits per pixel for the device
316 * @num_crtc: Number of CRTCs
317 * @max_conn_count: Maximum number of connectors
318 *
319 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
320 */
321struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
322 unsigned int preferred_bpp, unsigned int num_crtc,
323 unsigned int max_conn_count)
324{
325 struct drm_fbdev_cma *fbdev_cma;
326 struct drm_fb_helper *helper;
327 int ret;
328
329 fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
330 if (!fbdev_cma) {
331 dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
332 return ERR_PTR(-ENOMEM);
333 }
334
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200335 helper = &fbdev_cma->fb_helper;
336
Thierry Reding10a23102014-06-27 17:19:24 +0200337 drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
338
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200339 ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
340 if (ret < 0) {
341 dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
342 goto err_free;
343 }
344
345 ret = drm_fb_helper_single_add_all_connectors(helper);
346 if (ret < 0) {
347 dev_err(dev->dev, "Failed to add connectors.\n");
348 goto err_drm_fb_helper_fini;
349
350 }
351
352 ret = drm_fb_helper_initial_config(helper, preferred_bpp);
353 if (ret < 0) {
Masanari Iida8b513d02013-05-21 23:13:12 +0900354 dev_err(dev->dev, "Failed to set initial hw configuration.\n");
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200355 goto err_drm_fb_helper_fini;
356 }
357
358 return fbdev_cma;
359
360err_drm_fb_helper_fini:
361 drm_fb_helper_fini(helper);
362err_free:
363 kfree(fbdev_cma);
364
365 return ERR_PTR(ret);
366}
367EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
368
369/**
370 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
371 * @fbdev_cma: The drm_fbdev_cma struct
372 */
373void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
374{
Archit Taneja85f2edf2015-07-22 14:58:20 +0530375 drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
376 drm_fb_helper_release_fbi(&fbdev_cma->fb_helper);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200377
Daniel Vetter36206362012-12-10 20:42:17 +0100378 if (fbdev_cma->fb) {
379 drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200380 drm_fb_cma_destroy(&fbdev_cma->fb->fb);
Daniel Vetter36206362012-12-10 20:42:17 +0100381 }
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200382
383 drm_fb_helper_fini(&fbdev_cma->fb_helper);
384 kfree(fbdev_cma);
385}
386EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
387
388/**
389 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
390 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
391 *
392 * This function is usually called from the DRM drivers lastclose callback.
393 */
394void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
395{
Rob Clark5ea1f752014-05-30 12:29:48 -0400396 if (fbdev_cma)
397 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200398}
399EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
400
401/**
402 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
403 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
404 *
405 * This function is usually called from the DRM drivers output_poll_changed
406 * callback.
407 */
408void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
409{
410 if (fbdev_cma)
411 drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
412}
413EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);