exynos_drm_gem.c 18.2 KB
Newer Older
1 2 3 4 5
/* exynos_drm_gem.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 * Author: Inki Dae <inki.dae@samsung.com>
 *
6 7 8 9
 * 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.
10 11
 */

12
#include <drm/drmP.h>
13

14
#include <linux/shmem_fs.h>
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
#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;
}

44
static int check_gem_flags(unsigned int flags)
45
{
46 47 48 49 50 51 52 53
	if (flags & ~(EXYNOS_BO_MASK)) {
		DRM_ERROR("invalid flags.\n");
		return -EINVAL;
	}

	return 0;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static void update_vm_cache_attr(struct exynos_drm_gem_obj *obj,
					struct vm_area_struct *vma)
{
	DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags);

	/* non-cachable as default. */
	if (obj->flags & EXYNOS_BO_CACHABLE)
		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
	else if (obj->flags & EXYNOS_BO_WC)
		vma->vm_page_prot =
			pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
	else
		vma->vm_page_prot =
			pgprot_noncached(vm_get_page_prot(vma->vm_flags));
}

70 71
static unsigned long roundup_gem_size(unsigned long size, unsigned int flags)
{
72
	/* TODO */
73

74
	return roundup(size, PAGE_SIZE);
75 76
}

77
static int exynos_drm_gem_map_buf(struct drm_gem_object *obj,
78 79 80 81 82 83
					struct vm_area_struct *vma,
					unsigned long f_vaddr,
					pgoff_t page_offset)
{
	struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
	struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
84
	struct scatterlist *sgl;
85
	unsigned long pfn;
86
	int i;
87

88 89 90 91 92 93
	if (!buf->sgt)
		return -EINTR;

	if (page_offset >= (buf->size >> PAGE_SHIFT)) {
		DRM_ERROR("invalid page offset\n");
		return -EINVAL;
94
	}
95

96 97 98 99 100 101 102 103 104
	sgl = buf->sgt->sgl;
	for_each_sg(buf->sgt->sgl, sgl, buf->sgt->nents, i) {
		if (page_offset < (sgl->length >> PAGE_SHIFT))
			break;
		page_offset -=	(sgl->length >> PAGE_SHIFT);
	}

	pfn = __phys_to_pfn(sg_phys(sgl)) + page_offset;

105 106 107
	return vm_insert_mixed(vma, f_vaddr, pfn);
}

108 109 110
static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
					struct drm_file *file_priv,
					unsigned int *handle)
111 112 113 114 115 116 117 118 119
{
	int ret;

	/*
	 * 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)
120
		return ret;
121 122 123 124 125 126

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

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

127 128 129 130 131 132
	return 0;
}

void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
{
	struct drm_gem_object *obj;
133
	struct exynos_drm_gem_buf *buf;
134 135 136 137

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

	obj = &exynos_gem_obj->base;
138
	buf = exynos_gem_obj->buffer;
139 140

	DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count));
141

142 143 144 145 146 147 148 149 150
	/*
	 * do not release memory region from exporter.
	 *
	 * the region will be released by exporter
	 * once dmabuf's refcount becomes 0.
	 */
	if (obj->import_attach)
		goto out;

151
	exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf);
152

153
out:
154
	exynos_drm_fini_buf(obj->dev, buf);
155
	exynos_gem_obj->buffer = NULL;
156 157 158 159 160

	if (obj->map_list.map)
		drm_gem_free_mmap_offset(obj);

	/* release file pointer to gem object. */
161 162 163
	drm_gem_object_release(obj);

	kfree(exynos_gem_obj);
164
	exynos_gem_obj = NULL;
165 166
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
						unsigned int gem_handle,
						struct drm_file *file_priv)
{
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_gem_object *obj;

	obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
	if (!obj) {
		DRM_ERROR("failed to lookup gem object.\n");
		return 0;
	}

	exynos_gem_obj = to_exynos_gem_obj(obj);

	drm_gem_object_unreference_unlocked(obj);

	return exynos_gem_obj->buffer->size;
}


I
Inki Dae 已提交
188
struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
189 190 191 192 193 194 195 196 197 198 199 200
						      unsigned long size)
{
	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 NULL;
	}

201
	exynos_gem_obj->size = size;
202 203 204 205 206 207 208 209 210 211 212 213
	obj = &exynos_gem_obj->base;

	ret = drm_gem_object_init(dev, obj, size);
	if (ret < 0) {
		DRM_ERROR("failed to initialize gem object\n");
		kfree(exynos_gem_obj);
		return NULL;
	}

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

	return exynos_gem_obj;
214 215
}

216
struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
217 218
						unsigned int flags,
						unsigned long size)
219
{
220
	struct exynos_drm_gem_obj *exynos_gem_obj;
221 222
	struct exynos_drm_gem_buf *buf;
	int ret;
223

224 225 226 227 228 229 230
	if (!size) {
		DRM_ERROR("invalid size.\n");
		return ERR_PTR(-EINVAL);
	}

	size = roundup_gem_size(size, flags);
	DRM_DEBUG_KMS("%s\n", __FILE__);
231

232 233 234
	ret = check_gem_flags(flags);
	if (ret)
		return ERR_PTR(ret);
235 236 237

	buf = exynos_drm_init_buf(dev, size);
	if (!buf)
J
Joonyoung Shim 已提交
238
		return ERR_PTR(-ENOMEM);
239

240 241
	exynos_gem_obj = exynos_drm_gem_init(dev, size);
	if (!exynos_gem_obj) {
242
		ret = -ENOMEM;
243
		goto err_fini_buf;
244 245
	}

246 247 248 249 250
	exynos_gem_obj->buffer = buf;

	/* set memory type and cache attribute from user side. */
	exynos_gem_obj->flags = flags;

251 252 253 254
	ret = exynos_drm_alloc_buf(dev, buf, flags);
	if (ret < 0) {
		drm_gem_object_release(&exynos_gem_obj->base);
		goto err_fini_buf;
255
	}
256 257

	return exynos_gem_obj;
258 259

err_fini_buf:
260 261
	exynos_drm_fini_buf(dev, buf);
	return ERR_PTR(ret);
262 263
}

264
int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
J
Joonyoung Shim 已提交
265
				struct drm_file *file_priv)
266 267
{
	struct drm_exynos_gem_create *args = data;
J
Joonyoung Shim 已提交
268
	struct exynos_drm_gem_obj *exynos_gem_obj;
269
	int ret;
270

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

273
	exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
274 275 276
	if (IS_ERR(exynos_gem_obj))
		return PTR_ERR(exynos_gem_obj);

277 278 279 280 281 282 283
	ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
			&args->handle);
	if (ret) {
		exynos_drm_gem_destroy(exynos_gem_obj);
		return ret;
	}

284 285 286
	return 0;
}

I
Inki Dae 已提交
287
dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
288
					unsigned int gem_handle,
I
Inki Dae 已提交
289
					struct drm_file *filp)
290 291 292 293
{
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_gem_object *obj;

I
Inki Dae 已提交
294
	obj = drm_gem_object_lookup(dev, filp, gem_handle);
295 296 297 298 299 300 301 302 303 304 305 306
	if (!obj) {
		DRM_ERROR("failed to lookup gem object.\n");
		return ERR_PTR(-EINVAL);
	}

	exynos_gem_obj = to_exynos_gem_obj(obj);

	return &exynos_gem_obj->buffer->dma_addr;
}

void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
					unsigned int gem_handle,
I
Inki Dae 已提交
307
					struct drm_file *filp)
308 309 310 311
{
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_gem_object *obj;

I
Inki Dae 已提交
312
	obj = drm_gem_object_lookup(dev, filp, gem_handle);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	if (!obj) {
		DRM_ERROR("failed to lookup gem object.\n");
		return;
	}

	exynos_gem_obj = to_exynos_gem_obj(obj);

	drm_gem_object_unreference_unlocked(obj);

	/*
	 * decrease obj->refcount one more time because we has already
	 * increased it at exynos_drm_gem_get_dma_addr().
	 */
	drm_gem_object_unreference_unlocked(obj);
}

329
int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
J
Joonyoung Shim 已提交
330
				    struct drm_file *file_priv)
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
	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);
}

348 349 350 351 352 353
static struct drm_file *exynos_drm_find_drm_file(struct drm_device *drm_dev,
							struct file *filp)
{
	struct drm_file *file_priv;

	/* find current process's drm_file from filelist. */
354 355
	list_for_each_entry(file_priv, &drm_dev->filelist, lhead)
		if (file_priv->filp == filp)
356 357 358 359 360 361 362
			return file_priv;

	WARN_ON(1);

	return ERR_PTR(-EFAULT);
}

363
static int exynos_drm_gem_mmap_buffer(struct file *filp,
J
Joonyoung Shim 已提交
364
				      struct vm_area_struct *vma)
365 366 367
{
	struct drm_gem_object *obj = filp->private_data;
	struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
368
	struct drm_device *drm_dev = obj->dev;
I
Inki Dae 已提交
369
	struct exynos_drm_gem_buf *buffer;
370
	struct drm_file *file_priv;
371
	unsigned long vm_size;
372
	int ret;
373 374 375

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

376
	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
377 378 379 380 381 382 383 384 385 386 387 388
	vma->vm_private_data = obj;
	vma->vm_ops = drm_dev->driver->gem_vm_ops;

	/* restore it to driver's fops. */
	filp->f_op = fops_get(drm_dev->driver->fops);

	file_priv = exynos_drm_find_drm_file(drm_dev, filp);
	if (IS_ERR(file_priv))
		return PTR_ERR(file_priv);

	/* restore it to drm_file. */
	filp->private_data = file_priv;
389

390
	update_vm_cache_attr(exynos_gem_obj, vma);
391

392
	vm_size = vma->vm_end - vma->vm_start;
393

394
	/*
I
Inki Dae 已提交
395
	 * a buffer contains information to physically continuous memory
396 397
	 * allocated by user request or at framebuffer creation.
	 */
I
Inki Dae 已提交
398
	buffer = exynos_gem_obj->buffer;
399 400

	/* check if user-requested size is valid. */
I
Inki Dae 已提交
401
	if (vm_size > buffer->size)
402 403
		return -EINVAL;

404
	ret = dma_mmap_attrs(drm_dev->dev, vma, buffer->pages,
405 406
				buffer->dma_addr, buffer->size,
				&buffer->dma_attrs);
407 408 409 410 411 412 413 414 415 416 417 418 419 420
	if (ret < 0) {
		DRM_ERROR("failed to mmap.\n");
		return ret;
	}

	/*
	 * take a reference to this mapping of the object. And this reference
	 * is unreferenced by the corresponding vm_close call.
	 */
	drm_gem_object_reference(obj);

	drm_vm_open_locked(drm_dev, vma);

	return 0;
421 422 423 424 425 426 427
}

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 已提交
428
			      struct drm_file *file_priv)
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
{
	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;
	}

447 448 449 450 451 452 453 454 455 456
	/*
	 * We have to use gem object and its fops for specific mmaper,
	 * but vm_mmap() can deliver only filp. So we have to change
	 * filp->f_op and filp->private_data temporarily, then restore
	 * again. So it is important to keep lock until restoration the
	 * settings to prevent others from misuse of filp->f_op or
	 * filp->private_data.
	 */
	mutex_lock(&dev->struct_mutex);

457 458 459 460 461 462
	/*
	 * Set specific mmper's fops. And it will be restored by
	 * exynos_drm_gem_mmap_buffer to dev->driver->fops.
	 * This is used to call specific mapper temporarily.
	 */
	file_priv->filp->f_op = &exynos_drm_gem_fops;
463

464 465 466 467 468 469 470 471
	/*
	 * Set gem object to private_data so that specific mmaper
	 * can get the gem object. And it will be restored by
	 * exynos_drm_gem_mmap_buffer to drm_file.
	 */
	file_priv->filp->private_data = obj;

	addr = vm_mmap(file_priv->filp, 0, args->size,
472 473
			PROT_READ | PROT_WRITE, MAP_SHARED, 0);

474
	drm_gem_object_unreference(obj);
475

476
	if (IS_ERR((void *)addr)) {
477 478 479 480 481 482
		/* check filp->f_op, filp->private_data are restored */
		if (file_priv->filp->f_op == &exynos_drm_gem_fops) {
			file_priv->filp->f_op = fops_get(dev->driver->fops);
			file_priv->filp->private_data = file_priv;
		}
		mutex_unlock(&dev->struct_mutex);
483
		return PTR_ERR((void *)addr);
484
	}
485

486 487
	mutex_unlock(&dev->struct_mutex);

488 489 490 491 492 493 494
	args->mapped = addr;

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

	return 0;
}

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
				      struct drm_file *file_priv)
{	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_exynos_gem_info *args = data;
	struct drm_gem_object *obj;

	mutex_lock(&dev->struct_mutex);

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

	exynos_gem_obj = to_exynos_gem_obj(obj);

	args->flags = exynos_gem_obj->flags;
	args->size = exynos_gem_obj->size;

	drm_gem_object_unreference(obj);
	mutex_unlock(&dev->struct_mutex);

	return 0;
}

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma)
{
	struct vm_area_struct *vma_copy;

	vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
	if (!vma_copy)
		return NULL;

	if (vma->vm_ops && vma->vm_ops->open)
		vma->vm_ops->open(vma);

	if (vma->vm_file)
		get_file(vma->vm_file);

	memcpy(vma_copy, vma, sizeof(*vma));

	vma_copy->vm_mm = NULL;
	vma_copy->vm_next = NULL;
	vma_copy->vm_prev = NULL;

	return vma_copy;
}

void exynos_gem_put_vma(struct vm_area_struct *vma)
{
	if (!vma)
		return;

	if (vma->vm_ops && vma->vm_ops->close)
		vma->vm_ops->close(vma);

	if (vma->vm_file)
		fput(vma->vm_file);

	kfree(vma);
}

int exynos_gem_get_pages_from_userptr(unsigned long start,
						unsigned int npages,
						struct page **pages,
						struct vm_area_struct *vma)
{
	int get_npages;

	/* the memory region mmaped with VM_PFNMAP. */
	if (vma_is_io(vma)) {
		unsigned int i;

		for (i = 0; i < npages; ++i, start += PAGE_SIZE) {
			unsigned long pfn;
			int ret = follow_pfn(vma, start, &pfn);
			if (ret)
				return ret;

			pages[i] = pfn_to_page(pfn);
		}

		if (i != npages) {
			DRM_ERROR("failed to get user_pages.\n");
			return -EINVAL;
		}

		return 0;
	}

	get_npages = get_user_pages(current, current->mm, start,
					npages, 1, 1, pages, NULL);
	get_npages = max(get_npages, 0);
	if (get_npages != npages) {
		DRM_ERROR("failed to get user_pages.\n");
		while (get_npages)
			put_page(pages[--get_npages]);
		return -EFAULT;
	}

	return 0;
}

void exynos_gem_put_pages_to_userptr(struct page **pages,
					unsigned int npages,
					struct vm_area_struct *vma)
{
	if (!vma_is_io(vma)) {
		unsigned int i;

		for (i = 0; i < npages; i++) {
			set_page_dirty_lock(pages[i]);

			/*
			 * undo the reference we took when populating
			 * the table.
			 */
			put_page(pages[i]);
		}
	}
}

int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
				struct sg_table *sgt,
				enum dma_data_direction dir)
{
	int nents;

	mutex_lock(&drm_dev->struct_mutex);

	nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
	if (!nents) {
		DRM_ERROR("failed to map sgl with dma.\n");
		mutex_unlock(&drm_dev->struct_mutex);
		return nents;
	}

	mutex_unlock(&drm_dev->struct_mutex);
	return 0;
}

void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
				struct sg_table *sgt,
				enum dma_data_direction dir)
{
	dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
}

644 645 646 647 648 649 650
int exynos_drm_gem_init_object(struct drm_gem_object *obj)
{
	DRM_DEBUG_KMS("%s\n", __FILE__);

	return 0;
}

J
Joonyoung Shim 已提交
651
void exynos_drm_gem_free_object(struct drm_gem_object *obj)
652
{
I
Inki Dae 已提交
653 654 655
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct exynos_drm_gem_buf *buf;

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

I
Inki Dae 已提交
658 659 660 661 662 663
	exynos_gem_obj = to_exynos_gem_obj(obj);
	buf = exynos_gem_obj->buffer;

	if (obj->import_attach)
		drm_prime_gem_destroy(obj, buf->sgt);

664
	exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
665 666 667
}

int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
J
Joonyoung Shim 已提交
668 669
			       struct drm_device *dev,
			       struct drm_mode_create_dumb *args)
670 671
{
	struct exynos_drm_gem_obj *exynos_gem_obj;
672
	int ret;
673 674 675 676 677 678 679 680 681

	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.
	 */

682
	args->pitch = args->width * ((args->bpp + 7) / 8);
I
Inki Dae 已提交
683
	args->size = args->pitch * args->height;
684

685 686
	exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG |
						EXYNOS_BO_WC, args->size);
687 688 689
	if (IS_ERR(exynos_gem_obj))
		return PTR_ERR(exynos_gem_obj);

690 691 692 693 694 695 696
	ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
			&args->handle);
	if (ret) {
		exynos_drm_gem_destroy(exynos_gem_obj);
		return ret;
	}

697 698 699 700
	return 0;
}

int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
J
Joonyoung Shim 已提交
701 702
				   struct drm_device *dev, uint32_t handle,
				   uint64_t *offset)
703 704
{
	struct drm_gem_object *obj;
705
	int ret = 0;
706 707 708 709 710 711 712 713 714 715 716 717 718 719

	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");
720 721
		ret = -EINVAL;
		goto unlock;
722 723
	}

724 725
	if (!obj->map_list.map) {
		ret = drm_gem_create_mmap_offset(obj);
726 727 728
		if (ret)
			goto out;
	}
729

730
	*offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
731 732
	DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);

733 734 735
out:
	drm_gem_object_unreference(obj);
unlock:
736
	mutex_unlock(&dev->struct_mutex);
737
	return ret;
738 739
}

J
Joonyoung Shim 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
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;
}

762 763 764 765
int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
	struct drm_gem_object *obj = vma->vm_private_data;
	struct drm_device *dev = obj->dev;
766
	unsigned long f_vaddr;
767 768 769 770 771
	pgoff_t page_offset;
	int ret;

	page_offset = ((unsigned long)vmf->virtual_address -
			vma->vm_start) >> PAGE_SHIFT;
772
	f_vaddr = (unsigned long)vmf->virtual_address;
773 774 775

	mutex_lock(&dev->struct_mutex);

776
	ret = exynos_drm_gem_map_buf(obj, vma, f_vaddr, page_offset);
777
	if (ret < 0)
778
		DRM_ERROR("failed to map a buffer with user.\n");
779 780 781 782 783 784 785 786

	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)
{
787 788
	struct exynos_drm_gem_obj *exynos_gem_obj;
	struct drm_gem_object *obj;
789 790 791 792 793 794 795 796 797 798 799
	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;
	}

800 801 802 803 804 805 806 807 808 809
	obj = vma->vm_private_data;
	exynos_gem_obj = to_exynos_gem_obj(obj);

	ret = check_gem_flags(exynos_gem_obj->flags);
	if (ret) {
		drm_gem_vm_close(vma);
		drm_gem_free_mmap_offset(obj);
		return ret;
	}

810 811 812
	vma->vm_flags &= ~VM_PFNMAP;
	vma->vm_flags |= VM_MIXEDMAP;

813 814
	update_vm_cache_attr(exynos_gem_obj, vma);

815 816
	return ret;
}