Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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_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 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 28 | #define DEFAULT_FBDEFIO_DELAY_MS 50 |
| 29 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 30 | struct drm_fb_cma { |
| 31 | struct drm_framebuffer fb; |
| 32 | struct drm_gem_cma_object *obj[4]; |
| 33 | }; |
| 34 | |
| 35 | struct drm_fbdev_cma { |
| 36 | struct drm_fb_helper fb_helper; |
| 37 | struct drm_fb_cma *fb; |
| 38 | }; |
| 39 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 40 | /** |
| 41 | * DOC: framebuffer cma helper functions |
| 42 | * |
| 43 | * Provides helper functions for creating a cma (contiguous memory allocator) |
| 44 | * backed framebuffer. |
| 45 | * |
| 46 | * drm_fb_cma_create() is used in the |
| 47 | * (struct drm_mode_config_funcs *)->fb_create callback function to create the |
| 48 | * cma backed framebuffer. |
| 49 | * |
| 50 | * An fbdev framebuffer backed by cma is also available by calling |
| 51 | * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down. |
| 52 | * If CONFIG_FB_DEFERRED_IO is enabled and the callback |
| 53 | * (struct drm_framebuffer_funcs)->dirty is set, fb_deferred_io |
| 54 | * will be set up automatically. dirty() is called by |
| 55 | * drm_fb_helper_deferred_io() in process context (struct delayed_work). |
| 56 | * |
| 57 | * Example fbdev deferred io code: |
| 58 | * |
| 59 | * static int driver_fbdev_fb_dirty(struct drm_framebuffer *fb, |
| 60 | * struct drm_file *file_priv, |
| 61 | * unsigned flags, unsigned color, |
| 62 | * struct drm_clip_rect *clips, |
| 63 | * unsigned num_clips) |
| 64 | * { |
| 65 | * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0); |
| 66 | * ... push changes ... |
| 67 | * return 0; |
| 68 | * } |
| 69 | * |
| 70 | * static struct drm_framebuffer_funcs driver_fbdev_fb_funcs = { |
| 71 | * .destroy = drm_fb_cma_destroy, |
| 72 | * .create_handle = drm_fb_cma_create_handle, |
| 73 | * .dirty = driver_fbdev_fb_dirty, |
| 74 | * }; |
| 75 | * |
| 76 | * static int driver_fbdev_create(struct drm_fb_helper *helper, |
| 77 | * struct drm_fb_helper_surface_size *sizes) |
| 78 | * { |
| 79 | * return drm_fbdev_cma_create_with_funcs(helper, sizes, |
| 80 | * &driver_fbdev_fb_funcs); |
| 81 | * } |
| 82 | * |
| 83 | * static const struct drm_fb_helper_funcs driver_fb_helper_funcs = { |
| 84 | * .fb_probe = driver_fbdev_create, |
| 85 | * }; |
| 86 | * |
| 87 | * Initialize: |
| 88 | * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16, |
| 89 | * dev->mode_config.num_crtc, |
| 90 | * dev->mode_config.num_connector, |
| 91 | * &driver_fb_helper_funcs); |
| 92 | * |
| 93 | */ |
| 94 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 95 | static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper) |
| 96 | { |
| 97 | return container_of(helper, struct drm_fbdev_cma, fb_helper); |
| 98 | } |
| 99 | |
| 100 | static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb) |
| 101 | { |
| 102 | return container_of(fb, struct drm_fb_cma, fb); |
| 103 | } |
| 104 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 105 | void drm_fb_cma_destroy(struct drm_framebuffer *fb) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 106 | { |
| 107 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); |
| 108 | int i; |
| 109 | |
| 110 | for (i = 0; i < 4; i++) { |
| 111 | if (fb_cma->obj[i]) |
| 112 | drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base); |
| 113 | } |
| 114 | |
| 115 | drm_framebuffer_cleanup(fb); |
| 116 | kfree(fb_cma); |
| 117 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 118 | EXPORT_SYMBOL(drm_fb_cma_destroy); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 119 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 120 | int drm_fb_cma_create_handle(struct drm_framebuffer *fb, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 121 | struct drm_file *file_priv, unsigned int *handle) |
| 122 | { |
| 123 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); |
| 124 | |
| 125 | return drm_gem_handle_create(file_priv, |
| 126 | &fb_cma->obj[0]->base, handle); |
| 127 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 128 | EXPORT_SYMBOL(drm_fb_cma_create_handle); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 129 | |
| 130 | static struct drm_framebuffer_funcs drm_fb_cma_funcs = { |
| 131 | .destroy = drm_fb_cma_destroy, |
| 132 | .create_handle = drm_fb_cma_create_handle, |
| 133 | }; |
| 134 | |
| 135 | static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev, |
Colin Ian King | 70c0616 | 2016-01-20 10:59:34 +0000 | [diff] [blame] | 136 | const struct drm_mode_fb_cmd2 *mode_cmd, |
| 137 | struct drm_gem_cma_object **obj, |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 138 | unsigned int num_planes, struct drm_framebuffer_funcs *funcs) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 139 | { |
| 140 | struct drm_fb_cma *fb_cma; |
| 141 | int ret; |
| 142 | int i; |
| 143 | |
| 144 | fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL); |
| 145 | if (!fb_cma) |
| 146 | return ERR_PTR(-ENOMEM); |
| 147 | |
Daniel Vetter | c7d73f6 | 2012-12-13 23:38:38 +0100 | [diff] [blame] | 148 | drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd); |
| 149 | |
| 150 | for (i = 0; i < num_planes; i++) |
| 151 | fb_cma->obj[i] = obj[i]; |
| 152 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 153 | ret = drm_framebuffer_init(dev, &fb_cma->fb, funcs); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 154 | if (ret) { |
Masanari Iida | 8b513d0 | 2013-05-21 23:13:12 +0900 | [diff] [blame] | 155 | dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 156 | kfree(fb_cma); |
| 157 | return ERR_PTR(ret); |
| 158 | } |
| 159 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 160 | return fb_cma; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * drm_fb_cma_create() - (struct drm_mode_config_funcs *)->fb_create callback function |
| 165 | * |
| 166 | * If your hardware has special alignment or pitch requirements these should be |
| 167 | * checked before calling this function. |
| 168 | */ |
| 169 | struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev, |
Ville Syrjälä | 1eb83451 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 170 | struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 171 | { |
| 172 | struct drm_fb_cma *fb_cma; |
| 173 | struct drm_gem_cma_object *objs[4]; |
| 174 | struct drm_gem_object *obj; |
| 175 | unsigned int hsub; |
| 176 | unsigned int vsub; |
| 177 | int ret; |
| 178 | int i; |
| 179 | |
| 180 | hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format); |
| 181 | vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format); |
| 182 | |
| 183 | for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) { |
| 184 | unsigned int width = mode_cmd->width / (i ? hsub : 1); |
| 185 | unsigned int height = mode_cmd->height / (i ? vsub : 1); |
| 186 | unsigned int min_size; |
| 187 | |
| 188 | obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[i]); |
| 189 | if (!obj) { |
| 190 | dev_err(dev->dev, "Failed to lookup GEM object\n"); |
| 191 | ret = -ENXIO; |
| 192 | goto err_gem_object_unreference; |
| 193 | } |
| 194 | |
| 195 | min_size = (height - 1) * mode_cmd->pitches[i] |
| 196 | + width * drm_format_plane_cpp(mode_cmd->pixel_format, i) |
| 197 | + mode_cmd->offsets[i]; |
| 198 | |
| 199 | if (obj->size < min_size) { |
| 200 | drm_gem_object_unreference_unlocked(obj); |
| 201 | ret = -EINVAL; |
| 202 | goto err_gem_object_unreference; |
| 203 | } |
| 204 | objs[i] = to_drm_gem_cma_obj(obj); |
| 205 | } |
| 206 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 207 | fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i, &drm_fb_cma_funcs); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 208 | if (IS_ERR(fb_cma)) { |
| 209 | ret = PTR_ERR(fb_cma); |
| 210 | goto err_gem_object_unreference; |
| 211 | } |
| 212 | |
| 213 | return &fb_cma->fb; |
| 214 | |
| 215 | err_gem_object_unreference: |
| 216 | for (i--; i >= 0; i--) |
| 217 | drm_gem_object_unreference_unlocked(&objs[i]->base); |
| 218 | return ERR_PTR(ret); |
| 219 | } |
| 220 | EXPORT_SYMBOL_GPL(drm_fb_cma_create); |
| 221 | |
| 222 | /** |
| 223 | * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer |
| 224 | * @fb: The framebuffer |
| 225 | * @plane: Which plane |
| 226 | * |
| 227 | * Return the CMA GEM object for given framebuffer. |
| 228 | * |
| 229 | * This function will usually be called from the CRTC callback functions. |
| 230 | */ |
| 231 | struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb, |
| 232 | unsigned int plane) |
| 233 | { |
| 234 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); |
| 235 | |
| 236 | if (plane >= 4) |
| 237 | return NULL; |
| 238 | |
| 239 | return fb_cma->obj[plane]; |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj); |
| 242 | |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 243 | #ifdef CONFIG_DEBUG_FS |
Lespiau, Damien | 2c9c52e | 2013-08-20 00:53:08 +0100 | [diff] [blame] | 244 | /* |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 245 | * drm_fb_cma_describe() - Helper to dump information about a single |
| 246 | * CMA framebuffer object |
| 247 | */ |
Lespiau, Damien | 2c9c52e | 2013-08-20 00:53:08 +0100 | [diff] [blame] | 248 | static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m) |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 249 | { |
| 250 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); |
| 251 | int i, n = drm_format_num_planes(fb->pixel_format); |
| 252 | |
| 253 | seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height, |
| 254 | (char *)&fb->pixel_format); |
| 255 | |
| 256 | for (i = 0; i < n; i++) { |
| 257 | seq_printf(m, " %d: offset=%d pitch=%d, obj: ", |
| 258 | i, fb->offsets[i], fb->pitches[i]); |
| 259 | drm_gem_cma_describe(fb_cma->obj[i], m); |
| 260 | } |
| 261 | } |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects |
| 265 | * in debugfs. |
| 266 | */ |
| 267 | int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg) |
| 268 | { |
| 269 | struct drm_info_node *node = (struct drm_info_node *) m->private; |
| 270 | struct drm_device *dev = node->minor->dev; |
| 271 | struct drm_framebuffer *fb; |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 272 | |
Daniel Vetter | 9e75c0e | 2015-07-09 23:32:34 +0200 | [diff] [blame] | 273 | mutex_lock(&dev->mode_config.fb_lock); |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 274 | drm_for_each_fb(fb, dev) |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 275 | drm_fb_cma_describe(fb, m); |
Daniel Vetter | 9e75c0e | 2015-07-09 23:32:34 +0200 | [diff] [blame] | 276 | mutex_unlock(&dev->mode_config.fb_lock); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show); |
| 281 | #endif |
| 282 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 283 | static struct fb_ops drm_fbdev_cma_ops = { |
| 284 | .owner = THIS_MODULE, |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 285 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
| 286 | .fb_copyarea = drm_fb_helper_sys_copyarea, |
| 287 | .fb_imageblit = drm_fb_helper_sys_imageblit, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 288 | .fb_check_var = drm_fb_helper_check_var, |
| 289 | .fb_set_par = drm_fb_helper_set_par, |
| 290 | .fb_blank = drm_fb_helper_blank, |
| 291 | .fb_pan_display = drm_fb_helper_pan_display, |
| 292 | .fb_setcmap = drm_fb_helper_setcmap, |
| 293 | }; |
| 294 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 295 | static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info, |
| 296 | struct vm_area_struct *vma) |
| 297 | { |
| 298 | fb_deferred_io_mmap(info, vma); |
| 299 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int drm_fbdev_cma_defio_init(struct fb_info *fbi, |
| 305 | struct drm_gem_cma_object *cma_obj) |
| 306 | { |
| 307 | struct fb_deferred_io *fbdefio; |
| 308 | struct fb_ops *fbops; |
| 309 | |
| 310 | /* |
| 311 | * Per device structures are needed because: |
| 312 | * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap |
| 313 | * fbdefio: individual delays |
| 314 | */ |
| 315 | fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL); |
| 316 | fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); |
| 317 | if (!fbdefio || !fbops) { |
| 318 | kfree(fbdefio); |
| 319 | return -ENOMEM; |
| 320 | } |
| 321 | |
| 322 | /* can't be offset from vaddr since dirty() uses cma_obj */ |
| 323 | fbi->screen_buffer = cma_obj->vaddr; |
| 324 | /* fb_deferred_io_fault() needs a physical address */ |
| 325 | fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer)); |
| 326 | |
| 327 | *fbops = *fbi->fbops; |
| 328 | fbi->fbops = fbops; |
| 329 | |
| 330 | fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS); |
| 331 | fbdefio->deferred_io = drm_fb_helper_deferred_io; |
| 332 | fbi->fbdefio = fbdefio; |
| 333 | fb_deferred_io_init(fbi); |
| 334 | fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static void drm_fbdev_cma_defio_fini(struct fb_info *fbi) |
| 340 | { |
| 341 | if (!fbi->fbdefio) |
| 342 | return; |
| 343 | |
| 344 | fb_deferred_io_cleanup(fbi); |
| 345 | kfree(fbi->fbdefio); |
| 346 | kfree(fbi->fbops); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * For use in a (struct drm_fb_helper_funcs *)->fb_probe callback function that |
| 351 | * needs custom struct drm_framebuffer_funcs, like dirty() for deferred_io use. |
| 352 | */ |
| 353 | int drm_fbdev_cma_create_with_funcs(struct drm_fb_helper *helper, |
| 354 | struct drm_fb_helper_surface_size *sizes, |
| 355 | struct drm_framebuffer_funcs *funcs) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 356 | { |
| 357 | struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper); |
| 358 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; |
| 359 | struct drm_device *dev = helper->dev; |
| 360 | struct drm_gem_cma_object *obj; |
| 361 | struct drm_framebuffer *fb; |
| 362 | unsigned int bytes_per_pixel; |
| 363 | unsigned long offset; |
| 364 | struct fb_info *fbi; |
| 365 | size_t size; |
| 366 | int ret; |
| 367 | |
Thierry Reding | e0d78d08 | 2012-10-20 10:32:46 +0000 | [diff] [blame] | 368 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 369 | sizes->surface_width, sizes->surface_height, |
| 370 | sizes->surface_bpp); |
| 371 | |
| 372 | bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); |
| 373 | |
| 374 | mode_cmd.width = sizes->surface_width; |
| 375 | mode_cmd.height = sizes->surface_height; |
| 376 | mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel; |
| 377 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 378 | sizes->surface_depth); |
| 379 | |
| 380 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 381 | obj = drm_gem_cma_create(dev, size); |
Thierry Reding | 0281324 | 2012-10-20 10:32:47 +0000 | [diff] [blame] | 382 | if (IS_ERR(obj)) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 383 | return -ENOMEM; |
| 384 | |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 385 | fbi = drm_fb_helper_alloc_fbi(helper); |
| 386 | if (IS_ERR(fbi)) { |
| 387 | ret = PTR_ERR(fbi); |
Eric Anholt | 50cbc13 | 2015-12-14 16:26:26 -0800 | [diff] [blame] | 388 | goto err_gem_free_object; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 391 | fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1, funcs); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 392 | if (IS_ERR(fbdev_cma->fb)) { |
| 393 | dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n"); |
| 394 | ret = PTR_ERR(fbdev_cma->fb); |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 395 | goto err_fb_info_destroy; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | fb = &fbdev_cma->fb->fb; |
| 399 | helper->fb = fb; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 400 | |
| 401 | fbi->par = helper; |
| 402 | fbi->flags = FBINFO_FLAG_DEFAULT; |
| 403 | fbi->fbops = &drm_fbdev_cma_ops; |
| 404 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 405 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); |
Rob Clark | 8d76612 | 2015-03-11 10:23:10 -0400 | [diff] [blame] | 406 | drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 407 | |
| 408 | offset = fbi->var.xoffset * bytes_per_pixel; |
| 409 | offset += fbi->var.yoffset * fb->pitches[0]; |
| 410 | |
| 411 | dev->mode_config.fb_base = (resource_size_t)obj->paddr; |
| 412 | fbi->screen_base = obj->vaddr + offset; |
| 413 | fbi->fix.smem_start = (unsigned long)(obj->paddr + offset); |
| 414 | fbi->screen_size = size; |
| 415 | fbi->fix.smem_len = size; |
| 416 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 417 | if (funcs->dirty) { |
| 418 | ret = drm_fbdev_cma_defio_init(fbi, obj); |
| 419 | if (ret) |
| 420 | goto err_cma_destroy; |
| 421 | } |
| 422 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 423 | return 0; |
| 424 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 425 | err_cma_destroy: |
| 426 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); |
| 427 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 428 | err_fb_info_destroy: |
| 429 | drm_fb_helper_release_fbi(helper); |
Eric Anholt | 50cbc13 | 2015-12-14 16:26:26 -0800 | [diff] [blame] | 430 | err_gem_free_object: |
| 431 | dev->driver->gem_free_object(&obj->base); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 432 | return ret; |
| 433 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 434 | EXPORT_SYMBOL(drm_fbdev_cma_create_with_funcs); |
| 435 | |
| 436 | static int drm_fbdev_cma_create(struct drm_fb_helper *helper, |
| 437 | struct drm_fb_helper_surface_size *sizes) |
| 438 | { |
| 439 | return drm_fbdev_cma_create_with_funcs(helper, sizes, &drm_fb_cma_funcs); |
| 440 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 441 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 442 | static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = { |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 443 | .fb_probe = drm_fbdev_cma_create, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | /** |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 447 | * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 448 | * @dev: DRM device |
| 449 | * @preferred_bpp: Preferred bits per pixel for the device |
| 450 | * @num_crtc: Number of CRTCs |
| 451 | * @max_conn_count: Maximum number of connectors |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 452 | * @funcs: fb helper functions, in particular fb_probe() |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 453 | * |
| 454 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. |
| 455 | */ |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 456 | struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 457 | unsigned int preferred_bpp, unsigned int num_crtc, |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 458 | unsigned int max_conn_count, const struct drm_fb_helper_funcs *funcs) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 459 | { |
| 460 | struct drm_fbdev_cma *fbdev_cma; |
| 461 | struct drm_fb_helper *helper; |
| 462 | int ret; |
| 463 | |
| 464 | fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL); |
| 465 | if (!fbdev_cma) { |
| 466 | dev_err(dev->dev, "Failed to allocate drm fbdev.\n"); |
| 467 | return ERR_PTR(-ENOMEM); |
| 468 | } |
| 469 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 470 | helper = &fbdev_cma->fb_helper; |
| 471 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 472 | drm_fb_helper_prepare(dev, helper, funcs); |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 473 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 474 | ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count); |
| 475 | if (ret < 0) { |
| 476 | dev_err(dev->dev, "Failed to initialize drm fb helper.\n"); |
| 477 | goto err_free; |
| 478 | } |
| 479 | |
| 480 | ret = drm_fb_helper_single_add_all_connectors(helper); |
| 481 | if (ret < 0) { |
| 482 | dev_err(dev->dev, "Failed to add connectors.\n"); |
| 483 | goto err_drm_fb_helper_fini; |
| 484 | |
| 485 | } |
| 486 | |
| 487 | ret = drm_fb_helper_initial_config(helper, preferred_bpp); |
| 488 | if (ret < 0) { |
Masanari Iida | 8b513d0 | 2013-05-21 23:13:12 +0900 | [diff] [blame] | 489 | dev_err(dev->dev, "Failed to set initial hw configuration.\n"); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 490 | goto err_drm_fb_helper_fini; |
| 491 | } |
| 492 | |
| 493 | return fbdev_cma; |
| 494 | |
| 495 | err_drm_fb_helper_fini: |
| 496 | drm_fb_helper_fini(helper); |
| 497 | err_free: |
| 498 | kfree(fbdev_cma); |
| 499 | |
| 500 | return ERR_PTR(ret); |
| 501 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 502 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs); |
| 503 | |
| 504 | /** |
| 505 | * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct |
| 506 | * @dev: DRM device |
| 507 | * @preferred_bpp: Preferred bits per pixel for the device |
| 508 | * @num_crtc: Number of CRTCs |
| 509 | * @max_conn_count: Maximum number of connectors |
| 510 | * |
| 511 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. |
| 512 | */ |
| 513 | struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev, |
| 514 | unsigned int preferred_bpp, unsigned int num_crtc, |
| 515 | unsigned int max_conn_count) |
| 516 | { |
| 517 | return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp, num_crtc, |
| 518 | max_conn_count, &drm_fb_cma_helper_funcs); |
| 519 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 520 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_init); |
| 521 | |
| 522 | /** |
| 523 | * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct |
| 524 | * @fbdev_cma: The drm_fbdev_cma struct |
| 525 | */ |
| 526 | void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma) |
| 527 | { |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 528 | drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper); |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame^] | 529 | drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev); |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 530 | drm_fb_helper_release_fbi(&fbdev_cma->fb_helper); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 531 | |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 532 | if (fbdev_cma->fb) { |
| 533 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 534 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 535 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 536 | |
| 537 | drm_fb_helper_fini(&fbdev_cma->fb_helper); |
| 538 | kfree(fbdev_cma); |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini); |
| 541 | |
| 542 | /** |
| 543 | * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode |
| 544 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
| 545 | * |
| 546 | * This function is usually called from the DRM drivers lastclose callback. |
| 547 | */ |
| 548 | void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma) |
| 549 | { |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 550 | if (fbdev_cma) |
| 551 | drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 552 | } |
| 553 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode); |
| 554 | |
| 555 | /** |
| 556 | * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events |
| 557 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
| 558 | * |
| 559 | * This function is usually called from the DRM drivers output_poll_changed |
| 560 | * callback. |
| 561 | */ |
| 562 | void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma) |
| 563 | { |
| 564 | if (fbdev_cma) |
| 565 | drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper); |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event); |