exynos_drm_g2d.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  3. * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundationr
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/of.h>
  20. #include <drm/drmP.h>
  21. #include <drm/exynos_drm.h>
  22. #include "exynos_drm_drv.h"
  23. #include "exynos_drm_g2d.h"
  24. #include "exynos_drm_gem.h"
  25. #include "exynos_drm_iommu.h"
  26. #define G2D_HW_MAJOR_VER 4
  27. #define G2D_HW_MINOR_VER 1
  28. /* vaild register range set from user: 0x0104 ~ 0x0880 */
  29. #define G2D_VALID_START 0x0104
  30. #define G2D_VALID_END 0x0880
  31. /* general registers */
  32. #define G2D_SOFT_RESET 0x0000
  33. #define G2D_INTEN 0x0004
  34. #define G2D_INTC_PEND 0x000C
  35. #define G2D_DMA_SFR_BASE_ADDR 0x0080
  36. #define G2D_DMA_COMMAND 0x0084
  37. #define G2D_DMA_STATUS 0x008C
  38. #define G2D_DMA_HOLD_CMD 0x0090
  39. /* command registers */
  40. #define G2D_BITBLT_START 0x0100
  41. /* registers for base address */
  42. #define G2D_SRC_BASE_ADDR 0x0304
  43. #define G2D_SRC_STRIDE 0x0308
  44. #define G2D_SRC_COLOR_MODE 0x030C
  45. #define G2D_SRC_LEFT_TOP 0x0310
  46. #define G2D_SRC_RIGHT_BOTTOM 0x0314
  47. #define G2D_SRC_PLANE2_BASE_ADDR 0x0318
  48. #define G2D_DST_BASE_ADDR 0x0404
  49. #define G2D_DST_STRIDE 0x0408
  50. #define G2D_DST_COLOR_MODE 0x040C
  51. #define G2D_DST_LEFT_TOP 0x0410
  52. #define G2D_DST_RIGHT_BOTTOM 0x0414
  53. #define G2D_DST_PLANE2_BASE_ADDR 0x0418
  54. #define G2D_PAT_BASE_ADDR 0x0500
  55. #define G2D_MSK_BASE_ADDR 0x0520
  56. /* G2D_SOFT_RESET */
  57. #define G2D_SFRCLEAR (1 << 1)
  58. #define G2D_R (1 << 0)
  59. /* G2D_INTEN */
  60. #define G2D_INTEN_ACF (1 << 3)
  61. #define G2D_INTEN_UCF (1 << 2)
  62. #define G2D_INTEN_GCF (1 << 1)
  63. #define G2D_INTEN_SCF (1 << 0)
  64. /* G2D_INTC_PEND */
  65. #define G2D_INTP_ACMD_FIN (1 << 3)
  66. #define G2D_INTP_UCMD_FIN (1 << 2)
  67. #define G2D_INTP_GCMD_FIN (1 << 1)
  68. #define G2D_INTP_SCMD_FIN (1 << 0)
  69. /* G2D_DMA_COMMAND */
  70. #define G2D_DMA_HALT (1 << 2)
  71. #define G2D_DMA_CONTINUE (1 << 1)
  72. #define G2D_DMA_START (1 << 0)
  73. /* G2D_DMA_STATUS */
  74. #define G2D_DMA_LIST_DONE_COUNT (0xFF << 17)
  75. #define G2D_DMA_BITBLT_DONE_COUNT (0xFFFF << 1)
  76. #define G2D_DMA_DONE (1 << 0)
  77. #define G2D_DMA_LIST_DONE_COUNT_OFFSET 17
  78. /* G2D_DMA_HOLD_CMD */
  79. #define G2D_USER_HOLD (1 << 2)
  80. #define G2D_LIST_HOLD (1 << 1)
  81. #define G2D_BITBLT_HOLD (1 << 0)
  82. /* G2D_BITBLT_START */
  83. #define G2D_START_CASESEL (1 << 2)
  84. #define G2D_START_NHOLT (1 << 1)
  85. #define G2D_START_BITBLT (1 << 0)
  86. /* buffer color format */
  87. #define G2D_FMT_XRGB8888 0
  88. #define G2D_FMT_ARGB8888 1
  89. #define G2D_FMT_RGB565 2
  90. #define G2D_FMT_XRGB1555 3
  91. #define G2D_FMT_ARGB1555 4
  92. #define G2D_FMT_XRGB4444 5
  93. #define G2D_FMT_ARGB4444 6
  94. #define G2D_FMT_PACKED_RGB888 7
  95. #define G2D_FMT_A8 11
  96. #define G2D_FMT_L8 12
  97. /* buffer valid length */
  98. #define G2D_LEN_MIN 1
  99. #define G2D_LEN_MAX 8000
  100. #define G2D_CMDLIST_SIZE (PAGE_SIZE / 4)
  101. #define G2D_CMDLIST_NUM 64
  102. #define G2D_CMDLIST_POOL_SIZE (G2D_CMDLIST_SIZE * G2D_CMDLIST_NUM)
  103. #define G2D_CMDLIST_DATA_NUM (G2D_CMDLIST_SIZE / sizeof(u32) - 2)
  104. /* maximum buffer pool size of userptr is 64MB as default */
  105. #define MAX_POOL (64 * 1024 * 1024)
  106. enum {
  107. BUF_TYPE_GEM = 1,
  108. BUF_TYPE_USERPTR,
  109. };
  110. enum g2d_reg_type {
  111. REG_TYPE_NONE = -1,
  112. REG_TYPE_SRC,
  113. REG_TYPE_SRC_PLANE2,
  114. REG_TYPE_DST,
  115. REG_TYPE_DST_PLANE2,
  116. REG_TYPE_PAT,
  117. REG_TYPE_MSK,
  118. MAX_REG_TYPE_NR
  119. };
  120. enum g2d_flag_bits {
  121. /*
  122. * If set, suspends the runqueue worker after the currently
  123. * processed node is finished.
  124. */
  125. G2D_BIT_SUSPEND_RUNQUEUE,
  126. /*
  127. * If set, indicates that the engine is currently busy.
  128. */
  129. G2D_BIT_ENGINE_BUSY,
  130. };
  131. /* cmdlist data structure */
  132. struct g2d_cmdlist {
  133. u32 head;
  134. unsigned long data[G2D_CMDLIST_DATA_NUM];
  135. u32 last; /* last data offset */
  136. };
  137. /*
  138. * A structure of buffer description
  139. *
  140. * @format: color format
  141. * @stride: buffer stride/pitch in bytes
  142. * @left_x: the x coordinates of left top corner
  143. * @top_y: the y coordinates of left top corner
  144. * @right_x: the x coordinates of right bottom corner
  145. * @bottom_y: the y coordinates of right bottom corner
  146. *
  147. */
  148. struct g2d_buf_desc {
  149. unsigned int format;
  150. unsigned int stride;
  151. unsigned int left_x;
  152. unsigned int top_y;
  153. unsigned int right_x;
  154. unsigned int bottom_y;
  155. };
  156. /*
  157. * A structure of buffer information
  158. *
  159. * @map_nr: manages the number of mapped buffers
  160. * @reg_types: stores regitster type in the order of requested command
  161. * @handles: stores buffer handle in its reg_type position
  162. * @types: stores buffer type in its reg_type position
  163. * @descs: stores buffer description in its reg_type position
  164. *
  165. */
  166. struct g2d_buf_info {
  167. unsigned int map_nr;
  168. enum g2d_reg_type reg_types[MAX_REG_TYPE_NR];
  169. unsigned long handles[MAX_REG_TYPE_NR];
  170. unsigned int types[MAX_REG_TYPE_NR];
  171. struct g2d_buf_desc descs[MAX_REG_TYPE_NR];
  172. };
  173. struct drm_exynos_pending_g2d_event {
  174. struct drm_pending_event base;
  175. struct drm_exynos_g2d_event event;
  176. };
  177. struct g2d_cmdlist_userptr {
  178. struct list_head list;
  179. dma_addr_t dma_addr;
  180. unsigned long userptr;
  181. unsigned long size;
  182. struct frame_vector *vec;
  183. struct sg_table *sgt;
  184. atomic_t refcount;
  185. bool in_pool;
  186. bool out_of_list;
  187. };
  188. struct g2d_cmdlist_node {
  189. struct list_head list;
  190. struct g2d_cmdlist *cmdlist;
  191. dma_addr_t dma_addr;
  192. struct g2d_buf_info buf_info;
  193. struct drm_exynos_pending_g2d_event *event;
  194. };
  195. struct g2d_runqueue_node {
  196. struct list_head list;
  197. struct list_head run_cmdlist;
  198. struct list_head event_list;
  199. struct drm_file *filp;
  200. pid_t pid;
  201. struct completion complete;
  202. int async;
  203. };
  204. struct g2d_data {
  205. struct device *dev;
  206. struct clk *gate_clk;
  207. void __iomem *regs;
  208. int irq;
  209. struct workqueue_struct *g2d_workq;
  210. struct work_struct runqueue_work;
  211. struct exynos_drm_subdrv subdrv;
  212. unsigned long flags;
  213. /* cmdlist */
  214. struct g2d_cmdlist_node *cmdlist_node;
  215. struct list_head free_cmdlist;
  216. struct mutex cmdlist_mutex;
  217. dma_addr_t cmdlist_pool;
  218. void *cmdlist_pool_virt;
  219. unsigned long cmdlist_dma_attrs;
  220. /* runqueue*/
  221. struct g2d_runqueue_node *runqueue_node;
  222. struct list_head runqueue;
  223. struct mutex runqueue_mutex;
  224. struct kmem_cache *runqueue_slab;
  225. unsigned long current_pool;
  226. unsigned long max_pool;
  227. };
  228. static inline void g2d_hw_reset(struct g2d_data *g2d)
  229. {
  230. writel(G2D_R | G2D_SFRCLEAR, g2d->regs + G2D_SOFT_RESET);
  231. clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
  232. }
  233. static int g2d_init_cmdlist(struct g2d_data *g2d)
  234. {
  235. struct device *dev = g2d->dev;
  236. struct g2d_cmdlist_node *node = g2d->cmdlist_node;
  237. struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
  238. int nr;
  239. int ret;
  240. struct g2d_buf_info *buf_info;
  241. g2d->cmdlist_dma_attrs = DMA_ATTR_WRITE_COMBINE;
  242. g2d->cmdlist_pool_virt = dma_alloc_attrs(to_dma_dev(subdrv->drm_dev),
  243. G2D_CMDLIST_POOL_SIZE,
  244. &g2d->cmdlist_pool, GFP_KERNEL,
  245. g2d->cmdlist_dma_attrs);
  246. if (!g2d->cmdlist_pool_virt) {
  247. dev_err(dev, "failed to allocate dma memory\n");
  248. return -ENOMEM;
  249. }
  250. node = kcalloc(G2D_CMDLIST_NUM, sizeof(*node), GFP_KERNEL);
  251. if (!node) {
  252. dev_err(dev, "failed to allocate memory\n");
  253. ret = -ENOMEM;
  254. goto err;
  255. }
  256. for (nr = 0; nr < G2D_CMDLIST_NUM; nr++) {
  257. unsigned int i;
  258. node[nr].cmdlist =
  259. g2d->cmdlist_pool_virt + nr * G2D_CMDLIST_SIZE;
  260. node[nr].dma_addr =
  261. g2d->cmdlist_pool + nr * G2D_CMDLIST_SIZE;
  262. buf_info = &node[nr].buf_info;
  263. for (i = 0; i < MAX_REG_TYPE_NR; i++)
  264. buf_info->reg_types[i] = REG_TYPE_NONE;
  265. list_add_tail(&node[nr].list, &g2d->free_cmdlist);
  266. }
  267. return 0;
  268. err:
  269. dma_free_attrs(to_dma_dev(subdrv->drm_dev), G2D_CMDLIST_POOL_SIZE,
  270. g2d->cmdlist_pool_virt,
  271. g2d->cmdlist_pool, g2d->cmdlist_dma_attrs);
  272. return ret;
  273. }
  274. static void g2d_fini_cmdlist(struct g2d_data *g2d)
  275. {
  276. struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
  277. kfree(g2d->cmdlist_node);
  278. if (g2d->cmdlist_pool_virt && g2d->cmdlist_pool) {
  279. dma_free_attrs(to_dma_dev(subdrv->drm_dev),
  280. G2D_CMDLIST_POOL_SIZE,
  281. g2d->cmdlist_pool_virt,
  282. g2d->cmdlist_pool, g2d->cmdlist_dma_attrs);
  283. }
  284. }
  285. static struct g2d_cmdlist_node *g2d_get_cmdlist(struct g2d_data *g2d)
  286. {
  287. struct device *dev = g2d->dev;
  288. struct g2d_cmdlist_node *node;
  289. mutex_lock(&g2d->cmdlist_mutex);
  290. if (list_empty(&g2d->free_cmdlist)) {
  291. dev_err(dev, "there is no free cmdlist\n");
  292. mutex_unlock(&g2d->cmdlist_mutex);
  293. return NULL;
  294. }
  295. node = list_first_entry(&g2d->free_cmdlist, struct g2d_cmdlist_node,
  296. list);
  297. list_del_init(&node->list);
  298. mutex_unlock(&g2d->cmdlist_mutex);
  299. return node;
  300. }
  301. static void g2d_put_cmdlist(struct g2d_data *g2d, struct g2d_cmdlist_node *node)
  302. {
  303. mutex_lock(&g2d->cmdlist_mutex);
  304. list_move_tail(&node->list, &g2d->free_cmdlist);
  305. mutex_unlock(&g2d->cmdlist_mutex);
  306. }
  307. static void g2d_add_cmdlist_to_inuse(struct exynos_drm_g2d_private *g2d_priv,
  308. struct g2d_cmdlist_node *node)
  309. {
  310. struct g2d_cmdlist_node *lnode;
  311. if (list_empty(&g2d_priv->inuse_cmdlist))
  312. goto add_to_list;
  313. /* this links to base address of new cmdlist */
  314. lnode = list_entry(g2d_priv->inuse_cmdlist.prev,
  315. struct g2d_cmdlist_node, list);
  316. lnode->cmdlist->data[lnode->cmdlist->last] = node->dma_addr;
  317. add_to_list:
  318. list_add_tail(&node->list, &g2d_priv->inuse_cmdlist);
  319. if (node->event)
  320. list_add_tail(&node->event->base.link, &g2d_priv->event_list);
  321. }
  322. static void g2d_userptr_put_dma_addr(struct drm_device *drm_dev,
  323. unsigned long obj,
  324. bool force)
  325. {
  326. struct g2d_cmdlist_userptr *g2d_userptr =
  327. (struct g2d_cmdlist_userptr *)obj;
  328. struct page **pages;
  329. if (!obj)
  330. return;
  331. if (force)
  332. goto out;
  333. atomic_dec(&g2d_userptr->refcount);
  334. if (atomic_read(&g2d_userptr->refcount) > 0)
  335. return;
  336. if (g2d_userptr->in_pool)
  337. return;
  338. out:
  339. dma_unmap_sg(to_dma_dev(drm_dev), g2d_userptr->sgt->sgl,
  340. g2d_userptr->sgt->nents, DMA_BIDIRECTIONAL);
  341. pages = frame_vector_pages(g2d_userptr->vec);
  342. if (!IS_ERR(pages)) {
  343. int i;
  344. for (i = 0; i < frame_vector_count(g2d_userptr->vec); i++)
  345. set_page_dirty_lock(pages[i]);
  346. }
  347. put_vaddr_frames(g2d_userptr->vec);
  348. frame_vector_destroy(g2d_userptr->vec);
  349. if (!g2d_userptr->out_of_list)
  350. list_del_init(&g2d_userptr->list);
  351. sg_free_table(g2d_userptr->sgt);
  352. kfree(g2d_userptr->sgt);
  353. kfree(g2d_userptr);
  354. }
  355. static dma_addr_t *g2d_userptr_get_dma_addr(struct drm_device *drm_dev,
  356. unsigned long userptr,
  357. unsigned long size,
  358. struct drm_file *filp,
  359. unsigned long *obj)
  360. {
  361. struct drm_exynos_file_private *file_priv = filp->driver_priv;
  362. struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
  363. struct g2d_cmdlist_userptr *g2d_userptr;
  364. struct g2d_data *g2d;
  365. struct sg_table *sgt;
  366. unsigned long start, end;
  367. unsigned int npages, offset;
  368. int ret;
  369. if (!size) {
  370. DRM_ERROR("invalid userptr size.\n");
  371. return ERR_PTR(-EINVAL);
  372. }
  373. g2d = dev_get_drvdata(g2d_priv->dev);
  374. /* check if userptr already exists in userptr_list. */
  375. list_for_each_entry(g2d_userptr, &g2d_priv->userptr_list, list) {
  376. if (g2d_userptr->userptr == userptr) {
  377. /*
  378. * also check size because there could be same address
  379. * and different size.
  380. */
  381. if (g2d_userptr->size == size) {
  382. atomic_inc(&g2d_userptr->refcount);
  383. *obj = (unsigned long)g2d_userptr;
  384. return &g2d_userptr->dma_addr;
  385. }
  386. /*
  387. * at this moment, maybe g2d dma is accessing this
  388. * g2d_userptr memory region so just remove this
  389. * g2d_userptr object from userptr_list not to be
  390. * referred again and also except it the userptr
  391. * pool to be released after the dma access completion.
  392. */
  393. g2d_userptr->out_of_list = true;
  394. g2d_userptr->in_pool = false;
  395. list_del_init(&g2d_userptr->list);
  396. break;
  397. }
  398. }
  399. g2d_userptr = kzalloc(sizeof(*g2d_userptr), GFP_KERNEL);
  400. if (!g2d_userptr)
  401. return ERR_PTR(-ENOMEM);
  402. atomic_set(&g2d_userptr->refcount, 1);
  403. g2d_userptr->size = size;
  404. start = userptr & PAGE_MASK;
  405. offset = userptr & ~PAGE_MASK;
  406. end = PAGE_ALIGN(userptr + size);
  407. npages = (end - start) >> PAGE_SHIFT;
  408. g2d_userptr->vec = frame_vector_create(npages);
  409. if (!g2d_userptr->vec) {
  410. ret = -ENOMEM;
  411. goto err_free;
  412. }
  413. ret = get_vaddr_frames(start, npages, FOLL_FORCE | FOLL_WRITE,
  414. g2d_userptr->vec);
  415. if (ret != npages) {
  416. DRM_ERROR("failed to get user pages from userptr.\n");
  417. if (ret < 0)
  418. goto err_destroy_framevec;
  419. ret = -EFAULT;
  420. goto err_put_framevec;
  421. }
  422. if (frame_vector_to_pages(g2d_userptr->vec) < 0) {
  423. ret = -EFAULT;
  424. goto err_put_framevec;
  425. }
  426. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  427. if (!sgt) {
  428. ret = -ENOMEM;
  429. goto err_put_framevec;
  430. }
  431. ret = sg_alloc_table_from_pages(sgt,
  432. frame_vector_pages(g2d_userptr->vec),
  433. npages, offset, size, GFP_KERNEL);
  434. if (ret < 0) {
  435. DRM_ERROR("failed to get sgt from pages.\n");
  436. goto err_free_sgt;
  437. }
  438. g2d_userptr->sgt = sgt;
  439. if (!dma_map_sg(to_dma_dev(drm_dev), sgt->sgl, sgt->nents,
  440. DMA_BIDIRECTIONAL)) {
  441. DRM_ERROR("failed to map sgt with dma region.\n");
  442. ret = -ENOMEM;
  443. goto err_sg_free_table;
  444. }
  445. g2d_userptr->dma_addr = sgt->sgl[0].dma_address;
  446. g2d_userptr->userptr = userptr;
  447. list_add_tail(&g2d_userptr->list, &g2d_priv->userptr_list);
  448. if (g2d->current_pool + (npages << PAGE_SHIFT) < g2d->max_pool) {
  449. g2d->current_pool += npages << PAGE_SHIFT;
  450. g2d_userptr->in_pool = true;
  451. }
  452. *obj = (unsigned long)g2d_userptr;
  453. return &g2d_userptr->dma_addr;
  454. err_sg_free_table:
  455. sg_free_table(sgt);
  456. err_free_sgt:
  457. kfree(sgt);
  458. err_put_framevec:
  459. put_vaddr_frames(g2d_userptr->vec);
  460. err_destroy_framevec:
  461. frame_vector_destroy(g2d_userptr->vec);
  462. err_free:
  463. kfree(g2d_userptr);
  464. return ERR_PTR(ret);
  465. }
  466. static void g2d_userptr_free_all(struct drm_device *drm_dev,
  467. struct g2d_data *g2d,
  468. struct drm_file *filp)
  469. {
  470. struct drm_exynos_file_private *file_priv = filp->driver_priv;
  471. struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
  472. struct g2d_cmdlist_userptr *g2d_userptr, *n;
  473. list_for_each_entry_safe(g2d_userptr, n, &g2d_priv->userptr_list, list)
  474. if (g2d_userptr->in_pool)
  475. g2d_userptr_put_dma_addr(drm_dev,
  476. (unsigned long)g2d_userptr,
  477. true);
  478. g2d->current_pool = 0;
  479. }
  480. static enum g2d_reg_type g2d_get_reg_type(int reg_offset)
  481. {
  482. enum g2d_reg_type reg_type;
  483. switch (reg_offset) {
  484. case G2D_SRC_BASE_ADDR:
  485. case G2D_SRC_STRIDE:
  486. case G2D_SRC_COLOR_MODE:
  487. case G2D_SRC_LEFT_TOP:
  488. case G2D_SRC_RIGHT_BOTTOM:
  489. reg_type = REG_TYPE_SRC;
  490. break;
  491. case G2D_SRC_PLANE2_BASE_ADDR:
  492. reg_type = REG_TYPE_SRC_PLANE2;
  493. break;
  494. case G2D_DST_BASE_ADDR:
  495. case G2D_DST_STRIDE:
  496. case G2D_DST_COLOR_MODE:
  497. case G2D_DST_LEFT_TOP:
  498. case G2D_DST_RIGHT_BOTTOM:
  499. reg_type = REG_TYPE_DST;
  500. break;
  501. case G2D_DST_PLANE2_BASE_ADDR:
  502. reg_type = REG_TYPE_DST_PLANE2;
  503. break;
  504. case G2D_PAT_BASE_ADDR:
  505. reg_type = REG_TYPE_PAT;
  506. break;
  507. case G2D_MSK_BASE_ADDR:
  508. reg_type = REG_TYPE_MSK;
  509. break;
  510. default:
  511. reg_type = REG_TYPE_NONE;
  512. DRM_ERROR("Unknown register offset![%d]\n", reg_offset);
  513. break;
  514. }
  515. return reg_type;
  516. }
  517. static unsigned long g2d_get_buf_bpp(unsigned int format)
  518. {
  519. unsigned long bpp;
  520. switch (format) {
  521. case G2D_FMT_XRGB8888:
  522. case G2D_FMT_ARGB8888:
  523. bpp = 4;
  524. break;
  525. case G2D_FMT_RGB565:
  526. case G2D_FMT_XRGB1555:
  527. case G2D_FMT_ARGB1555:
  528. case G2D_FMT_XRGB4444:
  529. case G2D_FMT_ARGB4444:
  530. bpp = 2;
  531. break;
  532. case G2D_FMT_PACKED_RGB888:
  533. bpp = 3;
  534. break;
  535. default:
  536. bpp = 1;
  537. break;
  538. }
  539. return bpp;
  540. }
  541. static bool g2d_check_buf_desc_is_valid(struct g2d_buf_desc *buf_desc,
  542. enum g2d_reg_type reg_type,
  543. unsigned long size)
  544. {
  545. int width, height;
  546. unsigned long bpp, last_pos;
  547. /*
  548. * check source and destination buffers only.
  549. * so the others are always valid.
  550. */
  551. if (reg_type != REG_TYPE_SRC && reg_type != REG_TYPE_DST)
  552. return true;
  553. /* This check also makes sure that right_x > left_x. */
  554. width = (int)buf_desc->right_x - (int)buf_desc->left_x;
  555. if (width < G2D_LEN_MIN || width > G2D_LEN_MAX) {
  556. DRM_ERROR("width[%d] is out of range!\n", width);
  557. return false;
  558. }
  559. /* This check also makes sure that bottom_y > top_y. */
  560. height = (int)buf_desc->bottom_y - (int)buf_desc->top_y;
  561. if (height < G2D_LEN_MIN || height > G2D_LEN_MAX) {
  562. DRM_ERROR("height[%d] is out of range!\n", height);
  563. return false;
  564. }
  565. bpp = g2d_get_buf_bpp(buf_desc->format);
  566. /* Compute the position of the last byte that the engine accesses. */
  567. last_pos = ((unsigned long)buf_desc->bottom_y - 1) *
  568. (unsigned long)buf_desc->stride +
  569. (unsigned long)buf_desc->right_x * bpp - 1;
  570. /*
  571. * Since right_x > left_x and bottom_y > top_y we already know
  572. * that the first_pos < last_pos (first_pos being the position
  573. * of the first byte the engine accesses), it just remains to
  574. * check if last_pos is smaller then the buffer size.
  575. */
  576. if (last_pos >= size) {
  577. DRM_ERROR("last engine access position [%lu] "
  578. "is out of range [%lu]!\n", last_pos, size);
  579. return false;
  580. }
  581. return true;
  582. }
  583. static int g2d_map_cmdlist_gem(struct g2d_data *g2d,
  584. struct g2d_cmdlist_node *node,
  585. struct drm_device *drm_dev,
  586. struct drm_file *file)
  587. {
  588. struct g2d_cmdlist *cmdlist = node->cmdlist;
  589. struct g2d_buf_info *buf_info = &node->buf_info;
  590. int offset;
  591. int ret;
  592. int i;
  593. for (i = 0; i < buf_info->map_nr; i++) {
  594. struct g2d_buf_desc *buf_desc;
  595. enum g2d_reg_type reg_type;
  596. int reg_pos;
  597. unsigned long handle;
  598. dma_addr_t *addr;
  599. reg_pos = cmdlist->last - 2 * (i + 1);
  600. offset = cmdlist->data[reg_pos];
  601. handle = cmdlist->data[reg_pos + 1];
  602. reg_type = g2d_get_reg_type(offset);
  603. if (reg_type == REG_TYPE_NONE) {
  604. ret = -EFAULT;
  605. goto err;
  606. }
  607. buf_desc = &buf_info->descs[reg_type];
  608. if (buf_info->types[reg_type] == BUF_TYPE_GEM) {
  609. unsigned long size;
  610. size = exynos_drm_gem_get_size(drm_dev, handle, file);
  611. if (!size) {
  612. ret = -EFAULT;
  613. goto err;
  614. }
  615. if (!g2d_check_buf_desc_is_valid(buf_desc, reg_type,
  616. size)) {
  617. ret = -EFAULT;
  618. goto err;
  619. }
  620. addr = exynos_drm_gem_get_dma_addr(drm_dev, handle,
  621. file);
  622. if (IS_ERR(addr)) {
  623. ret = -EFAULT;
  624. goto err;
  625. }
  626. } else {
  627. struct drm_exynos_g2d_userptr g2d_userptr;
  628. if (copy_from_user(&g2d_userptr, (void __user *)handle,
  629. sizeof(struct drm_exynos_g2d_userptr))) {
  630. ret = -EFAULT;
  631. goto err;
  632. }
  633. if (!g2d_check_buf_desc_is_valid(buf_desc, reg_type,
  634. g2d_userptr.size)) {
  635. ret = -EFAULT;
  636. goto err;
  637. }
  638. addr = g2d_userptr_get_dma_addr(drm_dev,
  639. g2d_userptr.userptr,
  640. g2d_userptr.size,
  641. file,
  642. &handle);
  643. if (IS_ERR(addr)) {
  644. ret = -EFAULT;
  645. goto err;
  646. }
  647. }
  648. cmdlist->data[reg_pos + 1] = *addr;
  649. buf_info->reg_types[i] = reg_type;
  650. buf_info->handles[reg_type] = handle;
  651. }
  652. return 0;
  653. err:
  654. buf_info->map_nr = i;
  655. return ret;
  656. }
  657. static void g2d_unmap_cmdlist_gem(struct g2d_data *g2d,
  658. struct g2d_cmdlist_node *node,
  659. struct drm_file *filp)
  660. {
  661. struct exynos_drm_subdrv *subdrv = &g2d->subdrv;
  662. struct g2d_buf_info *buf_info = &node->buf_info;
  663. int i;
  664. for (i = 0; i < buf_info->map_nr; i++) {
  665. struct g2d_buf_desc *buf_desc;
  666. enum g2d_reg_type reg_type;
  667. unsigned long handle;
  668. reg_type = buf_info->reg_types[i];
  669. buf_desc = &buf_info->descs[reg_type];
  670. handle = buf_info->handles[reg_type];
  671. if (buf_info->types[reg_type] == BUF_TYPE_GEM)
  672. exynos_drm_gem_put_dma_addr(subdrv->drm_dev, handle,
  673. filp);
  674. else
  675. g2d_userptr_put_dma_addr(subdrv->drm_dev, handle,
  676. false);
  677. buf_info->reg_types[i] = REG_TYPE_NONE;
  678. buf_info->handles[reg_type] = 0;
  679. buf_info->types[reg_type] = 0;
  680. memset(buf_desc, 0x00, sizeof(*buf_desc));
  681. }
  682. buf_info->map_nr = 0;
  683. }
  684. static void g2d_dma_start(struct g2d_data *g2d,
  685. struct g2d_runqueue_node *runqueue_node)
  686. {
  687. struct g2d_cmdlist_node *node =
  688. list_first_entry(&runqueue_node->run_cmdlist,
  689. struct g2d_cmdlist_node, list);
  690. set_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
  691. writel_relaxed(node->dma_addr, g2d->regs + G2D_DMA_SFR_BASE_ADDR);
  692. writel_relaxed(G2D_DMA_START, g2d->regs + G2D_DMA_COMMAND);
  693. }
  694. static struct g2d_runqueue_node *g2d_get_runqueue_node(struct g2d_data *g2d)
  695. {
  696. struct g2d_runqueue_node *runqueue_node;
  697. if (list_empty(&g2d->runqueue))
  698. return NULL;
  699. runqueue_node = list_first_entry(&g2d->runqueue,
  700. struct g2d_runqueue_node, list);
  701. list_del_init(&runqueue_node->list);
  702. return runqueue_node;
  703. }
  704. static void g2d_free_runqueue_node(struct g2d_data *g2d,
  705. struct g2d_runqueue_node *runqueue_node)
  706. {
  707. struct g2d_cmdlist_node *node;
  708. mutex_lock(&g2d->cmdlist_mutex);
  709. /*
  710. * commands in run_cmdlist have been completed so unmap all gem
  711. * objects in each command node so that they are unreferenced.
  712. */
  713. list_for_each_entry(node, &runqueue_node->run_cmdlist, list)
  714. g2d_unmap_cmdlist_gem(g2d, node, runqueue_node->filp);
  715. list_splice_tail_init(&runqueue_node->run_cmdlist, &g2d->free_cmdlist);
  716. mutex_unlock(&g2d->cmdlist_mutex);
  717. kmem_cache_free(g2d->runqueue_slab, runqueue_node);
  718. }
  719. /**
  720. * g2d_remove_runqueue_nodes - remove items from the list of runqueue nodes
  721. * @g2d: G2D state object
  722. * @file: if not zero, only remove items with this DRM file
  723. *
  724. * Has to be called under runqueue lock.
  725. */
  726. static void g2d_remove_runqueue_nodes(struct g2d_data *g2d, struct drm_file* file)
  727. {
  728. struct g2d_runqueue_node *node, *n;
  729. if (list_empty(&g2d->runqueue))
  730. return;
  731. list_for_each_entry_safe(node, n, &g2d->runqueue, list) {
  732. if (file && node->filp != file)
  733. continue;
  734. list_del_init(&node->list);
  735. g2d_free_runqueue_node(g2d, node);
  736. }
  737. }
  738. static void g2d_runqueue_worker(struct work_struct *work)
  739. {
  740. struct g2d_data *g2d = container_of(work, struct g2d_data,
  741. runqueue_work);
  742. struct g2d_runqueue_node *runqueue_node;
  743. /*
  744. * The engine is busy and the completion of the current node is going
  745. * to poke the runqueue worker, so nothing to do here.
  746. */
  747. if (test_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags))
  748. return;
  749. mutex_lock(&g2d->runqueue_mutex);
  750. runqueue_node = g2d->runqueue_node;
  751. g2d->runqueue_node = NULL;
  752. if (runqueue_node) {
  753. pm_runtime_mark_last_busy(g2d->dev);
  754. pm_runtime_put_autosuspend(g2d->dev);
  755. complete(&runqueue_node->complete);
  756. if (runqueue_node->async)
  757. g2d_free_runqueue_node(g2d, runqueue_node);
  758. }
  759. if (!test_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags)) {
  760. g2d->runqueue_node = g2d_get_runqueue_node(g2d);
  761. if (g2d->runqueue_node) {
  762. pm_runtime_get_sync(g2d->dev);
  763. g2d_dma_start(g2d, g2d->runqueue_node);
  764. }
  765. }
  766. mutex_unlock(&g2d->runqueue_mutex);
  767. }
  768. static void g2d_finish_event(struct g2d_data *g2d, u32 cmdlist_no)
  769. {
  770. struct drm_device *drm_dev = g2d->subdrv.drm_dev;
  771. struct g2d_runqueue_node *runqueue_node = g2d->runqueue_node;
  772. struct drm_exynos_pending_g2d_event *e;
  773. struct timeval now;
  774. if (list_empty(&runqueue_node->event_list))
  775. return;
  776. e = list_first_entry(&runqueue_node->event_list,
  777. struct drm_exynos_pending_g2d_event, base.link);
  778. do_gettimeofday(&now);
  779. e->event.tv_sec = now.tv_sec;
  780. e->event.tv_usec = now.tv_usec;
  781. e->event.cmdlist_no = cmdlist_no;
  782. drm_send_event(drm_dev, &e->base);
  783. }
  784. static irqreturn_t g2d_irq_handler(int irq, void *dev_id)
  785. {
  786. struct g2d_data *g2d = dev_id;
  787. u32 pending;
  788. pending = readl_relaxed(g2d->regs + G2D_INTC_PEND);
  789. if (pending)
  790. writel_relaxed(pending, g2d->regs + G2D_INTC_PEND);
  791. if (pending & G2D_INTP_GCMD_FIN) {
  792. u32 cmdlist_no = readl_relaxed(g2d->regs + G2D_DMA_STATUS);
  793. cmdlist_no = (cmdlist_no & G2D_DMA_LIST_DONE_COUNT) >>
  794. G2D_DMA_LIST_DONE_COUNT_OFFSET;
  795. g2d_finish_event(g2d, cmdlist_no);
  796. writel_relaxed(0, g2d->regs + G2D_DMA_HOLD_CMD);
  797. if (!(pending & G2D_INTP_ACMD_FIN)) {
  798. writel_relaxed(G2D_DMA_CONTINUE,
  799. g2d->regs + G2D_DMA_COMMAND);
  800. }
  801. }
  802. if (pending & G2D_INTP_ACMD_FIN) {
  803. clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
  804. queue_work(g2d->g2d_workq, &g2d->runqueue_work);
  805. }
  806. return IRQ_HANDLED;
  807. }
  808. /**
  809. * g2d_wait_finish - wait for the G2D engine to finish the current runqueue node
  810. * @g2d: G2D state object
  811. * @file: if not zero, only wait if the current runqueue node belongs
  812. * to the DRM file
  813. *
  814. * Should the engine not become idle after a 100ms timeout, a hardware
  815. * reset is issued.
  816. */
  817. static void g2d_wait_finish(struct g2d_data *g2d, struct drm_file *file)
  818. {
  819. struct device *dev = g2d->dev;
  820. struct g2d_runqueue_node *runqueue_node = NULL;
  821. unsigned int tries = 10;
  822. mutex_lock(&g2d->runqueue_mutex);
  823. /* If no node is currently processed, we have nothing to do. */
  824. if (!g2d->runqueue_node)
  825. goto out;
  826. runqueue_node = g2d->runqueue_node;
  827. /* Check if the currently processed item belongs to us. */
  828. if (file && runqueue_node->filp != file)
  829. goto out;
  830. mutex_unlock(&g2d->runqueue_mutex);
  831. /* Wait for the G2D engine to finish. */
  832. while (tries-- && (g2d->runqueue_node == runqueue_node))
  833. mdelay(10);
  834. mutex_lock(&g2d->runqueue_mutex);
  835. if (g2d->runqueue_node != runqueue_node)
  836. goto out;
  837. dev_err(dev, "wait timed out, resetting engine...\n");
  838. g2d_hw_reset(g2d);
  839. /*
  840. * After the hardware reset of the engine we are going to loose
  841. * the IRQ which triggers the PM runtime put().
  842. * So do this manually here.
  843. */
  844. pm_runtime_mark_last_busy(dev);
  845. pm_runtime_put_autosuspend(dev);
  846. complete(&runqueue_node->complete);
  847. if (runqueue_node->async)
  848. g2d_free_runqueue_node(g2d, runqueue_node);
  849. out:
  850. mutex_unlock(&g2d->runqueue_mutex);
  851. }
  852. static int g2d_check_reg_offset(struct device *dev,
  853. struct g2d_cmdlist_node *node,
  854. int nr, bool for_addr)
  855. {
  856. struct g2d_cmdlist *cmdlist = node->cmdlist;
  857. int reg_offset;
  858. int index;
  859. int i;
  860. for (i = 0; i < nr; i++) {
  861. struct g2d_buf_info *buf_info = &node->buf_info;
  862. struct g2d_buf_desc *buf_desc;
  863. enum g2d_reg_type reg_type;
  864. unsigned long value;
  865. index = cmdlist->last - 2 * (i + 1);
  866. reg_offset = cmdlist->data[index] & ~0xfffff000;
  867. if (reg_offset < G2D_VALID_START || reg_offset > G2D_VALID_END)
  868. goto err;
  869. if (reg_offset % 4)
  870. goto err;
  871. switch (reg_offset) {
  872. case G2D_SRC_BASE_ADDR:
  873. case G2D_SRC_PLANE2_BASE_ADDR:
  874. case G2D_DST_BASE_ADDR:
  875. case G2D_DST_PLANE2_BASE_ADDR:
  876. case G2D_PAT_BASE_ADDR:
  877. case G2D_MSK_BASE_ADDR:
  878. if (!for_addr)
  879. goto err;
  880. reg_type = g2d_get_reg_type(reg_offset);
  881. /* check userptr buffer type. */
  882. if ((cmdlist->data[index] & ~0x7fffffff) >> 31) {
  883. buf_info->types[reg_type] = BUF_TYPE_USERPTR;
  884. cmdlist->data[index] &= ~G2D_BUF_USERPTR;
  885. } else
  886. buf_info->types[reg_type] = BUF_TYPE_GEM;
  887. break;
  888. case G2D_SRC_STRIDE:
  889. case G2D_DST_STRIDE:
  890. if (for_addr)
  891. goto err;
  892. reg_type = g2d_get_reg_type(reg_offset);
  893. buf_desc = &buf_info->descs[reg_type];
  894. buf_desc->stride = cmdlist->data[index + 1];
  895. break;
  896. case G2D_SRC_COLOR_MODE:
  897. case G2D_DST_COLOR_MODE:
  898. if (for_addr)
  899. goto err;
  900. reg_type = g2d_get_reg_type(reg_offset);
  901. buf_desc = &buf_info->descs[reg_type];
  902. value = cmdlist->data[index + 1];
  903. buf_desc->format = value & 0xf;
  904. break;
  905. case G2D_SRC_LEFT_TOP:
  906. case G2D_DST_LEFT_TOP:
  907. if (for_addr)
  908. goto err;
  909. reg_type = g2d_get_reg_type(reg_offset);
  910. buf_desc = &buf_info->descs[reg_type];
  911. value = cmdlist->data[index + 1];
  912. buf_desc->left_x = value & 0x1fff;
  913. buf_desc->top_y = (value & 0x1fff0000) >> 16;
  914. break;
  915. case G2D_SRC_RIGHT_BOTTOM:
  916. case G2D_DST_RIGHT_BOTTOM:
  917. if (for_addr)
  918. goto err;
  919. reg_type = g2d_get_reg_type(reg_offset);
  920. buf_desc = &buf_info->descs[reg_type];
  921. value = cmdlist->data[index + 1];
  922. buf_desc->right_x = value & 0x1fff;
  923. buf_desc->bottom_y = (value & 0x1fff0000) >> 16;
  924. break;
  925. default:
  926. if (for_addr)
  927. goto err;
  928. break;
  929. }
  930. }
  931. return 0;
  932. err:
  933. dev_err(dev, "Bad register offset: 0x%lx\n", cmdlist->data[index]);
  934. return -EINVAL;
  935. }
  936. /* ioctl functions */
  937. int exynos_g2d_get_ver_ioctl(struct drm_device *drm_dev, void *data,
  938. struct drm_file *file)
  939. {
  940. struct drm_exynos_file_private *file_priv = file->driver_priv;
  941. struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
  942. struct device *dev;
  943. struct g2d_data *g2d;
  944. struct drm_exynos_g2d_get_ver *ver = data;
  945. if (!g2d_priv)
  946. return -ENODEV;
  947. dev = g2d_priv->dev;
  948. if (!dev)
  949. return -ENODEV;
  950. g2d = dev_get_drvdata(dev);
  951. if (!g2d)
  952. return -EFAULT;
  953. ver->major = G2D_HW_MAJOR_VER;
  954. ver->minor = G2D_HW_MINOR_VER;
  955. return 0;
  956. }
  957. int exynos_g2d_set_cmdlist_ioctl(struct drm_device *drm_dev, void *data,
  958. struct drm_file *file)
  959. {
  960. struct drm_exynos_file_private *file_priv = file->driver_priv;
  961. struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
  962. struct device *dev;
  963. struct g2d_data *g2d;
  964. struct drm_exynos_g2d_set_cmdlist *req = data;
  965. struct drm_exynos_g2d_cmd *cmd;
  966. struct drm_exynos_pending_g2d_event *e;
  967. struct g2d_cmdlist_node *node;
  968. struct g2d_cmdlist *cmdlist;
  969. int size;
  970. int ret;
  971. if (!g2d_priv)
  972. return -ENODEV;
  973. dev = g2d_priv->dev;
  974. if (!dev)
  975. return -ENODEV;
  976. g2d = dev_get_drvdata(dev);
  977. if (!g2d)
  978. return -EFAULT;
  979. node = g2d_get_cmdlist(g2d);
  980. if (!node)
  981. return -ENOMEM;
  982. node->event = NULL;
  983. if (req->event_type != G2D_EVENT_NOT) {
  984. e = kzalloc(sizeof(*node->event), GFP_KERNEL);
  985. if (!e) {
  986. ret = -ENOMEM;
  987. goto err;
  988. }
  989. e->event.base.type = DRM_EXYNOS_G2D_EVENT;
  990. e->event.base.length = sizeof(e->event);
  991. e->event.user_data = req->user_data;
  992. ret = drm_event_reserve_init(drm_dev, file, &e->base, &e->event.base);
  993. if (ret) {
  994. kfree(e);
  995. goto err;
  996. }
  997. node->event = e;
  998. }
  999. cmdlist = node->cmdlist;
  1000. cmdlist->last = 0;
  1001. /*
  1002. * If don't clear SFR registers, the cmdlist is affected by register
  1003. * values of previous cmdlist. G2D hw executes SFR clear command and
  1004. * a next command at the same time then the next command is ignored and
  1005. * is executed rightly from next next command, so needs a dummy command
  1006. * to next command of SFR clear command.
  1007. */
  1008. cmdlist->data[cmdlist->last++] = G2D_SOFT_RESET;
  1009. cmdlist->data[cmdlist->last++] = G2D_SFRCLEAR;
  1010. cmdlist->data[cmdlist->last++] = G2D_SRC_BASE_ADDR;
  1011. cmdlist->data[cmdlist->last++] = 0;
  1012. /*
  1013. * 'LIST_HOLD' command should be set to the DMA_HOLD_CMD_REG
  1014. * and GCF bit should be set to INTEN register if user wants
  1015. * G2D interrupt event once current command list execution is
  1016. * finished.
  1017. * Otherwise only ACF bit should be set to INTEN register so
  1018. * that one interrupt is occurred after all command lists
  1019. * have been completed.
  1020. */
  1021. if (node->event) {
  1022. cmdlist->data[cmdlist->last++] = G2D_INTEN;
  1023. cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF | G2D_INTEN_GCF;
  1024. cmdlist->data[cmdlist->last++] = G2D_DMA_HOLD_CMD;
  1025. cmdlist->data[cmdlist->last++] = G2D_LIST_HOLD;
  1026. } else {
  1027. cmdlist->data[cmdlist->last++] = G2D_INTEN;
  1028. cmdlist->data[cmdlist->last++] = G2D_INTEN_ACF;
  1029. }
  1030. /* Check size of cmdlist: last 2 is about G2D_BITBLT_START */
  1031. size = cmdlist->last + req->cmd_nr * 2 + req->cmd_buf_nr * 2 + 2;
  1032. if (size > G2D_CMDLIST_DATA_NUM) {
  1033. dev_err(dev, "cmdlist size is too big\n");
  1034. ret = -EINVAL;
  1035. goto err_free_event;
  1036. }
  1037. cmd = (struct drm_exynos_g2d_cmd *)(unsigned long)req->cmd;
  1038. if (copy_from_user(cmdlist->data + cmdlist->last,
  1039. (void __user *)cmd,
  1040. sizeof(*cmd) * req->cmd_nr)) {
  1041. ret = -EFAULT;
  1042. goto err_free_event;
  1043. }
  1044. cmdlist->last += req->cmd_nr * 2;
  1045. ret = g2d_check_reg_offset(dev, node, req->cmd_nr, false);
  1046. if (ret < 0)
  1047. goto err_free_event;
  1048. node->buf_info.map_nr = req->cmd_buf_nr;
  1049. if (req->cmd_buf_nr) {
  1050. struct drm_exynos_g2d_cmd *cmd_buf;
  1051. cmd_buf = (struct drm_exynos_g2d_cmd *)
  1052. (unsigned long)req->cmd_buf;
  1053. if (copy_from_user(cmdlist->data + cmdlist->last,
  1054. (void __user *)cmd_buf,
  1055. sizeof(*cmd_buf) * req->cmd_buf_nr)) {
  1056. ret = -EFAULT;
  1057. goto err_free_event;
  1058. }
  1059. cmdlist->last += req->cmd_buf_nr * 2;
  1060. ret = g2d_check_reg_offset(dev, node, req->cmd_buf_nr, true);
  1061. if (ret < 0)
  1062. goto err_free_event;
  1063. ret = g2d_map_cmdlist_gem(g2d, node, drm_dev, file);
  1064. if (ret < 0)
  1065. goto err_unmap;
  1066. }
  1067. cmdlist->data[cmdlist->last++] = G2D_BITBLT_START;
  1068. cmdlist->data[cmdlist->last++] = G2D_START_BITBLT;
  1069. /* head */
  1070. cmdlist->head = cmdlist->last / 2;
  1071. /* tail */
  1072. cmdlist->data[cmdlist->last] = 0;
  1073. g2d_add_cmdlist_to_inuse(g2d_priv, node);
  1074. return 0;
  1075. err_unmap:
  1076. g2d_unmap_cmdlist_gem(g2d, node, file);
  1077. err_free_event:
  1078. if (node->event)
  1079. drm_event_cancel_free(drm_dev, &node->event->base);
  1080. err:
  1081. g2d_put_cmdlist(g2d, node);
  1082. return ret;
  1083. }
  1084. int exynos_g2d_exec_ioctl(struct drm_device *drm_dev, void *data,
  1085. struct drm_file *file)
  1086. {
  1087. struct drm_exynos_file_private *file_priv = file->driver_priv;
  1088. struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
  1089. struct device *dev;
  1090. struct g2d_data *g2d;
  1091. struct drm_exynos_g2d_exec *req = data;
  1092. struct g2d_runqueue_node *runqueue_node;
  1093. struct list_head *run_cmdlist;
  1094. struct list_head *event_list;
  1095. if (!g2d_priv)
  1096. return -ENODEV;
  1097. dev = g2d_priv->dev;
  1098. if (!dev)
  1099. return -ENODEV;
  1100. g2d = dev_get_drvdata(dev);
  1101. if (!g2d)
  1102. return -EFAULT;
  1103. runqueue_node = kmem_cache_alloc(g2d->runqueue_slab, GFP_KERNEL);
  1104. if (!runqueue_node) {
  1105. dev_err(dev, "failed to allocate memory\n");
  1106. return -ENOMEM;
  1107. }
  1108. run_cmdlist = &runqueue_node->run_cmdlist;
  1109. event_list = &runqueue_node->event_list;
  1110. INIT_LIST_HEAD(run_cmdlist);
  1111. INIT_LIST_HEAD(event_list);
  1112. init_completion(&runqueue_node->complete);
  1113. runqueue_node->async = req->async;
  1114. list_splice_init(&g2d_priv->inuse_cmdlist, run_cmdlist);
  1115. list_splice_init(&g2d_priv->event_list, event_list);
  1116. if (list_empty(run_cmdlist)) {
  1117. dev_err(dev, "there is no inuse cmdlist\n");
  1118. kmem_cache_free(g2d->runqueue_slab, runqueue_node);
  1119. return -EPERM;
  1120. }
  1121. mutex_lock(&g2d->runqueue_mutex);
  1122. runqueue_node->pid = current->pid;
  1123. runqueue_node->filp = file;
  1124. list_add_tail(&runqueue_node->list, &g2d->runqueue);
  1125. mutex_unlock(&g2d->runqueue_mutex);
  1126. /* Let the runqueue know that there is work to do. */
  1127. queue_work(g2d->g2d_workq, &g2d->runqueue_work);
  1128. if (runqueue_node->async)
  1129. goto out;
  1130. wait_for_completion(&runqueue_node->complete);
  1131. g2d_free_runqueue_node(g2d, runqueue_node);
  1132. out:
  1133. return 0;
  1134. }
  1135. static int g2d_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
  1136. {
  1137. struct g2d_data *g2d;
  1138. int ret;
  1139. g2d = dev_get_drvdata(dev);
  1140. if (!g2d)
  1141. return -EFAULT;
  1142. /* allocate dma-aware cmdlist buffer. */
  1143. ret = g2d_init_cmdlist(g2d);
  1144. if (ret < 0) {
  1145. dev_err(dev, "cmdlist init failed\n");
  1146. return ret;
  1147. }
  1148. ret = drm_iommu_attach_device(drm_dev, dev);
  1149. if (ret < 0) {
  1150. dev_err(dev, "failed to enable iommu.\n");
  1151. g2d_fini_cmdlist(g2d);
  1152. }
  1153. return ret;
  1154. }
  1155. static void g2d_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  1156. {
  1157. drm_iommu_detach_device(drm_dev, dev);
  1158. }
  1159. static int g2d_open(struct drm_device *drm_dev, struct device *dev,
  1160. struct drm_file *file)
  1161. {
  1162. struct drm_exynos_file_private *file_priv = file->driver_priv;
  1163. struct exynos_drm_g2d_private *g2d_priv;
  1164. g2d_priv = kzalloc(sizeof(*g2d_priv), GFP_KERNEL);
  1165. if (!g2d_priv)
  1166. return -ENOMEM;
  1167. g2d_priv->dev = dev;
  1168. file_priv->g2d_priv = g2d_priv;
  1169. INIT_LIST_HEAD(&g2d_priv->inuse_cmdlist);
  1170. INIT_LIST_HEAD(&g2d_priv->event_list);
  1171. INIT_LIST_HEAD(&g2d_priv->userptr_list);
  1172. return 0;
  1173. }
  1174. static void g2d_close(struct drm_device *drm_dev, struct device *dev,
  1175. struct drm_file *file)
  1176. {
  1177. struct drm_exynos_file_private *file_priv = file->driver_priv;
  1178. struct exynos_drm_g2d_private *g2d_priv = file_priv->g2d_priv;
  1179. struct g2d_data *g2d;
  1180. struct g2d_cmdlist_node *node, *n;
  1181. if (!dev)
  1182. return;
  1183. g2d = dev_get_drvdata(dev);
  1184. if (!g2d)
  1185. return;
  1186. /* Remove the runqueue nodes that belong to us. */
  1187. mutex_lock(&g2d->runqueue_mutex);
  1188. g2d_remove_runqueue_nodes(g2d, file);
  1189. mutex_unlock(&g2d->runqueue_mutex);
  1190. /*
  1191. * Wait for the runqueue worker to finish its current node.
  1192. * After this the engine should no longer be accessing any
  1193. * memory belonging to us.
  1194. */
  1195. g2d_wait_finish(g2d, file);
  1196. /*
  1197. * Even after the engine is idle, there might still be stale cmdlists
  1198. * (i.e. cmdlisst which we submitted but never executed) around, with
  1199. * their corresponding GEM/userptr buffers.
  1200. * Properly unmap these buffers here.
  1201. */
  1202. mutex_lock(&g2d->cmdlist_mutex);
  1203. list_for_each_entry_safe(node, n, &g2d_priv->inuse_cmdlist, list) {
  1204. g2d_unmap_cmdlist_gem(g2d, node, file);
  1205. list_move_tail(&node->list, &g2d->free_cmdlist);
  1206. }
  1207. mutex_unlock(&g2d->cmdlist_mutex);
  1208. /* release all g2d_userptr in pool. */
  1209. g2d_userptr_free_all(drm_dev, g2d, file);
  1210. kfree(file_priv->g2d_priv);
  1211. }
  1212. static int g2d_probe(struct platform_device *pdev)
  1213. {
  1214. struct device *dev = &pdev->dev;
  1215. struct resource *res;
  1216. struct g2d_data *g2d;
  1217. struct exynos_drm_subdrv *subdrv;
  1218. int ret;
  1219. g2d = devm_kzalloc(dev, sizeof(*g2d), GFP_KERNEL);
  1220. if (!g2d)
  1221. return -ENOMEM;
  1222. g2d->runqueue_slab = kmem_cache_create("g2d_runqueue_slab",
  1223. sizeof(struct g2d_runqueue_node), 0, 0, NULL);
  1224. if (!g2d->runqueue_slab)
  1225. return -ENOMEM;
  1226. g2d->dev = dev;
  1227. g2d->g2d_workq = create_singlethread_workqueue("g2d");
  1228. if (!g2d->g2d_workq) {
  1229. dev_err(dev, "failed to create workqueue\n");
  1230. ret = -EINVAL;
  1231. goto err_destroy_slab;
  1232. }
  1233. INIT_WORK(&g2d->runqueue_work, g2d_runqueue_worker);
  1234. INIT_LIST_HEAD(&g2d->free_cmdlist);
  1235. INIT_LIST_HEAD(&g2d->runqueue);
  1236. mutex_init(&g2d->cmdlist_mutex);
  1237. mutex_init(&g2d->runqueue_mutex);
  1238. g2d->gate_clk = devm_clk_get(dev, "fimg2d");
  1239. if (IS_ERR(g2d->gate_clk)) {
  1240. dev_err(dev, "failed to get gate clock\n");
  1241. ret = PTR_ERR(g2d->gate_clk);
  1242. goto err_destroy_workqueue;
  1243. }
  1244. pm_runtime_use_autosuspend(dev);
  1245. pm_runtime_set_autosuspend_delay(dev, 2000);
  1246. pm_runtime_enable(dev);
  1247. clear_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
  1248. clear_bit(G2D_BIT_ENGINE_BUSY, &g2d->flags);
  1249. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1250. g2d->regs = devm_ioremap_resource(dev, res);
  1251. if (IS_ERR(g2d->regs)) {
  1252. ret = PTR_ERR(g2d->regs);
  1253. goto err_put_clk;
  1254. }
  1255. g2d->irq = platform_get_irq(pdev, 0);
  1256. if (g2d->irq < 0) {
  1257. dev_err(dev, "failed to get irq\n");
  1258. ret = g2d->irq;
  1259. goto err_put_clk;
  1260. }
  1261. ret = devm_request_irq(dev, g2d->irq, g2d_irq_handler, 0,
  1262. "drm_g2d", g2d);
  1263. if (ret < 0) {
  1264. dev_err(dev, "irq request failed\n");
  1265. goto err_put_clk;
  1266. }
  1267. g2d->max_pool = MAX_POOL;
  1268. platform_set_drvdata(pdev, g2d);
  1269. subdrv = &g2d->subdrv;
  1270. subdrv->dev = dev;
  1271. subdrv->probe = g2d_subdrv_probe;
  1272. subdrv->remove = g2d_subdrv_remove;
  1273. subdrv->open = g2d_open;
  1274. subdrv->close = g2d_close;
  1275. ret = exynos_drm_subdrv_register(subdrv);
  1276. if (ret < 0) {
  1277. dev_err(dev, "failed to register drm g2d device\n");
  1278. goto err_put_clk;
  1279. }
  1280. dev_info(dev, "The Exynos G2D (ver %d.%d) successfully probed.\n",
  1281. G2D_HW_MAJOR_VER, G2D_HW_MINOR_VER);
  1282. return 0;
  1283. err_put_clk:
  1284. pm_runtime_disable(dev);
  1285. err_destroy_workqueue:
  1286. destroy_workqueue(g2d->g2d_workq);
  1287. err_destroy_slab:
  1288. kmem_cache_destroy(g2d->runqueue_slab);
  1289. return ret;
  1290. }
  1291. static int g2d_remove(struct platform_device *pdev)
  1292. {
  1293. struct g2d_data *g2d = platform_get_drvdata(pdev);
  1294. /* Suspend operation and wait for engine idle. */
  1295. set_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
  1296. g2d_wait_finish(g2d, NULL);
  1297. cancel_work_sync(&g2d->runqueue_work);
  1298. exynos_drm_subdrv_unregister(&g2d->subdrv);
  1299. /* There should be no locking needed here. */
  1300. g2d_remove_runqueue_nodes(g2d, NULL);
  1301. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1302. pm_runtime_disable(&pdev->dev);
  1303. g2d_fini_cmdlist(g2d);
  1304. destroy_workqueue(g2d->g2d_workq);
  1305. kmem_cache_destroy(g2d->runqueue_slab);
  1306. return 0;
  1307. }
  1308. #ifdef CONFIG_PM_SLEEP
  1309. static int g2d_suspend(struct device *dev)
  1310. {
  1311. struct g2d_data *g2d = dev_get_drvdata(dev);
  1312. /*
  1313. * Suspend the runqueue worker operation and wait until the G2D
  1314. * engine is idle.
  1315. */
  1316. set_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
  1317. g2d_wait_finish(g2d, NULL);
  1318. flush_work(&g2d->runqueue_work);
  1319. return 0;
  1320. }
  1321. static int g2d_resume(struct device *dev)
  1322. {
  1323. struct g2d_data *g2d = dev_get_drvdata(dev);
  1324. clear_bit(G2D_BIT_SUSPEND_RUNQUEUE, &g2d->flags);
  1325. queue_work(g2d->g2d_workq, &g2d->runqueue_work);
  1326. return 0;
  1327. }
  1328. #endif
  1329. #ifdef CONFIG_PM
  1330. static int g2d_runtime_suspend(struct device *dev)
  1331. {
  1332. struct g2d_data *g2d = dev_get_drvdata(dev);
  1333. clk_disable_unprepare(g2d->gate_clk);
  1334. return 0;
  1335. }
  1336. static int g2d_runtime_resume(struct device *dev)
  1337. {
  1338. struct g2d_data *g2d = dev_get_drvdata(dev);
  1339. int ret;
  1340. ret = clk_prepare_enable(g2d->gate_clk);
  1341. if (ret < 0)
  1342. dev_warn(dev, "failed to enable clock.\n");
  1343. return ret;
  1344. }
  1345. #endif
  1346. static const struct dev_pm_ops g2d_pm_ops = {
  1347. SET_SYSTEM_SLEEP_PM_OPS(g2d_suspend, g2d_resume)
  1348. SET_RUNTIME_PM_OPS(g2d_runtime_suspend, g2d_runtime_resume, NULL)
  1349. };
  1350. static const struct of_device_id exynos_g2d_match[] = {
  1351. { .compatible = "samsung,exynos5250-g2d" },
  1352. { .compatible = "samsung,exynos4212-g2d" },
  1353. {},
  1354. };
  1355. MODULE_DEVICE_TABLE(of, exynos_g2d_match);
  1356. struct platform_driver g2d_driver = {
  1357. .probe = g2d_probe,
  1358. .remove = g2d_remove,
  1359. .driver = {
  1360. .name = "s5p-g2d",
  1361. .owner = THIS_MODULE,
  1362. .pm = &g2d_pm_ops,
  1363. .of_match_table = exynos_g2d_match,
  1364. },
  1365. };