exynos_drm_ipp.c 48 KB

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