3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
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:
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
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.
31 #include "drm_crtc_helper.h"
32 #include "drm_fb_helper.h"
34 #include "exynos_drm_drv.h"
35 #include "exynos_drm_fb.h"
36 #include "exynos_drm_gem.h"
37 #include "exynos_drm_crtc.h"
39 /* Helper functions to push things in/out of the iommu. */
40 static int exynos_drm_fb_map(struct exynos_drm_fb *exynos_fb)
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;
47 buf = exynos_drm_fb_buffer(exynos_fb, 0);
49 DRM_ERROR("buffer is null\n");
53 drm_gem_object_reference(obj);
55 ret = dma_map_sg(obj->dev->dev,
60 DRM_ERROR("failed to map sg\n");
64 buf->dma_addr = buf->sgt->sgl->dma_address;
69 static int exynos_drm_fb_unmap(struct exynos_drm_fb *exynos_fb)
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;
75 buf = exynos_drm_fb_buffer(exynos_fb, 0);
77 DRM_ERROR("buffer is null\n");
82 * Not critical, this is used for cleanup in the fb_create error path
83 * path so keep it silent.
90 /* Unmap the SGT to remove the IOMMU mapping created for this buffer */
91 dma_unmap_sg(obj->dev->dev,
96 drm_gem_object_unreference_unlocked(obj);
101 void exynos_drm_fb_release(struct kref *kref)
103 struct exynos_drm_fb *exynos_fb;
105 exynos_fb = container_of(kref, struct exynos_drm_fb, refcount);
106 schedule_work(&exynos_fb->release_work);
109 static void exynos_drm_fb_release_work_fn(struct work_struct *work)
111 struct drm_framebuffer *fb;
112 struct exynos_drm_fb *exynos_fb;
115 DRM_DEBUG_KMS("%s\n", __FILE__);
117 exynos_fb = container_of(work, struct exynos_drm_fb, release_work);
120 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
121 if (exynos_fb->dma_buf)
122 dma_buf_put(exynos_fb->dma_buf);
125 drm_framebuffer_cleanup(fb);
127 if (exynos_drm_fb_unmap(exynos_fb))
128 DRM_ERROR("Couldn't unmap buffer\n");
130 nr = exynos_drm_format_num_buffers(fb->pixel_format);
132 for (i = 0; i < nr; i++) {
133 struct drm_gem_object *obj;
135 obj = &exynos_fb->exynos_gem_obj[i]->base;
136 drm_gem_object_unreference_unlocked(obj);
142 static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
144 exynos_drm_fb_put(to_exynos_fb(fb));
147 static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
148 struct drm_file *file_priv,
149 unsigned int *handle)
151 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
153 DRM_DEBUG_KMS("%s\n", __FILE__);
155 return drm_gem_handle_create(file_priv,
156 &exynos_fb->exynos_gem_obj[0]->base, handle);
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,
164 DRM_DEBUG_KMS("%s\n", __FILE__);
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,
177 struct exynos_drm_fb *
178 exynos_drm_fb_init(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd)
180 struct exynos_drm_fb *exynos_fb;
183 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
185 DRM_ERROR("failed to allocate exynos drm framebuffer\n");
186 return ERR_PTR(-ENOMEM);
189 kref_init(&exynos_fb->refcount);
190 INIT_WORK(&exynos_fb->release_work, exynos_drm_fb_release_work_fn);
192 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
194 DRM_ERROR("failed to initialize framebuffer\n");
198 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
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)
211 struct drm_gem_object *obj;
212 struct exynos_drm_fb *exynos_fb;
215 DRM_DEBUG_KMS("%s\n", __FILE__);
217 exynos_fb = exynos_drm_fb_init(dev, mode_cmd);
218 if (IS_ERR(exynos_fb))
219 return ERR_CAST(exynos_fb);
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]);
226 DRM_ERROR("failed to lookup gem object\n");
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.
236 exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
239 if (exynos_drm_fb_map(exynos_fb)) {
240 DRM_ERROR("Failed to map gem object\n");
245 return &exynos_fb->fb;
250 obj = &exynos_fb->exynos_gem_obj[i]->base;
251 drm_gem_object_unreference_unlocked(obj);
257 struct exynos_drm_gem_buf *
258 exynos_drm_fb_buffer(struct exynos_drm_fb *exynos_fb, int index)
260 struct exynos_drm_gem_buf *buffer;
262 DRM_DEBUG_KMS("%s\n", __FILE__);
264 if (index >= MAX_FB_BUFFER)
267 buffer = exynos_fb->exynos_gem_obj[index]->buffer;
271 DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
272 (unsigned long)buffer->kvaddr,
273 (unsigned long)buffer->dma_addr);
278 static void exynos_drm_output_poll_changed(struct drm_device *dev)
280 struct exynos_drm_private *private = dev->dev_private;
281 struct drm_fb_helper *fb_helper = private->fb_helper;
284 drm_fb_helper_hotplug_event(fb_helper);
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,
292 void exynos_drm_mode_config_init(struct drm_device *dev)
294 dev->mode_config.min_width = 0;
295 dev->mode_config.min_height = 0;
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().
302 dev->mode_config.max_width = 4096;
303 dev->mode_config.max_height = 4096;
305 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;