omap_dmm_tiler.c 26 KB

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