]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/msm/msm_fb.c
c87dd3e321ec6a17c0d1ccd87a61f5dd6ce99e24
[linux.git] / drivers / gpu / drm / msm / msm_fb.c
1 /*
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
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_crtc_helper.h>
20
21 #include "msm_drv.h"
22 #include "msm_kms.h"
23 #include "msm_gem.h"
24
25 struct msm_framebuffer {
26         struct drm_framebuffer base;
27         const struct msm_format *format;
28         struct drm_gem_object *planes[MAX_PLANE];
29 };
30 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
31
32
33 static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
34                 struct drm_file *file_priv,
35                 unsigned int *handle)
36 {
37         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
38         return drm_gem_handle_create(file_priv,
39                         msm_fb->planes[0], handle);
40 }
41
42 static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
43 {
44         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
45         int i, n = fb->format->num_planes;
46
47         DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
48
49         drm_framebuffer_cleanup(fb);
50
51         for (i = 0; i < n; i++) {
52                 struct drm_gem_object *bo = msm_fb->planes[i];
53
54                 drm_gem_object_unreference_unlocked(bo);
55         }
56
57         kfree(msm_fb);
58 }
59
60 static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
61         .create_handle = msm_framebuffer_create_handle,
62         .destroy = msm_framebuffer_destroy,
63 };
64
65 #ifdef CONFIG_DEBUG_FS
66 void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
67 {
68         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
69         int i, n = fb->format->num_planes;
70
71         seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
72                         fb->width, fb->height, (char *)&fb->format->format,
73                         drm_framebuffer_read_refcount(fb), fb->base.id);
74
75         for (i = 0; i < n; i++) {
76                 seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
77                                 i, fb->offsets[i], fb->pitches[i]);
78                 msm_gem_describe(msm_fb->planes[i], m);
79         }
80 }
81 #endif
82
83 /* prepare/pin all the fb's bo's for scanout.  Note that it is not valid
84  * to prepare an fb more multiple different initiator 'id's.  But that
85  * should be fine, since only the scanout (mdpN) side of things needs
86  * this, the gpu doesn't care about fb's.
87  */
88 int msm_framebuffer_prepare(struct drm_framebuffer *fb,
89                 struct msm_gem_address_space *aspace)
90 {
91         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
92         int ret, i, n = fb->format->num_planes;
93         uint64_t iova;
94
95         for (i = 0; i < n; i++) {
96                 ret = msm_gem_get_iova(msm_fb->planes[i], aspace, &iova);
97                 DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
98                 if (ret)
99                         return ret;
100         }
101
102         return 0;
103 }
104
105 void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
106                 struct msm_gem_address_space *aspace)
107 {
108         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
109         int i, n = fb->format->num_planes;
110
111         for (i = 0; i < n; i++)
112                 msm_gem_put_iova(msm_fb->planes[i], aspace);
113 }
114
115 uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
116                 struct msm_gem_address_space *aspace, int plane)
117 {
118         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
119         if (!msm_fb->planes[plane])
120                 return 0;
121         return msm_gem_iova(msm_fb->planes[plane], aspace) + fb->offsets[plane];
122 }
123
124 struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
125 {
126         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
127         return msm_fb->planes[plane];
128 }
129
130 const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
131 {
132         struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
133         return msm_fb->format;
134 }
135
136 struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
137                 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
138 {
139         struct drm_gem_object *bos[4] = {0};
140         struct drm_framebuffer *fb;
141         int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
142
143         for (i = 0; i < n; i++) {
144                 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
145                 if (!bos[i]) {
146                         ret = -ENXIO;
147                         goto out_unref;
148                 }
149         }
150
151         fb = msm_framebuffer_init(dev, mode_cmd, bos);
152         if (IS_ERR(fb)) {
153                 ret = PTR_ERR(fb);
154                 goto out_unref;
155         }
156
157         return fb;
158
159 out_unref:
160         for (i = 0; i < n; i++)
161                 drm_gem_object_unreference_unlocked(bos[i]);
162         return ERR_PTR(ret);
163 }
164
165 struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
166                 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
167 {
168         struct msm_drm_private *priv = dev->dev_private;
169         struct msm_kms *kms = priv->kms;
170         struct msm_framebuffer *msm_fb = NULL;
171         struct drm_framebuffer *fb;
172         const struct msm_format *format;
173         int ret, i, n;
174         unsigned int hsub, vsub;
175
176         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
177                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
178                         (char *)&mode_cmd->pixel_format);
179
180         n = drm_format_num_planes(mode_cmd->pixel_format);
181         hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
182         vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
183
184         format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
185         if (!format) {
186                 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
187                                 (char *)&mode_cmd->pixel_format);
188                 ret = -EINVAL;
189                 goto fail;
190         }
191
192         msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
193         if (!msm_fb) {
194                 ret = -ENOMEM;
195                 goto fail;
196         }
197
198         fb = &msm_fb->base;
199
200         msm_fb->format = format;
201
202         if (n > ARRAY_SIZE(msm_fb->planes)) {
203                 ret = -EINVAL;
204                 goto fail;
205         }
206
207         for (i = 0; i < n; i++) {
208                 unsigned int width = mode_cmd->width / (i ? hsub : 1);
209                 unsigned int height = mode_cmd->height / (i ? vsub : 1);
210                 unsigned int min_size;
211
212                 min_size = (height - 1) * mode_cmd->pitches[i]
213                          + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
214                          + mode_cmd->offsets[i];
215
216                 if (bos[i]->size < min_size) {
217                         ret = -EINVAL;
218                         goto fail;
219                 }
220
221                 msm_fb->planes[i] = bos[i];
222         }
223
224         drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
225
226         ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
227         if (ret) {
228                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
229                 goto fail;
230         }
231
232         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
233
234         return fb;
235
236 fail:
237         kfree(msm_fb);
238
239         return ERR_PTR(ret);
240 }
241
242 struct drm_framebuffer *
243 msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format)
244 {
245         struct drm_mode_fb_cmd2 mode_cmd = {
246                 .pixel_format = format,
247                 .width = w,
248                 .height = h,
249                 .pitches = { p },
250         };
251         struct drm_gem_object *bo;
252         struct drm_framebuffer *fb;
253         int size;
254
255         /* allocate backing bo */
256         size = mode_cmd.pitches[0] * mode_cmd.height;
257         DBG("allocating %d bytes for fb %d", size, dev->primary->index);
258         bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN);
259         if (IS_ERR(bo)) {
260                 dev_warn(dev->dev, "could not allocate stolen bo\n");
261                 /* try regular bo: */
262                 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
263         }
264         if (IS_ERR(bo)) {
265                 dev_err(dev->dev, "failed to allocate buffer object\n");
266                 return ERR_CAST(bo);
267         }
268
269         fb = msm_framebuffer_init(dev, &mode_cmd, &bo);
270         if (IS_ERR(fb)) {
271                 dev_err(dev->dev, "failed to allocate fb\n");
272                 /* note: if fb creation failed, we can't rely on fb destroy
273                  * to unref the bo:
274                  */
275                 drm_gem_object_unreference_unlocked(bo);
276                 return ERR_CAST(fb);
277         }
278
279         return fb;
280 }