Linux Audio

Check our new training course

Loading...
v5.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
 
 
   4 * Author: Rob Clark <rob.clark@linaro.org>
 
 
 
 
 
 
 
 
 
 
 
 
   5 */
   6
   7#include <linux/dma-mapping.h>
   8#include <linux/seq_file.h>
   9#include <linux/shmem_fs.h>
  10#include <linux/spinlock.h>
  11#include <linux/pfn_t.h>
  12
  13#include <drm/drm_prime.h>
 
  14#include <drm/drm_vma_manager.h>
  15
  16#include "omap_drv.h"
  17#include "omap_dmm_tiler.h"
  18
 
 
 
 
 
 
  19/*
  20 * GEM buffer object implementation.
  21 */
  22
 
 
  23/* note: we use upper 8 bits of flags for driver-internal flags: */
  24#define OMAP_BO_MEM_DMA_API	0x01000000	/* memory allocated with the dma_alloc_* API */
  25#define OMAP_BO_MEM_SHMEM	0x02000000	/* memory allocated through shmem backing */
  26#define OMAP_BO_MEM_DMABUF	0x08000000	/* memory imported from a dmabuf */
 
  27
  28struct omap_gem_object {
  29	struct drm_gem_object base;
  30
  31	struct list_head mm_list;
  32
  33	u32 flags;
  34
  35	/** width/height for tiled formats (rounded up to slot boundaries) */
  36	u16 width, height;
  37
  38	/** roll applied when mapping to DMM */
  39	u32 roll;
  40
  41	/** protects dma_addr_cnt, block, pages, dma_addrs and vaddr */
  42	struct mutex lock;
  43
  44	/**
  45	 * dma_addr contains the buffer DMA address. It is valid for
 
 
 
 
 
  46	 *
  47	 * - buffers allocated through the DMA mapping API (with the
  48	 *   OMAP_BO_MEM_DMA_API flag set)
  49	 *
  50	 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
  51	 *   if they are physically contiguous (when sgt->orig_nents == 1)
  52	 *
  53	 * - buffers mapped through the TILER when dma_addr_cnt is not zero, in
  54	 *   which case the DMA address points to the TILER aperture
  55	 *
  56	 * Physically contiguous buffers have their DMA address equal to the
  57	 * physical address as we don't remap those buffers through the TILER.
  58	 *
  59	 * Buffers mapped to the TILER have their DMA address pointing to the
  60	 * TILER aperture. As TILER mappings are refcounted (through
  61	 * dma_addr_cnt) the DMA address must be accessed through omap_gem_pin()
  62	 * to ensure that the mapping won't disappear unexpectedly. References
  63	 * must be released with omap_gem_unpin().
  64	 */
  65	dma_addr_t dma_addr;
  66
  67	/**
  68	 * # of users of dma_addr
  69	 */
  70	u32 dma_addr_cnt;
  71
  72	/**
  73	 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
  74	 * is set and the sgt field is valid.
  75	 */
  76	struct sg_table *sgt;
  77
  78	/**
  79	 * tiler block used when buffer is remapped in DMM/TILER.
  80	 */
  81	struct tiler_block *block;
  82
  83	/**
  84	 * Array of backing pages, if allocated.  Note that pages are never
  85	 * allocated for buffers originally allocated from contiguous memory
  86	 */
  87	struct page **pages;
  88
  89	/** addresses corresponding to pages in above array */
  90	dma_addr_t *dma_addrs;
  91
  92	/**
  93	 * Virtual address, if mapped.
  94	 */
  95	void *vaddr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  96};
  97
  98#define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
 
  99
 100/* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
 101 * not necessarily pinned in TILER all the time, and (b) when they are
 102 * they are not necessarily page aligned, we reserve one or more small
 103 * regions in each of the 2d containers to use as a user-GART where we
 104 * can create a second page-aligned mapping of parts of the buffer
 105 * being accessed from userspace.
 106 *
 107 * Note that we could optimize slightly when we know that multiple
 108 * tiler containers are backed by the same PAT.. but I'll leave that
 109 * for later..
 110 */
 111#define NUM_USERGART_ENTRIES 2
 112struct omap_drm_usergart_entry {
 113	struct tiler_block *block;	/* the reserved tiler block */
 114	dma_addr_t dma_addr;
 115	struct drm_gem_object *obj;	/* the current pinned obj */
 116	pgoff_t obj_pgoff;		/* page offset of obj currently
 117					   mapped in */
 118};
 119
 120struct omap_drm_usergart {
 121	struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
 122	int height;				/* height in rows */
 123	int height_shift;		/* ilog2(height in rows) */
 124	int slot_shift;			/* ilog2(width per slot) */
 125	int stride_pfn;			/* stride in pages */
 126	int last;				/* index of last used entry */
 127};
 128
 129/* -----------------------------------------------------------------------------
 130 * Helpers
 131 */
 132
 133/** get mmap offset */
 134u64 omap_gem_mmap_offset(struct drm_gem_object *obj)
 135{
 136	struct drm_device *dev = obj->dev;
 137	int ret;
 138	size_t size;
 139
 140	/* Make it mmapable */
 141	size = omap_gem_mmap_size(obj);
 142	ret = drm_gem_create_mmap_offset_size(obj, size);
 143	if (ret) {
 144		dev_err(dev->dev, "could not allocate mmap offset\n");
 145		return 0;
 146	}
 147
 148	return drm_vma_node_offset_addr(&obj->vma_node);
 149}
 150
 151static bool omap_gem_is_contiguous(struct omap_gem_object *omap_obj)
 152{
 153	if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
 154		return true;
 155
 156	if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
 157		return true;
 158
 159	return false;
 160}
 161
 162/* -----------------------------------------------------------------------------
 163 * Eviction
 164 */
 165
 166static void omap_gem_evict_entry(struct drm_gem_object *obj,
 167		enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
 168{
 169	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 170	struct omap_drm_private *priv = obj->dev->dev_private;
 171	int n = priv->usergart[fmt].height;
 172	size_t size = PAGE_SIZE * n;
 173	loff_t off = omap_gem_mmap_offset(obj) +
 174			(entry->obj_pgoff << PAGE_SHIFT);
 175	const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
 176
 177	if (m > 1) {
 178		int i;
 179		/* if stride > than PAGE_SIZE then sparse mapping: */
 180		for (i = n; i > 0; i--) {
 181			unmap_mapping_range(obj->dev->anon_inode->i_mapping,
 182					    off, PAGE_SIZE, 1);
 183			off += PAGE_SIZE * m;
 184		}
 185	} else {
 186		unmap_mapping_range(obj->dev->anon_inode->i_mapping,
 187				    off, size, 1);
 188	}
 189
 190	entry->obj = NULL;
 191}
 192
 193/* Evict a buffer from usergart, if it is mapped there */
 194static void omap_gem_evict(struct drm_gem_object *obj)
 195{
 196	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 197	struct omap_drm_private *priv = obj->dev->dev_private;
 198
 199	if (omap_obj->flags & OMAP_BO_TILED) {
 200		enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
 201		int i;
 202
 203		for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
 204			struct omap_drm_usergart_entry *entry =
 205				&priv->usergart[fmt].entry[i];
 206
 
 
 207			if (entry->obj == obj)
 208				omap_gem_evict_entry(obj, fmt, entry);
 209		}
 210	}
 211}
 212
 213/* -----------------------------------------------------------------------------
 214 * Page Management
 
 
 
 
 215 */
 
 
 
 
 216
 217/*
 218 * Ensure backing pages are allocated. Must be called with the omap_obj.lock
 219 * held.
 220 */
 
 
 
 
 
 
 
 
 
 
 221static int omap_gem_attach_pages(struct drm_gem_object *obj)
 222{
 223	struct drm_device *dev = obj->dev;
 224	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 225	struct page **pages;
 226	int npages = obj->size >> PAGE_SHIFT;
 227	int i, ret;
 228	dma_addr_t *addrs;
 229
 230	lockdep_assert_held(&omap_obj->lock);
 231
 232	/*
 233	 * If not using shmem (in which case backing pages don't need to be
 234	 * allocated) or if pages are already allocated we're done.
 235	 */
 236	if (!(omap_obj->flags & OMAP_BO_MEM_SHMEM) || omap_obj->pages)
 237		return 0;
 238
 239	pages = drm_gem_get_pages(obj);
 240	if (IS_ERR(pages)) {
 241		dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
 242		return PTR_ERR(pages);
 243	}
 244
 245	/* for non-cached buffers, ensure the new pages are clean because
 246	 * DSS, GPU, etc. are not cache coherent:
 247	 */
 248	if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
 249		addrs = kmalloc_array(npages, sizeof(*addrs), GFP_KERNEL);
 250		if (!addrs) {
 251			ret = -ENOMEM;
 252			goto free_pages;
 253		}
 254
 255		for (i = 0; i < npages; i++) {
 256			addrs[i] = dma_map_page(dev->dev, pages[i],
 257					0, PAGE_SIZE, DMA_TO_DEVICE);
 258
 259			if (dma_mapping_error(dev->dev, addrs[i])) {
 260				dev_warn(dev->dev,
 261					"%s: failed to map page\n", __func__);
 262
 263				for (i = i - 1; i >= 0; --i) {
 264					dma_unmap_page(dev->dev, addrs[i],
 265						PAGE_SIZE, DMA_TO_DEVICE);
 266				}
 267
 268				ret = -ENOMEM;
 269				goto free_addrs;
 270			}
 271		}
 272	} else {
 273		addrs = kcalloc(npages, sizeof(*addrs), GFP_KERNEL);
 274		if (!addrs) {
 275			ret = -ENOMEM;
 276			goto free_pages;
 277		}
 278	}
 279
 280	omap_obj->dma_addrs = addrs;
 281	omap_obj->pages = pages;
 282
 283	return 0;
 284
 285free_addrs:
 286	kfree(addrs);
 287free_pages:
 288	drm_gem_put_pages(obj, pages, true, false);
 289
 290	return ret;
 291}
 292
 293/* Release backing pages. Must be called with the omap_obj.lock held. */
 294static void omap_gem_detach_pages(struct drm_gem_object *obj)
 295{
 296	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 297	unsigned int npages = obj->size >> PAGE_SHIFT;
 298	unsigned int i;
 299
 300	lockdep_assert_held(&omap_obj->lock);
 301
 302	for (i = 0; i < npages; i++) {
 303		if (omap_obj->dma_addrs[i])
 304			dma_unmap_page(obj->dev->dev, omap_obj->dma_addrs[i],
 305				       PAGE_SIZE, DMA_TO_DEVICE);
 
 
 
 306	}
 307
 308	kfree(omap_obj->dma_addrs);
 309	omap_obj->dma_addrs = NULL;
 310
 311	drm_gem_put_pages(obj, omap_obj->pages, true, false);
 312	omap_obj->pages = NULL;
 313}
 314
 315/* get buffer flags */
 316u32 omap_gem_flags(struct drm_gem_object *obj)
 317{
 318	return to_omap_bo(obj)->flags;
 319}
 320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 321/** get mmap size */
 322size_t omap_gem_mmap_size(struct drm_gem_object *obj)
 323{
 324	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 325	size_t size = obj->size;
 326
 327	if (omap_obj->flags & OMAP_BO_TILED) {
 328		/* for tiled buffers, the virtual size has stride rounded up
 329		 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
 330		 * 32kb later!).  But we don't back the entire buffer with
 331		 * pages, only the valid picture part.. so need to adjust for
 332		 * this in the size used to mmap and generate mmap offset
 333		 */
 334		size = tiler_vsize(gem2fmt(omap_obj->flags),
 335				omap_obj->width, omap_obj->height);
 336	}
 337
 338	return size;
 339}
 340
 341/* -----------------------------------------------------------------------------
 342 * Fault Handling
 343 */
 
 
 
 
 
 
 
 
 344
 345/* Normal handling for the case of faulting in non-tiled buffers */
 346static vm_fault_t omap_gem_fault_1d(struct drm_gem_object *obj,
 347		struct vm_area_struct *vma, struct vm_fault *vmf)
 348{
 349	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 350	unsigned long pfn;
 351	pgoff_t pgoff;
 352
 353	/* We don't use vmf->pgoff since that has the fake offset: */
 354	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
 
 355
 356	if (omap_obj->pages) {
 357		omap_gem_cpu_sync_page(obj, pgoff);
 358		pfn = page_to_pfn(omap_obj->pages[pgoff]);
 359	} else {
 360		BUG_ON(!omap_gem_is_contiguous(omap_obj));
 361		pfn = (omap_obj->dma_addr >> PAGE_SHIFT) + pgoff;
 362	}
 363
 364	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
 365			pfn, pfn << PAGE_SHIFT);
 366
 367	return vmf_insert_mixed(vma, vmf->address,
 368			__pfn_to_pfn_t(pfn, PFN_DEV));
 369}
 370
 371/* Special handling for the case of faulting in 2d tiled buffers */
 372static vm_fault_t omap_gem_fault_2d(struct drm_gem_object *obj,
 373		struct vm_area_struct *vma, struct vm_fault *vmf)
 374{
 375	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 376	struct omap_drm_private *priv = obj->dev->dev_private;
 377	struct omap_drm_usergart_entry *entry;
 378	enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
 379	struct page *pages[64];  /* XXX is this too much to have on stack? */
 380	unsigned long pfn;
 381	pgoff_t pgoff, base_pgoff;
 382	unsigned long vaddr;
 383	int i, err, slots;
 384	vm_fault_t ret = VM_FAULT_NOPAGE;
 385
 386	/*
 387	 * Note the height of the slot is also equal to the number of pages
 388	 * that need to be mapped in to fill 4kb wide CPU page.  If the slot
 389	 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
 390	 */
 391	const int n = priv->usergart[fmt].height;
 392	const int n_shift = priv->usergart[fmt].height_shift;
 393
 394	/*
 395	 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
 396	 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
 397	 * into account in some of the math, so figure out virtual stride
 398	 * in pages
 399	 */
 400	const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
 401
 402	/* We don't use vmf->pgoff since that has the fake offset: */
 403	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
 
 404
 405	/*
 406	 * Actual address we start mapping at is rounded down to previous slot
 407	 * boundary in the y direction:
 408	 */
 409	base_pgoff = round_down(pgoff, m << n_shift);
 410
 411	/* figure out buffer width in slots */
 412	slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
 413
 414	vaddr = vmf->address - ((pgoff - base_pgoff) << PAGE_SHIFT);
 415
 416	entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
 417
 418	/* evict previous buffer using this usergart entry, if any: */
 419	if (entry->obj)
 420		omap_gem_evict_entry(entry->obj, fmt, entry);
 421
 422	entry->obj = obj;
 423	entry->obj_pgoff = base_pgoff;
 424
 425	/* now convert base_pgoff to phys offset from virt offset: */
 426	base_pgoff = (base_pgoff >> n_shift) * slots;
 427
 428	/* for wider-than 4k.. figure out which part of the slot-row we want: */
 429	if (m > 1) {
 430		int off = pgoff % m;
 431		entry->obj_pgoff += off;
 432		base_pgoff /= m;
 433		slots = min(slots - (off << n_shift), n);
 434		base_pgoff += off << n_shift;
 435		vaddr += off << PAGE_SHIFT;
 436	}
 437
 438	/*
 439	 * Map in pages. Beyond the valid pixel part of the buffer, we set
 440	 * pages[i] to NULL to get a dummy page mapped in.. if someone
 441	 * reads/writes it they will get random/undefined content, but at
 442	 * least it won't be corrupting whatever other random page used to
 443	 * be mapped in, or other undefined behavior.
 444	 */
 445	memcpy(pages, &omap_obj->pages[base_pgoff],
 446			sizeof(struct page *) * slots);
 447	memset(pages + slots, 0,
 448			sizeof(struct page *) * (n - slots));
 449
 450	err = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
 451	if (err) {
 452		ret = vmf_error(err);
 453		dev_err(obj->dev->dev, "failed to pin: %d\n", err);
 454		return ret;
 455	}
 456
 457	pfn = entry->dma_addr >> PAGE_SHIFT;
 458
 459	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
 460			pfn, pfn << PAGE_SHIFT);
 461
 462	for (i = n; i > 0; i--) {
 463		ret = vmf_insert_mixed(vma,
 464			vaddr, __pfn_to_pfn_t(pfn, PFN_DEV));
 465		if (ret & VM_FAULT_ERROR)
 466			break;
 467		pfn += priv->usergart[fmt].stride_pfn;
 468		vaddr += PAGE_SIZE * m;
 469	}
 470
 471	/* simple round-robin: */
 472	priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
 473				 % NUM_USERGART_ENTRIES;
 474
 475	return ret;
 476}
 477
 478/**
 479 * omap_gem_fault		-	pagefault handler for GEM objects
 
 480 * @vmf: fault detail
 481 *
 482 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
 483 * does most of the work for us including the actual map/unmap calls
 484 * but we need to do the actual page work.
 485 *
 486 * The VMA was set up by GEM. In doing so it also ensured that the
 487 * vma->vm_private_data points to the GEM object that is backing this
 488 * mapping.
 489 */
 490vm_fault_t omap_gem_fault(struct vm_fault *vmf)
 491{
 492	struct vm_area_struct *vma = vmf->vma;
 493	struct drm_gem_object *obj = vma->vm_private_data;
 494	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 495	int err;
 496	vm_fault_t ret;
 
 497
 498	/* Make sure we don't parallel update on a fault, nor move or remove
 499	 * something from beneath our feet
 500	 */
 501	mutex_lock(&omap_obj->lock);
 502
 503	/* if a shmem backed object, make sure we have pages attached now */
 504	err = omap_gem_attach_pages(obj);
 505	if (err) {
 506		ret = vmf_error(err);
 507		goto fail;
 508	}
 509
 510	/* where should we do corresponding put_pages().. we are mapping
 511	 * the original page, rather than thru a GART, so we can't rely
 512	 * on eviction to trigger this.  But munmap() or all mappings should
 513	 * probably trigger put_pages()?
 514	 */
 515
 516	if (omap_obj->flags & OMAP_BO_TILED)
 517		ret = omap_gem_fault_2d(obj, vma, vmf);
 518	else
 519		ret = omap_gem_fault_1d(obj, vma, vmf);
 520
 521
 522fail:
 523	mutex_unlock(&omap_obj->lock);
 524	return ret;
 
 
 
 
 
 
 
 
 
 525}
 526
 527/** We override mainly to fix up some of the vm mapping flags.. */
 528int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 529{
 530	int ret;
 531
 532	ret = drm_gem_mmap(filp, vma);
 533	if (ret) {
 534		DBG("mmap failed: %d", ret);
 535		return ret;
 536	}
 537
 538	return omap_gem_mmap_obj(vma->vm_private_data, vma);
 539}
 540
 541int omap_gem_mmap_obj(struct drm_gem_object *obj,
 542		struct vm_area_struct *vma)
 543{
 544	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 545
 546	vma->vm_flags &= ~VM_PFNMAP;
 547	vma->vm_flags |= VM_MIXEDMAP;
 548
 549	if (omap_obj->flags & OMAP_BO_WC) {
 550		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
 551	} else if (omap_obj->flags & OMAP_BO_UNCACHED) {
 552		vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
 553	} else {
 554		/*
 555		 * We do have some private objects, at least for scanout buffers
 556		 * on hardware without DMM/TILER.  But these are allocated write-
 557		 * combine
 558		 */
 559		if (WARN_ON(!obj->filp))
 560			return -EINVAL;
 561
 562		/*
 563		 * Shunt off cached objs to shmem file so they have their own
 564		 * address_space (so unmap_mapping_range does what we want,
 565		 * in particular in the case of mmap'd dmabufs)
 566		 */
 567		fput(vma->vm_file);
 568		vma->vm_pgoff = 0;
 569		vma->vm_file  = get_file(obj->filp);
 570
 571		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
 572	}
 573
 574	return 0;
 575}
 576
 577/* -----------------------------------------------------------------------------
 578 * Dumb Buffers
 579 */
 580
 581/**
 582 * omap_gem_dumb_create	-	create a dumb buffer
 583 * @drm_file: our client file
 584 * @dev: our device
 585 * @args: the requested arguments copied from userspace
 586 *
 587 * Allocate a buffer suitable for use for a frame buffer of the
 588 * form described by user space. Give userspace a handle by which
 589 * to reference it.
 590 */
 591int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
 592		struct drm_mode_create_dumb *args)
 593{
 594	union omap_gem_size gsize;
 595
 596	args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
 597
 598	args->size = PAGE_ALIGN(args->pitch * args->height);
 599
 600	gsize = (union omap_gem_size){
 601		.bytes = args->size,
 602	};
 603
 604	return omap_gem_new_handle(dev, file, gsize,
 605			OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
 606}
 607
 608/**
 609 * omap_gem_dumb_map	-	buffer mapping for dumb interface
 610 * @file: our drm client file
 611 * @dev: drm device
 612 * @handle: GEM handle to the object (from dumb_create)
 613 *
 614 * Do the necessary setup to allow the mapping of the frame buffer
 615 * into user memory. We don't have to do much here at the moment.
 616 */
 617int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
 618		u32 handle, u64 *offset)
 619{
 620	struct drm_gem_object *obj;
 621	int ret = 0;
 622
 623	/* GEM does all our handle to object mapping */
 624	obj = drm_gem_object_lookup(file, handle);
 625	if (obj == NULL) {
 626		ret = -ENOENT;
 627		goto fail;
 628	}
 629
 630	*offset = omap_gem_mmap_offset(obj);
 631
 632	drm_gem_object_put_unlocked(obj);
 633
 634fail:
 635	return ret;
 636}
 637
 638#ifdef CONFIG_DRM_FBDEV_EMULATION
 639/* Set scrolling position.  This allows us to implement fast scrolling
 640 * for console.
 641 *
 642 * Call only from non-atomic contexts.
 643 */
 644int omap_gem_roll(struct drm_gem_object *obj, u32 roll)
 645{
 646	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 647	u32 npages = obj->size >> PAGE_SHIFT;
 648	int ret = 0;
 649
 650	if (roll > npages) {
 651		dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
 652		return -EINVAL;
 653	}
 654
 655	omap_obj->roll = roll;
 656
 657	mutex_lock(&omap_obj->lock);
 658
 659	/* if we aren't mapped yet, we don't need to do anything */
 660	if (omap_obj->block) {
 661		ret = omap_gem_attach_pages(obj);
 
 662		if (ret)
 663			goto fail;
 664
 665		ret = tiler_pin(omap_obj->block, omap_obj->pages, npages,
 666				roll, true);
 667		if (ret)
 668			dev_err(obj->dev->dev, "could not repin: %d\n", ret);
 669	}
 670
 671fail:
 672	mutex_unlock(&omap_obj->lock);
 673
 674	return ret;
 675}
 676#endif
 677
 678/* -----------------------------------------------------------------------------
 679 * Memory Management & DMA Sync
 680 */
 681
 682/*
 683 * shmem buffers that are mapped cached are not coherent.
 684 *
 685 * We keep track of dirty pages using page faulting to perform cache management.
 686 * When a page is mapped to the CPU in read/write mode the device can't access
 687 * it and omap_obj->dma_addrs[i] is NULL. When a page is mapped to the device
 688 * the omap_obj->dma_addrs[i] is set to the DMA address, and the page is
 689 * unmapped from the CPU.
 690 */
 691static inline bool omap_gem_is_cached_coherent(struct drm_gem_object *obj)
 692{
 693	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 694
 695	return !((omap_obj->flags & OMAP_BO_MEM_SHMEM) &&
 696		((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED));
 697}
 698
 699/* Sync the buffer for CPU access.. note pages should already be
 700 * attached, ie. omap_gem_get_pages()
 701 */
 702void omap_gem_cpu_sync_page(struct drm_gem_object *obj, int pgoff)
 703{
 704	struct drm_device *dev = obj->dev;
 705	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 706
 707	if (omap_gem_is_cached_coherent(obj))
 708		return;
 709
 710	if (omap_obj->dma_addrs[pgoff]) {
 711		dma_unmap_page(dev->dev, omap_obj->dma_addrs[pgoff],
 712				PAGE_SIZE, DMA_TO_DEVICE);
 713		omap_obj->dma_addrs[pgoff] = 0;
 714	}
 715}
 716
 717/* sync the buffer for DMA access */
 718void omap_gem_dma_sync_buffer(struct drm_gem_object *obj,
 719		enum dma_data_direction dir)
 720{
 721	struct drm_device *dev = obj->dev;
 722	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 723	int i, npages = obj->size >> PAGE_SHIFT;
 724	struct page **pages = omap_obj->pages;
 725	bool dirty = false;
 726
 727	if (omap_gem_is_cached_coherent(obj))
 728		return;
 
 
 729
 730	for (i = 0; i < npages; i++) {
 731		if (!omap_obj->dma_addrs[i]) {
 732			dma_addr_t addr;
 733
 734			addr = dma_map_page(dev->dev, pages[i], 0,
 735					    PAGE_SIZE, dir);
 736			if (dma_mapping_error(dev->dev, addr)) {
 737				dev_warn(dev->dev, "%s: failed to map page\n",
 738					__func__);
 739				break;
 740			}
 741
 742			dirty = true;
 743			omap_obj->dma_addrs[i] = addr;
 744		}
 745	}
 746
 747	if (dirty) {
 748		unmap_mapping_range(obj->filp->f_mapping, 0,
 749				    omap_gem_mmap_size(obj), 1);
 
 750	}
 751}
 752
 753/**
 754 * omap_gem_pin() - Pin a GEM object in memory
 755 * @obj: the GEM object
 756 * @dma_addr: the DMA address
 757 *
 758 * Pin the given GEM object in memory and fill the dma_addr pointer with the
 759 * object's DMA address. If the buffer is not physically contiguous it will be
 760 * remapped through the TILER to provide a contiguous view.
 761 *
 762 * Pins are reference-counted, calling this function multiple times is allowed
 763 * as long the corresponding omap_gem_unpin() calls are balanced.
 764 *
 765 * Return 0 on success or a negative error code otherwise.
 766 */
 767int omap_gem_pin(struct drm_gem_object *obj, dma_addr_t *dma_addr)
 
 768{
 769	struct omap_drm_private *priv = obj->dev->dev_private;
 770	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 771	int ret = 0;
 772
 773	mutex_lock(&omap_obj->lock);
 774
 775	if (!omap_gem_is_contiguous(omap_obj) && priv->has_dmm) {
 776		if (omap_obj->dma_addr_cnt == 0) {
 777			u32 npages = obj->size >> PAGE_SHIFT;
 
 778			enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
 779			struct tiler_block *block;
 780
 781			BUG_ON(omap_obj->block);
 782
 783			ret = omap_gem_attach_pages(obj);
 784			if (ret)
 785				goto fail;
 786
 787			if (omap_obj->flags & OMAP_BO_TILED) {
 788				block = tiler_reserve_2d(fmt,
 789						omap_obj->width,
 790						omap_obj->height, 0);
 791			} else {
 792				block = tiler_reserve_1d(obj->size);
 793			}
 794
 795			if (IS_ERR(block)) {
 796				ret = PTR_ERR(block);
 797				dev_err(obj->dev->dev,
 798					"could not remap: %d (%d)\n", ret, fmt);
 799				goto fail;
 800			}
 801
 802			/* TODO: enable async refill.. */
 803			ret = tiler_pin(block, omap_obj->pages, npages,
 804					omap_obj->roll, true);
 805			if (ret) {
 806				tiler_release(block);
 807				dev_err(obj->dev->dev,
 808						"could not pin: %d\n", ret);
 809				goto fail;
 810			}
 811
 812			omap_obj->dma_addr = tiler_ssptr(block);
 813			omap_obj->block = block;
 814
 815			DBG("got dma address: %pad", &omap_obj->dma_addr);
 816		}
 817
 818		omap_obj->dma_addr_cnt++;
 819
 820		*dma_addr = omap_obj->dma_addr;
 821	} else if (omap_gem_is_contiguous(omap_obj)) {
 822		*dma_addr = omap_obj->dma_addr;
 823	} else {
 824		ret = -EINVAL;
 825		goto fail;
 826	}
 827
 828fail:
 829	mutex_unlock(&omap_obj->lock);
 830
 831	return ret;
 832}
 833
 834/**
 835 * omap_gem_unpin() - Unpin a GEM object from memory
 836 * @obj: the GEM object
 837 *
 838 * Unpin the given GEM object previously pinned with omap_gem_pin(). Pins are
 839 * reference-counted, the actualy unpin will only be performed when the number
 840 * of calls to this function matches the number of calls to omap_gem_pin().
 841 */
 842void omap_gem_unpin(struct drm_gem_object *obj)
 843{
 844	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 845	int ret;
 846
 847	mutex_lock(&omap_obj->lock);
 848
 849	if (omap_obj->dma_addr_cnt > 0) {
 850		omap_obj->dma_addr_cnt--;
 851		if (omap_obj->dma_addr_cnt == 0) {
 
 852			ret = tiler_unpin(omap_obj->block);
 853			if (ret) {
 854				dev_err(obj->dev->dev,
 855					"could not unpin pages: %d\n", ret);
 
 856			}
 857			ret = tiler_release(omap_obj->block);
 858			if (ret) {
 859				dev_err(obj->dev->dev,
 860					"could not release unmap: %d\n", ret);
 861			}
 862			omap_obj->dma_addr = 0;
 863			omap_obj->block = NULL;
 864		}
 865	}
 866
 867	mutex_unlock(&omap_obj->lock);
 
 868}
 869
 870/* Get rotated scanout address (only valid if already pinned), at the
 871 * specified orientation and x,y offset from top-left corner of buffer
 872 * (only valid for tiled 2d buffers)
 873 */
 874int omap_gem_rotated_dma_addr(struct drm_gem_object *obj, u32 orient,
 875		int x, int y, dma_addr_t *dma_addr)
 876{
 877	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 878	int ret = -EINVAL;
 879
 880	mutex_lock(&omap_obj->lock);
 881
 882	if ((omap_obj->dma_addr_cnt > 0) && omap_obj->block &&
 883			(omap_obj->flags & OMAP_BO_TILED)) {
 884		*dma_addr = tiler_tsptr(omap_obj->block, orient, x, y);
 885		ret = 0;
 886	}
 887
 888	mutex_unlock(&omap_obj->lock);
 889
 890	return ret;
 891}
 892
 893/* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
 894int omap_gem_tiled_stride(struct drm_gem_object *obj, u32 orient)
 895{
 896	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 897	int ret = -EINVAL;
 898	if (omap_obj->flags & OMAP_BO_TILED)
 899		ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
 900	return ret;
 901}
 902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 903/* if !remap, and we don't have pages backing, then fail, rather than
 904 * increasing the pin count (which we don't really do yet anyways,
 905 * because we don't support swapping pages back out).  And 'remap'
 906 * might not be quite the right name, but I wanted to keep it working
 907 * similarly to omap_gem_pin().  Note though that mutex is not
 908 * aquired if !remap (because this can be called in atomic ctxt),
 909 * but probably omap_gem_unpin() should be changed to work in the
 910 * same way.  If !remap, a matching omap_gem_put_pages() call is not
 911 * required (and should not be made).
 912 */
 913int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
 914		bool remap)
 915{
 916	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 917	int ret = 0;
 918
 919	mutex_lock(&omap_obj->lock);
 920
 921	if (remap) {
 922		ret = omap_gem_attach_pages(obj);
 923		if (ret)
 924			goto unlock;
 925	}
 926
 927	if (!omap_obj->pages) {
 928		ret = -ENOMEM;
 929		goto unlock;
 930	}
 931
 932	*pages = omap_obj->pages;
 933
 934unlock:
 935	mutex_unlock(&omap_obj->lock);
 936
 937	return ret;
 938}
 939
 940/* release pages when DMA no longer being performed */
 941int omap_gem_put_pages(struct drm_gem_object *obj)
 942{
 943	/* do something here if we dynamically attach/detach pages.. at
 944	 * least they would no longer need to be pinned if everyone has
 945	 * released the pages..
 946	 */
 947	return 0;
 948}
 949
 950#ifdef CONFIG_DRM_FBDEV_EMULATION
 951/*
 952 * Get kernel virtual address for CPU access.. this more or less only
 953 * exists for omap_fbdev.
 954 */
 955void *omap_gem_vaddr(struct drm_gem_object *obj)
 956{
 957	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 958	void *vaddr;
 959	int ret;
 960
 961	mutex_lock(&omap_obj->lock);
 962
 963	if (!omap_obj->vaddr) {
 964		ret = omap_gem_attach_pages(obj);
 965		if (ret) {
 966			vaddr = ERR_PTR(ret);
 967			goto unlock;
 968		}
 969
 970		omap_obj->vaddr = vmap(omap_obj->pages, obj->size >> PAGE_SHIFT,
 971				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
 972	}
 973
 974	vaddr = omap_obj->vaddr;
 975
 976unlock:
 977	mutex_unlock(&omap_obj->lock);
 978	return vaddr;
 979}
 980#endif
 981
 982/* -----------------------------------------------------------------------------
 983 * Power Management
 984 */
 985
 986#ifdef CONFIG_PM
 987/* re-pin objects in DMM in resume path: */
 988int omap_gem_resume(struct drm_device *dev)
 989{
 990	struct omap_drm_private *priv = dev->dev_private;
 
 991	struct omap_gem_object *omap_obj;
 992	int ret = 0;
 993
 994	mutex_lock(&priv->list_lock);
 995	list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
 996		if (omap_obj->block) {
 997			struct drm_gem_object *obj = &omap_obj->base;
 998			u32 npages = obj->size >> PAGE_SHIFT;
 999
1000			WARN_ON(!omap_obj->pages);  /* this can't happen */
1001			ret = tiler_pin(omap_obj->block,
1002					omap_obj->pages, npages,
1003					omap_obj->roll, true);
1004			if (ret) {
1005				dev_err(dev->dev, "could not repin: %d\n", ret);
1006				goto done;
1007			}
1008		}
1009	}
1010
1011done:
1012	mutex_unlock(&priv->list_lock);
1013	return ret;
1014}
1015#endif
1016
1017/* -----------------------------------------------------------------------------
1018 * DebugFS
1019 */
1020
1021#ifdef CONFIG_DEBUG_FS
1022void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
1023{
1024	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1025	u64 off;
1026
1027	off = drm_vma_node_start(&obj->vma_node);
1028
1029	mutex_lock(&omap_obj->lock);
1030
1031	seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
1032			omap_obj->flags, obj->name, kref_read(&obj->refcount),
1033			off, &omap_obj->dma_addr, omap_obj->dma_addr_cnt,
1034			omap_obj->vaddr, omap_obj->roll);
1035
1036	if (omap_obj->flags & OMAP_BO_TILED) {
1037		seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1038		if (omap_obj->block) {
1039			struct tcm_area *area = &omap_obj->block->area;
1040			seq_printf(m, " (%dx%d, %dx%d)",
1041					area->p0.x, area->p0.y,
1042					area->p1.x, area->p1.y);
1043		}
1044	} else {
1045		seq_printf(m, " %zu", obj->size);
1046	}
1047
1048	mutex_unlock(&omap_obj->lock);
1049
1050	seq_printf(m, "\n");
1051}
1052
1053void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1054{
1055	struct omap_gem_object *omap_obj;
1056	int count = 0;
1057	size_t size = 0;
1058
1059	list_for_each_entry(omap_obj, list, mm_list) {
1060		struct drm_gem_object *obj = &omap_obj->base;
1061		seq_printf(m, "   ");
1062		omap_gem_describe(obj, m);
1063		count++;
1064		size += obj->size;
1065	}
1066
1067	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1068}
1069#endif
1070
1071/* -----------------------------------------------------------------------------
1072 * Constructor & Destructor
1073 */
1074
1075void omap_gem_free_object(struct drm_gem_object *obj)
1076{
1077	struct drm_device *dev = obj->dev;
1078	struct omap_drm_private *priv = dev->dev_private;
1079	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 
 
 
 
1080
1081	omap_gem_evict(obj);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1082
1083	mutex_lock(&priv->list_lock);
1084	list_del(&omap_obj->mm_list);
1085	mutex_unlock(&priv->list_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1086
1087	/*
1088	 * We own the sole reference to the object at this point, but to keep
1089	 * lockdep happy, we must still take the omap_obj_lock to call
1090	 * omap_gem_detach_pages(). This should hardly make any difference as
1091	 * there can't be any lock contention.
1092	 */
1093	mutex_lock(&omap_obj->lock);
1094
1095	/* The object should not be pinned. */
1096	WARN_ON(omap_obj->dma_addr_cnt > 0);
1097
1098	if (omap_obj->pages) {
1099		if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1100			kfree(omap_obj->pages);
1101		else
1102			omap_gem_detach_pages(obj);
 
1103	}
1104
1105	if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
1106		dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1107			    omap_obj->dma_addr);
1108	} else if (omap_obj->vaddr) {
1109		vunmap(omap_obj->vaddr);
1110	} else if (obj->import_attach) {
1111		drm_prime_gem_destroy(obj, omap_obj->sgt);
 
 
 
 
1112	}
1113
1114	mutex_unlock(&omap_obj->lock);
1115
1116	drm_gem_object_release(obj);
1117
1118	mutex_destroy(&omap_obj->lock);
 
1119
1120	kfree(omap_obj);
 
 
 
 
 
 
 
 
 
 
 
1121}
1122
1123/* GEM buffer object constructor */
1124struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1125		union omap_gem_size gsize, u32 flags)
1126{
1127	struct omap_drm_private *priv = dev->dev_private;
1128	struct omap_gem_object *omap_obj;
1129	struct drm_gem_object *obj;
1130	struct address_space *mapping;
1131	size_t size;
1132	int ret;
1133
1134	/* Validate the flags and compute the memory and cache flags. */
1135	if (flags & OMAP_BO_TILED) {
1136		if (!priv->usergart) {
1137			dev_err(dev->dev, "Tiled buffers require DMM\n");
1138			return NULL;
1139		}
1140
1141		/*
1142		 * Tiled buffers are always shmem paged backed. When they are
1143		 * scanned out, they are remapped into DMM/TILER.
1144		 */
1145		flags &= ~OMAP_BO_SCANOUT;
1146		flags |= OMAP_BO_MEM_SHMEM;
1147
1148		/*
1149		 * Currently don't allow cached buffers. There is some caching
1150		 * stuff that needs to be handled better.
1151		 */
1152		flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1153		flags |= tiler_get_cpu_cache_flags();
1154	} else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1155		/*
1156		 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be
1157		 * tiled. However, to lower the pressure on memory allocation,
1158		 * use contiguous memory only if no TILER is available.
1159		 */
1160		flags |= OMAP_BO_MEM_DMA_API;
1161	} else if (!(flags & OMAP_BO_MEM_DMABUF)) {
1162		/*
1163		 * All other buffers not backed by dma_buf are shmem-backed.
1164		 */
1165		flags |= OMAP_BO_MEM_SHMEM;
1166	}
1167
1168	/* Allocate the initialize the OMAP GEM object. */
1169	omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1170	if (!omap_obj)
1171		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1172
1173	obj = &omap_obj->base;
1174	omap_obj->flags = flags;
1175	mutex_init(&omap_obj->lock);
 
 
1176
1177	if (flags & OMAP_BO_TILED) {
1178		/*
1179		 * For tiled buffers align dimensions to slot boundaries and
1180		 * calculate size based on aligned dimensions.
1181		 */
1182		tiler_align(gem2fmt(flags), &gsize.tiled.width,
1183			    &gsize.tiled.height);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1184
1185		size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1186				  gsize.tiled.height);
1187
1188		omap_obj->width = gsize.tiled.width;
1189		omap_obj->height = gsize.tiled.height;
1190	} else {
1191		size = PAGE_ALIGN(gsize.bytes);
1192	}
1193
1194	/* Initialize the GEM object. */
1195	if (!(flags & OMAP_BO_MEM_SHMEM)) {
1196		drm_gem_private_object_init(dev, obj, size);
1197	} else {
1198		ret = drm_gem_object_init(dev, obj, size);
1199		if (ret)
1200			goto err_free;
1201
1202		mapping = obj->filp->f_mapping;
1203		mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1204	}
1205
1206	/* Allocate memory if needed. */
1207	if (flags & OMAP_BO_MEM_DMA_API) {
1208		omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1209					       &omap_obj->dma_addr,
1210					       GFP_KERNEL);
1211		if (!omap_obj->vaddr)
1212			goto err_release;
1213	}
 
1214
1215	mutex_lock(&priv->list_lock);
1216	list_add(&omap_obj->mm_list, &priv->obj_list);
1217	mutex_unlock(&priv->list_lock);
1218
1219	return obj;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1220
1221err_release:
1222	drm_gem_object_release(obj);
1223err_free:
1224	kfree(omap_obj);
1225	return NULL;
1226}
1227
1228struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1229					   struct sg_table *sgt)
 
 
1230{
1231	struct omap_drm_private *priv = dev->dev_private;
1232	struct omap_gem_object *omap_obj;
1233	struct drm_gem_object *obj;
1234	union omap_gem_size gsize;
1235
1236	/* Without a DMM only physically contiguous buffers can be supported. */
1237	if (sgt->orig_nents != 1 && !priv->has_dmm)
1238		return ERR_PTR(-EINVAL);
1239
1240	gsize.bytes = PAGE_ALIGN(size);
1241	obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1242	if (!obj)
1243		return ERR_PTR(-ENOMEM);
1244
1245	omap_obj = to_omap_bo(obj);
1246
1247	mutex_lock(&omap_obj->lock);
1248
1249	omap_obj->sgt = sgt;
 
 
 
1250
1251	if (sgt->orig_nents == 1) {
1252		omap_obj->dma_addr = sg_dma_address(sgt->sgl);
1253	} else {
1254		/* Create pages list from sgt */
1255		struct sg_page_iter iter;
1256		struct page **pages;
1257		unsigned int npages;
1258		unsigned int i = 0;
1259
1260		npages = DIV_ROUND_UP(size, PAGE_SIZE);
1261		pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1262		if (!pages) {
1263			omap_gem_free_object(obj);
1264			obj = ERR_PTR(-ENOMEM);
1265			goto done;
1266		}
 
1267
1268		omap_obj->pages = pages;
1269
1270		for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1271			pages[i++] = sg_page_iter_page(&iter);
1272			if (i > npages)
1273				break;
1274		}
1275
1276		if (WARN_ON(i != npages)) {
1277			omap_gem_free_object(obj);
1278			obj = ERR_PTR(-ENOMEM);
1279			goto done;
1280		}
1281	}
1282
1283done:
1284	mutex_unlock(&omap_obj->lock);
1285	return obj;
1286}
1287
1288/* convenience method to construct a GEM buffer object, and userspace handle */
1289int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1290		union omap_gem_size gsize, u32 flags, u32 *handle)
1291{
1292	struct drm_gem_object *obj;
1293	int ret;
1294
1295	obj = omap_gem_new(dev, gsize, flags);
1296	if (!obj)
1297		return -ENOMEM;
1298
1299	ret = drm_gem_handle_create(file, obj, handle);
1300	if (ret) {
1301		omap_gem_free_object(obj);
 
1302		return ret;
1303	}
1304
1305	/* drop reference from allocate - handle holds it now */
1306	drm_gem_object_put_unlocked(obj);
1307
1308	return 0;
1309}
1310
1311/* -----------------------------------------------------------------------------
1312 * Init & Cleanup
1313 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1314
1315/* If DMM is used, we need to set some stuff up.. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1316void omap_gem_init(struct drm_device *dev)
1317{
1318	struct omap_drm_private *priv = dev->dev_private;
1319	struct omap_drm_usergart *usergart;
1320	const enum tiler_fmt fmts[] = {
1321			TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1322	};
1323	int i, j;
1324
1325	if (!dmm_is_available()) {
1326		/* DMM only supported on OMAP4 and later, so this isn't fatal */
1327		dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1328		return;
1329	}
1330
1331	usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1332	if (!usergart)
1333		return;
1334
1335	/* reserve 4k aligned/wide regions for userspace mappings: */
1336	for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1337		u16 h = 1, w = PAGE_SIZE >> i;
1338
1339		tiler_align(fmts[i], &w, &h);
1340		/* note: since each region is 1 4kb page wide, and minimum
1341		 * number of rows, the height ends up being the same as the
1342		 * # of pages in the region
1343		 */
1344		usergart[i].height = h;
1345		usergart[i].height_shift = ilog2(h);
1346		usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1347		usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1348		for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1349			struct omap_drm_usergart_entry *entry;
1350			struct tiler_block *block;
1351
1352			entry = &usergart[i].entry[j];
1353			block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
1354			if (IS_ERR(block)) {
1355				dev_err(dev->dev,
1356						"reserve failed: %d, %d, %ld\n",
1357						i, j, PTR_ERR(block));
1358				return;
1359			}
1360			entry->dma_addr = tiler_ssptr(block);
1361			entry->block = block;
1362
1363			DBG("%d:%d: %dx%d: dma_addr=%pad stride=%d", i, j, w, h,
1364					&entry->dma_addr,
1365					usergart[i].stride_pfn << PAGE_SHIFT);
1366		}
1367	}
1368
1369	priv->usergart = usergart;
1370	priv->has_dmm = true;
1371}
1372
1373void omap_gem_deinit(struct drm_device *dev)
1374{
1375	struct omap_drm_private *priv = dev->dev_private;
1376
1377	/* I believe we can rely on there being no more outstanding GEM
1378	 * objects which could depend on usergart/dmm at this point.
1379	 */
1380	kfree(priv->usergart);
1381}
v3.15
 
   1/*
   2 * drivers/gpu/drm/omapdrm/omap_gem.c
   3 *
   4 * Copyright (C) 2011 Texas Instruments
   5 * Author: Rob Clark <rob.clark@linaro.org>
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms of the GNU General Public License version 2 as published by
   9 * the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along with
  17 * this program.  If not, see <http://www.gnu.org/licenses/>.
  18 */
  19
 
 
 
 
 
  20
  21#include <linux/spinlock.h>
  22#include <linux/shmem_fs.h>
  23#include <drm/drm_vma_manager.h>
  24
  25#include "omap_drv.h"
  26#include "omap_dmm_tiler.h"
  27
  28/* remove these once drm core helpers are merged */
  29struct page **_drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
  30void _drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  31		bool dirty, bool accessed);
  32int _drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  33
  34/*
  35 * GEM buffer object implementation.
  36 */
  37
  38#define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
  39
  40/* note: we use upper 8 bits of flags for driver-internal flags: */
  41#define OMAP_BO_DMA			0x01000000	/* actually is physically contiguous */
  42#define OMAP_BO_EXT_SYNC	0x02000000	/* externally allocated sync object */
  43#define OMAP_BO_EXT_MEM		0x04000000	/* externally allocated memory */
  44
  45
  46struct omap_gem_object {
  47	struct drm_gem_object base;
  48
  49	struct list_head mm_list;
  50
  51	uint32_t flags;
  52
  53	/** width/height for tiled formats (rounded up to slot boundaries) */
  54	uint16_t width, height;
  55
  56	/** roll applied when mapping to DMM */
  57	uint32_t roll;
 
 
 
  58
  59	/**
  60	 * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
  61	 * is set and the paddr is valid.  Also if the buffer is remapped in
  62	 * TILER and paddr_cnt > 0, then paddr is valid.  But if you are using
  63	 * the physical address and OMAP_BO_DMA is not set, then you should
  64	 * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
  65	 * not removed from under your feet.
  66	 *
  67	 * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
  68	 * buffer is requested, but doesn't mean that it is.  Use the
  69	 * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
  70	 * physical address.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  71	 */
  72	dma_addr_t paddr;
  73
  74	/**
  75	 * # of users of paddr
 
  76	 */
  77	uint32_t paddr_cnt;
  78
  79	/**
  80	 * tiler block used when buffer is remapped in DMM/TILER.
  81	 */
  82	struct tiler_block *block;
  83
  84	/**
  85	 * Array of backing pages, if allocated.  Note that pages are never
  86	 * allocated for buffers originally allocated from contiguous memory
  87	 */
  88	struct page **pages;
  89
  90	/** addresses corresponding to pages in above array */
  91	dma_addr_t *addrs;
  92
  93	/**
  94	 * Virtual address, if mapped.
  95	 */
  96	void *vaddr;
  97
  98	/**
  99	 * sync-object allocated on demand (if needed)
 100	 *
 101	 * Per-buffer sync-object for tracking pending and completed hw/dma
 102	 * read and write operations.  The layout in memory is dictated by
 103	 * the SGX firmware, which uses this information to stall the command
 104	 * stream if a surface is not ready yet.
 105	 *
 106	 * Note that when buffer is used by SGX, the sync-object needs to be
 107	 * allocated from a special heap of sync-objects.  This way many sync
 108	 * objects can be packed in a page, and not waste GPU virtual address
 109	 * space.  Because of this we have to have a omap_gem_set_sync_object()
 110	 * API to allow replacement of the syncobj after it has (potentially)
 111	 * already been allocated.  A bit ugly but I haven't thought of a
 112	 * better alternative.
 113	 */
 114	struct {
 115		uint32_t write_pending;
 116		uint32_t write_complete;
 117		uint32_t read_pending;
 118		uint32_t read_complete;
 119	} *sync;
 120};
 121
 122static int get_pages(struct drm_gem_object *obj, struct page ***pages);
 123static uint64_t mmap_offset(struct drm_gem_object *obj);
 124
 125/* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
 126 * not necessarily pinned in TILER all the time, and (b) when they are
 127 * they are not necessarily page aligned, we reserve one or more small
 128 * regions in each of the 2d containers to use as a user-GART where we
 129 * can create a second page-aligned mapping of parts of the buffer
 130 * being accessed from userspace.
 131 *
 132 * Note that we could optimize slightly when we know that multiple
 133 * tiler containers are backed by the same PAT.. but I'll leave that
 134 * for later..
 135 */
 136#define NUM_USERGART_ENTRIES 2
 137struct usergart_entry {
 138	struct tiler_block *block;	/* the reserved tiler block */
 139	dma_addr_t paddr;
 140	struct drm_gem_object *obj;	/* the current pinned obj */
 141	pgoff_t obj_pgoff;		/* page offset of obj currently
 142					   mapped in */
 143};
 144static struct {
 145	struct usergart_entry entry[NUM_USERGART_ENTRIES];
 
 146	int height;				/* height in rows */
 147	int height_shift;		/* ilog2(height in rows) */
 148	int slot_shift;			/* ilog2(width per slot) */
 149	int stride_pfn;			/* stride in pages */
 150	int last;				/* index of last used entry */
 151} *usergart;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 152
 153static void evict_entry(struct drm_gem_object *obj,
 154		enum tiler_fmt fmt, struct usergart_entry *entry)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 155{
 156	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 157	int n = usergart[fmt].height;
 
 158	size_t size = PAGE_SIZE * n;
 159	loff_t off = mmap_offset(obj) +
 160			(entry->obj_pgoff << PAGE_SHIFT);
 161	const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
 162
 163	if (m > 1) {
 164		int i;
 165		/* if stride > than PAGE_SIZE then sparse mapping: */
 166		for (i = n; i > 0; i--) {
 167			unmap_mapping_range(obj->dev->anon_inode->i_mapping,
 168					    off, PAGE_SIZE, 1);
 169			off += PAGE_SIZE * m;
 170		}
 171	} else {
 172		unmap_mapping_range(obj->dev->anon_inode->i_mapping,
 173				    off, size, 1);
 174	}
 175
 176	entry->obj = NULL;
 177}
 178
 179/* Evict a buffer from usergart, if it is mapped there */
 180static void evict(struct drm_gem_object *obj)
 181{
 182	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 
 183
 184	if (omap_obj->flags & OMAP_BO_TILED) {
 185		enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
 186		int i;
 187
 188		if (!usergart)
 189			return;
 
 190
 191		for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
 192			struct usergart_entry *entry = &usergart[fmt].entry[i];
 193			if (entry->obj == obj)
 194				evict_entry(obj, fmt, entry);
 195		}
 196	}
 197}
 198
 199/* GEM objects can either be allocated from contiguous memory (in which
 200 * case obj->filp==NULL), or w/ shmem backing (obj->filp!=NULL).  But non
 201 * contiguous buffers can be remapped in TILER/DMM if they need to be
 202 * contiguous... but we don't do this all the time to reduce pressure
 203 * on TILER/DMM space when we know at allocation time that the buffer
 204 * will need to be scanned out.
 205 */
 206static inline bool is_shmem(struct drm_gem_object *obj)
 207{
 208	return obj->filp != NULL;
 209}
 210
 211/**
 212 * shmem buffers that are mapped cached can simulate coherency via using
 213 * page faulting to keep track of dirty pages
 214 */
 215static inline bool is_cached_coherent(struct drm_gem_object *obj)
 216{
 217	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 218	return is_shmem(obj) &&
 219		((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED);
 220}
 221
 222static DEFINE_SPINLOCK(sync_lock);
 223
 224/** ensure backing pages are allocated */
 225static int omap_gem_attach_pages(struct drm_gem_object *obj)
 226{
 227	struct drm_device *dev = obj->dev;
 228	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 229	struct page **pages;
 230	int npages = obj->size >> PAGE_SHIFT;
 231	int i, ret;
 232	dma_addr_t *addrs;
 233
 234	WARN_ON(omap_obj->pages);
 235
 236	/* TODO: __GFP_DMA32 .. but somehow GFP_HIGHMEM is coming from the
 237	 * mapping_gfp_mask(mapping) which conflicts w/ GFP_DMA32.. probably
 238	 * we actually want CMA memory for it all anyways..
 239	 */
 240	pages = drm_gem_get_pages(obj, GFP_KERNEL);
 
 
 
 241	if (IS_ERR(pages)) {
 242		dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
 243		return PTR_ERR(pages);
 244	}
 245
 246	/* for non-cached buffers, ensure the new pages are clean because
 247	 * DSS, GPU, etc. are not cache coherent:
 248	 */
 249	if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
 250		addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
 251		if (!addrs) {
 252			ret = -ENOMEM;
 253			goto free_pages;
 254		}
 255
 256		for (i = 0; i < npages; i++) {
 257			addrs[i] = dma_map_page(dev->dev, pages[i],
 258					0, PAGE_SIZE, DMA_BIDIRECTIONAL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 259		}
 260	} else {
 261		addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
 262		if (!addrs) {
 263			ret = -ENOMEM;
 264			goto free_pages;
 265		}
 266	}
 267
 268	omap_obj->addrs = addrs;
 269	omap_obj->pages = pages;
 270
 271	return 0;
 272
 
 
 273free_pages:
 274	drm_gem_put_pages(obj, pages, true, false);
 275
 276	return ret;
 277}
 278
 279/** release backing pages */
 280static void omap_gem_detach_pages(struct drm_gem_object *obj)
 281{
 282	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 
 
 283
 284	/* for non-cached buffers, ensure the new pages are clean because
 285	 * DSS, GPU, etc. are not cache coherent:
 286	 */
 287	if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
 288		int i, npages = obj->size >> PAGE_SHIFT;
 289		for (i = 0; i < npages; i++) {
 290			dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
 291					PAGE_SIZE, DMA_BIDIRECTIONAL);
 292		}
 293	}
 294
 295	kfree(omap_obj->addrs);
 296	omap_obj->addrs = NULL;
 297
 298	drm_gem_put_pages(obj, omap_obj->pages, true, false);
 299	omap_obj->pages = NULL;
 300}
 301
 302/* get buffer flags */
 303uint32_t omap_gem_flags(struct drm_gem_object *obj)
 304{
 305	return to_omap_bo(obj)->flags;
 306}
 307
 308/** get mmap offset */
 309static uint64_t mmap_offset(struct drm_gem_object *obj)
 310{
 311	struct drm_device *dev = obj->dev;
 312	int ret;
 313	size_t size;
 314
 315	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
 316
 317	/* Make it mmapable */
 318	size = omap_gem_mmap_size(obj);
 319	ret = drm_gem_create_mmap_offset_size(obj, size);
 320	if (ret) {
 321		dev_err(dev->dev, "could not allocate mmap offset\n");
 322		return 0;
 323	}
 324
 325	return drm_vma_node_offset_addr(&obj->vma_node);
 326}
 327
 328uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
 329{
 330	uint64_t offset;
 331	mutex_lock(&obj->dev->struct_mutex);
 332	offset = mmap_offset(obj);
 333	mutex_unlock(&obj->dev->struct_mutex);
 334	return offset;
 335}
 336
 337/** get mmap size */
 338size_t omap_gem_mmap_size(struct drm_gem_object *obj)
 339{
 340	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 341	size_t size = obj->size;
 342
 343	if (omap_obj->flags & OMAP_BO_TILED) {
 344		/* for tiled buffers, the virtual size has stride rounded up
 345		 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
 346		 * 32kb later!).  But we don't back the entire buffer with
 347		 * pages, only the valid picture part.. so need to adjust for
 348		 * this in the size used to mmap and generate mmap offset
 349		 */
 350		size = tiler_vsize(gem2fmt(omap_obj->flags),
 351				omap_obj->width, omap_obj->height);
 352	}
 353
 354	return size;
 355}
 356
 357/* get tiled size, returns -EINVAL if not tiled buffer */
 358int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h)
 359{
 360	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 361	if (omap_obj->flags & OMAP_BO_TILED) {
 362		*w = omap_obj->width;
 363		*h = omap_obj->height;
 364		return 0;
 365	}
 366	return -EINVAL;
 367}
 368
 369/* Normal handling for the case of faulting in non-tiled buffers */
 370static int fault_1d(struct drm_gem_object *obj,
 371		struct vm_area_struct *vma, struct vm_fault *vmf)
 372{
 373	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 374	unsigned long pfn;
 375	pgoff_t pgoff;
 376
 377	/* We don't use vmf->pgoff since that has the fake offset: */
 378	pgoff = ((unsigned long)vmf->virtual_address -
 379			vma->vm_start) >> PAGE_SHIFT;
 380
 381	if (omap_obj->pages) {
 382		omap_gem_cpu_sync(obj, pgoff);
 383		pfn = page_to_pfn(omap_obj->pages[pgoff]);
 384	} else {
 385		BUG_ON(!(omap_obj->flags & OMAP_BO_DMA));
 386		pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
 387	}
 388
 389	VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
 390			pfn, pfn << PAGE_SHIFT);
 391
 392	return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
 
 393}
 394
 395/* Special handling for the case of faulting in 2d tiled buffers */
 396static int fault_2d(struct drm_gem_object *obj,
 397		struct vm_area_struct *vma, struct vm_fault *vmf)
 398{
 399	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 400	struct usergart_entry *entry;
 
 401	enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
 402	struct page *pages[64];  /* XXX is this too much to have on stack? */
 403	unsigned long pfn;
 404	pgoff_t pgoff, base_pgoff;
 405	void __user *vaddr;
 406	int i, ret, slots;
 
 407
 408	/*
 409	 * Note the height of the slot is also equal to the number of pages
 410	 * that need to be mapped in to fill 4kb wide CPU page.  If the slot
 411	 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
 412	 */
 413	const int n = usergart[fmt].height;
 414	const int n_shift = usergart[fmt].height_shift;
 415
 416	/*
 417	 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
 418	 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
 419	 * into account in some of the math, so figure out virtual stride
 420	 * in pages
 421	 */
 422	const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
 423
 424	/* We don't use vmf->pgoff since that has the fake offset: */
 425	pgoff = ((unsigned long)vmf->virtual_address -
 426			vma->vm_start) >> PAGE_SHIFT;
 427
 428	/*
 429	 * Actual address we start mapping at is rounded down to previous slot
 430	 * boundary in the y direction:
 431	 */
 432	base_pgoff = round_down(pgoff, m << n_shift);
 433
 434	/* figure out buffer width in slots */
 435	slots = omap_obj->width >> usergart[fmt].slot_shift;
 436
 437	vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
 438
 439	entry = &usergart[fmt].entry[usergart[fmt].last];
 440
 441	/* evict previous buffer using this usergart entry, if any: */
 442	if (entry->obj)
 443		evict_entry(entry->obj, fmt, entry);
 444
 445	entry->obj = obj;
 446	entry->obj_pgoff = base_pgoff;
 447
 448	/* now convert base_pgoff to phys offset from virt offset: */
 449	base_pgoff = (base_pgoff >> n_shift) * slots;
 450
 451	/* for wider-than 4k.. figure out which part of the slot-row we want: */
 452	if (m > 1) {
 453		int off = pgoff % m;
 454		entry->obj_pgoff += off;
 455		base_pgoff /= m;
 456		slots = min(slots - (off << n_shift), n);
 457		base_pgoff += off << n_shift;
 458		vaddr += off << PAGE_SHIFT;
 459	}
 460
 461	/*
 462	 * Map in pages. Beyond the valid pixel part of the buffer, we set
 463	 * pages[i] to NULL to get a dummy page mapped in.. if someone
 464	 * reads/writes it they will get random/undefined content, but at
 465	 * least it won't be corrupting whatever other random page used to
 466	 * be mapped in, or other undefined behavior.
 467	 */
 468	memcpy(pages, &omap_obj->pages[base_pgoff],
 469			sizeof(struct page *) * slots);
 470	memset(pages + slots, 0,
 471			sizeof(struct page *) * (n - slots));
 472
 473	ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
 474	if (ret) {
 475		dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
 
 476		return ret;
 477	}
 478
 479	pfn = entry->paddr >> PAGE_SHIFT;
 480
 481	VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
 482			pfn, pfn << PAGE_SHIFT);
 483
 484	for (i = n; i > 0; i--) {
 485		vm_insert_mixed(vma, (unsigned long)vaddr, pfn);
 486		pfn += usergart[fmt].stride_pfn;
 
 
 
 487		vaddr += PAGE_SIZE * m;
 488	}
 489
 490	/* simple round-robin: */
 491	usergart[fmt].last = (usergart[fmt].last + 1) % NUM_USERGART_ENTRIES;
 
 492
 493	return 0;
 494}
 495
 496/**
 497 * omap_gem_fault		-	pagefault handler for GEM objects
 498 * @vma: the VMA of the GEM object
 499 * @vmf: fault detail
 500 *
 501 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
 502 * does most of the work for us including the actual map/unmap calls
 503 * but we need to do the actual page work.
 504 *
 505 * The VMA was set up by GEM. In doing so it also ensured that the
 506 * vma->vm_private_data points to the GEM object that is backing this
 507 * mapping.
 508 */
 509int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 510{
 
 511	struct drm_gem_object *obj = vma->vm_private_data;
 512	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 513	struct drm_device *dev = obj->dev;
 514	struct page **pages;
 515	int ret;
 516
 517	/* Make sure we don't parallel update on a fault, nor move or remove
 518	 * something from beneath our feet
 519	 */
 520	mutex_lock(&dev->struct_mutex);
 521
 522	/* if a shmem backed object, make sure we have pages attached now */
 523	ret = get_pages(obj, &pages);
 524	if (ret)
 
 525		goto fail;
 
 526
 527	/* where should we do corresponding put_pages().. we are mapping
 528	 * the original page, rather than thru a GART, so we can't rely
 529	 * on eviction to trigger this.  But munmap() or all mappings should
 530	 * probably trigger put_pages()?
 531	 */
 532
 533	if (omap_obj->flags & OMAP_BO_TILED)
 534		ret = fault_2d(obj, vma, vmf);
 535	else
 536		ret = fault_1d(obj, vma, vmf);
 537
 538
 539fail:
 540	mutex_unlock(&dev->struct_mutex);
 541	switch (ret) {
 542	case 0:
 543	case -ERESTARTSYS:
 544	case -EINTR:
 545		return VM_FAULT_NOPAGE;
 546	case -ENOMEM:
 547		return VM_FAULT_OOM;
 548	default:
 549		return VM_FAULT_SIGBUS;
 550	}
 551}
 552
 553/** We override mainly to fix up some of the vm mapping flags.. */
 554int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 555{
 556	int ret;
 557
 558	ret = drm_gem_mmap(filp, vma);
 559	if (ret) {
 560		DBG("mmap failed: %d", ret);
 561		return ret;
 562	}
 563
 564	return omap_gem_mmap_obj(vma->vm_private_data, vma);
 565}
 566
 567int omap_gem_mmap_obj(struct drm_gem_object *obj,
 568		struct vm_area_struct *vma)
 569{
 570	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 571
 572	vma->vm_flags &= ~VM_PFNMAP;
 573	vma->vm_flags |= VM_MIXEDMAP;
 574
 575	if (omap_obj->flags & OMAP_BO_WC) {
 576		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
 577	} else if (omap_obj->flags & OMAP_BO_UNCACHED) {
 578		vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
 579	} else {
 580		/*
 581		 * We do have some private objects, at least for scanout buffers
 582		 * on hardware without DMM/TILER.  But these are allocated write-
 583		 * combine
 584		 */
 585		if (WARN_ON(!obj->filp))
 586			return -EINVAL;
 587
 588		/*
 589		 * Shunt off cached objs to shmem file so they have their own
 590		 * address_space (so unmap_mapping_range does what we want,
 591		 * in particular in the case of mmap'd dmabufs)
 592		 */
 593		fput(vma->vm_file);
 594		vma->vm_pgoff = 0;
 595		vma->vm_file  = get_file(obj->filp);
 596
 597		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
 598	}
 599
 600	return 0;
 601}
 602
 
 
 
 603
 604/**
 605 * omap_gem_dumb_create	-	create a dumb buffer
 606 * @drm_file: our client file
 607 * @dev: our device
 608 * @args: the requested arguments copied from userspace
 609 *
 610 * Allocate a buffer suitable for use for a frame buffer of the
 611 * form described by user space. Give userspace a handle by which
 612 * to reference it.
 613 */
 614int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
 615		struct drm_mode_create_dumb *args)
 616{
 617	union omap_gem_size gsize;
 618
 619	/* in case someone tries to feed us a completely bogus stride: */
 620	args->pitch = align_pitch(args->pitch, args->width, args->bpp);
 621	args->size = PAGE_ALIGN(args->pitch * args->height);
 622
 623	gsize = (union omap_gem_size){
 624		.bytes = args->size,
 625	};
 626
 627	return omap_gem_new_handle(dev, file, gsize,
 628			OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
 629}
 630
 631/**
 632 * omap_gem_dumb_map	-	buffer mapping for dumb interface
 633 * @file: our drm client file
 634 * @dev: drm device
 635 * @handle: GEM handle to the object (from dumb_create)
 636 *
 637 * Do the necessary setup to allow the mapping of the frame buffer
 638 * into user memory. We don't have to do much here at the moment.
 639 */
 640int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
 641		uint32_t handle, uint64_t *offset)
 642{
 643	struct drm_gem_object *obj;
 644	int ret = 0;
 645
 646	/* GEM does all our handle to object mapping */
 647	obj = drm_gem_object_lookup(dev, file, handle);
 648	if (obj == NULL) {
 649		ret = -ENOENT;
 650		goto fail;
 651	}
 652
 653	*offset = omap_gem_mmap_offset(obj);
 654
 655	drm_gem_object_unreference_unlocked(obj);
 656
 657fail:
 658	return ret;
 659}
 660
 
 661/* Set scrolling position.  This allows us to implement fast scrolling
 662 * for console.
 663 *
 664 * Call only from non-atomic contexts.
 665 */
 666int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
 667{
 668	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 669	uint32_t npages = obj->size >> PAGE_SHIFT;
 670	int ret = 0;
 671
 672	if (roll > npages) {
 673		dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
 674		return -EINVAL;
 675	}
 676
 677	omap_obj->roll = roll;
 678
 679	mutex_lock(&obj->dev->struct_mutex);
 680
 681	/* if we aren't mapped yet, we don't need to do anything */
 682	if (omap_obj->block) {
 683		struct page **pages;
 684		ret = get_pages(obj, &pages);
 685		if (ret)
 686			goto fail;
 687		ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
 
 
 688		if (ret)
 689			dev_err(obj->dev->dev, "could not repin: %d\n", ret);
 690	}
 691
 692fail:
 693	mutex_unlock(&obj->dev->struct_mutex);
 694
 695	return ret;
 696}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 697
 698/* Sync the buffer for CPU access.. note pages should already be
 699 * attached, ie. omap_gem_get_pages()
 700 */
 701void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
 702{
 703	struct drm_device *dev = obj->dev;
 704	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 705
 706	if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
 707		dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
 708				PAGE_SIZE, DMA_BIDIRECTIONAL);
 709		omap_obj->addrs[pgoff] = 0;
 
 
 
 710	}
 711}
 712
 713/* sync the buffer for DMA access */
 714void omap_gem_dma_sync(struct drm_gem_object *obj,
 715		enum dma_data_direction dir)
 716{
 717	struct drm_device *dev = obj->dev;
 718	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 
 
 
 719
 720	if (is_cached_coherent(obj)) {
 721		int i, npages = obj->size >> PAGE_SHIFT;
 722		struct page **pages = omap_obj->pages;
 723		bool dirty = false;
 724
 725		for (i = 0; i < npages; i++) {
 726			if (!omap_obj->addrs[i]) {
 727				omap_obj->addrs[i] = dma_map_page(dev->dev, pages[i], 0,
 728						PAGE_SIZE, DMA_BIDIRECTIONAL);
 729				dirty = true;
 
 
 
 
 
 730			}
 
 
 
 731		}
 
 732
 733		if (dirty) {
 734			unmap_mapping_range(obj->filp->f_mapping, 0,
 735					omap_gem_mmap_size(obj), 1);
 736		}
 737	}
 738}
 739
 740/* Get physical address for DMA.. if 'remap' is true, and the buffer is not
 741 * already contiguous, remap it to pin in physically contiguous memory.. (ie.
 742 * map in TILER)
 
 
 
 
 
 
 
 
 
 
 743 */
 744int omap_gem_get_paddr(struct drm_gem_object *obj,
 745		dma_addr_t *paddr, bool remap)
 746{
 747	struct omap_drm_private *priv = obj->dev->dev_private;
 748	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 749	int ret = 0;
 750
 751	mutex_lock(&obj->dev->struct_mutex);
 752
 753	if (remap && is_shmem(obj) && priv->has_dmm) {
 754		if (omap_obj->paddr_cnt == 0) {
 755			struct page **pages;
 756			uint32_t npages = obj->size >> PAGE_SHIFT;
 757			enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
 758			struct tiler_block *block;
 759
 760			BUG_ON(omap_obj->block);
 761
 762			ret = get_pages(obj, &pages);
 763			if (ret)
 764				goto fail;
 765
 766			if (omap_obj->flags & OMAP_BO_TILED) {
 767				block = tiler_reserve_2d(fmt,
 768						omap_obj->width,
 769						omap_obj->height, 0);
 770			} else {
 771				block = tiler_reserve_1d(obj->size);
 772			}
 773
 774			if (IS_ERR(block)) {
 775				ret = PTR_ERR(block);
 776				dev_err(obj->dev->dev,
 777					"could not remap: %d (%d)\n", ret, fmt);
 778				goto fail;
 779			}
 780
 781			/* TODO: enable async refill.. */
 782			ret = tiler_pin(block, pages, npages,
 783					omap_obj->roll, true);
 784			if (ret) {
 785				tiler_release(block);
 786				dev_err(obj->dev->dev,
 787						"could not pin: %d\n", ret);
 788				goto fail;
 789			}
 790
 791			omap_obj->paddr = tiler_ssptr(block);
 792			omap_obj->block = block;
 793
 794			DBG("got paddr: %08x", omap_obj->paddr);
 795		}
 796
 797		omap_obj->paddr_cnt++;
 798
 799		*paddr = omap_obj->paddr;
 800	} else if (omap_obj->flags & OMAP_BO_DMA) {
 801		*paddr = omap_obj->paddr;
 802	} else {
 803		ret = -EINVAL;
 804		goto fail;
 805	}
 806
 807fail:
 808	mutex_unlock(&obj->dev->struct_mutex);
 809
 810	return ret;
 811}
 812
 813/* Release physical address, when DMA is no longer being performed.. this
 814 * could potentially unpin and unmap buffers from TILER
 
 
 
 
 
 815 */
 816int omap_gem_put_paddr(struct drm_gem_object *obj)
 817{
 818	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 819	int ret = 0;
 
 
 820
 821	mutex_lock(&obj->dev->struct_mutex);
 822	if (omap_obj->paddr_cnt > 0) {
 823		omap_obj->paddr_cnt--;
 824		if (omap_obj->paddr_cnt == 0) {
 825			ret = tiler_unpin(omap_obj->block);
 826			if (ret) {
 827				dev_err(obj->dev->dev,
 828					"could not unpin pages: %d\n", ret);
 829				goto fail;
 830			}
 831			ret = tiler_release(omap_obj->block);
 832			if (ret) {
 833				dev_err(obj->dev->dev,
 834					"could not release unmap: %d\n", ret);
 835			}
 
 836			omap_obj->block = NULL;
 837		}
 838	}
 839fail:
 840	mutex_unlock(&obj->dev->struct_mutex);
 841	return ret;
 842}
 843
 844/* Get rotated scanout address (only valid if already pinned), at the
 845 * specified orientation and x,y offset from top-left corner of buffer
 846 * (only valid for tiled 2d buffers)
 847 */
 848int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
 849		int x, int y, dma_addr_t *paddr)
 850{
 851	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 852	int ret = -EINVAL;
 853
 854	mutex_lock(&obj->dev->struct_mutex);
 855	if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
 
 856			(omap_obj->flags & OMAP_BO_TILED)) {
 857		*paddr = tiler_tsptr(omap_obj->block, orient, x, y);
 858		ret = 0;
 859	}
 860	mutex_unlock(&obj->dev->struct_mutex);
 
 
 861	return ret;
 862}
 863
 864/* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
 865int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
 866{
 867	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 868	int ret = -EINVAL;
 869	if (omap_obj->flags & OMAP_BO_TILED)
 870		ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
 871	return ret;
 872}
 873
 874/* acquire pages when needed (for example, for DMA where physically
 875 * contiguous buffer is not required
 876 */
 877static int get_pages(struct drm_gem_object *obj, struct page ***pages)
 878{
 879	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 880	int ret = 0;
 881
 882	if (is_shmem(obj) && !omap_obj->pages) {
 883		ret = omap_gem_attach_pages(obj);
 884		if (ret) {
 885			dev_err(obj->dev->dev, "could not attach pages\n");
 886			return ret;
 887		}
 888	}
 889
 890	/* TODO: even phys-contig.. we should have a list of pages? */
 891	*pages = omap_obj->pages;
 892
 893	return 0;
 894}
 895
 896/* if !remap, and we don't have pages backing, then fail, rather than
 897 * increasing the pin count (which we don't really do yet anyways,
 898 * because we don't support swapping pages back out).  And 'remap'
 899 * might not be quite the right name, but I wanted to keep it working
 900 * similarly to omap_gem_get_paddr().  Note though that mutex is not
 901 * aquired if !remap (because this can be called in atomic ctxt),
 902 * but probably omap_gem_get_paddr() should be changed to work in the
 903 * same way.  If !remap, a matching omap_gem_put_pages() call is not
 904 * required (and should not be made).
 905 */
 906int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
 907		bool remap)
 908{
 909	int ret;
 910	if (!remap) {
 911		struct omap_gem_object *omap_obj = to_omap_bo(obj);
 912		if (!omap_obj->pages)
 913			return -ENOMEM;
 914		*pages = omap_obj->pages;
 915		return 0;
 
 
 
 
 
 
 
 916	}
 917	mutex_lock(&obj->dev->struct_mutex);
 918	ret = get_pages(obj, pages);
 919	mutex_unlock(&obj->dev->struct_mutex);
 
 
 
 920	return ret;
 921}
 922
 923/* release pages when DMA no longer being performed */
 924int omap_gem_put_pages(struct drm_gem_object *obj)
 925{
 926	/* do something here if we dynamically attach/detach pages.. at
 927	 * least they would no longer need to be pinned if everyone has
 928	 * released the pages..
 929	 */
 930	return 0;
 931}
 932
 933/* Get kernel virtual address for CPU access.. this more or less only
 934 * exists for omap_fbdev.  This should be called with struct_mutex
 935 * held.
 
 936 */
 937void *omap_gem_vaddr(struct drm_gem_object *obj)
 938{
 939	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 940	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
 
 
 
 
 941	if (!omap_obj->vaddr) {
 942		struct page **pages;
 943		int ret = get_pages(obj, &pages);
 944		if (ret)
 945			return ERR_PTR(ret);
 946		omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
 
 
 947				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
 948	}
 949	return omap_obj->vaddr;
 
 
 
 
 
 950}
 
 
 
 
 
 951
 952#ifdef CONFIG_PM
 953/* re-pin objects in DMM in resume path: */
 954int omap_gem_resume(struct device *dev)
 955{
 956	struct drm_device *drm_dev = dev_get_drvdata(dev);
 957	struct omap_drm_private *priv = drm_dev->dev_private;
 958	struct omap_gem_object *omap_obj;
 959	int ret = 0;
 960
 
 961	list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
 962		if (omap_obj->block) {
 963			struct drm_gem_object *obj = &omap_obj->base;
 964			uint32_t npages = obj->size >> PAGE_SHIFT;
 
 965			WARN_ON(!omap_obj->pages);  /* this can't happen */
 966			ret = tiler_pin(omap_obj->block,
 967					omap_obj->pages, npages,
 968					omap_obj->roll, true);
 969			if (ret) {
 970				dev_err(dev, "could not repin: %d\n", ret);
 971				return ret;
 972			}
 973		}
 974	}
 975
 976	return 0;
 
 
 977}
 978#endif
 979
 
 
 
 
 980#ifdef CONFIG_DEBUG_FS
 981void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
 982{
 983	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 984	uint64_t off;
 985
 986	off = drm_vma_node_start(&obj->vma_node);
 987
 988	seq_printf(m, "%08x: %2d (%2d) %08llx %08Zx (%2d) %p %4d",
 989			omap_obj->flags, obj->name, obj->refcount.refcount.counter,
 990			off, omap_obj->paddr, omap_obj->paddr_cnt,
 
 
 991			omap_obj->vaddr, omap_obj->roll);
 992
 993	if (omap_obj->flags & OMAP_BO_TILED) {
 994		seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
 995		if (omap_obj->block) {
 996			struct tcm_area *area = &omap_obj->block->area;
 997			seq_printf(m, " (%dx%d, %dx%d)",
 998					area->p0.x, area->p0.y,
 999					area->p1.x, area->p1.y);
1000		}
1001	} else {
1002		seq_printf(m, " %d", obj->size);
1003	}
1004
 
 
1005	seq_printf(m, "\n");
1006}
1007
1008void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1009{
1010	struct omap_gem_object *omap_obj;
1011	int count = 0;
1012	size_t size = 0;
1013
1014	list_for_each_entry(omap_obj, list, mm_list) {
1015		struct drm_gem_object *obj = &omap_obj->base;
1016		seq_printf(m, "   ");
1017		omap_gem_describe(obj, m);
1018		count++;
1019		size += obj->size;
1020	}
1021
1022	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1023}
1024#endif
1025
1026/* Buffer Synchronization:
 
1027 */
1028
1029struct omap_gem_sync_waiter {
1030	struct list_head list;
1031	struct omap_gem_object *omap_obj;
1032	enum omap_gem_op op;
1033	uint32_t read_target, write_target;
1034	/* notify called w/ sync_lock held */
1035	void (*notify)(void *arg);
1036	void *arg;
1037};
1038
1039/* list of omap_gem_sync_waiter.. the notify fxn gets called back when
1040 * the read and/or write target count is achieved which can call a user
1041 * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
1042 * cpu access), etc.
1043 */
1044static LIST_HEAD(waiters);
1045
1046static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
1047{
1048	struct omap_gem_object *omap_obj = waiter->omap_obj;
1049	if ((waiter->op & OMAP_GEM_READ) &&
1050			(omap_obj->sync->write_complete < waiter->write_target))
1051		return true;
1052	if ((waiter->op & OMAP_GEM_WRITE) &&
1053			(omap_obj->sync->read_complete < waiter->read_target))
1054		return true;
1055	return false;
1056}
1057
1058/* macro for sync debug.. */
1059#define SYNCDBG 0
1060#define SYNC(fmt, ...) do { if (SYNCDBG) \
1061		printk(KERN_ERR "%s:%d: "fmt"\n", \
1062				__func__, __LINE__, ##__VA_ARGS__); \
1063	} while (0)
1064
1065
1066static void sync_op_update(void)
1067{
1068	struct omap_gem_sync_waiter *waiter, *n;
1069	list_for_each_entry_safe(waiter, n, &waiters, list) {
1070		if (!is_waiting(waiter)) {
1071			list_del(&waiter->list);
1072			SYNC("notify: %p", waiter);
1073			waiter->notify(waiter->arg);
1074			kfree(waiter);
1075		}
1076	}
1077}
1078
1079static inline int sync_op(struct drm_gem_object *obj,
1080		enum omap_gem_op op, bool start)
1081{
1082	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1083	int ret = 0;
 
 
1084
1085	spin_lock(&sync_lock);
 
1086
1087	if (!omap_obj->sync) {
1088		omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
1089		if (!omap_obj->sync) {
1090			ret = -ENOMEM;
1091			goto unlock;
1092		}
1093	}
1094
1095	if (start) {
1096		if (op & OMAP_GEM_READ)
1097			omap_obj->sync->read_pending++;
1098		if (op & OMAP_GEM_WRITE)
1099			omap_obj->sync->write_pending++;
1100	} else {
1101		if (op & OMAP_GEM_READ)
1102			omap_obj->sync->read_complete++;
1103		if (op & OMAP_GEM_WRITE)
1104			omap_obj->sync->write_complete++;
1105		sync_op_update();
1106	}
1107
1108unlock:
1109	spin_unlock(&sync_lock);
 
1110
1111	return ret;
1112}
1113
1114/* it is a bit lame to handle updates in this sort of polling way, but
1115 * in case of PVR, the GPU can directly update read/write complete
1116 * values, and not really tell us which ones it updated.. this also
1117 * means that sync_lock is not quite sufficient.  So we'll need to
1118 * do something a bit better when it comes time to add support for
1119 * separate 2d hw..
1120 */
1121void omap_gem_op_update(void)
1122{
1123	spin_lock(&sync_lock);
1124	sync_op_update();
1125	spin_unlock(&sync_lock);
1126}
1127
1128/* mark the start of read and/or write operation */
1129int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
 
1130{
1131	return sync_op(obj, op, true);
1132}
 
 
 
 
1133
1134int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
1135{
1136	return sync_op(obj, op, false);
1137}
 
 
1138
1139static DECLARE_WAIT_QUEUE_HEAD(sync_event);
 
 
 
 
 
1140
1141static void sync_notify(void *arg)
1142{
1143	struct task_struct **waiter_task = arg;
1144	*waiter_task = NULL;
1145	wake_up_all(&sync_event);
1146}
 
 
 
 
 
 
 
 
 
 
 
 
 
1147
1148int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
1149{
1150	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1151	int ret = 0;
1152	if (omap_obj->sync) {
1153		struct task_struct *waiter_task = current;
1154		struct omap_gem_sync_waiter *waiter =
1155				kzalloc(sizeof(*waiter), GFP_KERNEL);
1156
1157		if (!waiter)
1158			return -ENOMEM;
1159
1160		waiter->omap_obj = omap_obj;
1161		waiter->op = op;
1162		waiter->read_target = omap_obj->sync->read_pending;
1163		waiter->write_target = omap_obj->sync->write_pending;
1164		waiter->notify = sync_notify;
1165		waiter->arg = &waiter_task;
1166
1167		spin_lock(&sync_lock);
1168		if (is_waiting(waiter)) {
1169			SYNC("waited: %p", waiter);
1170			list_add_tail(&waiter->list, &waiters);
1171			spin_unlock(&sync_lock);
1172			ret = wait_event_interruptible(sync_event,
1173					(waiter_task == NULL));
1174			spin_lock(&sync_lock);
1175			if (waiter_task) {
1176				SYNC("interrupted: %p", waiter);
1177				/* we were interrupted */
1178				list_del(&waiter->list);
1179				waiter_task = NULL;
1180			} else {
1181				/* freed in sync_op_update() */
1182				waiter = NULL;
1183			}
1184		}
1185		spin_unlock(&sync_lock);
1186
1187		if (waiter)
1188			kfree(waiter);
1189	}
1190	return ret;
1191}
1192
1193/* call fxn(arg), either synchronously or asynchronously if the op
1194 * is currently blocked..  fxn() can be called from any context
1195 *
1196 * (TODO for now fxn is called back from whichever context calls
1197 * omap_gem_op_update().. but this could be better defined later
1198 * if needed)
1199 *
1200 * TODO more code in common w/ _sync()..
1201 */
1202int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
1203		void (*fxn)(void *arg), void *arg)
1204{
1205	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1206	if (omap_obj->sync) {
1207		struct omap_gem_sync_waiter *waiter =
1208				kzalloc(sizeof(*waiter), GFP_ATOMIC);
1209
1210		if (!waiter)
1211			return -ENOMEM;
1212
1213		waiter->omap_obj = omap_obj;
1214		waiter->op = op;
1215		waiter->read_target = omap_obj->sync->read_pending;
1216		waiter->write_target = omap_obj->sync->write_pending;
1217		waiter->notify = fxn;
1218		waiter->arg = arg;
1219
1220		spin_lock(&sync_lock);
1221		if (is_waiting(waiter)) {
1222			SYNC("waited: %p", waiter);
1223			list_add_tail(&waiter->list, &waiters);
1224			spin_unlock(&sync_lock);
1225			return 0;
1226		}
1227
1228		spin_unlock(&sync_lock);
 
1229
1230		kfree(waiter);
 
 
 
1231	}
1232
1233	/* no waiting.. */
1234	fxn(arg);
 
 
 
 
 
1235
1236	return 0;
1237}
 
1238
1239/* special API so PVR can update the buffer to use a sync-object allocated
1240 * from it's sync-obj heap.  Only used for a newly allocated (from PVR's
1241 * perspective) sync-object, so we overwrite the new syncobj w/ values
1242 * from the already allocated syncobj (if there is one)
1243 */
1244int omap_gem_set_sync_object(struct drm_gem_object *obj, void *syncobj)
1245{
1246	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1247	int ret = 0;
1248
1249	spin_lock(&sync_lock);
 
 
1250
1251	if ((omap_obj->flags & OMAP_BO_EXT_SYNC) && !syncobj) {
1252		/* clearing a previously set syncobj */
1253		syncobj = kmemdup(omap_obj->sync, sizeof(*omap_obj->sync),
1254				  GFP_ATOMIC);
1255		if (!syncobj) {
1256			ret = -ENOMEM;
1257			goto unlock;
1258		}
1259		omap_obj->flags &= ~OMAP_BO_EXT_SYNC;
1260		omap_obj->sync = syncobj;
1261	} else if (syncobj && !(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
1262		/* replacing an existing syncobj */
1263		if (omap_obj->sync) {
1264			memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
1265			kfree(omap_obj->sync);
1266		}
1267		omap_obj->flags |= OMAP_BO_EXT_SYNC;
1268		omap_obj->sync = syncobj;
1269	}
1270
1271unlock:
1272	spin_unlock(&sync_lock);
1273	return ret;
 
 
1274}
1275
1276/* don't call directly.. called from GEM core when it is time to actually
1277 * free the object..
1278 */
1279void omap_gem_free_object(struct drm_gem_object *obj)
1280{
1281	struct drm_device *dev = obj->dev;
1282	struct omap_gem_object *omap_obj = to_omap_bo(obj);
 
 
1283
1284	evict(obj);
 
 
1285
1286	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
 
 
 
1287
1288	list_del(&omap_obj->mm_list);
1289
1290	drm_gem_free_mmap_offset(obj);
1291
1292	/* this means the object is still pinned.. which really should
1293	 * not happen.  I think..
1294	 */
1295	WARN_ON(omap_obj->paddr_cnt > 0);
1296
1297	/* don't free externally allocated backing memory */
1298	if (!(omap_obj->flags & OMAP_BO_EXT_MEM)) {
1299		if (omap_obj->pages)
1300			omap_gem_detach_pages(obj);
 
 
 
 
1301
1302		if (!is_shmem(obj)) {
1303			dma_free_writecombine(dev->dev, obj->size,
1304					omap_obj->vaddr, omap_obj->paddr);
1305		} else if (omap_obj->vaddr) {
1306			vunmap(omap_obj->vaddr);
 
1307		}
1308	}
1309
1310	/* don't free externally allocated syncobj */
1311	if (!(omap_obj->flags & OMAP_BO_EXT_SYNC))
1312		kfree(omap_obj->sync);
 
 
 
 
1313
1314	drm_gem_object_release(obj);
 
 
 
 
 
1315
1316	kfree(obj);
 
 
1317}
1318
1319/* convenience method to construct a GEM buffer object, and userspace handle */
1320int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1321		union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
1322{
1323	struct drm_gem_object *obj;
1324	int ret;
1325
1326	obj = omap_gem_new(dev, gsize, flags);
1327	if (!obj)
1328		return -ENOMEM;
1329
1330	ret = drm_gem_handle_create(file, obj, handle);
1331	if (ret) {
1332		drm_gem_object_release(obj);
1333		kfree(obj); /* TODO isn't there a dtor to call? just copying i915 */
1334		return ret;
1335	}
1336
1337	/* drop reference from allocate - handle holds it now */
1338	drm_gem_object_unreference_unlocked(obj);
1339
1340	return 0;
1341}
1342
1343/* GEM buffer object constructor */
1344struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1345		union omap_gem_size gsize, uint32_t flags)
1346{
1347	struct omap_drm_private *priv = dev->dev_private;
1348	struct omap_gem_object *omap_obj;
1349	struct drm_gem_object *obj = NULL;
1350	size_t size;
1351	int ret;
1352
1353	if (flags & OMAP_BO_TILED) {
1354		if (!usergart) {
1355			dev_err(dev->dev, "Tiled buffers require DMM\n");
1356			goto fail;
1357		}
1358
1359		/* tiled buffers are always shmem paged backed.. when they are
1360		 * scanned out, they are remapped into DMM/TILER
1361		 */
1362		flags &= ~OMAP_BO_SCANOUT;
1363
1364		/* currently don't allow cached buffers.. there is some caching
1365		 * stuff that needs to be handled better
1366		 */
1367		flags &= ~(OMAP_BO_CACHED|OMAP_BO_UNCACHED);
1368		flags |= OMAP_BO_WC;
1369
1370		/* align dimensions to slot boundaries... */
1371		tiler_align(gem2fmt(flags),
1372				&gsize.tiled.width, &gsize.tiled.height);
1373
1374		/* ...and calculate size based on aligned dimensions */
1375		size = tiler_size(gem2fmt(flags),
1376				gsize.tiled.width, gsize.tiled.height);
1377	} else {
1378		size = PAGE_ALIGN(gsize.bytes);
1379	}
1380
1381	omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1382	if (!omap_obj)
1383		goto fail;
1384
1385	list_add(&omap_obj->mm_list, &priv->obj_list);
1386
1387	obj = &omap_obj->base;
1388
1389	if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1390		/* attempt to allocate contiguous memory if we don't
1391		 * have DMM for remappign discontiguous buffers
1392		 */
1393		omap_obj->vaddr =  dma_alloc_writecombine(dev->dev, size,
1394				&omap_obj->paddr, GFP_KERNEL);
1395		if (omap_obj->vaddr)
1396			flags |= OMAP_BO_DMA;
1397
1398	}
1399
1400	omap_obj->flags = flags;
1401
1402	if (flags & OMAP_BO_TILED) {
1403		omap_obj->width = gsize.tiled.width;
1404		omap_obj->height = gsize.tiled.height;
1405	}
1406
1407	ret = 0;
1408	if (flags & (OMAP_BO_DMA|OMAP_BO_EXT_MEM))
1409		drm_gem_private_object_init(dev, obj, size);
1410	else
1411		ret = drm_gem_object_init(dev, obj, size);
1412
1413	if (ret)
1414		goto fail;
1415
1416	return obj;
1417
1418fail:
1419	if (obj)
1420		omap_gem_free_object(obj);
1421
1422	return NULL;
1423}
1424
1425/* init/cleanup.. if DMM is used, we need to set some stuff up.. */
1426void omap_gem_init(struct drm_device *dev)
1427{
1428	struct omap_drm_private *priv = dev->dev_private;
 
1429	const enum tiler_fmt fmts[] = {
1430			TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1431	};
1432	int i, j;
1433
1434	if (!dmm_is_available()) {
1435		/* DMM only supported on OMAP4 and later, so this isn't fatal */
1436		dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1437		return;
1438	}
1439
1440	usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1441	if (!usergart)
1442		return;
1443
1444	/* reserve 4k aligned/wide regions for userspace mappings: */
1445	for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1446		uint16_t h = 1, w = PAGE_SIZE >> i;
 
1447		tiler_align(fmts[i], &w, &h);
1448		/* note: since each region is 1 4kb page wide, and minimum
1449		 * number of rows, the height ends up being the same as the
1450		 * # of pages in the region
1451		 */
1452		usergart[i].height = h;
1453		usergart[i].height_shift = ilog2(h);
1454		usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1455		usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1456		for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1457			struct usergart_entry *entry = &usergart[i].entry[j];
1458			struct tiler_block *block =
1459					tiler_reserve_2d(fmts[i], w, h,
1460							PAGE_SIZE);
 
1461			if (IS_ERR(block)) {
1462				dev_err(dev->dev,
1463						"reserve failed: %d, %d, %ld\n",
1464						i, j, PTR_ERR(block));
1465				return;
1466			}
1467			entry->paddr = tiler_ssptr(block);
1468			entry->block = block;
1469
1470			DBG("%d:%d: %dx%d: paddr=%08x stride=%d", i, j, w, h,
1471					entry->paddr,
1472					usergart[i].stride_pfn << PAGE_SHIFT);
1473		}
1474	}
1475
 
1476	priv->has_dmm = true;
1477}
1478
1479void omap_gem_deinit(struct drm_device *dev)
1480{
 
 
1481	/* I believe we can rely on there being no more outstanding GEM
1482	 * objects which could depend on usergart/dmm at this point.
1483	 */
1484	kfree(usergart);
1485}