msm_fb.c 7.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2013 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

18 19 20
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>

21
#include "msm_drv.h"
Rob Clark's avatar
Rob Clark committed
22
#include "msm_kms.h"
23
#include "msm_gem.h"
24 25 26 27

struct msm_framebuffer {
	struct drm_framebuffer base;
	const struct msm_format *format;
28
	struct drm_gem_object *planes[MAX_PLANE];
29 30 31
};
#define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)

32 33
static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
		const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos);
34 35 36 37 38 39 40 41 42 43 44 45 46

static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
		struct drm_file *file_priv,
		unsigned int *handle)
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
	return drm_gem_handle_create(file_priv,
			msm_fb->planes[0], handle);
}

static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
47
	int i, n = fb->format->num_planes;
48 49 50 51 52 53 54

	DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);

	drm_framebuffer_cleanup(fb);

	for (i = 0; i < n; i++) {
		struct drm_gem_object *bo = msm_fb->planes[i];
55 56

		drm_gem_object_unreference_unlocked(bo);
57 58 59 60 61 62 63 64 65 66 67 68 69 70
	}

	kfree(msm_fb);
}

static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
	.create_handle = msm_framebuffer_create_handle,
	.destroy = msm_framebuffer_destroy,
};

#ifdef CONFIG_DEBUG_FS
void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
{
	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 78 79 80 81 82 83 84

	for (i = 0; i < n; i++) {
		seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
				i, fb->offsets[i], fb->pitches[i]);
		msm_gem_describe(msm_fb->planes[i], m);
	}
}
#endif

Rob Clark's avatar
Rob Clark committed
85 86 87 88 89
/* prepare/pin all the fb's bo's for scanout.  Note that it is not valid
 * to prepare an fb more multiple different initiator 'id's.  But that
 * should be fine, since only the scanout (mdpN) side of things needs
 * this, the gpu doesn't care about fb's.
 */
90 91
int msm_framebuffer_prepare(struct drm_framebuffer *fb,
		struct msm_gem_address_space *aspace)
Rob Clark's avatar
Rob Clark committed
92 93
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
94
	int ret, i, n = fb->format->num_planes;
Rob Clark's avatar
Rob Clark committed
95
	uint64_t iova;
Rob Clark's avatar
Rob Clark committed
96 97

	for (i = 0; i < n; i++) {
98
		ret = msm_gem_get_iova(msm_fb->planes[i], aspace, &iova);
Rob Clark's avatar
Rob Clark committed
99
		DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
Rob Clark's avatar
Rob Clark committed
100 101 102 103 104 105 106
		if (ret)
			return ret;
	}

	return 0;
}

107 108
void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
		struct msm_gem_address_space *aspace)
Rob Clark's avatar
Rob Clark committed
109 110
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
111
	int i, n = fb->format->num_planes;
Rob Clark's avatar
Rob Clark committed
112 113

	for (i = 0; i < n; i++)
114
		msm_gem_put_iova(msm_fb->planes[i], aspace);
Rob Clark's avatar
Rob Clark committed
115 116
}

117 118
uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
		struct msm_gem_address_space *aspace, int plane)
Rob Clark's avatar
Rob Clark committed
119 120
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
Rob Clark's avatar
Rob Clark committed
121 122
	if (!msm_fb->planes[plane])
		return 0;
123
	return msm_gem_iova(msm_fb->planes[plane], aspace) + fb->offsets[plane];
Rob Clark's avatar
Rob Clark committed
124 125
}

126 127 128 129 130 131 132 133 134 135 136 137 138
struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
	return msm_fb->planes[plane];
}

const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
{
	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
	return msm_fb->format;
}

struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
139
		struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
140 141 142 143 144 145
{
	struct drm_gem_object *bos[4] = {0};
	struct drm_framebuffer *fb;
	int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);

	for (i = 0; i < n; i++) {
146
		bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		if (!bos[i]) {
			ret = -ENXIO;
			goto out_unref;
		}
	}

	fb = msm_framebuffer_init(dev, mode_cmd, bos);
	if (IS_ERR(fb)) {
		ret = PTR_ERR(fb);
		goto out_unref;
	}

	return fb;

out_unref:
	for (i = 0; i < n; i++)
		drm_gem_object_unreference_unlocked(bos[i]);
	return ERR_PTR(ret);
}

167
static 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 171
{
	struct msm_drm_private *priv = dev->dev_private;
	struct msm_kms *kms = priv->kms;
172 173
	struct msm_framebuffer *msm_fb = NULL;
	struct drm_framebuffer *fb;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	const struct msm_format *format;
	int ret, i, n;
	unsigned int hsub, vsub;

	DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
			dev, mode_cmd, mode_cmd->width, mode_cmd->height,
			(char *)&mode_cmd->pixel_format);

	n = drm_format_num_planes(mode_cmd->pixel_format);
	hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
	vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);

	format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
	if (!format) {
		dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
				(char *)&mode_cmd->pixel_format);
		ret = -EINVAL;
		goto fail;
	}

	msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
	if (!msm_fb) {
		ret = -ENOMEM;
		goto fail;
	}

	fb = &msm_fb->base;

	msm_fb->format = format;

204 205 206 207 208
	if (n > ARRAY_SIZE(msm_fb->planes)) {
		ret = -EINVAL;
		goto fail;
	}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	for (i = 0; i < n; i++) {
		unsigned int width = mode_cmd->width / (i ? hsub : 1);
		unsigned int height = mode_cmd->height / (i ? vsub : 1);
		unsigned int min_size;

		min_size = (height - 1) * mode_cmd->pitches[i]
			 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
			 + mode_cmd->offsets[i];

		if (bos[i]->size < min_size) {
			ret = -EINVAL;
			goto fail;
		}

		msm_fb->planes[i] = bos[i];
	}

226
	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
227 228 229 230 231 232 233 234 235 236 237 238

	ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
	if (ret) {
		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
		goto fail;
	}

	DBG("create: FB ID: %d (%p)", fb->base.id, fb);

	return fb;

fail:
239
	kfree(msm_fb);
240 241 242

	return ERR_PTR(ret);
}
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

struct drm_framebuffer *
msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format)
{
	struct drm_mode_fb_cmd2 mode_cmd = {
		.pixel_format = format,
		.width = w,
		.height = h,
		.pitches = { p },
	};
	struct drm_gem_object *bo;
	struct drm_framebuffer *fb;
	int size;

	/* allocate backing bo */
	size = mode_cmd.pitches[0] * mode_cmd.height;
	DBG("allocating %d bytes for fb %d", size, dev->primary->index);
	bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN);
	if (IS_ERR(bo)) {
		dev_warn(dev->dev, "could not allocate stolen bo\n");
		/* try regular bo: */
		bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
	}
	if (IS_ERR(bo)) {
		dev_err(dev->dev, "failed to allocate buffer object\n");
		return ERR_CAST(bo);
	}

	fb = msm_framebuffer_init(dev, &mode_cmd, &bo);
	if (IS_ERR(fb)) {
		dev_err(dev->dev, "failed to allocate fb\n");
		/* note: if fb creation failed, we can't rely on fb destroy
		 * to unref the bo:
		 */
		drm_gem_object_unreference_unlocked(bo);
		return ERR_CAST(fb);
	}

	return fb;
}