omap_dmm_tiler.c 26 KB

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