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> | |
d6562a29 | 19 | #include <drm/drm_atomic.h> |
910874a8 | 20 | #include <drm/drm_atomic_helper.h> |
0519f9a1 | 21 | #include <uapi/drm/exynos_drm.h> |
1c248b7d | 22 | |
7db3eba6 | 23 | #include "exynos_drm_drv.h" |
1c248b7d | 24 | #include "exynos_drm_fb.h" |
25928a39 | 25 | #include "exynos_drm_fbdev.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. | |
813fd67b | 35 | * @exynos_gem: array of exynos specific gem object containing a gem object. |
1c248b7d ID |
36 | */ |
37 | struct exynos_drm_fb { | |
813fd67b JS |
38 | struct drm_framebuffer fb; |
39 | struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER]; | |
0488f50e | 40 | dma_addr_t dma_addr[MAX_FB_BUFFER]; |
1c248b7d ID |
41 | }; |
42 | ||
0519f9a1 | 43 | static int check_fb_gem_memory_type(struct drm_device *drm_dev, |
813fd67b | 44 | struct exynos_drm_gem *exynos_gem) |
0519f9a1 ID |
45 | { |
46 | unsigned int flags; | |
47 | ||
48 | /* | |
49 | * if exynos drm driver supports iommu then framebuffer can use | |
50 | * all the buffer types. | |
51 | */ | |
52 | if (is_drm_iommu_supported(drm_dev)) | |
53 | return 0; | |
54 | ||
813fd67b | 55 | flags = exynos_gem->flags; |
0519f9a1 ID |
56 | |
57 | /* | |
6244bd65 SK |
58 | * Physically non-contiguous memory type for framebuffer is not |
59 | * supported without IOMMU. | |
0519f9a1 ID |
60 | */ |
61 | if (IS_NONCONTIG_BUFFER(flags)) { | |
6244bd65 | 62 | DRM_ERROR("Non-contiguous GEM memory is not supported.\n"); |
0519f9a1 ID |
63 | return -EINVAL; |
64 | } | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
1c248b7d ID |
69 | static void exynos_drm_fb_destroy(struct drm_framebuffer *fb) |
70 | { | |
71 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); | |
07b6835f | 72 | unsigned int i; |
1c248b7d | 73 | |
1c248b7d ID |
74 | drm_framebuffer_cleanup(fb); |
75 | ||
813fd67b | 76 | for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) { |
07b6835f LP |
77 | struct drm_gem_object *obj; |
78 | ||
813fd67b | 79 | if (exynos_fb->exynos_gem[i] == NULL) |
07b6835f LP |
80 | continue; |
81 | ||
813fd67b | 82 | obj = &exynos_fb->exynos_gem[i]->base; |
07b6835f LP |
83 | drm_gem_object_unreference_unlocked(obj); |
84 | } | |
85 | ||
1c248b7d ID |
86 | kfree(exynos_fb); |
87 | exynos_fb = NULL; | |
88 | } | |
89 | ||
90 | static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb, | |
91 | struct drm_file *file_priv, | |
92 | unsigned int *handle) | |
93 | { | |
94 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); | |
95 | ||
1c248b7d | 96 | return drm_gem_handle_create(file_priv, |
813fd67b | 97 | &exynos_fb->exynos_gem[0]->base, handle); |
1c248b7d ID |
98 | } |
99 | ||
800ba2b5 | 100 | static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = { |
1c248b7d ID |
101 | .destroy = exynos_drm_fb_destroy, |
102 | .create_handle = exynos_drm_fb_create_handle, | |
1c248b7d ID |
103 | }; |
104 | ||
e1533c08 JS |
105 | struct drm_framebuffer * |
106 | exynos_drm_framebuffer_init(struct drm_device *dev, | |
1eb83451 | 107 | const struct drm_mode_fb_cmd2 *mode_cmd, |
813fd67b | 108 | struct exynos_drm_gem **exynos_gem, |
d56125af | 109 | int count) |
1c248b7d ID |
110 | { |
111 | struct exynos_drm_fb *exynos_fb; | |
d56125af | 112 | int i; |
1c248b7d ID |
113 | int ret; |
114 | ||
1c248b7d | 115 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
38bb5253 | 116 | if (!exynos_fb) |
1c248b7d | 117 | return ERR_PTR(-ENOMEM); |
1c248b7d | 118 | |
d56125af | 119 | for (i = 0; i < count; i++) { |
813fd67b | 120 | ret = check_fb_gem_memory_type(dev, exynos_gem[i]); |
d56125af JS |
121 | if (ret < 0) |
122 | goto err; | |
123 | ||
813fd67b | 124 | exynos_fb->exynos_gem[i] = exynos_gem[i]; |
0488f50e MS |
125 | exynos_fb->dma_addr[i] = exynos_gem[i]->dma_addr |
126 | + mode_cmd->offsets[i]; | |
d56125af JS |
127 | } |
128 | ||
a3f913ca | 129 | drm_helper_mode_fill_fb_struct(dev, &exynos_fb->fb, mode_cmd); |
94e30d93 | 130 | |
e1533c08 | 131 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
d56125af | 132 | if (ret < 0) { |
e1533c08 | 133 | DRM_ERROR("failed to initialize framebuffer\n"); |
d56125af | 134 | goto err; |
1c248b7d ID |
135 | } |
136 | ||
e1533c08 | 137 | return &exynos_fb->fb; |
d56125af JS |
138 | |
139 | err: | |
140 | kfree(exynos_fb); | |
141 | return ERR_PTR(ret); | |
1c248b7d ID |
142 | } |
143 | ||
e1533c08 JS |
144 | static struct drm_framebuffer * |
145 | exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, | |
1eb83451 | 146 | const struct drm_mode_fb_cmd2 *mode_cmd) |
1c248b7d | 147 | { |
813fd67b | 148 | struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER]; |
e1533c08 | 149 | struct drm_gem_object *obj; |
8d31758e JS |
150 | struct drm_framebuffer *fb; |
151 | int i; | |
152 | int ret; | |
229d3534 | 153 | |
8d31758e | 154 | for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) { |
a8ad0bd8 | 155 | obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]); |
229d3534 SWK |
156 | if (!obj) { |
157 | DRM_ERROR("failed to lookup gem object\n"); | |
979c0c7e | 158 | ret = -ENOENT; |
dcbb85a1 | 159 | goto err; |
229d3534 SWK |
160 | } |
161 | ||
813fd67b | 162 | exynos_gem[i] = to_exynos_gem(obj); |
229d3534 SWK |
163 | } |
164 | ||
813fd67b | 165 | fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i); |
8d31758e JS |
166 | if (IS_ERR(fb)) { |
167 | ret = PTR_ERR(fb); | |
dcbb85a1 | 168 | goto err; |
f2c0095a DV |
169 | } |
170 | ||
8d31758e | 171 | return fb; |
979c0c7e | 172 | |
dcbb85a1 | 173 | err: |
8d31758e | 174 | while (i--) |
813fd67b | 175 | drm_gem_object_unreference_unlocked(&exynos_gem[i]->base); |
979c0c7e | 176 | |
979c0c7e | 177 | return ERR_PTR(ret); |
1c248b7d ID |
178 | } |
179 | ||
0488f50e | 180 | dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index) |
1c248b7d ID |
181 | { |
182 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); | |
1c248b7d | 183 | |
e0c7a510 CH |
184 | if (WARN_ON_ONCE(index >= MAX_FB_BUFFER)) |
185 | return 0; | |
229d3534 | 186 | |
0488f50e | 187 | return exynos_fb->dma_addr[index]; |
1c248b7d ID |
188 | } |
189 | ||
41cbf0fd ID |
190 | static void exynos_drm_atomic_commit_tail(struct drm_atomic_state *state) |
191 | { | |
192 | struct drm_device *dev = state->dev; | |
193 | ||
194 | drm_atomic_helper_commit_modeset_disables(dev, state); | |
195 | ||
196 | drm_atomic_helper_commit_modeset_enables(dev, state); | |
197 | ||
198 | /* | |
199 | * Exynos can't update planes with CRTCs and encoders disabled, | |
200 | * its updates routines, specially for FIMD, requires the clocks | |
201 | * to be enabled. So it is necessary to handle the modeset operations | |
202 | * *before* the commit_planes() step, this way it will always | |
203 | * have the relevant clocks enabled to perform the update. | |
204 | */ | |
205 | drm_atomic_helper_commit_planes(dev, state, | |
206 | DRM_PLANE_COMMIT_ACTIVE_ONLY); | |
207 | ||
208 | drm_atomic_helper_commit_hw_done(state); | |
209 | ||
210 | drm_atomic_helper_wait_for_vblanks(dev, state); | |
211 | ||
212 | drm_atomic_helper_cleanup_planes(dev, state); | |
213 | } | |
214 | ||
215 | static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = { | |
216 | .atomic_commit_tail = exynos_drm_atomic_commit_tail, | |
217 | }; | |
218 | ||
e6ecefaa | 219 | static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { |
e1533c08 | 220 | .fb_create = exynos_user_fb_create, |
7db3eba6 | 221 | .output_poll_changed = exynos_drm_output_poll_changed, |
38d868e4 | 222 | .atomic_check = exynos_atomic_check, |
41cbf0fd | 223 | .atomic_commit = drm_atomic_helper_commit, |
1c248b7d ID |
224 | }; |
225 | ||
226 | void exynos_drm_mode_config_init(struct drm_device *dev) | |
227 | { | |
228 | dev->mode_config.min_width = 0; | |
229 | dev->mode_config.min_height = 0; | |
230 | ||
231 | /* | |
232 | * set max width and height as default value(4096x4096). | |
233 | * this value would be used to check framebuffer size limitation | |
234 | * at drm_mode_addfb(). | |
235 | */ | |
236 | dev->mode_config.max_width = 4096; | |
237 | dev->mode_config.max_height = 4096; | |
238 | ||
239 | dev->mode_config.funcs = &exynos_drm_mode_config_funcs; | |
41cbf0fd | 240 | dev->mode_config.helper_private = &exynos_drm_mode_config_helpers; |
1c248b7d | 241 | } |