]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/drm_fb_cma_helper.c
Merge tag 'befs-v4.10-rc1' of git://github.com/luisbg/linux-befs
[karo-tx-linux.git] / drivers / gpu / drm / drm_fb_cma_helper.c
1 /*
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_atomic.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
27 #include <linux/dma-buf.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/module.h>
30 #include <linux/reservation.h>
31
32 #define DEFAULT_FBDEFIO_DELAY_MS 50
33
34 struct drm_fb_cma {
35         struct drm_framebuffer          fb;
36         struct drm_gem_cma_object       *obj[4];
37 };
38
39 struct drm_fbdev_cma {
40         struct drm_fb_helper    fb_helper;
41         struct drm_fb_cma       *fb;
42 };
43
44 /**
45  * DOC: framebuffer cma helper functions
46  *
47  * Provides helper functions for creating a cma (contiguous memory allocator)
48  * backed framebuffer.
49  *
50  * drm_fb_cma_create() is used in the &drm_mode_config_funcs ->fb_create
51  * callback function to create a cma backed framebuffer.
52  *
53  * An fbdev framebuffer backed by cma is also available by calling
54  * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
55  * If the &drm_framebuffer_funcs ->dirty callback is set, fb_deferred_io
56  * will be set up automatically. dirty() is called by
57  * drm_fb_helper_deferred_io() in process context (struct delayed_work).
58  *
59  * Example fbdev deferred io code::
60  *
61  *     static int driver_fbdev_fb_dirty(struct drm_framebuffer *fb,
62  *                                      struct drm_file *file_priv,
63  *                                      unsigned flags, unsigned color,
64  *                                      struct drm_clip_rect *clips,
65  *                                      unsigned num_clips)
66  *     {
67  *         struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
68  *         ... push changes ...
69  *         return 0;
70  *     }
71  *
72  *     static struct drm_framebuffer_funcs driver_fbdev_fb_funcs = {
73  *         .destroy       = drm_fb_cma_destroy,
74  *         .create_handle = drm_fb_cma_create_handle,
75  *         .dirty         = driver_fbdev_fb_dirty,
76  *     };
77  *
78  *     static int driver_fbdev_create(struct drm_fb_helper *helper,
79  *             struct drm_fb_helper_surface_size *sizes)
80  *     {
81  *         return drm_fbdev_cma_create_with_funcs(helper, sizes,
82  *                                                &driver_fbdev_fb_funcs);
83  *     }
84  *
85  *     static const struct drm_fb_helper_funcs driver_fb_helper_funcs = {
86  *         .fb_probe = driver_fbdev_create,
87  *     };
88  *
89  *     Initialize:
90  *     fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
91  *                                           dev->mode_config.num_crtc,
92  *                                           dev->mode_config.num_connector,
93  *                                           &driver_fb_helper_funcs);
94  *
95  */
96
97 static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
98 {
99         return container_of(helper, struct drm_fbdev_cma, fb_helper);
100 }
101
102 static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb)
103 {
104         return container_of(fb, struct drm_fb_cma, fb);
105 }
106
107 void drm_fb_cma_destroy(struct drm_framebuffer *fb)
108 {
109         struct drm_fb_cma *fb_cma = to_fb_cma(fb);
110         int i;
111
112         for (i = 0; i < 4; i++) {
113                 if (fb_cma->obj[i])
114                         drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base);
115         }
116
117         drm_framebuffer_cleanup(fb);
118         kfree(fb_cma);
119 }
120 EXPORT_SYMBOL(drm_fb_cma_destroy);
121
122 int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
123         struct drm_file *file_priv, unsigned int *handle)
124 {
125         struct drm_fb_cma *fb_cma = to_fb_cma(fb);
126
127         return drm_gem_handle_create(file_priv,
128                         &fb_cma->obj[0]->base, handle);
129 }
130 EXPORT_SYMBOL(drm_fb_cma_create_handle);
131
132 static struct drm_framebuffer_funcs drm_fb_cma_funcs = {
133         .destroy        = drm_fb_cma_destroy,
134         .create_handle  = drm_fb_cma_create_handle,
135 };
136
137 static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev,
138         const struct drm_mode_fb_cmd2 *mode_cmd,
139         struct drm_gem_cma_object **obj,
140         unsigned int num_planes, const struct drm_framebuffer_funcs *funcs)
141 {
142         struct drm_fb_cma *fb_cma;
143         int ret;
144         int i;
145
146         fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL);
147         if (!fb_cma)
148                 return ERR_PTR(-ENOMEM);
149
150         drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd);
151
152         for (i = 0; i < num_planes; i++)
153                 fb_cma->obj[i] = obj[i];
154
155         ret = drm_framebuffer_init(dev, &fb_cma->fb, funcs);
156         if (ret) {
157                 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret);
158                 kfree(fb_cma);
159                 return ERR_PTR(ret);
160         }
161
162         return fb_cma;
163 }
164
165 /**
166  * drm_fb_cma_create_with_funcs() - helper function for the
167  *                                  &drm_mode_config_funcs ->fb_create
168  *                                  callback function
169  * @dev: DRM device
170  * @file_priv: drm file for the ioctl call
171  * @mode_cmd: metadata from the userspace fb creation request
172  * @funcs: vtable to be used for the new framebuffer object
173  *
174  * This can be used to set &drm_framebuffer_funcs for drivers that need the
175  * dirty() callback. Use drm_fb_cma_create() if you don't need to change
176  * &drm_framebuffer_funcs.
177  */
178 struct drm_framebuffer *drm_fb_cma_create_with_funcs(struct drm_device *dev,
179         struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd,
180         const struct drm_framebuffer_funcs *funcs)
181 {
182         const struct drm_format_info *info;
183         struct drm_fb_cma *fb_cma;
184         struct drm_gem_cma_object *objs[4];
185         struct drm_gem_object *obj;
186         int ret;
187         int i;
188
189         info = drm_format_info(mode_cmd->pixel_format);
190         if (!info)
191                 return ERR_PTR(-EINVAL);
192
193         for (i = 0; i < info->num_planes; i++) {
194                 unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
195                 unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
196                 unsigned int min_size;
197
198                 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
199                 if (!obj) {
200                         dev_err(dev->dev, "Failed to lookup GEM object\n");
201                         ret = -ENXIO;
202                         goto err_gem_object_unreference;
203                 }
204
205                 min_size = (height - 1) * mode_cmd->pitches[i]
206                          + width * info->cpp[i]
207                          + mode_cmd->offsets[i];
208
209                 if (obj->size < min_size) {
210                         drm_gem_object_unreference_unlocked(obj);
211                         ret = -EINVAL;
212                         goto err_gem_object_unreference;
213                 }
214                 objs[i] = to_drm_gem_cma_obj(obj);
215         }
216
217         fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i, funcs);
218         if (IS_ERR(fb_cma)) {
219                 ret = PTR_ERR(fb_cma);
220                 goto err_gem_object_unreference;
221         }
222
223         return &fb_cma->fb;
224
225 err_gem_object_unreference:
226         for (i--; i >= 0; i--)
227                 drm_gem_object_unreference_unlocked(&objs[i]->base);
228         return ERR_PTR(ret);
229 }
230 EXPORT_SYMBOL_GPL(drm_fb_cma_create_with_funcs);
231
232 /**
233  * drm_fb_cma_create() - &drm_mode_config_funcs ->fb_create callback function
234  * @dev: DRM device
235  * @file_priv: drm file for the ioctl call
236  * @mode_cmd: metadata from the userspace fb creation request
237  *
238  * If your hardware has special alignment or pitch requirements these should be
239  * checked before calling this function. Use drm_fb_cma_create_with_funcs() if
240  * you need to set &drm_framebuffer_funcs ->dirty.
241  */
242 struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
243         struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
244 {
245         return drm_fb_cma_create_with_funcs(dev, file_priv, mode_cmd,
246                                             &drm_fb_cma_funcs);
247 }
248 EXPORT_SYMBOL_GPL(drm_fb_cma_create);
249
250 /**
251  * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
252  * @fb: The framebuffer
253  * @plane: Which plane
254  *
255  * Return the CMA GEM object for given framebuffer.
256  *
257  * This function will usually be called from the CRTC callback functions.
258  */
259 struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
260                                                   unsigned int plane)
261 {
262         struct drm_fb_cma *fb_cma = to_fb_cma(fb);
263
264         if (plane >= 4)
265                 return NULL;
266
267         return fb_cma->obj[plane];
268 }
269 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
270
271 /**
272  * drm_fb_cma_prepare_fb() - Prepare CMA framebuffer
273  * @plane: Which plane
274  * @state: Plane state attach fence to
275  *
276  * This should be put into prepare_fb hook of struct &drm_plane_helper_funcs .
277  *
278  * This function checks if the plane FB has an dma-buf attached, extracts
279  * the exclusive fence and attaches it to plane state for the atomic helper
280  * to wait on.
281  *
282  * There is no need for cleanup_fb for CMA based framebuffer drivers.
283  */
284 int drm_fb_cma_prepare_fb(struct drm_plane *plane,
285                           struct drm_plane_state *state)
286 {
287         struct dma_buf *dma_buf;
288         struct dma_fence *fence;
289
290         if ((plane->state->fb == state->fb) || !state->fb)
291                 return 0;
292
293         dma_buf = drm_fb_cma_get_gem_obj(state->fb, 0)->base.dma_buf;
294         if (dma_buf) {
295                 fence = reservation_object_get_excl_rcu(dma_buf->resv);
296                 drm_atomic_set_fence_for_plane(state, fence);
297         }
298
299         return 0;
300 }
301 EXPORT_SYMBOL_GPL(drm_fb_cma_prepare_fb);
302
303 #ifdef CONFIG_DEBUG_FS
304 static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
305 {
306         struct drm_fb_cma *fb_cma = to_fb_cma(fb);
307         const struct drm_format_info *info;
308         int i;
309
310         seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
311                         (char *)&fb->pixel_format);
312
313         info = drm_format_info(fb->pixel_format);
314
315         for (i = 0; i < info->num_planes; i++) {
316                 seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
317                                 i, fb->offsets[i], fb->pitches[i]);
318                 drm_gem_cma_describe(fb_cma->obj[i], m);
319         }
320 }
321
322 /**
323  * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
324  *                             in debugfs.
325  * @m: output file
326  * @arg: private data for the callback
327  */
328 int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
329 {
330         struct drm_info_node *node = (struct drm_info_node *) m->private;
331         struct drm_device *dev = node->minor->dev;
332         struct drm_framebuffer *fb;
333
334         mutex_lock(&dev->mode_config.fb_lock);
335         drm_for_each_fb(fb, dev)
336                 drm_fb_cma_describe(fb, m);
337         mutex_unlock(&dev->mode_config.fb_lock);
338
339         return 0;
340 }
341 EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
342 #endif
343
344 static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
345 {
346         return dma_mmap_writecombine(info->device, vma, info->screen_base,
347                                      info->fix.smem_start, info->fix.smem_len);
348 }
349
350 static struct fb_ops drm_fbdev_cma_ops = {
351         .owner          = THIS_MODULE,
352         DRM_FB_HELPER_DEFAULT_OPS,
353         .fb_fillrect    = drm_fb_helper_sys_fillrect,
354         .fb_copyarea    = drm_fb_helper_sys_copyarea,
355         .fb_imageblit   = drm_fb_helper_sys_imageblit,
356         .fb_mmap        = drm_fb_cma_mmap,
357 };
358
359 static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
360                                           struct vm_area_struct *vma)
361 {
362         fb_deferred_io_mmap(info, vma);
363         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
364
365         return 0;
366 }
367
368 static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
369                                     struct drm_gem_cma_object *cma_obj)
370 {
371         struct fb_deferred_io *fbdefio;
372         struct fb_ops *fbops;
373
374         /*
375          * Per device structures are needed because:
376          * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
377          * fbdefio: individual delays
378          */
379         fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
380         fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
381         if (!fbdefio || !fbops) {
382                 kfree(fbdefio);
383                 kfree(fbops);
384                 return -ENOMEM;
385         }
386
387         /* can't be offset from vaddr since dirty() uses cma_obj */
388         fbi->screen_buffer = cma_obj->vaddr;
389         /* fb_deferred_io_fault() needs a physical address */
390         fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
391
392         *fbops = *fbi->fbops;
393         fbi->fbops = fbops;
394
395         fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
396         fbdefio->deferred_io = drm_fb_helper_deferred_io;
397         fbi->fbdefio = fbdefio;
398         fb_deferred_io_init(fbi);
399         fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
400
401         return 0;
402 }
403
404 static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
405 {
406         if (!fbi->fbdefio)
407                 return;
408
409         fb_deferred_io_cleanup(fbi);
410         kfree(fbi->fbdefio);
411         kfree(fbi->fbops);
412 }
413
414 /*
415  * For use in a (struct drm_fb_helper_funcs *)->fb_probe callback function that
416  * needs custom struct drm_framebuffer_funcs, like dirty() for deferred_io use.
417  */
418 int drm_fbdev_cma_create_with_funcs(struct drm_fb_helper *helper,
419         struct drm_fb_helper_surface_size *sizes,
420         const struct drm_framebuffer_funcs *funcs)
421 {
422         struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
423         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
424         struct drm_device *dev = helper->dev;
425         struct drm_gem_cma_object *obj;
426         struct drm_framebuffer *fb;
427         unsigned int bytes_per_pixel;
428         unsigned long offset;
429         struct fb_info *fbi;
430         size_t size;
431         int ret;
432
433         DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
434                         sizes->surface_width, sizes->surface_height,
435                         sizes->surface_bpp);
436
437         bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
438
439         mode_cmd.width = sizes->surface_width;
440         mode_cmd.height = sizes->surface_height;
441         mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
442         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
443                 sizes->surface_depth);
444
445         size = mode_cmd.pitches[0] * mode_cmd.height;
446         obj = drm_gem_cma_create(dev, size);
447         if (IS_ERR(obj))
448                 return -ENOMEM;
449
450         fbi = drm_fb_helper_alloc_fbi(helper);
451         if (IS_ERR(fbi)) {
452                 ret = PTR_ERR(fbi);
453                 goto err_gem_free_object;
454         }
455
456         fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1, funcs);
457         if (IS_ERR(fbdev_cma->fb)) {
458                 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
459                 ret = PTR_ERR(fbdev_cma->fb);
460                 goto err_fb_info_destroy;
461         }
462
463         fb = &fbdev_cma->fb->fb;
464         helper->fb = fb;
465
466         fbi->par = helper;
467         fbi->flags = FBINFO_FLAG_DEFAULT;
468         fbi->fbops = &drm_fbdev_cma_ops;
469
470         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
471         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
472
473         offset = fbi->var.xoffset * bytes_per_pixel;
474         offset += fbi->var.yoffset * fb->pitches[0];
475
476         dev->mode_config.fb_base = (resource_size_t)obj->paddr;
477         fbi->screen_base = obj->vaddr + offset;
478         fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
479         fbi->screen_size = size;
480         fbi->fix.smem_len = size;
481
482         if (funcs->dirty) {
483                 ret = drm_fbdev_cma_defio_init(fbi, obj);
484                 if (ret)
485                         goto err_cma_destroy;
486         }
487
488         return 0;
489
490 err_cma_destroy:
491         drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
492         drm_fb_cma_destroy(&fbdev_cma->fb->fb);
493 err_fb_info_destroy:
494         drm_fb_helper_release_fbi(helper);
495 err_gem_free_object:
496         drm_gem_object_unreference_unlocked(&obj->base);
497         return ret;
498 }
499 EXPORT_SYMBOL(drm_fbdev_cma_create_with_funcs);
500
501 static int drm_fbdev_cma_create(struct drm_fb_helper *helper,
502         struct drm_fb_helper_surface_size *sizes)
503 {
504         return drm_fbdev_cma_create_with_funcs(helper, sizes, &drm_fb_cma_funcs);
505 }
506
507 static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
508         .fb_probe = drm_fbdev_cma_create,
509 };
510
511 /**
512  * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
513  * @dev: DRM device
514  * @preferred_bpp: Preferred bits per pixel for the device
515  * @num_crtc: Number of CRTCs
516  * @max_conn_count: Maximum number of connectors
517  * @funcs: fb helper functions, in particular fb_probe()
518  *
519  * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
520  */
521 struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
522         unsigned int preferred_bpp, unsigned int num_crtc,
523         unsigned int max_conn_count, const struct drm_fb_helper_funcs *funcs)
524 {
525         struct drm_fbdev_cma *fbdev_cma;
526         struct drm_fb_helper *helper;
527         int ret;
528
529         fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
530         if (!fbdev_cma) {
531                 dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
532                 return ERR_PTR(-ENOMEM);
533         }
534
535         helper = &fbdev_cma->fb_helper;
536
537         drm_fb_helper_prepare(dev, helper, funcs);
538
539         ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
540         if (ret < 0) {
541                 dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
542                 goto err_free;
543         }
544
545         ret = drm_fb_helper_single_add_all_connectors(helper);
546         if (ret < 0) {
547                 dev_err(dev->dev, "Failed to add connectors.\n");
548                 goto err_drm_fb_helper_fini;
549
550         }
551
552         ret = drm_fb_helper_initial_config(helper, preferred_bpp);
553         if (ret < 0) {
554                 dev_err(dev->dev, "Failed to set initial hw configuration.\n");
555                 goto err_drm_fb_helper_fini;
556         }
557
558         return fbdev_cma;
559
560 err_drm_fb_helper_fini:
561         drm_fb_helper_fini(helper);
562 err_free:
563         kfree(fbdev_cma);
564
565         return ERR_PTR(ret);
566 }
567 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
568
569 /**
570  * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
571  * @dev: DRM device
572  * @preferred_bpp: Preferred bits per pixel for the device
573  * @num_crtc: Number of CRTCs
574  * @max_conn_count: Maximum number of connectors
575  *
576  * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
577  */
578 struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
579         unsigned int preferred_bpp, unsigned int num_crtc,
580         unsigned int max_conn_count)
581 {
582         return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp, num_crtc,
583                                 max_conn_count, &drm_fb_cma_helper_funcs);
584 }
585 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
586
587 /**
588  * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
589  * @fbdev_cma: The drm_fbdev_cma struct
590  */
591 void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
592 {
593         drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
594         if (fbdev_cma->fb_helper.fbdev)
595                 drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
596         drm_fb_helper_release_fbi(&fbdev_cma->fb_helper);
597
598         if (fbdev_cma->fb) {
599                 drm_framebuffer_unregister_private(&fbdev_cma->fb->fb);
600                 drm_fb_cma_destroy(&fbdev_cma->fb->fb);
601         }
602
603         drm_fb_helper_fini(&fbdev_cma->fb_helper);
604         kfree(fbdev_cma);
605 }
606 EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
607
608 /**
609  * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
610  * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
611  *
612  * This function is usually called from the DRM drivers lastclose callback.
613  */
614 void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
615 {
616         if (fbdev_cma)
617                 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
618 }
619 EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
620
621 /**
622  * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
623  * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
624  *
625  * This function is usually called from the DRM drivers output_poll_changed
626  * callback.
627  */
628 void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
629 {
630         if (fbdev_cma)
631                 drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
632 }
633 EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
634
635 /**
636  * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
637  * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
638  * @state: desired state, zero to resume, non-zero to suspend
639  *
640  * Calls drm_fb_helper_set_suspend, which is a wrapper around
641  * fb_set_suspend implemented by fbdev core.
642  */
643 void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, int state)
644 {
645         if (fbdev_cma)
646                 drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
647 }
648 EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);