blob: 9890eccadd3bd48dcc0aec3faf00846b60c3dc0b [file] [log] [blame]
Mark Yao2048e322014-08-22 18:36:26 +08001/*
2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/kernel.h>
16#include <drm/drm.h>
17#include <drm/drmP.h>
Mark Yao63ebb9f2015-11-30 18:22:42 +080018#include <drm/drm_atomic.h>
Mark Yao2048e322014-08-22 18:36:26 +080019#include <drm/drm_fb_helper.h>
20#include <drm/drm_crtc_helper.h>
21
22#include "rockchip_drm_drv.h"
John Keepinga7e03fb2016-05-10 17:03:55 +010023#include "rockchip_drm_fb.h"
Mark Yao2048e322014-08-22 18:36:26 +080024#include "rockchip_drm_gem.h"
Yakir Yang5182c1a2016-07-24 14:57:44 +080025#include "rockchip_drm_psr.h"
Mark Yao2048e322014-08-22 18:36:26 +080026
27#define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
28
29struct rockchip_drm_fb {
30 struct drm_framebuffer fb;
31 struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
32};
33
34struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb,
35 unsigned int plane)
36{
37 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
38
39 if (plane >= ROCKCHIP_MAX_FB_BUFFER)
40 return NULL;
41
42 return rk_fb->obj[plane];
43}
Mark Yao2048e322014-08-22 18:36:26 +080044
45static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
46{
47 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
Mark Yao2048e322014-08-22 18:36:26 +080048 int i;
49
Markus Elfringf5e193a2016-07-15 15:23:22 +020050 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++)
51 drm_gem_object_unreference_unlocked(rockchip_fb->obj[i]);
Mark Yao2048e322014-08-22 18:36:26 +080052
53 drm_framebuffer_cleanup(fb);
54 kfree(rockchip_fb);
55}
56
57static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
58 struct drm_file *file_priv,
59 unsigned int *handle)
60{
61 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
62
63 return drm_gem_handle_create(file_priv,
64 rockchip_fb->obj[0], handle);
65}
66
Yakir Yang5182c1a2016-07-24 14:57:44 +080067static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb,
68 struct drm_file *file,
69 unsigned int flags, unsigned int color,
70 struct drm_clip_rect *clips,
71 unsigned int num_clips)
72{
Sean Paulb883c9b2016-08-18 12:01:46 -070073 rockchip_drm_psr_flush_all(fb->dev);
Yakir Yang5182c1a2016-07-24 14:57:44 +080074 return 0;
75}
76
Ville Syrjälä28c508e2015-12-15 12:21:12 +010077static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
Mark Yao2048e322014-08-22 18:36:26 +080078 .destroy = rockchip_drm_fb_destroy,
79 .create_handle = rockchip_drm_fb_create_handle,
Yakir Yang5182c1a2016-07-24 14:57:44 +080080 .dirty = rockchip_drm_fb_dirty,
Mark Yao2048e322014-08-22 18:36:26 +080081};
82
83static struct rockchip_drm_fb *
Ville Syrjälä1eb83452015-11-11 19:11:29 +020084rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
Mark Yao2048e322014-08-22 18:36:26 +080085 struct drm_gem_object **obj, unsigned int num_planes)
86{
87 struct rockchip_drm_fb *rockchip_fb;
88 int ret;
89 int i;
90
91 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
92 if (!rockchip_fb)
93 return ERR_PTR(-ENOMEM);
94
95 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
96
97 for (i = 0; i < num_planes; i++)
98 rockchip_fb->obj[i] = obj[i];
99
100 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
101 &rockchip_drm_fb_funcs);
102 if (ret) {
103 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
104 ret);
105 kfree(rockchip_fb);
106 return ERR_PTR(ret);
107 }
108
109 return rockchip_fb;
110}
111
112static struct drm_framebuffer *
113rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200114 const struct drm_mode_fb_cmd2 *mode_cmd)
Mark Yao2048e322014-08-22 18:36:26 +0800115{
116 struct rockchip_drm_fb *rockchip_fb;
117 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
118 struct drm_gem_object *obj;
119 unsigned int hsub;
120 unsigned int vsub;
121 int num_planes;
122 int ret;
123 int i;
124
125 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
126 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
127 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
128 ROCKCHIP_MAX_FB_BUFFER);
129
130 for (i = 0; i < num_planes; i++) {
131 unsigned int width = mode_cmd->width / (i ? hsub : 1);
132 unsigned int height = mode_cmd->height / (i ? vsub : 1);
133 unsigned int min_size;
134
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100135 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
Mark Yao2048e322014-08-22 18:36:26 +0800136 if (!obj) {
137 dev_err(dev->dev, "Failed to lookup GEM object\n");
138 ret = -ENXIO;
139 goto err_gem_object_unreference;
140 }
141
142 min_size = (height - 1) * mode_cmd->pitches[i] +
143 mode_cmd->offsets[i] +
144 width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
145
146 if (obj->size < min_size) {
147 drm_gem_object_unreference_unlocked(obj);
148 ret = -EINVAL;
149 goto err_gem_object_unreference;
150 }
151 objs[i] = obj;
152 }
153
154 rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
155 if (IS_ERR(rockchip_fb)) {
156 ret = PTR_ERR(rockchip_fb);
157 goto err_gem_object_unreference;
158 }
159
160 return &rockchip_fb->fb;
161
162err_gem_object_unreference:
163 for (i--; i >= 0; i--)
164 drm_gem_object_unreference_unlocked(objs[i]);
165 return ERR_PTR(ret);
166}
167
168static void rockchip_drm_output_poll_changed(struct drm_device *dev)
169{
170 struct rockchip_drm_private *private = dev->dev_private;
171 struct drm_fb_helper *fb_helper = &private->fbdev_helper;
172
Heiko Stübner765c35b2015-06-02 16:41:45 +0200173 if (fb_helper)
174 drm_fb_helper_hotplug_event(fb_helper);
Mark Yao2048e322014-08-22 18:36:26 +0800175}
176
Mark Yao63ebb9f2015-11-30 18:22:42 +0800177static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
178{
179 struct rockchip_drm_private *priv = crtc->dev->dev_private;
180 int pipe = drm_crtc_index(crtc);
181 const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
182
183 if (crtc_funcs && crtc_funcs->wait_for_update)
184 crtc_funcs->wait_for_update(crtc);
185}
186
John Keepingc9ad1d92016-01-19 10:47:00 +0000187/*
188 * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
189 * have hardware counters for neither vblanks nor scanlines, which results in
190 * a race where:
191 * | <-- HW vsync irq and reg take effect
192 * plane_commit --> |
193 * get_vblank and wait --> |
194 * | <-- handle_vblank, vblank->count + 1
195 * cleanup_fb --> |
196 * iommu crash --> |
197 * | <-- HW vsync irq and reg take effect
198 *
199 * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
200 * of waiting for vblank_count to change.
201 */
Mark Yao63ebb9f2015-11-30 18:22:42 +0800202static void
John Keepingf2227f42016-01-19 10:46:59 +0000203rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
Mark Yao63ebb9f2015-11-30 18:22:42 +0800204{
205 struct drm_crtc_state *old_crtc_state;
206 struct drm_crtc *crtc;
207 int i, ret;
208
209 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
210 /* No one cares about the old state, so abuse it for tracking
211 * and store whether we hold a vblank reference (and should do a
212 * vblank wait) in the ->enable boolean.
213 */
214 old_crtc_state->enable = false;
215
216 if (!crtc->state->active)
217 continue;
218
John Keepingf2227f42016-01-19 10:46:59 +0000219 if (!drm_atomic_helper_framebuffer_changed(dev,
220 old_state, crtc))
221 continue;
222
Mark Yao63ebb9f2015-11-30 18:22:42 +0800223 ret = drm_crtc_vblank_get(crtc);
224 if (ret != 0)
225 continue;
226
227 old_crtc_state->enable = true;
228 }
229
230 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
231 if (!old_crtc_state->enable)
232 continue;
233
234 rockchip_crtc_wait_for_update(crtc);
235 drm_crtc_vblank_put(crtc);
236 }
237}
238
Mark Yaof32fad52015-12-16 18:09:38 +0800239static void
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200240rockchip_atomic_commit_tail(struct drm_atomic_state *state)
Mark Yao63ebb9f2015-11-30 18:22:42 +0800241{
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200242 struct drm_device *dev = state->dev;
Mark Yao63ebb9f2015-11-30 18:22:42 +0800243
Mark Yao63ebb9f2015-11-30 18:22:42 +0800244 drm_atomic_helper_commit_modeset_disables(dev, state);
245
246 drm_atomic_helper_commit_modeset_enables(dev, state);
247
Liu Ying2b58e982016-08-29 17:12:03 +0800248 drm_atomic_helper_commit_planes(dev, state,
249 DRM_PLANE_COMMIT_ACTIVE_ONLY);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800250
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200251 drm_atomic_helper_commit_hw_done(state);
252
John Keepingf2227f42016-01-19 10:46:59 +0000253 rockchip_atomic_wait_for_complete(dev, state);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800254
255 drm_atomic_helper_cleanup_planes(dev, state);
Mark Yaof32fad52015-12-16 18:09:38 +0800256}
257
John Keeping8ff490a2016-05-10 17:03:56 +0100258static struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200259 .atomic_commit_tail = rockchip_atomic_commit_tail,
260};
Mark Yao63ebb9f2015-11-30 18:22:42 +0800261
Mark Yao2048e322014-08-22 18:36:26 +0800262static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
263 .fb_create = rockchip_user_fb_create,
264 .output_poll_changed = rockchip_drm_output_poll_changed,
Mark Yao63ebb9f2015-11-30 18:22:42 +0800265 .atomic_check = drm_atomic_helper_check,
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200266 .atomic_commit = drm_atomic_helper_commit,
Mark Yao2048e322014-08-22 18:36:26 +0800267};
268
269struct drm_framebuffer *
270rockchip_drm_framebuffer_init(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200271 const struct drm_mode_fb_cmd2 *mode_cmd,
Mark Yao2048e322014-08-22 18:36:26 +0800272 struct drm_gem_object *obj)
273{
274 struct rockchip_drm_fb *rockchip_fb;
275
276 rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
277 if (IS_ERR(rockchip_fb))
278 return NULL;
279
280 return &rockchip_fb->fb;
281}
282
283void rockchip_drm_mode_config_init(struct drm_device *dev)
284{
285 dev->mode_config.min_width = 0;
286 dev->mode_config.min_height = 0;
287
288 /*
289 * set max width and height as default value(4096x4096).
290 * this value would be used to check framebuffer size limitation
291 * at drm_mode_addfb().
292 */
293 dev->mode_config.max_width = 4096;
294 dev->mode_config.max_height = 4096;
295
296 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200297 dev->mode_config.helper_private = &rockchip_mode_config_helpers;
Mark Yao2048e322014-08-22 18:36:26 +0800298}