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