omap_dmm_tiler.c 30 KB

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