blob: fc175e724ad63c3dd602a2ef3e3ece254168077a [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 Clark466e5602017-07-11 10:40:13 -040023#include "msm_gem.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040024
Rob Clarkc8afe682013-06-26 12:44:06 -040025struct msm_framebuffer {
26 struct drm_framebuffer base;
27 const struct msm_format *format;
Stephane Viau7ca12712014-12-08 10:48:57 -050028 struct drm_gem_object *planes[MAX_PLANE];
Rob Clarkc8afe682013-06-26 12:44:06 -040029};
30#define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
31
Rob Clark6c0693b2017-07-11 10:08:05 -040032static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
33 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos);
Rob Clarkc8afe682013-06-26 12:44:06 -040034
35static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
36 struct drm_file *file_priv,
37 unsigned int *handle)
38{
39 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
40 return drm_gem_handle_create(file_priv,
41 msm_fb->planes[0], handle);
42}
43
44static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
45{
46 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020047 int i, n = fb->format->num_planes;
Rob Clarkc8afe682013-06-26 12:44:06 -040048
49 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
50
51 drm_framebuffer_cleanup(fb);
52
53 for (i = 0; i < n; i++) {
54 struct drm_gem_object *bo = msm_fb->planes[i];
Markus Elfringe73a8562016-07-13 19:15:35 +020055
56 drm_gem_object_unreference_unlocked(bo);
Rob Clarkc8afe682013-06-26 12:44:06 -040057 }
58
59 kfree(msm_fb);
60}
61
Rob Clarkc8afe682013-06-26 12:44:06 -040062static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
63 .create_handle = msm_framebuffer_create_handle,
64 .destroy = msm_framebuffer_destroy,
Rob Clarkc8afe682013-06-26 12:44:06 -040065};
66
67#ifdef CONFIG_DEBUG_FS
68void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
69{
70 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020071 int i, n = fb->format->num_planes;
Rob Clarkc8afe682013-06-26 12:44:06 -040072
73 seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +020074 fb->width, fb->height, (char *)&fb->format->format,
Dave Airlie747a5982016-04-15 15:10:35 +100075 drm_framebuffer_read_refcount(fb), fb->base.id);
Rob Clarkc8afe682013-06-26 12:44:06 -040076
77 for (i = 0; i < n; i++) {
78 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
79 i, fb->offsets[i], fb->pitches[i]);
80 msm_gem_describe(msm_fb->planes[i], m);
81 }
82}
83#endif
84
Rob Clark2638d902014-11-08 09:13:37 -050085/* prepare/pin all the fb's bo's for scanout. Note that it is not valid
86 * to prepare an fb more multiple different initiator 'id's. But that
87 * should be fine, since only the scanout (mdpN) side of things needs
88 * this, the gpu doesn't care about fb's.
89 */
Rob Clark8bdcd942017-06-13 11:07:08 -040090int msm_framebuffer_prepare(struct drm_framebuffer *fb,
91 struct msm_gem_address_space *aspace)
Rob Clark2638d902014-11-08 09:13:37 -050092{
93 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020094 int ret, i, n = fb->format->num_planes;
Rob Clark78babc12016-11-11 12:06:46 -050095 uint64_t iova;
Rob Clark2638d902014-11-08 09:13:37 -050096
97 for (i = 0; i < n; i++) {
Rob Clark8bdcd942017-06-13 11:07:08 -040098 ret = msm_gem_get_iova(msm_fb->planes[i], aspace, &iova);
Rob Clark78babc12016-11-11 12:06:46 -050099 DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
Rob Clark2638d902014-11-08 09:13:37 -0500100 if (ret)
101 return ret;
102 }
103
104 return 0;
105}
106
Rob Clark8bdcd942017-06-13 11:07:08 -0400107void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
108 struct msm_gem_address_space *aspace)
Rob Clark2638d902014-11-08 09:13:37 -0500109{
110 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200111 int i, n = fb->format->num_planes;
Rob Clark2638d902014-11-08 09:13:37 -0500112
113 for (i = 0; i < n; i++)
Rob Clark8bdcd942017-06-13 11:07:08 -0400114 msm_gem_put_iova(msm_fb->planes[i], aspace);
Rob Clark2638d902014-11-08 09:13:37 -0500115}
116
Rob Clark8bdcd942017-06-13 11:07:08 -0400117uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
118 struct msm_gem_address_space *aspace, int plane)
Rob Clark2638d902014-11-08 09:13:37 -0500119{
120 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Rob Clark3e2f29e2014-11-19 12:29:33 -0500121 if (!msm_fb->planes[plane])
122 return 0;
Rob Clark8bdcd942017-06-13 11:07:08 -0400123 return msm_gem_iova(msm_fb->planes[plane], aspace) + fb->offsets[plane];
Rob Clark2638d902014-11-08 09:13:37 -0500124}
125
Rob Clarkc8afe682013-06-26 12:44:06 -0400126struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
127{
128 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
129 return msm_fb->planes[plane];
130}
131
132const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
133{
134 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
135 return msm_fb->format;
136}
137
138struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200139 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
Rob Clarkc8afe682013-06-26 12:44:06 -0400140{
141 struct drm_gem_object *bos[4] = {0};
142 struct drm_framebuffer *fb;
143 int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
144
145 for (i = 0; i < n; i++) {
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100146 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
Rob Clarkc8afe682013-06-26 12:44:06 -0400147 if (!bos[i]) {
148 ret = -ENXIO;
149 goto out_unref;
150 }
151 }
152
153 fb = msm_framebuffer_init(dev, mode_cmd, bos);
154 if (IS_ERR(fb)) {
155 ret = PTR_ERR(fb);
156 goto out_unref;
157 }
158
159 return fb;
160
161out_unref:
162 for (i = 0; i < n; i++)
163 drm_gem_object_unreference_unlocked(bos[i]);
164 return ERR_PTR(ret);
165}
166
Rob Clark6c0693b2017-07-11 10:08:05 -0400167static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200168 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
Rob Clarkc8afe682013-06-26 12:44:06 -0400169{
170 struct msm_drm_private *priv = dev->dev_private;
171 struct msm_kms *kms = priv->kms;
Stephane Viau7194b622015-05-05 09:47:57 -0400172 struct msm_framebuffer *msm_fb = NULL;
173 struct drm_framebuffer *fb;
Rob Clarkc8afe682013-06-26 12:44:06 -0400174 const struct msm_format *format;
175 int ret, i, n;
176 unsigned int hsub, vsub;
177
178 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
179 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
180 (char *)&mode_cmd->pixel_format);
181
182 n = drm_format_num_planes(mode_cmd->pixel_format);
183 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
184 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
185
186 format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
187 if (!format) {
188 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
189 (char *)&mode_cmd->pixel_format);
190 ret = -EINVAL;
191 goto fail;
192 }
193
194 msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
195 if (!msm_fb) {
196 ret = -ENOMEM;
197 goto fail;
198 }
199
200 fb = &msm_fb->base;
201
202 msm_fb->format = format;
203
Rob Clark10291bf2014-11-08 09:20:28 -0500204 if (n > ARRAY_SIZE(msm_fb->planes)) {
205 ret = -EINVAL;
206 goto fail;
207 }
208
Rob Clarkc8afe682013-06-26 12:44:06 -0400209 for (i = 0; i < n; i++) {
210 unsigned int width = mode_cmd->width / (i ? hsub : 1);
211 unsigned int height = mode_cmd->height / (i ? vsub : 1);
212 unsigned int min_size;
213
214 min_size = (height - 1) * mode_cmd->pitches[i]
215 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
216 + mode_cmd->offsets[i];
217
218 if (bos[i]->size < min_size) {
219 ret = -EINVAL;
220 goto fail;
221 }
222
223 msm_fb->planes[i] = bos[i];
224 }
225
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200226 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
Rob Clarkc8afe682013-06-26 12:44:06 -0400227
228 ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
229 if (ret) {
230 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
231 goto fail;
232 }
233
234 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
235
236 return fb;
237
238fail:
Stephane Viau7194b622015-05-05 09:47:57 -0400239 kfree(msm_fb);
Rob Clarkc8afe682013-06-26 12:44:06 -0400240
241 return ERR_PTR(ret);
242}
Rob Clark466e5602017-07-11 10:40:13 -0400243
244struct drm_framebuffer *
245msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format)
246{
247 struct drm_mode_fb_cmd2 mode_cmd = {
248 .pixel_format = format,
249 .width = w,
250 .height = h,
251 .pitches = { p },
252 };
253 struct drm_gem_object *bo;
254 struct drm_framebuffer *fb;
255 int size;
256
257 /* allocate backing bo */
258 size = mode_cmd.pitches[0] * mode_cmd.height;
259 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
260 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN);
261 if (IS_ERR(bo)) {
262 dev_warn(dev->dev, "could not allocate stolen bo\n");
263 /* try regular bo: */
264 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
265 }
266 if (IS_ERR(bo)) {
267 dev_err(dev->dev, "failed to allocate buffer object\n");
268 return ERR_CAST(bo);
269 }
270
271 fb = msm_framebuffer_init(dev, &mode_cmd, &bo);
272 if (IS_ERR(fb)) {
273 dev_err(dev->dev, "failed to allocate fb\n");
274 /* note: if fb creation failed, we can't rely on fb destroy
275 * to unref the bo:
276 */
277 drm_gem_object_unreference_unlocked(bo);
278 return ERR_CAST(fb);
279 }
280
281 return fb;
282}