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