omap_dmm_tiler.c 26.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * DMM IOMMU driver support functions for TI OMAP processors.
 *
 * Author: Rob Clark <rob@ti.com>
 *         Andy Gross <andy.gross@ti.com>
 *
 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
18 19 20 21 22

#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/errno.h>
23
#include <linux/init.h>
24 25 26
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/mm.h>
27 28 29
#include <linux/module.h>
#include <linux/platform_device.h> /* platform_device() */
#include <linux/sched.h>
30
#include <linux/seq_file.h>
31 32
#include <linux/slab.h>
#include <linux/time.h>
33 34
#include <linux/vmalloc.h>
#include <linux/wait.h>
35 36 37 38

#include "omap_dmm_tiler.h"
#include "omap_dmm_priv.h"

39 40
#define DMM_DRIVER_NAME "dmm"

41 42 43 44
/* mappings for associating views to luts */
static struct tcm *containers[TILFMT_NFORMATS];
static struct dmm *omap_dmm;

45 46 47 48
#if defined(CONFIG_OF)
static const struct of_device_id dmm_of_match[];
#endif

49 50 51
/* global spinlock for protecting lists */
static DEFINE_SPINLOCK(list_lock);

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/* Geometry table */
#define GEOM(xshift, yshift, bytes_per_pixel) { \
		.x_shft = (xshift), \
		.y_shft = (yshift), \
		.cpp    = (bytes_per_pixel), \
		.slot_w = 1 << (SLOT_WIDTH_BITS - (xshift)), \
		.slot_h = 1 << (SLOT_HEIGHT_BITS - (yshift)), \
	}

static const struct {
	uint32_t x_shft;	/* unused X-bits (as part of bpp) */
	uint32_t y_shft;	/* unused Y-bits (as part of bpp) */
	uint32_t cpp;		/* bytes/chars per pixel */
	uint32_t slot_w;	/* width of each slot (in pixels) */
	uint32_t slot_h;	/* height of each slot (in pixels) */
} geom[TILFMT_NFORMATS] = {
68 69 70 71
	[TILFMT_8BIT]  = GEOM(0, 0, 1),
	[TILFMT_16BIT] = GEOM(0, 1, 2),
	[TILFMT_32BIT] = GEOM(1, 1, 4),
	[TILFMT_PAGE]  = GEOM(SLOT_WIDTH_BITS, SLOT_HEIGHT_BITS, 1),
72 73 74 75 76
};


/* lookup table for registers w/ per-engine instances */
static const uint32_t reg[][4] = {
77 78 79 80
	[PAT_STATUS] = {DMM_PAT_STATUS__0, DMM_PAT_STATUS__1,
			DMM_PAT_STATUS__2, DMM_PAT_STATUS__3},
	[PAT_DESCR]  = {DMM_PAT_DESCR__0, DMM_PAT_DESCR__1,
			DMM_PAT_DESCR__2, DMM_PAT_DESCR__3},
81 82
};

83 84 85 86 87 88 89 90 91 92
static u32 dmm_read(struct dmm *dmm, u32 reg)
{
	return readl(dmm->base + reg);
}

static void dmm_write(struct dmm *dmm, u32 val, u32 reg)
{
	writel(val, dmm->base + reg);
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* simple allocator to grab next 16 byte aligned memory from txn */
static void *alloc_dma(struct dmm_txn *txn, size_t sz, dma_addr_t *pa)
{
	void *ptr;
	struct refill_engine *engine = txn->engine_handle;

	/* dmm programming requires 16 byte aligned addresses */
	txn->current_pa = round_up(txn->current_pa, 16);
	txn->current_va = (void *)round_up((long)txn->current_va, 16);

	ptr = txn->current_va;
	*pa = txn->current_pa;

	txn->current_pa += sz;
	txn->current_va += sz;

	BUG_ON((txn->current_va - engine->refill_va) > REFILL_BUFFER_SIZE);

	return ptr;
}

/* check status and spin until wait_mask comes true */
static int wait_status(struct refill_engine *engine, uint32_t wait_mask)
{
	struct dmm *dmm = engine->dmm;
	uint32_t r = 0, err, i;

	i = DMM_FIXED_RETRY_COUNT;
	while (true) {
122
		r = dmm_read(dmm, reg[PAT_STATUS][engine->id]);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		err = r & DMM_PATSTATUS_ERR;
		if (err)
			return -EFAULT;

		if ((r & wait_mask) == wait_mask)
			break;

		if (--i == 0)
			return -ETIMEDOUT;

		udelay(1);
	}

	return 0;
}

139 140 141 142 143 144 145 146 147 148 149 150
static void release_engine(struct refill_engine *engine)
{
	unsigned long flags;

	spin_lock_irqsave(&list_lock, flags);
	list_add(&engine->idle_node, &omap_dmm->idle_head);
	spin_unlock_irqrestore(&list_lock, flags);

	atomic_inc(&omap_dmm->engine_counter);
	wake_up_interruptible(&omap_dmm->engine_queue);
}

151
static irqreturn_t omap_dmm_irq_handler(int irq, void *arg)
152 153
{
	struct dmm *dmm = arg;
154
	uint32_t status = dmm_read(dmm, DMM_PAT_IRQSTATUS);
155 156 157
	int i;

	/* ack IRQ */
158
	dmm_write(dmm, status, DMM_PAT_IRQSTATUS);
159 160

	for (i = 0; i < dmm->num_engines; i++) {
161 162 163
		if (status & DMM_IRQSTAT_LST) {
			if (dmm->engines[i].async)
				release_engine(&dmm->engines[i]);
164 165

			complete(&dmm->engines[i].compl);
166 167
		}

168 169 170 171 172 173 174 175 176 177 178 179 180
		status >>= 8;
	}

	return IRQ_HANDLED;
}

/**
 * Get a handle for a DMM transaction
 */
static struct dmm_txn *dmm_txn_init(struct dmm *dmm, struct tcm *tcm)
{
	struct dmm_txn *txn = NULL;
	struct refill_engine *engine = NULL;
181 182 183
	int ret;
	unsigned long flags;

184

185 186 187 188 189
	/* wait until an engine is available */
	ret = wait_event_interruptible(omap_dmm->engine_queue,
		atomic_add_unless(&omap_dmm->engine_counter, -1, 0));
	if (ret)
		return ERR_PTR(ret);
190 191

	/* grab an idle engine */
192
	spin_lock_irqsave(&list_lock, flags);
193 194 195 196 197
	if (!list_empty(&dmm->idle_head)) {
		engine = list_entry(dmm->idle_head.next, struct refill_engine,
					idle_node);
		list_del(&engine->idle_node);
	}
198
	spin_unlock_irqrestore(&list_lock, flags);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	BUG_ON(!engine);

	txn = &engine->txn;
	engine->tcm = tcm;
	txn->engine_handle = engine;
	txn->last_pat = NULL;
	txn->current_va = engine->refill_va;
	txn->current_pa = engine->refill_pa;

	return txn;
}

/**
 * Add region to DMM transaction.  If pages or pages[i] is NULL, then the
 * corresponding slot is cleared (ie. dummy_pa is programmed)
 */
216
static void dmm_txn_append(struct dmm_txn *txn, struct pat_area *area,
217
		struct page **pages, uint32_t npages, uint32_t roll)
218
{
219
	dma_addr_t pat_pa = 0, data_pa = 0;
220 221 222 223 224 225 226
	uint32_t *data;
	struct pat *pat;
	struct refill_engine *engine = txn->engine_handle;
	int columns = (1 + area->x1 - area->x0);
	int rows = (1 + area->y1 - area->y0);
	int i = columns*rows;

227
	pat = alloc_dma(txn, sizeof(*pat), &pat_pa);
228 229 230 231 232

	if (txn->last_pat)
		txn->last_pat->next_pa = (uint32_t)pat_pa;

	pat->area = *area;
Andy Gross's avatar
Andy Gross committed
233 234 235 236 237

	/* adjust Y coordinates based off of container parameters */
	pat->area.y0 += engine->tcm->y_offset;
	pat->area.y1 += engine->tcm->y_offset;

238 239 240 241 242
	pat->ctrl = (struct pat_ctrl){
			.start = 1,
			.lut_id = engine->tcm->lut_id,
		};

243 244 245
	data = alloc_dma(txn, 4*i, &data_pa);
	/* FIXME: what if data_pa is more than 32-bit ? */
	pat->data_pa = data_pa;
246 247

	while (i--) {
248 249 250 251 252
		int n = i + roll;
		if (n >= npages)
			n -= npages;
		data[i] = (pages && pages[n]) ?
			page_to_phys(pages[n]) : engine->dmm->dummy_pa;
253 254 255 256
	}

	txn->last_pat = pat;

257
	return;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
}

/**
 * Commit the DMM transaction.
 */
static int dmm_txn_commit(struct dmm_txn *txn, bool wait)
{
	int ret = 0;
	struct refill_engine *engine = txn->engine_handle;
	struct dmm *dmm = engine->dmm;

	if (!txn->last_pat) {
		dev_err(engine->dmm->dev, "need at least one txn\n");
		ret = -EINVAL;
		goto cleanup;
	}

	txn->last_pat->next_pa = 0;

	/* write to PAT_DESCR to clear out any pending transaction */
278
	dmm_write(dmm, 0x0, reg[PAT_DESCR][engine->id]);
279 280 281 282 283 284 285 286

	/* wait for engine ready: */
	ret = wait_status(engine, DMM_PATSTATUS_READY);
	if (ret) {
		ret = -EFAULT;
		goto cleanup;
	}

287 288
	/* mark whether it is async to denote list management in IRQ handler */
	engine->async = wait ? false : true;
289 290
	reinit_completion(&engine->compl);
	/* verify that the irq handler sees the 'async' and completion value */
291
	smp_mb();
292

293
	/* kick reload */
294
	dmm_write(dmm, engine->refill_pa, reg[PAT_DESCR][engine->id]);
295 296

	if (wait) {
297
		if (!wait_for_completion_timeout(&engine->compl,
298
				msecs_to_jiffies(100))) {
299 300 301 302 303 304
			dev_err(dmm->dev, "timed out waiting for done\n");
			ret = -ETIMEDOUT;
		}
	}

cleanup:
305 306 307
	/* only place engine back on list if we are done with it */
	if (ret || wait)
		release_engine(engine);
308 309 310 311 312 313 314

	return ret;
}

/*
 * DMM programming
 */
315 316
static int fill(struct tcm_area *area, struct page **pages,
		uint32_t npages, uint32_t roll, bool wait)
317 318 319 320 321
{
	int ret = 0;
	struct tcm_area slice, area_s;
	struct dmm_txn *txn;

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	/*
	 * FIXME
	 *
	 * Asynchronous fill does not work reliably, as the driver does not
	 * handle errors in the async code paths. The fill operation may
	 * silently fail, leading to leaking DMM engines, which may eventually
	 * lead to deadlock if we run out of DMM engines.
	 *
	 * For now, always set 'wait' so that we only use sync fills. Async
	 * fills should be fixed, or alternatively we could decide to only
	 * support sync fills and so the whole async code path could be removed.
	 */

	wait = true;

337 338
	txn = dmm_txn_init(omap_dmm, area->tcm);
	if (IS_ERR_OR_NULL(txn))
339
		return -ENOMEM;
340 341 342 343 344 345 346

	tcm_for_each_slice(slice, *area, area_s) {
		struct pat_area p_area = {
				.x0 = slice.p0.x,  .y0 = slice.p0.y,
				.x1 = slice.p1.x,  .y1 = slice.p1.y,
		};

347
		dmm_txn_append(txn, &p_area, pages, npages, roll);
348

349
		roll += tcm_sizeof(slice);
350 351 352 353 354 355 356 357 358 359 360 361 362
	}

	ret = dmm_txn_commit(txn, wait);

	return ret;
}

/*
 * Pin/unpin
 */

/* note: slots for which pages[i] == NULL are filled w/ dummy page
 */
363 364
int tiler_pin(struct tiler_block *block, struct page **pages,
		uint32_t npages, uint32_t roll, bool wait)
365 366 367
{
	int ret;

368
	ret = fill(&block->area, pages, npages, roll, wait);
369 370 371 372 373 374 375 376 377

	if (ret)
		tiler_unpin(block);

	return ret;
}

int tiler_unpin(struct tiler_block *block)
{
378
	return fill(&block->area, NULL, 0, 0, false);
379 380 381 382 383 384 385 386 387 388 389
}

/*
 * Reserve/release
 */
struct tiler_block *tiler_reserve_2d(enum tiler_fmt fmt, uint16_t w,
		uint16_t h, uint16_t align)
{
	struct tiler_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
	u32 min_align = 128;
	int ret;
390
	unsigned long flags;
391
	u32 slot_bytes;
392 393 394 395 396 397 398 399

	BUG_ON(!validfmt(fmt));

	/* convert width/height to slots */
	w = DIV_ROUND_UP(w, geom[fmt].slot_w);
	h = DIV_ROUND_UP(h, geom[fmt].slot_h);

	/* convert alignment to slots */
400 401 402 403
	slot_bytes = geom[fmt].slot_w * geom[fmt].cpp;
	min_align = max(min_align, slot_bytes);
	align = (align > min_align) ? ALIGN(align, min_align) : min_align;
	align /= slot_bytes;
404 405 406

	block->fmt = fmt;

407 408
	ret = tcm_reserve_2d(containers[fmt], w, h, align, -1, slot_bytes,
			&block->area);
409 410
	if (ret) {
		kfree(block);
411
		return ERR_PTR(-ENOMEM);
412 413 414
	}

	/* add to allocation list */
415
	spin_lock_irqsave(&list_lock, flags);
416
	list_add(&block->alloc_node, &omap_dmm->alloc_head);
417
	spin_unlock_irqrestore(&list_lock, flags);
418 419 420 421 422 423 424 425

	return block;
}

struct tiler_block *tiler_reserve_1d(size_t size)
{
	struct tiler_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
	int num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
426
	unsigned long flags;
427 428

	if (!block)
429
		return ERR_PTR(-ENOMEM);
430 431 432 433 434 435

	block->fmt = TILFMT_PAGE;

	if (tcm_reserve_1d(containers[TILFMT_PAGE], num_pages,
				&block->area)) {
		kfree(block);
436
		return ERR_PTR(-ENOMEM);
437 438
	}

439
	spin_lock_irqsave(&list_lock, flags);
440
	list_add(&block->alloc_node, &omap_dmm->alloc_head);
441
	spin_unlock_irqrestore(&list_lock, flags);
442 443 444 445 446 447 448 449

	return block;
}

/* note: if you have pin'd pages, you should have already unpin'd first! */
int tiler_release(struct tiler_block *block)
{
	int ret = tcm_free(&block->area);
450
	unsigned long flags;
451 452 453 454

	if (block->area.tcm)
		dev_err(omap_dmm->dev, "failed to release block\n");

455
	spin_lock_irqsave(&list_lock, flags);
456
	list_del(&block->alloc_node);
457
	spin_unlock_irqrestore(&list_lock, flags);
458 459 460 461 462 463 464 465 466

	kfree(block);
	return ret;
}

/*
 * Utils
 */

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
/* calculate the tiler space address of a pixel in a view orientation...
 * below description copied from the display subsystem section of TRM:
 *
 * When the TILER is addressed, the bits:
 *   [28:27] = 0x0 for 8-bit tiled
 *             0x1 for 16-bit tiled
 *             0x2 for 32-bit tiled
 *             0x3 for page mode
 *   [31:29] = 0x0 for 0-degree view
 *             0x1 for 180-degree view + mirroring
 *             0x2 for 0-degree view + mirroring
 *             0x3 for 180-degree view
 *             0x4 for 270-degree view + mirroring
 *             0x5 for 270-degree view
 *             0x6 for 90-degree view
 *             0x7 for 90-degree view + mirroring
 * Otherwise the bits indicated the corresponding bit address to access
 * the SDRAM.
 */
static u32 tiler_get_address(enum tiler_fmt fmt, u32 orient, u32 x, u32 y)
487 488 489 490 491 492 493 494 495 496 497
{
	u32 x_bits, y_bits, tmp, x_mask, y_mask, alignment;

	x_bits = CONT_WIDTH_BITS - geom[fmt].x_shft;
	y_bits = CONT_HEIGHT_BITS - geom[fmt].y_shft;
	alignment = geom[fmt].x_shft + geom[fmt].y_shft;

	/* validate coordinate */
	x_mask = MASK(x_bits);
	y_mask = MASK(y_bits);

498 499 500
	if (x < 0 || x > x_mask || y < 0 || y > y_mask) {
		DBG("invalid coords: %u < 0 || %u > %u || %u < 0 || %u > %u",
				x, x, x_mask, y, y, y_mask);
501
		return 0;
502
	}
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

	/* account for mirroring */
	if (orient & MASK_X_INVERT)
		x ^= x_mask;
	if (orient & MASK_Y_INVERT)
		y ^= y_mask;

	/* get coordinate address */
	if (orient & MASK_XY_FLIP)
		tmp = ((x << y_bits) + y);
	else
		tmp = ((y << x_bits) + x);

	return TIL_ADDR((tmp << alignment), orient, fmt);
}

dma_addr_t tiler_ssptr(struct tiler_block *block)
{
	BUG_ON(!validfmt(block->fmt));

523
	return TILVIEW_8BIT + tiler_get_address(block->fmt, 0,
524 525 526 527
			block->area.p0.x * geom[block->fmt].slot_w,
			block->area.p0.y * geom[block->fmt].slot_h);
}

528 529 530 531 532 533 534 535 536 537 538
dma_addr_t tiler_tsptr(struct tiler_block *block, uint32_t orient,
		uint32_t x, uint32_t y)
{
	struct tcm_pt *p = &block->area.p0;
	BUG_ON(!validfmt(block->fmt));

	return tiler_get_address(block->fmt, orient,
			(p->x * geom[block->fmt].slot_w) + x,
			(p->y * geom[block->fmt].slot_h) + y);
}

539 540 541 542 543 544 545
void tiler_align(enum tiler_fmt fmt, uint16_t *w, uint16_t *h)
{
	BUG_ON(!validfmt(fmt));
	*w = round_up(*w, geom[fmt].slot_w);
	*h = round_up(*h, geom[fmt].slot_h);
}

546
uint32_t tiler_stride(enum tiler_fmt fmt, uint32_t orient)
547 548 549
{
	BUG_ON(!validfmt(fmt));

550 551 552 553
	if (orient & MASK_XY_FLIP)
		return 1 << (CONT_HEIGHT_BITS + geom[fmt].x_shft);
	else
		return 1 << (CONT_WIDTH_BITS + geom[fmt].y_shft);
554 555 556 557 558 559 560 561 562 563 564 565 566 567
}

size_t tiler_size(enum tiler_fmt fmt, uint16_t w, uint16_t h)
{
	tiler_align(fmt, &w, &h);
	return geom[fmt].cpp * w * h;
}

size_t tiler_vsize(enum tiler_fmt fmt, uint16_t w, uint16_t h)
{
	BUG_ON(!validfmt(fmt));
	return round_up(geom[fmt].cpp * w, PAGE_SIZE) * h;
}

568 569 570 571 572
uint32_t tiler_get_cpu_cache_flags(void)
{
	return omap_dmm->plat_data->cpu_cache_flags;
}

573
bool dmm_is_available(void)
574 575 576 577 578
{
	return omap_dmm ? true : false;
}

static int omap_dmm_remove(struct platform_device *dev)
579 580 581
{
	struct tiler_block *block, *_block;
	int i;
582
	unsigned long flags;
583 584 585

	if (omap_dmm) {
		/* free all area regions */
586
		spin_lock_irqsave(&list_lock, flags);
587 588 589 590 591
		list_for_each_entry_safe(block, _block, &omap_dmm->alloc_head,
					alloc_node) {
			list_del(&block->alloc_node);
			kfree(block);
		}
592
		spin_unlock_irqrestore(&list_lock, flags);
593 594 595 596 597 598 599 600

		for (i = 0; i < omap_dmm->num_lut; i++)
			if (omap_dmm->tcm && omap_dmm->tcm[i])
				omap_dmm->tcm[i]->deinit(omap_dmm->tcm[i]);
		kfree(omap_dmm->tcm);

		kfree(omap_dmm->engines);
		if (omap_dmm->refill_va)
601 602 603
			dma_free_wc(omap_dmm->dev,
				    REFILL_BUFFER_SIZE * omap_dmm->num_engines,
				    omap_dmm->refill_va, omap_dmm->refill_pa);
604 605 606
		if (omap_dmm->dummy_page)
			__free_page(omap_dmm->dummy_page);

607
		if (omap_dmm->irq > 0)
608 609
			free_irq(omap_dmm->irq, omap_dmm);

610
		iounmap(omap_dmm->base);
611
		kfree(omap_dmm);
612
		omap_dmm = NULL;
613 614 615 616 617
	}

	return 0;
}

618
static int omap_dmm_probe(struct platform_device *dev)
619 620 621
{
	int ret = -EFAULT, i;
	struct tcm_area area = {0};
622
	u32 hwinfo, pat_geom;
623
	struct resource *mem;
624 625

	omap_dmm = kzalloc(sizeof(*omap_dmm), GFP_KERNEL);
626
	if (!omap_dmm)
627 628
		goto fail;

629 630 631 632
	/* initialize lists */
	INIT_LIST_HEAD(&omap_dmm->alloc_head);
	INIT_LIST_HEAD(&omap_dmm->idle_head);

633 634
	init_waitqueue_head(&omap_dmm->engine_queue);

635 636 637 638 639 640
	if (dev->dev.of_node) {
		const struct of_device_id *match;

		match = of_match_node(dmm_of_match, dev->dev.of_node);
		if (!match) {
			dev_err(&dev->dev, "failed to find matching device node\n");
641 642
			ret = -ENODEV;
			goto fail;
643 644 645 646 647
		}

		omap_dmm->plat_data = match->data;
	}

648
	/* lookup hwmod data - base address and irq */
649 650 651 652 653 654 655
	mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
	if (!mem) {
		dev_err(&dev->dev, "failed to get base address resource\n");
		goto fail;
	}

	omap_dmm->base = ioremap(mem->start, SZ_2K);
656 657

	if (!omap_dmm->base) {
658
		dev_err(&dev->dev, "failed to get dmm base address\n");
659 660 661
		goto fail;
	}

662 663 664 665 666 667 668 669
	omap_dmm->irq = platform_get_irq(dev, 0);
	if (omap_dmm->irq < 0) {
		dev_err(&dev->dev, "failed to get IRQ resource\n");
		goto fail;
	}

	omap_dmm->dev = &dev->dev;

670
	hwinfo = dmm_read(omap_dmm, DMM_PAT_HWINFO);
671 672 673 674 675
	omap_dmm->num_engines = (hwinfo >> 24) & 0x1F;
	omap_dmm->num_lut = (hwinfo >> 16) & 0x1F;
	omap_dmm->container_width = 256;
	omap_dmm->container_height = 128;

676 677
	atomic_set(&omap_dmm->engine_counter, omap_dmm->num_engines);

678
	/* read out actual LUT width and height */
679
	pat_geom = dmm_read(omap_dmm, DMM_PAT_GEOMETRY);
680 681 682
	omap_dmm->lut_width = ((pat_geom >> 16) & 0xF) << 5;
	omap_dmm->lut_height = ((pat_geom >> 24) & 0xF) << 5;

Andy Gross's avatar
Andy Gross committed
683 684 685 686 687
	/* increment LUT by one if on OMAP5 */
	/* LUT has twice the height, and is split into a separate container */
	if (omap_dmm->lut_height != omap_dmm->container_height)
		omap_dmm->num_lut++;

688
	/* initialize DMM registers */
689 690 691 692 693 694
	dmm_write(omap_dmm, 0x88888888, DMM_PAT_VIEW__0);
	dmm_write(omap_dmm, 0x88888888, DMM_PAT_VIEW__1);
	dmm_write(omap_dmm, 0x80808080, DMM_PAT_VIEW_MAP__0);
	dmm_write(omap_dmm, 0x80000000, DMM_PAT_VIEW_MAP_BASE);
	dmm_write(omap_dmm, 0x88888888, DMM_TILER_OR__0);
	dmm_write(omap_dmm, 0x88888888, DMM_TILER_OR__1);
695 696 697 698 699

	ret = request_irq(omap_dmm->irq, omap_dmm_irq_handler, IRQF_SHARED,
				"omap_dmm_irq_handler", omap_dmm);

	if (ret) {
700
		dev_err(&dev->dev, "couldn't register IRQ %d, error %d\n",
701 702 703 704 705
			omap_dmm->irq, ret);
		omap_dmm->irq = -1;
		goto fail;
	}

706 707 708 709 710 711
	/* Enable all interrupts for each refill engine except
	 * ERR_LUT_MISS<n> (which is just advisory, and we don't care
	 * about because we want to be able to refill live scanout
	 * buffers for accelerated pan/scroll) and FILL_DSC<n> which
	 * we just generally don't care about.
	 */
712
	dmm_write(omap_dmm, 0x7e7e7e7e, DMM_PAT_IRQENABLE_SET);
713 714 715

	omap_dmm->dummy_page = alloc_page(GFP_KERNEL | __GFP_DMA32);
	if (!omap_dmm->dummy_page) {
716
		dev_err(&dev->dev, "could not allocate dummy page\n");
717 718 719
		ret = -ENOMEM;
		goto fail;
	}
720 721

	/* set dma mask for device */
722 723 724
	ret = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
	if (ret)
		goto fail;
725

726 727 728
	omap_dmm->dummy_pa = page_to_phys(omap_dmm->dummy_page);

	/* alloc refill memory */
729 730 731
	omap_dmm->refill_va = dma_alloc_wc(&dev->dev,
					   REFILL_BUFFER_SIZE * omap_dmm->num_engines,
					   &omap_dmm->refill_pa, GFP_KERNEL);
732
	if (!omap_dmm->refill_va) {
733
		dev_err(&dev->dev, "could not allocate refill memory\n");
734 735 736 737
		goto fail;
	}

	/* alloc engines */
738
	omap_dmm->engines = kcalloc(omap_dmm->num_engines,
739
				    sizeof(*omap_dmm->engines), GFP_KERNEL);
740 741 742 743 744 745 746 747 748 749 750 751
	if (!omap_dmm->engines) {
		ret = -ENOMEM;
		goto fail;
	}

	for (i = 0; i < omap_dmm->num_engines; i++) {
		omap_dmm->engines[i].id = i;
		omap_dmm->engines[i].dmm = omap_dmm;
		omap_dmm->engines[i].refill_va = omap_dmm->refill_va +
						(REFILL_BUFFER_SIZE * i);
		omap_dmm->engines[i].refill_pa = omap_dmm->refill_pa +
						(REFILL_BUFFER_SIZE * i);
752
		init_completion(&omap_dmm->engines[i].compl);
753 754 755 756

		list_add(&omap_dmm->engines[i].idle_node, &omap_dmm->idle_head);
	}

757
	omap_dmm->tcm = kcalloc(omap_dmm->num_lut, sizeof(*omap_dmm->tcm),
758 759 760 761 762 763 764
				GFP_KERNEL);
	if (!omap_dmm->tcm) {
		ret = -ENOMEM;
		goto fail;
	}

	/* init containers */
Andy Gross's avatar
Andy Gross committed
765 766 767
	/* Each LUT is associated with a TCM (container manager).  We use the
	   lut_id to denote the lut_id used to identify the correct LUT for
	   programming during reill operations */
768 769
	for (i = 0; i < omap_dmm->num_lut; i++) {
		omap_dmm->tcm[i] = sita_init(omap_dmm->container_width,
770
						omap_dmm->container_height);
771 772

		if (!omap_dmm->tcm[i]) {
773
			dev_err(&dev->dev, "failed to allocate container\n");
774 775 776 777 778 779 780 781 782
			ret = -ENOMEM;
			goto fail;
		}

		omap_dmm->tcm[i]->lut_id = i;
	}

	/* assign access mode containers to applicable tcm container */
	/* OMAP 4 has 1 container for all 4 views */
Andy Gross's avatar
Andy Gross committed
783
	/* OMAP 5 has 2 containers, 1 for 2D and 1 for 1D */
784 785 786
	containers[TILFMT_8BIT] = omap_dmm->tcm[0];
	containers[TILFMT_16BIT] = omap_dmm->tcm[0];
	containers[TILFMT_32BIT] = omap_dmm->tcm[0];
Andy Gross's avatar
Andy Gross committed
787 788 789 790 791 792 793 794 795 796 797

	if (omap_dmm->container_height != omap_dmm->lut_height) {
		/* second LUT is used for PAGE mode.  Programming must use
		   y offset that is added to all y coordinates.  LUT id is still
		   0, because it is the same LUT, just the upper 128 lines */
		containers[TILFMT_PAGE] = omap_dmm->tcm[1];
		omap_dmm->tcm[1]->y_offset = OMAP5_LUT_OFFSET;
		omap_dmm->tcm[1]->lut_id = 0;
	} else {
		containers[TILFMT_PAGE] = omap_dmm->tcm[0];
	}
798 799 800 801 802 803 804 805 806 807

	area = (struct tcm_area) {
		.tcm = NULL,
		.p1.x = omap_dmm->container_width - 1,
		.p1.y = omap_dmm->container_height - 1,
	};

	/* initialize all LUTs to dummy page entries */
	for (i = 0; i < omap_dmm->num_lut; i++) {
		area.tcm = omap_dmm->tcm[i];
808
		if (fill(&area, NULL, 0, 0, true))
809 810 811 812 813 814 815 816
			dev_err(omap_dmm->dev, "refill failed");
	}

	dev_info(omap_dmm->dev, "initialized all PAT entries\n");

	return 0;

fail:
817 818
	if (omap_dmm_remove(dev))
		dev_err(&dev->dev, "cleanup failed\n");
819 820
	return ret;
}
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

/*
 * debugfs support
 */

#ifdef CONFIG_DEBUG_FS

static const char *alphabet = "abcdefghijklmnopqrstuvwxyz"
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const char *special = ".,:;'\"`~!^-+";

static void fill_map(char **map, int xdiv, int ydiv, struct tcm_area *a,
							char c, bool ovw)
{
	int x, y;
	for (y = a->p0.y / ydiv; y <= a->p1.y / ydiv; y++)
		for (x = a->p0.x / xdiv; x <= a->p1.x / xdiv; x++)
			if (map[y][x] == ' ' || ovw)
				map[y][x] = c;
}

static void fill_map_pt(char **map, int xdiv, int ydiv, struct tcm_pt *p,
									char c)
{
	map[p->y / ydiv][p->x / xdiv] = c;
}

static char read_map_pt(char **map, int xdiv, int ydiv, struct tcm_pt *p)
{
	return map[p->y / ydiv][p->x / xdiv];
}

static int map_width(int xdiv, int x0, int x1)
{
	return (x1 / xdiv) - (x0 / xdiv) + 1;
}

static void text_map(char **map, int xdiv, char *nice, int yd, int x0, int x1)
{
	char *p = map[yd] + (x0 / xdiv);
	int w = (map_width(xdiv, x0, x1) - strlen(nice)) / 2;
	if (w >= 0) {
		p += w;
		while (*nice)
			*p++ = *nice++;
	}
}

static void map_1d_info(char **map, int xdiv, int ydiv, char *nice,
							struct tcm_area *a)
{
	sprintf(nice, "%dK", tcm_sizeof(*a) * 4);
	if (a->p0.y + 1 < a->p1.y) {
		text_map(map, xdiv, nice, (a->p0.y + a->p1.y) / 2 / ydiv, 0,
							256 - 1);
	} else if (a->p0.y < a->p1.y) {
		if (strlen(nice) < map_width(xdiv, a->p0.x, 256 - 1))
			text_map(map, xdiv, nice, a->p0.y / ydiv,
					a->p0.x + xdiv,	256 - 1);
		else if (strlen(nice) < map_width(xdiv, 0, a->p1.x))
			text_map(map, xdiv, nice, a->p1.y / ydiv,
					0, a->p1.y - xdiv);
	} else if (strlen(nice) + 1 < map_width(xdiv, a->p0.x, a->p1.x)) {
		text_map(map, xdiv, nice, a->p0.y / ydiv, a->p0.x, a->p1.x);
	}
}

static void map_2d_info(char **map, int xdiv, int ydiv, char *nice,
							struct tcm_area *a)
{
	sprintf(nice, "(%d*%d)", tcm_awidth(*a), tcm_aheight(*a));
	if (strlen(nice) + 1 < map_width(xdiv, a->p0.x, a->p1.x))
		text_map(map, xdiv, nice, (a->p0.y + a->p1.y) / 2 / ydiv,
							a->p0.x, a->p1.x);
}

int tiler_map_show(struct seq_file *s, void *arg)
{
	int xdiv = 2, ydiv = 1;
	char **map = NULL, *global_map;
	struct tiler_block *block;
	struct tcm_area a, p;
	int i;
	const char *m2d = alphabet;
	const char *a2d = special;
	const char *m2dp = m2d, *a2dp = a2d;
	char nice[128];
908 909
	int h_adj;
	int w_adj;
910
	unsigned long flags;
Andy Gross's avatar
Andy Gross committed
911 912
	int lut_idx;

913

914 915 916 917 918
	if (!omap_dmm) {
		/* early return if dmm/tiler device is not initialized */
		return 0;
	}

Andy Gross's avatar
Andy Gross committed
919 920
	h_adj = omap_dmm->container_height / ydiv;
	w_adj = omap_dmm->container_width / xdiv;
921

Andy Gross's avatar
Andy Gross committed
922 923
	map = kmalloc(h_adj * sizeof(*map), GFP_KERNEL);
	global_map = kmalloc((w_adj + 1) * h_adj, GFP_KERNEL);
924 925 926 927

	if (!map || !global_map)
		goto error;

Andy Gross's avatar
Andy Gross committed
928
	for (lut_idx = 0; lut_idx < omap_dmm->num_lut; lut_idx++) {
929
		memset(map, 0, h_adj * sizeof(*map));
Andy Gross's avatar
Andy Gross committed
930
		memset(global_map, ' ', (w_adj + 1) * h_adj);
931

Andy Gross's avatar
Andy Gross committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
		for (i = 0; i < omap_dmm->container_height; i++) {
			map[i] = global_map + i * (w_adj + 1);
			map[i][w_adj] = 0;
		}

		spin_lock_irqsave(&list_lock, flags);

		list_for_each_entry(block, &omap_dmm->alloc_head, alloc_node) {
			if (block->area.tcm == omap_dmm->tcm[lut_idx]) {
				if (block->fmt != TILFMT_PAGE) {
					fill_map(map, xdiv, ydiv, &block->area,
						*m2dp, true);
					if (!*++a2dp)
						a2dp = a2d;
					if (!*++m2dp)
						m2dp = m2d;
					map_2d_info(map, xdiv, ydiv, nice,
							&block->area);
				} else {
					bool start = read_map_pt(map, xdiv,
						ydiv, &block->area.p0) == ' ';
					bool end = read_map_pt(map, xdiv, ydiv,
							&block->area.p1) == ' ';

					tcm_for_each_slice(a, block->area, p)
						fill_map(map, xdiv, ydiv, &a,
							'=', true);
					fill_map_pt(map, xdiv, ydiv,
							&block->area.p0,
961
							start ? '<' : 'X');
Andy Gross's avatar
Andy Gross committed
962 963
					fill_map_pt(map, xdiv, ydiv,
							&block->area.p1,
964
							end ? '>' : 'X');
Andy Gross's avatar
Andy Gross committed
965 966 967 968
					map_1d_info(map, xdiv, ydiv, nice,
							&block->area);
				}
			}
969 970
		}

Andy Gross's avatar
Andy Gross committed
971
		spin_unlock_irqrestore(&list_lock, flags);
972

Andy Gross's avatar
Andy Gross committed
973 974 975 976 977 978 979 980 981 982 983 984 985
		if (s) {
			seq_printf(s, "CONTAINER %d DUMP BEGIN\n", lut_idx);
			for (i = 0; i < 128; i++)
				seq_printf(s, "%03d:%s\n", i, map[i]);
			seq_printf(s, "CONTAINER %d DUMP END\n", lut_idx);
		} else {
			dev_dbg(omap_dmm->dev, "CONTAINER %d DUMP BEGIN\n",
				lut_idx);
			for (i = 0; i < 128; i++)
				dev_dbg(omap_dmm->dev, "%03d:%s\n", i, map[i]);
			dev_dbg(omap_dmm->dev, "CONTAINER %d DUMP END\n",
				lut_idx);
		}
986 987 988 989 990 991 992 993 994
	}

error:
	kfree(map);
	kfree(global_map);

	return 0;
}
#endif
995

996
#ifdef CONFIG_PM_SLEEP
Andy Gross's avatar
Andy Gross committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
static int omap_dmm_resume(struct device *dev)
{
	struct tcm_area area;
	int i;

	if (!omap_dmm)
		return -ENODEV;

	area = (struct tcm_area) {
		.tcm = NULL,
		.p1.x = omap_dmm->container_width - 1,
		.p1.y = omap_dmm->container_height - 1,
	};

	/* initialize all LUTs to dummy page entries */
	for (i = 0; i < omap_dmm->num_lut; i++) {
		area.tcm = omap_dmm->tcm[i];
		if (fill(&area, NULL, 0, 0, true))
			dev_err(dev, "refill failed");
	}

	return 0;
}
#endif

1022 1023
static SIMPLE_DEV_PM_OPS(omap_dmm_pm_ops, NULL, omap_dmm_resume);

1024
#if defined(CONFIG_OF)
1025 1026 1027 1028 1029 1030 1031 1032
static const struct dmm_platform_data dmm_omap4_platform_data = {
	.cpu_cache_flags = OMAP_BO_WC,
};

static const struct dmm_platform_data dmm_omap5_platform_data = {
	.cpu_cache_flags = OMAP_BO_UNCACHED,
};

1033
static const struct of_device_id dmm_of_match[] = {
1034 1035 1036 1037 1038 1039 1040 1041
	{
		.compatible = "ti,omap4-dmm",
		.data = &dmm_omap4_platform_data,
	},
	{
		.compatible = "ti,omap5-dmm",
		.data = &dmm_omap5_platform_data,
	},
1042 1043 1044 1045
	{},
};
#endif

1046 1047 1048 1049 1050 1051
struct platform_driver omap_dmm_driver = {
	.probe = omap_dmm_probe,
	.remove = omap_dmm_remove,
	.driver = {
		.owner = THIS_MODULE,
		.name = DMM_DRIVER_NAME,
1052
		.of_match_table = of_match_ptr(dmm_of_match),
Andy Gross's avatar
Andy Gross committed
1053
		.pm = &omap_dmm_pm_ops,
1054 1055 1056 1057 1058 1059
	},
};

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Andy Gross <andy.gross@ti.com>");
MODULE_DESCRIPTION("OMAP DMM/Tiler Driver");