exynos_drm_g2d.c 37 KB

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