3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
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.
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <uapi/drm/exynos_drm.h>
23 #include "exynos_drm_drv.h"
24 #include "exynos_drm_fb.h"
25 #include "exynos_drm_fbdev.h"
26 #include "exynos_drm_iommu.h"
27 #include "exynos_drm_crtc.h"
29 #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
32 * exynos specific framebuffer structure.
34 * @fb: drm framebuffer obejct.
35 * @exynos_gem: array of exynos specific gem object containing a gem object.
37 struct exynos_drm_fb {
38 struct drm_framebuffer fb;
39 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
40 dma_addr_t dma_addr[MAX_FB_BUFFER];
43 static int check_fb_gem_memory_type(struct drm_device *drm_dev,
44 struct exynos_drm_gem *exynos_gem)
49 * if exynos drm driver supports iommu then framebuffer can use
50 * all the buffer types.
52 if (is_drm_iommu_supported(drm_dev))
55 flags = exynos_gem->flags;
58 * without iommu support, not support physically non-continuous memory
61 if (IS_NONCONTIG_BUFFER(flags)) {
62 DRM_ERROR("cannot use this gem memory type for fb.\n");
69 static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
71 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
74 drm_framebuffer_cleanup(fb);
76 for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) {
77 struct drm_gem_object *obj;
79 if (exynos_fb->exynos_gem[i] == NULL)
82 obj = &exynos_fb->exynos_gem[i]->base;
83 drm_gem_object_unreference_unlocked(obj);
90 static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
91 struct drm_file *file_priv,
94 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
96 return drm_gem_handle_create(file_priv,
97 &exynos_fb->exynos_gem[0]->base, handle);
100 static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
101 struct drm_file *file_priv, unsigned flags,
102 unsigned color, struct drm_clip_rect *clips,
110 static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
111 .destroy = exynos_drm_fb_destroy,
112 .create_handle = exynos_drm_fb_create_handle,
113 .dirty = exynos_drm_fb_dirty,
116 struct drm_framebuffer *
117 exynos_drm_framebuffer_init(struct drm_device *dev,
118 const struct drm_mode_fb_cmd2 *mode_cmd,
119 struct exynos_drm_gem **exynos_gem,
122 struct exynos_drm_fb *exynos_fb;
126 exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
128 return ERR_PTR(-ENOMEM);
130 for (i = 0; i < count; i++) {
131 ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
135 exynos_fb->exynos_gem[i] = exynos_gem[i];
136 exynos_fb->dma_addr[i] = exynos_gem[i]->dma_addr
137 + mode_cmd->offsets[i];
140 drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
142 ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
144 DRM_ERROR("failed to initialize framebuffer\n");
148 return &exynos_fb->fb;
155 static struct drm_framebuffer *
156 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
157 const struct drm_mode_fb_cmd2 *mode_cmd)
159 struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
160 struct drm_gem_object *obj;
161 struct drm_framebuffer *fb;
165 for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
166 obj = drm_gem_object_lookup(dev, file_priv,
167 mode_cmd->handles[i]);
169 DRM_ERROR("failed to lookup gem object\n");
174 exynos_gem[i] = to_exynos_gem(obj);
177 fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
187 drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
192 dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
194 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
196 if (index >= MAX_FB_BUFFER)
197 return DMA_ERROR_CODE;
199 return exynos_fb->dma_addr[index];
202 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
203 .fb_create = exynos_user_fb_create,
204 .output_poll_changed = exynos_drm_output_poll_changed,
205 .atomic_check = drm_atomic_helper_check,
206 .atomic_commit = exynos_atomic_commit,
209 void exynos_drm_mode_config_init(struct drm_device *dev)
211 dev->mode_config.min_width = 0;
212 dev->mode_config.min_height = 0;
215 * set max width and height as default value(4096x4096).
216 * this value would be used to check framebuffer size limitation
217 * at drm_mode_addfb().
219 dev->mode_config.max_width = 4096;
220 dev->mode_config.max_height = 4096;
222 dev->mode_config.funcs = &exynos_drm_mode_config_funcs;