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> |
Marek Vasut | 14d7f96 | 2016-11-14 11:07:31 +0100 | [diff] [blame] | 21 | #include <drm/drm_atomic.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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> |
Marek Vasut | 14d7f96 | 2016-11-14 11:07:31 +0100 | [diff] [blame] | 27 | #include <linux/dma-buf.h> |
Robin Murphy | ce0c575 | 2016-06-07 13:18:09 +0100 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 29 | #include <linux/module.h> |
Marek Vasut | 14d7f96 | 2016-11-14 11:07:31 +0100 | [diff] [blame] | 30 | #include <linux/reservation.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 31 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 32 | #define DEFAULT_FBDEFIO_DELAY_MS 50 |
| 33 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 44 | /** |
| 45 | * DOC: framebuffer cma helper functions |
| 46 | * |
| 47 | * Provides helper functions for creating a cma (contiguous memory allocator) |
| 48 | * backed framebuffer. |
| 49 | * |
Noralf Trønnes | 02da16d | 2016-05-11 18:09:18 +0200 | [diff] [blame] | 50 | * drm_fb_cma_create() is used in the &drm_mode_config_funcs ->fb_create |
| 51 | * callback function to create a cma backed framebuffer. |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 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. |
Noralf Trønnes | 02da16d | 2016-05-11 18:09:18 +0200 | [diff] [blame] | 55 | * If the &drm_framebuffer_funcs ->dirty callback is set, fb_deferred_io |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 56 | * will be set up automatically. dirty() is called by |
| 57 | * drm_fb_helper_deferred_io() in process context (struct delayed_work). |
| 58 | * |
Daniel Vetter | da5335b | 2016-05-31 22:55:13 +0200 | [diff] [blame] | 59 | * Example fbdev deferred io code:: |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 107 | void drm_fb_cma_destroy(struct drm_framebuffer *fb) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 120 | EXPORT_SYMBOL(drm_fb_cma_destroy); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 121 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 122 | int drm_fb_cma_create_handle(struct drm_framebuffer *fb, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 130 | EXPORT_SYMBOL(drm_fb_cma_create_handle); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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, |
Colin Ian King | 70c0616 | 2016-01-20 10:59:34 +0000 | [diff] [blame] | 138 | const struct drm_mode_fb_cmd2 *mode_cmd, |
| 139 | struct drm_gem_cma_object **obj, |
Noralf Trønnes | fdce184 | 2016-05-12 20:25:21 +0200 | [diff] [blame] | 140 | unsigned int num_planes, const struct drm_framebuffer_funcs *funcs) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Daniel Vetter | c7d73f6 | 2012-12-13 23:38:38 +0100 | [diff] [blame] | 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 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 155 | ret = drm_framebuffer_init(dev, &fb_cma->fb, funcs); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 156 | if (ret) { |
Masanari Iida | 8b513d0 | 2013-05-21 23:13:12 +0900 | [diff] [blame] | 157 | dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 158 | kfree(fb_cma); |
| 159 | return ERR_PTR(ret); |
| 160 | } |
| 161 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 162 | return fb_cma; |
| 163 | } |
| 164 | |
| 165 | /** |
Noralf Trønnes | 3995b39 | 2016-05-12 20:25:22 +0200 | [diff] [blame] | 166 | * drm_fb_cma_create_with_funcs() - helper function for the |
| 167 | * &drm_mode_config_funcs ->fb_create |
| 168 | * callback function |
Daniel Vetter | 890358a | 2016-05-31 23:11:12 +0200 | [diff] [blame] | 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 |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 173 | * |
Noralf Trønnes | 3995b39 | 2016-05-12 20:25:22 +0200 | [diff] [blame] | 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. |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 177 | */ |
Noralf Trønnes | 3995b39 | 2016-05-12 20:25:22 +0200 | [diff] [blame] | 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) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 181 | { |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 182 | const struct drm_format_info *info; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 183 | struct drm_fb_cma *fb_cma; |
| 184 | struct drm_gem_cma_object *objs[4]; |
| 185 | struct drm_gem_object *obj; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 186 | int ret; |
| 187 | int i; |
| 188 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 189 | info = drm_format_info(mode_cmd->pixel_format); |
| 190 | if (!info) |
| 191 | return ERR_PTR(-EINVAL); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 192 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 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); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 196 | unsigned int min_size; |
| 197 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 198 | obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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] |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 206 | + width * info->cpp[i] |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Noralf Trønnes | 3995b39 | 2016-05-12 20:25:22 +0200 | [diff] [blame] | 217 | fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i, funcs); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | } |
Noralf Trønnes | 3995b39 | 2016-05-12 20:25:22 +0200 | [diff] [blame] | 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 |
Daniel Vetter | 890358a | 2016-05-31 23:11:12 +0200 | [diff] [blame] | 234 | * @dev: DRM device |
| 235 | * @file_priv: drm file for the ioctl call |
| 236 | * @mode_cmd: metadata from the userspace fb creation request |
Noralf Trønnes | 3995b39 | 2016-05-12 20:25:22 +0200 | [diff] [blame] | 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 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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, |
Daniel Vetter | 890358a | 2016-05-31 23:11:12 +0200 | [diff] [blame] | 260 | unsigned int plane) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Marek Vasut | 14d7f96 | 2016-11-14 11:07:31 +0100 | [diff] [blame] | 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 | |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 303 | #ifdef CONFIG_DEBUG_FS |
Lespiau, Damien | 2c9c52e | 2013-08-20 00:53:08 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
| 306 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 307 | const struct drm_format_info *info; |
| 308 | int i; |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 309 | |
| 310 | seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height, |
| 311 | (char *)&fb->pixel_format); |
| 312 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 313 | info = drm_format_info(fb->pixel_format); |
| 314 | |
| 315 | for (i = 0; i < info->num_planes; i++) { |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 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 | } |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects |
Daniel Vetter | 890358a | 2016-05-31 23:11:12 +0200 | [diff] [blame] | 324 | * in debugfs. |
| 325 | * @m: output file |
| 326 | * @arg: private data for the callback |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 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; |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 333 | |
Daniel Vetter | 9e75c0e | 2015-07-09 23:32:34 +0200 | [diff] [blame] | 334 | mutex_lock(&dev->mode_config.fb_lock); |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 335 | drm_for_each_fb(fb, dev) |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 336 | drm_fb_cma_describe(fb, m); |
Daniel Vetter | 9e75c0e | 2015-07-09 23:32:34 +0200 | [diff] [blame] | 337 | mutex_unlock(&dev->mode_config.fb_lock); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show); |
| 342 | #endif |
| 343 | |
Robin Murphy | ce0c575 | 2016-06-07 13:18:09 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 350 | static struct fb_ops drm_fbdev_cma_ops = { |
| 351 | .owner = THIS_MODULE, |
Stefan Christ | 659119d | 2016-11-14 00:03:16 +0100 | [diff] [blame] | 352 | DRM_FB_HELPER_DEFAULT_OPS, |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 353 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
| 354 | .fb_copyarea = drm_fb_helper_sys_copyarea, |
| 355 | .fb_imageblit = drm_fb_helper_sys_imageblit, |
Robin Murphy | ce0c575 | 2016-06-07 13:18:09 +0100 | [diff] [blame] | 356 | .fb_mmap = drm_fb_cma_mmap, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 357 | }; |
| 358 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 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); |
Sudip Mukherjee | 2a92762 | 2016-06-12 16:03:56 +0100 | [diff] [blame] | 383 | kfree(fbops); |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 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, |
Noralf Trønnes | fdce184 | 2016-05-12 20:25:21 +0200 | [diff] [blame] | 420 | const struct drm_framebuffer_funcs *funcs) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Thierry Reding | e0d78d08 | 2012-10-20 10:32:46 +0000 | [diff] [blame] | 433 | 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] | 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); |
Thierry Reding | 0281324 | 2012-10-20 10:32:47 +0000 | [diff] [blame] | 447 | if (IS_ERR(obj)) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 448 | return -ENOMEM; |
| 449 | |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 450 | fbi = drm_fb_helper_alloc_fbi(helper); |
| 451 | if (IS_ERR(fbi)) { |
| 452 | ret = PTR_ERR(fbi); |
Eric Anholt | 50cbc13 | 2015-12-14 16:26:26 -0800 | [diff] [blame] | 453 | goto err_gem_free_object; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 456 | 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] | 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); |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 460 | goto err_fb_info_destroy; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | fb = &fbdev_cma->fb->fb; |
| 464 | helper->fb = fb; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 465 | |
| 466 | fbi->par = helper; |
| 467 | fbi->flags = FBINFO_FLAG_DEFAULT; |
| 468 | fbi->fbops = &drm_fbdev_cma_ops; |
| 469 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 470 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); |
Rob Clark | 8d76612 | 2015-03-11 10:23:10 -0400 | [diff] [blame] | 471 | 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] | 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 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 482 | if (funcs->dirty) { |
| 483 | ret = drm_fbdev_cma_defio_init(fbi, obj); |
| 484 | if (ret) |
| 485 | goto err_cma_destroy; |
| 486 | } |
| 487 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 488 | return 0; |
| 489 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 490 | err_cma_destroy: |
| 491 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); |
| 492 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 493 | err_fb_info_destroy: |
| 494 | drm_fb_helper_release_fbi(helper); |
Eric Anholt | 50cbc13 | 2015-12-14 16:26:26 -0800 | [diff] [blame] | 495 | err_gem_free_object: |
Chris Wilson | afe705b | 2016-05-31 22:25:52 +0100 | [diff] [blame] | 496 | drm_gem_object_unreference_unlocked(&obj->base); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 497 | return ret; |
| 498 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 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 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 506 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 507 | static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = { |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 508 | .fb_probe = drm_fbdev_cma_create, |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | /** |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 512 | * 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] | 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 |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 517 | * @funcs: fb helper functions, in particular fb_probe() |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 518 | * |
| 519 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. |
| 520 | */ |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 521 | 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] | 522 | unsigned int preferred_bpp, unsigned int num_crtc, |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 523 | 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] | 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 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 535 | helper = &fbdev_cma->fb_helper; |
| 536 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 537 | drm_fb_helper_prepare(dev, helper, funcs); |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 538 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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) { |
Masanari Iida | 8b513d0 | 2013-05-21 23:13:12 +0900 | [diff] [blame] | 554 | dev_err(dev->dev, "Failed to set initial hw configuration.\n"); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | } |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 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 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | { |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 593 | drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper); |
Stefan Agner | 41b9bb1 | 2016-10-19 17:32:19 -0700 | [diff] [blame] | 594 | if (fbdev_cma->fb_helper.fbdev) |
| 595 | drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev); |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 596 | drm_fb_helper_release_fbi(&fbdev_cma->fb_helper); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 597 | |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 598 | if (fbdev_cma->fb) { |
| 599 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 600 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 601 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | { |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 616 | if (fbdev_cma) |
| 617 | drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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); |
Stefan Agner | 917f425 | 2016-02-11 17:30:14 -0800 | [diff] [blame] | 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); |