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 * 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. 13 */ 14 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> 19 #include <uapi/drm/exynos_drm.h> 20 21 #include "exynos_drm_drv.h" 22 #include "exynos_drm_fb.h" 23 #include "exynos_drm_fbdev.h" 24 #include "exynos_drm_gem.h" 25 #include "exynos_drm_iommu.h" 26 #include "exynos_drm_crtc.h" 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. 34 * @buf_cnt: a buffer count to drm framebuffer. 35 * @exynos_gem_obj: array of exynos specific gem object containing a gem object. 36 */ 37 struct exynos_drm_fb { 38 struct drm_framebuffer fb; 39 unsigned int buf_cnt; 40 struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER]; 41 }; 42 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 69 static void exynos_drm_fb_destroy(struct drm_framebuffer *fb) 70 { 71 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); 72 unsigned int i; 73 74 /* make sure that overlay data are updated before relesing fb. */ 75 exynos_drm_crtc_complete_scanout(fb); 76 77 drm_framebuffer_cleanup(fb); 78 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 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 99 /* This fb should have only one gem object. */ 100 if (WARN_ON(exynos_fb->buf_cnt != 1)) 101 return -EINVAL; 102 103 return drm_gem_handle_create(file_priv, 104 &exynos_fb->exynos_gem_obj[0]->base, handle); 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 { 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 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 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) 146 { 147 struct exynos_drm_fb *exynos_fb; 148 struct exynos_drm_gem_obj *exynos_gem_obj; 149 int ret; 150 151 exynos_gem_obj = to_exynos_gem_obj(obj); 152 153 ret = check_fb_gem_memory_type(dev, exynos_gem_obj); 154 if (ret < 0) { 155 DRM_ERROR("cannot use this gem memory type for fb.\n"); 156 return ERR_PTR(-EINVAL); 157 } 158 159 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); 160 if (!exynos_fb) 161 return ERR_PTR(-ENOMEM); 162 163 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); 164 exynos_fb->exynos_gem_obj[0] = exynos_gem_obj; 165 166 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); 167 if (ret) { 168 kfree(exynos_fb); 169 DRM_ERROR("failed to initialize framebuffer\n"); 170 return ERR_PTR(ret); 171 } 172 173 return &exynos_fb->fb; 174 } 175 176 static u32 exynos_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd) 177 { 178 unsigned int cnt = 0; 179 180 if (mode_cmd->pixel_format != DRM_FORMAT_NV12) 181 return drm_format_num_planes(mode_cmd->pixel_format); 182 183 while (cnt != MAX_FB_BUFFER) { 184 if (!mode_cmd->handles[cnt]) 185 break; 186 cnt++; 187 } 188 189 /* 190 * check if NV12 or NV12M. 191 * 192 * NV12 193 * handles[0] = base1, offsets[0] = 0 194 * handles[1] = base1, offsets[1] = Y_size 195 * 196 * NV12M 197 * handles[0] = base1, offsets[0] = 0 198 * handles[1] = base2, offsets[1] = 0 199 */ 200 if (cnt == 2) { 201 /* 202 * in case of NV12 format, offsets[1] is not 0 and 203 * handles[0] is same as handles[1]. 204 */ 205 if (mode_cmd->offsets[1] && 206 mode_cmd->handles[0] == mode_cmd->handles[1]) 207 cnt = 1; 208 } 209 210 return cnt; 211 } 212 213 static struct drm_framebuffer * 214 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, 215 struct drm_mode_fb_cmd2 *mode_cmd) 216 { 217 struct drm_gem_object *obj; 218 struct exynos_drm_gem_obj *exynos_gem_obj; 219 struct exynos_drm_fb *exynos_fb; 220 int i, ret; 221 222 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); 223 if (!exynos_fb) 224 return ERR_PTR(-ENOMEM); 225 226 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]); 227 if (!obj) { 228 DRM_ERROR("failed to lookup gem object\n"); 229 ret = -ENOENT; 230 goto err_free; 231 } 232 233 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); 234 exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj); 235 exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd); 236 237 DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt); 238 239 for (i = 1; i < exynos_fb->buf_cnt; i++) { 240 obj = drm_gem_object_lookup(dev, file_priv, 241 mode_cmd->handles[i]); 242 if (!obj) { 243 DRM_ERROR("failed to lookup gem object\n"); 244 ret = -ENOENT; 245 exynos_fb->buf_cnt = i; 246 goto err_unreference; 247 } 248 249 exynos_gem_obj = to_exynos_gem_obj(obj); 250 exynos_fb->exynos_gem_obj[i] = exynos_gem_obj; 251 252 ret = check_fb_gem_memory_type(dev, exynos_gem_obj); 253 if (ret < 0) { 254 DRM_ERROR("cannot use this gem memory type for fb.\n"); 255 goto err_unreference; 256 } 257 } 258 259 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); 260 if (ret) { 261 DRM_ERROR("failed to init framebuffer.\n"); 262 goto err_unreference; 263 } 264 265 return &exynos_fb->fb; 266 267 err_unreference: 268 for (i = 0; i < exynos_fb->buf_cnt; i++) { 269 struct drm_gem_object *obj; 270 271 obj = &exynos_fb->exynos_gem_obj[i]->base; 272 if (obj) 273 drm_gem_object_unreference_unlocked(obj); 274 } 275 err_free: 276 kfree(exynos_fb); 277 return ERR_PTR(ret); 278 } 279 280 struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb, 281 int index) 282 { 283 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); 284 struct exynos_drm_gem_buf *buffer; 285 286 if (index >= MAX_FB_BUFFER) 287 return NULL; 288 289 buffer = exynos_fb->exynos_gem_obj[index]->buffer; 290 if (!buffer) 291 return NULL; 292 293 DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr); 294 295 return buffer; 296 } 297 298 static void exynos_drm_output_poll_changed(struct drm_device *dev) 299 { 300 struct exynos_drm_private *private = dev->dev_private; 301 struct drm_fb_helper *fb_helper = private->fb_helper; 302 303 if (fb_helper) 304 drm_fb_helper_hotplug_event(fb_helper); 305 else 306 exynos_drm_fbdev_init(dev); 307 } 308 309 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { 310 .fb_create = exynos_user_fb_create, 311 .output_poll_changed = exynos_drm_output_poll_changed, 312 }; 313 314 void exynos_drm_mode_config_init(struct drm_device *dev) 315 { 316 dev->mode_config.min_width = 0; 317 dev->mode_config.min_height = 0; 318 319 /* 320 * set max width and height as default value(4096x4096). 321 * this value would be used to check framebuffer size limitation 322 * at drm_mode_addfb(). 323 */ 324 dev->mode_config.max_width = 4096; 325 dev->mode_config.max_height = 4096; 326 327 dev->mode_config.funcs = &exynos_drm_mode_config_funcs; 328 } 329