exynos_drm_g2d.c 40 KB

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