exynos_drm_g2d.c 39 KB

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