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