blob: 1fd6eac1400c1c92bae8edaac6a622a322d7ace0 [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>
Robin Murphyce0c5752016-06-07 13:18:09 +010026#include <linux/dma-mapping.h>
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020027#include <linux/module.h>
28
Noralf Trønnes199c7712016-04-28 17:18:35 +020029#define DEFAULT_FBDEFIO_DELAY_MS 50
30
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020031struct drm_fb_cma {
32 struct drm_framebuffer fb;
33 struct drm_gem_cma_object *obj[4];
34};
35
36struct drm_fbdev_cma {
37 struct drm_fb_helper fb_helper;
38 struct drm_fb_cma *fb;
39};
40
Noralf Trønnes199c7712016-04-28 17:18:35 +020041/**
42 * DOC: framebuffer cma helper functions
43 *
44 * Provides helper functions for creating a cma (contiguous memory allocator)
45 * backed framebuffer.
46 *
Noralf Trønnes02da16d2016-05-11 18:09:18 +020047 * drm_fb_cma_create() is used in the &drm_mode_config_funcs ->fb_create
48 * callback function to create a cma backed framebuffer.
Noralf Trønnes199c7712016-04-28 17:18:35 +020049 *
50 * An fbdev framebuffer backed by cma is also available by calling
51 * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
Noralf Trønnes02da16d2016-05-11 18:09:18 +020052 * If the &drm_framebuffer_funcs ->dirty callback is set, fb_deferred_io
Noralf Trønnes199c7712016-04-28 17:18:35 +020053 * will be set up automatically. dirty() is called by
54 * drm_fb_helper_deferred_io() in process context (struct delayed_work).
55 *
Daniel Vetterda5335b2016-05-31 22:55:13 +020056 * Example fbdev deferred io code::
Noralf Trønnes199c7712016-04-28 17:18:35 +020057 *
58 * static int driver_fbdev_fb_dirty(struct drm_framebuffer *fb,
59 * struct drm_file *file_priv,
60 * unsigned flags, unsigned color,
61 * struct drm_clip_rect *clips,
62 * unsigned num_clips)
63 * {
64 * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
65 * ... push changes ...
66 * return 0;
67 * }
68 *
69 * static struct drm_framebuffer_funcs driver_fbdev_fb_funcs = {
70 * .destroy = drm_fb_cma_destroy,
71 * .create_handle = drm_fb_cma_create_handle,
72 * .dirty = driver_fbdev_fb_dirty,
73 * };
74 *
75 * static int driver_fbdev_create(struct drm_fb_helper *helper,
76 * struct drm_fb_helper_surface_size *sizes)
77 * {
78 * return drm_fbdev_cma_create_with_funcs(helper, sizes,
79 * &driver_fbdev_fb_funcs);
80 * }
81 *
82 * static const struct drm_fb_helper_funcs driver_fb_helper_funcs = {
83 * .fb_probe = driver_fbdev_create,
84 * };
85 *
86 * Initialize:
87 * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
88 * dev->mode_config.num_crtc,
89 * dev->mode_config.num_connector,
90 * &driver_fb_helper_funcs);
91 *
92 */
93
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020094static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
95{
96 return container_of(helper, struct drm_fbdev_cma, fb_helper);
97}
98
99static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb)
100{
101 return container_of(fb, struct drm_fb_cma, fb);
102}
103
Noralf Trønnes199c7712016-04-28 17:18:35 +0200104void drm_fb_cma_destroy(struct drm_framebuffer *fb)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200105{
106 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
107 int i;
108
109 for (i = 0; i < 4; i++) {
110 if (fb_cma->obj[i])
111 drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base);
112 }
113
114 drm_framebuffer_cleanup(fb);
115 kfree(fb_cma);
116}
Noralf Trønnes199c7712016-04-28 17:18:35 +0200117EXPORT_SYMBOL(drm_fb_cma_destroy);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200118
Noralf Trønnes199c7712016-04-28 17:18:35 +0200119int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200120 struct drm_file *file_priv, unsigned int *handle)
121{
122 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
123
124 return drm_gem_handle_create(file_priv,
125 &fb_cma->obj[0]->base, handle);
126}
Noralf Trønnes199c7712016-04-28 17:18:35 +0200127EXPORT_SYMBOL(drm_fb_cma_create_handle);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200128
129static struct drm_framebuffer_funcs drm_fb_cma_funcs = {
130 .destroy = drm_fb_cma_destroy,
131 .create_handle = drm_fb_cma_create_handle,
132};
133
134static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev,
Colin Ian King70c06162016-01-20 10:59:34 +0000135 const struct drm_mode_fb_cmd2 *mode_cmd,
136 struct drm_gem_cma_object **obj,
Noralf Trønnesfdce1842016-05-12 20:25:21 +0200137 unsigned int num_planes, const struct drm_framebuffer_funcs *funcs)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200138{
139 struct drm_fb_cma *fb_cma;
140 int ret;
141 int i;
142
143 fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL);
144 if (!fb_cma)
145 return ERR_PTR(-ENOMEM);
146
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100147 drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd);
148
149 for (i = 0; i < num_planes; i++)
150 fb_cma->obj[i] = obj[i];
151
Noralf Trønnes199c7712016-04-28 17:18:35 +0200152 ret = drm_framebuffer_init(dev, &fb_cma->fb, funcs);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200153 if (ret) {
Masanari Iida8b513d02013-05-21 23:13:12 +0900154 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200155 kfree(fb_cma);
156 return ERR_PTR(ret);
157 }
158
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200159 return fb_cma;
160}
161
162/**
Noralf Trønnes3995b392016-05-12 20:25:22 +0200163 * drm_fb_cma_create_with_funcs() - helper function for the
164 * &drm_mode_config_funcs ->fb_create
165 * callback function
Daniel Vetter890358a2016-05-31 23:11:12 +0200166 * @dev: DRM device
167 * @file_priv: drm file for the ioctl call
168 * @mode_cmd: metadata from the userspace fb creation request
169 * @funcs: vtable to be used for the new framebuffer object
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200170 *
Noralf Trønnes3995b392016-05-12 20:25:22 +0200171 * This can be used to set &drm_framebuffer_funcs for drivers that need the
172 * dirty() callback. Use drm_fb_cma_create() if you don't need to change
173 * &drm_framebuffer_funcs.
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200174 */
Noralf Trønnes3995b392016-05-12 20:25:22 +0200175struct drm_framebuffer *drm_fb_cma_create_with_funcs(struct drm_device *dev,
176 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd,
177 const struct drm_framebuffer_funcs *funcs)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200178{
179 struct drm_fb_cma *fb_cma;
180 struct drm_gem_cma_object *objs[4];
181 struct drm_gem_object *obj;
182 unsigned int hsub;
183 unsigned int vsub;
184 int ret;
185 int i;
186
187 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
188 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
189
190 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
191 unsigned int width = mode_cmd->width / (i ? hsub : 1);
192 unsigned int height = mode_cmd->height / (i ? vsub : 1);
193 unsigned int min_size;
194
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100195 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200196 if (!obj) {
197 dev_err(dev->dev, "Failed to lookup GEM object\n");
198 ret = -ENXIO;
199 goto err_gem_object_unreference;
200 }
201
202 min_size = (height - 1) * mode_cmd->pitches[i]
203 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
204 + mode_cmd->offsets[i];
205
206 if (obj->size < min_size) {
207 drm_gem_object_unreference_unlocked(obj);
208 ret = -EINVAL;
209 goto err_gem_object_unreference;
210 }
211 objs[i] = to_drm_gem_cma_obj(obj);
212 }
213
Noralf Trønnes3995b392016-05-12 20:25:22 +0200214 fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i, funcs);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200215 if (IS_ERR(fb_cma)) {
216 ret = PTR_ERR(fb_cma);
217 goto err_gem_object_unreference;
218 }
219
220 return &fb_cma->fb;
221
222err_gem_object_unreference:
223 for (i--; i >= 0; i--)
224 drm_gem_object_unreference_unlocked(&objs[i]->base);
225 return ERR_PTR(ret);
226}
Noralf Trønnes3995b392016-05-12 20:25:22 +0200227EXPORT_SYMBOL_GPL(drm_fb_cma_create_with_funcs);
228
229/**
230 * drm_fb_cma_create() - &drm_mode_config_funcs ->fb_create callback function
Daniel Vetter890358a2016-05-31 23:11:12 +0200231 * @dev: DRM device
232 * @file_priv: drm file for the ioctl call
233 * @mode_cmd: metadata from the userspace fb creation request
Noralf Trønnes3995b392016-05-12 20:25:22 +0200234 *
235 * If your hardware has special alignment or pitch requirements these should be
236 * checked before calling this function. Use drm_fb_cma_create_with_funcs() if
237 * you need to set &drm_framebuffer_funcs ->dirty.
238 */
239struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
240 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
241{
242 return drm_fb_cma_create_with_funcs(dev, file_priv, mode_cmd,
243 &drm_fb_cma_funcs);
244}
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200245EXPORT_SYMBOL_GPL(drm_fb_cma_create);
246
247/**
248 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
249 * @fb: The framebuffer
250 * @plane: Which plane
251 *
252 * Return the CMA GEM object for given framebuffer.
253 *
254 * This function will usually be called from the CRTC callback functions.
255 */
256struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
Daniel Vetter890358a2016-05-31 23:11:12 +0200257 unsigned int plane)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200258{
259 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
260
261 if (plane >= 4)
262 return NULL;
263
264 return fb_cma->obj[plane];
265}
266EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
267
Rob Clark6f646092012-12-10 10:46:43 -0600268#ifdef CONFIG_DEBUG_FS
Lespiau, Damien2c9c52e2013-08-20 00:53:08 +0100269static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
Rob Clark6f646092012-12-10 10:46:43 -0600270{
271 struct drm_fb_cma *fb_cma = to_fb_cma(fb);
272 int i, n = drm_format_num_planes(fb->pixel_format);
273
274 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
275 (char *)&fb->pixel_format);
276
277 for (i = 0; i < n; i++) {
278 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
279 i, fb->offsets[i], fb->pitches[i]);
280 drm_gem_cma_describe(fb_cma->obj[i], m);
281 }
282}
Rob Clark6f646092012-12-10 10:46:43 -0600283
284/**
285 * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
Daniel Vetter890358a2016-05-31 23:11:12 +0200286 * in debugfs.
287 * @m: output file
288 * @arg: private data for the callback
Rob Clark6f646092012-12-10 10:46:43 -0600289 */
290int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
291{
292 struct drm_info_node *node = (struct drm_info_node *) m->private;
293 struct drm_device *dev = node->minor->dev;
294 struct drm_framebuffer *fb;
Rob Clark6f646092012-12-10 10:46:43 -0600295
Daniel Vetter9e75c0e2015-07-09 23:32:34 +0200296 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vettere4f62542015-07-09 23:44:35 +0200297 drm_for_each_fb(fb, dev)
Rob Clark6f646092012-12-10 10:46:43 -0600298 drm_fb_cma_describe(fb, m);
Daniel Vetter9e75c0e2015-07-09 23:32:34 +0200299 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clark6f646092012-12-10 10:46:43 -0600300
301 return 0;
302}
303EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
304#endif
305
Robin Murphyce0c5752016-06-07 13:18:09 +0100306static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
307{
308 return dma_mmap_writecombine(info->device, vma, info->screen_base,
309 info->fix.smem_start, info->fix.smem_len);
310}
311
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200312static struct fb_ops drm_fbdev_cma_ops = {
313 .owner = THIS_MODULE,
Archit Taneja85f2edf2015-07-22 14:58:20 +0530314 .fb_fillrect = drm_fb_helper_sys_fillrect,
315 .fb_copyarea = drm_fb_helper_sys_copyarea,
316 .fb_imageblit = drm_fb_helper_sys_imageblit,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200317 .fb_check_var = drm_fb_helper_check_var,
318 .fb_set_par = drm_fb_helper_set_par,
319 .fb_blank = drm_fb_helper_blank,
320 .fb_pan_display = drm_fb_helper_pan_display,
321 .fb_setcmap = drm_fb_helper_setcmap,
Robin Murphyce0c5752016-06-07 13:18:09 +0100322 .fb_mmap = drm_fb_cma_mmap,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200323};
324
Noralf Trønnes199c7712016-04-28 17:18:35 +0200325static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
326 struct vm_area_struct *vma)
327{
328 fb_deferred_io_mmap(info, vma);
329 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
330
331 return 0;
332}
333
334static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
335 struct drm_gem_cma_object *cma_obj)
336{
337 struct fb_deferred_io *fbdefio;
338 struct fb_ops *fbops;
339
340 /*
341 * Per device structures are needed because:
342 * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
343 * fbdefio: individual delays
344 */
345 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
346 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
347 if (!fbdefio || !fbops) {
348 kfree(fbdefio);
Sudip Mukherjee2a927622016-06-12 16:03:56 +0100349 kfree(fbops);
Noralf Trønnes199c7712016-04-28 17:18:35 +0200350 return -ENOMEM;
351 }
352
353 /* can't be offset from vaddr since dirty() uses cma_obj */
354 fbi->screen_buffer = cma_obj->vaddr;
355 /* fb_deferred_io_fault() needs a physical address */
356 fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
357
358 *fbops = *fbi->fbops;
359 fbi->fbops = fbops;
360
361 fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
362 fbdefio->deferred_io = drm_fb_helper_deferred_io;
363 fbi->fbdefio = fbdefio;
364 fb_deferred_io_init(fbi);
365 fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
366
367 return 0;
368}
369
370static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
371{
372 if (!fbi->fbdefio)
373 return;
374
375 fb_deferred_io_cleanup(fbi);
376 kfree(fbi->fbdefio);
377 kfree(fbi->fbops);
378}
379
380/*
381 * For use in a (struct drm_fb_helper_funcs *)->fb_probe callback function that
382 * needs custom struct drm_framebuffer_funcs, like dirty() for deferred_io use.
383 */
384int drm_fbdev_cma_create_with_funcs(struct drm_fb_helper *helper,
385 struct drm_fb_helper_surface_size *sizes,
Noralf Trønnesfdce1842016-05-12 20:25:21 +0200386 const struct drm_framebuffer_funcs *funcs)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200387{
388 struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
389 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
390 struct drm_device *dev = helper->dev;
391 struct drm_gem_cma_object *obj;
392 struct drm_framebuffer *fb;
393 unsigned int bytes_per_pixel;
394 unsigned long offset;
395 struct fb_info *fbi;
396 size_t size;
397 int ret;
398
Thierry Redinge0d78d02012-10-20 10:32:46 +0000399 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200400 sizes->surface_width, sizes->surface_height,
401 sizes->surface_bpp);
402
403 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
404
405 mode_cmd.width = sizes->surface_width;
406 mode_cmd.height = sizes->surface_height;
407 mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
408 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
409 sizes->surface_depth);
410
411 size = mode_cmd.pitches[0] * mode_cmd.height;
412 obj = drm_gem_cma_create(dev, size);
Thierry Reding02813242012-10-20 10:32:47 +0000413 if (IS_ERR(obj))
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200414 return -ENOMEM;
415
Archit Taneja85f2edf2015-07-22 14:58:20 +0530416 fbi = drm_fb_helper_alloc_fbi(helper);
417 if (IS_ERR(fbi)) {
418 ret = PTR_ERR(fbi);
Eric Anholt50cbc132015-12-14 16:26:26 -0800419 goto err_gem_free_object;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200420 }
421
Noralf Trønnes199c7712016-04-28 17:18:35 +0200422 fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1, funcs);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200423 if (IS_ERR(fbdev_cma->fb)) {
424 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
425 ret = PTR_ERR(fbdev_cma->fb);
Archit Taneja85f2edf2015-07-22 14:58:20 +0530426 goto err_fb_info_destroy;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200427 }
428
429 fb = &fbdev_cma->fb->fb;
430 helper->fb = fb;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200431
432 fbi->par = helper;
433 fbi->flags = FBINFO_FLAG_DEFAULT;
434 fbi->fbops = &drm_fbdev_cma_ops;
435
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200436 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
Rob Clark8d766122015-03-11 10:23:10 -0400437 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200438
439 offset = fbi->var.xoffset * bytes_per_pixel;
440 offset += fbi->var.yoffset * fb->pitches[0];
441
442 dev->mode_config.fb_base = (resource_size_t)obj->paddr;
443 fbi->screen_base = obj->vaddr + offset;
444 fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
445 fbi->screen_size = size;
446 fbi->fix.smem_len = size;
447
Noralf Trønnes199c7712016-04-28 17:18:35 +0200448 if (funcs->dirty) {
449 ret = drm_fbdev_cma_defio_init(fbi, obj);
450 if (ret)
451 goto err_cma_destroy;
452 }
453
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200454 return 0;
455
Noralf Trønnes199c7712016-04-28 17:18:35 +0200456err_cma_destroy:
457 drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
458 drm_fb_cma_destroy(&fbdev_cma->fb->fb);
Archit Taneja85f2edf2015-07-22 14:58:20 +0530459err_fb_info_destroy:
460 drm_fb_helper_release_fbi(helper);
Eric Anholt50cbc132015-12-14 16:26:26 -0800461err_gem_free_object:
Chris Wilsonafe705b2016-05-31 22:25:52 +0100462 drm_gem_object_unreference_unlocked(&obj->base);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200463 return ret;
464}
Noralf Trønnes199c7712016-04-28 17:18:35 +0200465EXPORT_SYMBOL(drm_fbdev_cma_create_with_funcs);
466
467static int drm_fbdev_cma_create(struct drm_fb_helper *helper,
468 struct drm_fb_helper_surface_size *sizes)
469{
470 return drm_fbdev_cma_create_with_funcs(helper, sizes, &drm_fb_cma_funcs);
471}
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200472
Thierry Reding3a493872014-06-27 17:19:23 +0200473static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
Daniel Vettercd5428a2013-01-21 23:42:49 +0100474 .fb_probe = drm_fbdev_cma_create,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200475};
476
477/**
Noralf Trønnes199c7712016-04-28 17:18:35 +0200478 * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200479 * @dev: DRM device
480 * @preferred_bpp: Preferred bits per pixel for the device
481 * @num_crtc: Number of CRTCs
482 * @max_conn_count: Maximum number of connectors
Noralf Trønnes199c7712016-04-28 17:18:35 +0200483 * @funcs: fb helper functions, in particular fb_probe()
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200484 *
485 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
486 */
Noralf Trønnes199c7712016-04-28 17:18:35 +0200487struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200488 unsigned int preferred_bpp, unsigned int num_crtc,
Noralf Trønnes199c7712016-04-28 17:18:35 +0200489 unsigned int max_conn_count, const struct drm_fb_helper_funcs *funcs)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200490{
491 struct drm_fbdev_cma *fbdev_cma;
492 struct drm_fb_helper *helper;
493 int ret;
494
495 fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
496 if (!fbdev_cma) {
497 dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
498 return ERR_PTR(-ENOMEM);
499 }
500
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200501 helper = &fbdev_cma->fb_helper;
502
Noralf Trønnes199c7712016-04-28 17:18:35 +0200503 drm_fb_helper_prepare(dev, helper, funcs);
Thierry Reding10a23102014-06-27 17:19:24 +0200504
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200505 ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
506 if (ret < 0) {
507 dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
508 goto err_free;
509 }
510
511 ret = drm_fb_helper_single_add_all_connectors(helper);
512 if (ret < 0) {
513 dev_err(dev->dev, "Failed to add connectors.\n");
514 goto err_drm_fb_helper_fini;
515
516 }
517
518 ret = drm_fb_helper_initial_config(helper, preferred_bpp);
519 if (ret < 0) {
Masanari Iida8b513d02013-05-21 23:13:12 +0900520 dev_err(dev->dev, "Failed to set initial hw configuration.\n");
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200521 goto err_drm_fb_helper_fini;
522 }
523
524 return fbdev_cma;
525
526err_drm_fb_helper_fini:
527 drm_fb_helper_fini(helper);
528err_free:
529 kfree(fbdev_cma);
530
531 return ERR_PTR(ret);
532}
Noralf Trønnes199c7712016-04-28 17:18:35 +0200533EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
534
535/**
536 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
537 * @dev: DRM device
538 * @preferred_bpp: Preferred bits per pixel for the device
539 * @num_crtc: Number of CRTCs
540 * @max_conn_count: Maximum number of connectors
541 *
542 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
543 */
544struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
545 unsigned int preferred_bpp, unsigned int num_crtc,
546 unsigned int max_conn_count)
547{
548 return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp, num_crtc,
549 max_conn_count, &drm_fb_cma_helper_funcs);
550}
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200551EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
552
553/**
554 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
555 * @fbdev_cma: The drm_fbdev_cma struct
556 */
557void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
558{
Archit Taneja85f2edf2015-07-22 14:58:20 +0530559 drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
Noralf Trønnes199c7712016-04-28 17:18:35 +0200560 drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
Archit Taneja85f2edf2015-07-22 14:58:20 +0530561 drm_fb_helper_release_fbi(&fbdev_cma->fb_helper);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200562
Daniel Vetter36206362012-12-10 20:42:17 +0100563 if (fbdev_cma->fb) {
564 drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200565 drm_fb_cma_destroy(&fbdev_cma->fb->fb);
Daniel Vetter36206362012-12-10 20:42:17 +0100566 }
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200567
568 drm_fb_helper_fini(&fbdev_cma->fb_helper);
569 kfree(fbdev_cma);
570}
571EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
572
573/**
574 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
575 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
576 *
577 * This function is usually called from the DRM drivers lastclose callback.
578 */
579void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
580{
Rob Clark5ea1f752014-05-30 12:29:48 -0400581 if (fbdev_cma)
582 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200583}
584EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
585
586/**
587 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
588 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
589 *
590 * This function is usually called from the DRM drivers output_poll_changed
591 * callback.
592 */
593void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
594{
595 if (fbdev_cma)
596 drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
597}
598EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
Stefan Agner917f4252016-02-11 17:30:14 -0800599
600/**
601 * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
602 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
603 * @state: desired state, zero to resume, non-zero to suspend
604 *
605 * Calls drm_fb_helper_set_suspend, which is a wrapper around
606 * fb_set_suspend implemented by fbdev core.
607 */
608void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, int state)
609{
610 if (fbdev_cma)
611 drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
612}
613EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);