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 <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>
23
24 #include "exynos_drm_drv.h"
25 #include "exynos_drm_fb.h"
26 #include "exynos_drm_fbdev.h"
27 #include "exynos_drm_iommu.h"
28 #include "exynos_drm_crtc.h"
29
check_fb_gem_memory_type(struct drm_device * drm_dev,struct exynos_drm_gem * exynos_gem)30 static int check_fb_gem_memory_type(struct drm_device *drm_dev,
31 struct exynos_drm_gem *exynos_gem)
32 {
33 unsigned int flags;
34
35 /*
36 * if exynos drm driver supports iommu then framebuffer can use
37 * all the buffer types.
38 */
39 if (is_drm_iommu_supported(drm_dev))
40 return 0;
41
42 flags = exynos_gem->flags;
43
44 /*
45 * Physically non-contiguous memory type for framebuffer is not
46 * supported without IOMMU.
47 */
48 if (IS_NONCONTIG_BUFFER(flags)) {
49 DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
50 return -EINVAL;
51 }
52
53 return 0;
54 }
55
56 static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
57 .destroy = drm_gem_fb_destroy,
58 .create_handle = drm_gem_fb_create_handle,
59 };
60
61 struct drm_framebuffer *
exynos_drm_framebuffer_init(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct exynos_drm_gem ** exynos_gem,int count)62 exynos_drm_framebuffer_init(struct drm_device *dev,
63 const struct drm_mode_fb_cmd2 *mode_cmd,
64 struct exynos_drm_gem **exynos_gem,
65 int count)
66 {
67 struct drm_framebuffer *fb;
68 int i;
69 int ret;
70
71 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
72 if (!fb)
73 return ERR_PTR(-ENOMEM);
74
75 for (i = 0; i < count; i++) {
76 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
77 if (ret < 0)
78 goto err;
79
80 fb->obj[i] = &exynos_gem[i]->base;
81 }
82
83 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
84
85 ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
86 if (ret < 0) {
87 DRM_ERROR("failed to initialize framebuffer\n");
88 goto err;
89 }
90
91 return fb;
92
93 err:
94 kfree(fb);
95 return ERR_PTR(ret);
96 }
97
98 static struct drm_framebuffer *
exynos_user_fb_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)99 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
100 const struct drm_mode_fb_cmd2 *mode_cmd)
101 {
102 const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
103 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
104 struct drm_framebuffer *fb;
105 int i;
106 int ret;
107
108 for (i = 0; i < info->num_planes; i++) {
109 unsigned int height = (i == 0) ? mode_cmd->height :
110 DIV_ROUND_UP(mode_cmd->height, info->vsub);
111 unsigned long size = height * mode_cmd->pitches[i] +
112 mode_cmd->offsets[i];
113
114 exynos_gem[i] = exynos_drm_gem_get(file_priv,
115 mode_cmd->handles[i]);
116 if (!exynos_gem[i]) {
117 DRM_ERROR("failed to lookup gem object\n");
118 ret = -ENOENT;
119 goto err;
120 }
121
122 if (size > exynos_gem[i]->size) {
123 i++;
124 ret = -EINVAL;
125 goto err;
126 }
127 }
128
129 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
130 if (IS_ERR(fb)) {
131 ret = PTR_ERR(fb);
132 goto err;
133 }
134
135 return fb;
136
137 err:
138 while (i--)
139 exynos_drm_gem_put(exynos_gem[i]);
140
141 return ERR_PTR(ret);
142 }
143
exynos_drm_fb_dma_addr(struct drm_framebuffer * fb,int index)144 dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
145 {
146 struct exynos_drm_gem *exynos_gem;
147
148 if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
149 return 0;
150
151 exynos_gem = to_exynos_gem(fb->obj[index]);
152 return exynos_gem->dma_addr + fb->offsets[index];
153 }
154
155 static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
156 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
157 };
158
159 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
160 .fb_create = exynos_user_fb_create,
161 .output_poll_changed = drm_fb_helper_output_poll_changed,
162 .atomic_check = drm_atomic_helper_check,
163 .atomic_commit = drm_atomic_helper_commit,
164 };
165
exynos_drm_mode_config_init(struct drm_device * dev)166 void exynos_drm_mode_config_init(struct drm_device *dev)
167 {
168 dev->mode_config.min_width = 0;
169 dev->mode_config.min_height = 0;
170
171 /*
172 * set max width and height as default value(4096x4096).
173 * this value would be used to check framebuffer size limitation
174 * at drm_mode_addfb().
175 */
176 dev->mode_config.max_width = 4096;
177 dev->mode_config.max_height = 4096;
178
179 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
180 dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
181
182 dev->mode_config.allow_fb_modifiers = true;
183
184 dev->mode_config.normalize_zpos = true;
185 }
186