exynos_drm_ipp.c 49 KB

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