omap_dmm_tiler.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * DMM IOMMU driver support functions for TI OMAP processors.
  3. *
  4. * Author: Rob Clark <rob@ti.com>
  5. * Andy Gross <andy.gross@ti.com>
  6. *
  7. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation version 2.
  12. *
  13. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  14. * kind, whether express or implied; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/completion.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h> /* platform_device() */
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/time.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/wait.h>
  33. #include "omap_dmm_tiler.h"
  34. #include "omap_dmm_priv.h"
  35. #define DMM_DRIVER_NAME "dmm"
  36. /* mappings for associating views to luts */
  37. static struct tcm *containers[TILFMT_NFORMATS];
  38. static struct dmm *omap_dmm;
  39. #if defined(CONFIG_OF)
  40. static const struct of_device_id dmm_of_match[];
  41. #endif
  42. /* global spinlock for protecting lists */
  43. static DEFINE_SPINLOCK(list_lock);
  44. /* Geometry table */
  45. #define GEOM(xshift, yshift, bytes_per_pixel) { \
  46. .x_shft = (xshift), \
  47. .y_shft = (yshift), \
  48. .cpp = (bytes_per_pixel), \
  49. .slot_w = 1 << (SLOT_WIDTH_BITS - (xshift)), \
  50. .slot_h = 1 << (SLOT_HEIGHT_BITS - (yshift)), \
  51. }
  52. static const struct {
  53. uint32_t x_shft; /* unused X-bits (as part of bpp) */
  54. uint32_t y_shft; /* unused Y-bits (as part of bpp) */
  55. uint32_t cpp; /* bytes/chars per pixel */
  56. uint32_t slot_w; /* width of each slot (in pixels) */
  57. uint32_t slot_h; /* height of each slot (in pixels) */
  58. } geom[TILFMT_NFORMATS] = {
  59. [TILFMT_8BIT] = GEOM(0, 0, 1),
  60. [TILFMT_16BIT] = GEOM(0, 1, 2),
  61. [TILFMT_32BIT] = GEOM(1, 1, 4),
  62. [TILFMT_PAGE] = GEOM(SLOT_WIDTH_BITS, SLOT_HEIGHT_BITS, 1),
  63. };
  64. /* lookup table for registers w/ per-engine instances */
  65. static const uint32_t reg[][4] = {
  66. [PAT_STATUS] = {DMM_PAT_STATUS__0, DMM_PAT_STATUS__1,
  67. DMM_PAT_STATUS__2, DMM_PAT_STATUS__3},
  68. [PAT_DESCR] = {DMM_PAT_DESCR__0, DMM_PAT_DESCR__1,
  69. DMM_PAT_DESCR__2, DMM_PAT_DESCR__3},
  70. };
  71. static u32 dmm_read(struct dmm *dmm, u32 reg)
  72. {
  73. return readl(dmm->base + reg);
  74. }
  75. static void dmm_write(struct dmm *dmm, u32 val, u32 reg)
  76. {
  77. writel(val, dmm->base + reg);
  78. }
  79. /* simple allocator to grab next 16 byte aligned memory from txn */
  80. static void *alloc_dma(struct dmm_txn *txn, size_t sz, dma_addr_t *pa)
  81. {
  82. void *ptr;
  83. struct refill_engine *engine = txn->engine_handle;
  84. /* dmm programming requires 16 byte aligned addresses */
  85. txn->current_pa = round_up(txn->current_pa, 16);
  86. txn->current_va = (void *)round_up((long)txn->current_va, 16);
  87. ptr = txn->current_va;
  88. *pa = txn->current_pa;
  89. txn->current_pa += sz;
  90. txn->current_va += sz;
  91. BUG_ON((txn->current_va - engine->refill_va) > REFILL_BUFFER_SIZE);
  92. return ptr;
  93. }
  94. /* check status and spin until wait_mask comes true */
  95. static int wait_status(struct refill_engine *engine, uint32_t wait_mask)
  96. {
  97. struct dmm *dmm = engine->dmm;
  98. uint32_t r = 0, err, i;
  99. i = DMM_FIXED_RETRY_COUNT;
  100. while (true) {
  101. r = dmm_read(dmm, reg[PAT_STATUS][engine->id]);
  102. err = r & DMM_PATSTATUS_ERR;
  103. if (err)
  104. return -EFAULT;
  105. if ((r & wait_mask) == wait_mask)
  106. break;
  107. if (--i == 0)
  108. return -ETIMEDOUT;
  109. udelay(1);
  110. }
  111. return 0;
  112. }
  113. static void release_engine(struct refill_engine *engine)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&list_lock, flags);
  117. list_add(&engine->idle_node, &omap_dmm->idle_head);
  118. spin_unlock_irqrestore(&list_lock, flags);
  119. atomic_inc(&omap_dmm->engine_counter);
  120. wake_up_interruptible(&omap_dmm->engine_queue);
  121. }
  122. static irqreturn_t omap_dmm_irq_handler(int irq, void *arg)
  123. {
  124. struct dmm *dmm = arg;
  125. uint32_t status = dmm_read(dmm, DMM_PAT_IRQSTATUS);
  126. int i;
  127. /* ack IRQ */
  128. dmm_write(dmm, status, DMM_PAT_IRQSTATUS);
  129. for (i = 0; i < dmm->num_engines; i++) {
  130. if (status & DMM_IRQSTAT_LST) {
  131. if (dmm->engines[i].async)
  132. release_engine(&dmm->engines[i]);
  133. complete(&dmm->engines[i].compl);
  134. }
  135. status >>= 8;
  136. }
  137. return IRQ_HANDLED;
  138. }
  139. /**
  140. * Get a handle for a DMM transaction
  141. */
  142. static struct dmm_txn *dmm_txn_init(struct dmm *dmm, struct tcm *tcm)
  143. {
  144. struct dmm_txn *txn = NULL;
  145. struct refill_engine *engine = NULL;
  146. int ret;
  147. unsigned long flags;
  148. /* wait until an engine is available */
  149. ret = wait_event_interruptible(omap_dmm->engine_queue,
  150. atomic_add_unless(&omap_dmm->engine_counter, -1, 0));
  151. if (ret)
  152. return ERR_PTR(ret);
  153. /* grab an idle engine */
  154. spin_lock_irqsave(&list_lock, flags);
  155. if (!list_empty(&dmm->idle_head)) {
  156. engine = list_entry(dmm->idle_head.next, struct refill_engine,
  157. idle_node);
  158. list_del(&engine->idle_node);
  159. }
  160. spin_unlock_irqrestore(&list_lock, flags);
  161. BUG_ON(!engine);
  162. txn = &engine->txn;
  163. engine->tcm = tcm;
  164. txn->engine_handle = engine;
  165. txn->last_pat = NULL;
  166. txn->current_va = engine->refill_va;
  167. txn->current_pa = engine->refill_pa;
  168. return txn;
  169. }
  170. /**
  171. * Add region to DMM transaction. If pages or pages[i] is NULL, then the
  172. * corresponding slot is cleared (ie. dummy_pa is programmed)
  173. */
  174. static void dmm_txn_append(struct dmm_txn *txn, struct pat_area *area,
  175. struct page **pages, uint32_t npages, uint32_t roll)
  176. {
  177. dma_addr_t pat_pa = 0, data_pa = 0;
  178. uint32_t *data;
  179. struct pat *pat;
  180. struct refill_engine *engine = txn->engine_handle;
  181. int columns = (1 + area->x1 - area->x0);
  182. int rows = (1 + area->y1 - area->y0);
  183. int i = columns*rows;
  184. pat = alloc_dma(txn, sizeof(struct pat), &pat_pa);
  185. if (txn->last_pat)
  186. txn->last_pat->next_pa = (uint32_t)pat_pa;
  187. pat->area = *area;
  188. /* adjust Y coordinates based off of container parameters */
  189. pat->area.y0 += engine->tcm->y_offset;
  190. pat->area.y1 += engine->tcm->y_offset;
  191. pat->ctrl = (struct pat_ctrl){
  192. .start = 1,
  193. .lut_id = engine->tcm->lut_id,
  194. };
  195. data = alloc_dma(txn, 4*i, &data_pa);
  196. /* FIXME: what if data_pa is more than 32-bit ? */
  197. pat->data_pa = data_pa;
  198. while (i--) {
  199. int n = i + roll;
  200. if (n >= npages)
  201. n -= npages;
  202. data[i] = (pages && pages[n]) ?
  203. page_to_phys(pages[n]) : engine->dmm->dummy_pa;
  204. }
  205. txn->last_pat = pat;
  206. return;
  207. }
  208. /**
  209. * Commit the DMM transaction.
  210. */
  211. static int dmm_txn_commit(struct dmm_txn *txn, bool wait)
  212. {
  213. int ret = 0;
  214. struct refill_engine *engine = txn->engine_handle;
  215. struct dmm *dmm = engine->dmm;
  216. if (!txn->last_pat) {
  217. dev_err(engine->dmm->dev, "need at least one txn\n");
  218. ret = -EINVAL;
  219. goto cleanup;
  220. }
  221. txn->last_pat->next_pa = 0;
  222. /* write to PAT_DESCR to clear out any pending transaction */
  223. dmm_write(dmm, 0x0, reg[PAT_DESCR][engine->id]);
  224. /* wait for engine ready: */
  225. ret = wait_status(engine, DMM_PATSTATUS_READY);
  226. if (ret) {
  227. ret = -EFAULT;
  228. goto cleanup;
  229. }
  230. /* mark whether it is async to denote list management in IRQ handler */
  231. engine->async = wait ? false : true;
  232. reinit_completion(&engine->compl);
  233. /* verify that the irq handler sees the 'async' and completion value */
  234. smp_mb();
  235. /* kick reload */
  236. dmm_write(dmm, engine->refill_pa, reg[PAT_DESCR][engine->id]);
  237. if (wait) {
  238. if (!wait_for_completion_timeout(&engine->compl,
  239. msecs_to_jiffies(100))) {
  240. dev_err(dmm->dev, "timed out waiting for done\n");
  241. ret = -ETIMEDOUT;
  242. }
  243. }
  244. cleanup:
  245. /* only place engine back on list if we are done with it */
  246. if (ret || wait)
  247. release_engine(engine);
  248. return ret;
  249. }
  250. /*
  251. * DMM programming
  252. */
  253. static int fill(struct tcm_area *area, struct page **pages,
  254. uint32_t npages, uint32_t roll, bool wait)
  255. {
  256. int ret = 0;
  257. struct tcm_area slice, area_s;
  258. struct dmm_txn *txn;
  259. /*
  260. * FIXME
  261. *
  262. * Asynchronous fill does not work reliably, as the driver does not
  263. * handle errors in the async code paths. The fill operation may
  264. * silently fail, leading to leaking DMM engines, which may eventually
  265. * lead to deadlock if we run out of DMM engines.
  266. *
  267. * For now, always set 'wait' so that we only use sync fills. Async
  268. * fills should be fixed, or alternatively we could decide to only
  269. * support sync fills and so the whole async code path could be removed.
  270. */
  271. wait = true;
  272. txn = dmm_txn_init(omap_dmm, area->tcm);
  273. if (IS_ERR_OR_NULL(txn))
  274. return -ENOMEM;
  275. tcm_for_each_slice(slice, *area, area_s) {
  276. struct pat_area p_area = {
  277. .x0 = slice.p0.x, .y0 = slice.p0.y,
  278. .x1 = slice.p1.x, .y1 = slice.p1.y,
  279. };
  280. dmm_txn_append(txn, &p_area, pages, npages, roll);
  281. roll += tcm_sizeof(slice);
  282. }
  283. ret = dmm_txn_commit(txn, wait);
  284. return ret;
  285. }
  286. /*
  287. * Pin/unpin
  288. */
  289. /* note: slots for which pages[i] == NULL are filled w/ dummy page
  290. */
  291. int tiler_pin(struct tiler_block *block, struct page **pages,
  292. uint32_t npages, uint32_t roll, bool wait)
  293. {
  294. int ret;
  295. ret = fill(&block->area, pages, npages, roll, wait);
  296. if (ret)
  297. tiler_unpin(block);
  298. return ret;
  299. }
  300. int tiler_unpin(struct tiler_block *block)
  301. {
  302. return fill(&block->area, NULL, 0, 0, false);
  303. }
  304. /*
  305. * Reserve/release
  306. */
  307. struct tiler_block *tiler_reserve_2d(enum tiler_fmt fmt, uint16_t w,
  308. uint16_t h, uint16_t align)
  309. {
  310. struct tiler_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  311. u32 min_align = 128;
  312. int ret;
  313. unsigned long flags;
  314. size_t slot_bytes;
  315. BUG_ON(!validfmt(fmt));
  316. /* convert width/height to slots */
  317. w = DIV_ROUND_UP(w, geom[fmt].slot_w);
  318. h = DIV_ROUND_UP(h, geom[fmt].slot_h);
  319. /* convert alignment to slots */
  320. slot_bytes = geom[fmt].slot_w * geom[fmt].cpp;
  321. min_align = max(min_align, slot_bytes);
  322. align = (align > min_align) ? ALIGN(align, min_align) : min_align;
  323. align /= slot_bytes;
  324. block->fmt = fmt;
  325. ret = tcm_reserve_2d(containers[fmt], w, h, align, -1, slot_bytes,
  326. &block->area);
  327. if (ret) {
  328. kfree(block);
  329. return ERR_PTR(-ENOMEM);
  330. }
  331. /* add to allocation list */
  332. spin_lock_irqsave(&list_lock, flags);
  333. list_add(&block->alloc_node, &omap_dmm->alloc_head);
  334. spin_unlock_irqrestore(&list_lock, flags);
  335. return block;
  336. }
  337. struct tiler_block *tiler_reserve_1d(size_t size)
  338. {
  339. struct tiler_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  340. int num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  341. unsigned long flags;
  342. if (!block)
  343. return ERR_PTR(-ENOMEM);
  344. block->fmt = TILFMT_PAGE;
  345. if (tcm_reserve_1d(containers[TILFMT_PAGE], num_pages,
  346. &block->area)) {
  347. kfree(block);
  348. return ERR_PTR(-ENOMEM);
  349. }
  350. spin_lock_irqsave(&list_lock, flags);
  351. list_add(&block->alloc_node, &omap_dmm->alloc_head);
  352. spin_unlock_irqrestore(&list_lock, flags);
  353. return block;
  354. }
  355. /* note: if you have pin'd pages, you should have already unpin'd first! */
  356. int tiler_release(struct tiler_block *block)
  357. {
  358. int ret = tcm_free(&block->area);
  359. unsigned long flags;
  360. if (block->area.tcm)
  361. dev_err(omap_dmm->dev, "failed to release block\n");
  362. spin_lock_irqsave(&list_lock, flags);
  363. list_del(&block->alloc_node);
  364. spin_unlock_irqrestore(&list_lock, flags);
  365. kfree(block);
  366. return ret;
  367. }
  368. /*
  369. * Utils
  370. */
  371. /* calculate the tiler space address of a pixel in a view orientation...
  372. * below description copied from the display subsystem section of TRM:
  373. *
  374. * When the TILER is addressed, the bits:
  375. * [28:27] = 0x0 for 8-bit tiled
  376. * 0x1 for 16-bit tiled
  377. * 0x2 for 32-bit tiled
  378. * 0x3 for page mode
  379. * [31:29] = 0x0 for 0-degree view
  380. * 0x1 for 180-degree view + mirroring
  381. * 0x2 for 0-degree view + mirroring
  382. * 0x3 for 180-degree view
  383. * 0x4 for 270-degree view + mirroring
  384. * 0x5 for 270-degree view
  385. * 0x6 for 90-degree view
  386. * 0x7 for 90-degree view + mirroring
  387. * Otherwise the bits indicated the corresponding bit address to access
  388. * the SDRAM.
  389. */
  390. static u32 tiler_get_address(enum tiler_fmt fmt, u32 orient, u32 x, u32 y)
  391. {
  392. u32 x_bits, y_bits, tmp, x_mask, y_mask, alignment;
  393. x_bits = CONT_WIDTH_BITS - geom[fmt].x_shft;
  394. y_bits = CONT_HEIGHT_BITS - geom[fmt].y_shft;
  395. alignment = geom[fmt].x_shft + geom[fmt].y_shft;
  396. /* validate coordinate */
  397. x_mask = MASK(x_bits);
  398. y_mask = MASK(y_bits);
  399. if (x < 0 || x > x_mask || y < 0 || y > y_mask) {
  400. DBG("invalid coords: %u < 0 || %u > %u || %u < 0 || %u > %u",
  401. x, x, x_mask, y, y, y_mask);
  402. return 0;
  403. }
  404. /* account for mirroring */
  405. if (orient & MASK_X_INVERT)
  406. x ^= x_mask;
  407. if (orient & MASK_Y_INVERT)
  408. y ^= y_mask;
  409. /* get coordinate address */
  410. if (orient & MASK_XY_FLIP)
  411. tmp = ((x << y_bits) + y);
  412. else
  413. tmp = ((y << x_bits) + x);
  414. return TIL_ADDR((tmp << alignment), orient, fmt);
  415. }
  416. dma_addr_t tiler_ssptr(struct tiler_block *block)
  417. {
  418. BUG_ON(!validfmt(block->fmt));
  419. return TILVIEW_8BIT + tiler_get_address(block->fmt, 0,
  420. block->area.p0.x * geom[block->fmt].slot_w,
  421. block->area.p0.y * geom[block->fmt].slot_h);
  422. }
  423. dma_addr_t tiler_tsptr(struct tiler_block *block, uint32_t orient,
  424. uint32_t x, uint32_t y)
  425. {
  426. struct tcm_pt *p = &block->area.p0;
  427. BUG_ON(!validfmt(block->fmt));
  428. return tiler_get_address(block->fmt, orient,
  429. (p->x * geom[block->fmt].slot_w) + x,
  430. (p->y * geom[block->fmt].slot_h) + y);
  431. }
  432. void tiler_align(enum tiler_fmt fmt, uint16_t *w, uint16_t *h)
  433. {
  434. BUG_ON(!validfmt(fmt));
  435. *w = round_up(*w, geom[fmt].slot_w);
  436. *h = round_up(*h, geom[fmt].slot_h);
  437. }
  438. uint32_t tiler_stride(enum tiler_fmt fmt, uint32_t orient)
  439. {
  440. BUG_ON(!validfmt(fmt));
  441. if (orient & MASK_XY_FLIP)
  442. return 1 << (CONT_HEIGHT_BITS + geom[fmt].x_shft);
  443. else
  444. return 1 << (CONT_WIDTH_BITS + geom[fmt].y_shft);
  445. }
  446. size_t tiler_size(enum tiler_fmt fmt, uint16_t w, uint16_t h)
  447. {
  448. tiler_align(fmt, &w, &h);
  449. return geom[fmt].cpp * w * h;
  450. }
  451. size_t tiler_vsize(enum tiler_fmt fmt, uint16_t w, uint16_t h)
  452. {
  453. BUG_ON(!validfmt(fmt));
  454. return round_up(geom[fmt].cpp * w, PAGE_SIZE) * h;
  455. }
  456. uint32_t tiler_get_cpu_cache_flags(void)
  457. {
  458. return omap_dmm->plat_data->cpu_cache_flags;
  459. }
  460. bool dmm_is_available(void)
  461. {
  462. return omap_dmm ? true : false;
  463. }
  464. static int omap_dmm_remove(struct platform_device *dev)
  465. {
  466. struct tiler_block *block, *_block;
  467. int i;
  468. unsigned long flags;
  469. if (omap_dmm) {
  470. /* free all area regions */
  471. spin_lock_irqsave(&list_lock, flags);
  472. list_for_each_entry_safe(block, _block, &omap_dmm->alloc_head,
  473. alloc_node) {
  474. list_del(&block->alloc_node);
  475. kfree(block);
  476. }
  477. spin_unlock_irqrestore(&list_lock, flags);
  478. for (i = 0; i < omap_dmm->num_lut; i++)
  479. if (omap_dmm->tcm && omap_dmm->tcm[i])
  480. omap_dmm->tcm[i]->deinit(omap_dmm->tcm[i]);
  481. kfree(omap_dmm->tcm);
  482. kfree(omap_dmm->engines);
  483. if (omap_dmm->refill_va)
  484. dma_free_wc(omap_dmm->dev,
  485. REFILL_BUFFER_SIZE * omap_dmm->num_engines,
  486. omap_dmm->refill_va, omap_dmm->refill_pa);
  487. if (omap_dmm->dummy_page)
  488. __free_page(omap_dmm->dummy_page);
  489. if (omap_dmm->irq > 0)
  490. free_irq(omap_dmm->irq, omap_dmm);
  491. iounmap(omap_dmm->base);
  492. kfree(omap_dmm);
  493. omap_dmm = NULL;
  494. }
  495. return 0;
  496. }
  497. static int omap_dmm_probe(struct platform_device *dev)
  498. {
  499. int ret = -EFAULT, i;
  500. struct tcm_area area = {0};
  501. u32 hwinfo, pat_geom;
  502. struct resource *mem;
  503. omap_dmm = kzalloc(sizeof(*omap_dmm), GFP_KERNEL);
  504. if (!omap_dmm)
  505. goto fail;
  506. /* initialize lists */
  507. INIT_LIST_HEAD(&omap_dmm->alloc_head);
  508. INIT_LIST_HEAD(&omap_dmm->idle_head);
  509. init_waitqueue_head(&omap_dmm->engine_queue);
  510. if (dev->dev.of_node) {
  511. const struct of_device_id *match;
  512. match = of_match_node(dmm_of_match, dev->dev.of_node);
  513. if (!match) {
  514. dev_err(&dev->dev, "failed to find matching device node\n");
  515. return -ENODEV;
  516. }
  517. omap_dmm->plat_data = match->data;
  518. }
  519. /* lookup hwmod data - base address and irq */
  520. mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  521. if (!mem) {
  522. dev_err(&dev->dev, "failed to get base address resource\n");
  523. goto fail;
  524. }
  525. omap_dmm->base = ioremap(mem->start, SZ_2K);
  526. if (!omap_dmm->base) {
  527. dev_err(&dev->dev, "failed to get dmm base address\n");
  528. goto fail;
  529. }
  530. omap_dmm->irq = platform_get_irq(dev, 0);
  531. if (omap_dmm->irq < 0) {
  532. dev_err(&dev->dev, "failed to get IRQ resource\n");
  533. goto fail;
  534. }
  535. omap_dmm->dev = &dev->dev;
  536. hwinfo = dmm_read(omap_dmm, DMM_PAT_HWINFO);
  537. omap_dmm->num_engines = (hwinfo >> 24) & 0x1F;
  538. omap_dmm->num_lut = (hwinfo >> 16) & 0x1F;
  539. omap_dmm->container_width = 256;
  540. omap_dmm->container_height = 128;
  541. atomic_set(&omap_dmm->engine_counter, omap_dmm->num_engines);
  542. /* read out actual LUT width and height */
  543. pat_geom = dmm_read(omap_dmm, DMM_PAT_GEOMETRY);
  544. omap_dmm->lut_width = ((pat_geom >> 16) & 0xF) << 5;
  545. omap_dmm->lut_height = ((pat_geom >> 24) & 0xF) << 5;
  546. /* increment LUT by one if on OMAP5 */
  547. /* LUT has twice the height, and is split into a separate container */
  548. if (omap_dmm->lut_height != omap_dmm->container_height)
  549. omap_dmm->num_lut++;
  550. /* initialize DMM registers */
  551. dmm_write(omap_dmm, 0x88888888, DMM_PAT_VIEW__0);
  552. dmm_write(omap_dmm, 0x88888888, DMM_PAT_VIEW__1);
  553. dmm_write(omap_dmm, 0x80808080, DMM_PAT_VIEW_MAP__0);
  554. dmm_write(omap_dmm, 0x80000000, DMM_PAT_VIEW_MAP_BASE);
  555. dmm_write(omap_dmm, 0x88888888, DMM_TILER_OR__0);
  556. dmm_write(omap_dmm, 0x88888888, DMM_TILER_OR__1);
  557. ret = request_irq(omap_dmm->irq, omap_dmm_irq_handler, IRQF_SHARED,
  558. "omap_dmm_irq_handler", omap_dmm);
  559. if (ret) {
  560. dev_err(&dev->dev, "couldn't register IRQ %d, error %d\n",
  561. omap_dmm->irq, ret);
  562. omap_dmm->irq = -1;
  563. goto fail;
  564. }
  565. /* Enable all interrupts for each refill engine except
  566. * ERR_LUT_MISS<n> (which is just advisory, and we don't care
  567. * about because we want to be able to refill live scanout
  568. * buffers for accelerated pan/scroll) and FILL_DSC<n> which
  569. * we just generally don't care about.
  570. */
  571. dmm_write(omap_dmm, 0x7e7e7e7e, DMM_PAT_IRQENABLE_SET);
  572. omap_dmm->dummy_page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  573. if (!omap_dmm->dummy_page) {
  574. dev_err(&dev->dev, "could not allocate dummy page\n");
  575. ret = -ENOMEM;
  576. goto fail;
  577. }
  578. /* set dma mask for device */
  579. ret = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
  580. if (ret)
  581. goto fail;
  582. omap_dmm->dummy_pa = page_to_phys(omap_dmm->dummy_page);
  583. /* alloc refill memory */
  584. omap_dmm->refill_va = dma_alloc_wc(&dev->dev,
  585. REFILL_BUFFER_SIZE * omap_dmm->num_engines,
  586. &omap_dmm->refill_pa, GFP_KERNEL);
  587. if (!omap_dmm->refill_va) {
  588. dev_err(&dev->dev, "could not allocate refill memory\n");
  589. goto fail;
  590. }
  591. /* alloc engines */
  592. omap_dmm->engines = kcalloc(omap_dmm->num_engines,
  593. sizeof(struct refill_engine), GFP_KERNEL);
  594. if (!omap_dmm->engines) {
  595. ret = -ENOMEM;
  596. goto fail;
  597. }
  598. for (i = 0; i < omap_dmm->num_engines; i++) {
  599. omap_dmm->engines[i].id = i;
  600. omap_dmm->engines[i].dmm = omap_dmm;
  601. omap_dmm->engines[i].refill_va = omap_dmm->refill_va +
  602. (REFILL_BUFFER_SIZE * i);
  603. omap_dmm->engines[i].refill_pa = omap_dmm->refill_pa +
  604. (REFILL_BUFFER_SIZE * i);
  605. init_completion(&omap_dmm->engines[i].compl);
  606. list_add(&omap_dmm->engines[i].idle_node, &omap_dmm->idle_head);
  607. }
  608. omap_dmm->tcm = kcalloc(omap_dmm->num_lut, sizeof(*omap_dmm->tcm),
  609. GFP_KERNEL);
  610. if (!omap_dmm->tcm) {
  611. ret = -ENOMEM;
  612. goto fail;
  613. }
  614. /* init containers */
  615. /* Each LUT is associated with a TCM (container manager). We use the
  616. lut_id to denote the lut_id used to identify the correct LUT for
  617. programming during reill operations */
  618. for (i = 0; i < omap_dmm->num_lut; i++) {
  619. omap_dmm->tcm[i] = sita_init(omap_dmm->container_width,
  620. omap_dmm->container_height);
  621. if (!omap_dmm->tcm[i]) {
  622. dev_err(&dev->dev, "failed to allocate container\n");
  623. ret = -ENOMEM;
  624. goto fail;
  625. }
  626. omap_dmm->tcm[i]->lut_id = i;
  627. }
  628. /* assign access mode containers to applicable tcm container */
  629. /* OMAP 4 has 1 container for all 4 views */
  630. /* OMAP 5 has 2 containers, 1 for 2D and 1 for 1D */
  631. containers[TILFMT_8BIT] = omap_dmm->tcm[0];
  632. containers[TILFMT_16BIT] = omap_dmm->tcm[0];
  633. containers[TILFMT_32BIT] = omap_dmm->tcm[0];
  634. if (omap_dmm->container_height != omap_dmm->lut_height) {
  635. /* second LUT is used for PAGE mode. Programming must use
  636. y offset that is added to all y coordinates. LUT id is still
  637. 0, because it is the same LUT, just the upper 128 lines */
  638. containers[TILFMT_PAGE] = omap_dmm->tcm[1];
  639. omap_dmm->tcm[1]->y_offset = OMAP5_LUT_OFFSET;
  640. omap_dmm->tcm[1]->lut_id = 0;
  641. } else {
  642. containers[TILFMT_PAGE] = omap_dmm->tcm[0];
  643. }
  644. area = (struct tcm_area) {
  645. .tcm = NULL,
  646. .p1.x = omap_dmm->container_width - 1,
  647. .p1.y = omap_dmm->container_height - 1,
  648. };
  649. /* initialize all LUTs to dummy page entries */
  650. for (i = 0; i < omap_dmm->num_lut; i++) {
  651. area.tcm = omap_dmm->tcm[i];
  652. if (fill(&area, NULL, 0, 0, true))
  653. dev_err(omap_dmm->dev, "refill failed");
  654. }
  655. dev_info(omap_dmm->dev, "initialized all PAT entries\n");
  656. return 0;
  657. fail:
  658. if (omap_dmm_remove(dev))
  659. dev_err(&dev->dev, "cleanup failed\n");
  660. return ret;
  661. }
  662. /*
  663. * debugfs support
  664. */
  665. #ifdef CONFIG_DEBUG_FS
  666. static const char *alphabet = "abcdefghijklmnopqrstuvwxyz"
  667. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  668. static const char *special = ".,:;'\"`~!^-+";
  669. static void fill_map(char **map, int xdiv, int ydiv, struct tcm_area *a,
  670. char c, bool ovw)
  671. {
  672. int x, y;
  673. for (y = a->p0.y / ydiv; y <= a->p1.y / ydiv; y++)
  674. for (x = a->p0.x / xdiv; x <= a->p1.x / xdiv; x++)
  675. if (map[y][x] == ' ' || ovw)
  676. map[y][x] = c;
  677. }
  678. static void fill_map_pt(char **map, int xdiv, int ydiv, struct tcm_pt *p,
  679. char c)
  680. {
  681. map[p->y / ydiv][p->x / xdiv] = c;
  682. }
  683. static char read_map_pt(char **map, int xdiv, int ydiv, struct tcm_pt *p)
  684. {
  685. return map[p->y / ydiv][p->x / xdiv];
  686. }
  687. static int map_width(int xdiv, int x0, int x1)
  688. {
  689. return (x1 / xdiv) - (x0 / xdiv) + 1;
  690. }
  691. static void text_map(char **map, int xdiv, char *nice, int yd, int x0, int x1)
  692. {
  693. char *p = map[yd] + (x0 / xdiv);
  694. int w = (map_width(xdiv, x0, x1) - strlen(nice)) / 2;
  695. if (w >= 0) {
  696. p += w;
  697. while (*nice)
  698. *p++ = *nice++;
  699. }
  700. }
  701. static void map_1d_info(char **map, int xdiv, int ydiv, char *nice,
  702. struct tcm_area *a)
  703. {
  704. sprintf(nice, "%dK", tcm_sizeof(*a) * 4);
  705. if (a->p0.y + 1 < a->p1.y) {
  706. text_map(map, xdiv, nice, (a->p0.y + a->p1.y) / 2 / ydiv, 0,
  707. 256 - 1);
  708. } else if (a->p0.y < a->p1.y) {
  709. if (strlen(nice) < map_width(xdiv, a->p0.x, 256 - 1))
  710. text_map(map, xdiv, nice, a->p0.y / ydiv,
  711. a->p0.x + xdiv, 256 - 1);
  712. else if (strlen(nice) < map_width(xdiv, 0, a->p1.x))
  713. text_map(map, xdiv, nice, a->p1.y / ydiv,
  714. 0, a->p1.y - xdiv);
  715. } else if (strlen(nice) + 1 < map_width(xdiv, a->p0.x, a->p1.x)) {
  716. text_map(map, xdiv, nice, a->p0.y / ydiv, a->p0.x, a->p1.x);
  717. }
  718. }
  719. static void map_2d_info(char **map, int xdiv, int ydiv, char *nice,
  720. struct tcm_area *a)
  721. {
  722. sprintf(nice, "(%d*%d)", tcm_awidth(*a), tcm_aheight(*a));
  723. if (strlen(nice) + 1 < map_width(xdiv, a->p0.x, a->p1.x))
  724. text_map(map, xdiv, nice, (a->p0.y + a->p1.y) / 2 / ydiv,
  725. a->p0.x, a->p1.x);
  726. }
  727. int tiler_map_show(struct seq_file *s, void *arg)
  728. {
  729. int xdiv = 2, ydiv = 1;
  730. char **map = NULL, *global_map;
  731. struct tiler_block *block;
  732. struct tcm_area a, p;
  733. int i;
  734. const char *m2d = alphabet;
  735. const char *a2d = special;
  736. const char *m2dp = m2d, *a2dp = a2d;
  737. char nice[128];
  738. int h_adj;
  739. int w_adj;
  740. unsigned long flags;
  741. int lut_idx;
  742. if (!omap_dmm) {
  743. /* early return if dmm/tiler device is not initialized */
  744. return 0;
  745. }
  746. h_adj = omap_dmm->container_height / ydiv;
  747. w_adj = omap_dmm->container_width / xdiv;
  748. map = kmalloc(h_adj * sizeof(*map), GFP_KERNEL);
  749. global_map = kmalloc((w_adj + 1) * h_adj, GFP_KERNEL);
  750. if (!map || !global_map)
  751. goto error;
  752. for (lut_idx = 0; lut_idx < omap_dmm->num_lut; lut_idx++) {
  753. memset(map, 0, h_adj * sizeof(*map));
  754. memset(global_map, ' ', (w_adj + 1) * h_adj);
  755. for (i = 0; i < omap_dmm->container_height; i++) {
  756. map[i] = global_map + i * (w_adj + 1);
  757. map[i][w_adj] = 0;
  758. }
  759. spin_lock_irqsave(&list_lock, flags);
  760. list_for_each_entry(block, &omap_dmm->alloc_head, alloc_node) {
  761. if (block->area.tcm == omap_dmm->tcm[lut_idx]) {
  762. if (block->fmt != TILFMT_PAGE) {
  763. fill_map(map, xdiv, ydiv, &block->area,
  764. *m2dp, true);
  765. if (!*++a2dp)
  766. a2dp = a2d;
  767. if (!*++m2dp)
  768. m2dp = m2d;
  769. map_2d_info(map, xdiv, ydiv, nice,
  770. &block->area);
  771. } else {
  772. bool start = read_map_pt(map, xdiv,
  773. ydiv, &block->area.p0) == ' ';
  774. bool end = read_map_pt(map, xdiv, ydiv,
  775. &block->area.p1) == ' ';
  776. tcm_for_each_slice(a, block->area, p)
  777. fill_map(map, xdiv, ydiv, &a,
  778. '=', true);
  779. fill_map_pt(map, xdiv, ydiv,
  780. &block->area.p0,
  781. start ? '<' : 'X');
  782. fill_map_pt(map, xdiv, ydiv,
  783. &block->area.p1,
  784. end ? '>' : 'X');
  785. map_1d_info(map, xdiv, ydiv, nice,
  786. &block->area);
  787. }
  788. }
  789. }
  790. spin_unlock_irqrestore(&list_lock, flags);
  791. if (s) {
  792. seq_printf(s, "CONTAINER %d DUMP BEGIN\n", lut_idx);
  793. for (i = 0; i < 128; i++)
  794. seq_printf(s, "%03d:%s\n", i, map[i]);
  795. seq_printf(s, "CONTAINER %d DUMP END\n", lut_idx);
  796. } else {
  797. dev_dbg(omap_dmm->dev, "CONTAINER %d DUMP BEGIN\n",
  798. lut_idx);
  799. for (i = 0; i < 128; i++)
  800. dev_dbg(omap_dmm->dev, "%03d:%s\n", i, map[i]);
  801. dev_dbg(omap_dmm->dev, "CONTAINER %d DUMP END\n",
  802. lut_idx);
  803. }
  804. }
  805. error:
  806. kfree(map);
  807. kfree(global_map);
  808. return 0;
  809. }
  810. #endif
  811. #ifdef CONFIG_PM_SLEEP
  812. static int omap_dmm_resume(struct device *dev)
  813. {
  814. struct tcm_area area;
  815. int i;
  816. if (!omap_dmm)
  817. return -ENODEV;
  818. area = (struct tcm_area) {
  819. .tcm = NULL,
  820. .p1.x = omap_dmm->container_width - 1,
  821. .p1.y = omap_dmm->container_height - 1,
  822. };
  823. /* initialize all LUTs to dummy page entries */
  824. for (i = 0; i < omap_dmm->num_lut; i++) {
  825. area.tcm = omap_dmm->tcm[i];
  826. if (fill(&area, NULL, 0, 0, true))
  827. dev_err(dev, "refill failed");
  828. }
  829. return 0;
  830. }
  831. #endif
  832. static SIMPLE_DEV_PM_OPS(omap_dmm_pm_ops, NULL, omap_dmm_resume);
  833. #if defined(CONFIG_OF)
  834. static const struct dmm_platform_data dmm_omap4_platform_data = {
  835. .cpu_cache_flags = OMAP_BO_WC,
  836. };
  837. static const struct dmm_platform_data dmm_omap5_platform_data = {
  838. .cpu_cache_flags = OMAP_BO_UNCACHED,
  839. };
  840. static const struct of_device_id dmm_of_match[] = {
  841. {
  842. .compatible = "ti,omap4-dmm",
  843. .data = &dmm_omap4_platform_data,
  844. },
  845. {
  846. .compatible = "ti,omap5-dmm",
  847. .data = &dmm_omap5_platform_data,
  848. },
  849. {},
  850. };
  851. #endif
  852. struct platform_driver omap_dmm_driver = {
  853. .probe = omap_dmm_probe,
  854. .remove = omap_dmm_remove,
  855. .driver = {
  856. .owner = THIS_MODULE,
  857. .name = DMM_DRIVER_NAME,
  858. .of_match_table = of_match_ptr(dmm_of_match),
  859. .pm = &omap_dmm_pm_ops,
  860. },
  861. };
  862. MODULE_LICENSE("GPL v2");
  863. MODULE_AUTHOR("Andy Gross <andy.gross@ti.com>");
  864. MODULE_DESCRIPTION("OMAP DMM/Tiler Driver");