exynos_drm_g2d.c 40 KB

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