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