msm_gem.c 22.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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/>.
 */

#include <linux/spinlock.h>
#include <linux/shmem_fs.h>
R
Rob Clark 已提交
20
#include <linux/dma-buf.h>
21
#include <linux/pfn_t.h>
22 23

#include "msm_drv.h"
24
#include "msm_fence.h"
25
#include "msm_gem.h"
R
Rob Clark 已提交
26
#include "msm_gpu.h"
27
#include "msm_mmu.h"
28

29 30 31 32 33 34 35 36
static dma_addr_t physaddr(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct msm_drm_private *priv = obj->dev->dev_private;
	return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
			priv->vram.paddr;
}

37 38 39 40 41 42
static bool use_pages(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	return !msm_obj->vram_node;
}

43 44 45 46 47 48 49 50 51 52
/* allocate pages from VRAM carveout, used when no IOMMU: */
static struct page **get_pages_vram(struct drm_gem_object *obj,
		int npages)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct msm_drm_private *priv = obj->dev->dev_private;
	dma_addr_t paddr;
	struct page **p;
	int ret, i;

M
Michal Hocko 已提交
53
	p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
54 55 56
	if (!p)
		return ERR_PTR(-ENOMEM);

57
	ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
58
	if (ret) {
M
Michal Hocko 已提交
59
		kvfree(p);
60 61 62 63 64 65 66 67 68 69 70
		return ERR_PTR(ret);
	}

	paddr = physaddr(obj);
	for (i = 0; i < npages; i++) {
		p[i] = phys_to_page(paddr);
		paddr += PAGE_SIZE;
	}

	return p;
}
71 72 73 74 75 76 77 78

/* called with dev->struct_mutex held */
static struct page **get_pages(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	if (!msm_obj->pages) {
		struct drm_device *dev = obj->dev;
79
		struct page **p;
80 81
		int npages = obj->size >> PAGE_SHIFT;

82
		if (use_pages(obj))
83
			p = drm_gem_get_pages(obj);
84 85 86
		else
			p = get_pages_vram(obj, npages);

87 88 89 90 91 92 93
		if (IS_ERR(p)) {
			dev_err(dev->dev, "could not get pages: %ld\n",
					PTR_ERR(p));
			return p;
		}

		msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
94
		if (IS_ERR(msm_obj->sgt)) {
95
			dev_err(dev->dev, "failed to allocate sgt\n");
96
			return ERR_CAST(msm_obj->sgt);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		}

		msm_obj->pages = p;

		/* For non-cached buffers, ensure the new pages are clean
		 * because display controller, GPU, etc. are not coherent:
		 */
		if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
			dma_map_sg(dev->dev, msm_obj->sgt->sgl,
					msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
	}

	return msm_obj->pages;
}

static void put_pages(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	if (msm_obj->pages) {
		/* For non-cached buffers, ensure the new pages are clean
		 * because display controller, GPU, etc. are not coherent:
		 */
		if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
			dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
					msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
		sg_free_table(msm_obj->sgt);
		kfree(msm_obj->sgt);

126
		if (use_pages(obj))
127
			drm_gem_put_pages(obj, msm_obj->pages, true, false);
M
Micah Richert 已提交
128
		else {
129
			drm_mm_remove_node(msm_obj->vram_node);
M
Michal Hocko 已提交
130
			kvfree(msm_obj->pages);
M
Micah Richert 已提交
131
		}
132

133 134 135 136
		msm_obj->pages = NULL;
	}
}

R
Rob Clark 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
struct page **msm_gem_get_pages(struct drm_gem_object *obj)
{
	struct drm_device *dev = obj->dev;
	struct page **p;
	mutex_lock(&dev->struct_mutex);
	p = get_pages(obj);
	mutex_unlock(&dev->struct_mutex);
	return p;
}

void msm_gem_put_pages(struct drm_gem_object *obj)
{
	/* when we start tracking the pin count, then do something here */
}

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193
int msm_gem_mmap_obj(struct drm_gem_object *obj,
		struct vm_area_struct *vma)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

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

	if (msm_obj->flags & MSM_BO_WC) {
		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
	} else if (msm_obj->flags & MSM_BO_UNCACHED) {
		vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
	} else {
		/*
		 * Shunt off cached objs to shmem file so they have their own
		 * address_space (so unmap_mapping_range does what we want,
		 * in particular in the case of mmap'd dmabufs)
		 */
		fput(vma->vm_file);
		get_file(obj->filp);
		vma->vm_pgoff = 0;
		vma->vm_file  = obj->filp;

		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
	}

	return 0;
}

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

	ret = drm_gem_mmap(filp, vma);
	if (ret) {
		DBG("mmap failed: %d", ret);
		return ret;
	}

	return msm_gem_mmap_obj(vma->vm_private_data, vma);
}

194
int msm_gem_fault(struct vm_fault *vmf)
195
{
196
	struct vm_area_struct *vma = vmf->vma;
197 198
	struct drm_gem_object *obj = vma->vm_private_data;
	struct drm_device *dev = obj->dev;
199
	struct msm_drm_private *priv = dev->dev_private;
200 201 202 203 204
	struct page **pages;
	unsigned long pfn;
	pgoff_t pgoff;
	int ret;

205 206 207 208 209 210 211 212
	/* This should only happen if userspace tries to pass a mmap'd
	 * but unfaulted gem bo vaddr into submit ioctl, triggering
	 * a page fault while struct_mutex is already held.  This is
	 * not a valid use-case so just bail.
	 */
	if (priv->struct_mutex_task == current)
		return VM_FAULT_SIGBUS;

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	/* Make sure we don't parallel update on a fault, nor move or remove
	 * something from beneath our feet
	 */
	ret = mutex_lock_interruptible(&dev->struct_mutex);
	if (ret)
		goto out;

	/* make sure we have pages attached now */
	pages = get_pages(obj);
	if (IS_ERR(pages)) {
		ret = PTR_ERR(pages);
		goto out_unlock;
	}

	/* We don't use vmf->pgoff since that has the fake offset: */
228
	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
229

230
	pfn = page_to_pfn(pages[pgoff]);
231

232
	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
233 234
			pfn, pfn << PAGE_SHIFT);

235
	ret = vm_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
236 237 238 239 240 241 242 243 244

out_unlock:
	mutex_unlock(&dev->struct_mutex);
out:
	switch (ret) {
	case -EAGAIN:
	case 0:
	case -ERESTARTSYS:
	case -EINTR:
245 246 247 248 249
	case -EBUSY:
		/*
		 * EBUSY is ok: this just means that another thread
		 * already did the job.
		 */
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 283 284 285
		return VM_FAULT_NOPAGE;
	case -ENOMEM:
		return VM_FAULT_OOM;
	default:
		return VM_FAULT_SIGBUS;
	}
}

/** get mmap offset */
static uint64_t mmap_offset(struct drm_gem_object *obj)
{
	struct drm_device *dev = obj->dev;
	int ret;

	WARN_ON(!mutex_is_locked(&dev->struct_mutex));

	/* Make it mmapable */
	ret = drm_gem_create_mmap_offset(obj);

	if (ret) {
		dev_err(dev->dev, "could not allocate mmap offset\n");
		return 0;
	}

	return drm_vma_node_offset_addr(&obj->vma_node);
}

uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
{
	uint64_t offset;
	mutex_lock(&obj->dev->struct_mutex);
	offset = mmap_offset(obj);
	mutex_unlock(&obj->dev->struct_mutex);
	return offset;
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct msm_gem_vma *vma;

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

	vma->aspace = aspace;

	list_add_tail(&vma->list, &msm_obj->vmas);

	return vma;
}

static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct msm_gem_vma *vma;

	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));

	list_for_each_entry(vma, &msm_obj->vmas, list) {
		if (vma->aspace == aspace)
			return vma;
	}

	return NULL;
}

static void del_vma(struct msm_gem_vma *vma)
{
	if (!vma)
		return;

	list_del(&vma->list);
	kfree(vma);
}

R
Rob Clark 已提交
328 329 330 331
static void
put_iova(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
332
	struct msm_gem_vma *vma, *tmp;
R
Rob Clark 已提交
333

334
	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
R
Rob Clark 已提交
335

336 337 338
	list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
		msm_gem_unmap_vma(vma->aspace, vma, msm_obj->sgt);
		del_vma(vma);
R
Rob Clark 已提交
339 340 341
	}
}

342 343 344 345 346 347 348
/* should be called under struct_mutex.. although it can be called
 * from atomic context without struct_mutex to acquire an extra
 * iova ref if you know one is already held.
 *
 * That means when I do eventually need to add support for unpinning
 * the refcnt counter needs to be atomic_t.
 */
349 350
int msm_gem_get_iova_locked(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace, uint64_t *iova)
351 352
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
353
	struct msm_gem_vma *vma;
354 355
	int ret = 0;

356 357
	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));

358
	vma = lookup_vma(obj, aspace);
359

360 361 362 363 364 365 366 367 368 369 370 371
	if (!vma) {
		struct page **pages;

		vma = add_vma(obj, aspace);
		if (IS_ERR(vma))
			return PTR_ERR(vma);

		pages = get_pages(obj);
		if (IS_ERR(pages)) {
			ret = PTR_ERR(pages);
			goto fail;
		}
372

373 374 375 376
		ret = msm_gem_map_vma(aspace, vma, msm_obj->sgt,
				obj->size >> PAGE_SHIFT);
		if (ret)
			goto fail;
377 378
	}

379 380 381 382 383
	*iova = vma->iova;
	return 0;

fail:
	del_vma(vma);
384 385 386 387

	return ret;
}

R
Rob Clark 已提交
388
/* get iova, taking a reference.  Should have a matching put */
389 390
int msm_gem_get_iova(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace, uint64_t *iova)
391 392
{
	int ret;
R
Rob Clark 已提交
393

394
	mutex_lock(&obj->dev->struct_mutex);
395
	ret = msm_gem_get_iova_locked(obj, aspace, iova);
396
	mutex_unlock(&obj->dev->struct_mutex);
397

398 399 400
	return ret;
}

R
Rob Clark 已提交
401 402 403
/* get iova without taking a reference, used in places where you have
 * already done a 'msm_gem_get_iova()'.
 */
404 405
uint64_t msm_gem_iova(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace)
R
Rob Clark 已提交
406
{
407 408 409 410 411 412 413 414
	struct msm_gem_vma *vma;

	mutex_lock(&obj->dev->struct_mutex);
	vma = lookup_vma(obj, aspace);
	mutex_unlock(&obj->dev->struct_mutex);
	WARN_ON(!vma);

	return vma ? vma->iova : 0;
R
Rob Clark 已提交
415 416
}

417 418
void msm_gem_put_iova(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
	// XXX TODO ..
	// NOTE: probably don't need a _locked() version.. we wouldn't
	// normally unmap here, but instead just mark that it could be
	// unmapped (if the iova refcnt drops to zero), but then later
	// if another _get_iova_locked() fails we can start unmapping
	// things that are no longer needed..
}

int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
		struct drm_mode_create_dumb *args)
{
	args->pitch = align_pitch(args->width, args->bpp);
	args->size  = PAGE_ALIGN(args->pitch * args->height);
	return msm_gem_new_handle(dev, file, args->size,
			MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
}

int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
		uint32_t handle, uint64_t *offset)
{
	struct drm_gem_object *obj;
	int ret = 0;

	/* GEM does all our handle to object mapping */
444
	obj = drm_gem_object_lookup(file, handle);
445 446 447 448 449 450 451 452 453 454 455 456 457
	if (obj == NULL) {
		ret = -ENOENT;
		goto fail;
	}

	*offset = msm_gem_mmap_offset(obj);

	drm_gem_object_unreference_unlocked(obj);

fail:
	return ret;
}

458
void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
459 460 461 462 463 464 465 466 467
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
	if (!msm_obj->vaddr) {
		struct page **pages = get_pages(obj);
		if (IS_ERR(pages))
			return ERR_CAST(pages);
		msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
468 469
		if (msm_obj->vaddr == NULL)
			return ERR_PTR(-ENOMEM);
470
	}
R
Rob Clark 已提交
471
	msm_obj->vmap_count++;
472 473 474
	return msm_obj->vaddr;
}

475
void *msm_gem_get_vaddr(struct drm_gem_object *obj)
476 477 478
{
	void *ret;
	mutex_lock(&obj->dev->struct_mutex);
479
	ret = msm_gem_get_vaddr_locked(obj);
480 481 482 483
	mutex_unlock(&obj->dev->struct_mutex);
	return ret;
}

484 485
void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
{
R
Rob Clark 已提交
486
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
487
	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
R
Rob Clark 已提交
488 489
	WARN_ON(msm_obj->vmap_count < 1);
	msm_obj->vmap_count--;
490 491 492 493
}

void msm_gem_put_vaddr(struct drm_gem_object *obj)
{
R
Rob Clark 已提交
494 495 496
	mutex_lock(&obj->dev->struct_mutex);
	msm_gem_put_vaddr_locked(obj);
	mutex_unlock(&obj->dev->struct_mutex);
497 498
}

R
Rob Clark 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
/* Update madvise status, returns true if not purged, else
 * false or -errno.
 */
int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));

	if (msm_obj->madv != __MSM_MADV_PURGED)
		msm_obj->madv = madv;

	return (msm_obj->madv != __MSM_MADV_PURGED);
}

R
Rob Clark 已提交
514 515 516 517 518 519 520 521 522 523 524
void msm_gem_purge(struct drm_gem_object *obj)
{
	struct drm_device *dev = obj->dev;
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
	WARN_ON(!is_purgeable(msm_obj));
	WARN_ON(obj->import_attach);

	put_iova(obj);

R
Rob Clark 已提交
525
	msm_gem_vunmap(obj);
R
Rob Clark 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

	put_pages(obj);

	msm_obj->madv = __MSM_MADV_PURGED;

	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
	drm_gem_free_mmap_offset(obj);

	/* Our goal here is to return as much of the memory as
	 * is possible back to the system as we are called from OOM.
	 * To do this we must instruct the shmfs to drop all of its
	 * backing pages, *now*.
	 */
	shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);

	invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
			0, (loff_t)-1);
}

R
Rob Clark 已提交
545 546 547 548 549 550 551 552 553 554 555
void msm_gem_vunmap(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
		return;

	vunmap(msm_obj->vaddr);
	msm_obj->vaddr = NULL;
}

R
Rob Clark 已提交
556 557 558 559 560 561
/* must be called before _move_to_active().. */
int msm_gem_sync_object(struct drm_gem_object *obj,
		struct msm_fence_context *fctx, bool exclusive)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct reservation_object_list *fobj;
562
	struct dma_fence *fence;
R
Rob Clark 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	int i, ret;

	if (!exclusive) {
		/* NOTE: _reserve_shared() must happen before _add_shared_fence(),
		 * which makes this a slightly strange place to call it.  OTOH this
		 * is a convenient can-fail point to hook it in.  (And similar to
		 * how etnaviv and nouveau handle this.)
		 */
		ret = reservation_object_reserve_shared(msm_obj->resv);
		if (ret)
			return ret;
	}

	fobj = reservation_object_get_list(msm_obj->resv);
	if (!fobj || (fobj->shared_count == 0)) {
		fence = reservation_object_get_excl(msm_obj->resv);
		/* don't need to wait on our own fences, since ring is fifo */
		if (fence && (fence->context != fctx->context)) {
581
			ret = dma_fence_wait(fence, true);
R
Rob Clark 已提交
582 583 584 585 586 587 588 589 590 591 592 593
			if (ret)
				return ret;
		}
	}

	if (!exclusive || !fobj)
		return 0;

	for (i = 0; i < fobj->shared_count; i++) {
		fence = rcu_dereference_protected(fobj->shared[i],
						reservation_object_held(msm_obj->resv));
		if (fence->context != fctx->context) {
594
			ret = dma_fence_wait(fence, true);
R
Rob Clark 已提交
595 596 597 598 599 600 601 602
			if (ret)
				return ret;
		}
	}

	return 0;
}

R
Rob Clark 已提交
603
void msm_gem_move_to_active(struct drm_gem_object *obj,
604
		struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence)
R
Rob Clark 已提交
605 606
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
R
Rob Clark 已提交
607
	WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
R
Rob Clark 已提交
608
	msm_obj->gpu = gpu;
R
Rob Clark 已提交
609 610
	if (exclusive)
		reservation_object_add_excl_fence(msm_obj->resv, fence);
R
Rob Clark 已提交
611
	else
R
Rob Clark 已提交
612
		reservation_object_add_shared_fence(msm_obj->resv, fence);
R
Rob Clark 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	list_del_init(&msm_obj->mm_list);
	list_add_tail(&msm_obj->mm_list, &gpu->active_list);
}

void msm_gem_move_to_inactive(struct drm_gem_object *obj)
{
	struct drm_device *dev = obj->dev;
	struct msm_drm_private *priv = dev->dev_private;
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	WARN_ON(!mutex_is_locked(&dev->struct_mutex));

	msm_obj->gpu = NULL;
	list_del_init(&msm_obj->mm_list);
	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
}

R
Rob Clark 已提交
630
int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
R
Rob Clark 已提交
631 632
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
R
Rob Clark 已提交
633
	bool write = !!(op & MSM_PREP_WRITE);
634 635 636 637 638 639 640 641 642 643
	unsigned long remain =
		op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
	long ret;

	ret = reservation_object_wait_timeout_rcu(msm_obj->resv, write,
						  true,  remain);
	if (ret == 0)
		return remain == 0 ? -EBUSY : -ETIMEDOUT;
	else if (ret < 0)
		return ret;
R
Rob Clark 已提交
644 645

	/* TODO cache maintenance */
646

R
Rob Clark 已提交
647
	return 0;
R
Rob Clark 已提交
648
}
649

R
Rob Clark 已提交
650 651 652
int msm_gem_cpu_fini(struct drm_gem_object *obj)
{
	/* TODO cache maintenance */
653 654 655 656
	return 0;
}

#ifdef CONFIG_DEBUG_FS
657
static void describe_fence(struct dma_fence *fence, const char *type,
R
Rob Clark 已提交
658 659
		struct seq_file *m)
{
660
	if (!dma_fence_is_signaled(fence))
R
Rob Clark 已提交
661 662 663 664 665 666
		seq_printf(m, "\t%9s: %s %s seq %u\n", type,
				fence->ops->get_driver_name(fence),
				fence->ops->get_timeline_name(fence),
				fence->seqno);
}

667 668 669
void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
R
Rob Clark 已提交
670 671
	struct reservation_object *robj = msm_obj->resv;
	struct reservation_object_list *fobj;
672
	struct dma_fence *fence;
673
	struct msm_gem_vma *vma;
674
	uint64_t off = drm_vma_node_start(&obj->vma_node);
R
Rob Clark 已提交
675
	const char *madv;
676

R
Rob Clark 已提交
677 678
	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));

R
Rob Clark 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691
	switch (msm_obj->madv) {
	case __MSM_MADV_PURGED:
		madv = " purged";
		break;
	case MSM_MADV_DONTNEED:
		madv = " purgeable";
		break;
	case MSM_MADV_WILLNEED:
	default:
		madv = "";
		break;
	}

692
	seq_printf(m, "%08x: %c %2d (%2d) %08llx %p\t",
R
Rob Clark 已提交
693
			msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
694
			obj->name, kref_read(&obj->refcount),
695 696
			off, msm_obj->vaddr);

697 698 699
	/* FIXME: we need to print the address space here too */
	list_for_each_entry(vma, &msm_obj->vmas, list)
		seq_printf(m, " %08llx", vma->iova);
700 701

	seq_printf(m, " %zu%s\n", obj->size, madv);
R
Rob Clark 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

	rcu_read_lock();
	fobj = rcu_dereference(robj->fence);
	if (fobj) {
		unsigned int i, shared_count = fobj->shared_count;

		for (i = 0; i < shared_count; i++) {
			fence = rcu_dereference(fobj->shared[i]);
			describe_fence(fence, "Shared", m);
		}
	}

	fence = rcu_dereference(robj->fence_excl);
	if (fence)
		describe_fence(fence, "Exclusive", m);
	rcu_read_unlock();
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
}

void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
{
	struct msm_gem_object *msm_obj;
	int count = 0;
	size_t size = 0;

	list_for_each_entry(msm_obj, list, mm_list) {
		struct drm_gem_object *obj = &msm_obj->base;
		seq_printf(m, "   ");
		msm_gem_describe(obj, m);
		count++;
		size += obj->size;
	}

	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
}
#endif

void msm_gem_free_object(struct drm_gem_object *obj)
{
	struct drm_device *dev = obj->dev;
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	WARN_ON(!mutex_is_locked(&dev->struct_mutex));

R
Rob Clark 已提交
745 746 747
	/* object should not be on active list: */
	WARN_ON(is_active(msm_obj));

748 749
	list_del(&msm_obj->mm_list);

R
Rob Clark 已提交
750
	put_iova(obj);
751

R
Rob Clark 已提交
752 753 754 755 756 757 758 759
	if (obj->import_attach) {
		if (msm_obj->vaddr)
			dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);

		/* Don't drop the pages for imported dmabuf, as they are not
		 * ours, just free the array we allocated:
		 */
		if (msm_obj->pages)
M
Michal Hocko 已提交
760
			kvfree(msm_obj->pages);
761

762
		drm_prime_gem_destroy(obj, msm_obj->sgt);
R
Rob Clark 已提交
763
	} else {
R
Rob Clark 已提交
764
		msm_gem_vunmap(obj);
R
Rob Clark 已提交
765 766
		put_pages(obj);
	}
767

R
Rob Clark 已提交
768 769 770
	if (msm_obj->resv == &msm_obj->_resv)
		reservation_object_fini(msm_obj->resv);

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	drm_gem_object_release(obj);

	kfree(msm_obj);
}

/* convenience method to construct a GEM buffer object, and userspace handle */
int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
		uint32_t size, uint32_t flags, uint32_t *handle)
{
	struct drm_gem_object *obj;
	int ret;

	ret = mutex_lock_interruptible(&dev->struct_mutex);
	if (ret)
		return ret;

	obj = msm_gem_new(dev, size, flags);

	mutex_unlock(&dev->struct_mutex);

	if (IS_ERR(obj))
		return PTR_ERR(obj);

	ret = drm_gem_handle_create(file, obj, handle);

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

	return ret;
}

R
Rob Clark 已提交
802 803
static int msm_gem_new_impl(struct drm_device *dev,
		uint32_t size, uint32_t flags,
804
		struct reservation_object *resv,
R
Rob Clark 已提交
805
		struct drm_gem_object **obj)
806 807 808 809
{
	struct msm_drm_private *priv = dev->dev_private;
	struct msm_gem_object *msm_obj;

810 811
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));

812 813 814 815 816 817 818 819
	switch (flags & MSM_BO_CACHE_MASK) {
	case MSM_BO_UNCACHED:
	case MSM_BO_CACHED:
	case MSM_BO_WC:
		break;
	default:
		dev_err(dev->dev, "invalid cache flag: %x\n",
				(flags & MSM_BO_CACHE_MASK));
R
Rob Clark 已提交
820
		return -EINVAL;
821 822
	}

823
	msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
R
Rob Clark 已提交
824 825
	if (!msm_obj)
		return -ENOMEM;
826 827

	msm_obj->flags = flags;
R
Rob Clark 已提交
828
	msm_obj->madv = MSM_MADV_WILLNEED;
829

830 831 832 833 834 835
	if (resv) {
		msm_obj->resv = resv;
	} else {
		msm_obj->resv = &msm_obj->_resv;
		reservation_object_init(msm_obj->resv);
	}
836

R
Rob Clark 已提交
837
	INIT_LIST_HEAD(&msm_obj->submit_entry);
838 839
	INIT_LIST_HEAD(&msm_obj->vmas);

840 841
	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);

R
Rob Clark 已提交
842 843 844 845 846 847 848 849
	*obj = &msm_obj->base;

	return 0;
}

struct drm_gem_object *msm_gem_new(struct drm_device *dev,
		uint32_t size, uint32_t flags)
{
850
	struct msm_drm_private *priv = dev->dev_private;
851
	struct drm_gem_object *obj = NULL;
852
	bool use_vram = false;
R
Rob Clark 已提交
853 854 855 856 857 858
	int ret;

	WARN_ON(!mutex_is_locked(&dev->struct_mutex));

	size = PAGE_ALIGN(size);

859 860 861 862 863 864 865 866
	if (!iommu_present(&platform_bus_type))
		use_vram = true;
	else if ((flags & MSM_BO_STOLEN) && priv->vram.size)
		use_vram = true;

	if (WARN_ON(use_vram && !priv->vram.size))
		return ERR_PTR(-EINVAL);

867 868 869 870 871 872
	/* Disallow zero sized objects as they make the underlying
	 * infrastructure grumpy
	 */
	if (size == 0)
		return ERR_PTR(-EINVAL);

873
	ret = msm_gem_new_impl(dev, size, flags, NULL, &obj);
R
Rob Clark 已提交
874 875 876
	if (ret)
		goto fail;

877
	if (use_vram) {
878
		struct msm_gem_vma *vma;
879 880
		struct page **pages;

881 882 883 884 885 886 887 888
		vma = add_vma(obj, NULL);
		if (IS_ERR(vma)) {
			ret = PTR_ERR(vma);
			goto fail;
		}

		to_msm_bo(obj)->vram_node = &vma->node;

889 890 891 892 893 894 895
		drm_gem_private_object_init(dev, obj, size);

		pages = get_pages(obj);
		if (IS_ERR(pages)) {
			ret = PTR_ERR(pages);
			goto fail;
		}
896 897

		vma->iova = physaddr(obj);
898
	} else {
899 900 901 902
		ret = drm_gem_object_init(dev, obj, size);
		if (ret)
			goto fail;
	}
R
Rob Clark 已提交
903 904 905 906

	return obj;

fail:
907
	drm_gem_object_unreference(obj);
R
Rob Clark 已提交
908 909 910 911
	return ERR_PTR(ret);
}

struct drm_gem_object *msm_gem_import(struct drm_device *dev,
912
		struct dma_buf *dmabuf, struct sg_table *sgt)
R
Rob Clark 已提交
913 914 915
{
	struct msm_gem_object *msm_obj;
	struct drm_gem_object *obj;
916
	uint32_t size;
R
Rob Clark 已提交
917 918
	int ret, npages;

919 920 921 922 923 924
	/* if we don't have IOMMU, don't bother pretending we can import: */
	if (!iommu_present(&platform_bus_type)) {
		dev_err(dev->dev, "cannot import without IOMMU\n");
		return ERR_PTR(-EINVAL);
	}

925
	size = PAGE_ALIGN(dmabuf->size);
R
Rob Clark 已提交
926

927 928
	/* Take mutex so we can modify the inactive list in msm_gem_new_impl */
	mutex_lock(&dev->struct_mutex);
929
	ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj);
930 931
	mutex_unlock(&dev->struct_mutex);

R
Rob Clark 已提交
932 933 934 935 936 937 938 939 940
	if (ret)
		goto fail;

	drm_gem_private_object_init(dev, obj, size);

	npages = size / PAGE_SIZE;

	msm_obj = to_msm_bo(obj);
	msm_obj->sgt = sgt;
M
Michal Hocko 已提交
941
	msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
R
Rob Clark 已提交
942 943 944 945 946 947 948 949 950
	if (!msm_obj->pages) {
		ret = -ENOMEM;
		goto fail;
	}

	ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
	if (ret)
		goto fail;

951 952 953
	return obj;

fail:
954
	drm_gem_object_unreference_unlocked(obj);
955 956
	return ERR_PTR(ret);
}