blob: 6ecb7b17031631ceb34eca0a5b7a22ee23d7f0a4 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Masahiro Yamada78f27b12017-04-24 13:50:28 +090018#include <drm/drm_crtc.h>
19#include <drm/drm_crtc_helper.h>
20
Rob Clarkc8afe682013-06-26 12:44:06 -040021#include "msm_drv.h"
Rob Clarkdd2da6e2013-11-30 16:12:10 -050022#include "msm_kms.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040023
Rob Clarkc8afe682013-06-26 12:44:06 -040024struct msm_framebuffer {
25 struct drm_framebuffer base;
26 const struct msm_format *format;
Stephane Viau7ca12712014-12-08 10:48:57 -050027 struct drm_gem_object *planes[MAX_PLANE];
Rob Clarkc8afe682013-06-26 12:44:06 -040028};
29#define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
30
31
32static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
33 struct drm_file *file_priv,
34 unsigned int *handle)
35{
36 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
37 return drm_gem_handle_create(file_priv,
38 msm_fb->planes[0], handle);
39}
40
41static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
42{
43 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020044 int i, n = fb->format->num_planes;
Rob Clarkc8afe682013-06-26 12:44:06 -040045
46 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
47
48 drm_framebuffer_cleanup(fb);
49
50 for (i = 0; i < n; i++) {
51 struct drm_gem_object *bo = msm_fb->planes[i];
Markus Elfringe73a8562016-07-13 19:15:35 +020052
53 drm_gem_object_unreference_unlocked(bo);
Rob Clarkc8afe682013-06-26 12:44:06 -040054 }
55
56 kfree(msm_fb);
57}
58
Rob Clarkc8afe682013-06-26 12:44:06 -040059static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
60 .create_handle = msm_framebuffer_create_handle,
61 .destroy = msm_framebuffer_destroy,
Rob Clarkc8afe682013-06-26 12:44:06 -040062};
63
64#ifdef CONFIG_DEBUG_FS
65void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
66{
67 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020068 int i, n = fb->format->num_planes;
Rob Clarkc8afe682013-06-26 12:44:06 -040069
70 seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +020071 fb->width, fb->height, (char *)&fb->format->format,
Dave Airlie747a5982016-04-15 15:10:35 +100072 drm_framebuffer_read_refcount(fb), fb->base.id);
Rob Clarkc8afe682013-06-26 12:44:06 -040073
74 for (i = 0; i < n; i++) {
75 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
76 i, fb->offsets[i], fb->pitches[i]);
77 msm_gem_describe(msm_fb->planes[i], m);
78 }
79}
80#endif
81
Rob Clark2638d902014-11-08 09:13:37 -050082/* prepare/pin all the fb's bo's for scanout. Note that it is not valid
83 * to prepare an fb more multiple different initiator 'id's. But that
84 * should be fine, since only the scanout (mdpN) side of things needs
85 * this, the gpu doesn't care about fb's.
86 */
Rob Clark8bdcd942017-06-13 11:07:08 -040087int msm_framebuffer_prepare(struct drm_framebuffer *fb,
88 struct msm_gem_address_space *aspace)
Rob Clark2638d902014-11-08 09:13:37 -050089{
90 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020091 int ret, i, n = fb->format->num_planes;
Rob Clark78babc12016-11-11 12:06:46 -050092 uint64_t iova;
Rob Clark2638d902014-11-08 09:13:37 -050093
94 for (i = 0; i < n; i++) {
Rob Clark8bdcd942017-06-13 11:07:08 -040095 ret = msm_gem_get_iova(msm_fb->planes[i], aspace, &iova);
Rob Clark78babc12016-11-11 12:06:46 -050096 DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
Rob Clark2638d902014-11-08 09:13:37 -050097 if (ret)
98 return ret;
99 }
100
101 return 0;
102}
103
Rob Clark8bdcd942017-06-13 11:07:08 -0400104void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
105 struct msm_gem_address_space *aspace)
Rob Clark2638d902014-11-08 09:13:37 -0500106{
107 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200108 int i, n = fb->format->num_planes;
Rob Clark2638d902014-11-08 09:13:37 -0500109
110 for (i = 0; i < n; i++)
Rob Clark8bdcd942017-06-13 11:07:08 -0400111 msm_gem_put_iova(msm_fb->planes[i], aspace);
Rob Clark2638d902014-11-08 09:13:37 -0500112}
113
Rob Clark8bdcd942017-06-13 11:07:08 -0400114uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
115 struct msm_gem_address_space *aspace, int plane)
Rob Clark2638d902014-11-08 09:13:37 -0500116{
117 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Rob Clark3e2f29e2014-11-19 12:29:33 -0500118 if (!msm_fb->planes[plane])
119 return 0;
Rob Clark8bdcd942017-06-13 11:07:08 -0400120 return msm_gem_iova(msm_fb->planes[plane], aspace) + fb->offsets[plane];
Rob Clark2638d902014-11-08 09:13:37 -0500121}
122
Rob Clarkc8afe682013-06-26 12:44:06 -0400123struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
124{
125 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
126 return msm_fb->planes[plane];
127}
128
129const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
130{
131 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
132 return msm_fb->format;
133}
134
135struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200136 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
Rob Clarkc8afe682013-06-26 12:44:06 -0400137{
138 struct drm_gem_object *bos[4] = {0};
139 struct drm_framebuffer *fb;
140 int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
141
142 for (i = 0; i < n; i++) {
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100143 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
Rob Clarkc8afe682013-06-26 12:44:06 -0400144 if (!bos[i]) {
145 ret = -ENXIO;
146 goto out_unref;
147 }
148 }
149
150 fb = msm_framebuffer_init(dev, mode_cmd, bos);
151 if (IS_ERR(fb)) {
152 ret = PTR_ERR(fb);
153 goto out_unref;
154 }
155
156 return fb;
157
158out_unref:
159 for (i = 0; i < n; i++)
160 drm_gem_object_unreference_unlocked(bos[i]);
161 return ERR_PTR(ret);
162}
163
164struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200165 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
Rob Clarkc8afe682013-06-26 12:44:06 -0400166{
167 struct msm_drm_private *priv = dev->dev_private;
168 struct msm_kms *kms = priv->kms;
Stephane Viau7194b622015-05-05 09:47:57 -0400169 struct msm_framebuffer *msm_fb = NULL;
170 struct drm_framebuffer *fb;
Rob Clarkc8afe682013-06-26 12:44:06 -0400171 const struct msm_format *format;
172 int ret, i, n;
173 unsigned int hsub, vsub;
174
175 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
176 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
177 (char *)&mode_cmd->pixel_format);
178
179 n = drm_format_num_planes(mode_cmd->pixel_format);
180 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
181 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
182
183 format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
184 if (!format) {
185 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
186 (char *)&mode_cmd->pixel_format);
187 ret = -EINVAL;
188 goto fail;
189 }
190
191 msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
192 if (!msm_fb) {
193 ret = -ENOMEM;
194 goto fail;
195 }
196
197 fb = &msm_fb->base;
198
199 msm_fb->format = format;
200
Rob Clark10291bf2014-11-08 09:20:28 -0500201 if (n > ARRAY_SIZE(msm_fb->planes)) {
202 ret = -EINVAL;
203 goto fail;
204 }
205
Rob Clarkc8afe682013-06-26 12:44:06 -0400206 for (i = 0; i < n; i++) {
207 unsigned int width = mode_cmd->width / (i ? hsub : 1);
208 unsigned int height = mode_cmd->height / (i ? vsub : 1);
209 unsigned int min_size;
210
211 min_size = (height - 1) * mode_cmd->pitches[i]
212 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
213 + mode_cmd->offsets[i];
214
215 if (bos[i]->size < min_size) {
216 ret = -EINVAL;
217 goto fail;
218 }
219
220 msm_fb->planes[i] = bos[i];
221 }
222
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200223 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
Rob Clarkc8afe682013-06-26 12:44:06 -0400224
225 ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
226 if (ret) {
227 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
228 goto fail;
229 }
230
231 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
232
233 return fb;
234
235fail:
Stephane Viau7194b622015-05-05 09:47:57 -0400236 kfree(msm_fb);
Rob Clarkc8afe682013-06-26 12:44:06 -0400237
238 return ERR_PTR(ret);
239}