omap_dmm_tiler.c 30 KB

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