exynos_drm_ipp.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Eunchul Kim <chulspro.kim@samsung.com>
  5. * Jinyoung Jeon <jy0.jeon@samsung.com>
  6. * Sangmin Lee <lsmin.lee@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/types.h>
  17. #include <linux/clk.h>
  18. #include <linux/pm_runtime.h>
  19. #include <drm/drmP.h>
  20. #include <drm/exynos_drm.h>
  21. #include "exynos_drm_drv.h"
  22. #include "exynos_drm_gem.h"
  23. #include "exynos_drm_ipp.h"
  24. #include "exynos_drm_iommu.h"
  25. /*
  26. * IPP stands for Image Post Processing and
  27. * supports image scaler/rotator and input/output DMA operations.
  28. * using FIMC, GSC, Rotator, so on.
  29. * IPP is integration device driver of same attribute h/w
  30. */
  31. /*
  32. * TODO
  33. * 1. expand command control id.
  34. * 2. integrate property and config.
  35. * 3. removed send_event id check routine.
  36. * 4. compare send_event id if needed.
  37. * 5. free subdrv_remove notifier callback list if needed.
  38. * 6. need to check subdrv_open about multi-open.
  39. * 7. need to power_on implement power and sysmmu ctrl.
  40. */
  41. #define get_ipp_context(dev) platform_get_drvdata(to_platform_device(dev))
  42. #define ipp_is_m2m_cmd(c) (c == IPP_CMD_M2M)
  43. /* platform device pointer for ipp device. */
  44. static struct platform_device *exynos_drm_ipp_pdev;
  45. /*
  46. * A structure of event.
  47. *
  48. * @base: base of event.
  49. * @event: ipp event.
  50. */
  51. struct drm_exynos_ipp_send_event {
  52. struct drm_pending_event base;
  53. struct drm_exynos_ipp_event event;
  54. };
  55. /*
  56. * A structure of memory node.
  57. *
  58. * @list: list head to memory queue information.
  59. * @ops_id: id of operations.
  60. * @prop_id: id of property.
  61. * @buf_id: id of buffer.
  62. * @buf_info: gem objects and dma address, size.
  63. * @filp: a pointer to drm_file.
  64. */
  65. struct drm_exynos_ipp_mem_node {
  66. struct list_head list;
  67. enum drm_exynos_ops_id ops_id;
  68. u32 prop_id;
  69. u32 buf_id;
  70. struct drm_exynos_ipp_buf_info buf_info;
  71. };
  72. /*
  73. * A structure of ipp context.
  74. *
  75. * @subdrv: prepare initialization using subdrv.
  76. * @ipp_lock: lock for synchronization of access to ipp_idr.
  77. * @prop_lock: lock for synchronization of access to prop_idr.
  78. * @ipp_idr: ipp driver idr.
  79. * @prop_idr: property idr.
  80. * @event_workq: event work queue.
  81. * @cmd_workq: command work queue.
  82. */
  83. struct ipp_context {
  84. struct exynos_drm_subdrv subdrv;
  85. struct mutex ipp_lock;
  86. struct mutex prop_lock;
  87. struct idr ipp_idr;
  88. struct idr prop_idr;
  89. struct workqueue_struct *event_workq;
  90. struct workqueue_struct *cmd_workq;
  91. };
  92. static LIST_HEAD(exynos_drm_ippdrv_list);
  93. static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
  94. static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
  95. int exynos_platform_device_ipp_register(void)
  96. {
  97. struct platform_device *pdev;
  98. if (exynos_drm_ipp_pdev)
  99. return -EEXIST;
  100. pdev = platform_device_register_simple("exynos-drm-ipp", -1, NULL, 0);
  101. if (IS_ERR(pdev))
  102. return PTR_ERR(pdev);
  103. exynos_drm_ipp_pdev = pdev;
  104. return 0;
  105. }
  106. void exynos_platform_device_ipp_unregister(void)
  107. {
  108. if (exynos_drm_ipp_pdev) {
  109. platform_device_unregister(exynos_drm_ipp_pdev);
  110. exynos_drm_ipp_pdev = NULL;
  111. }
  112. }
  113. int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
  114. {
  115. mutex_lock(&exynos_drm_ippdrv_lock);
  116. list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
  117. mutex_unlock(&exynos_drm_ippdrv_lock);
  118. return 0;
  119. }
  120. int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
  121. {
  122. mutex_lock(&exynos_drm_ippdrv_lock);
  123. list_del(&ippdrv->drv_list);
  124. mutex_unlock(&exynos_drm_ippdrv_lock);
  125. return 0;
  126. }
  127. static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj)
  128. {
  129. int ret;
  130. mutex_lock(lock);
  131. ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
  132. mutex_unlock(lock);
  133. return ret;
  134. }
  135. static void ipp_remove_id(struct idr *id_idr, struct mutex *lock, u32 id)
  136. {
  137. mutex_lock(lock);
  138. idr_remove(id_idr, id);
  139. mutex_unlock(lock);
  140. }
  141. static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
  142. {
  143. void *obj;
  144. mutex_lock(lock);
  145. obj = idr_find(id_idr, id);
  146. mutex_unlock(lock);
  147. return obj;
  148. }
  149. static int ipp_check_driver(struct exynos_drm_ippdrv *ippdrv,
  150. struct drm_exynos_ipp_property *property)
  151. {
  152. if (ippdrv->dedicated || (!ipp_is_m2m_cmd(property->cmd) &&
  153. !pm_runtime_suspended(ippdrv->dev)))
  154. return -EBUSY;
  155. if (ippdrv->check_property &&
  156. ippdrv->check_property(ippdrv->dev, property))
  157. return -EINVAL;
  158. return 0;
  159. }
  160. static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
  161. struct drm_exynos_ipp_property *property)
  162. {
  163. struct exynos_drm_ippdrv *ippdrv;
  164. u32 ipp_id = property->ipp_id;
  165. int ret;
  166. if (ipp_id) {
  167. ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock, ipp_id);
  168. if (!ippdrv) {
  169. DRM_DEBUG("ipp%d driver not found\n", ipp_id);
  170. return ERR_PTR(-ENODEV);
  171. }
  172. ret = ipp_check_driver(ippdrv, property);
  173. if (ret < 0) {
  174. DRM_DEBUG("ipp%d driver check error %d\n", ipp_id, ret);
  175. return ERR_PTR(ret);
  176. }
  177. return ippdrv;
  178. } else {
  179. list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
  180. ret = ipp_check_driver(ippdrv, property);
  181. if (ret == 0)
  182. return ippdrv;
  183. }
  184. DRM_DEBUG("cannot find driver suitable for given property.\n");
  185. }
  186. return ERR_PTR(-ENODEV);
  187. }
  188. static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
  189. {
  190. struct exynos_drm_ippdrv *ippdrv;
  191. struct drm_exynos_ipp_cmd_node *c_node;
  192. int count = 0;
  193. DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
  194. /*
  195. * This case is search ipp driver by prop_id handle.
  196. * sometimes, ipp subsystem find driver by prop_id.
  197. * e.g PAUSE state, queue buf, command control.
  198. */
  199. list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
  200. DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
  201. mutex_lock(&ippdrv->cmd_lock);
  202. list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
  203. if (c_node->property.prop_id == prop_id) {
  204. mutex_unlock(&ippdrv->cmd_lock);
  205. return ippdrv;
  206. }
  207. }
  208. mutex_unlock(&ippdrv->cmd_lock);
  209. }
  210. return ERR_PTR(-ENODEV);
  211. }
  212. int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
  213. struct drm_file *file)
  214. {
  215. struct drm_exynos_file_private *file_priv = file->driver_priv;
  216. struct device *dev = file_priv->ipp_dev;
  217. struct ipp_context *ctx = get_ipp_context(dev);
  218. struct drm_exynos_ipp_prop_list *prop_list = data;
  219. struct exynos_drm_ippdrv *ippdrv;
  220. int count = 0;
  221. if (!ctx) {
  222. DRM_ERROR("invalid context.\n");
  223. return -EINVAL;
  224. }
  225. if (!prop_list) {
  226. DRM_ERROR("invalid property parameter.\n");
  227. return -EINVAL;
  228. }
  229. DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
  230. if (!prop_list->ipp_id) {
  231. list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
  232. count++;
  233. /*
  234. * Supports ippdrv list count for user application.
  235. * First step user application getting ippdrv count.
  236. * and second step getting ippdrv capability using ipp_id.
  237. */
  238. prop_list->count = count;
  239. } else {
  240. /*
  241. * Getting ippdrv capability by ipp_id.
  242. * some device not supported wb, output interface.
  243. * so, user application detect correct ipp driver
  244. * using this ioctl.
  245. */
  246. ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
  247. prop_list->ipp_id);
  248. if (!ippdrv) {
  249. DRM_ERROR("not found ipp%d driver.\n",
  250. prop_list->ipp_id);
  251. return -ENODEV;
  252. }
  253. *prop_list = ippdrv->prop_list;
  254. }
  255. return 0;
  256. }
  257. static void ipp_print_property(struct drm_exynos_ipp_property *property,
  258. int idx)
  259. {
  260. struct drm_exynos_ipp_config *config = &property->config[idx];
  261. struct drm_exynos_pos *pos = &config->pos;
  262. struct drm_exynos_sz *sz = &config->sz;
  263. DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
  264. property->prop_id, idx ? "dst" : "src", config->fmt);
  265. DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
  266. pos->x, pos->y, pos->w, pos->h,
  267. sz->hsize, sz->vsize, config->flip, config->degree);
  268. }
  269. static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
  270. {
  271. struct drm_exynos_ipp_cmd_work *cmd_work;
  272. cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
  273. if (!cmd_work)
  274. return ERR_PTR(-ENOMEM);
  275. INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
  276. return cmd_work;
  277. }
  278. static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
  279. {
  280. struct drm_exynos_ipp_event_work *event_work;
  281. event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
  282. if (!event_work)
  283. return ERR_PTR(-ENOMEM);
  284. INIT_WORK(&event_work->work, ipp_sched_event);
  285. return event_work;
  286. }
  287. int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
  288. struct drm_file *file)
  289. {
  290. struct drm_exynos_file_private *file_priv = file->driver_priv;
  291. struct device *dev = file_priv->ipp_dev;
  292. struct ipp_context *ctx = get_ipp_context(dev);
  293. struct drm_exynos_ipp_property *property = data;
  294. struct exynos_drm_ippdrv *ippdrv;
  295. struct drm_exynos_ipp_cmd_node *c_node;
  296. u32 prop_id;
  297. int ret, i;
  298. if (!ctx) {
  299. DRM_ERROR("invalid context.\n");
  300. return -EINVAL;
  301. }
  302. if (!property) {
  303. DRM_ERROR("invalid property parameter.\n");
  304. return -EINVAL;
  305. }
  306. prop_id = property->prop_id;
  307. /*
  308. * This is log print for user application property.
  309. * user application set various property.
  310. */
  311. for_each_ipp_ops(i)
  312. ipp_print_property(property, i);
  313. /*
  314. * In case prop_id is not zero try to set existing property.
  315. */
  316. if (prop_id) {
  317. c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock, prop_id);
  318. if (!c_node || c_node->filp != file) {
  319. DRM_DEBUG_KMS("prop_id[%d] not found\n", prop_id);
  320. return -EINVAL;
  321. }
  322. if (c_node->state != IPP_STATE_STOP) {
  323. DRM_DEBUG_KMS("prop_id[%d] not stopped\n", prop_id);
  324. return -EINVAL;
  325. }
  326. c_node->property = *property;
  327. return 0;
  328. }
  329. /* find ipp driver using ipp id */
  330. ippdrv = ipp_find_driver(ctx, property);
  331. if (IS_ERR(ippdrv)) {
  332. DRM_ERROR("failed to get ipp driver.\n");
  333. return -EINVAL;
  334. }
  335. /* allocate command node */
  336. c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
  337. if (!c_node)
  338. return -ENOMEM;
  339. ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node);
  340. if (ret < 0) {
  341. DRM_ERROR("failed to create id.\n");
  342. goto err_clear;
  343. }
  344. property->prop_id = ret;
  345. DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
  346. property->prop_id, property->cmd, (int)ippdrv);
  347. /* stored property information and ippdrv in private data */
  348. c_node->property = *property;
  349. c_node->state = IPP_STATE_IDLE;
  350. c_node->filp = file;
  351. c_node->start_work = ipp_create_cmd_work();
  352. if (IS_ERR(c_node->start_work)) {
  353. DRM_ERROR("failed to create start work.\n");
  354. ret = PTR_ERR(c_node->start_work);
  355. goto err_remove_id;
  356. }
  357. c_node->stop_work = ipp_create_cmd_work();
  358. if (IS_ERR(c_node->stop_work)) {
  359. DRM_ERROR("failed to create stop work.\n");
  360. ret = PTR_ERR(c_node->stop_work);
  361. goto err_free_start;
  362. }
  363. c_node->event_work = ipp_create_event_work();
  364. if (IS_ERR(c_node->event_work)) {
  365. DRM_ERROR("failed to create event work.\n");
  366. ret = PTR_ERR(c_node->event_work);
  367. goto err_free_stop;
  368. }
  369. mutex_init(&c_node->lock);
  370. mutex_init(&c_node->mem_lock);
  371. mutex_init(&c_node->event_lock);
  372. init_completion(&c_node->start_complete);
  373. init_completion(&c_node->stop_complete);
  374. for_each_ipp_ops(i)
  375. INIT_LIST_HEAD(&c_node->mem_list[i]);
  376. INIT_LIST_HEAD(&c_node->event_list);
  377. mutex_lock(&ippdrv->cmd_lock);
  378. list_add_tail(&c_node->list, &ippdrv->cmd_list);
  379. mutex_unlock(&ippdrv->cmd_lock);
  380. /* make dedicated state without m2m */
  381. if (!ipp_is_m2m_cmd(property->cmd))
  382. ippdrv->dedicated = true;
  383. return 0;
  384. err_free_stop:
  385. kfree(c_node->stop_work);
  386. err_free_start:
  387. kfree(c_node->start_work);
  388. err_remove_id:
  389. ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
  390. err_clear:
  391. kfree(c_node);
  392. return ret;
  393. }
  394. static int ipp_validate_mem_node(struct drm_device *drm_dev,
  395. struct drm_exynos_ipp_mem_node *m_node,
  396. struct drm_exynos_ipp_cmd_node *c_node)
  397. {
  398. struct drm_exynos_ipp_config *ipp_cfg;
  399. unsigned int num_plane;
  400. unsigned long min_size, size;
  401. unsigned int bpp;
  402. int i;
  403. /* The property id should already be varified */
  404. ipp_cfg = &c_node->property.config[m_node->prop_id];
  405. num_plane = drm_format_num_planes(ipp_cfg->fmt);
  406. /**
  407. * This is a rather simplified validation of a memory node.
  408. * It basically verifies provided gem object handles
  409. * and the buffer sizes with respect to current configuration.
  410. * This is not the best that can be done
  411. * but it seems more than enough
  412. */
  413. for (i = 0; i < num_plane; ++i) {
  414. if (!m_node->buf_info.handles[i]) {
  415. DRM_ERROR("invalid handle for plane %d\n", i);
  416. return -EINVAL;
  417. }
  418. bpp = drm_format_plane_cpp(ipp_cfg->fmt, i);
  419. min_size = (ipp_cfg->sz.hsize * ipp_cfg->sz.vsize * bpp) >> 3;
  420. size = exynos_drm_gem_get_size(drm_dev,
  421. m_node->buf_info.handles[i],
  422. c_node->filp);
  423. if (min_size > size) {
  424. DRM_ERROR("invalid size for plane %d\n", i);
  425. return -EINVAL;
  426. }
  427. }
  428. return 0;
  429. }
  430. static int ipp_put_mem_node(struct drm_device *drm_dev,
  431. struct drm_exynos_ipp_cmd_node *c_node,
  432. struct drm_exynos_ipp_mem_node *m_node)
  433. {
  434. int i;
  435. DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
  436. if (!m_node) {
  437. DRM_ERROR("invalid dequeue node.\n");
  438. return -EFAULT;
  439. }
  440. DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
  441. /* put gem buffer */
  442. for_each_ipp_planar(i) {
  443. unsigned long handle = m_node->buf_info.handles[i];
  444. if (handle)
  445. exynos_drm_gem_put_dma_addr(drm_dev, handle,
  446. c_node->filp);
  447. }
  448. list_del(&m_node->list);
  449. kfree(m_node);
  450. return 0;
  451. }
  452. static struct drm_exynos_ipp_mem_node
  453. *ipp_get_mem_node(struct drm_device *drm_dev,
  454. struct drm_exynos_ipp_cmd_node *c_node,
  455. struct drm_exynos_ipp_queue_buf *qbuf)
  456. {
  457. struct drm_exynos_ipp_mem_node *m_node;
  458. struct drm_exynos_ipp_buf_info *buf_info;
  459. int i;
  460. m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
  461. if (!m_node)
  462. return ERR_PTR(-ENOMEM);
  463. buf_info = &m_node->buf_info;
  464. /* operations, buffer id */
  465. m_node->ops_id = qbuf->ops_id;
  466. m_node->prop_id = qbuf->prop_id;
  467. m_node->buf_id = qbuf->buf_id;
  468. INIT_LIST_HEAD(&m_node->list);
  469. DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
  470. DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
  471. for_each_ipp_planar(i) {
  472. DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
  473. /* get dma address by handle */
  474. if (qbuf->handle[i]) {
  475. dma_addr_t *addr;
  476. addr = exynos_drm_gem_get_dma_addr(drm_dev,
  477. qbuf->handle[i], c_node->filp);
  478. if (IS_ERR(addr)) {
  479. DRM_ERROR("failed to get addr.\n");
  480. ipp_put_mem_node(drm_dev, c_node, m_node);
  481. return ERR_PTR(-EFAULT);
  482. }
  483. buf_info->handles[i] = qbuf->handle[i];
  484. buf_info->base[i] = *addr;
  485. DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
  486. buf_info->base[i], buf_info->handles[i]);
  487. }
  488. }
  489. mutex_lock(&c_node->mem_lock);
  490. if (ipp_validate_mem_node(drm_dev, m_node, c_node)) {
  491. ipp_put_mem_node(drm_dev, c_node, m_node);
  492. mutex_unlock(&c_node->mem_lock);
  493. return ERR_PTR(-EFAULT);
  494. }
  495. list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
  496. mutex_unlock(&c_node->mem_lock);
  497. return m_node;
  498. }
  499. static void ipp_clean_mem_nodes(struct drm_device *drm_dev,
  500. struct drm_exynos_ipp_cmd_node *c_node, int ops)
  501. {
  502. struct drm_exynos_ipp_mem_node *m_node, *tm_node;
  503. struct list_head *head = &c_node->mem_list[ops];
  504. mutex_lock(&c_node->mem_lock);
  505. list_for_each_entry_safe(m_node, tm_node, head, list) {
  506. int ret;
  507. ret = ipp_put_mem_node(drm_dev, c_node, m_node);
  508. if (ret)
  509. DRM_ERROR("failed to put m_node.\n");
  510. }
  511. mutex_unlock(&c_node->mem_lock);
  512. }
  513. static void ipp_free_event(struct drm_pending_event *event)
  514. {
  515. kfree(event);
  516. }
  517. static int ipp_get_event(struct drm_device *drm_dev,
  518. struct drm_exynos_ipp_cmd_node *c_node,
  519. struct drm_exynos_ipp_queue_buf *qbuf)
  520. {
  521. struct drm_exynos_ipp_send_event *e;
  522. unsigned long flags;
  523. DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
  524. e = kzalloc(sizeof(*e), GFP_KERNEL);
  525. if (!e) {
  526. spin_lock_irqsave(&drm_dev->event_lock, flags);
  527. c_node->filp->event_space += sizeof(e->event);
  528. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  529. return -ENOMEM;
  530. }
  531. /* make event */
  532. e->event.base.type = DRM_EXYNOS_IPP_EVENT;
  533. e->event.base.length = sizeof(e->event);
  534. e->event.user_data = qbuf->user_data;
  535. e->event.prop_id = qbuf->prop_id;
  536. e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
  537. e->base.event = &e->event.base;
  538. e->base.file_priv = c_node->filp;
  539. e->base.destroy = ipp_free_event;
  540. mutex_lock(&c_node->event_lock);
  541. list_add_tail(&e->base.link, &c_node->event_list);
  542. mutex_unlock(&c_node->event_lock);
  543. return 0;
  544. }
  545. static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
  546. struct drm_exynos_ipp_queue_buf *qbuf)
  547. {
  548. struct drm_exynos_ipp_send_event *e, *te;
  549. int count = 0;
  550. mutex_lock(&c_node->event_lock);
  551. list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
  552. DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
  553. /*
  554. * qbuf == NULL condition means all event deletion.
  555. * stop operations want to delete all event list.
  556. * another case delete only same buf id.
  557. */
  558. if (!qbuf) {
  559. /* delete list */
  560. list_del(&e->base.link);
  561. kfree(e);
  562. }
  563. /* compare buffer id */
  564. if (qbuf && (qbuf->buf_id ==
  565. e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
  566. /* delete list */
  567. list_del(&e->base.link);
  568. kfree(e);
  569. goto out_unlock;
  570. }
  571. }
  572. out_unlock:
  573. mutex_unlock(&c_node->event_lock);
  574. return;
  575. }
  576. static void ipp_clean_cmd_node(struct ipp_context *ctx,
  577. struct drm_exynos_ipp_cmd_node *c_node)
  578. {
  579. int i;
  580. /* cancel works */
  581. cancel_work_sync(&c_node->start_work->work);
  582. cancel_work_sync(&c_node->stop_work->work);
  583. cancel_work_sync(&c_node->event_work->work);
  584. /* put event */
  585. ipp_put_event(c_node, NULL);
  586. for_each_ipp_ops(i)
  587. ipp_clean_mem_nodes(ctx->subdrv.drm_dev, c_node, i);
  588. /* delete list */
  589. list_del(&c_node->list);
  590. ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
  591. c_node->property.prop_id);
  592. /* destroy mutex */
  593. mutex_destroy(&c_node->lock);
  594. mutex_destroy(&c_node->mem_lock);
  595. mutex_destroy(&c_node->event_lock);
  596. /* free command node */
  597. kfree(c_node->start_work);
  598. kfree(c_node->stop_work);
  599. kfree(c_node->event_work);
  600. kfree(c_node);
  601. }
  602. static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
  603. {
  604. switch (c_node->property.cmd) {
  605. case IPP_CMD_WB:
  606. return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
  607. case IPP_CMD_OUTPUT:
  608. return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
  609. case IPP_CMD_M2M:
  610. default:
  611. return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
  612. !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
  613. }
  614. }
  615. static struct drm_exynos_ipp_mem_node
  616. *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
  617. struct drm_exynos_ipp_queue_buf *qbuf)
  618. {
  619. struct drm_exynos_ipp_mem_node *m_node;
  620. struct list_head *head;
  621. int count = 0;
  622. DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
  623. /* source/destination memory list */
  624. head = &c_node->mem_list[qbuf->ops_id];
  625. /* find memory node from memory list */
  626. list_for_each_entry(m_node, head, list) {
  627. DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
  628. /* compare buffer id */
  629. if (m_node->buf_id == qbuf->buf_id)
  630. return m_node;
  631. }
  632. return NULL;
  633. }
  634. static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
  635. struct drm_exynos_ipp_cmd_node *c_node,
  636. struct drm_exynos_ipp_mem_node *m_node)
  637. {
  638. struct exynos_drm_ipp_ops *ops = NULL;
  639. int ret = 0;
  640. DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
  641. if (!m_node) {
  642. DRM_ERROR("invalid queue node.\n");
  643. return -EFAULT;
  644. }
  645. DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
  646. /* get operations callback */
  647. ops = ippdrv->ops[m_node->ops_id];
  648. if (!ops) {
  649. DRM_ERROR("not support ops.\n");
  650. return -EFAULT;
  651. }
  652. /* set address and enable irq */
  653. if (ops->set_addr) {
  654. ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
  655. m_node->buf_id, IPP_BUF_ENQUEUE);
  656. if (ret) {
  657. DRM_ERROR("failed to set addr.\n");
  658. return ret;
  659. }
  660. }
  661. return ret;
  662. }
  663. static void ipp_handle_cmd_work(struct device *dev,
  664. struct exynos_drm_ippdrv *ippdrv,
  665. struct drm_exynos_ipp_cmd_work *cmd_work,
  666. struct drm_exynos_ipp_cmd_node *c_node)
  667. {
  668. struct ipp_context *ctx = get_ipp_context(dev);
  669. cmd_work->ippdrv = ippdrv;
  670. cmd_work->c_node = c_node;
  671. queue_work(ctx->cmd_workq, &cmd_work->work);
  672. }
  673. static int ipp_queue_buf_with_run(struct device *dev,
  674. struct drm_exynos_ipp_cmd_node *c_node,
  675. struct drm_exynos_ipp_mem_node *m_node,
  676. struct drm_exynos_ipp_queue_buf *qbuf)
  677. {
  678. struct exynos_drm_ippdrv *ippdrv;
  679. struct drm_exynos_ipp_property *property;
  680. struct exynos_drm_ipp_ops *ops;
  681. int ret;
  682. ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
  683. if (IS_ERR(ippdrv)) {
  684. DRM_ERROR("failed to get ipp driver.\n");
  685. return -EFAULT;
  686. }
  687. ops = ippdrv->ops[qbuf->ops_id];
  688. if (!ops) {
  689. DRM_ERROR("failed to get ops.\n");
  690. return -EFAULT;
  691. }
  692. property = &c_node->property;
  693. if (c_node->state != IPP_STATE_START) {
  694. DRM_DEBUG_KMS("bypass for invalid state.\n");
  695. return 0;
  696. }
  697. mutex_lock(&c_node->mem_lock);
  698. if (!ipp_check_mem_list(c_node)) {
  699. mutex_unlock(&c_node->mem_lock);
  700. DRM_DEBUG_KMS("empty memory.\n");
  701. return 0;
  702. }
  703. /*
  704. * If set destination buffer and enabled clock,
  705. * then m2m operations need start operations at queue_buf
  706. */
  707. if (ipp_is_m2m_cmd(property->cmd)) {
  708. struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
  709. cmd_work->ctrl = IPP_CTRL_PLAY;
  710. ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
  711. } else {
  712. ret = ipp_set_mem_node(ippdrv, c_node, m_node);
  713. if (ret) {
  714. mutex_unlock(&c_node->mem_lock);
  715. DRM_ERROR("failed to set m node.\n");
  716. return ret;
  717. }
  718. }
  719. mutex_unlock(&c_node->mem_lock);
  720. return 0;
  721. }
  722. static void ipp_clean_queue_buf(struct drm_device *drm_dev,
  723. struct drm_exynos_ipp_cmd_node *c_node,
  724. struct drm_exynos_ipp_queue_buf *qbuf)
  725. {
  726. struct drm_exynos_ipp_mem_node *m_node, *tm_node;
  727. /* delete list */
  728. mutex_lock(&c_node->mem_lock);
  729. list_for_each_entry_safe(m_node, tm_node,
  730. &c_node->mem_list[qbuf->ops_id], list) {
  731. if (m_node->buf_id == qbuf->buf_id &&
  732. m_node->ops_id == qbuf->ops_id)
  733. ipp_put_mem_node(drm_dev, c_node, m_node);
  734. }
  735. mutex_unlock(&c_node->mem_lock);
  736. }
  737. int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
  738. struct drm_file *file)
  739. {
  740. struct drm_exynos_file_private *file_priv = file->driver_priv;
  741. struct device *dev = file_priv->ipp_dev;
  742. struct ipp_context *ctx = get_ipp_context(dev);
  743. struct drm_exynos_ipp_queue_buf *qbuf = data;
  744. struct drm_exynos_ipp_cmd_node *c_node;
  745. struct drm_exynos_ipp_mem_node *m_node;
  746. int ret;
  747. if (!qbuf) {
  748. DRM_ERROR("invalid buf parameter.\n");
  749. return -EINVAL;
  750. }
  751. if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
  752. DRM_ERROR("invalid ops parameter.\n");
  753. return -EINVAL;
  754. }
  755. DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
  756. qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
  757. qbuf->buf_id, qbuf->buf_type);
  758. /* find command node */
  759. c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
  760. qbuf->prop_id);
  761. if (!c_node || c_node->filp != file) {
  762. DRM_ERROR("failed to get command node.\n");
  763. return -ENODEV;
  764. }
  765. /* buffer control */
  766. switch (qbuf->buf_type) {
  767. case IPP_BUF_ENQUEUE:
  768. /* get memory node */
  769. m_node = ipp_get_mem_node(drm_dev, c_node, qbuf);
  770. if (IS_ERR(m_node)) {
  771. DRM_ERROR("failed to get m_node.\n");
  772. return PTR_ERR(m_node);
  773. }
  774. /*
  775. * first step get event for destination buffer.
  776. * and second step when M2M case run with destination buffer
  777. * if needed.
  778. */
  779. if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
  780. /* get event for destination buffer */
  781. ret = ipp_get_event(drm_dev, c_node, qbuf);
  782. if (ret) {
  783. DRM_ERROR("failed to get event.\n");
  784. goto err_clean_node;
  785. }
  786. /*
  787. * M2M case run play control for streaming feature.
  788. * other case set address and waiting.
  789. */
  790. ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
  791. if (ret) {
  792. DRM_ERROR("failed to run command.\n");
  793. goto err_clean_node;
  794. }
  795. }
  796. break;
  797. case IPP_BUF_DEQUEUE:
  798. mutex_lock(&c_node->lock);
  799. /* put event for destination buffer */
  800. if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
  801. ipp_put_event(c_node, qbuf);
  802. ipp_clean_queue_buf(drm_dev, c_node, qbuf);
  803. mutex_unlock(&c_node->lock);
  804. break;
  805. default:
  806. DRM_ERROR("invalid buffer control.\n");
  807. return -EINVAL;
  808. }
  809. return 0;
  810. err_clean_node:
  811. DRM_ERROR("clean memory nodes.\n");
  812. ipp_clean_queue_buf(drm_dev, c_node, qbuf);
  813. return ret;
  814. }
  815. static bool exynos_drm_ipp_check_valid(struct device *dev,
  816. enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
  817. {
  818. if (ctrl != IPP_CTRL_PLAY) {
  819. if (pm_runtime_suspended(dev)) {
  820. DRM_ERROR("pm:runtime_suspended.\n");
  821. goto err_status;
  822. }
  823. }
  824. switch (ctrl) {
  825. case IPP_CTRL_PLAY:
  826. if (state != IPP_STATE_IDLE)
  827. goto err_status;
  828. break;
  829. case IPP_CTRL_STOP:
  830. if (state == IPP_STATE_STOP)
  831. goto err_status;
  832. break;
  833. case IPP_CTRL_PAUSE:
  834. if (state != IPP_STATE_START)
  835. goto err_status;
  836. break;
  837. case IPP_CTRL_RESUME:
  838. if (state != IPP_STATE_STOP)
  839. goto err_status;
  840. break;
  841. default:
  842. DRM_ERROR("invalid state.\n");
  843. goto err_status;
  844. }
  845. return true;
  846. err_status:
  847. DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
  848. return false;
  849. }
  850. int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
  851. struct drm_file *file)
  852. {
  853. struct drm_exynos_file_private *file_priv = file->driver_priv;
  854. struct exynos_drm_ippdrv *ippdrv = NULL;
  855. struct device *dev = file_priv->ipp_dev;
  856. struct ipp_context *ctx = get_ipp_context(dev);
  857. struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
  858. struct drm_exynos_ipp_cmd_work *cmd_work;
  859. struct drm_exynos_ipp_cmd_node *c_node;
  860. if (!ctx) {
  861. DRM_ERROR("invalid context.\n");
  862. return -EINVAL;
  863. }
  864. if (!cmd_ctrl) {
  865. DRM_ERROR("invalid control parameter.\n");
  866. return -EINVAL;
  867. }
  868. DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
  869. cmd_ctrl->ctrl, cmd_ctrl->prop_id);
  870. ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
  871. if (IS_ERR(ippdrv)) {
  872. DRM_ERROR("failed to get ipp driver.\n");
  873. return PTR_ERR(ippdrv);
  874. }
  875. c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
  876. cmd_ctrl->prop_id);
  877. if (!c_node || c_node->filp != file) {
  878. DRM_ERROR("invalid command node list.\n");
  879. return -ENODEV;
  880. }
  881. if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
  882. c_node->state)) {
  883. DRM_ERROR("invalid state.\n");
  884. return -EINVAL;
  885. }
  886. switch (cmd_ctrl->ctrl) {
  887. case IPP_CTRL_PLAY:
  888. if (pm_runtime_suspended(ippdrv->dev))
  889. pm_runtime_get_sync(ippdrv->dev);
  890. c_node->state = IPP_STATE_START;
  891. cmd_work = c_node->start_work;
  892. cmd_work->ctrl = cmd_ctrl->ctrl;
  893. ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
  894. break;
  895. case IPP_CTRL_STOP:
  896. cmd_work = c_node->stop_work;
  897. cmd_work->ctrl = cmd_ctrl->ctrl;
  898. ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
  899. if (!wait_for_completion_timeout(&c_node->stop_complete,
  900. msecs_to_jiffies(300))) {
  901. DRM_ERROR("timeout stop:prop_id[%d]\n",
  902. c_node->property.prop_id);
  903. }
  904. c_node->state = IPP_STATE_STOP;
  905. ippdrv->dedicated = false;
  906. mutex_lock(&ippdrv->cmd_lock);
  907. ipp_clean_cmd_node(ctx, c_node);
  908. if (list_empty(&ippdrv->cmd_list))
  909. pm_runtime_put_sync(ippdrv->dev);
  910. mutex_unlock(&ippdrv->cmd_lock);
  911. break;
  912. case IPP_CTRL_PAUSE:
  913. cmd_work = c_node->stop_work;
  914. cmd_work->ctrl = cmd_ctrl->ctrl;
  915. ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
  916. if (!wait_for_completion_timeout(&c_node->stop_complete,
  917. msecs_to_jiffies(200))) {
  918. DRM_ERROR("timeout stop:prop_id[%d]\n",
  919. c_node->property.prop_id);
  920. }
  921. c_node->state = IPP_STATE_STOP;
  922. break;
  923. case IPP_CTRL_RESUME:
  924. c_node->state = IPP_STATE_START;
  925. cmd_work = c_node->start_work;
  926. cmd_work->ctrl = cmd_ctrl->ctrl;
  927. ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
  928. break;
  929. default:
  930. DRM_ERROR("could not support this state currently.\n");
  931. return -EINVAL;
  932. }
  933. DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
  934. cmd_ctrl->ctrl, cmd_ctrl->prop_id);
  935. return 0;
  936. }
  937. int exynos_drm_ippnb_register(struct notifier_block *nb)
  938. {
  939. return blocking_notifier_chain_register(
  940. &exynos_drm_ippnb_list, nb);
  941. }
  942. int exynos_drm_ippnb_unregister(struct notifier_block *nb)
  943. {
  944. return blocking_notifier_chain_unregister(
  945. &exynos_drm_ippnb_list, nb);
  946. }
  947. int exynos_drm_ippnb_send_event(unsigned long val, void *v)
  948. {
  949. return blocking_notifier_call_chain(
  950. &exynos_drm_ippnb_list, val, v);
  951. }
  952. static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
  953. struct drm_exynos_ipp_property *property)
  954. {
  955. struct exynos_drm_ipp_ops *ops = NULL;
  956. bool swap = false;
  957. int ret, i;
  958. if (!property) {
  959. DRM_ERROR("invalid property parameter.\n");
  960. return -EINVAL;
  961. }
  962. DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
  963. /* reset h/w block */
  964. if (ippdrv->reset &&
  965. ippdrv->reset(ippdrv->dev)) {
  966. return -EINVAL;
  967. }
  968. /* set source,destination operations */
  969. for_each_ipp_ops(i) {
  970. struct drm_exynos_ipp_config *config =
  971. &property->config[i];
  972. ops = ippdrv->ops[i];
  973. if (!ops || !config) {
  974. DRM_ERROR("not support ops and config.\n");
  975. return -EINVAL;
  976. }
  977. /* set format */
  978. if (ops->set_fmt) {
  979. ret = ops->set_fmt(ippdrv->dev, config->fmt);
  980. if (ret)
  981. return ret;
  982. }
  983. /* set transform for rotation, flip */
  984. if (ops->set_transf) {
  985. ret = ops->set_transf(ippdrv->dev, config->degree,
  986. config->flip, &swap);
  987. if (ret)
  988. return ret;
  989. }
  990. /* set size */
  991. if (ops->set_size) {
  992. ret = ops->set_size(ippdrv->dev, swap, &config->pos,
  993. &config->sz);
  994. if (ret)
  995. return ret;
  996. }
  997. }
  998. return 0;
  999. }
  1000. static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
  1001. struct drm_exynos_ipp_cmd_node *c_node)
  1002. {
  1003. struct drm_exynos_ipp_mem_node *m_node;
  1004. struct drm_exynos_ipp_property *property = &c_node->property;
  1005. struct list_head *head;
  1006. int ret, i;
  1007. DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
  1008. /* store command info in ippdrv */
  1009. ippdrv->c_node = c_node;
  1010. mutex_lock(&c_node->mem_lock);
  1011. if (!ipp_check_mem_list(c_node)) {
  1012. DRM_DEBUG_KMS("empty memory.\n");
  1013. ret = -ENOMEM;
  1014. goto err_unlock;
  1015. }
  1016. /* set current property in ippdrv */
  1017. ret = ipp_set_property(ippdrv, property);
  1018. if (ret) {
  1019. DRM_ERROR("failed to set property.\n");
  1020. ippdrv->c_node = NULL;
  1021. goto err_unlock;
  1022. }
  1023. /* check command */
  1024. switch (property->cmd) {
  1025. case IPP_CMD_M2M:
  1026. for_each_ipp_ops(i) {
  1027. /* source/destination memory list */
  1028. head = &c_node->mem_list[i];
  1029. m_node = list_first_entry(head,
  1030. struct drm_exynos_ipp_mem_node, list);
  1031. DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
  1032. ret = ipp_set_mem_node(ippdrv, c_node, m_node);
  1033. if (ret) {
  1034. DRM_ERROR("failed to set m node.\n");
  1035. goto err_unlock;
  1036. }
  1037. }
  1038. break;
  1039. case IPP_CMD_WB:
  1040. /* destination memory list */
  1041. head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
  1042. list_for_each_entry(m_node, head, list) {
  1043. ret = ipp_set_mem_node(ippdrv, c_node, m_node);
  1044. if (ret) {
  1045. DRM_ERROR("failed to set m node.\n");
  1046. goto err_unlock;
  1047. }
  1048. }
  1049. break;
  1050. case IPP_CMD_OUTPUT:
  1051. /* source memory list */
  1052. head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
  1053. list_for_each_entry(m_node, head, list) {
  1054. ret = ipp_set_mem_node(ippdrv, c_node, m_node);
  1055. if (ret) {
  1056. DRM_ERROR("failed to set m node.\n");
  1057. goto err_unlock;
  1058. }
  1059. }
  1060. break;
  1061. default:
  1062. DRM_ERROR("invalid operations.\n");
  1063. ret = -EINVAL;
  1064. goto err_unlock;
  1065. }
  1066. mutex_unlock(&c_node->mem_lock);
  1067. DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
  1068. /* start operations */
  1069. if (ippdrv->start) {
  1070. ret = ippdrv->start(ippdrv->dev, property->cmd);
  1071. if (ret) {
  1072. DRM_ERROR("failed to start ops.\n");
  1073. ippdrv->c_node = NULL;
  1074. return ret;
  1075. }
  1076. }
  1077. return 0;
  1078. err_unlock:
  1079. mutex_unlock(&c_node->mem_lock);
  1080. ippdrv->c_node = NULL;
  1081. return ret;
  1082. }
  1083. static int ipp_stop_property(struct drm_device *drm_dev,
  1084. struct exynos_drm_ippdrv *ippdrv,
  1085. struct drm_exynos_ipp_cmd_node *c_node)
  1086. {
  1087. struct drm_exynos_ipp_property *property = &c_node->property;
  1088. int i;
  1089. DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
  1090. /* stop operations */
  1091. if (ippdrv->stop)
  1092. ippdrv->stop(ippdrv->dev, property->cmd);
  1093. /* check command */
  1094. switch (property->cmd) {
  1095. case IPP_CMD_M2M:
  1096. for_each_ipp_ops(i)
  1097. ipp_clean_mem_nodes(drm_dev, c_node, i);
  1098. break;
  1099. case IPP_CMD_WB:
  1100. ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_DST);
  1101. break;
  1102. case IPP_CMD_OUTPUT:
  1103. ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_SRC);
  1104. break;
  1105. default:
  1106. DRM_ERROR("invalid operations.\n");
  1107. return -EINVAL;
  1108. }
  1109. return 0;
  1110. }
  1111. void ipp_sched_cmd(struct work_struct *work)
  1112. {
  1113. struct drm_exynos_ipp_cmd_work *cmd_work =
  1114. container_of(work, struct drm_exynos_ipp_cmd_work, work);
  1115. struct exynos_drm_ippdrv *ippdrv;
  1116. struct drm_exynos_ipp_cmd_node *c_node;
  1117. struct drm_exynos_ipp_property *property;
  1118. int ret;
  1119. ippdrv = cmd_work->ippdrv;
  1120. if (!ippdrv) {
  1121. DRM_ERROR("invalid ippdrv list.\n");
  1122. return;
  1123. }
  1124. c_node = cmd_work->c_node;
  1125. if (!c_node) {
  1126. DRM_ERROR("invalid command node list.\n");
  1127. return;
  1128. }
  1129. mutex_lock(&c_node->lock);
  1130. property = &c_node->property;
  1131. switch (cmd_work->ctrl) {
  1132. case IPP_CTRL_PLAY:
  1133. case IPP_CTRL_RESUME:
  1134. ret = ipp_start_property(ippdrv, c_node);
  1135. if (ret) {
  1136. DRM_ERROR("failed to start property:prop_id[%d]\n",
  1137. c_node->property.prop_id);
  1138. goto err_unlock;
  1139. }
  1140. /*
  1141. * M2M case supports wait_completion of transfer.
  1142. * because M2M case supports single unit operation
  1143. * with multiple queue.
  1144. * M2M need to wait completion of data transfer.
  1145. */
  1146. if (ipp_is_m2m_cmd(property->cmd)) {
  1147. if (!wait_for_completion_timeout
  1148. (&c_node->start_complete, msecs_to_jiffies(200))) {
  1149. DRM_ERROR("timeout event:prop_id[%d]\n",
  1150. c_node->property.prop_id);
  1151. goto err_unlock;
  1152. }
  1153. }
  1154. break;
  1155. case IPP_CTRL_STOP:
  1156. case IPP_CTRL_PAUSE:
  1157. ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
  1158. c_node);
  1159. if (ret) {
  1160. DRM_ERROR("failed to stop property.\n");
  1161. goto err_unlock;
  1162. }
  1163. complete(&c_node->stop_complete);
  1164. break;
  1165. default:
  1166. DRM_ERROR("unknown control type\n");
  1167. break;
  1168. }
  1169. DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
  1170. err_unlock:
  1171. mutex_unlock(&c_node->lock);
  1172. }
  1173. static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
  1174. struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
  1175. {
  1176. struct drm_device *drm_dev = ippdrv->drm_dev;
  1177. struct drm_exynos_ipp_property *property = &c_node->property;
  1178. struct drm_exynos_ipp_mem_node *m_node;
  1179. struct drm_exynos_ipp_queue_buf qbuf;
  1180. struct drm_exynos_ipp_send_event *e;
  1181. struct list_head *head;
  1182. struct timeval now;
  1183. unsigned long flags;
  1184. u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
  1185. int ret, i;
  1186. for_each_ipp_ops(i)
  1187. DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
  1188. if (!drm_dev) {
  1189. DRM_ERROR("failed to get drm_dev.\n");
  1190. return -EINVAL;
  1191. }
  1192. if (!property) {
  1193. DRM_ERROR("failed to get property.\n");
  1194. return -EINVAL;
  1195. }
  1196. mutex_lock(&c_node->event_lock);
  1197. if (list_empty(&c_node->event_list)) {
  1198. DRM_DEBUG_KMS("event list is empty.\n");
  1199. ret = 0;
  1200. goto err_event_unlock;
  1201. }
  1202. mutex_lock(&c_node->mem_lock);
  1203. if (!ipp_check_mem_list(c_node)) {
  1204. DRM_DEBUG_KMS("empty memory.\n");
  1205. ret = 0;
  1206. goto err_mem_unlock;
  1207. }
  1208. /* check command */
  1209. switch (property->cmd) {
  1210. case IPP_CMD_M2M:
  1211. for_each_ipp_ops(i) {
  1212. /* source/destination memory list */
  1213. head = &c_node->mem_list[i];
  1214. m_node = list_first_entry(head,
  1215. struct drm_exynos_ipp_mem_node, list);
  1216. tbuf_id[i] = m_node->buf_id;
  1217. DRM_DEBUG_KMS("%s buf_id[%d]\n",
  1218. i ? "dst" : "src", tbuf_id[i]);
  1219. ret = ipp_put_mem_node(drm_dev, c_node, m_node);
  1220. if (ret)
  1221. DRM_ERROR("failed to put m_node.\n");
  1222. }
  1223. break;
  1224. case IPP_CMD_WB:
  1225. /* clear buf for finding */
  1226. memset(&qbuf, 0x0, sizeof(qbuf));
  1227. qbuf.ops_id = EXYNOS_DRM_OPS_DST;
  1228. qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
  1229. /* get memory node entry */
  1230. m_node = ipp_find_mem_node(c_node, &qbuf);
  1231. if (!m_node) {
  1232. DRM_ERROR("empty memory node.\n");
  1233. ret = -ENOMEM;
  1234. goto err_mem_unlock;
  1235. }
  1236. tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
  1237. ret = ipp_put_mem_node(drm_dev, c_node, m_node);
  1238. if (ret)
  1239. DRM_ERROR("failed to put m_node.\n");
  1240. break;
  1241. case IPP_CMD_OUTPUT:
  1242. /* source memory list */
  1243. head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
  1244. m_node = list_first_entry(head,
  1245. struct drm_exynos_ipp_mem_node, list);
  1246. tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
  1247. ret = ipp_put_mem_node(drm_dev, c_node, m_node);
  1248. if (ret)
  1249. DRM_ERROR("failed to put m_node.\n");
  1250. break;
  1251. default:
  1252. DRM_ERROR("invalid operations.\n");
  1253. ret = -EINVAL;
  1254. goto err_mem_unlock;
  1255. }
  1256. mutex_unlock(&c_node->mem_lock);
  1257. if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
  1258. DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
  1259. tbuf_id[1], buf_id[1], property->prop_id);
  1260. /*
  1261. * command node have event list of destination buffer
  1262. * If destination buffer enqueue to mem list,
  1263. * then we make event and link to event list tail.
  1264. * so, we get first event for first enqueued buffer.
  1265. */
  1266. e = list_first_entry(&c_node->event_list,
  1267. struct drm_exynos_ipp_send_event, base.link);
  1268. do_gettimeofday(&now);
  1269. DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
  1270. e->event.tv_sec = now.tv_sec;
  1271. e->event.tv_usec = now.tv_usec;
  1272. e->event.prop_id = property->prop_id;
  1273. /* set buffer id about source destination */
  1274. for_each_ipp_ops(i)
  1275. e->event.buf_id[i] = tbuf_id[i];
  1276. spin_lock_irqsave(&drm_dev->event_lock, flags);
  1277. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  1278. wake_up_interruptible(&e->base.file_priv->event_wait);
  1279. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  1280. mutex_unlock(&c_node->event_lock);
  1281. DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
  1282. property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
  1283. return 0;
  1284. err_mem_unlock:
  1285. mutex_unlock(&c_node->mem_lock);
  1286. err_event_unlock:
  1287. mutex_unlock(&c_node->event_lock);
  1288. return ret;
  1289. }
  1290. void ipp_sched_event(struct work_struct *work)
  1291. {
  1292. struct drm_exynos_ipp_event_work *event_work =
  1293. container_of(work, struct drm_exynos_ipp_event_work, work);
  1294. struct exynos_drm_ippdrv *ippdrv;
  1295. struct drm_exynos_ipp_cmd_node *c_node;
  1296. int ret;
  1297. if (!event_work) {
  1298. DRM_ERROR("failed to get event_work.\n");
  1299. return;
  1300. }
  1301. DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
  1302. ippdrv = event_work->ippdrv;
  1303. if (!ippdrv) {
  1304. DRM_ERROR("failed to get ipp driver.\n");
  1305. return;
  1306. }
  1307. c_node = ippdrv->c_node;
  1308. if (!c_node) {
  1309. DRM_ERROR("failed to get command node.\n");
  1310. return;
  1311. }
  1312. /*
  1313. * IPP supports command thread, event thread synchronization.
  1314. * If IPP close immediately from user land, then IPP make
  1315. * synchronization with command thread, so make complete event.
  1316. * or going out operations.
  1317. */
  1318. if (c_node->state != IPP_STATE_START) {
  1319. DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
  1320. c_node->state, c_node->property.prop_id);
  1321. goto err_completion;
  1322. }
  1323. ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
  1324. if (ret) {
  1325. DRM_ERROR("failed to send event.\n");
  1326. goto err_completion;
  1327. }
  1328. err_completion:
  1329. if (ipp_is_m2m_cmd(c_node->property.cmd))
  1330. complete(&c_node->start_complete);
  1331. }
  1332. static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
  1333. {
  1334. struct ipp_context *ctx = get_ipp_context(dev);
  1335. struct exynos_drm_ippdrv *ippdrv;
  1336. int ret, count = 0;
  1337. /* get ipp driver entry */
  1338. list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
  1339. ippdrv->drm_dev = drm_dev;
  1340. ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
  1341. if (ret < 0) {
  1342. DRM_ERROR("failed to create id.\n");
  1343. goto err;
  1344. }
  1345. ippdrv->prop_list.ipp_id = ret;
  1346. DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
  1347. count++, (int)ippdrv, ret);
  1348. /* store parent device for node */
  1349. ippdrv->parent_dev = dev;
  1350. /* store event work queue and handler */
  1351. ippdrv->event_workq = ctx->event_workq;
  1352. ippdrv->sched_event = ipp_sched_event;
  1353. INIT_LIST_HEAD(&ippdrv->cmd_list);
  1354. mutex_init(&ippdrv->cmd_lock);
  1355. if (is_drm_iommu_supported(drm_dev)) {
  1356. ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
  1357. if (ret) {
  1358. DRM_ERROR("failed to activate iommu\n");
  1359. goto err;
  1360. }
  1361. }
  1362. }
  1363. return 0;
  1364. err:
  1365. /* get ipp driver entry */
  1366. list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
  1367. drv_list) {
  1368. if (is_drm_iommu_supported(drm_dev))
  1369. drm_iommu_detach_device(drm_dev, ippdrv->dev);
  1370. ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
  1371. ippdrv->prop_list.ipp_id);
  1372. }
  1373. return ret;
  1374. }
  1375. static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  1376. {
  1377. struct exynos_drm_ippdrv *ippdrv, *t;
  1378. struct ipp_context *ctx = get_ipp_context(dev);
  1379. /* get ipp driver entry */
  1380. list_for_each_entry_safe(ippdrv, t, &exynos_drm_ippdrv_list, drv_list) {
  1381. if (is_drm_iommu_supported(drm_dev))
  1382. drm_iommu_detach_device(drm_dev, ippdrv->dev);
  1383. ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
  1384. ippdrv->prop_list.ipp_id);
  1385. ippdrv->drm_dev = NULL;
  1386. exynos_drm_ippdrv_unregister(ippdrv);
  1387. }
  1388. }
  1389. static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
  1390. struct drm_file *file)
  1391. {
  1392. struct drm_exynos_file_private *file_priv = file->driver_priv;
  1393. file_priv->ipp_dev = dev;
  1394. DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
  1395. return 0;
  1396. }
  1397. static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
  1398. struct drm_file *file)
  1399. {
  1400. struct exynos_drm_ippdrv *ippdrv = NULL;
  1401. struct ipp_context *ctx = get_ipp_context(dev);
  1402. struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
  1403. int count = 0;
  1404. list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
  1405. mutex_lock(&ippdrv->cmd_lock);
  1406. list_for_each_entry_safe(c_node, tc_node,
  1407. &ippdrv->cmd_list, list) {
  1408. DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
  1409. count++, (int)ippdrv);
  1410. if (c_node->filp == file) {
  1411. /*
  1412. * userland goto unnormal state. process killed.
  1413. * and close the file.
  1414. * so, IPP didn't called stop cmd ctrl.
  1415. * so, we are make stop operation in this state.
  1416. */
  1417. if (c_node->state == IPP_STATE_START) {
  1418. ipp_stop_property(drm_dev, ippdrv,
  1419. c_node);
  1420. c_node->state = IPP_STATE_STOP;
  1421. }
  1422. ippdrv->dedicated = false;
  1423. ipp_clean_cmd_node(ctx, c_node);
  1424. if (list_empty(&ippdrv->cmd_list))
  1425. pm_runtime_put_sync(ippdrv->dev);
  1426. }
  1427. }
  1428. mutex_unlock(&ippdrv->cmd_lock);
  1429. }
  1430. return;
  1431. }
  1432. static int ipp_probe(struct platform_device *pdev)
  1433. {
  1434. struct device *dev = &pdev->dev;
  1435. struct ipp_context *ctx;
  1436. struct exynos_drm_subdrv *subdrv;
  1437. int ret;
  1438. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1439. if (!ctx)
  1440. return -ENOMEM;
  1441. mutex_init(&ctx->ipp_lock);
  1442. mutex_init(&ctx->prop_lock);
  1443. idr_init(&ctx->ipp_idr);
  1444. idr_init(&ctx->prop_idr);
  1445. /*
  1446. * create single thread for ipp event
  1447. * IPP supports event thread for IPP drivers.
  1448. * IPP driver send event_work to this thread.
  1449. * and IPP event thread send event to user process.
  1450. */
  1451. ctx->event_workq = create_singlethread_workqueue("ipp_event");
  1452. if (!ctx->event_workq) {
  1453. dev_err(dev, "failed to create event workqueue\n");
  1454. return -EINVAL;
  1455. }
  1456. /*
  1457. * create single thread for ipp command
  1458. * IPP supports command thread for user process.
  1459. * user process make command node using set property ioctl.
  1460. * and make start_work and send this work to command thread.
  1461. * and then this command thread start property.
  1462. */
  1463. ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
  1464. if (!ctx->cmd_workq) {
  1465. dev_err(dev, "failed to create cmd workqueue\n");
  1466. ret = -EINVAL;
  1467. goto err_event_workq;
  1468. }
  1469. /* set sub driver informations */
  1470. subdrv = &ctx->subdrv;
  1471. subdrv->dev = dev;
  1472. subdrv->probe = ipp_subdrv_probe;
  1473. subdrv->remove = ipp_subdrv_remove;
  1474. subdrv->open = ipp_subdrv_open;
  1475. subdrv->close = ipp_subdrv_close;
  1476. platform_set_drvdata(pdev, ctx);
  1477. ret = exynos_drm_subdrv_register(subdrv);
  1478. if (ret < 0) {
  1479. DRM_ERROR("failed to register drm ipp device.\n");
  1480. goto err_cmd_workq;
  1481. }
  1482. dev_info(dev, "drm ipp registered successfully.\n");
  1483. return 0;
  1484. err_cmd_workq:
  1485. destroy_workqueue(ctx->cmd_workq);
  1486. err_event_workq:
  1487. destroy_workqueue(ctx->event_workq);
  1488. return ret;
  1489. }
  1490. static int ipp_remove(struct platform_device *pdev)
  1491. {
  1492. struct ipp_context *ctx = platform_get_drvdata(pdev);
  1493. /* unregister sub driver */
  1494. exynos_drm_subdrv_unregister(&ctx->subdrv);
  1495. /* remove,destroy ipp idr */
  1496. idr_destroy(&ctx->ipp_idr);
  1497. idr_destroy(&ctx->prop_idr);
  1498. mutex_destroy(&ctx->ipp_lock);
  1499. mutex_destroy(&ctx->prop_lock);
  1500. /* destroy command, event work queue */
  1501. destroy_workqueue(ctx->cmd_workq);
  1502. destroy_workqueue(ctx->event_workq);
  1503. return 0;
  1504. }
  1505. struct platform_driver ipp_driver = {
  1506. .probe = ipp_probe,
  1507. .remove = ipp_remove,
  1508. .driver = {
  1509. .name = "exynos-drm-ipp",
  1510. .owner = THIS_MODULE,
  1511. },
  1512. };