omap_dmm_tiler.c 25 KB

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