fb.c 10.2 KB
Newer Older
T
Thierry Reding 已提交
1
/*
2
 * Copyright (C) 2012-2013 Avionic Design GmbH
T
Thierry Reding 已提交
3 4
 * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
 *
5 6 7
 * Based on the KMS/FB CMA helpers
 *   Copyright (C) 2012 Analog Device Inc.
 *
T
Thierry Reding 已提交
8 9 10 11 12 13
 * 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.
 */

#include "drm.h"
14 15 16 17 18 19 20
#include "gem.h"

static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
{
	return container_of(fb, struct tegra_fb, base);
}

21
#ifdef CONFIG_DRM_TEGRA_FBDEV
22 23 24 25
static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
{
	return container_of(helper, struct tegra_fbdev, base);
}
26
#endif
27 28 29 30 31 32 33 34 35 36 37 38

struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
				    unsigned int index)
{
	struct tegra_fb *fb = to_tegra_fb(framebuffer);

	if (index >= drm_format_num_planes(framebuffer->pixel_format))
		return NULL;

	return fb->planes[index];
}

39 40 41 42 43 44 45 46 47 48
bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
{
	struct tegra_fb *fb = to_tegra_fb(framebuffer);

	if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
		return true;

	return false;
}

49 50
int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
			struct tegra_bo_tiling *tiling)
51 52 53
{
	struct tegra_fb *fb = to_tegra_fb(framebuffer);

54 55
	/* TODO: handle YUV formats? */
	*tiling = fb->planes[0]->tiling;
56

57
	return 0;
58 59
}

60 61 62 63 64 65 66 67
static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
{
	struct tegra_fb *fb = to_tegra_fb(framebuffer);
	unsigned int i;

	for (i = 0; i < fb->num_planes; i++) {
		struct tegra_bo *bo = fb->planes[i];

T
Thierry Reding 已提交
68 69 70 71
		if (bo) {
			if (bo->pages && bo->vaddr)
				vunmap(bo->vaddr);

72
			drm_gem_object_unreference_unlocked(&bo->gem);
T
Thierry Reding 已提交
73
		}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	}

	drm_framebuffer_cleanup(framebuffer);
	kfree(fb->planes);
	kfree(fb);
}

static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
				  struct drm_file *file, unsigned int *handle)
{
	struct tegra_fb *fb = to_tegra_fb(framebuffer);

	return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
}

static struct drm_framebuffer_funcs tegra_fb_funcs = {
	.destroy = tegra_fb_destroy,
	.create_handle = tegra_fb_create_handle,
};

static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
				       struct drm_mode_fb_cmd2 *mode_cmd,
				       struct tegra_bo **planes,
				       unsigned int num_planes)
{
	struct tegra_fb *fb;
	unsigned int i;
	int err;

	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
	if (!fb)
		return ERR_PTR(-ENOMEM);

	fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
108 109
	if (!fb->planes) {
		kfree(fb);
110
		return ERR_PTR(-ENOMEM);
111
	}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	fb->num_planes = num_planes;

	drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);

	for (i = 0; i < fb->num_planes; i++)
		fb->planes[i] = planes[i];

	err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
	if (err < 0) {
		dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
			err);
		kfree(fb->planes);
		kfree(fb);
		return ERR_PTR(err);
	}

	return fb;
}

static struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
					       struct drm_file *file,
					       struct drm_mode_fb_cmd2 *cmd)
{
	unsigned int hsub, vsub, i;
	struct tegra_bo *planes[4];
	struct drm_gem_object *gem;
	struct tegra_fb *fb;
	int err;

	hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
	vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);

	for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
		unsigned int width = cmd->width / (i ? hsub : 1);
		unsigned int height = cmd->height / (i ? vsub : 1);
		unsigned int size, bpp;

		gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
		if (!gem) {
			err = -ENXIO;
			goto unreference;
		}

		bpp = drm_format_plane_cpp(cmd->pixel_format, i);

		size = (height - 1) * cmd->pitches[i] +
		       width * bpp + cmd->offsets[i];

		if (gem->size < size) {
			err = -EINVAL;
			goto unreference;
		}

		planes[i] = to_tegra_bo(gem);
	}

	fb = tegra_fb_alloc(drm, cmd, planes, i);
	if (IS_ERR(fb)) {
		err = PTR_ERR(fb);
		goto unreference;
	}

	return &fb->base;

unreference:
	while (i--)
		drm_gem_object_unreference_unlocked(&planes[i]->gem);
T
Thierry Reding 已提交
180

181 182 183
	return ERR_PTR(err);
}

184
#ifdef CONFIG_DRM_TEGRA_FBDEV
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static struct fb_ops tegra_fb_ops = {
	.owner = THIS_MODULE,
	.fb_fillrect = sys_fillrect,
	.fb_copyarea = sys_copyarea,
	.fb_imageblit = sys_imageblit,
	.fb_check_var = drm_fb_helper_check_var,
	.fb_set_par = drm_fb_helper_set_par,
	.fb_blank = drm_fb_helper_blank,
	.fb_pan_display = drm_fb_helper_pan_display,
	.fb_setcmap = drm_fb_helper_setcmap,
};

static int tegra_fbdev_probe(struct drm_fb_helper *helper,
			     struct drm_fb_helper_surface_size *sizes)
{
	struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
201
	struct tegra_drm *tegra = helper->dev->dev_private;
202 203 204 205 206 207 208 209 210 211 212 213 214 215
	struct drm_device *drm = helper->dev;
	struct drm_mode_fb_cmd2 cmd = { 0 };
	unsigned int bytes_per_pixel;
	struct drm_framebuffer *fb;
	unsigned long offset;
	struct fb_info *info;
	struct tegra_bo *bo;
	size_t size;
	int err;

	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);

	cmd.width = sizes->surface_width;
	cmd.height = sizes->surface_height;
216 217
	cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
				  tegra->pitch_align);
218 219 220 221 222
	cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
						     sizes->surface_depth);

	size = cmd.pitches[0] * cmd.height;

223
	bo = tegra_bo_create(drm, size, 0);
224 225 226 227 228 229
	if (IS_ERR(bo))
		return PTR_ERR(bo);

	info = framebuffer_alloc(0, drm->dev);
	if (!info) {
		dev_err(drm->dev, "failed to allocate framebuffer info\n");
230
		drm_gem_object_unreference_unlocked(&bo->gem);
231 232 233 234 235 236
		return -ENOMEM;
	}

	fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
	if (IS_ERR(fbdev->fb)) {
		err = PTR_ERR(fbdev->fb);
237 238
		dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
			err);
239
		drm_gem_object_unreference_unlocked(&bo->gem);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		goto release;
	}

	fb = &fbdev->fb->base;
	helper->fb = fb;
	helper->fbdev = info;

	info->par = helper;
	info->flags = FBINFO_FLAG_DEFAULT;
	info->fbops = &tegra_fb_ops;

	err = fb_alloc_cmap(&info->cmap, 256, 0);
	if (err < 0) {
		dev_err(drm->dev, "failed to allocate color map: %d\n", err);
		goto destroy;
	}

	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
	drm_fb_helper_fill_var(info, helper, fb->width, fb->height);

	offset = info->var.xoffset * bytes_per_pixel +
		 info->var.yoffset * fb->pitches[0];

T
Thierry Reding 已提交
263 264 265 266 267 268 269 270 271 272
	if (bo->pages) {
		bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
				 pgprot_writecombine(PAGE_KERNEL));
		if (!bo->vaddr) {
			dev_err(drm->dev, "failed to vmap() framebuffer\n");
			err = -ENOMEM;
			goto destroy;
		}
	}

273
	drm->mode_config.fb_base = (resource_size_t)bo->paddr;
274
	info->screen_base = (void __iomem *)bo->vaddr + offset;
275 276 277 278 279 280 281 282 283 284 285 286 287 288
	info->screen_size = size;
	info->fix.smem_start = (unsigned long)(bo->paddr + offset);
	info->fix.smem_len = size;

	return 0;

destroy:
	drm_framebuffer_unregister_private(fb);
	tegra_fb_destroy(fb);
release:
	framebuffer_release(info);
	return err;
}

289
static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
290 291 292
	.fb_probe = tegra_fbdev_probe,
};

293
static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
294 295 296 297 298 299 300 301 302
{
	struct tegra_fbdev *fbdev;

	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
	if (!fbdev) {
		dev_err(drm->dev, "failed to allocate DRM fbdev\n");
		return ERR_PTR(-ENOMEM);
	}

303
	drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
304

305 306 307
	return fbdev;
}

308 309 310 311 312
static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
{
	kfree(fbdev);
}

313 314 315 316 317 318 319 320
static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
			    unsigned int preferred_bpp,
			    unsigned int num_crtc,
			    unsigned int max_connectors)
{
	struct drm_device *drm = fbdev->base.dev;
	int err;

321 322
	err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
	if (err < 0) {
323 324
		dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
			err);
325
		return err;
326 327 328 329
	}

	err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
	if (err < 0) {
330
		dev_err(drm->dev, "failed to add connectors: %d\n", err);
331 332 333 334 335
		goto fini;
	}

	err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
	if (err < 0) {
336 337
		dev_err(drm->dev, "failed to set initial configuration: %d\n",
			err);
338 339 340
		goto fini;
	}

341
	return 0;
342 343 344

fini:
	drm_fb_helper_fini(&fbdev->base);
345
	return err;
346 347
}

348
static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
{
	struct fb_info *info = fbdev->base.fbdev;

	if (info) {
		int err;

		err = unregister_framebuffer(info);
		if (err < 0)
			DRM_DEBUG_KMS("failed to unregister framebuffer\n");

		if (info->cmap.len)
			fb_dealloc_cmap(&info->cmap);

		framebuffer_release(info);
	}

	if (fbdev->fb) {
		drm_framebuffer_unregister_private(&fbdev->fb->base);
367
		drm_framebuffer_remove(&fbdev->fb->base);
368 369 370
	}

	drm_fb_helper_fini(&fbdev->base);
371
	tegra_fbdev_free(fbdev);
372 373
}

374 375
void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
{
376 377
	if (fbdev)
		drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
378 379
}

380
static void tegra_fb_output_poll_changed(struct drm_device *drm)
T
Thierry Reding 已提交
381
{
382
	struct tegra_drm *tegra = drm->dev_private;
T
Thierry Reding 已提交
383

384 385
	if (tegra->fbdev)
		drm_fb_helper_hotplug_event(&tegra->fbdev->base);
T
Thierry Reding 已提交
386
}
387
#endif
T
Thierry Reding 已提交
388 389

static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
390
	.fb_create = tegra_fb_create,
391
#ifdef CONFIG_DRM_TEGRA_FBDEV
392
	.output_poll_changed = tegra_fb_output_poll_changed,
393
#endif
T
Thierry Reding 已提交
394 395
};

396
int tegra_drm_fb_prepare(struct drm_device *drm)
T
Thierry Reding 已提交
397
{
398
#ifdef CONFIG_DRM_TEGRA_FBDEV
399
	struct tegra_drm *tegra = drm->dev_private;
400
#endif
T
Thierry Reding 已提交
401 402 403 404 405 406 407 408 409

	drm->mode_config.min_width = 0;
	drm->mode_config.min_height = 0;

	drm->mode_config.max_width = 4096;
	drm->mode_config.max_height = 4096;

	drm->mode_config.funcs = &tegra_drm_mode_funcs;

410
#ifdef CONFIG_DRM_TEGRA_FBDEV
411
	tegra->fbdev = tegra_fbdev_create(drm);
412 413 414
	if (IS_ERR(tegra->fbdev))
		return PTR_ERR(tegra->fbdev);
#endif
T
Thierry Reding 已提交
415 416 417 418

	return 0;
}

419 420 421 422 423 424 425 426 427
void tegra_drm_fb_free(struct drm_device *drm)
{
#ifdef CONFIG_DRM_TEGRA_FBDEV
	struct tegra_drm *tegra = drm->dev_private;

	tegra_fbdev_free(tegra->fbdev);
#endif
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
int tegra_drm_fb_init(struct drm_device *drm)
{
#ifdef CONFIG_DRM_TEGRA_FBDEV
	struct tegra_drm *tegra = drm->dev_private;
	int err;

	err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
			       drm->mode_config.num_connector);
	if (err < 0)
		return err;
#endif

	return 0;
}

T
Thierry Reding 已提交
443 444
void tegra_drm_fb_exit(struct drm_device *drm)
{
445
#ifdef CONFIG_DRM_TEGRA_FBDEV
446
	struct tegra_drm *tegra = drm->dev_private;
T
Thierry Reding 已提交
447

448
	tegra_fbdev_exit(tegra->fbdev);
449
#endif
T
Thierry Reding 已提交
450
}