exynos_drm_ipp.c 42 KB

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