Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_fb.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Authors: |
| 5 | * Inki Dae <inki.dae@samsung.com> |
| 6 | * Joonyoung Shim <jy0922.shim@samsung.com> |
| 7 | * Seung-Woo Kim <sw0312.kim@samsung.com> |
| 8 | * |
Inki Dae | d81aecb | 2012-12-18 02:30:17 +0900 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 13 | */ |
| 14 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 15 | #include <drm/drmP.h> |
| 16 | #include <drm/drm_crtc.h> |
| 17 | #include <drm/drm_crtc_helper.h> |
| 18 | #include <drm/drm_fb_helper.h> |
Gustavo Padovan | d6562a2 | 2015-06-01 12:04:52 -0300 | [diff] [blame] | 19 | #include <drm/drm_atomic.h> |
Gustavo Padovan | 910874a | 2015-06-01 12:04:46 -0300 | [diff] [blame] | 20 | #include <drm/drm_atomic_helper.h> |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 21 | #include <uapi/drm/exynos_drm.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 22 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 23 | #include "exynos_drm_drv.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 24 | #include "exynos_drm_fb.h" |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 25 | #include "exynos_drm_fbdev.h" |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 26 | #include "exynos_drm_iommu.h" |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 27 | #include "exynos_drm_crtc.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 28 | |
| 29 | #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb) |
| 30 | |
| 31 | /* |
| 32 | * exynos specific framebuffer structure. |
| 33 | * |
| 34 | * @fb: drm framebuffer obejct. |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 35 | * @buf_cnt: a buffer count to drm framebuffer. |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 36 | * @exynos_gem_obj: array of exynos specific gem object containing a gem object. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 37 | */ |
| 38 | struct exynos_drm_fb { |
| 39 | struct drm_framebuffer fb; |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 40 | unsigned int buf_cnt; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 41 | struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER]; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 44 | static int check_fb_gem_memory_type(struct drm_device *drm_dev, |
| 45 | struct exynos_drm_gem_obj *exynos_gem_obj) |
| 46 | { |
| 47 | unsigned int flags; |
| 48 | |
| 49 | /* |
| 50 | * if exynos drm driver supports iommu then framebuffer can use |
| 51 | * all the buffer types. |
| 52 | */ |
| 53 | if (is_drm_iommu_supported(drm_dev)) |
| 54 | return 0; |
| 55 | |
| 56 | flags = exynos_gem_obj->flags; |
| 57 | |
| 58 | /* |
| 59 | * without iommu support, not support physically non-continuous memory |
| 60 | * for framebuffer. |
| 61 | */ |
| 62 | if (IS_NONCONTIG_BUFFER(flags)) { |
| 63 | DRM_ERROR("cannot use this gem memory type for fb.\n"); |
| 64 | return -EINVAL; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 70 | static void exynos_drm_fb_destroy(struct drm_framebuffer *fb) |
| 71 | { |
| 72 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 73 | unsigned int i; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 74 | |
Inki Dae | 1daa892 | 2012-11-22 17:41:23 +0900 | [diff] [blame] | 75 | /* make sure that overlay data are updated before relesing fb. */ |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 76 | exynos_drm_crtc_complete_scanout(fb); |
Inki Dae | 1daa892 | 2012-11-22 17:41:23 +0900 | [diff] [blame] | 77 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 78 | drm_framebuffer_cleanup(fb); |
| 79 | |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 80 | for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) { |
| 81 | struct drm_gem_object *obj; |
| 82 | |
| 83 | if (exynos_fb->exynos_gem_obj[i] == NULL) |
| 84 | continue; |
| 85 | |
| 86 | obj = &exynos_fb->exynos_gem_obj[i]->base; |
| 87 | drm_gem_object_unreference_unlocked(obj); |
| 88 | } |
| 89 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 90 | kfree(exynos_fb); |
| 91 | exynos_fb = NULL; |
| 92 | } |
| 93 | |
| 94 | static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb, |
| 95 | struct drm_file *file_priv, |
| 96 | unsigned int *handle) |
| 97 | { |
| 98 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
| 99 | |
Inki Dae | b9ede27 | 2013-01-29 17:51:09 +0900 | [diff] [blame] | 100 | /* This fb should have only one gem object. */ |
| 101 | if (WARN_ON(exynos_fb->buf_cnt != 1)) |
| 102 | return -EINVAL; |
| 103 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 104 | return drm_gem_handle_create(file_priv, |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 105 | &exynos_fb->exynos_gem_obj[0]->base, handle); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int exynos_drm_fb_dirty(struct drm_framebuffer *fb, |
| 109 | struct drm_file *file_priv, unsigned flags, |
| 110 | unsigned color, struct drm_clip_rect *clips, |
| 111 | unsigned num_clips) |
| 112 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 113 | /* TODO */ |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static struct drm_framebuffer_funcs exynos_drm_fb_funcs = { |
| 119 | .destroy = exynos_drm_fb_destroy, |
| 120 | .create_handle = exynos_drm_fb_create_handle, |
| 121 | .dirty = exynos_drm_fb_dirty, |
| 122 | }; |
| 123 | |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 124 | unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb) |
| 125 | { |
| 126 | struct exynos_drm_fb *exynos_fb; |
| 127 | |
| 128 | exynos_fb = to_exynos_fb(fb); |
| 129 | |
| 130 | return exynos_fb->buf_cnt; |
| 131 | } |
| 132 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 133 | struct drm_framebuffer * |
| 134 | exynos_drm_framebuffer_init(struct drm_device *dev, |
| 135 | struct drm_mode_fb_cmd2 *mode_cmd, |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 136 | struct exynos_drm_gem_obj **gem_obj, |
| 137 | int count) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 138 | { |
| 139 | struct exynos_drm_fb *exynos_fb; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 140 | int i; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 141 | int ret; |
| 142 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 143 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
Sachin Kamat | 38bb525 | 2013-08-19 19:04:55 +0900 | [diff] [blame] | 144 | if (!exynos_fb) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 145 | return ERR_PTR(-ENOMEM); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 146 | |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 147 | exynos_fb->buf_cnt = count; |
| 148 | DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 149 | |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 150 | for (i = 0; i < count; i++) { |
| 151 | ret = check_fb_gem_memory_type(dev, gem_obj[i]); |
| 152 | if (ret < 0) |
| 153 | goto err; |
| 154 | |
| 155 | exynos_fb->exynos_gem_obj[i] = gem_obj[i]; |
| 156 | } |
| 157 | |
| 158 | drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); |
Joonyoung Shim | 94e30d9 | 2015-09-01 16:22:47 +0900 | [diff] [blame] | 159 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 160 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 161 | if (ret < 0) { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 162 | DRM_ERROR("failed to initialize framebuffer\n"); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 163 | goto err; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 164 | } |
| 165 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 166 | return &exynos_fb->fb; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 167 | |
| 168 | err: |
| 169 | kfree(exynos_fb); |
| 170 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 173 | static struct drm_framebuffer * |
| 174 | exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
| 175 | struct drm_mode_fb_cmd2 *mode_cmd) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 176 | { |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 177 | struct exynos_drm_gem_obj *gem_objs[MAX_FB_BUFFER]; |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 178 | struct drm_gem_object *obj; |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 179 | struct drm_framebuffer *fb; |
| 180 | int i; |
| 181 | int ret; |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 182 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 183 | for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) { |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 184 | obj = drm_gem_object_lookup(dev, file_priv, |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 185 | mode_cmd->handles[i]); |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 186 | if (!obj) { |
| 187 | DRM_ERROR("failed to lookup gem object\n"); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 188 | ret = -ENOENT; |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 189 | goto err; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 192 | gem_objs[i] = to_exynos_gem_obj(obj); |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 193 | } |
| 194 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 195 | fb = exynos_drm_framebuffer_init(dev, mode_cmd, gem_objs, i); |
| 196 | if (IS_ERR(fb)) { |
| 197 | ret = PTR_ERR(fb); |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 198 | goto err; |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 201 | return fb; |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 202 | |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 203 | err: |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame^] | 204 | while (i--) |
| 205 | drm_gem_object_unreference_unlocked(&gem_objs[i]->base); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 206 | |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 207 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 210 | struct exynos_drm_gem_obj *exynos_drm_fb_gem_obj(struct drm_framebuffer *fb, |
| 211 | int index) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 212 | { |
| 213 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 214 | struct exynos_drm_gem_obj *obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 215 | |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 216 | if (index >= MAX_FB_BUFFER) |
| 217 | return NULL; |
| 218 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 219 | obj = exynos_fb->exynos_gem_obj[index]; |
| 220 | if (!obj) |
Inki Dae | 19c8b83 | 2011-10-14 13:29:46 +0900 | [diff] [blame] | 221 | return NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 222 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 223 | DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)obj->dma_addr); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 224 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 225 | return obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 226 | } |
| 227 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 228 | static void exynos_drm_output_poll_changed(struct drm_device *dev) |
| 229 | { |
| 230 | struct exynos_drm_private *private = dev->dev_private; |
| 231 | struct drm_fb_helper *fb_helper = private->fb_helper; |
| 232 | |
| 233 | if (fb_helper) |
| 234 | drm_fb_helper_hotplug_event(fb_helper); |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 235 | else |
| 236 | exynos_drm_fbdev_init(dev); |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 237 | } |
| 238 | |
Laurent Pinchart | e6ecefa | 2012-05-17 13:27:23 +0200 | [diff] [blame] | 239 | static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 240 | .fb_create = exynos_user_fb_create, |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 241 | .output_poll_changed = exynos_drm_output_poll_changed, |
Gustavo Padovan | 910874a | 2015-06-01 12:04:46 -0300 | [diff] [blame] | 242 | .atomic_check = drm_atomic_helper_check, |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 243 | .atomic_commit = exynos_atomic_commit, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | void exynos_drm_mode_config_init(struct drm_device *dev) |
| 247 | { |
| 248 | dev->mode_config.min_width = 0; |
| 249 | dev->mode_config.min_height = 0; |
| 250 | |
| 251 | /* |
| 252 | * set max width and height as default value(4096x4096). |
| 253 | * this value would be used to check framebuffer size limitation |
| 254 | * at drm_mode_addfb(). |
| 255 | */ |
| 256 | dev->mode_config.max_width = 4096; |
| 257 | dev->mode_config.max_height = 4096; |
| 258 | |
| 259 | dev->mode_config.funcs = &exynos_drm_mode_config_funcs; |
| 260 | } |