exynos_drm_ipp.c 49 KB

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