exynos_drm_ipp.c 48 KB

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