1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
7 #include <drm/drm_crtc.h>
8 #include <drm/drm_damage_helper.h>
9 #include <drm/drm_file.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
12 #include <drm/drm_probe_helper.h>
18 struct msm_framebuffer
{
19 struct drm_framebuffer base
;
20 const struct msm_format
*format
;
22 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
24 static struct drm_framebuffer
*msm_framebuffer_init(struct drm_device
*dev
,
25 const struct drm_mode_fb_cmd2
*mode_cmd
, struct drm_gem_object
**bos
);
27 static const struct drm_framebuffer_funcs msm_framebuffer_funcs
= {
28 .create_handle
= drm_gem_fb_create_handle
,
29 .destroy
= drm_gem_fb_destroy
,
30 .dirty
= drm_atomic_helper_dirtyfb
,
33 #ifdef CONFIG_DEBUG_FS
34 void msm_framebuffer_describe(struct drm_framebuffer
*fb
, struct seq_file
*m
)
36 int i
, n
= fb
->format
->num_planes
;
38 seq_printf(m
, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
39 fb
->width
, fb
->height
, (char *)&fb
->format
->format
,
40 drm_framebuffer_read_refcount(fb
), fb
->base
.id
);
42 for (i
= 0; i
< n
; i
++) {
43 seq_printf(m
, " %d: offset=%d pitch=%d, obj: ",
44 i
, fb
->offsets
[i
], fb
->pitches
[i
]);
45 msm_gem_describe(fb
->obj
[i
], m
);
50 /* prepare/pin all the fb's bo's for scanout. Note that it is not valid
51 * to prepare an fb more multiple different initiator 'id's. But that
52 * should be fine, since only the scanout (mdpN) side of things needs
53 * this, the gpu doesn't care about fb's.
55 int msm_framebuffer_prepare(struct drm_framebuffer
*fb
,
56 struct msm_gem_address_space
*aspace
)
58 int ret
, i
, n
= fb
->format
->num_planes
;
61 for (i
= 0; i
< n
; i
++) {
62 ret
= msm_gem_get_and_pin_iova(fb
->obj
[i
], aspace
, &iova
);
63 DBG("FB[%u]: iova[%d]: %08llx (%d)", fb
->base
.id
, i
, iova
, ret
);
71 void msm_framebuffer_cleanup(struct drm_framebuffer
*fb
,
72 struct msm_gem_address_space
*aspace
)
74 int i
, n
= fb
->format
->num_planes
;
76 for (i
= 0; i
< n
; i
++)
77 msm_gem_unpin_iova(fb
->obj
[i
], aspace
);
80 uint32_t msm_framebuffer_iova(struct drm_framebuffer
*fb
,
81 struct msm_gem_address_space
*aspace
, int plane
)
85 return msm_gem_iova(fb
->obj
[plane
], aspace
) + fb
->offsets
[plane
];
88 struct drm_gem_object
*msm_framebuffer_bo(struct drm_framebuffer
*fb
, int plane
)
90 return drm_gem_fb_get_obj(fb
, plane
);
93 const struct msm_format
*msm_framebuffer_format(struct drm_framebuffer
*fb
)
95 struct msm_framebuffer
*msm_fb
= to_msm_framebuffer(fb
);
96 return msm_fb
->format
;
99 struct drm_framebuffer
*msm_framebuffer_create(struct drm_device
*dev
,
100 struct drm_file
*file
, const struct drm_mode_fb_cmd2
*mode_cmd
)
102 const struct drm_format_info
*info
= drm_get_format_info(dev
,
104 struct drm_gem_object
*bos
[4] = {0};
105 struct drm_framebuffer
*fb
;
106 int ret
, i
, n
= info
->num_planes
;
108 for (i
= 0; i
< n
; i
++) {
109 bos
[i
] = drm_gem_object_lookup(file
, mode_cmd
->handles
[i
]);
116 fb
= msm_framebuffer_init(dev
, mode_cmd
, bos
);
125 for (i
= 0; i
< n
; i
++)
126 drm_gem_object_put(bos
[i
]);
130 static struct drm_framebuffer
*msm_framebuffer_init(struct drm_device
*dev
,
131 const struct drm_mode_fb_cmd2
*mode_cmd
, struct drm_gem_object
**bos
)
133 const struct drm_format_info
*info
= drm_get_format_info(dev
,
135 struct msm_drm_private
*priv
= dev
->dev_private
;
136 struct msm_kms
*kms
= priv
->kms
;
137 struct msm_framebuffer
*msm_fb
= NULL
;
138 struct drm_framebuffer
*fb
;
139 const struct msm_format
*format
;
142 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
143 dev
, mode_cmd
, mode_cmd
->width
, mode_cmd
->height
,
144 (char *)&mode_cmd
->pixel_format
);
146 n
= info
->num_planes
;
147 format
= kms
->funcs
->get_format(kms
, mode_cmd
->pixel_format
,
148 mode_cmd
->modifier
[0]);
150 DRM_DEV_ERROR(dev
->dev
, "unsupported pixel format: %4.4s\n",
151 (char *)&mode_cmd
->pixel_format
);
156 msm_fb
= kzalloc(sizeof(*msm_fb
), GFP_KERNEL
);
164 msm_fb
->format
= format
;
166 if (n
> ARRAY_SIZE(fb
->obj
)) {
171 for (i
= 0; i
< n
; i
++) {
172 unsigned int width
= mode_cmd
->width
/ (i
? info
->hsub
: 1);
173 unsigned int height
= mode_cmd
->height
/ (i
? info
->vsub
: 1);
174 unsigned int min_size
;
176 min_size
= (height
- 1) * mode_cmd
->pitches
[i
]
177 + width
* info
->cpp
[i
]
178 + mode_cmd
->offsets
[i
];
180 if (bos
[i
]->size
< min_size
) {
185 msm_fb
->base
.obj
[i
] = bos
[i
];
188 drm_helper_mode_fill_fb_struct(dev
, fb
, mode_cmd
);
190 ret
= drm_framebuffer_init(dev
, fb
, &msm_framebuffer_funcs
);
192 DRM_DEV_ERROR(dev
->dev
, "framebuffer init failed: %d\n", ret
);
196 DBG("create: FB ID: %d (%p)", fb
->base
.id
, fb
);
206 struct drm_framebuffer
*
207 msm_alloc_stolen_fb(struct drm_device
*dev
, int w
, int h
, int p
, uint32_t format
)
209 struct drm_mode_fb_cmd2 mode_cmd
= {
210 .pixel_format
= format
,
215 struct drm_gem_object
*bo
;
216 struct drm_framebuffer
*fb
;
219 /* allocate backing bo */
220 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
221 DBG("allocating %d bytes for fb %d", size
, dev
->primary
->index
);
222 bo
= msm_gem_new(dev
, size
, MSM_BO_SCANOUT
| MSM_BO_WC
| MSM_BO_STOLEN
);
224 dev_warn(dev
->dev
, "could not allocate stolen bo\n");
225 /* try regular bo: */
226 bo
= msm_gem_new(dev
, size
, MSM_BO_SCANOUT
| MSM_BO_WC
);
229 DRM_DEV_ERROR(dev
->dev
, "failed to allocate buffer object\n");
233 msm_gem_object_set_name(bo
, "stolenfb");
235 fb
= msm_framebuffer_init(dev
, &mode_cmd
, &bo
);
237 DRM_DEV_ERROR(dev
->dev
, "failed to allocate fb\n");
238 /* note: if fb creation failed, we can't rely on fb destroy
241 drm_gem_object_put(bo
);