omap_dmm_tiler.c 26 KB

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