exynos_drm_gem.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/* exynos_drm_gem.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 * Author: Inki Dae <inki.dae@samsung.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "drmP.h"
#include "drm.h"

#include <drm/exynos_drm.h>

#include "exynos_drm_drv.h"
#include "exynos_drm_gem.h"
#include "exynos_drm_buf.h"

static unsigned int convert_to_vm_err_msg(int msg)
{
	unsigned int out_msg;

	switch (msg) {
	case 0:
	case -ERESTARTSYS:
	case -EINTR:
		out_msg = VM_FAULT_NOPAGE;
		break;

	case -ENOMEM:
		out_msg = VM_FAULT_OOM;
		break;

	default:
		out_msg = VM_FAULT_SIGBUS;
		break;
	}

	return out_msg;
}

J
Joonyoung Shim 已提交
58 59 60
static struct exynos_drm_gem_obj *
exynos_drm_gem_init(struct drm_device *drm_dev, struct drm_file *file_priv,
		    unsigned int *handle, unsigned int size)
61 62 63 64 65 66 67 68 69 70 71 72 73
{
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_gem_object *obj;
	int ret;

	exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
	if (!exynos_gem_obj) {
		DRM_ERROR("failed to allocate exynos gem object.\n");
		return ERR_PTR(-ENOMEM);
	}

	obj = &exynos_gem_obj->base;

74
	ret = drm_gem_object_init(drm_dev, obj, size);
75
	if (ret < 0) {
76 77
		DRM_ERROR("failed to initialize gem object.\n");
		ret = -EINVAL;
J
Joonyoung Shim 已提交
78
		goto err;
79 80 81 82 83 84 85 86 87 88
	}

	DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);

	/*
	 * allocate a id of idr table where the obj is registered
	 * and handle has the id what user can see.
	 */
	ret = drm_gem_handle_create(file_priv, obj, handle);
	if (ret)
89
		goto err_release;
90 91 92 93 94 95 96 97

	DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);

	/* drop reference from allocate - handle holds it now. */
	drm_gem_object_unreference_unlocked(obj);

	return exynos_gem_obj;

J
Joonyoung Shim 已提交
98
err_release:
99 100
	drm_gem_object_release(obj);

J
Joonyoung Shim 已提交
101
err:
102 103 104 105
	kfree(exynos_gem_obj);
	return ERR_PTR(ret);
}

106
struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
J
Joonyoung Shim 已提交
107 108 109
						 struct drm_file *file_priv,
						 unsigned int *handle,
						 unsigned long size)
110 111
{

J
Joonyoung Shim 已提交
112
	struct exynos_drm_gem_obj *exynos_gem_obj;
I
Inki Dae 已提交
113
	struct exynos_drm_gem_buf *buffer;
114 115 116 117

	size = roundup(size, PAGE_SIZE);
	DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);

I
Inki Dae 已提交
118
	buffer = exynos_drm_buf_create(dev, size);
J
Joonyoung Shim 已提交
119 120
	if (!buffer)
		return ERR_PTR(-ENOMEM);
121 122 123

	exynos_gem_obj = exynos_drm_gem_init(dev, file_priv, handle, size);
	if (IS_ERR(exynos_gem_obj)) {
124 125
		exynos_drm_buf_destroy(dev, buffer);
		return exynos_gem_obj;
126 127
	}

I
Inki Dae 已提交
128
	exynos_gem_obj->buffer = buffer;
129 130 131 132

	return exynos_gem_obj;
}

133
int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
J
Joonyoung Shim 已提交
134
				struct drm_file *file_priv)
135 136
{
	struct drm_exynos_gem_create *args = data;
J
Joonyoung Shim 已提交
137
	struct exynos_drm_gem_obj *exynos_gem_obj;
138

139
	DRM_DEBUG_KMS("%s\n", __FILE__);
140

J
Joonyoung Shim 已提交
141 142
	exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle,
			args->size);
143 144 145 146 147 148 149
	if (IS_ERR(exynos_gem_obj))
		return PTR_ERR(exynos_gem_obj);

	return 0;
}

int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
J
Joonyoung Shim 已提交
150
				    struct drm_file *file_priv)
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
{
	struct drm_exynos_gem_map_off *args = data;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
			args->handle, (unsigned long)args->offset);

	if (!(dev->driver->driver_features & DRIVER_GEM)) {
		DRM_ERROR("does not support GEM.\n");
		return -ENODEV;
	}

	return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
			&args->offset);
}

static int exynos_drm_gem_mmap_buffer(struct file *filp,
J
Joonyoung Shim 已提交
169
				      struct vm_area_struct *vma)
170 171 172
{
	struct drm_gem_object *obj = filp->private_data;
	struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
I
Inki Dae 已提交
173
	struct exynos_drm_gem_buf *buffer;
174 175 176 177 178 179 180 181 182 183 184
	unsigned long pfn, vm_size;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	vma->vm_flags |= (VM_IO | VM_RESERVED);

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	vma->vm_file = filp;

	vm_size = vma->vm_end - vma->vm_start;
	/*
I
Inki Dae 已提交
185
	 * a buffer contains information to physically continuous memory
186 187
	 * allocated by user request or at framebuffer creation.
	 */
I
Inki Dae 已提交
188
	buffer = exynos_gem_obj->buffer;
189 190

	/* check if user-requested size is valid. */
I
Inki Dae 已提交
191
	if (vm_size > buffer->size)
192 193 194 195 196 197
		return -EINVAL;

	/*
	 * get page frame number to physical memory to be mapped
	 * to user space.
	 */
I
Inki Dae 已提交
198
	pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> PAGE_SHIFT;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);

	if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
				vma->vm_page_prot)) {
		DRM_ERROR("failed to remap pfn range.\n");
		return -EAGAIN;
	}

	return 0;
}

static const struct file_operations exynos_drm_gem_fops = {
	.mmap = exynos_drm_gem_mmap_buffer,
};

int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
J
Joonyoung Shim 已提交
216
			      struct drm_file *file_priv)
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
{
	struct drm_exynos_gem_mmap *args = data;
	struct drm_gem_object *obj;
	unsigned int addr;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	if (!(dev->driver->driver_features & DRIVER_GEM)) {
		DRM_ERROR("does not support GEM.\n");
		return -ENODEV;
	}

	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
	if (!obj) {
		DRM_ERROR("failed to lookup gem object.\n");
		return -EINVAL;
	}

	obj->filp->f_op = &exynos_drm_gem_fops;
	obj->filp->private_data = obj;

	down_write(&current->mm->mmap_sem);
	addr = do_mmap(obj->filp, 0, args->size,
			PROT_READ | PROT_WRITE, MAP_SHARED, 0);
	up_write(&current->mm->mmap_sem);

	drm_gem_object_unreference_unlocked(obj);

	if (IS_ERR((void *)addr))
		return PTR_ERR((void *)addr);

	args->mapped = addr;

	DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);

	return 0;
}

int exynos_drm_gem_init_object(struct drm_gem_object *obj)
{
	DRM_DEBUG_KMS("%s\n", __FILE__);

	return 0;
}

J
Joonyoung Shim 已提交
262
void exynos_drm_gem_free_object(struct drm_gem_object *obj)
263 264 265 266 267 268
{
	struct exynos_drm_gem_obj *exynos_gem_obj;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	DRM_DEBUG_KMS("handle count = %d\n",
J
Joonyoung Shim 已提交
269
			atomic_read(&obj->handle_count));
270

J
Joonyoung Shim 已提交
271 272
	if (obj->map_list.map)
		drm_gem_free_mmap_offset(obj);
273 274

	/* release file pointer to gem object. */
J
Joonyoung Shim 已提交
275
	drm_gem_object_release(obj);
276

J
Joonyoung Shim 已提交
277
	exynos_gem_obj = to_exynos_gem_obj(obj);
278

J
Joonyoung Shim 已提交
279
	exynos_drm_buf_destroy(obj->dev, exynos_gem_obj->buffer);
280 281 282 283 284

	kfree(exynos_gem_obj);
}

int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
J
Joonyoung Shim 已提交
285 286
			       struct drm_device *dev,
			       struct drm_mode_create_dumb *args)
287 288 289 290 291 292 293 294 295 296 297 298 299 300
{
	struct exynos_drm_gem_obj *exynos_gem_obj;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	/*
	 * alocate memory to be used for framebuffer.
	 * - this callback would be called by user application
	 *	with DRM_IOCTL_MODE_CREATE_DUMB command.
	 */

	args->pitch = args->width * args->bpp >> 3;
	args->size = args->pitch * args->height;

301
	exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle,
J
Joonyoung Shim 已提交
302
			args->size);
303 304 305 306 307 308 309
	if (IS_ERR(exynos_gem_obj))
		return PTR_ERR(exynos_gem_obj);

	return 0;
}

int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
J
Joonyoung Shim 已提交
310 311
				   struct drm_device *dev, uint32_t handle,
				   uint64_t *offset)
312 313 314
{
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_gem_object *obj;
315
	int ret = 0;
316 317 318 319 320 321 322 323 324 325 326 327 328 329

	DRM_DEBUG_KMS("%s\n", __FILE__);

	mutex_lock(&dev->struct_mutex);

	/*
	 * get offset of memory allocated for drm framebuffer.
	 * - this callback would be called by user application
	 *	with DRM_IOCTL_MODE_MAP_DUMB command.
	 */

	obj = drm_gem_object_lookup(dev, file_priv, handle);
	if (!obj) {
		DRM_ERROR("failed to lookup gem object.\n");
330 331
		ret = -EINVAL;
		goto unlock;
332 333 334 335
	}

	exynos_gem_obj = to_exynos_gem_obj(obj);

336 337 338 339 340
	if (!exynos_gem_obj->base.map_list.map) {
		ret = drm_gem_create_mmap_offset(&exynos_gem_obj->base);
		if (ret)
			goto out;
	}
341

342
	*offset = (u64)exynos_gem_obj->base.map_list.hash.key << PAGE_SHIFT;
343 344
	DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);

345 346 347
out:
	drm_gem_object_unreference(obj);
unlock:
348
	mutex_unlock(&dev->struct_mutex);
349
	return ret;
350 351
}

J
Joonyoung Shim 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
				struct drm_device *dev,
				unsigned int handle)
{
	int ret;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	/*
	 * obj->refcount and obj->handle_count are decreased and
	 * if both them are 0 then exynos_drm_gem_free_object()
	 * would be called by callback to release resources.
	 */
	ret = drm_gem_handle_delete(file_priv, handle);
	if (ret < 0) {
		DRM_ERROR("failed to delete drm_gem_handle.\n");
		return ret;
	}

	return 0;
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387
int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
	struct drm_gem_object *obj = vma->vm_private_data;
	struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
	struct drm_device *dev = obj->dev;
	unsigned long pfn;
	pgoff_t page_offset;
	int ret;

	page_offset = ((unsigned long)vmf->virtual_address -
			vma->vm_start) >> PAGE_SHIFT;

	mutex_lock(&dev->struct_mutex);

I
Inki Dae 已提交
388 389
	pfn = (((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
			PAGE_SHIFT) + page_offset;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

	ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);

	mutex_unlock(&dev->struct_mutex);

	return convert_to_vm_err_msg(ret);
}

int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
{
	int ret;

	DRM_DEBUG_KMS("%s\n", __FILE__);

	/* set vm_area_struct. */
	ret = drm_gem_mmap(filp, vma);
	if (ret < 0) {
		DRM_ERROR("failed to mmap.\n");
		return ret;
	}

	vma->vm_flags &= ~VM_PFNMAP;
	vma->vm_flags |= VM_MIXEDMAP;

	return ret;
}

MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
MODULE_LICENSE("GPL");