omap_dmm_tiler.c 27 KB

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