omap_dmm_tiler.c 27 KB

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