omap_dmm_tiler.c 25 KB

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