Commit | Line | Data |
---|---|---|
1c248b7d ID |
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 | * | |
d81aecb5 ID |
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. | |
1c248b7d ID |
13 | */ |
14 | ||
760285e7 DH |
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> | |
0519f9a1 | 19 | #include <uapi/drm/exynos_drm.h> |
1c248b7d | 20 | |
7db3eba6 | 21 | #include "exynos_drm_drv.h" |
1c248b7d | 22 | #include "exynos_drm_fb.h" |
1c248b7d | 23 | #include "exynos_drm_gem.h" |
0519f9a1 | 24 | #include "exynos_drm_iommu.h" |
1daa892c | 25 | #include "exynos_drm_encoder.h" |
1c248b7d ID |
26 | |
27 | #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb) | |
28 | ||
29 | /* | |
30 | * exynos specific framebuffer structure. | |
31 | * | |
32 | * @fb: drm framebuffer obejct. | |
01ed8126 | 33 | * @buf_cnt: a buffer count to drm framebuffer. |
229d3534 | 34 | * @exynos_gem_obj: array of exynos specific gem object containing a gem object. |
1c248b7d ID |
35 | */ |
36 | struct exynos_drm_fb { | |
37 | struct drm_framebuffer fb; | |
01ed8126 | 38 | unsigned int buf_cnt; |
229d3534 | 39 | struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER]; |
1c248b7d ID |
40 | }; |
41 | ||
0519f9a1 ID |
42 | static int check_fb_gem_memory_type(struct drm_device *drm_dev, |
43 | struct exynos_drm_gem_obj *exynos_gem_obj) | |
44 | { | |
45 | unsigned int flags; | |
46 | ||
47 | /* | |
48 | * if exynos drm driver supports iommu then framebuffer can use | |
49 | * all the buffer types. | |
50 | */ | |
51 | if (is_drm_iommu_supported(drm_dev)) | |
52 | return 0; | |
53 | ||
54 | flags = exynos_gem_obj->flags; | |
55 | ||
56 | /* | |
57 | * without iommu support, not support physically non-continuous memory | |
58 | * for framebuffer. | |
59 | */ | |
60 | if (IS_NONCONTIG_BUFFER(flags)) { | |
61 | DRM_ERROR("cannot use this gem memory type for fb.\n"); | |
62 | return -EINVAL; | |
63 | } | |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
1c248b7d ID |
68 | static void exynos_drm_fb_destroy(struct drm_framebuffer *fb) |
69 | { | |
70 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); | |
07b6835f | 71 | unsigned int i; |
1c248b7d | 72 | |
1daa892c ID |
73 | /* make sure that overlay data are updated before relesing fb. */ |
74 | exynos_drm_encoder_complete_scanout(fb); | |
75 | ||
1c248b7d ID |
76 | drm_framebuffer_cleanup(fb); |
77 | ||
07b6835f LP |
78 | for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) { |
79 | struct drm_gem_object *obj; | |
80 | ||
81 | if (exynos_fb->exynos_gem_obj[i] == NULL) | |
82 | continue; | |
83 | ||
84 | obj = &exynos_fb->exynos_gem_obj[i]->base; | |
85 | drm_gem_object_unreference_unlocked(obj); | |
86 | } | |
87 | ||
1c248b7d ID |
88 | kfree(exynos_fb); |
89 | exynos_fb = NULL; | |
90 | } | |
91 | ||
92 | static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb, | |
93 | struct drm_file *file_priv, | |
94 | unsigned int *handle) | |
95 | { | |
96 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); | |
97 | ||
b9ede277 ID |
98 | /* This fb should have only one gem object. */ |
99 | if (WARN_ON(exynos_fb->buf_cnt != 1)) | |
100 | return -EINVAL; | |
101 | ||
1c248b7d | 102 | return drm_gem_handle_create(file_priv, |
229d3534 | 103 | &exynos_fb->exynos_gem_obj[0]->base, handle); |
1c248b7d ID |
104 | } |
105 | ||
106 | static int exynos_drm_fb_dirty(struct drm_framebuffer *fb, | |
107 | struct drm_file *file_priv, unsigned flags, | |
108 | unsigned color, struct drm_clip_rect *clips, | |
109 | unsigned num_clips) | |
110 | { | |
1c248b7d ID |
111 | /* TODO */ |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | static struct drm_framebuffer_funcs exynos_drm_fb_funcs = { | |
117 | .destroy = exynos_drm_fb_destroy, | |
118 | .create_handle = exynos_drm_fb_create_handle, | |
119 | .dirty = exynos_drm_fb_dirty, | |
120 | }; | |
121 | ||
01ed8126 ID |
122 | void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb, |
123 | unsigned int cnt) | |
124 | { | |
125 | struct exynos_drm_fb *exynos_fb; | |
126 | ||
127 | exynos_fb = to_exynos_fb(fb); | |
128 | ||
129 | exynos_fb->buf_cnt = cnt; | |
130 | } | |
131 | ||
132 | unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb) | |
133 | { | |
134 | struct exynos_drm_fb *exynos_fb; | |
135 | ||
136 | exynos_fb = to_exynos_fb(fb); | |
137 | ||
138 | return exynos_fb->buf_cnt; | |
139 | } | |
140 | ||
e1533c08 JS |
141 | struct drm_framebuffer * |
142 | exynos_drm_framebuffer_init(struct drm_device *dev, | |
143 | struct drm_mode_fb_cmd2 *mode_cmd, | |
144 | struct drm_gem_object *obj) | |
1c248b7d ID |
145 | { |
146 | struct exynos_drm_fb *exynos_fb; | |
0519f9a1 | 147 | struct exynos_drm_gem_obj *exynos_gem_obj; |
1c248b7d ID |
148 | int ret; |
149 | ||
0519f9a1 ID |
150 | exynos_gem_obj = to_exynos_gem_obj(obj); |
151 | ||
152 | ret = check_fb_gem_memory_type(dev, exynos_gem_obj); | |
153 | if (ret < 0) { | |
154 | DRM_ERROR("cannot use this gem memory type for fb.\n"); | |
155 | return ERR_PTR(-EINVAL); | |
156 | } | |
157 | ||
1c248b7d ID |
158 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
159 | if (!exynos_fb) { | |
e1533c08 | 160 | DRM_ERROR("failed to allocate exynos drm framebuffer\n"); |
1c248b7d ID |
161 | return ERR_PTR(-ENOMEM); |
162 | } | |
163 | ||
f2c0095a | 164 | drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); |
0519f9a1 ID |
165 | exynos_fb->exynos_gem_obj[0] = exynos_gem_obj; |
166 | ||
e1533c08 | 167 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
1c248b7d | 168 | if (ret) { |
e1533c08 JS |
169 | DRM_ERROR("failed to initialize framebuffer\n"); |
170 | return ERR_PTR(ret); | |
1c248b7d ID |
171 | } |
172 | ||
e1533c08 | 173 | return &exynos_fb->fb; |
1c248b7d ID |
174 | } |
175 | ||
01ed8126 ID |
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 | ||
e1533c08 JS |
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) | |
1c248b7d | 216 | { |
e1533c08 | 217 | struct drm_gem_object *obj; |
979c0c7e | 218 | struct exynos_drm_gem_obj *exynos_gem_obj; |
229d3534 | 219 | struct exynos_drm_fb *exynos_fb; |
f2c0095a | 220 | int i, ret; |
e1533c08 | 221 | |
f2c0095a DV |
222 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
223 | if (!exynos_fb) { | |
224 | DRM_ERROR("failed to allocate exynos drm framebuffer\n"); | |
225 | return ERR_PTR(-ENOMEM); | |
07b6835f | 226 | } |
229d3534 | 227 | |
979c0c7e YC |
228 | obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]); |
229 | if (!obj) { | |
230 | DRM_ERROR("failed to lookup gem object\n"); | |
231 | ret = -ENOENT; | |
232 | goto err_free; | |
233 | } | |
234 | ||
f2c0095a DV |
235 | drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); |
236 | exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj); | |
01ed8126 ID |
237 | exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd); |
238 | ||
239 | DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt); | |
229d3534 | 240 | |
01ed8126 | 241 | for (i = 1; i < exynos_fb->buf_cnt; i++) { |
229d3534 SWK |
242 | obj = drm_gem_object_lookup(dev, file_priv, |
243 | mode_cmd->handles[i]); | |
244 | if (!obj) { | |
245 | DRM_ERROR("failed to lookup gem object\n"); | |
979c0c7e YC |
246 | ret = -ENOENT; |
247 | exynos_fb->buf_cnt = i; | |
248 | goto err_unreference; | |
229d3534 SWK |
249 | } |
250 | ||
0519f9a1 | 251 | exynos_gem_obj = to_exynos_gem_obj(obj); |
979c0c7e | 252 | exynos_fb->exynos_gem_obj[i] = exynos_gem_obj; |
0519f9a1 ID |
253 | |
254 | ret = check_fb_gem_memory_type(dev, exynos_gem_obj); | |
255 | if (ret < 0) { | |
256 | DRM_ERROR("cannot use this gem memory type for fb.\n"); | |
979c0c7e | 257 | goto err_unreference; |
0519f9a1 | 258 | } |
229d3534 SWK |
259 | } |
260 | ||
f2c0095a DV |
261 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
262 | if (ret) { | |
979c0c7e YC |
263 | DRM_ERROR("failed to init framebuffer.\n"); |
264 | goto err_unreference; | |
f2c0095a DV |
265 | } |
266 | ||
267 | return &exynos_fb->fb; | |
979c0c7e YC |
268 | |
269 | err_unreference: | |
270 | for (i = 0; i < exynos_fb->buf_cnt; i++) { | |
271 | struct drm_gem_object *obj; | |
272 | ||
273 | obj = &exynos_fb->exynos_gem_obj[i]->base; | |
274 | if (obj) | |
275 | drm_gem_object_unreference_unlocked(obj); | |
276 | } | |
277 | err_free: | |
278 | kfree(exynos_fb); | |
279 | return ERR_PTR(ret); | |
1c248b7d ID |
280 | } |
281 | ||
229d3534 SWK |
282 | struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb, |
283 | int index) | |
1c248b7d ID |
284 | { |
285 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); | |
2c871127 | 286 | struct exynos_drm_gem_buf *buffer; |
1c248b7d | 287 | |
229d3534 SWK |
288 | if (index >= MAX_FB_BUFFER) |
289 | return NULL; | |
290 | ||
291 | buffer = exynos_fb->exynos_gem_obj[index]->buffer; | |
2c871127 | 292 | if (!buffer) |
19c8b834 | 293 | return NULL; |
1c248b7d | 294 | |
4744ad24 | 295 | DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr); |
1c248b7d | 296 | |
2c871127 | 297 | return buffer; |
1c248b7d ID |
298 | } |
299 | ||
7db3eba6 SWK |
300 | static void exynos_drm_output_poll_changed(struct drm_device *dev) |
301 | { | |
302 | struct exynos_drm_private *private = dev->dev_private; | |
303 | struct drm_fb_helper *fb_helper = private->fb_helper; | |
304 | ||
305 | if (fb_helper) | |
306 | drm_fb_helper_hotplug_event(fb_helper); | |
307 | } | |
308 | ||
e6ecefaa | 309 | static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { |
e1533c08 | 310 | .fb_create = exynos_user_fb_create, |
7db3eba6 | 311 | .output_poll_changed = exynos_drm_output_poll_changed, |
1c248b7d ID |
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 | } |