omap_dmm_tiler.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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 "omap_dmm_tiler.h"
  28. #include "omap_dmm_priv.h"
  29. #define DMM_DRIVER_NAME "dmm"
  30. /* mappings for associating views to luts */
  31. static struct tcm *containers[TILFMT_NFORMATS];
  32. static struct dmm *omap_dmm;
  33. #if defined(CONFIG_OF)
  34. static const struct of_device_id dmm_of_match[];
  35. #endif
  36. /* global spinlock for protecting lists */
  37. static DEFINE_SPINLOCK(list_lock);
  38. /* Geometry table */
  39. #define GEOM(xshift, yshift, bytes_per_pixel) { \
  40. .x_shft = (xshift), \
  41. .y_shft = (yshift), \
  42. .cpp = (bytes_per_pixel), \
  43. .slot_w = 1 << (SLOT_WIDTH_BITS - (xshift)), \
  44. .slot_h = 1 << (SLOT_HEIGHT_BITS - (yshift)), \
  45. }
  46. static const struct {
  47. u32 x_shft; /* unused X-bits (as part of bpp) */
  48. u32 y_shft; /* unused Y-bits (as part of bpp) */
  49. u32 cpp; /* bytes/chars per pixel */
  50. u32 slot_w; /* width of each slot (in pixels) */
  51. u32 slot_h; /* height of each slot (in pixels) */
  52. } geom[TILFMT_NFORMATS] = {
  53. [TILFMT_8BIT] = GEOM(0, 0, 1),
  54. [TILFMT_16BIT] = GEOM(0, 1, 2),
  55. [TILFMT_32BIT] = GEOM(1, 1, 4),
  56. [TILFMT_PAGE] = GEOM(SLOT_WIDTH_BITS, SLOT_HEIGHT_BITS, 1),
  57. };
  58. /* lookup table for registers w/ per-engine instances */
  59. static const u32 reg[][4] = {
  60. [PAT_STATUS] = {DMM_PAT_STATUS__0, DMM_PAT_STATUS__1,
  61. DMM_PAT_STATUS__2, DMM_PAT_STATUS__3},
  62. [PAT_DESCR] = {DMM_PAT_DESCR__0, DMM_PAT_DESCR__1,
  63. DMM_PAT_DESCR__2, DMM_PAT_DESCR__3},
  64. };
  65. static int dmm_dma_copy(struct dmm *dmm, dma_addr_t src, dma_addr_t dst)
  66. {
  67. struct dma_async_tx_descriptor *tx;
  68. enum dma_status status;
  69. dma_cookie_t cookie;
  70. tx = dmaengine_prep_dma_memcpy(dmm->wa_dma_chan, dst, src, 4, 0);
  71. if (!tx) {
  72. dev_err(dmm->dev, "Failed to prepare DMA memcpy\n");
  73. return -EIO;
  74. }
  75. cookie = tx->tx_submit(tx);
  76. if (dma_submit_error(cookie)) {
  77. dev_err(dmm->dev, "Failed to do DMA tx_submit\n");
  78. return -EIO;
  79. }
  80. status = dma_sync_wait(dmm->wa_dma_chan, cookie);
  81. if (status != DMA_COMPLETE)
  82. dev_err(dmm->dev, "i878 wa DMA copy failure\n");
  83. dmaengine_terminate_all(dmm->wa_dma_chan);
  84. return 0;
  85. }
  86. static u32 dmm_read_wa(struct dmm *dmm, u32 reg)
  87. {
  88. dma_addr_t src, dst;
  89. int r;
  90. src = dmm->phys_base + reg;
  91. dst = dmm->wa_dma_handle;
  92. r = dmm_dma_copy(dmm, src, dst);
  93. if (r) {
  94. dev_err(dmm->dev, "sDMA read transfer timeout\n");
  95. return readl(dmm->base + reg);
  96. }
  97. /*
  98. * As per i878 workaround, the DMA is used to access the DMM registers.
  99. * Make sure that the readl is not moved by the compiler or the CPU
  100. * earlier than the DMA finished writing the value to memory.
  101. */
  102. rmb();
  103. return readl(dmm->wa_dma_data);
  104. }
  105. static void dmm_write_wa(struct dmm *dmm, u32 val, u32 reg)
  106. {
  107. dma_addr_t src, dst;
  108. int r;
  109. writel(val, dmm->wa_dma_data);
  110. /*
  111. * As per i878 workaround, the DMA is used to access the DMM registers.
  112. * Make sure that the writel is not moved by the compiler or the CPU, so
  113. * the data will be in place before we start the DMA to do the actual
  114. * register write.
  115. */
  116. wmb();
  117. src = dmm->wa_dma_handle;
  118. dst = dmm->phys_base + reg;
  119. r = dmm_dma_copy(dmm, src, dst);
  120. if (r) {
  121. dev_err(dmm->dev, "sDMA write transfer timeout\n");
  122. writel(val, dmm->base + reg);
  123. }
  124. }
  125. static u32 dmm_read(struct dmm *dmm, u32 reg)
  126. {
  127. if (dmm->dmm_workaround) {
  128. u32 v;
  129. unsigned long flags;
  130. spin_lock_irqsave(&dmm->wa_lock, flags);
  131. v = dmm_read_wa(dmm, reg);
  132. spin_unlock_irqrestore(&dmm->wa_lock, flags);
  133. return v;
  134. } else {
  135. return readl(dmm->base + reg);
  136. }
  137. }
  138. static void dmm_write(struct dmm *dmm, u32 val, u32 reg)
  139. {
  140. if (dmm->dmm_workaround) {
  141. unsigned long flags;
  142. spin_lock_irqsave(&dmm->wa_lock, flags);
  143. dmm_write_wa(dmm, val, reg);
  144. spin_unlock_irqrestore(&dmm->wa_lock, flags);
  145. } else {
  146. writel(val, dmm->base + reg);
  147. }
  148. }
  149. static int dmm_workaround_init(struct dmm *dmm)
  150. {
  151. dma_cap_mask_t mask;
  152. spin_lock_init(&dmm->wa_lock);
  153. dmm->wa_dma_data = dma_alloc_coherent(dmm->dev, sizeof(u32),
  154. &dmm->wa_dma_handle, GFP_KERNEL);
  155. if (!dmm->wa_dma_data)
  156. return -ENOMEM;
  157. dma_cap_zero(mask);
  158. dma_cap_set(DMA_MEMCPY, mask);
  159. dmm->wa_dma_chan = dma_request_channel(mask, NULL, NULL);
  160. if (!dmm->wa_dma_chan) {
  161. dma_free_coherent(dmm->dev, 4, dmm->wa_dma_data, dmm->wa_dma_handle);
  162. return -ENODEV;
  163. }
  164. return 0;
  165. }
  166. static void dmm_workaround_uninit(struct dmm *dmm)
  167. {
  168. dma_release_channel(dmm->wa_dma_chan);
  169. dma_free_coherent(dmm->dev, 4, dmm->wa_dma_data, dmm->wa_dma_handle);
  170. }
  171. /* simple allocator to grab next 16 byte aligned memory from txn */
  172. static void *alloc_dma(struct dmm_txn *txn, size_t sz, dma_addr_t *pa)
  173. {
  174. void *ptr;
  175. struct refill_engine *engine = txn->engine_handle;
  176. /* dmm programming requires 16 byte aligned addresses */
  177. txn->current_pa = round_up(txn->current_pa, 16);
  178. txn->current_va = (void *)round_up((long)txn->current_va, 16);
  179. ptr = txn->current_va;
  180. *pa = txn->current_pa;
  181. txn->current_pa += sz;
  182. txn->current_va += sz;
  183. BUG_ON((txn->current_va - engine->refill_va) > REFILL_BUFFER_SIZE);
  184. return ptr;
  185. }
  186. /* check status and spin until wait_mask comes true */
  187. static int wait_status(struct refill_engine *engine, u32 wait_mask)
  188. {
  189. struct dmm *dmm = engine->dmm;
  190. u32 r = 0, err, i;
  191. i = DMM_FIXED_RETRY_COUNT;
  192. while (true) {
  193. r = dmm_read(dmm, reg[PAT_STATUS][engine->id]);
  194. err = r & DMM_PATSTATUS_ERR;
  195. if (err) {
  196. dev_err(dmm->dev,
  197. "%s: error (engine%d). PAT_STATUS: 0x%08x\n",
  198. __func__, engine->id, r);
  199. return -EFAULT;
  200. }
  201. if ((r & wait_mask) == wait_mask)
  202. break;
  203. if (--i == 0) {
  204. dev_err(dmm->dev,
  205. "%s: timeout (engine%d). PAT_STATUS: 0x%08x\n",
  206. __func__, engine->id, r);
  207. return -ETIMEDOUT;
  208. }
  209. udelay(1);
  210. }
  211. return 0;
  212. }
  213. static void release_engine(struct refill_engine *engine)
  214. {
  215. unsigned long flags;
  216. spin_lock_irqsave(&list_lock, flags);
  217. list_add(&engine->idle_node, &omap_dmm->idle_head);
  218. spin_unlock_irqrestore(&list_lock, flags);
  219. atomic_inc(&omap_dmm->engine_counter);
  220. wake_up_interruptible(&omap_dmm->engine_queue);
  221. }
  222. static irqreturn_t omap_dmm_irq_handler(int irq, void *arg)
  223. {
  224. struct dmm *dmm = arg;
  225. u32 status = dmm_read(dmm, DMM_PAT_IRQSTATUS);
  226. int i;
  227. /* ack IRQ */
  228. dmm_write(dmm, status, DMM_PAT_IRQSTATUS);
  229. for (i = 0; i < dmm->num_engines; i++) {
  230. if (status & DMM_IRQSTAT_ERR_MASK)
  231. dev_err(dmm->dev,
  232. "irq error(engine%d): IRQSTAT 0x%02x\n",
  233. i, status & 0xff);
  234. if (status & DMM_IRQSTAT_LST) {
  235. if (dmm->engines[i].async)
  236. release_engine(&dmm->engines[i]);
  237. complete(&dmm->engines[i].compl);
  238. }
  239. status >>= 8;
  240. }
  241. return IRQ_HANDLED;
  242. }
  243. /*
  244. * Get a handle for a DMM transaction
  245. */
  246. static struct dmm_txn *dmm_txn_init(struct dmm *dmm, struct tcm *tcm)
  247. {
  248. struct dmm_txn *txn = NULL;
  249. struct refill_engine *engine = NULL;
  250. int ret;
  251. unsigned long flags;
  252. /* wait until an engine is available */
  253. ret = wait_event_interruptible(omap_dmm->engine_queue,
  254. atomic_add_unless(&omap_dmm->engine_counter, -1, 0));
  255. if (ret)
  256. return ERR_PTR(ret);
  257. /* grab an idle engine */
  258. spin_lock_irqsave(&list_lock, flags);
  259. if (!list_empty(&dmm->idle_head)) {
  260. engine = list_entry(dmm->idle_head.next, struct refill_engine,
  261. idle_node);
  262. list_del(&engine->idle_node);
  263. }
  264. spin_unlock_irqrestore(&list_lock, flags);
  265. BUG_ON(!engine);
  266. txn = &engine->txn;
  267. engine->tcm = tcm;
  268. txn->engine_handle = engine;
  269. txn->last_pat = NULL;
  270. txn->current_va = engine->refill_va;
  271. txn->current_pa = engine->refill_pa;
  272. return txn;
  273. }
  274. /*
  275. * Add region to DMM transaction. If pages or pages[i] is NULL, then the
  276. * corresponding slot is cleared (ie. dummy_pa is programmed)
  277. */
  278. static void dmm_txn_append(struct dmm_txn *txn, struct pat_area *area,
  279. struct page **pages, u32 npages, u32 roll)
  280. {
  281. dma_addr_t pat_pa = 0, data_pa = 0;
  282. u32 *data;
  283. struct pat *pat;
  284. struct refill_engine *engine = txn->engine_handle;
  285. int columns = (1 + area->x1 - area->x0);
  286. int rows = (1 + area->y1 - area->y0);
  287. int i = columns*rows;
  288. pat = alloc_dma(txn, sizeof(*pat), &pat_pa);
  289. if (txn->last_pat)
  290. txn->last_pat->next_pa = (u32)pat_pa;
  291. pat->area = *area;
  292. /* adjust Y coordinates based off of container parameters */
  293. pat->area.y0 += engine->tcm->y_offset;
  294. pat->area.y1 += engine->tcm->y_offset;
  295. pat->ctrl = (struct pat_ctrl){
  296. .start = 1,
  297. .lut_id = engine->tcm->lut_id,
  298. };
  299. data = alloc_dma(txn, 4*i, &data_pa);
  300. /* FIXME: what if data_pa is more than 32-bit ? */
  301. pat->data_pa = data_pa;
  302. while (i--) {
  303. int n = i + roll;
  304. if (n >= npages)
  305. n -= npages;
  306. data[i] = (pages && pages[n]) ?
  307. page_to_phys(pages[n]) : engine->dmm->dummy_pa;
  308. }
  309. txn->last_pat = pat;
  310. return;
  311. }
  312. /*
  313. * Commit the DMM transaction.
  314. */
  315. static int dmm_txn_commit(struct dmm_txn *txn, bool wait)
  316. {
  317. int ret = 0;
  318. struct refill_engine *engine = txn->engine_handle;
  319. struct dmm *dmm = engine->dmm;
  320. if (!txn->last_pat) {
  321. dev_err(engine->dmm->dev, "need at least one txn\n");
  322. ret = -EINVAL;
  323. goto cleanup;
  324. }
  325. txn->last_pat->next_pa = 0;
  326. /* ensure that the written descriptors are visible to DMM */
  327. wmb();
  328. /*
  329. * NOTE: the wmb() above should be enough, but there seems to be a bug
  330. * in OMAP's memory barrier implementation, which in some rare cases may
  331. * cause the writes not to be observable after wmb().
  332. */
  333. /* read back to ensure the data is in RAM */
  334. readl(&txn->last_pat->next_pa);
  335. /* write to PAT_DESCR to clear out any pending transaction */
  336. dmm_write(dmm, 0x0, reg[PAT_DESCR][engine->id]);
  337. /* wait for engine ready: */
  338. ret = wait_status(engine, DMM_PATSTATUS_READY);
  339. if (ret) {
  340. ret = -EFAULT;
  341. goto cleanup;
  342. }
  343. /* mark whether it is async to denote list management in IRQ handler */
  344. engine->async = wait ? false : true;
  345. reinit_completion(&engine->compl);
  346. /* verify that the irq handler sees the 'async' and completion value */
  347. smp_mb();
  348. /* kick reload */
  349. dmm_write(dmm, engine->refill_pa, reg[PAT_DESCR][engine->id]);
  350. if (wait) {
  351. if (!wait_for_completion_timeout(&engine->compl,
  352. msecs_to_jiffies(100))) {
  353. dev_err(dmm->dev, "timed out waiting for done\n");
  354. ret = -ETIMEDOUT;
  355. goto cleanup;
  356. }
  357. /* Check the engine status before continue */
  358. ret = wait_status(engine, DMM_PATSTATUS_READY |
  359. DMM_PATSTATUS_VALID | DMM_PATSTATUS_DONE);
  360. }
  361. cleanup:
  362. /* only place engine back on list if we are done with it */
  363. if (ret || wait)
  364. release_engine(engine);
  365. return ret;
  366. }
  367. /*
  368. * DMM programming
  369. */
  370. static int fill(struct tcm_area *area, struct page **pages,
  371. u32 npages, u32 roll, bool wait)
  372. {
  373. int ret = 0;
  374. struct tcm_area slice, area_s;
  375. struct dmm_txn *txn;
  376. /*
  377. * FIXME
  378. *
  379. * Asynchronous fill does not work reliably, as the driver does not
  380. * handle errors in the async code paths. The fill operation may
  381. * silently fail, leading to leaking DMM engines, which may eventually
  382. * lead to deadlock if we run out of DMM engines.
  383. *
  384. * For now, always set 'wait' so that we only use sync fills. Async
  385. * fills should be fixed, or alternatively we could decide to only
  386. * support sync fills and so the whole async code path could be removed.
  387. */
  388. wait = true;
  389. txn = dmm_txn_init(omap_dmm, area->tcm);
  390. if (IS_ERR_OR_NULL(txn))
  391. return -ENOMEM;
  392. tcm_for_each_slice(slice, *area, area_s) {
  393. struct pat_area p_area = {
  394. .x0 = slice.p0.x, .y0 = slice.p0.y,
  395. .x1 = slice.p1.x, .y1 = slice.p1.y,
  396. };
  397. dmm_txn_append(txn, &p_area, pages, npages, roll);
  398. roll += tcm_sizeof(slice);
  399. }
  400. ret = dmm_txn_commit(txn, wait);
  401. return ret;
  402. }
  403. /*
  404. * Pin/unpin
  405. */
  406. /* note: slots for which pages[i] == NULL are filled w/ dummy page
  407. */
  408. int tiler_pin(struct tiler_block *block, struct page **pages,
  409. u32 npages, u32 roll, bool wait)
  410. {
  411. int ret;
  412. ret = fill(&block->area, pages, npages, roll, wait);
  413. if (ret)
  414. tiler_unpin(block);
  415. return ret;
  416. }
  417. int tiler_unpin(struct tiler_block *block)
  418. {
  419. return fill(&block->area, NULL, 0, 0, false);
  420. }
  421. /*
  422. * Reserve/release
  423. */
  424. struct tiler_block *tiler_reserve_2d(enum tiler_fmt fmt, u16 w,
  425. u16 h, u16 align)
  426. {
  427. struct tiler_block *block;
  428. u32 min_align = 128;
  429. int ret;
  430. unsigned long flags;
  431. u32 slot_bytes;
  432. block = kzalloc(sizeof(*block), GFP_KERNEL);
  433. if (!block)
  434. return ERR_PTR(-ENOMEM);
  435. BUG_ON(!validfmt(fmt));
  436. /* convert width/height to slots */
  437. w = DIV_ROUND_UP(w, geom[fmt].slot_w);
  438. h = DIV_ROUND_UP(h, geom[fmt].slot_h);
  439. /* convert alignment to slots */
  440. slot_bytes = geom[fmt].slot_w * geom[fmt].cpp;
  441. min_align = max(min_align, slot_bytes);
  442. align = (align > min_align) ? ALIGN(align, min_align) : min_align;
  443. align /= slot_bytes;
  444. block->fmt = fmt;
  445. ret = tcm_reserve_2d(containers[fmt], w, h, align, -1, slot_bytes,
  446. &block->area);
  447. if (ret) {
  448. kfree(block);
  449. return ERR_PTR(-ENOMEM);
  450. }
  451. /* add to allocation list */
  452. spin_lock_irqsave(&list_lock, flags);
  453. list_add(&block->alloc_node, &omap_dmm->alloc_head);
  454. spin_unlock_irqrestore(&list_lock, flags);
  455. return block;
  456. }
  457. struct tiler_block *tiler_reserve_1d(size_t size)
  458. {
  459. struct tiler_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  460. int num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  461. unsigned long flags;
  462. if (!block)
  463. return ERR_PTR(-ENOMEM);
  464. block->fmt = TILFMT_PAGE;
  465. if (tcm_reserve_1d(containers[TILFMT_PAGE], num_pages,
  466. &block->area)) {
  467. kfree(block);
  468. return ERR_PTR(-ENOMEM);
  469. }
  470. spin_lock_irqsave(&list_lock, flags);
  471. list_add(&block->alloc_node, &omap_dmm->alloc_head);
  472. spin_unlock_irqrestore(&list_lock, flags);
  473. return block;
  474. }
  475. /* note: if you have pin'd pages, you should have already unpin'd first! */
  476. int tiler_release(struct tiler_block *block)
  477. {
  478. int ret = tcm_free(&block->area);
  479. unsigned long flags;
  480. if (block->area.tcm)
  481. dev_err(omap_dmm->dev, "failed to release block\n");
  482. spin_lock_irqsave(&list_lock, flags);
  483. list_del(&block->alloc_node);
  484. spin_unlock_irqrestore(&list_lock, flags);
  485. kfree(block);
  486. return ret;
  487. }
  488. /*
  489. * Utils
  490. */
  491. /* calculate the tiler space address of a pixel in a view orientation...
  492. * below description copied from the display subsystem section of TRM:
  493. *
  494. * When the TILER is addressed, the bits:
  495. * [28:27] = 0x0 for 8-bit tiled
  496. * 0x1 for 16-bit tiled
  497. * 0x2 for 32-bit tiled
  498. * 0x3 for page mode
  499. * [31:29] = 0x0 for 0-degree view
  500. * 0x1 for 180-degree view + mirroring
  501. * 0x2 for 0-degree view + mirroring
  502. * 0x3 for 180-degree view
  503. * 0x4 for 270-degree view + mirroring
  504. * 0x5 for 270-degree view
  505. * 0x6 for 90-degree view
  506. * 0x7 for 90-degree view + mirroring
  507. * Otherwise the bits indicated the corresponding bit address to access
  508. * the SDRAM.
  509. */
  510. static u32 tiler_get_address(enum tiler_fmt fmt, u32 orient, u32 x, u32 y)
  511. {
  512. u32 x_bits, y_bits, tmp, x_mask, y_mask, alignment;
  513. x_bits = CONT_WIDTH_BITS - geom[fmt].x_shft;
  514. y_bits = CONT_HEIGHT_BITS - geom[fmt].y_shft;
  515. alignment = geom[fmt].x_shft + geom[fmt].y_shft;
  516. /* validate coordinate */
  517. x_mask = MASK(x_bits);
  518. y_mask = MASK(y_bits);
  519. if (x < 0 || x > x_mask || y < 0 || y > y_mask) {
  520. DBG("invalid coords: %u < 0 || %u > %u || %u < 0 || %u > %u",
  521. x, x, x_mask, y, y, y_mask);
  522. return 0;
  523. }
  524. /* account for mirroring */
  525. if (orient & MASK_X_INVERT)
  526. x ^= x_mask;
  527. if (orient & MASK_Y_INVERT)
  528. y ^= y_mask;
  529. /* get coordinate address */
  530. if (orient & MASK_XY_FLIP)
  531. tmp = ((x << y_bits) + y);
  532. else
  533. tmp = ((y << x_bits) + x);
  534. return TIL_ADDR((tmp << alignment), orient, fmt);
  535. }
  536. dma_addr_t tiler_ssptr(struct tiler_block *block)
  537. {
  538. BUG_ON(!validfmt(block->fmt));
  539. return TILVIEW_8BIT + tiler_get_address(block->fmt, 0,
  540. block->area.p0.x * geom[block->fmt].slot_w,
  541. block->area.p0.y * geom[block->fmt].slot_h);
  542. }
  543. dma_addr_t tiler_tsptr(struct tiler_block *block, u32 orient,
  544. u32 x, u32 y)
  545. {
  546. struct tcm_pt *p = &block->area.p0;
  547. BUG_ON(!validfmt(block->fmt));
  548. return tiler_get_address(block->fmt, orient,
  549. (p->x * geom[block->fmt].slot_w) + x,
  550. (p->y * geom[block->fmt].slot_h) + y);
  551. }
  552. void tiler_align(enum tiler_fmt fmt, u16 *w, u16 *h)
  553. {
  554. BUG_ON(!validfmt(fmt));
  555. *w = round_up(*w, geom[fmt].slot_w);
  556. *h = round_up(*h, geom[fmt].slot_h);
  557. }
  558. u32 tiler_stride(enum tiler_fmt fmt, u32 orient)
  559. {
  560. BUG_ON(!validfmt(fmt));
  561. if (orient & MASK_XY_FLIP)
  562. return 1 << (CONT_HEIGHT_BITS + geom[fmt].x_shft);
  563. else
  564. return 1 << (CONT_WIDTH_BITS + geom[fmt].y_shft);
  565. }
  566. size_t tiler_size(enum tiler_fmt fmt, u16 w, u16 h)
  567. {
  568. tiler_align(fmt, &w, &h);
  569. return geom[fmt].cpp * w * h;
  570. }
  571. size_t tiler_vsize(enum tiler_fmt fmt, u16 w, u16 h)
  572. {
  573. BUG_ON(!validfmt(fmt));
  574. return round_up(geom[fmt].cpp * w, PAGE_SIZE) * h;
  575. }
  576. u32 tiler_get_cpu_cache_flags(void)
  577. {
  578. return omap_dmm->plat_data->cpu_cache_flags;
  579. }
  580. bool dmm_is_available(void)
  581. {
  582. return omap_dmm ? true : false;
  583. }
  584. static void omap_dmm_remove(struct platform_device *dev)
  585. {
  586. struct tiler_block *block, *_block;
  587. int i;
  588. unsigned long flags;
  589. if (omap_dmm) {
  590. /* Disable all enabled interrupts */
  591. dmm_write(omap_dmm, 0x7e7e7e7e, DMM_PAT_IRQENABLE_CLR);
  592. free_irq(omap_dmm->irq, omap_dmm);
  593. /* free all area regions */
  594. spin_lock_irqsave(&list_lock, flags);
  595. list_for_each_entry_safe(block, _block, &omap_dmm->alloc_head,
  596. alloc_node) {
  597. list_del(&block->alloc_node);
  598. kfree(block);
  599. }
  600. spin_unlock_irqrestore(&list_lock, flags);
  601. for (i = 0; i < omap_dmm->num_lut; i++)
  602. if (omap_dmm->tcm && omap_dmm->tcm[i])
  603. omap_dmm->tcm[i]->deinit(omap_dmm->tcm[i]);
  604. kfree(omap_dmm->tcm);
  605. kfree(omap_dmm->engines);
  606. if (omap_dmm->refill_va)
  607. dma_free_wc(omap_dmm->dev,
  608. REFILL_BUFFER_SIZE * omap_dmm->num_engines,
  609. omap_dmm->refill_va, omap_dmm->refill_pa);
  610. if (omap_dmm->dummy_page)
  611. __free_page(omap_dmm->dummy_page);
  612. if (omap_dmm->dmm_workaround)
  613. dmm_workaround_uninit(omap_dmm);
  614. iounmap(omap_dmm->base);
  615. kfree(omap_dmm);
  616. omap_dmm = NULL;
  617. }
  618. }
  619. static int omap_dmm_probe(struct platform_device *dev)
  620. {
  621. int ret = -EFAULT, i;
  622. struct tcm_area area = {0};
  623. u32 hwinfo, pat_geom;
  624. struct resource *mem;
  625. omap_dmm = kzalloc(sizeof(*omap_dmm), GFP_KERNEL);
  626. if (!omap_dmm)
  627. goto fail;
  628. /* initialize lists */
  629. INIT_LIST_HEAD(&omap_dmm->alloc_head);
  630. INIT_LIST_HEAD(&omap_dmm->idle_head);
  631. init_waitqueue_head(&omap_dmm->engine_queue);
  632. if (dev->dev.of_node) {
  633. const struct of_device_id *match;
  634. match = of_match_node(dmm_of_match, dev->dev.of_node);
  635. if (!match) {
  636. dev_err(&dev->dev, "failed to find matching device node\n");
  637. ret = -ENODEV;
  638. goto fail;
  639. }
  640. omap_dmm->plat_data = match->data;
  641. }
  642. /* lookup hwmod data - base address and irq */
  643. mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  644. if (!mem) {
  645. dev_err(&dev->dev, "failed to get base address resource\n");
  646. goto fail;
  647. }
  648. omap_dmm->phys_base = mem->start;
  649. omap_dmm->base = ioremap(mem->start, SZ_2K);
  650. if (!omap_dmm->base) {
  651. dev_err(&dev->dev, "failed to get dmm base address\n");
  652. goto fail;
  653. }
  654. omap_dmm->irq = platform_get_irq(dev, 0);
  655. if (omap_dmm->irq < 0)
  656. goto fail;
  657. omap_dmm->dev = &dev->dev;
  658. if (of_machine_is_compatible("ti,dra7")) {
  659. /*
  660. * DRA7 Errata i878 says that MPU should not be used to access
  661. * RAM and DMM at the same time. As it's not possible to prevent
  662. * MPU accessing RAM, we need to access DMM via a proxy.
  663. */
  664. if (!dmm_workaround_init(omap_dmm)) {
  665. omap_dmm->dmm_workaround = true;
  666. dev_info(&dev->dev,
  667. "workaround for errata i878 in use\n");
  668. } else {
  669. dev_warn(&dev->dev,
  670. "failed to initialize work-around for i878\n");
  671. }
  672. }
  673. hwinfo = dmm_read(omap_dmm, DMM_PAT_HWINFO);
  674. omap_dmm->num_engines = (hwinfo >> 24) & 0x1F;
  675. omap_dmm->num_lut = (hwinfo >> 16) & 0x1F;
  676. omap_dmm->container_width = 256;
  677. omap_dmm->container_height = 128;
  678. atomic_set(&omap_dmm->engine_counter, omap_dmm->num_engines);
  679. /* read out actual LUT width and height */
  680. pat_geom = dmm_read(omap_dmm, DMM_PAT_GEOMETRY);
  681. omap_dmm->lut_width = ((pat_geom >> 16) & 0xF) << 5;
  682. omap_dmm->lut_height = ((pat_geom >> 24) & 0xF) << 5;
  683. /* increment LUT by one if on OMAP5 */
  684. /* LUT has twice the height, and is split into a separate container */
  685. if (omap_dmm->lut_height != omap_dmm->container_height)
  686. omap_dmm->num_lut++;
  687. /* initialize DMM registers */
  688. dmm_write(omap_dmm, 0x88888888, DMM_PAT_VIEW__0);
  689. dmm_write(omap_dmm, 0x88888888, DMM_PAT_VIEW__1);
  690. dmm_write(omap_dmm, 0x80808080, DMM_PAT_VIEW_MAP__0);
  691. dmm_write(omap_dmm, 0x80000000, DMM_PAT_VIEW_MAP_BASE);
  692. dmm_write(omap_dmm, 0x88888888, DMM_TILER_OR__0);
  693. dmm_write(omap_dmm, 0x88888888, DMM_TILER_OR__1);
  694. omap_dmm->dummy_page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  695. if (!omap_dmm->dummy_page) {
  696. dev_err(&dev->dev, "could not allocate dummy page\n");
  697. ret = -ENOMEM;
  698. goto fail;
  699. }
  700. /* set dma mask for device */
  701. ret = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
  702. if (ret)
  703. goto fail;
  704. omap_dmm->dummy_pa = page_to_phys(omap_dmm->dummy_page);
  705. /* alloc refill memory */
  706. omap_dmm->refill_va = dma_alloc_wc(&dev->dev,
  707. REFILL_BUFFER_SIZE * omap_dmm->num_engines,
  708. &omap_dmm->refill_pa, GFP_KERNEL);
  709. if (!omap_dmm->refill_va) {
  710. dev_err(&dev->dev, "could not allocate refill memory\n");
  711. ret = -ENOMEM;
  712. goto fail;
  713. }
  714. /* alloc engines */
  715. omap_dmm->engines = kcalloc(omap_dmm->num_engines,
  716. sizeof(*omap_dmm->engines), GFP_KERNEL);
  717. if (!omap_dmm->engines) {
  718. ret = -ENOMEM;
  719. goto fail;
  720. }
  721. for (i = 0; i < omap_dmm->num_engines; i++) {
  722. omap_dmm->engines[i].id = i;
  723. omap_dmm->engines[i].dmm = omap_dmm;
  724. omap_dmm->engines[i].refill_va = omap_dmm->refill_va +
  725. (REFILL_BUFFER_SIZE * i);
  726. omap_dmm->engines[i].refill_pa = omap_dmm->refill_pa +
  727. (REFILL_BUFFER_SIZE * i);
  728. init_completion(&omap_dmm->engines[i].compl);
  729. list_add(&omap_dmm->engines[i].idle_node, &omap_dmm->idle_head);
  730. }
  731. omap_dmm->tcm = kcalloc(omap_dmm->num_lut, sizeof(*omap_dmm->tcm),
  732. GFP_KERNEL);
  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_new = 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");