3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
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.
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <uapi/drm/exynos_drm.h>
24 #include "exynos_drm_drv.h"
25 #include "exynos_drm_fb.h"
26 #include "exynos_drm_fbdev.h"
27 #include "exynos_drm_crtc.h"
29 static int check_fb_gem_memory_type(struct drm_device *drm_dev,
30 struct exynos_drm_gem *exynos_gem)
35 * if exynos drm driver supports iommu then framebuffer can use
36 * all the buffer types.
38 if (is_drm_iommu_supported(drm_dev))
41 flags = exynos_gem->flags;
44 * Physically non-contiguous memory type for framebuffer is not
45 * supported without IOMMU.
47 if (IS_NONCONTIG_BUFFER(flags)) {
48 DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
55 static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
56 .destroy = drm_gem_fb_destroy,
57 .create_handle = drm_gem_fb_create_handle,
60 struct drm_framebuffer *
61 exynos_drm_framebuffer_init(struct drm_device *dev,
62 const struct drm_mode_fb_cmd2 *mode_cmd,
63 struct exynos_drm_gem **exynos_gem,
66 struct drm_framebuffer *fb;
70 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
72 return ERR_PTR(-ENOMEM);
74 for (i = 0; i < count; i++) {
75 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
79 fb->obj[i] = &exynos_gem[i]->base;
82 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
84 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
86 DRM_ERROR("failed to initialize framebuffer\n");
97 static struct drm_framebuffer *
98 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
99 const struct drm_mode_fb_cmd2 *mode_cmd)
101 const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
102 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
103 struct drm_framebuffer *fb;
107 for (i = 0; i < info->num_planes; i++) {
108 unsigned int height = (i == 0) ? mode_cmd->height :
109 DIV_ROUND_UP(mode_cmd->height, info->vsub);
110 unsigned long size = height * mode_cmd->pitches[i] +
111 mode_cmd->offsets[i];
113 exynos_gem[i] = exynos_drm_gem_get(file_priv,
114 mode_cmd->handles[i]);
115 if (!exynos_gem[i]) {
116 DRM_ERROR("failed to lookup gem object\n");
121 if (size > exynos_gem[i]->size) {
128 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
138 exynos_drm_gem_put(exynos_gem[i]);
143 dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
145 struct exynos_drm_gem *exynos_gem;
147 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
150 exynos_gem = to_exynos_gem(fb->obj[index]);
151 return exynos_gem->dma_addr + fb->offsets[index];
154 static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
155 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
158 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
159 .fb_create = exynos_user_fb_create,
160 .output_poll_changed = drm_fb_helper_output_poll_changed,
161 .atomic_check = drm_atomic_helper_check,
162 .atomic_commit = drm_atomic_helper_commit,
165 void exynos_drm_mode_config_init(struct drm_device *dev)
167 dev->mode_config.min_width = 0;
168 dev->mode_config.min_height = 0;
171 * set max width and height as default value(4096x4096).
172 * this value would be used to check framebuffer size limitation
173 * at drm_mode_addfb().
175 dev->mode_config.max_width = 4096;
176 dev->mode_config.max_height = 4096;
178 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
179 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
181 dev->mode_config.allow_fb_modifiers = true;
183 dev->mode_config.normalize_zpos = true;