CHROMIUM: drm/exynos: fb: Cleanup exynos_drm_fb_init
[cascardo/linux.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  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include "drmP.h"
30 #include "drm_crtc.h"
31 #include "drm_crtc_helper.h"
32 #include "drm_fb_helper.h"
33
34 #include "exynos_drm_drv.h"
35 #include "exynos_drm_fb.h"
36 #include "exynos_drm_gem.h"
37 #include "exynos_drm_crtc.h"
38
39 /* Helper functions to push things in/out of the iommu. */
40 static int exynos_drm_fb_map(struct exynos_drm_fb *exynos_fb)
41 {
42         struct exynos_drm_gem_obj *gem_ob = exynos_fb->exynos_gem_obj[0];
43         struct drm_gem_object *obj = &gem_ob->base;
44         struct exynos_drm_gem_buf *buf;
45         int ret;
46
47         buf = exynos_drm_fb_buffer(exynos_fb, 0);
48         if (!buf) {
49                 DRM_ERROR("buffer is null\n");
50                 return -ENOMEM;
51         }
52
53         drm_gem_object_reference(obj);
54
55         ret = dma_map_sg(obj->dev->dev,
56                          buf->sgt->sgl,
57                          buf->sgt->orig_nents,
58                          DMA_BIDIRECTIONAL);
59         if (!ret) {
60                 DRM_ERROR("failed to map sg\n");
61                 return -ENOMEM;
62         }
63
64         buf->dma_addr = buf->sgt->sgl->dma_address;
65
66         return 0;
67 }
68
69 static int exynos_drm_fb_unmap(struct exynos_drm_fb *exynos_fb)
70 {
71         struct exynos_drm_gem_obj *gem_ob = exynos_fb->exynos_gem_obj[0];
72         struct drm_gem_object *obj = &gem_ob->base;
73         struct exynos_drm_gem_buf *buf;
74
75         buf = exynos_drm_fb_buffer(exynos_fb, 0);
76         if (!buf) {
77                 DRM_ERROR("buffer is null\n");
78                 return -ENOMEM;
79         }
80
81         /*
82          * Not critical, this is used for cleanup in the fb_create error path
83          * path so keep it silent.
84          */
85         if (!buf->dma_addr)
86                 return -ENOMEM;
87
88         buf->dma_addr = 0;
89
90         /* Unmap the SGT to remove the IOMMU mapping created for this buffer */
91         dma_unmap_sg(obj->dev->dev,
92                      buf->sgt->sgl,
93                      buf->sgt->orig_nents,
94                      DMA_BIDIRECTIONAL);
95
96         drm_gem_object_unreference_unlocked(obj);
97
98         return 0;
99 }
100
101 void exynos_drm_fb_release(struct kref *kref)
102 {
103         struct exynos_drm_fb *exynos_fb;
104
105         exynos_fb = container_of(kref, struct exynos_drm_fb, refcount);
106         schedule_work(&exynos_fb->release_work);
107 }
108
109 static void exynos_drm_fb_release_work_fn(struct work_struct *work)
110 {
111         struct drm_framebuffer *fb;
112         struct exynos_drm_fb *exynos_fb;
113         int i, nr;
114
115         DRM_DEBUG_KMS("%s\n", __FILE__);
116
117         exynos_fb = container_of(work, struct exynos_drm_fb, release_work);
118         fb = &exynos_fb->fb;
119
120 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
121         if (exynos_fb->dma_buf)
122                 dma_buf_put(exynos_fb->dma_buf);
123 #endif
124
125         drm_framebuffer_cleanup(fb);
126
127         if (exynos_drm_fb_unmap(exynos_fb))
128                 DRM_ERROR("Couldn't unmap buffer\n");
129
130         nr = exynos_drm_format_num_buffers(fb->pixel_format);
131
132         for (i = 0; i < nr; i++) {
133                 struct drm_gem_object *obj;
134
135                 obj = &exynos_fb->exynos_gem_obj[i]->base;
136                 drm_gem_object_unreference_unlocked(obj);
137         }
138
139         kfree(exynos_fb);
140 }
141
142 static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
143 {
144         exynos_drm_fb_put(to_exynos_fb(fb));
145 }
146
147 static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
148                                         struct drm_file *file_priv,
149                                         unsigned int *handle)
150 {
151         struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
152
153         DRM_DEBUG_KMS("%s\n", __FILE__);
154
155         return drm_gem_handle_create(file_priv,
156                         &exynos_fb->exynos_gem_obj[0]->base, handle);
157 }
158
159 static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
160                                 struct drm_file *file_priv, unsigned flags,
161                                 unsigned color, struct drm_clip_rect *clips,
162                                 unsigned num_clips)
163 {
164         DRM_DEBUG_KMS("%s\n", __FILE__);
165
166         /* TODO */
167
168         return 0;
169 }
170
171 static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
172         .destroy        = exynos_drm_fb_destroy,
173         .create_handle  = exynos_drm_fb_create_handle,
174         .dirty          = exynos_drm_fb_dirty,
175 };
176
177 struct exynos_drm_fb *
178 exynos_drm_fb_init(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd)
179 {
180         struct exynos_drm_fb *exynos_fb;
181         int ret;
182
183         exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
184         if (!exynos_fb) {
185                 DRM_ERROR("failed to allocate exynos drm framebuffer\n");
186                 return ERR_PTR(-ENOMEM);
187         }
188
189         kref_init(&exynos_fb->refcount);
190         INIT_WORK(&exynos_fb->release_work, exynos_drm_fb_release_work_fn);
191
192         ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
193         if (ret) {
194                 DRM_ERROR("failed to initialize framebuffer\n");
195                 goto err_init;
196         }
197
198         drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
199
200         return exynos_fb;
201
202 err_init:
203         kfree(exynos_fb);
204         return ERR_PTR(ret);
205 }
206
207 static struct drm_framebuffer *
208 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
209                       struct drm_mode_fb_cmd2 *mode_cmd)
210 {
211         struct drm_gem_object *obj;
212         struct exynos_drm_fb *exynos_fb;
213         int i, nr, ret = 0;
214
215         DRM_DEBUG_KMS("%s\n", __FILE__);
216
217         exynos_fb = exynos_drm_fb_init(dev, mode_cmd);
218         if (IS_ERR(exynos_fb))
219                 return ERR_CAST(exynos_fb);
220
221         nr = exynos_drm_format_num_buffers(mode_cmd->pixel_format);
222         for (i = 0; i < nr; i++) {
223                 obj = drm_gem_object_lookup(dev, file_priv,
224                                 mode_cmd->handles[i]);
225                 if (!obj) {
226                         DRM_ERROR("failed to lookup gem object\n");
227                         ret = -ENOENT;
228                         goto err_lookup;
229                 }
230
231                 /*
232                  * We keep the reference which came from drm_gem_object_lookup.
233                  * It is used to ensure the bo's lifetime is >= the framebuffer.
234                  */
235
236                 exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
237         }
238
239         if (exynos_drm_fb_map(exynos_fb)) {
240                 DRM_ERROR("Failed to map gem object\n");
241                 ret = -ENOMEM;
242                 goto err_map;
243         }
244
245         return &exynos_fb->fb;
246
247 err_map:
248 err_lookup:
249         while (--i >= 0) {
250                 obj = &exynos_fb->exynos_gem_obj[i]->base;
251                 drm_gem_object_unreference_unlocked(obj);
252         }
253         kfree(exynos_fb);
254         return ERR_PTR(ret);
255 }
256
257 struct exynos_drm_gem_buf *
258 exynos_drm_fb_buffer(struct exynos_drm_fb *exynos_fb, int index)
259 {
260         struct exynos_drm_gem_buf *buffer;
261
262         DRM_DEBUG_KMS("%s\n", __FILE__);
263
264         if (index >= MAX_FB_BUFFER)
265                 return NULL;
266
267         buffer = exynos_fb->exynos_gem_obj[index]->buffer;
268         if (!buffer)
269                 return NULL;
270
271         DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
272                         (unsigned long)buffer->kvaddr,
273                         (unsigned long)buffer->dma_addr);
274
275         return buffer;
276 }
277
278 static void exynos_drm_output_poll_changed(struct drm_device *dev)
279 {
280         struct exynos_drm_private *private = dev->dev_private;
281         struct drm_fb_helper *fb_helper = private->fb_helper;
282
283         if (fb_helper)
284                 drm_fb_helper_hotplug_event(fb_helper);
285 }
286
287 static struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
288         .fb_create = exynos_user_fb_create,
289         .output_poll_changed = exynos_drm_output_poll_changed,
290 };
291
292 void exynos_drm_mode_config_init(struct drm_device *dev)
293 {
294         dev->mode_config.min_width = 0;
295         dev->mode_config.min_height = 0;
296
297         /*
298          * set max width and height as default value(4096x4096).
299          * this value would be used to check framebuffer size limitation
300          * at drm_mode_addfb().
301          */
302         dev->mode_config.max_width = 4096;
303         dev->mode_config.max_height = 4096;
304
305         dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
306 }