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> |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 19 | #include <uapi/drm/exynos_drm.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 20 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 21 | #include "exynos_drm_drv.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 22 | #include "exynos_drm_fb.h" |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 23 | #include "exynos_drm_fbdev.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 24 | #include "exynos_drm_gem.h" |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 25 | #include "exynos_drm_iommu.h" |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 26 | #include "exynos_drm_crtc.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 27 | |
| 28 | #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb) |
| 29 | |
| 30 | /* |
| 31 | * exynos specific framebuffer structure. |
| 32 | * |
| 33 | * @fb: drm framebuffer obejct. |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 34 | * @buf_cnt: a buffer count to drm framebuffer. |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 35 | * @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] | 36 | */ |
| 37 | struct exynos_drm_fb { |
| 38 | struct drm_framebuffer fb; |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 39 | unsigned int buf_cnt; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 40 | struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER]; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 41 | }; |
| 42 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 43 | static int check_fb_gem_memory_type(struct drm_device *drm_dev, |
| 44 | struct exynos_drm_gem_obj *exynos_gem_obj) |
| 45 | { |
| 46 | unsigned int flags; |
| 47 | |
| 48 | /* |
| 49 | * if exynos drm driver supports iommu then framebuffer can use |
| 50 | * all the buffer types. |
| 51 | */ |
| 52 | if (is_drm_iommu_supported(drm_dev)) |
| 53 | return 0; |
| 54 | |
| 55 | flags = exynos_gem_obj->flags; |
| 56 | |
| 57 | /* |
| 58 | * without iommu support, not support physically non-continuous memory |
| 59 | * for framebuffer. |
| 60 | */ |
| 61 | if (IS_NONCONTIG_BUFFER(flags)) { |
| 62 | DRM_ERROR("cannot use this gem memory type for fb.\n"); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 69 | static void exynos_drm_fb_destroy(struct drm_framebuffer *fb) |
| 70 | { |
| 71 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 72 | unsigned int i; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 73 | |
Inki Dae | 1daa892 | 2012-11-22 17:41:23 +0900 | [diff] [blame] | 74 | /* make sure that overlay data are updated before relesing fb. */ |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 75 | exynos_drm_crtc_complete_scanout(fb); |
Inki Dae | 1daa892 | 2012-11-22 17:41:23 +0900 | [diff] [blame] | 76 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 77 | drm_framebuffer_cleanup(fb); |
| 78 | |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 79 | for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) { |
| 80 | struct drm_gem_object *obj; |
| 81 | |
| 82 | if (exynos_fb->exynos_gem_obj[i] == NULL) |
| 83 | continue; |
| 84 | |
| 85 | obj = &exynos_fb->exynos_gem_obj[i]->base; |
| 86 | drm_gem_object_unreference_unlocked(obj); |
| 87 | } |
| 88 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 89 | kfree(exynos_fb); |
| 90 | exynos_fb = NULL; |
| 91 | } |
| 92 | |
| 93 | static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb, |
| 94 | struct drm_file *file_priv, |
| 95 | unsigned int *handle) |
| 96 | { |
| 97 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
| 98 | |
Inki Dae | b9ede27 | 2013-01-29 17:51:09 +0900 | [diff] [blame] | 99 | /* This fb should have only one gem object. */ |
| 100 | if (WARN_ON(exynos_fb->buf_cnt != 1)) |
| 101 | return -EINVAL; |
| 102 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 103 | return drm_gem_handle_create(file_priv, |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 104 | &exynos_fb->exynos_gem_obj[0]->base, handle); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int exynos_drm_fb_dirty(struct drm_framebuffer *fb, |
| 108 | struct drm_file *file_priv, unsigned flags, |
| 109 | unsigned color, struct drm_clip_rect *clips, |
| 110 | unsigned num_clips) |
| 111 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 112 | /* TODO */ |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static struct drm_framebuffer_funcs exynos_drm_fb_funcs = { |
| 118 | .destroy = exynos_drm_fb_destroy, |
| 119 | .create_handle = exynos_drm_fb_create_handle, |
| 120 | .dirty = exynos_drm_fb_dirty, |
| 121 | }; |
| 122 | |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 123 | void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb, |
| 124 | unsigned int cnt) |
| 125 | { |
| 126 | struct exynos_drm_fb *exynos_fb; |
| 127 | |
| 128 | exynos_fb = to_exynos_fb(fb); |
| 129 | |
| 130 | exynos_fb->buf_cnt = cnt; |
| 131 | } |
| 132 | |
| 133 | unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb) |
| 134 | { |
| 135 | struct exynos_drm_fb *exynos_fb; |
| 136 | |
| 137 | exynos_fb = to_exynos_fb(fb); |
| 138 | |
| 139 | return exynos_fb->buf_cnt; |
| 140 | } |
| 141 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 142 | struct drm_framebuffer * |
| 143 | exynos_drm_framebuffer_init(struct drm_device *dev, |
| 144 | struct drm_mode_fb_cmd2 *mode_cmd, |
| 145 | struct drm_gem_object *obj) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 146 | { |
| 147 | struct exynos_drm_fb *exynos_fb; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 148 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 149 | int ret; |
| 150 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 151 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 152 | |
| 153 | ret = check_fb_gem_memory_type(dev, exynos_gem_obj); |
Tobias Jakobi | 7ded858 | 2015-04-07 01:14:51 +0200 | [diff] [blame] | 154 | if (ret < 0) |
| 155 | return ERR_PTR(ret); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 156 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 157 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
Sachin Kamat | 38bb525 | 2013-08-19 19:04:55 +0900 | [diff] [blame] | 158 | if (!exynos_fb) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 159 | return ERR_PTR(-ENOMEM); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 160 | |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 161 | drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 162 | exynos_fb->exynos_gem_obj[0] = exynos_gem_obj; |
| 163 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 164 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 165 | if (ret) { |
Andrzej Hajda | 849b431 | 2014-09-09 15:16:08 +0200 | [diff] [blame] | 166 | kfree(exynos_fb); |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 167 | DRM_ERROR("failed to initialize framebuffer\n"); |
| 168 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 171 | return &exynos_fb->fb; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 174 | static struct drm_framebuffer * |
| 175 | exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
| 176 | struct drm_mode_fb_cmd2 *mode_cmd) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 177 | { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 178 | struct drm_gem_object *obj; |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 179 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 180 | struct exynos_drm_fb *exynos_fb; |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 181 | int i, ret; |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 182 | |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 183 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
Sachin Kamat | 38bb525 | 2013-08-19 19:04:55 +0900 | [diff] [blame] | 184 | if (!exynos_fb) |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 185 | return ERR_PTR(-ENOMEM); |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 186 | |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 187 | obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]); |
| 188 | if (!obj) { |
| 189 | DRM_ERROR("failed to lookup gem object\n"); |
| 190 | ret = -ENOENT; |
| 191 | goto err_free; |
| 192 | } |
| 193 | |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 194 | drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); |
| 195 | exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj); |
Tobias Jakobi | d10ebb9 | 2015-04-27 23:10:13 +0200 | [diff] [blame] | 196 | exynos_fb->buf_cnt = drm_format_num_planes(mode_cmd->pixel_format); |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 197 | |
Inki Dae | 01ed812 | 2012-08-20 20:05:56 +0900 | [diff] [blame] | 198 | DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt); |
| 199 | |
| 200 | for (i = 1; i < exynos_fb->buf_cnt; i++) { |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 201 | obj = drm_gem_object_lookup(dev, file_priv, |
| 202 | mode_cmd->handles[i]); |
| 203 | if (!obj) { |
| 204 | DRM_ERROR("failed to lookup gem object\n"); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 205 | ret = -ENOENT; |
| 206 | exynos_fb->buf_cnt = i; |
| 207 | goto err_unreference; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 210 | exynos_gem_obj = to_exynos_gem_obj(obj); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 211 | exynos_fb->exynos_gem_obj[i] = exynos_gem_obj; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 212 | |
| 213 | ret = check_fb_gem_memory_type(dev, exynos_gem_obj); |
Tobias Jakobi | 7ded858 | 2015-04-07 01:14:51 +0200 | [diff] [blame] | 214 | if (ret < 0) |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 215 | goto err_unreference; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 216 | } |
| 217 | |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 218 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
| 219 | if (ret) { |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 220 | DRM_ERROR("failed to init framebuffer.\n"); |
| 221 | goto err_unreference; |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return &exynos_fb->fb; |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 225 | |
| 226 | err_unreference: |
| 227 | for (i = 0; i < exynos_fb->buf_cnt; i++) { |
| 228 | struct drm_gem_object *obj; |
| 229 | |
| 230 | obj = &exynos_fb->exynos_gem_obj[i]->base; |
| 231 | if (obj) |
| 232 | drm_gem_object_unreference_unlocked(obj); |
| 233 | } |
| 234 | err_free: |
| 235 | kfree(exynos_fb); |
| 236 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 237 | } |
| 238 | |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 239 | struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb, |
| 240 | int index) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 241 | { |
| 242 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 243 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 244 | |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 245 | if (index >= MAX_FB_BUFFER) |
| 246 | return NULL; |
| 247 | |
| 248 | buffer = exynos_fb->exynos_gem_obj[index]->buffer; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 249 | if (!buffer) |
Inki Dae | 19c8b83 | 2011-10-14 13:29:46 +0900 | [diff] [blame] | 250 | return NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 251 | |
Inki Dae | 4744ad2 | 2012-12-07 17:51:27 +0900 | [diff] [blame] | 252 | DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 253 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 254 | return buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 255 | } |
| 256 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 257 | static void exynos_drm_output_poll_changed(struct drm_device *dev) |
| 258 | { |
| 259 | struct exynos_drm_private *private = dev->dev_private; |
| 260 | struct drm_fb_helper *fb_helper = private->fb_helper; |
| 261 | |
| 262 | if (fb_helper) |
| 263 | drm_fb_helper_hotplug_event(fb_helper); |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 264 | else |
| 265 | exynos_drm_fbdev_init(dev); |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 266 | } |
| 267 | |
Laurent Pinchart | e6ecefa | 2012-05-17 13:27:23 +0200 | [diff] [blame] | 268 | static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 269 | .fb_create = exynos_user_fb_create, |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 270 | .output_poll_changed = exynos_drm_output_poll_changed, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | void exynos_drm_mode_config_init(struct drm_device *dev) |
| 274 | { |
| 275 | dev->mode_config.min_width = 0; |
| 276 | dev->mode_config.min_height = 0; |
| 277 | |
| 278 | /* |
| 279 | * set max width and height as default value(4096x4096). |
| 280 | * this value would be used to check framebuffer size limitation |
| 281 | * at drm_mode_addfb(). |
| 282 | */ |
| 283 | dev->mode_config.max_width = 4096; |
| 284 | dev->mode_config.max_height = 4096; |
| 285 | |
| 286 | dev->mode_config.funcs = &exynos_drm_mode_config_funcs; |
| 287 | } |