exynos_drm_fb.c 4.84 KB
Newer Older
1 2 3 4 5 6 7 8
/* exynos_drm_fb.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 * Authors:
 *	Inki Dae <inki.dae@samsung.com>
 *	Joonyoung Shim <jy0922.shim@samsung.com>
 *	Seung-Woo Kim <sw0312.kim@samsung.com>
 *
9 10 11 12
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
13 14
 */

15 16 17 18
#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_fb_helper.h>
19
#include <drm/drm_atomic.h>
20
#include <drm/drm_atomic_helper.h>
21
#include <drm/drm_gem_framebuffer_helper.h>
22
#include <uapi/drm/exynos_drm.h>
23

24
#include "exynos_drm_drv.h"
25
#include "exynos_drm_fb.h"
26
#include "exynos_drm_fbdev.h"
27
#include "exynos_drm_iommu.h"
28
#include "exynos_drm_crtc.h"
29 30 31 32 33 34 35

#define to_exynos_fb(x)	container_of(x, struct exynos_drm_fb, fb)

/*
 * exynos specific framebuffer structure.
 *
 * @fb: drm framebuffer obejct.
36
 * @exynos_gem: array of exynos specific gem object containing a gem object.
37 38
 */
struct exynos_drm_fb {
39
	struct drm_framebuffer	fb;
40 41
};

42
static int check_fb_gem_memory_type(struct drm_device *drm_dev,
43
				    struct exynos_drm_gem *exynos_gem)
44 45 46 47 48 49 50 51 52 53
{
	unsigned int flags;

	/*
	 * if exynos drm driver supports iommu then framebuffer can use
	 * all the buffer types.
	 */
	if (is_drm_iommu_supported(drm_dev))
		return 0;

54
	flags = exynos_gem->flags;
55 56

	/*
57 58
	 * Physically non-contiguous memory type for framebuffer is not
	 * supported without IOMMU.
59 60
	 */
	if (IS_NONCONTIG_BUFFER(flags)) {
61
		DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
62 63 64 65 66 67
		return -EINVAL;
	}

	return 0;
}

68
static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
69 70
	.destroy	= drm_gem_fb_destroy,
	.create_handle	= drm_gem_fb_create_handle,
71 72
};

73 74
struct drm_framebuffer *
exynos_drm_framebuffer_init(struct drm_device *dev,
75
			    const struct drm_mode_fb_cmd2 *mode_cmd,
76
			    struct exynos_drm_gem **exynos_gem,
77
			    int count)
78 79
{
	struct exynos_drm_fb *exynos_fb;
80
	int i;
81 82 83
	int ret;

	exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
84
	if (!exynos_fb)
85 86
		return ERR_PTR(-ENOMEM);

87
	for (i = 0; i < count; i++) {
88
		ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
89 90 91
		if (ret < 0)
			goto err;

92
		exynos_fb->fb.obj[i] = &exynos_gem[i]->base;
93 94
	}

95
	drm_helper_mode_fill_fb_struct(dev, &exynos_fb->fb, mode_cmd);
96

97
	ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
98
	if (ret < 0) {
99
		DRM_ERROR("failed to initialize framebuffer\n");
100
		goto err;
101 102
	}

103
	return &exynos_fb->fb;
104 105 106 107

err:
	kfree(exynos_fb);
	return ERR_PTR(ret);
108 109
}

110 111
static struct drm_framebuffer *
exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
112
		      const struct drm_mode_fb_cmd2 *mode_cmd)
113
{
114
	const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
115
	struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
116
	struct drm_gem_object *obj;
117 118 119
	struct drm_framebuffer *fb;
	int i;
	int ret;
120

121 122 123 124 125 126
	for (i = 0; i < info->num_planes; i++) {
		unsigned int height = (i == 0) ? mode_cmd->height :
				     DIV_ROUND_UP(mode_cmd->height, info->vsub);
		unsigned long size = height * mode_cmd->pitches[i] +
				     mode_cmd->offsets[i];

127
		obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
128 129
		if (!obj) {
			DRM_ERROR("failed to lookup gem object\n");
130
			ret = -ENOENT;
131
			goto err;
132 133
		}

134
		exynos_gem[i] = to_exynos_gem(obj);
135 136 137 138 139 140

		if (size > exynos_gem[i]->size) {
			i++;
			ret = -EINVAL;
			goto err;
		}
141 142
	}

143
	fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
144 145
	if (IS_ERR(fb)) {
		ret = PTR_ERR(fb);
146
		goto err;
147 148
	}

149
	return fb;
150

151
err:
152
	while (i--)
153
		drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
154 155

	return ERR_PTR(ret);
156 157
}

158
dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
159
{
160
	struct exynos_drm_gem *exynos_gem;
161

162 163
	if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
		return 0;
164

165 166
	exynos_gem = to_exynos_gem(fb->obj[index]);
	return exynos_gem->dma_addr + fb->offsets[index];
167 168
}

169
static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
170
	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
171 172
};

173
static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
174
	.fb_create = exynos_user_fb_create,
175
	.output_poll_changed = drm_fb_helper_output_poll_changed,
176
	.atomic_check = exynos_atomic_check,
177
	.atomic_commit = drm_atomic_helper_commit,
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
};

void exynos_drm_mode_config_init(struct drm_device *dev)
{
	dev->mode_config.min_width = 0;
	dev->mode_config.min_height = 0;

	/*
	 * set max width and height as default value(4096x4096).
	 * this value would be used to check framebuffer size limitation
	 * at drm_mode_addfb().
	 */
	dev->mode_config.max_width = 4096;
	dev->mode_config.max_height = 4096;

	dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
194
	dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
195 196

	dev->mode_config.allow_fb_modifiers = true;
197
}