omap_dmm_tiler.c 26 KB

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