2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
26 struct msm_framebuffer {
27 struct drm_framebuffer base;
28 const struct msm_format *format;
30 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
32 static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
33 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos);
35 static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
36 .create_handle = drm_gem_fb_create_handle,
37 .destroy = drm_gem_fb_destroy,
40 #ifdef CONFIG_DEBUG_FS
41 void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
43 int i, n = fb->format->num_planes;
45 seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
46 fb->width, fb->height, (char *)&fb->format->format,
47 drm_framebuffer_read_refcount(fb), fb->base.id);
49 for (i = 0; i < n; i++) {
50 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
51 i, fb->offsets[i], fb->pitches[i]);
52 msm_gem_describe(fb->obj[i], m);
57 /* prepare/pin all the fb's bo's for scanout. Note that it is not valid
58 * to prepare an fb more multiple different initiator 'id's. But that
59 * should be fine, since only the scanout (mdpN) side of things needs
60 * this, the gpu doesn't care about fb's.
62 int msm_framebuffer_prepare(struct drm_framebuffer *fb,
63 struct msm_gem_address_space *aspace)
65 int ret, i, n = fb->format->num_planes;
68 for (i = 0; i < n; i++) {
69 ret = msm_gem_get_iova(fb->obj[i], aspace, &iova);
70 DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
78 void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
79 struct msm_gem_address_space *aspace)
81 int i, n = fb->format->num_planes;
83 for (i = 0; i < n; i++)
84 msm_gem_put_iova(fb->obj[i], aspace);
87 uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
88 struct msm_gem_address_space *aspace, int plane)
92 return msm_gem_iova(fb->obj[plane], aspace) + fb->offsets[plane];
95 struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
97 return drm_gem_fb_get_obj(fb, plane);
100 const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
102 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
103 return msm_fb->format;
106 struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
107 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
109 struct drm_gem_object *bos[4] = {0};
110 struct drm_framebuffer *fb;
111 int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
113 for (i = 0; i < n; i++) {
114 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
121 fb = msm_framebuffer_init(dev, mode_cmd, bos);
130 for (i = 0; i < n; i++)
131 drm_gem_object_put_unlocked(bos[i]);
135 static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
136 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
138 struct msm_drm_private *priv = dev->dev_private;
139 struct msm_kms *kms = priv->kms;
140 struct msm_framebuffer *msm_fb = NULL;
141 struct drm_framebuffer *fb;
142 const struct msm_format *format;
144 unsigned int hsub, vsub;
146 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
147 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
148 (char *)&mode_cmd->pixel_format);
150 n = drm_format_num_planes(mode_cmd->pixel_format);
151 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
152 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
154 format = kms->funcs->get_format(kms, mode_cmd->pixel_format,
155 mode_cmd->modifier[0]);
157 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
158 (char *)&mode_cmd->pixel_format);
163 msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
171 msm_fb->format = format;
173 if (n > ARRAY_SIZE(fb->obj)) {
178 for (i = 0; i < n; i++) {
179 unsigned int width = mode_cmd->width / (i ? hsub : 1);
180 unsigned int height = mode_cmd->height / (i ? vsub : 1);
181 unsigned int min_size;
183 min_size = (height - 1) * mode_cmd->pitches[i]
184 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
185 + mode_cmd->offsets[i];
187 if (bos[i]->size < min_size) {
192 msm_fb->base.obj[i] = bos[i];
195 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
197 ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
199 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
203 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
213 struct drm_framebuffer *
214 msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format)
216 struct drm_mode_fb_cmd2 mode_cmd = {
217 .pixel_format = format,
222 struct drm_gem_object *bo;
223 struct drm_framebuffer *fb;
226 /* allocate backing bo */
227 size = mode_cmd.pitches[0] * mode_cmd.height;
228 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
229 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN);
231 dev_warn(dev->dev, "could not allocate stolen bo\n");
232 /* try regular bo: */
233 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
236 dev_err(dev->dev, "failed to allocate buffer object\n");
240 fb = msm_framebuffer_init(dev, &mode_cmd, &bo);
242 dev_err(dev->dev, "failed to allocate fb\n");
243 /* note: if fb creation failed, we can't rely on fb destroy
246 drm_gem_object_put_unlocked(bo);