omap_dmm_tiler.c 26 KB

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