blob: 4b5afd37eaf97400d7a785289ccccf4ec9ec0f3d [file] [log] [blame]
Eunchul Kimcb471f142012-12-14 18:10:31 +09001/*
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>
Eunchul Kimcb471f142012-12-14 18:10:31 +090015#include <linux/platform_device.h>
16#include <linux/types.h>
17#include <linux/clk.h>
18#include <linux/pm_runtime.h>
Eunchul Kimcb471f142012-12-14 18:10:31 +090019
20#include <drm/drmP.h>
21#include <drm/exynos_drm.h>
22#include "exynos_drm_drv.h"
23#include "exynos_drm_gem.h"
24#include "exynos_drm_ipp.h"
Eunchul Kimc12e2612012-12-14 17:58:54 +090025#include "exynos_drm_iommu.h"
Eunchul Kimcb471f142012-12-14 18:10:31 +090026
27/*
Eunchul Kim6fe891f2012-12-22 17:49:26 +090028 * IPP stands for Image Post Processing and
Eunchul Kimcb471f142012-12-14 18:10:31 +090029 * 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/*
35 * TODO
36 * 1. expand command control id.
37 * 2. integrate property and config.
38 * 3. removed send_event id check routine.
39 * 4. compare send_event id if needed.
40 * 5. free subdrv_remove notifier callback list if needed.
41 * 6. need to check subdrv_open about multi-open.
42 * 7. need to power_on implement power and sysmmu ctrl.
43 */
44
45#define get_ipp_context(dev) platform_get_drvdata(to_platform_device(dev))
46#define ipp_is_m2m_cmd(c) (c == IPP_CMD_M2M)
47
Seung-Woo Kim43f41902013-04-23 14:02:53 +090048/* platform device pointer for ipp device. */
49static struct platform_device *exynos_drm_ipp_pdev;
50
Eunchul Kimcb471f142012-12-14 18:10:31 +090051/*
52 * A structure of event.
53 *
54 * @base: base of event.
55 * @event: ipp event.
56 */
57struct drm_exynos_ipp_send_event {
58 struct drm_pending_event base;
59 struct drm_exynos_ipp_event event;
60};
61
62/*
63 * A structure of memory node.
64 *
65 * @list: list head to memory queue information.
66 * @ops_id: id of operations.
67 * @prop_id: id of property.
68 * @buf_id: id of buffer.
69 * @buf_info: gem objects and dma address, size.
70 * @filp: a pointer to drm_file.
71 */
72struct drm_exynos_ipp_mem_node {
73 struct list_head list;
74 enum drm_exynos_ops_id ops_id;
75 u32 prop_id;
76 u32 buf_id;
77 struct drm_exynos_ipp_buf_info buf_info;
78 struct drm_file *filp;
79};
80
81/*
82 * A structure of ipp context.
83 *
84 * @subdrv: prepare initialization using subdrv.
85 * @ipp_lock: lock for synchronization of access to ipp_idr.
86 * @prop_lock: lock for synchronization of access to prop_idr.
87 * @ipp_idr: ipp driver idr.
88 * @prop_idr: property idr.
89 * @event_workq: event work queue.
90 * @cmd_workq: command work queue.
91 */
92struct ipp_context {
93 struct exynos_drm_subdrv subdrv;
94 struct mutex ipp_lock;
95 struct mutex prop_lock;
96 struct idr ipp_idr;
97 struct idr prop_idr;
98 struct workqueue_struct *event_workq;
99 struct workqueue_struct *cmd_workq;
100};
101
102static LIST_HEAD(exynos_drm_ippdrv_list);
103static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
104static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
105
Seung-Woo Kim43f41902013-04-23 14:02:53 +0900106int exynos_platform_device_ipp_register(void)
107{
108 struct platform_device *pdev;
109
110 if (exynos_drm_ipp_pdev)
111 return -EEXIST;
112
113 pdev = platform_device_register_simple("exynos-drm-ipp", -1, NULL, 0);
114 if (IS_ERR(pdev))
115 return PTR_ERR(pdev);
116
117 exynos_drm_ipp_pdev = pdev;
118
119 return 0;
120}
121
122void exynos_platform_device_ipp_unregister(void)
123{
124 if (exynos_drm_ipp_pdev) {
125 platform_device_unregister(exynos_drm_ipp_pdev);
126 exynos_drm_ipp_pdev = NULL;
127 }
128}
129
Eunchul Kimcb471f142012-12-14 18:10:31 +0900130int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
131{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900132 if (!ippdrv)
133 return -EINVAL;
134
135 mutex_lock(&exynos_drm_ippdrv_lock);
136 list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
137 mutex_unlock(&exynos_drm_ippdrv_lock);
138
139 return 0;
140}
141
142int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
143{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900144 if (!ippdrv)
145 return -EINVAL;
146
147 mutex_lock(&exynos_drm_ippdrv_lock);
148 list_del(&ippdrv->drv_list);
149 mutex_unlock(&exynos_drm_ippdrv_lock);
150
151 return 0;
152}
153
154static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj,
155 u32 *idp)
156{
157 int ret;
158
Eunchul Kimcb471f142012-12-14 18:10:31 +0900159 /* do the allocation under our mutexlock */
160 mutex_lock(lock);
Tejun Heo8550cb22013-02-27 17:04:09 -0800161 ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900162 mutex_unlock(lock);
Tejun Heo8550cb22013-02-27 17:04:09 -0800163 if (ret < 0)
164 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900165
Tejun Heo8550cb22013-02-27 17:04:09 -0800166 *idp = ret;
167 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900168}
169
YoungJun Cho075436b2014-05-26 10:17:19 +0200170static void ipp_remove_id(struct idr *id_idr, struct mutex *lock, u32 id)
171{
172 mutex_lock(lock);
173 idr_remove(id_idr, id);
174 mutex_unlock(lock);
175}
176
Eunchul Kimcb471f142012-12-14 18:10:31 +0900177static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
178{
179 void *obj;
180
YoungJun Chocbc4c332013-06-12 10:44:40 +0900181 DRM_DEBUG_KMS("id[%d]\n", id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900182
183 mutex_lock(lock);
184
185 /* find object using handle */
186 obj = idr_find(id_idr, id);
187 if (!obj) {
188 DRM_ERROR("failed to find object.\n");
189 mutex_unlock(lock);
190 return ERR_PTR(-ENODEV);
191 }
192
193 mutex_unlock(lock);
194
195 return obj;
196}
197
198static inline bool ipp_check_dedicated(struct exynos_drm_ippdrv *ippdrv,
199 enum drm_exynos_ipp_cmd cmd)
200{
201 /*
202 * check dedicated flag and WB, OUTPUT operation with
203 * power on state.
204 */
205 if (ippdrv->dedicated || (!ipp_is_m2m_cmd(cmd) &&
206 !pm_runtime_suspended(ippdrv->dev)))
207 return true;
208
209 return false;
210}
211
212static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
213 struct drm_exynos_ipp_property *property)
214{
215 struct exynos_drm_ippdrv *ippdrv;
216 u32 ipp_id = property->ipp_id;
217
YoungJun Chocbc4c332013-06-12 10:44:40 +0900218 DRM_DEBUG_KMS("ipp_id[%d]\n", ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900219
220 if (ipp_id) {
221 /* find ipp driver using idr */
222 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
223 ipp_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530224 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900225 DRM_ERROR("not found ipp%d driver.\n", ipp_id);
226 return ippdrv;
227 }
228
229 /*
230 * WB, OUTPUT opertion not supported multi-operation.
231 * so, make dedicated state at set property ioctl.
232 * when ipp driver finished operations, clear dedicated flags.
233 */
234 if (ipp_check_dedicated(ippdrv, property->cmd)) {
235 DRM_ERROR("already used choose device.\n");
236 return ERR_PTR(-EBUSY);
237 }
238
239 /*
240 * This is necessary to find correct device in ipp drivers.
241 * ipp drivers have different abilities,
242 * so need to check property.
243 */
244 if (ippdrv->check_property &&
245 ippdrv->check_property(ippdrv->dev, property)) {
246 DRM_ERROR("not support property.\n");
247 return ERR_PTR(-EINVAL);
248 }
249
250 return ippdrv;
251 } else {
252 /*
253 * This case is search all ipp driver for finding.
254 * user application don't set ipp_id in this case,
255 * so ipp subsystem search correct driver in driver list.
256 */
257 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
258 if (ipp_check_dedicated(ippdrv, property->cmd)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900259 DRM_DEBUG_KMS("used device.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900260 continue;
261 }
262
263 if (ippdrv->check_property &&
264 ippdrv->check_property(ippdrv->dev, property)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900265 DRM_DEBUG_KMS("not support property.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900266 continue;
267 }
268
269 return ippdrv;
270 }
271
272 DRM_ERROR("not support ipp driver operations.\n");
273 }
274
275 return ERR_PTR(-ENODEV);
276}
277
278static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
279{
280 struct exynos_drm_ippdrv *ippdrv;
281 struct drm_exynos_ipp_cmd_node *c_node;
282 int count = 0;
283
YoungJun Chocbc4c332013-06-12 10:44:40 +0900284 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900285
Eunchul Kimcb471f142012-12-14 18:10:31 +0900286 /*
287 * This case is search ipp driver by prop_id handle.
288 * sometimes, ipp subsystem find driver by prop_id.
289 * e.g PAUSE state, queue buf, command contro.
290 */
291 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900292 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900293
YoungJun Cho7f5af052014-05-26 10:17:18 +0200294 mutex_lock(&ippdrv->cmd_lock);
295 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
296 if (c_node->property.prop_id == prop_id) {
297 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200298 return ippdrv;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200299 }
300 }
301 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900302 }
303
304 return ERR_PTR(-ENODEV);
305}
306
307int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
308 struct drm_file *file)
309{
310 struct drm_exynos_file_private *file_priv = file->driver_priv;
311 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
312 struct device *dev = priv->dev;
313 struct ipp_context *ctx = get_ipp_context(dev);
314 struct drm_exynos_ipp_prop_list *prop_list = data;
315 struct exynos_drm_ippdrv *ippdrv;
316 int count = 0;
317
Eunchul Kimcb471f142012-12-14 18:10:31 +0900318 if (!ctx) {
319 DRM_ERROR("invalid context.\n");
320 return -EINVAL;
321 }
322
323 if (!prop_list) {
324 DRM_ERROR("invalid property parameter.\n");
325 return -EINVAL;
326 }
327
YoungJun Chocbc4c332013-06-12 10:44:40 +0900328 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900329
330 if (!prop_list->ipp_id) {
331 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
332 count++;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200333
Eunchul Kimcb471f142012-12-14 18:10:31 +0900334 /*
335 * Supports ippdrv list count for user application.
336 * First step user application getting ippdrv count.
337 * and second step getting ippdrv capability using ipp_id.
338 */
339 prop_list->count = count;
340 } else {
341 /*
342 * Getting ippdrv capability by ipp_id.
Masanari Iidac6b78bc2013-10-24 16:02:57 +0900343 * some device not supported wb, output interface.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900344 * so, user application detect correct ipp driver
345 * using this ioctl.
346 */
347 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
348 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800349 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900350 DRM_ERROR("not found ipp%d driver.\n",
351 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800352 return PTR_ERR(ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900353 }
354
Andrzej Hajda31646052014-05-19 12:54:05 +0200355 *prop_list = ippdrv->prop_list;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900356 }
357
358 return 0;
359}
360
361static void ipp_print_property(struct drm_exynos_ipp_property *property,
362 int idx)
363{
364 struct drm_exynos_ipp_config *config = &property->config[idx];
365 struct drm_exynos_pos *pos = &config->pos;
366 struct drm_exynos_sz *sz = &config->sz;
367
YoungJun Chocbc4c332013-06-12 10:44:40 +0900368 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
369 property->prop_id, idx ? "dst" : "src", config->fmt);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900370
YoungJun Chocbc4c332013-06-12 10:44:40 +0900371 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
372 pos->x, pos->y, pos->w, pos->h,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900373 sz->hsize, sz->vsize, config->flip, config->degree);
374}
375
376static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
377{
378 struct exynos_drm_ippdrv *ippdrv;
379 struct drm_exynos_ipp_cmd_node *c_node;
380 u32 prop_id = property->prop_id;
381
YoungJun Chocbc4c332013-06-12 10:44:40 +0900382 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900383
384 ippdrv = ipp_find_drv_by_handle(prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530385 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900386 DRM_ERROR("failed to get ipp driver.\n");
387 return -EINVAL;
388 }
389
390 /*
391 * Find command node using command list in ippdrv.
392 * when we find this command no using prop_id.
393 * return property information set in this command node.
394 */
YoungJun Cho7f5af052014-05-26 10:17:18 +0200395 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900396 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
397 if ((c_node->property.prop_id == prop_id) &&
398 (c_node->state == IPP_STATE_STOP)) {
YoungJun Cho7f5af052014-05-26 10:17:18 +0200399 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900400 DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
401 property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900402
403 c_node->property = *property;
404 return 0;
405 }
406 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200407 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900408
409 DRM_ERROR("failed to search property.\n");
410
411 return -EINVAL;
412}
413
414static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
415{
416 struct drm_exynos_ipp_cmd_work *cmd_work;
417
Eunchul Kimcb471f142012-12-14 18:10:31 +0900418 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900419 if (!cmd_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900420 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900421
422 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
423
424 return cmd_work;
425}
426
427static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
428{
429 struct drm_exynos_ipp_event_work *event_work;
430
Eunchul Kimcb471f142012-12-14 18:10:31 +0900431 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900432 if (!event_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900433 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900434
435 INIT_WORK((struct work_struct *)event_work, ipp_sched_event);
436
437 return event_work;
438}
439
440int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
441 struct drm_file *file)
442{
443 struct drm_exynos_file_private *file_priv = file->driver_priv;
444 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
445 struct device *dev = priv->dev;
446 struct ipp_context *ctx = get_ipp_context(dev);
447 struct drm_exynos_ipp_property *property = data;
448 struct exynos_drm_ippdrv *ippdrv;
449 struct drm_exynos_ipp_cmd_node *c_node;
450 int ret, i;
451
Eunchul Kimcb471f142012-12-14 18:10:31 +0900452 if (!ctx) {
453 DRM_ERROR("invalid context.\n");
454 return -EINVAL;
455 }
456
457 if (!property) {
458 DRM_ERROR("invalid property parameter.\n");
459 return -EINVAL;
460 }
461
462 /*
463 * This is log print for user application property.
464 * user application set various property.
465 */
466 for_each_ipp_ops(i)
467 ipp_print_property(property, i);
468
469 /*
470 * set property ioctl generated new prop_id.
471 * but in this case already asigned prop_id using old set property.
472 * e.g PAUSE state. this case supports find current prop_id and use it
473 * instead of allocation.
474 */
475 if (property->prop_id) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900476 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900477 return ipp_find_and_set_property(property);
478 }
479
480 /* find ipp driver using ipp id */
481 ippdrv = ipp_find_driver(ctx, property);
Sachin Kamatf0250452013-04-29 12:27:06 +0530482 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900483 DRM_ERROR("failed to get ipp driver.\n");
484 return -EINVAL;
485 }
486
487 /* allocate command node */
488 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900489 if (!c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900490 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900491
492 /* create property id */
493 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node,
494 &property->prop_id);
495 if (ret) {
496 DRM_ERROR("failed to create id.\n");
497 goto err_clear;
498 }
499
YoungJun Chocbc4c332013-06-12 10:44:40 +0900500 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
501 property->prop_id, property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900502
503 /* stored property information and ippdrv in private data */
504 c_node->priv = priv;
505 c_node->property = *property;
506 c_node->state = IPP_STATE_IDLE;
507
508 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530509 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900510 DRM_ERROR("failed to create start work.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +0200511 goto err_remove_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900512 }
513
514 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530515 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900516 DRM_ERROR("failed to create stop work.\n");
517 goto err_free_start;
518 }
519
520 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530521 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900522 DRM_ERROR("failed to create event work.\n");
523 goto err_free_stop;
524 }
525
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200526 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900527 mutex_init(&c_node->mem_lock);
528 mutex_init(&c_node->event_lock);
529
530 init_completion(&c_node->start_complete);
531 init_completion(&c_node->stop_complete);
532
533 for_each_ipp_ops(i)
534 INIT_LIST_HEAD(&c_node->mem_list[i]);
535
536 INIT_LIST_HEAD(&c_node->event_list);
537 list_splice_init(&priv->event_list, &c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200538 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900539 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200540 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900541
542 /* make dedicated state without m2m */
543 if (!ipp_is_m2m_cmd(property->cmd))
544 ippdrv->dedicated = true;
545
546 return 0;
547
548err_free_stop:
549 kfree(c_node->stop_work);
550err_free_start:
551 kfree(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200552err_remove_id:
553 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900554err_clear:
555 kfree(c_node);
556 return ret;
557}
558
YoungJun Cho075436b2014-05-26 10:17:19 +0200559static void ipp_clean_cmd_node(struct ipp_context *ctx,
560 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900561{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900562 /* delete list */
563 list_del(&c_node->list);
564
YoungJun Cho075436b2014-05-26 10:17:19 +0200565 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
566 c_node->property.prop_id);
567
Eunchul Kimcb471f142012-12-14 18:10:31 +0900568 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200569 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900570 mutex_destroy(&c_node->mem_lock);
571 mutex_destroy(&c_node->event_lock);
572
573 /* free command node */
574 kfree(c_node->start_work);
575 kfree(c_node->stop_work);
576 kfree(c_node->event_work);
577 kfree(c_node);
578}
579
580static int ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
581{
582 struct drm_exynos_ipp_property *property = &c_node->property;
583 struct drm_exynos_ipp_mem_node *m_node;
584 struct list_head *head;
585 int ret, i, count[EXYNOS_DRM_OPS_MAX] = { 0, };
586
Eunchul Kimcb471f142012-12-14 18:10:31 +0900587 for_each_ipp_ops(i) {
588 /* source/destination memory list */
589 head = &c_node->mem_list[i];
590
Eunchul Kimcb471f142012-12-14 18:10:31 +0900591 /* find memory node entry */
592 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900593 DRM_DEBUG_KMS("%s,count[%d]m_node[0x%x]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900594 i ? "dst" : "src", count[i], (int)m_node);
595 count[i]++;
596 }
597 }
598
YoungJun Chocbc4c332013-06-12 10:44:40 +0900599 DRM_DEBUG_KMS("min[%d]max[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900600 min(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]),
601 max(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]));
602
603 /*
604 * M2M operations should be need paired memory address.
605 * so, need to check minimum count about src, dst.
606 * other case not use paired memory, so use maximum count
607 */
608 if (ipp_is_m2m_cmd(property->cmd))
609 ret = min(count[EXYNOS_DRM_OPS_SRC],
610 count[EXYNOS_DRM_OPS_DST]);
611 else
612 ret = max(count[EXYNOS_DRM_OPS_SRC],
613 count[EXYNOS_DRM_OPS_DST]);
614
Eunchul Kimcb471f142012-12-14 18:10:31 +0900615 return ret;
616}
617
618static struct drm_exynos_ipp_mem_node
619 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
620 struct drm_exynos_ipp_queue_buf *qbuf)
621{
622 struct drm_exynos_ipp_mem_node *m_node;
623 struct list_head *head;
624 int count = 0;
625
YoungJun Chocbc4c332013-06-12 10:44:40 +0900626 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900627
628 /* source/destination memory list */
629 head = &c_node->mem_list[qbuf->ops_id];
630
631 /* find memory node from memory list */
632 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900633 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900634
635 /* compare buffer id */
636 if (m_node->buf_id == qbuf->buf_id)
637 return m_node;
638 }
639
640 return NULL;
641}
642
643static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
644 struct drm_exynos_ipp_cmd_node *c_node,
645 struct drm_exynos_ipp_mem_node *m_node)
646{
647 struct exynos_drm_ipp_ops *ops = NULL;
648 int ret = 0;
649
YoungJun Chocbc4c332013-06-12 10:44:40 +0900650 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900651
652 if (!m_node) {
653 DRM_ERROR("invalid queue node.\n");
654 return -EFAULT;
655 }
656
YoungJun Chocbc4c332013-06-12 10:44:40 +0900657 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900658
659 /* get operations callback */
660 ops = ippdrv->ops[m_node->ops_id];
661 if (!ops) {
662 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200663 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900664 }
665
666 /* set address and enable irq */
667 if (ops->set_addr) {
668 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
669 m_node->buf_id, IPP_BUF_ENQUEUE);
670 if (ret) {
671 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200672 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900673 }
674 }
675
Eunchul Kimcb471f142012-12-14 18:10:31 +0900676 return ret;
677}
678
679static struct drm_exynos_ipp_mem_node
680 *ipp_get_mem_node(struct drm_device *drm_dev,
681 struct drm_file *file,
682 struct drm_exynos_ipp_cmd_node *c_node,
683 struct drm_exynos_ipp_queue_buf *qbuf)
684{
685 struct drm_exynos_ipp_mem_node *m_node;
686 struct drm_exynos_ipp_buf_info buf_info;
687 void *addr;
688 int i;
689
Eunchul Kimcb471f142012-12-14 18:10:31 +0900690 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900691 if (!m_node)
YoungJun Cho220db6f2014-05-26 10:17:20 +0200692 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900693
694 /* clear base address for error handling */
695 memset(&buf_info, 0x0, sizeof(buf_info));
696
697 /* operations, buffer id */
698 m_node->ops_id = qbuf->ops_id;
699 m_node->prop_id = qbuf->prop_id;
700 m_node->buf_id = qbuf->buf_id;
701
YoungJun Chocbc4c332013-06-12 10:44:40 +0900702 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
703 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900704
705 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900706 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900707
708 /* get dma address by handle */
709 if (qbuf->handle[i]) {
710 addr = exynos_drm_gem_get_dma_addr(drm_dev,
711 qbuf->handle[i], file);
712 if (IS_ERR(addr)) {
713 DRM_ERROR("failed to get addr.\n");
714 goto err_clear;
715 }
716
717 buf_info.handles[i] = qbuf->handle[i];
718 buf_info.base[i] = *(dma_addr_t *) addr;
YoungJun Chocbc4c332013-06-12 10:44:40 +0900719 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%x]\n",
720 i, buf_info.base[i], (int)buf_info.handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900721 }
722 }
723
724 m_node->filp = file;
725 m_node->buf_info = buf_info;
YoungJun Cho220db6f2014-05-26 10:17:20 +0200726 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900727 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900728 mutex_unlock(&c_node->mem_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +0200729
Eunchul Kimcb471f142012-12-14 18:10:31 +0900730 return m_node;
731
732err_clear:
733 kfree(m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900734 return ERR_PTR(-EFAULT);
735}
736
737static int ipp_put_mem_node(struct drm_device *drm_dev,
738 struct drm_exynos_ipp_cmd_node *c_node,
739 struct drm_exynos_ipp_mem_node *m_node)
740{
741 int i;
742
YoungJun Chocbc4c332013-06-12 10:44:40 +0900743 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900744
745 if (!m_node) {
746 DRM_ERROR("invalid dequeue node.\n");
747 return -EFAULT;
748 }
749
YoungJun Chocbc4c332013-06-12 10:44:40 +0900750 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900751
752 /* put gem buffer */
753 for_each_ipp_planar(i) {
754 unsigned long handle = m_node->buf_info.handles[i];
755 if (handle)
756 exynos_drm_gem_put_dma_addr(drm_dev, handle,
757 m_node->filp);
758 }
759
760 /* delete list in queue */
761 list_del(&m_node->list);
762 kfree(m_node);
763
Eunchul Kimcb471f142012-12-14 18:10:31 +0900764 return 0;
765}
766
767static void ipp_free_event(struct drm_pending_event *event)
768{
769 kfree(event);
770}
771
772static int ipp_get_event(struct drm_device *drm_dev,
773 struct drm_file *file,
774 struct drm_exynos_ipp_cmd_node *c_node,
775 struct drm_exynos_ipp_queue_buf *qbuf)
776{
777 struct drm_exynos_ipp_send_event *e;
778 unsigned long flags;
779
YoungJun Chocbc4c332013-06-12 10:44:40 +0900780 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900781
782 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900783 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900784 spin_lock_irqsave(&drm_dev->event_lock, flags);
785 file->event_space += sizeof(e->event);
786 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
787 return -ENOMEM;
788 }
789
790 /* make event */
791 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
792 e->event.base.length = sizeof(e->event);
793 e->event.user_data = qbuf->user_data;
794 e->event.prop_id = qbuf->prop_id;
795 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
796 e->base.event = &e->event.base;
797 e->base.file_priv = file;
798 e->base.destroy = ipp_free_event;
799 list_add_tail(&e->base.link, &c_node->event_list);
800
801 return 0;
802}
803
804static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
805 struct drm_exynos_ipp_queue_buf *qbuf)
806{
807 struct drm_exynos_ipp_send_event *e, *te;
808 int count = 0;
809
Eunchul Kimcb471f142012-12-14 18:10:31 +0900810 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900811 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900812
813 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530814 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900815 * stop operations want to delete all event list.
816 * another case delete only same buf id.
817 */
818 if (!qbuf) {
819 /* delete list */
820 list_del(&e->base.link);
821 kfree(e);
822 }
823
824 /* compare buffer id */
825 if (qbuf && (qbuf->buf_id ==
826 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
827 /* delete list */
828 list_del(&e->base.link);
829 kfree(e);
830 return;
831 }
832 }
833}
834
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530835static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900836 struct exynos_drm_ippdrv *ippdrv,
837 struct drm_exynos_ipp_cmd_work *cmd_work,
838 struct drm_exynos_ipp_cmd_node *c_node)
839{
840 struct ipp_context *ctx = get_ipp_context(dev);
841
842 cmd_work->ippdrv = ippdrv;
843 cmd_work->c_node = c_node;
844 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
845}
846
847static int ipp_queue_buf_with_run(struct device *dev,
848 struct drm_exynos_ipp_cmd_node *c_node,
849 struct drm_exynos_ipp_mem_node *m_node,
850 struct drm_exynos_ipp_queue_buf *qbuf)
851{
852 struct exynos_drm_ippdrv *ippdrv;
853 struct drm_exynos_ipp_property *property;
854 struct exynos_drm_ipp_ops *ops;
855 int ret;
856
Eunchul Kimcb471f142012-12-14 18:10:31 +0900857 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530858 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900859 DRM_ERROR("failed to get ipp driver.\n");
860 return -EFAULT;
861 }
862
863 ops = ippdrv->ops[qbuf->ops_id];
864 if (!ops) {
865 DRM_ERROR("failed to get ops.\n");
866 return -EFAULT;
867 }
868
869 property = &c_node->property;
870
871 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900872 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900873 return 0;
874 }
875
YoungJun Cho220db6f2014-05-26 10:17:20 +0200876 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900877 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200878 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900879 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900880 return 0;
881 }
882
883 /*
884 * If set destination buffer and enabled clock,
885 * then m2m operations need start operations at queue_buf
886 */
887 if (ipp_is_m2m_cmd(property->cmd)) {
888 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
889
890 cmd_work->ctrl = IPP_CTRL_PLAY;
891 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
892 } else {
893 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
894 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200895 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900896 DRM_ERROR("failed to set m node.\n");
897 return ret;
898 }
899 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200900 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900901
902 return 0;
903}
904
905static void ipp_clean_queue_buf(struct drm_device *drm_dev,
906 struct drm_exynos_ipp_cmd_node *c_node,
907 struct drm_exynos_ipp_queue_buf *qbuf)
908{
909 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
910
YoungJun Choc66ce402014-05-26 10:17:15 +0200911 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200912 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200913 list_for_each_entry_safe(m_node, tm_node,
914 &c_node->mem_list[qbuf->ops_id], list) {
915 if (m_node->buf_id == qbuf->buf_id &&
916 m_node->ops_id == qbuf->ops_id)
917 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900918 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200919 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900920}
921
922int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
923 struct drm_file *file)
924{
925 struct drm_exynos_file_private *file_priv = file->driver_priv;
926 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
927 struct device *dev = priv->dev;
928 struct ipp_context *ctx = get_ipp_context(dev);
929 struct drm_exynos_ipp_queue_buf *qbuf = data;
930 struct drm_exynos_ipp_cmd_node *c_node;
931 struct drm_exynos_ipp_mem_node *m_node;
932 int ret;
933
Eunchul Kimcb471f142012-12-14 18:10:31 +0900934 if (!qbuf) {
935 DRM_ERROR("invalid buf parameter.\n");
936 return -EINVAL;
937 }
938
939 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
940 DRM_ERROR("invalid ops parameter.\n");
941 return -EINVAL;
942 }
943
YoungJun Chocbc4c332013-06-12 10:44:40 +0900944 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
945 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900946 qbuf->buf_id, qbuf->buf_type);
947
948 /* find command node */
949 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
950 qbuf->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800951 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900952 DRM_ERROR("failed to get command node.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +0800953 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900954 }
955
956 /* buffer control */
957 switch (qbuf->buf_type) {
958 case IPP_BUF_ENQUEUE:
959 /* get memory node */
960 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
961 if (IS_ERR(m_node)) {
962 DRM_ERROR("failed to get m_node.\n");
963 return PTR_ERR(m_node);
964 }
965
966 /*
967 * first step get event for destination buffer.
968 * and second step when M2M case run with destination buffer
969 * if needed.
970 */
971 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
972 /* get event for destination buffer */
973 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
974 if (ret) {
975 DRM_ERROR("failed to get event.\n");
976 goto err_clean_node;
977 }
978
979 /*
980 * M2M case run play control for streaming feature.
981 * other case set address and waiting.
982 */
983 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
984 if (ret) {
985 DRM_ERROR("failed to run command.\n");
986 goto err_clean_node;
987 }
988 }
989 break;
990 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200991 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900992
993 /* put event for destination buffer */
994 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
995 ipp_put_event(c_node, qbuf);
996
997 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
998
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200999 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001000 break;
1001 default:
1002 DRM_ERROR("invalid buffer control.\n");
1003 return -EINVAL;
1004 }
1005
1006 return 0;
1007
1008err_clean_node:
1009 DRM_ERROR("clean memory nodes.\n");
1010
1011 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1012 return ret;
1013}
1014
1015static bool exynos_drm_ipp_check_valid(struct device *dev,
1016 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
1017{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001018 if (ctrl != IPP_CTRL_PLAY) {
1019 if (pm_runtime_suspended(dev)) {
1020 DRM_ERROR("pm:runtime_suspended.\n");
1021 goto err_status;
1022 }
1023 }
1024
1025 switch (ctrl) {
1026 case IPP_CTRL_PLAY:
1027 if (state != IPP_STATE_IDLE)
1028 goto err_status;
1029 break;
1030 case IPP_CTRL_STOP:
1031 if (state == IPP_STATE_STOP)
1032 goto err_status;
1033 break;
1034 case IPP_CTRL_PAUSE:
1035 if (state != IPP_STATE_START)
1036 goto err_status;
1037 break;
1038 case IPP_CTRL_RESUME:
1039 if (state != IPP_STATE_STOP)
1040 goto err_status;
1041 break;
1042 default:
1043 DRM_ERROR("invalid state.\n");
1044 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001045 }
1046
1047 return true;
1048
1049err_status:
1050 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1051 return false;
1052}
1053
1054int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1055 struct drm_file *file)
1056{
1057 struct drm_exynos_file_private *file_priv = file->driver_priv;
1058 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1059 struct exynos_drm_ippdrv *ippdrv = NULL;
1060 struct device *dev = priv->dev;
1061 struct ipp_context *ctx = get_ipp_context(dev);
1062 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1063 struct drm_exynos_ipp_cmd_work *cmd_work;
1064 struct drm_exynos_ipp_cmd_node *c_node;
1065
Eunchul Kimcb471f142012-12-14 18:10:31 +09001066 if (!ctx) {
1067 DRM_ERROR("invalid context.\n");
1068 return -EINVAL;
1069 }
1070
1071 if (!cmd_ctrl) {
1072 DRM_ERROR("invalid control parameter.\n");
1073 return -EINVAL;
1074 }
1075
YoungJun Chocbc4c332013-06-12 10:44:40 +09001076 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001077 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1078
1079 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1080 if (IS_ERR(ippdrv)) {
1081 DRM_ERROR("failed to get ipp driver.\n");
1082 return PTR_ERR(ippdrv);
1083 }
1084
1085 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1086 cmd_ctrl->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +08001087 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001088 DRM_ERROR("invalid command node list.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +08001089 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001090 }
1091
1092 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1093 c_node->state)) {
1094 DRM_ERROR("invalid state.\n");
1095 return -EINVAL;
1096 }
1097
1098 switch (cmd_ctrl->ctrl) {
1099 case IPP_CTRL_PLAY:
1100 if (pm_runtime_suspended(ippdrv->dev))
1101 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001102
Eunchul Kimcb471f142012-12-14 18:10:31 +09001103 c_node->state = IPP_STATE_START;
1104
1105 cmd_work = c_node->start_work;
1106 cmd_work->ctrl = cmd_ctrl->ctrl;
1107 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001108 break;
1109 case IPP_CTRL_STOP:
1110 cmd_work = c_node->stop_work;
1111 cmd_work->ctrl = cmd_ctrl->ctrl;
1112 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1113
1114 if (!wait_for_completion_timeout(&c_node->stop_complete,
1115 msecs_to_jiffies(300))) {
1116 DRM_ERROR("timeout stop:prop_id[%d]\n",
1117 c_node->property.prop_id);
1118 }
1119
1120 c_node->state = IPP_STATE_STOP;
1121 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001122 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001123 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001124
1125 if (list_empty(&ippdrv->cmd_list))
1126 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001127 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001128 break;
1129 case IPP_CTRL_PAUSE:
1130 cmd_work = c_node->stop_work;
1131 cmd_work->ctrl = cmd_ctrl->ctrl;
1132 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1133
1134 if (!wait_for_completion_timeout(&c_node->stop_complete,
1135 msecs_to_jiffies(200))) {
1136 DRM_ERROR("timeout stop:prop_id[%d]\n",
1137 c_node->property.prop_id);
1138 }
1139
1140 c_node->state = IPP_STATE_STOP;
1141 break;
1142 case IPP_CTRL_RESUME:
1143 c_node->state = IPP_STATE_START;
1144 cmd_work = c_node->start_work;
1145 cmd_work->ctrl = cmd_ctrl->ctrl;
1146 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1147 break;
1148 default:
1149 DRM_ERROR("could not support this state currently.\n");
1150 return -EINVAL;
1151 }
1152
YoungJun Chocbc4c332013-06-12 10:44:40 +09001153 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001154 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1155
1156 return 0;
1157}
1158
1159int exynos_drm_ippnb_register(struct notifier_block *nb)
1160{
1161 return blocking_notifier_chain_register(
1162 &exynos_drm_ippnb_list, nb);
1163}
1164
1165int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1166{
1167 return blocking_notifier_chain_unregister(
1168 &exynos_drm_ippnb_list, nb);
1169}
1170
1171int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1172{
1173 return blocking_notifier_call_chain(
1174 &exynos_drm_ippnb_list, val, v);
1175}
1176
1177static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1178 struct drm_exynos_ipp_property *property)
1179{
1180 struct exynos_drm_ipp_ops *ops = NULL;
1181 bool swap = false;
1182 int ret, i;
1183
1184 if (!property) {
1185 DRM_ERROR("invalid property parameter.\n");
1186 return -EINVAL;
1187 }
1188
YoungJun Chocbc4c332013-06-12 10:44:40 +09001189 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001190
1191 /* reset h/w block */
1192 if (ippdrv->reset &&
1193 ippdrv->reset(ippdrv->dev)) {
1194 DRM_ERROR("failed to reset.\n");
1195 return -EINVAL;
1196 }
1197
1198 /* set source,destination operations */
1199 for_each_ipp_ops(i) {
1200 struct drm_exynos_ipp_config *config =
1201 &property->config[i];
1202
1203 ops = ippdrv->ops[i];
1204 if (!ops || !config) {
1205 DRM_ERROR("not support ops and config.\n");
1206 return -EINVAL;
1207 }
1208
1209 /* set format */
1210 if (ops->set_fmt) {
1211 ret = ops->set_fmt(ippdrv->dev, config->fmt);
1212 if (ret) {
1213 DRM_ERROR("not support format.\n");
1214 return ret;
1215 }
1216 }
1217
1218 /* set transform for rotation, flip */
1219 if (ops->set_transf) {
1220 ret = ops->set_transf(ippdrv->dev, config->degree,
1221 config->flip, &swap);
1222 if (ret) {
1223 DRM_ERROR("not support tranf.\n");
1224 return -EINVAL;
1225 }
1226 }
1227
1228 /* set size */
1229 if (ops->set_size) {
1230 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1231 &config->sz);
1232 if (ret) {
1233 DRM_ERROR("not support size.\n");
1234 return ret;
1235 }
1236 }
1237 }
1238
1239 return 0;
1240}
1241
1242static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1243 struct drm_exynos_ipp_cmd_node *c_node)
1244{
1245 struct drm_exynos_ipp_mem_node *m_node;
1246 struct drm_exynos_ipp_property *property = &c_node->property;
1247 struct list_head *head;
1248 int ret, i;
1249
YoungJun Chocbc4c332013-06-12 10:44:40 +09001250 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001251
1252 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001253 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001254
YoungJun Cho220db6f2014-05-26 10:17:20 +02001255 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001256 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001257 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001258 ret = -ENOMEM;
1259 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001260 }
1261
1262 /* set current property in ippdrv */
1263 ret = ipp_set_property(ippdrv, property);
1264 if (ret) {
1265 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001266 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001267 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001268 }
1269
1270 /* check command */
1271 switch (property->cmd) {
1272 case IPP_CMD_M2M:
1273 for_each_ipp_ops(i) {
1274 /* source/destination memory list */
1275 head = &c_node->mem_list[i];
1276
1277 m_node = list_first_entry(head,
1278 struct drm_exynos_ipp_mem_node, list);
1279 if (!m_node) {
1280 DRM_ERROR("failed to get node.\n");
1281 ret = -EFAULT;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001282 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001283 }
1284
YoungJun Chocbc4c332013-06-12 10:44:40 +09001285 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001286
1287 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1288 if (ret) {
1289 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001290 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001291 }
1292 }
1293 break;
1294 case IPP_CMD_WB:
1295 /* destination memory list */
1296 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1297
1298 list_for_each_entry(m_node, head, list) {
1299 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1300 if (ret) {
1301 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001302 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001303 }
1304 }
1305 break;
1306 case IPP_CMD_OUTPUT:
1307 /* source memory list */
1308 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1309
1310 list_for_each_entry(m_node, head, list) {
1311 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1312 if (ret) {
1313 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001314 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001315 }
1316 }
1317 break;
1318 default:
1319 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001320 ret = -EINVAL;
1321 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001322 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001323 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001324
YoungJun Chocbc4c332013-06-12 10:44:40 +09001325 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001326
1327 /* start operations */
1328 if (ippdrv->start) {
1329 ret = ippdrv->start(ippdrv->dev, property->cmd);
1330 if (ret) {
1331 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001332 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001333 return ret;
1334 }
1335 }
1336
1337 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001338
1339err_unlock:
1340 mutex_unlock(&c_node->mem_lock);
1341 ippdrv->c_node = NULL;
1342 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001343}
1344
1345static int ipp_stop_property(struct drm_device *drm_dev,
1346 struct exynos_drm_ippdrv *ippdrv,
1347 struct drm_exynos_ipp_cmd_node *c_node)
1348{
1349 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1350 struct drm_exynos_ipp_property *property = &c_node->property;
1351 struct list_head *head;
1352 int ret = 0, i;
1353
YoungJun Chocbc4c332013-06-12 10:44:40 +09001354 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001355
1356 /* put event */
1357 ipp_put_event(c_node, NULL);
1358
YoungJun Cho220db6f2014-05-26 10:17:20 +02001359 mutex_lock(&c_node->mem_lock);
1360
Eunchul Kimcb471f142012-12-14 18:10:31 +09001361 /* check command */
1362 switch (property->cmd) {
1363 case IPP_CMD_M2M:
1364 for_each_ipp_ops(i) {
1365 /* source/destination memory list */
1366 head = &c_node->mem_list[i];
1367
Eunchul Kimcb471f142012-12-14 18:10:31 +09001368 list_for_each_entry_safe(m_node, tm_node,
1369 head, list) {
1370 ret = ipp_put_mem_node(drm_dev, c_node,
1371 m_node);
1372 if (ret) {
1373 DRM_ERROR("failed to put m_node.\n");
1374 goto err_clear;
1375 }
1376 }
1377 }
1378 break;
1379 case IPP_CMD_WB:
1380 /* destination memory list */
1381 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1382
Eunchul Kimcb471f142012-12-14 18:10:31 +09001383 list_for_each_entry_safe(m_node, tm_node, head, list) {
1384 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1385 if (ret) {
1386 DRM_ERROR("failed to put m_node.\n");
1387 goto err_clear;
1388 }
1389 }
1390 break;
1391 case IPP_CMD_OUTPUT:
1392 /* source memory list */
1393 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1394
Eunchul Kimcb471f142012-12-14 18:10:31 +09001395 list_for_each_entry_safe(m_node, tm_node, head, list) {
1396 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1397 if (ret) {
1398 DRM_ERROR("failed to put m_node.\n");
1399 goto err_clear;
1400 }
1401 }
1402 break;
1403 default:
1404 DRM_ERROR("invalid operations.\n");
1405 ret = -EINVAL;
1406 goto err_clear;
1407 }
1408
1409err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001410 mutex_unlock(&c_node->mem_lock);
1411
Eunchul Kimcb471f142012-12-14 18:10:31 +09001412 /* stop operations */
1413 if (ippdrv->stop)
1414 ippdrv->stop(ippdrv->dev, property->cmd);
1415
1416 return ret;
1417}
1418
1419void ipp_sched_cmd(struct work_struct *work)
1420{
1421 struct drm_exynos_ipp_cmd_work *cmd_work =
1422 (struct drm_exynos_ipp_cmd_work *)work;
1423 struct exynos_drm_ippdrv *ippdrv;
1424 struct drm_exynos_ipp_cmd_node *c_node;
1425 struct drm_exynos_ipp_property *property;
1426 int ret;
1427
Eunchul Kimcb471f142012-12-14 18:10:31 +09001428 ippdrv = cmd_work->ippdrv;
1429 if (!ippdrv) {
1430 DRM_ERROR("invalid ippdrv list.\n");
1431 return;
1432 }
1433
1434 c_node = cmd_work->c_node;
1435 if (!c_node) {
1436 DRM_ERROR("invalid command node list.\n");
1437 return;
1438 }
1439
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001440 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001441
1442 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001443
1444 switch (cmd_work->ctrl) {
1445 case IPP_CTRL_PLAY:
1446 case IPP_CTRL_RESUME:
1447 ret = ipp_start_property(ippdrv, c_node);
1448 if (ret) {
1449 DRM_ERROR("failed to start property:prop_id[%d]\n",
1450 c_node->property.prop_id);
1451 goto err_unlock;
1452 }
1453
1454 /*
1455 * M2M case supports wait_completion of transfer.
1456 * because M2M case supports single unit operation
1457 * with multiple queue.
1458 * M2M need to wait completion of data transfer.
1459 */
1460 if (ipp_is_m2m_cmd(property->cmd)) {
1461 if (!wait_for_completion_timeout
1462 (&c_node->start_complete, msecs_to_jiffies(200))) {
1463 DRM_ERROR("timeout event:prop_id[%d]\n",
1464 c_node->property.prop_id);
1465 goto err_unlock;
1466 }
1467 }
1468 break;
1469 case IPP_CTRL_STOP:
1470 case IPP_CTRL_PAUSE:
1471 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1472 c_node);
1473 if (ret) {
1474 DRM_ERROR("failed to stop property.\n");
1475 goto err_unlock;
1476 }
1477
1478 complete(&c_node->stop_complete);
1479 break;
1480 default:
1481 DRM_ERROR("unknown control type\n");
1482 break;
1483 }
1484
YoungJun Chocbc4c332013-06-12 10:44:40 +09001485 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001486
1487err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001488 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001489}
1490
1491static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1492 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1493{
1494 struct drm_device *drm_dev = ippdrv->drm_dev;
1495 struct drm_exynos_ipp_property *property = &c_node->property;
1496 struct drm_exynos_ipp_mem_node *m_node;
1497 struct drm_exynos_ipp_queue_buf qbuf;
1498 struct drm_exynos_ipp_send_event *e;
1499 struct list_head *head;
1500 struct timeval now;
1501 unsigned long flags;
1502 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1503 int ret, i;
1504
1505 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001506 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001507
1508 if (!drm_dev) {
1509 DRM_ERROR("failed to get drm_dev.\n");
1510 return -EINVAL;
1511 }
1512
1513 if (!property) {
1514 DRM_ERROR("failed to get property.\n");
1515 return -EINVAL;
1516 }
1517
1518 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001519 DRM_DEBUG_KMS("event list is empty.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001520 return 0;
1521 }
1522
YoungJun Cho220db6f2014-05-26 10:17:20 +02001523 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001524 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001525 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001526 ret = 0;
1527 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001528 }
1529
1530 /* check command */
1531 switch (property->cmd) {
1532 case IPP_CMD_M2M:
1533 for_each_ipp_ops(i) {
1534 /* source/destination memory list */
1535 head = &c_node->mem_list[i];
1536
1537 m_node = list_first_entry(head,
1538 struct drm_exynos_ipp_mem_node, list);
1539 if (!m_node) {
1540 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001541 ret = -ENOMEM;
1542 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001543 }
1544
1545 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001546 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001547 i ? "dst" : "src", tbuf_id[i]);
1548
1549 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1550 if (ret)
1551 DRM_ERROR("failed to put m_node.\n");
1552 }
1553 break;
1554 case IPP_CMD_WB:
1555 /* clear buf for finding */
1556 memset(&qbuf, 0x0, sizeof(qbuf));
1557 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1558 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1559
1560 /* get memory node entry */
1561 m_node = ipp_find_mem_node(c_node, &qbuf);
1562 if (!m_node) {
1563 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001564 ret = -ENOMEM;
1565 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001566 }
1567
1568 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1569
1570 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1571 if (ret)
1572 DRM_ERROR("failed to put m_node.\n");
1573 break;
1574 case IPP_CMD_OUTPUT:
1575 /* source memory list */
1576 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1577
1578 m_node = list_first_entry(head,
1579 struct drm_exynos_ipp_mem_node, list);
1580 if (!m_node) {
1581 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001582 ret = -ENOMEM;
1583 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001584 }
1585
1586 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1587
1588 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1589 if (ret)
1590 DRM_ERROR("failed to put m_node.\n");
1591 break;
1592 default:
1593 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001594 ret = -EINVAL;
1595 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001596 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001597 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001598
1599 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1600 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1601 tbuf_id[1], buf_id[1], property->prop_id);
1602
1603 /*
1604 * command node have event list of destination buffer
1605 * If destination buffer enqueue to mem list,
1606 * then we make event and link to event list tail.
1607 * so, we get first event for first enqueued buffer.
1608 */
1609 e = list_first_entry(&c_node->event_list,
1610 struct drm_exynos_ipp_send_event, base.link);
1611
1612 if (!e) {
1613 DRM_ERROR("empty event.\n");
1614 return -EINVAL;
1615 }
1616
1617 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001618 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001619 e->event.tv_sec = now.tv_sec;
1620 e->event.tv_usec = now.tv_usec;
1621 e->event.prop_id = property->prop_id;
1622
1623 /* set buffer id about source destination */
1624 for_each_ipp_ops(i)
1625 e->event.buf_id[i] = tbuf_id[i];
1626
1627 spin_lock_irqsave(&drm_dev->event_lock, flags);
1628 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1629 wake_up_interruptible(&e->base.file_priv->event_wait);
1630 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1631
YoungJun Chocbc4c332013-06-12 10:44:40 +09001632 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001633 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1634
1635 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001636
1637err_mem_unlock:
1638 mutex_unlock(&c_node->mem_lock);
1639 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001640}
1641
1642void ipp_sched_event(struct work_struct *work)
1643{
1644 struct drm_exynos_ipp_event_work *event_work =
1645 (struct drm_exynos_ipp_event_work *)work;
1646 struct exynos_drm_ippdrv *ippdrv;
1647 struct drm_exynos_ipp_cmd_node *c_node;
1648 int ret;
1649
1650 if (!event_work) {
1651 DRM_ERROR("failed to get event_work.\n");
1652 return;
1653 }
1654
YoungJun Chocbc4c332013-06-12 10:44:40 +09001655 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001656
1657 ippdrv = event_work->ippdrv;
1658 if (!ippdrv) {
1659 DRM_ERROR("failed to get ipp driver.\n");
1660 return;
1661 }
1662
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001663 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001664 if (!c_node) {
1665 DRM_ERROR("failed to get command node.\n");
1666 return;
1667 }
1668
1669 /*
1670 * IPP supports command thread, event thread synchronization.
1671 * If IPP close immediately from user land, then IPP make
1672 * synchronization with command thread, so make complete event.
1673 * or going out operations.
1674 */
1675 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001676 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1677 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001678 goto err_completion;
1679 }
1680
1681 mutex_lock(&c_node->event_lock);
1682
1683 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1684 if (ret) {
1685 DRM_ERROR("failed to send event.\n");
1686 goto err_completion;
1687 }
1688
1689err_completion:
1690 if (ipp_is_m2m_cmd(c_node->property.cmd))
1691 complete(&c_node->start_complete);
1692
1693 mutex_unlock(&c_node->event_lock);
1694}
1695
1696static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1697{
1698 struct ipp_context *ctx = get_ipp_context(dev);
1699 struct exynos_drm_ippdrv *ippdrv;
1700 int ret, count = 0;
1701
Eunchul Kimcb471f142012-12-14 18:10:31 +09001702 /* get ipp driver entry */
1703 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001704 u32 ipp_id;
1705
Eunchul Kimcb471f142012-12-14 18:10:31 +09001706 ippdrv->drm_dev = drm_dev;
1707
1708 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001709 &ipp_id);
1710 if (ret || ipp_id == 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001711 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001712 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001713 }
1714
YoungJun Chocbc4c332013-06-12 10:44:40 +09001715 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001716 count++, (int)ippdrv, ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001717
Andrzej Hajda31646052014-05-19 12:54:05 +02001718 ippdrv->prop_list.ipp_id = ipp_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001719
1720 /* store parent device for node */
1721 ippdrv->parent_dev = dev;
1722
1723 /* store event work queue and handler */
1724 ippdrv->event_workq = ctx->event_workq;
1725 ippdrv->sched_event = ipp_sched_event;
1726 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001727 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001728
1729 if (is_drm_iommu_supported(drm_dev)) {
1730 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1731 if (ret) {
1732 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001733 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001734 }
1735 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001736 }
1737
1738 return 0;
1739
YoungJun Cho075436b2014-05-26 10:17:19 +02001740err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001741 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001742 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1743 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001744 if (is_drm_iommu_supported(drm_dev))
1745 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1746
YoungJun Cho075436b2014-05-26 10:17:19 +02001747 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1748 ippdrv->prop_list.ipp_id);
1749 }
1750
Eunchul Kimcb471f142012-12-14 18:10:31 +09001751 return ret;
1752}
1753
1754static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1755{
1756 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001757 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001758
Eunchul Kimcb471f142012-12-14 18:10:31 +09001759 /* get ipp driver entry */
1760 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001761 if (is_drm_iommu_supported(drm_dev))
1762 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1763
YoungJun Cho075436b2014-05-26 10:17:19 +02001764 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1765 ippdrv->prop_list.ipp_id);
1766
Eunchul Kimcb471f142012-12-14 18:10:31 +09001767 ippdrv->drm_dev = NULL;
1768 exynos_drm_ippdrv_unregister(ippdrv);
1769 }
1770}
1771
1772static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1773 struct drm_file *file)
1774{
1775 struct drm_exynos_file_private *file_priv = file->driver_priv;
1776 struct exynos_drm_ipp_private *priv;
1777
Eunchul Kimcb471f142012-12-14 18:10:31 +09001778 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +09001779 if (!priv)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001780 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001781 priv->dev = dev;
1782 file_priv->ipp_priv = priv;
1783
1784 INIT_LIST_HEAD(&priv->event_list);
1785
YoungJun Chocbc4c332013-06-12 10:44:40 +09001786 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)priv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001787
1788 return 0;
1789}
1790
1791static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1792 struct drm_file *file)
1793{
1794 struct drm_exynos_file_private *file_priv = file->driver_priv;
1795 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1796 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001797 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001798 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1799 int count = 0;
1800
YoungJun Chocbc4c332013-06-12 10:44:40 +09001801 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)priv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001802
Eunchul Kimcb471f142012-12-14 18:10:31 +09001803 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001804 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001805 list_for_each_entry_safe(c_node, tc_node,
1806 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001807 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1808 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001809
1810 if (c_node->priv == priv) {
1811 /*
1812 * userland goto unnormal state. process killed.
1813 * and close the file.
1814 * so, IPP didn't called stop cmd ctrl.
1815 * so, we are make stop operation in this state.
1816 */
1817 if (c_node->state == IPP_STATE_START) {
1818 ipp_stop_property(drm_dev, ippdrv,
1819 c_node);
1820 c_node->state = IPP_STATE_STOP;
1821 }
1822
1823 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001824 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001825 if (list_empty(&ippdrv->cmd_list))
1826 pm_runtime_put_sync(ippdrv->dev);
1827 }
1828 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001829 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001830 }
1831
Eunchul Kimcb471f142012-12-14 18:10:31 +09001832 kfree(priv);
1833 return;
1834}
1835
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001836static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001837{
1838 struct device *dev = &pdev->dev;
1839 struct ipp_context *ctx;
1840 struct exynos_drm_subdrv *subdrv;
1841 int ret;
1842
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001843 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001844 if (!ctx)
1845 return -ENOMEM;
1846
Eunchul Kimcb471f142012-12-14 18:10:31 +09001847 mutex_init(&ctx->ipp_lock);
1848 mutex_init(&ctx->prop_lock);
1849
1850 idr_init(&ctx->ipp_idr);
1851 idr_init(&ctx->prop_idr);
1852
1853 /*
1854 * create single thread for ipp event
1855 * IPP supports event thread for IPP drivers.
1856 * IPP driver send event_work to this thread.
1857 * and IPP event thread send event to user process.
1858 */
1859 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1860 if (!ctx->event_workq) {
1861 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301862 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001863 }
1864
1865 /*
1866 * create single thread for ipp command
1867 * IPP supports command thread for user process.
1868 * user process make command node using set property ioctl.
1869 * and make start_work and send this work to command thread.
1870 * and then this command thread start property.
1871 */
1872 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1873 if (!ctx->cmd_workq) {
1874 dev_err(dev, "failed to create cmd workqueue\n");
1875 ret = -EINVAL;
1876 goto err_event_workq;
1877 }
1878
1879 /* set sub driver informations */
1880 subdrv = &ctx->subdrv;
1881 subdrv->dev = dev;
1882 subdrv->probe = ipp_subdrv_probe;
1883 subdrv->remove = ipp_subdrv_remove;
1884 subdrv->open = ipp_subdrv_open;
1885 subdrv->close = ipp_subdrv_close;
1886
1887 platform_set_drvdata(pdev, ctx);
1888
1889 ret = exynos_drm_subdrv_register(subdrv);
1890 if (ret < 0) {
1891 DRM_ERROR("failed to register drm ipp device.\n");
1892 goto err_cmd_workq;
1893 }
1894
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001895 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001896
1897 return 0;
1898
1899err_cmd_workq:
1900 destroy_workqueue(ctx->cmd_workq);
1901err_event_workq:
1902 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001903 return ret;
1904}
1905
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001906static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001907{
1908 struct ipp_context *ctx = platform_get_drvdata(pdev);
1909
Eunchul Kimcb471f142012-12-14 18:10:31 +09001910 /* unregister sub driver */
1911 exynos_drm_subdrv_unregister(&ctx->subdrv);
1912
1913 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001914 idr_destroy(&ctx->ipp_idr);
1915 idr_destroy(&ctx->prop_idr);
1916
1917 mutex_destroy(&ctx->ipp_lock);
1918 mutex_destroy(&ctx->prop_lock);
1919
1920 /* destroy command, event work queue */
1921 destroy_workqueue(ctx->cmd_workq);
1922 destroy_workqueue(ctx->event_workq);
1923
Eunchul Kimcb471f142012-12-14 18:10:31 +09001924 return 0;
1925}
1926
1927static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1928{
YoungJun Chocbc4c332013-06-12 10:44:40 +09001929 DRM_DEBUG_KMS("enable[%d]\n", enable);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001930
1931 return 0;
1932}
1933
1934#ifdef CONFIG_PM_SLEEP
1935static int ipp_suspend(struct device *dev)
1936{
1937 struct ipp_context *ctx = get_ipp_context(dev);
1938
Eunchul Kimcb471f142012-12-14 18:10:31 +09001939 if (pm_runtime_suspended(dev))
1940 return 0;
1941
1942 return ipp_power_ctrl(ctx, false);
1943}
1944
1945static int ipp_resume(struct device *dev)
1946{
1947 struct ipp_context *ctx = get_ipp_context(dev);
1948
Eunchul Kimcb471f142012-12-14 18:10:31 +09001949 if (!pm_runtime_suspended(dev))
1950 return ipp_power_ctrl(ctx, true);
1951
1952 return 0;
1953}
1954#endif
1955
1956#ifdef CONFIG_PM_RUNTIME
1957static int ipp_runtime_suspend(struct device *dev)
1958{
1959 struct ipp_context *ctx = get_ipp_context(dev);
1960
Eunchul Kimcb471f142012-12-14 18:10:31 +09001961 return ipp_power_ctrl(ctx, false);
1962}
1963
1964static int ipp_runtime_resume(struct device *dev)
1965{
1966 struct ipp_context *ctx = get_ipp_context(dev);
1967
Eunchul Kimcb471f142012-12-14 18:10:31 +09001968 return ipp_power_ctrl(ctx, true);
1969}
1970#endif
1971
1972static const struct dev_pm_ops ipp_pm_ops = {
1973 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1974 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1975};
1976
1977struct platform_driver ipp_driver = {
1978 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001979 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001980 .driver = {
1981 .name = "exynos-drm-ipp",
1982 .owner = THIS_MODULE,
1983 .pm = &ipp_pm_ops,
1984 },
1985};
1986