msm_gem.c 28.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8
/*
 * Copyright (C) 2013 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 */

#include <linux/spinlock.h>
#include <linux/shmem_fs.h>
R
Rob Clark 已提交
9
#include <linux/dma-buf.h>
10
#include <linux/pfn_t.h>
11

S
Sam Ravnborg 已提交
12 13
#include <drm/drm_prime.h>

14
#include "msm_drv.h"
15
#include "msm_fence.h"
16
#include "msm_gem.h"
R
Rob Clark 已提交
17
#include "msm_gpu.h"
18
#include "msm_mmu.h"
19

20 21 22
static void msm_gem_vunmap_locked(struct drm_gem_object *obj);


23 24 25 26 27 28 29 30
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;
}

31 32 33 34 35 36
static bool use_pages(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	return !msm_obj->vram_node;
}

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * Cache sync.. this is a bit over-complicated, to fit dma-mapping
 * API.  Really GPU cache is out of scope here (handled on cmdstream)
 * and all we need to do is invalidate newly allocated pages before
 * mapping to CPU as uncached/writecombine.
 *
 * On top of this, we have the added headache, that depending on
 * display generation, the display's iommu may be wired up to either
 * the toplevel drm device (mdss), or to the mdp sub-node, meaning
 * that here we either have dma-direct or iommu ops.
 *
 * Let this be a cautionary tail of abstraction gone wrong.
 */

static void sync_for_device(struct msm_gem_object *msm_obj)
{
	struct device *dev = msm_obj->base.dev->dev;

55
	if (get_dma_ops(dev) && IS_ENABLED(CONFIG_ARM64)) {
56 57 58 59 60 61 62 63 64 65 66 67
		dma_sync_sg_for_device(dev, msm_obj->sgt->sgl,
			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
	} else {
		dma_map_sg(dev, msm_obj->sgt->sgl,
			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
	}
}

static void sync_for_cpu(struct msm_gem_object *msm_obj)
{
	struct device *dev = msm_obj->base.dev->dev;

68
	if (get_dma_ops(dev) && IS_ENABLED(CONFIG_ARM64)) {
69 70 71 72 73 74 75 76
		dma_sync_sg_for_cpu(dev, msm_obj->sgt->sgl,
			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
	} else {
		dma_unmap_sg(dev, msm_obj->sgt->sgl,
			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
	}
}

77
/* allocate pages from VRAM carveout, used when no IOMMU: */
78
static struct page **get_pages_vram(struct drm_gem_object *obj, int npages)
79 80 81 82 83 84 85
{
	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 已提交
86
	p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
87 88 89
	if (!p)
		return ERR_PTR(-ENOMEM);

90
	spin_lock(&priv->vram.lock);
91
	ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
92
	spin_unlock(&priv->vram.lock);
93
	if (ret) {
M
Michal Hocko 已提交
94
		kvfree(p);
95 96 97 98 99 100 101 102 103 104 105
		return ERR_PTR(ret);
	}

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

	return p;
}
106 107 108 109 110 111 112

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;
113
		struct page **p;
114 115
		int npages = obj->size >> PAGE_SHIFT;

116
		if (use_pages(obj))
117
			p = drm_gem_get_pages(obj);
118 119 120
		else
			p = get_pages_vram(obj, npages);

121
		if (IS_ERR(p)) {
122
			DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
123 124 125 126
					PTR_ERR(p));
			return p;
		}

127 128
		msm_obj->pages = p;

129
		msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
130
		if (IS_ERR(msm_obj->sgt)) {
131 132
			void *ptr = ERR_CAST(msm_obj->sgt);

133
			DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n");
134 135
			msm_obj->sgt = NULL;
			return ptr;
136 137 138 139 140 141
		}

		/* 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))
142
			sync_for_device(msm_obj);
143 144 145 146 147
	}

	return msm_obj->pages;
}

148 149 150 151 152 153 154 155 156 157 158 159
static void put_pages_vram(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct msm_drm_private *priv = obj->dev->dev_private;

	spin_lock(&priv->vram.lock);
	drm_mm_remove_node(msm_obj->vram_node);
	spin_unlock(&priv->vram.lock);

	kvfree(msm_obj->pages);
}

160 161 162 163 164
static void put_pages(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	if (msm_obj->pages) {
165 166 167 168 169 170
		if (msm_obj->sgt) {
			/* 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))
171
				sync_for_cpu(msm_obj);
172 173

			sg_free_table(msm_obj->sgt);
174 175
			kfree(msm_obj->sgt);
		}
176

177
		if (use_pages(obj))
178
			drm_gem_put_pages(obj, msm_obj->pages, true, false);
179 180
		else
			put_pages_vram(obj);
181

182 183 184 185
		msm_obj->pages = NULL;
	}
}

R
Rob Clark 已提交
186 187
struct page **msm_gem_get_pages(struct drm_gem_object *obj)
{
188
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
R
Rob Clark 已提交
189
	struct page **p;
190 191 192 193 194 195 196 197

	mutex_lock(&msm_obj->lock);

	if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
		mutex_unlock(&msm_obj->lock);
		return ERR_PTR(-EBUSY);
	}

R
Rob Clark 已提交
198
	p = get_pages(obj);
199
	mutex_unlock(&msm_obj->lock);
R
Rob Clark 已提交
200 201 202 203 204 205 206 207
	return p;
}

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

208 209 210 211 212 213 214 215 216 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
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);
}

250
vm_fault_t msm_gem_fault(struct vm_fault *vmf)
251
{
252
	struct vm_area_struct *vma = vmf->vma;
253
	struct drm_gem_object *obj = vma->vm_private_data;
254
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
255 256 257
	struct page **pages;
	unsigned long pfn;
	pgoff_t pgoff;
258 259
	int err;
	vm_fault_t ret;
260

261 262 263
	/*
	 * vm_ops.open/drm_gem_mmap_obj and close get and put
	 * a reference on obj. So, we dont need to hold one here.
264
	 */
265 266 267
	err = mutex_lock_interruptible(&msm_obj->lock);
	if (err) {
		ret = VM_FAULT_NOPAGE;
268
		goto out;
269
	}
270

271 272 273 274 275
	if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
		mutex_unlock(&msm_obj->lock);
		return VM_FAULT_SIGBUS;
	}

276 277 278
	/* make sure we have pages attached now */
	pages = get_pages(obj);
	if (IS_ERR(pages)) {
279
		ret = vmf_error(PTR_ERR(pages));
280 281 282 283
		goto out_unlock;
	}

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

286
	pfn = page_to_pfn(pages[pgoff]);
287

288
	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
289 290
			pfn, pfn << PAGE_SHIFT);

291
	ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
292
out_unlock:
293
	mutex_unlock(&msm_obj->lock);
294
out:
295
	return ret;
296 297 298 299 300 301
}

/** get mmap offset */
static uint64_t mmap_offset(struct drm_gem_object *obj)
{
	struct drm_device *dev = obj->dev;
302
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
303 304
	int ret;

305
	WARN_ON(!mutex_is_locked(&msm_obj->lock));
306 307 308 309 310

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

	if (ret) {
311
		DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
312 313 314 315 316 317 318 319 320
		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;
321 322 323
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	mutex_lock(&msm_obj->lock);
324
	offset = mmap_offset(obj);
325
	mutex_unlock(&msm_obj->lock);
326 327 328
	return offset;
}

329 330 331 332 333 334
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;

335 336
	WARN_ON(!mutex_is_locked(&msm_obj->lock));

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	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;

354
	WARN_ON(!mutex_is_locked(&msm_obj->lock));
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

	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);
}

373
/* Called with msm_obj->lock locked */
R
Rob Clark 已提交
374 375 376 377
static void
put_iova(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
378
	struct msm_gem_vma *vma, *tmp;
R
Rob Clark 已提交
379

380
	WARN_ON(!mutex_is_locked(&msm_obj->lock));
R
Rob Clark 已提交
381

382
	list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
383 384 385 386
		if (vma->aspace) {
			msm_gem_purge_vma(vma->aspace, vma);
			msm_gem_close_vma(vma->aspace, vma);
		}
387
		del_vma(vma);
R
Rob Clark 已提交
388 389 390
	}
}

391
static int msm_gem_get_iova_locked(struct drm_gem_object *obj,
392 393
		struct msm_gem_address_space *aspace, uint64_t *iova,
		u64 range_start, u64 range_end)
394 395
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
396
	struct msm_gem_vma *vma;
397 398
	int ret = 0;

399
	WARN_ON(!mutex_is_locked(&msm_obj->lock));
400

401
	vma = lookup_vma(obj, aspace);
402

403 404
	if (!vma) {
		vma = add_vma(obj, aspace);
405 406
		if (IS_ERR(vma))
			return PTR_ERR(vma);
407

408 409
		ret = msm_gem_init_vma(aspace, vma, obj->size >> PAGE_SHIFT,
			range_start, range_end);
410 411 412
		if (ret) {
			del_vma(vma);
			return ret;
413
		}
414 415
	}

416 417
	*iova = vma->iova;
	return 0;
418 419 420 421 422 423 424 425
}

static int msm_gem_pin_iova(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;
	struct page **pages;
R
Rob Clark 已提交
426 427 428 429
	int prot = IOMMU_READ;

	if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
		prot |= IOMMU_WRITE;
430

431 432 433
	if (msm_obj->flags & MSM_BO_MAP_PRIV)
		prot |= IOMMU_PRIV;

434 435 436 437 438 439 440 441 442 443 444 445 446
	WARN_ON(!mutex_is_locked(&msm_obj->lock));

	if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
		return -EBUSY;

	vma = lookup_vma(obj, aspace);
	if (WARN_ON(!vma))
		return -EINVAL;

	pages = get_pages(obj);
	if (IS_ERR(pages))
		return PTR_ERR(pages);

R
Rob Clark 已提交
447 448
	return msm_gem_map_vma(aspace, vma, prot,
			msm_obj->sgt, obj->size >> PAGE_SHIFT);
449 450
}

451 452 453 454 455 456 457
/*
 * get iova and pin it. Should have a matching put
 * limits iova to specified range (in pages)
 */
int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace, uint64_t *iova,
		u64 range_start, u64 range_end)
458 459 460 461 462 463 464
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	u64 local;
	int ret;

	mutex_lock(&msm_obj->lock);

465 466
	ret = msm_gem_get_iova_locked(obj, aspace, &local,
		range_start, range_end);
467 468 469 470 471 472

	if (!ret)
		ret = msm_gem_pin_iova(obj, aspace);

	if (!ret)
		*iova = local;
473

474
	mutex_unlock(&msm_obj->lock);
475 476 477
	return ret;
}

478 479 480 481 482 483 484
/* get iova and pin it. Should have a matching put */
int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace, uint64_t *iova)
{
	return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
}

485 486 487 488
/*
 * Get an iova but don't pin it. Doesn't need a put because iovas are currently
 * valid for the life of the object
 */
489 490 491 492 493 494 495
int msm_gem_get_iova(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace, uint64_t *iova)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	int ret;

	mutex_lock(&msm_obj->lock);
496
	ret = msm_gem_get_iova_locked(obj, aspace, iova, 0, U64_MAX);
497 498 499 500 501
	mutex_unlock(&msm_obj->lock);

	return ret;
}

R
Rob Clark 已提交
502
/* get iova without taking a reference, used in places where you have
503
 * already done a 'msm_gem_get_and_pin_iova' or 'msm_gem_get_iova'
R
Rob Clark 已提交
504
 */
505 506
uint64_t msm_gem_iova(struct drm_gem_object *obj,
		struct msm_gem_address_space *aspace)
R
Rob Clark 已提交
507
{
508
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
509 510
	struct msm_gem_vma *vma;

511
	mutex_lock(&msm_obj->lock);
512
	vma = lookup_vma(obj, aspace);
513
	mutex_unlock(&msm_obj->lock);
514 515 516
	WARN_ON(!vma);

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

519 520 521 522 523 524
/*
 * Unpin a iova by updating the reference counts. The memory isn't actually
 * purged until something else (shrinker, mm_notifier, destroy, etc) decides
 * to get rid of it
 */
void msm_gem_unpin_iova(struct drm_gem_object *obj,
525
		struct msm_gem_address_space *aspace)
526
{
527 528 529 530 531 532 533 534 535 536
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
	struct msm_gem_vma *vma;

	mutex_lock(&msm_obj->lock);
	vma = lookup_vma(obj, aspace);

	if (!WARN_ON(!vma))
		msm_gem_unmap_vma(aspace, vma);

	mutex_unlock(&msm_obj->lock);
537 538 539 540 541 542 543 544
}

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,
545
			MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
546 547 548 549 550 551 552 553 554
}

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 */
555
	obj = drm_gem_object_lookup(file, handle);
556 557 558 559 560 561 562
	if (obj == NULL) {
		ret = -ENOENT;
		goto fail;
	}

	*offset = msm_gem_mmap_offset(obj);

563
	drm_gem_object_put(obj);
564 565 566 567 568

fail:
	return ret;
}

569
static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
570 571
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
572 573
	int ret = 0;

574 575 576
	if (obj->import_attach)
		return ERR_PTR(-ENODEV);

577 578
	mutex_lock(&msm_obj->lock);

579
	if (WARN_ON(msm_obj->madv > madv)) {
580
		DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
581
			msm_obj->madv, madv);
582 583 584 585 586 587 588 589 590 591 592 593
		mutex_unlock(&msm_obj->lock);
		return ERR_PTR(-EBUSY);
	}

	/* increment vmap_count *before* vmap() call, so shrinker can
	 * check vmap_count (is_vunmapable()) outside of msm_obj->lock.
	 * This guarantees that we won't try to msm_gem_vunmap() this
	 * same object from within the vmap() call (while we already
	 * hold msm_obj->lock)
	 */
	msm_obj->vmap_count++;

594 595
	if (!msm_obj->vaddr) {
		struct page **pages = get_pages(obj);
596 597 598 599
		if (IS_ERR(pages)) {
			ret = PTR_ERR(pages);
			goto fail;
		}
600 601
		msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
602 603 604 605
		if (msm_obj->vaddr == NULL) {
			ret = -ENOMEM;
			goto fail;
		}
606
	}
607 608

	mutex_unlock(&msm_obj->lock);
609 610
	return msm_obj->vaddr;

611 612 613 614
fail:
	msm_obj->vmap_count--;
	mutex_unlock(&msm_obj->lock);
	return ERR_PTR(ret);
615 616
}

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
void *msm_gem_get_vaddr(struct drm_gem_object *obj)
{
	return get_vaddr(obj, MSM_MADV_WILLNEED);
}

/*
 * Don't use this!  It is for the very special case of dumping
 * submits from GPU hangs or faults, were the bo may already
 * be MSM_MADV_DONTNEED, but we know the buffer is still on the
 * active list.
 */
void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
{
	return get_vaddr(obj, __MSM_MADV_PURGED);
}

633
void msm_gem_put_vaddr(struct drm_gem_object *obj)
634
{
R
Rob Clark 已提交
635
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
636 637

	mutex_lock(&msm_obj->lock);
R
Rob Clark 已提交
638 639
	WARN_ON(msm_obj->vmap_count < 1);
	msm_obj->vmap_count--;
640
	mutex_unlock(&msm_obj->lock);
641 642
}

R
Rob Clark 已提交
643 644 645 646 647 648 649
/* 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);

650 651
	mutex_lock(&msm_obj->lock);

R
Rob Clark 已提交
652 653 654 655 656
	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));

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

657 658 659 660 661
	madv = msm_obj->madv;

	mutex_unlock(&msm_obj->lock);

	return (madv != __MSM_MADV_PURGED);
R
Rob Clark 已提交
662 663
}

664
void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass)
R
Rob Clark 已提交
665 666 667 668 669 670 671 672
{
	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);

673 674
	mutex_lock_nested(&msm_obj->lock, subclass);

R
Rob Clark 已提交
675 676
	put_iova(obj);

677
	msm_gem_vunmap_locked(obj);
R
Rob Clark 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

	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);
695 696

	mutex_unlock(&msm_obj->lock);
R
Rob Clark 已提交
697 698
}

699
static void msm_gem_vunmap_locked(struct drm_gem_object *obj)
R
Rob Clark 已提交
700 701 702
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

703 704
	WARN_ON(!mutex_is_locked(&msm_obj->lock));

R
Rob Clark 已提交
705 706 707 708 709 710 711
	if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
		return;

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

712 713 714 715 716 717 718 719 720
void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);

	mutex_lock_nested(&msm_obj->lock, subclass);
	msm_gem_vunmap_locked(obj);
	mutex_unlock(&msm_obj->lock);
}

R
Rob Clark 已提交
721 722 723 724
/* must be called before _move_to_active().. */
int msm_gem_sync_object(struct drm_gem_object *obj,
		struct msm_fence_context *fctx, bool exclusive)
{
725
	struct dma_resv_list *fobj;
726
	struct dma_fence *fence;
R
Rob Clark 已提交
727 728
	int i, ret;

729
	fobj = dma_resv_get_list(obj->resv);
R
Rob Clark 已提交
730
	if (!fobj || (fobj->shared_count == 0)) {
731
		fence = dma_resv_get_excl(obj->resv);
R
Rob Clark 已提交
732 733
		/* don't need to wait on our own fences, since ring is fifo */
		if (fence && (fence->context != fctx->context)) {
734
			ret = dma_fence_wait(fence, true);
R
Rob Clark 已提交
735 736 737 738 739 740 741 742 743 744
			if (ret)
				return ret;
		}
	}

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

	for (i = 0; i < fobj->shared_count; i++) {
		fence = rcu_dereference_protected(fobj->shared[i],
745
						dma_resv_held(obj->resv));
R
Rob Clark 已提交
746
		if (fence->context != fctx->context) {
747
			ret = dma_fence_wait(fence, true);
R
Rob Clark 已提交
748 749 750 751 752 753 754 755
			if (ret)
				return ret;
		}
	}

	return 0;
}

R
Rob Clark 已提交
756
void msm_gem_move_to_active(struct drm_gem_object *obj,
757
		struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence)
R
Rob Clark 已提交
758 759
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
R
Rob Clark 已提交
760
	WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
R
Rob Clark 已提交
761
	msm_obj->gpu = gpu;
R
Rob Clark 已提交
762
	if (exclusive)
763
		dma_resv_add_excl_fence(obj->resv, fence);
R
Rob Clark 已提交
764
	else
765
		dma_resv_add_shared_fence(obj->resv, fence);
R
Rob Clark 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	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 已提交
783
int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
R
Rob Clark 已提交
784
{
R
Rob Clark 已提交
785
	bool write = !!(op & MSM_PREP_WRITE);
786 787 788 789
	unsigned long remain =
		op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
	long ret;

790
	ret = dma_resv_wait_timeout_rcu(obj->resv, write,
791 792 793 794 795
						  true,  remain);
	if (ret == 0)
		return remain == 0 ? -EBUSY : -ETIMEDOUT;
	else if (ret < 0)
		return ret;
R
Rob Clark 已提交
796 797

	/* TODO cache maintenance */
798

R
Rob Clark 已提交
799
	return 0;
R
Rob Clark 已提交
800
}
801

R
Rob Clark 已提交
802 803 804
int msm_gem_cpu_fini(struct drm_gem_object *obj)
{
	/* TODO cache maintenance */
805 806 807 808
	return 0;
}

#ifdef CONFIG_DEBUG_FS
809
static void describe_fence(struct dma_fence *fence, const char *type,
R
Rob Clark 已提交
810 811
		struct seq_file *m)
{
812
	if (!dma_fence_is_signaled(fence))
813
		seq_printf(m, "\t%9s: %s %s seq %llu\n", type,
R
Rob Clark 已提交
814 815 816 817 818
				fence->ops->get_driver_name(fence),
				fence->ops->get_timeline_name(fence),
				fence->seqno);
}

819 820 821
void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
822 823
	struct dma_resv *robj = obj->resv;
	struct dma_resv_list *fobj;
824
	struct dma_fence *fence;
825
	struct msm_gem_vma *vma;
826
	uint64_t off = drm_vma_node_start(&obj->vma_node);
R
Rob Clark 已提交
827
	const char *madv;
828

829
	mutex_lock(&msm_obj->lock);
R
Rob Clark 已提交
830

R
Rob Clark 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843
	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;
	}

844
	seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
R
Rob Clark 已提交
845
			msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
846
			obj->name, kref_read(&obj->refcount),
847 848
			off, msm_obj->vaddr);

849
	seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
850

851 852
	if (!list_empty(&msm_obj->vmas)) {

853
		seq_puts(m, "      vmas:");
854 855

		list_for_each_entry(vma, &msm_obj->vmas, list)
856 857
			seq_printf(m, " [%s: %08llx,%s,inuse=%d]",
				vma->aspace != NULL ? vma->aspace->name : NULL,
858 859
				vma->iova, vma->mapped ? "mapped" : "unmapped",
				vma->inuse);
860 861 862

		seq_puts(m, "\n");
	}
R
Rob Clark 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

	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();
879 880

	mutex_unlock(&msm_obj->lock);
881 882 883 884 885 886 887 888
}

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;

889
	seq_puts(m, "   flags       id ref  offset   kaddr            size     madv      name\n");
890 891
	list_for_each_entry(msm_obj, list, mm_list) {
		struct drm_gem_object *obj = &msm_obj->base;
892
		seq_puts(m, "   ");
893 894 895 896 897 898 899 900 901
		msm_gem_describe(obj, m);
		count++;
		size += obj->size;
	}

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

902
/* don't call directly!  Use drm_gem_object_put_locked() and friends */
903 904 905
void msm_gem_free_object(struct drm_gem_object *obj)
{
	struct msm_gem_object *msm_obj = to_msm_bo(obj);
906 907 908 909 910 911 912 913 914 915 916
	struct drm_device *dev = obj->dev;
	struct msm_drm_private *priv = dev->dev_private;

	if (llist_add(&msm_obj->freed, &priv->free_list))
		queue_work(priv->wq, &priv->free_work);
}

static void free_object(struct msm_gem_object *msm_obj)
{
	struct drm_gem_object *obj = &msm_obj->base;
	struct drm_device *dev = obj->dev;
917 918 919

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

R
Rob Clark 已提交
920 921 922
	/* object should not be on active list: */
	WARN_ON(is_active(msm_obj));

923 924
	list_del(&msm_obj->mm_list);

925 926
	mutex_lock(&msm_obj->lock);

R
Rob Clark 已提交
927
	put_iova(obj);
928

R
Rob Clark 已提交
929
	if (obj->import_attach) {
930
		WARN_ON(msm_obj->vaddr);
R
Rob Clark 已提交
931 932 933 934 935

		/* 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 已提交
936
			kvfree(msm_obj->pages);
937

938
		drm_prime_gem_destroy(obj, msm_obj->sgt);
R
Rob Clark 已提交
939
	} else {
940
		msm_gem_vunmap_locked(obj);
R
Rob Clark 已提交
941 942
		put_pages(obj);
	}
943 944 945

	drm_gem_object_release(obj);

946
	mutex_unlock(&msm_obj->lock);
947 948 949
	kfree(msm_obj);
}

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
void msm_gem_free_work(struct work_struct *work)
{
	struct msm_drm_private *priv =
		container_of(work, struct msm_drm_private, free_work);
	struct drm_device *dev = priv->dev;
	struct llist_node *freed;
	struct msm_gem_object *msm_obj, *next;

	while ((freed = llist_del_all(&priv->free_list))) {

		mutex_lock(&dev->struct_mutex);

		llist_for_each_entry_safe(msm_obj, next,
					  freed, freed)
			free_object(msm_obj);

		mutex_unlock(&dev->struct_mutex);

		if (need_resched())
			break;
	}
}

973 974
/* convenience method to construct a GEM buffer object, and userspace handle */
int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
975 976
		uint32_t size, uint32_t flags, uint32_t *handle,
		char *name)
977 978 979 980 981 982 983 984 985
{
	struct drm_gem_object *obj;
	int ret;

	obj = msm_gem_new(dev, size, flags);

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

986 987 988
	if (name)
		msm_gem_object_set_name(obj, "%s", name);

989 990 991
	ret = drm_gem_handle_create(file, obj, handle);

	/* drop reference from allocate - handle holds it now */
992
	drm_gem_object_put(obj);
993 994 995 996

	return ret;
}

R
Rob Clark 已提交
997 998
static int msm_gem_new_impl(struct drm_device *dev,
		uint32_t size, uint32_t flags,
999
		struct drm_gem_object **obj)
1000 1001 1002 1003 1004 1005 1006 1007 1008
{
	struct msm_gem_object *msm_obj;

	switch (flags & MSM_BO_CACHE_MASK) {
	case MSM_BO_UNCACHED:
	case MSM_BO_CACHED:
	case MSM_BO_WC:
		break;
	default:
1009
		DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n",
1010
				(flags & MSM_BO_CACHE_MASK));
R
Rob Clark 已提交
1011
		return -EINVAL;
1012 1013
	}

1014
	msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
R
Rob Clark 已提交
1015 1016
	if (!msm_obj)
		return -ENOMEM;
1017

1018 1019
	mutex_init(&msm_obj->lock);

1020
	msm_obj->flags = flags;
R
Rob Clark 已提交
1021
	msm_obj->madv = MSM_MADV_WILLNEED;
1022

R
Rob Clark 已提交
1023
	INIT_LIST_HEAD(&msm_obj->submit_entry);
1024 1025
	INIT_LIST_HEAD(&msm_obj->vmas);

R
Rob Clark 已提交
1026 1027 1028 1029 1030
	*obj = &msm_obj->base;

	return 0;
}

1031 1032
static struct drm_gem_object *_msm_gem_new(struct drm_device *dev,
		uint32_t size, uint32_t flags, bool struct_mutex_locked)
R
Rob Clark 已提交
1033
{
1034
	struct msm_drm_private *priv = dev->dev_private;
1035
	struct msm_gem_object *msm_obj;
1036
	struct drm_gem_object *obj = NULL;
1037
	bool use_vram = false;
R
Rob Clark 已提交
1038 1039 1040 1041
	int ret;

	size = PAGE_ALIGN(size);

J
Jonathan Marek 已提交
1042
	if (!msm_use_mmu(dev))
1043
		use_vram = true;
1044
	else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1045 1046 1047 1048 1049
		use_vram = true;

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

1050 1051 1052 1053 1054 1055
	/* Disallow zero sized objects as they make the underlying
	 * infrastructure grumpy
	 */
	if (size == 0)
		return ERR_PTR(-EINVAL);

1056
	ret = msm_gem_new_impl(dev, size, flags, &obj);
R
Rob Clark 已提交
1057 1058 1059
	if (ret)
		goto fail;

1060 1061
	msm_obj = to_msm_bo(obj);

1062
	if (use_vram) {
1063
		struct msm_gem_vma *vma;
1064
		struct page **pages;
1065 1066

		mutex_lock(&msm_obj->lock);
1067

1068
		vma = add_vma(obj, NULL);
1069
		mutex_unlock(&msm_obj->lock);
1070 1071 1072 1073 1074 1075 1076
		if (IS_ERR(vma)) {
			ret = PTR_ERR(vma);
			goto fail;
		}

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

1077 1078 1079 1080 1081 1082 1083
		drm_gem_private_object_init(dev, obj, size);

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

		vma->iova = physaddr(obj);
1086
	} else {
1087 1088 1089
		ret = drm_gem_object_init(dev, obj, size);
		if (ret)
			goto fail;
1090 1091 1092 1093 1094 1095 1096
		/*
		 * Our buffers are kept pinned, so allocating them from the
		 * MOVABLE zone is a really bad idea, and conflicts with CMA.
		 * See comments above new_inode() why this is required _and_
		 * expected if you're going to pin these pages.
		 */
		mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1097
	}
R
Rob Clark 已提交
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107
	if (struct_mutex_locked) {
		WARN_ON(!mutex_is_locked(&dev->struct_mutex));
		list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
	} else {
		mutex_lock(&dev->struct_mutex);
		list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
		mutex_unlock(&dev->struct_mutex);
	}

R
Rob Clark 已提交
1108 1109 1110
	return obj;

fail:
1111
	drm_gem_object_put(obj);
R
Rob Clark 已提交
1112 1113 1114
	return ERR_PTR(ret);
}

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
		uint32_t size, uint32_t flags)
{
	return _msm_gem_new(dev, size, flags, true);
}

struct drm_gem_object *msm_gem_new(struct drm_device *dev,
		uint32_t size, uint32_t flags)
{
	return _msm_gem_new(dev, size, flags, false);
}

R
Rob Clark 已提交
1127
struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1128
		struct dma_buf *dmabuf, struct sg_table *sgt)
R
Rob Clark 已提交
1129
{
1130
	struct msm_drm_private *priv = dev->dev_private;
R
Rob Clark 已提交
1131 1132
	struct msm_gem_object *msm_obj;
	struct drm_gem_object *obj;
1133
	uint32_t size;
R
Rob Clark 已提交
1134 1135
	int ret, npages;

1136
	/* if we don't have IOMMU, don't bother pretending we can import: */
J
Jonathan Marek 已提交
1137
	if (!msm_use_mmu(dev)) {
1138
		DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1139 1140 1141
		return ERR_PTR(-EINVAL);
	}

1142
	size = PAGE_ALIGN(dmabuf->size);
R
Rob Clark 已提交
1143

1144
	ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
R
Rob Clark 已提交
1145 1146 1147 1148 1149 1150 1151 1152
	if (ret)
		goto fail;

	drm_gem_private_object_init(dev, obj, size);

	npages = size / PAGE_SIZE;

	msm_obj = to_msm_bo(obj);
1153
	mutex_lock(&msm_obj->lock);
R
Rob Clark 已提交
1154
	msm_obj->sgt = sgt;
M
Michal Hocko 已提交
1155
	msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
R
Rob Clark 已提交
1156
	if (!msm_obj->pages) {
1157
		mutex_unlock(&msm_obj->lock);
R
Rob Clark 已提交
1158 1159 1160 1161 1162
		ret = -ENOMEM;
		goto fail;
	}

	ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
1163 1164
	if (ret) {
		mutex_unlock(&msm_obj->lock);
R
Rob Clark 已提交
1165
		goto fail;
1166
	}
R
Rob Clark 已提交
1167

1168
	mutex_unlock(&msm_obj->lock);
1169 1170 1171 1172 1173

	mutex_lock(&dev->struct_mutex);
	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
	mutex_unlock(&dev->struct_mutex);

1174 1175 1176
	return obj;

fail:
1177
	drm_gem_object_put(obj);
1178 1179
	return ERR_PTR(ret);
}
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192

static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
		uint32_t flags, struct msm_gem_address_space *aspace,
		struct drm_gem_object **bo, uint64_t *iova, bool locked)
{
	void *vaddr;
	struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked);
	int ret;

	if (IS_ERR(obj))
		return ERR_CAST(obj);

	if (iova) {
1193
		ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1194 1195
		if (ret)
			goto err;
1196 1197 1198
	}

	vaddr = msm_gem_get_vaddr(obj);
1199
	if (IS_ERR(vaddr)) {
1200
		msm_gem_unpin_iova(obj, aspace);
1201 1202
		ret = PTR_ERR(vaddr);
		goto err;
1203 1204 1205 1206 1207 1208
	}

	if (bo)
		*bo = obj;

	return vaddr;
1209 1210
err:
	if (locked)
1211
		drm_gem_object_put_locked(obj);
1212
	else
1213
		drm_gem_object_put(obj);
1214 1215 1216

	return ERR_PTR(ret);

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
}

void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
		uint32_t flags, struct msm_gem_address_space *aspace,
		struct drm_gem_object **bo, uint64_t *iova)
{
	return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false);
}

void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
		uint32_t flags, struct msm_gem_address_space *aspace,
		struct drm_gem_object **bo, uint64_t *iova)
{
	return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true);
}
1232 1233 1234 1235 1236 1237 1238 1239

void msm_gem_kernel_put(struct drm_gem_object *bo,
		struct msm_gem_address_space *aspace, bool locked)
{
	if (IS_ERR_OR_NULL(bo))
		return;

	msm_gem_put_vaddr(bo);
1240
	msm_gem_unpin_iova(bo, aspace);
1241 1242

	if (locked)
1243
		drm_gem_object_put_locked(bo);
1244
	else
1245
		drm_gem_object_put(bo);
1246
}
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
{
	struct msm_gem_object *msm_obj = to_msm_bo(bo);
	va_list ap;

	if (!fmt)
		return;

	va_start(ap, fmt);
	vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);
	va_end(ap);
}