Merge branch 'asoc-4.19' into asoc-4.20 Cirrus conflict
[linux-block.git] / drivers / gpu / drm / rockchip / rockchip_drm_fb.c
CommitLineData
2048e328
MY
1/*
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>
63ebb9fa 18#include <drm/drm_atomic.h>
2048e328
MY
19#include <drm/drm_fb_helper.h>
20#include <drm/drm_crtc_helper.h>
4c7c9a63 21#include <drm/drm_gem_framebuffer_helper.h>
2048e328
MY
22
23#include "rockchip_drm_drv.h"
a7e03fb5 24#include "rockchip_drm_fb.h"
2048e328 25#include "rockchip_drm_gem.h"
5182c1a5 26#include "rockchip_drm_psr.h"
2048e328 27
5182c1a5
YY
28static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb,
29 struct drm_file *file,
30 unsigned int flags, unsigned int color,
31 struct drm_clip_rect *clips,
32 unsigned int num_clips)
33{
b883c9ba 34 rockchip_drm_psr_flush_all(fb->dev);
5182c1a5
YY
35 return 0;
36}
37
28c508ec 38static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
4c7c9a63
DS
39 .destroy = drm_gem_fb_destroy,
40 .create_handle = drm_gem_fb_create_handle,
41 .dirty = rockchip_drm_fb_dirty,
2048e328
MY
42};
43
957428f9 44static struct drm_framebuffer *
1eb83451 45rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
2048e328
MY
46 struct drm_gem_object **obj, unsigned int num_planes)
47{
957428f9 48 struct drm_framebuffer *fb;
2048e328
MY
49 int ret;
50 int i;
51
957428f9
DS
52 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
53 if (!fb)
2048e328
MY
54 return ERR_PTR(-ENOMEM);
55
957428f9 56 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
2048e328
MY
57
58 for (i = 0; i < num_planes; i++)
957428f9 59 fb->obj[i] = obj[i];
2048e328 60
957428f9 61 ret = drm_framebuffer_init(dev, fb, &rockchip_drm_fb_funcs);
2048e328 62 if (ret) {
d8dd6804
HM
63 DRM_DEV_ERROR(dev->dev,
64 "Failed to initialize framebuffer: %d\n",
65 ret);
957428f9 66 kfree(fb);
2048e328
MY
67 return ERR_PTR(ret);
68 }
69
957428f9 70 return fb;
2048e328
MY
71}
72
73static struct drm_framebuffer *
74rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
1eb83451 75 const struct drm_mode_fb_cmd2 *mode_cmd)
2048e328 76{
957428f9 77 struct drm_framebuffer *fb;
2048e328
MY
78 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
79 struct drm_gem_object *obj;
80 unsigned int hsub;
81 unsigned int vsub;
82 int num_planes;
83 int ret;
84 int i;
85
86 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
87 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
88 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
89 ROCKCHIP_MAX_FB_BUFFER);
90
91 for (i = 0; i < num_planes; i++) {
92 unsigned int width = mode_cmd->width / (i ? hsub : 1);
93 unsigned int height = mode_cmd->height / (i ? vsub : 1);
94 unsigned int min_size;
95
a8ad0bd8 96 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
2048e328 97 if (!obj) {
d8dd6804
HM
98 DRM_DEV_ERROR(dev->dev,
99 "Failed to lookup GEM object\n");
2048e328
MY
100 ret = -ENXIO;
101 goto err_gem_object_unreference;
102 }
103
104 min_size = (height - 1) * mode_cmd->pitches[i] +
105 mode_cmd->offsets[i] +
106 width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
107
108 if (obj->size < min_size) {
adedbf03 109 drm_gem_object_put_unlocked(obj);
2048e328
MY
110 ret = -EINVAL;
111 goto err_gem_object_unreference;
112 }
113 objs[i] = obj;
114 }
115
957428f9
DS
116 fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
117 if (IS_ERR(fb)) {
118 ret = PTR_ERR(fb);
2048e328
MY
119 goto err_gem_object_unreference;
120 }
121
957428f9 122 return fb;
2048e328
MY
123
124err_gem_object_unreference:
125 for (i--; i >= 0; i--)
adedbf03 126 drm_gem_object_put_unlocked(objs[i]);
2048e328
MY
127 return ERR_PTR(ret);
128}
129
d2d4f51d
TF
130static void
131rockchip_drm_psr_inhibit_get_state(struct drm_atomic_state *state)
132{
133 struct drm_crtc *crtc;
134 struct drm_crtc_state *crtc_state;
135 struct drm_encoder *encoder;
136 u32 encoder_mask = 0;
137 int i;
138
139 for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
140 encoder_mask |= crtc_state->encoder_mask;
141 encoder_mask |= crtc->state->encoder_mask;
142 }
143
144 drm_for_each_encoder_mask(encoder, state->dev, encoder_mask)
145 rockchip_drm_psr_inhibit_get(encoder);
146}
147
148static void
149rockchip_drm_psr_inhibit_put_state(struct drm_atomic_state *state)
150{
151 struct drm_crtc *crtc;
152 struct drm_crtc_state *crtc_state;
153 struct drm_encoder *encoder;
154 u32 encoder_mask = 0;
155 int i;
156
157 for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
158 encoder_mask |= crtc_state->encoder_mask;
159 encoder_mask |= crtc->state->encoder_mask;
160 }
161
162 drm_for_each_encoder_mask(encoder, state->dev, encoder_mask)
163 rockchip_drm_psr_inhibit_put(encoder);
164}
165
166static void
167rockchip_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
168{
169 struct drm_device *dev = old_state->dev;
170
171 rockchip_drm_psr_inhibit_get_state(old_state);
172
173 drm_atomic_helper_commit_modeset_disables(dev, old_state);
174
175 drm_atomic_helper_commit_modeset_enables(dev, old_state);
176
177 drm_atomic_helper_commit_planes(dev, old_state,
178 DRM_PLANE_COMMIT_ACTIVE_ONLY);
179
180 rockchip_drm_psr_inhibit_put_state(old_state);
181
182 drm_atomic_helper_commit_hw_done(old_state);
183
184 drm_atomic_helper_wait_for_vblanks(dev, old_state);
185
186 drm_atomic_helper_cleanup_planes(dev, old_state);
187}
188
a4b10cce 189static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
d2d4f51d 190 .atomic_commit_tail = rockchip_atomic_helper_commit_tail_rpm,
893b6cad 191};
63ebb9fa 192
2048e328
MY
193static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
194 .fb_create = rockchip_user_fb_create,
33e9d038 195 .output_poll_changed = drm_fb_helper_output_poll_changed,
63ebb9fa 196 .atomic_check = drm_atomic_helper_check,
893b6cad 197 .atomic_commit = drm_atomic_helper_commit,
2048e328
MY
198};
199
200struct drm_framebuffer *
201rockchip_drm_framebuffer_init(struct drm_device *dev,
1eb83451 202 const struct drm_mode_fb_cmd2 *mode_cmd,
2048e328
MY
203 struct drm_gem_object *obj)
204{
957428f9 205 struct drm_framebuffer *fb;
2048e328 206
957428f9
DS
207 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
208 if (IS_ERR(fb))
209 return ERR_CAST(fb);
2048e328 210
957428f9 211 return fb;
2048e328
MY
212}
213
214void rockchip_drm_mode_config_init(struct drm_device *dev)
215{
216 dev->mode_config.min_width = 0;
217 dev->mode_config.min_height = 0;
218
219 /*
220 * set max width and height as default value(4096x4096).
221 * this value would be used to check framebuffer size limitation
222 * at drm_mode_addfb().
223 */
224 dev->mode_config.max_width = 4096;
225 dev->mode_config.max_height = 4096;
226
227 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
893b6cad 228 dev->mode_config.helper_private = &rockchip_mode_config_helpers;
2048e328 229}