blob: b7ce14ec4ef866ea2b936bf3fbdf14a8baaf5bfc [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.
Geert Uytterhoeven9fca9ac2014-03-11 11:23:37 +0100289 * e.g PAUSE state, queue buf, command control.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900290 */
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 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900300 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200301 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;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200311 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900312 struct ipp_context *ctx = get_ipp_context(dev);
313 struct drm_exynos_ipp_prop_list *prop_list = data;
314 struct exynos_drm_ippdrv *ippdrv;
315 int count = 0;
316
Eunchul Kimcb471f142012-12-14 18:10:31 +0900317 if (!ctx) {
318 DRM_ERROR("invalid context.\n");
319 return -EINVAL;
320 }
321
322 if (!prop_list) {
323 DRM_ERROR("invalid property parameter.\n");
324 return -EINVAL;
325 }
326
YoungJun Chocbc4c332013-06-12 10:44:40 +0900327 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900328
329 if (!prop_list->ipp_id) {
330 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
331 count++;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200332
Eunchul Kimcb471f142012-12-14 18:10:31 +0900333 /*
334 * Supports ippdrv list count for user application.
335 * First step user application getting ippdrv count.
336 * and second step getting ippdrv capability using ipp_id.
337 */
338 prop_list->count = count;
339 } else {
340 /*
341 * Getting ippdrv capability by ipp_id.
Masanari Iidac6b78bc2013-10-24 16:02:57 +0900342 * some device not supported wb, output interface.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900343 * so, user application detect correct ipp driver
344 * using this ioctl.
345 */
346 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
347 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800348 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900349 DRM_ERROR("not found ipp%d driver.\n",
350 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800351 return PTR_ERR(ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900352 }
353
Andrzej Hajda31646052014-05-19 12:54:05 +0200354 *prop_list = ippdrv->prop_list;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900355 }
356
357 return 0;
358}
359
360static void ipp_print_property(struct drm_exynos_ipp_property *property,
361 int idx)
362{
363 struct drm_exynos_ipp_config *config = &property->config[idx];
364 struct drm_exynos_pos *pos = &config->pos;
365 struct drm_exynos_sz *sz = &config->sz;
366
YoungJun Chocbc4c332013-06-12 10:44:40 +0900367 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
368 property->prop_id, idx ? "dst" : "src", config->fmt);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900369
YoungJun Chocbc4c332013-06-12 10:44:40 +0900370 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
371 pos->x, pos->y, pos->w, pos->h,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900372 sz->hsize, sz->vsize, config->flip, config->degree);
373}
374
375static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
376{
377 struct exynos_drm_ippdrv *ippdrv;
378 struct drm_exynos_ipp_cmd_node *c_node;
379 u32 prop_id = property->prop_id;
380
YoungJun Chocbc4c332013-06-12 10:44:40 +0900381 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900382
383 ippdrv = ipp_find_drv_by_handle(prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530384 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900385 DRM_ERROR("failed to get ipp driver.\n");
386 return -EINVAL;
387 }
388
389 /*
390 * Find command node using command list in ippdrv.
391 * when we find this command no using prop_id.
392 * return property information set in this command node.
393 */
YoungJun Cho7f5af052014-05-26 10:17:18 +0200394 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900395 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
396 if ((c_node->property.prop_id == prop_id) &&
397 (c_node->state == IPP_STATE_STOP)) {
YoungJun Cho7f5af052014-05-26 10:17:18 +0200398 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900399 DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
400 property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900401
402 c_node->property = *property;
403 return 0;
404 }
405 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200406 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900407
408 DRM_ERROR("failed to search property.\n");
409
410 return -EINVAL;
411}
412
413static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
414{
415 struct drm_exynos_ipp_cmd_work *cmd_work;
416
Eunchul Kimcb471f142012-12-14 18:10:31 +0900417 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900418 if (!cmd_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900419 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900420
421 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
422
423 return cmd_work;
424}
425
426static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
427{
428 struct drm_exynos_ipp_event_work *event_work;
429
Eunchul Kimcb471f142012-12-14 18:10:31 +0900430 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900431 if (!event_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900432 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900433
Andrzej Hajda60b61c22014-07-03 15:10:26 +0200434 INIT_WORK(&event_work->work, ipp_sched_event);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900435
436 return event_work;
437}
438
439int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
440 struct drm_file *file)
441{
442 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200443 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900444 struct ipp_context *ctx = get_ipp_context(dev);
445 struct drm_exynos_ipp_property *property = data;
446 struct exynos_drm_ippdrv *ippdrv;
447 struct drm_exynos_ipp_cmd_node *c_node;
448 int ret, i;
449
Eunchul Kimcb471f142012-12-14 18:10:31 +0900450 if (!ctx) {
451 DRM_ERROR("invalid context.\n");
452 return -EINVAL;
453 }
454
455 if (!property) {
456 DRM_ERROR("invalid property parameter.\n");
457 return -EINVAL;
458 }
459
460 /*
461 * This is log print for user application property.
462 * user application set various property.
463 */
464 for_each_ipp_ops(i)
465 ipp_print_property(property, i);
466
467 /*
468 * set property ioctl generated new prop_id.
469 * but in this case already asigned prop_id using old set property.
470 * e.g PAUSE state. this case supports find current prop_id and use it
471 * instead of allocation.
472 */
473 if (property->prop_id) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900474 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900475 return ipp_find_and_set_property(property);
476 }
477
478 /* find ipp driver using ipp id */
479 ippdrv = ipp_find_driver(ctx, property);
Sachin Kamatf0250452013-04-29 12:27:06 +0530480 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900481 DRM_ERROR("failed to get ipp driver.\n");
482 return -EINVAL;
483 }
484
485 /* allocate command node */
486 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900487 if (!c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900488 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900489
490 /* create property id */
491 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node,
492 &property->prop_id);
493 if (ret) {
494 DRM_ERROR("failed to create id.\n");
495 goto err_clear;
496 }
497
YoungJun Chocbc4c332013-06-12 10:44:40 +0900498 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
499 property->prop_id, property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900500
501 /* stored property information and ippdrv in private data */
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200502 c_node->dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900503 c_node->property = *property;
504 c_node->state = IPP_STATE_IDLE;
505
506 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530507 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900508 DRM_ERROR("failed to create start work.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +0200509 goto err_remove_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900510 }
511
512 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530513 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900514 DRM_ERROR("failed to create stop work.\n");
515 goto err_free_start;
516 }
517
518 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530519 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900520 DRM_ERROR("failed to create event work.\n");
521 goto err_free_stop;
522 }
523
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200524 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900525 mutex_init(&c_node->mem_lock);
526 mutex_init(&c_node->event_lock);
527
528 init_completion(&c_node->start_complete);
529 init_completion(&c_node->stop_complete);
530
531 for_each_ipp_ops(i)
532 INIT_LIST_HEAD(&c_node->mem_list[i]);
533
534 INIT_LIST_HEAD(&c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200535 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900536 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200537 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900538
539 /* make dedicated state without m2m */
540 if (!ipp_is_m2m_cmd(property->cmd))
541 ippdrv->dedicated = true;
542
543 return 0;
544
545err_free_stop:
546 kfree(c_node->stop_work);
547err_free_start:
548 kfree(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200549err_remove_id:
550 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900551err_clear:
552 kfree(c_node);
553 return ret;
554}
555
YoungJun Cho075436b2014-05-26 10:17:19 +0200556static void ipp_clean_cmd_node(struct ipp_context *ctx,
557 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900558{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900559 /* delete list */
560 list_del(&c_node->list);
561
YoungJun Cho075436b2014-05-26 10:17:19 +0200562 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
563 c_node->property.prop_id);
564
Eunchul Kimcb471f142012-12-14 18:10:31 +0900565 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200566 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900567 mutex_destroy(&c_node->mem_lock);
568 mutex_destroy(&c_node->event_lock);
569
570 /* free command node */
571 kfree(c_node->start_work);
572 kfree(c_node->stop_work);
573 kfree(c_node->event_work);
574 kfree(c_node);
575}
576
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200577static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900578{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200579 switch (c_node->property.cmd) {
580 case IPP_CMD_WB:
581 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
582 case IPP_CMD_OUTPUT:
583 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
584 case IPP_CMD_M2M:
585 default:
586 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
587 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900588 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900589}
590
591static struct drm_exynos_ipp_mem_node
592 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
593 struct drm_exynos_ipp_queue_buf *qbuf)
594{
595 struct drm_exynos_ipp_mem_node *m_node;
596 struct list_head *head;
597 int count = 0;
598
YoungJun Chocbc4c332013-06-12 10:44:40 +0900599 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900600
601 /* source/destination memory list */
602 head = &c_node->mem_list[qbuf->ops_id];
603
604 /* find memory node from memory list */
605 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900606 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900607
608 /* compare buffer id */
609 if (m_node->buf_id == qbuf->buf_id)
610 return m_node;
611 }
612
613 return NULL;
614}
615
616static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
617 struct drm_exynos_ipp_cmd_node *c_node,
618 struct drm_exynos_ipp_mem_node *m_node)
619{
620 struct exynos_drm_ipp_ops *ops = NULL;
621 int ret = 0;
622
YoungJun Chocbc4c332013-06-12 10:44:40 +0900623 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900624
625 if (!m_node) {
626 DRM_ERROR("invalid queue node.\n");
627 return -EFAULT;
628 }
629
YoungJun Chocbc4c332013-06-12 10:44:40 +0900630 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900631
632 /* get operations callback */
633 ops = ippdrv->ops[m_node->ops_id];
634 if (!ops) {
635 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200636 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900637 }
638
639 /* set address and enable irq */
640 if (ops->set_addr) {
641 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
642 m_node->buf_id, IPP_BUF_ENQUEUE);
643 if (ret) {
644 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200645 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900646 }
647 }
648
Eunchul Kimcb471f142012-12-14 18:10:31 +0900649 return ret;
650}
651
652static struct drm_exynos_ipp_mem_node
653 *ipp_get_mem_node(struct drm_device *drm_dev,
654 struct drm_file *file,
655 struct drm_exynos_ipp_cmd_node *c_node,
656 struct drm_exynos_ipp_queue_buf *qbuf)
657{
658 struct drm_exynos_ipp_mem_node *m_node;
Andrzej Hajda73b00232014-07-03 15:10:30 +0200659 struct drm_exynos_ipp_buf_info *buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900660 int i;
661
Eunchul Kimcb471f142012-12-14 18:10:31 +0900662 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900663 if (!m_node)
YoungJun Cho220db6f2014-05-26 10:17:20 +0200664 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900665
Andrzej Hajda73b00232014-07-03 15:10:30 +0200666 buf_info = &m_node->buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900667
668 /* operations, buffer id */
669 m_node->ops_id = qbuf->ops_id;
670 m_node->prop_id = qbuf->prop_id;
671 m_node->buf_id = qbuf->buf_id;
672
YoungJun Chocbc4c332013-06-12 10:44:40 +0900673 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
674 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900675
676 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900677 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900678
679 /* get dma address by handle */
680 if (qbuf->handle[i]) {
Andrzej Hajdaa8ea17f2014-07-03 15:10:29 +0200681 dma_addr_t *addr;
682
Eunchul Kimcb471f142012-12-14 18:10:31 +0900683 addr = exynos_drm_gem_get_dma_addr(drm_dev,
684 qbuf->handle[i], file);
685 if (IS_ERR(addr)) {
686 DRM_ERROR("failed to get addr.\n");
687 goto err_clear;
688 }
689
Andrzej Hajda73b00232014-07-03 15:10:30 +0200690 buf_info->handles[i] = qbuf->handle[i];
691 buf_info->base[i] = *addr;
692 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
693 buf_info->base[i], buf_info->handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900694 }
695 }
696
697 m_node->filp = file;
YoungJun Cho220db6f2014-05-26 10:17:20 +0200698 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900699 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900700 mutex_unlock(&c_node->mem_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +0200701
Eunchul Kimcb471f142012-12-14 18:10:31 +0900702 return m_node;
703
704err_clear:
705 kfree(m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900706 return ERR_PTR(-EFAULT);
707}
708
709static int ipp_put_mem_node(struct drm_device *drm_dev,
710 struct drm_exynos_ipp_cmd_node *c_node,
711 struct drm_exynos_ipp_mem_node *m_node)
712{
713 int i;
714
YoungJun Chocbc4c332013-06-12 10:44:40 +0900715 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900716
717 if (!m_node) {
718 DRM_ERROR("invalid dequeue node.\n");
719 return -EFAULT;
720 }
721
YoungJun Chocbc4c332013-06-12 10:44:40 +0900722 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900723
724 /* put gem buffer */
725 for_each_ipp_planar(i) {
726 unsigned long handle = m_node->buf_info.handles[i];
727 if (handle)
728 exynos_drm_gem_put_dma_addr(drm_dev, handle,
729 m_node->filp);
730 }
731
732 /* delete list in queue */
733 list_del(&m_node->list);
734 kfree(m_node);
735
Eunchul Kimcb471f142012-12-14 18:10:31 +0900736 return 0;
737}
738
739static void ipp_free_event(struct drm_pending_event *event)
740{
741 kfree(event);
742}
743
744static int ipp_get_event(struct drm_device *drm_dev,
745 struct drm_file *file,
746 struct drm_exynos_ipp_cmd_node *c_node,
747 struct drm_exynos_ipp_queue_buf *qbuf)
748{
749 struct drm_exynos_ipp_send_event *e;
750 unsigned long flags;
751
YoungJun Chocbc4c332013-06-12 10:44:40 +0900752 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900753
754 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900755 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900756 spin_lock_irqsave(&drm_dev->event_lock, flags);
757 file->event_space += sizeof(e->event);
758 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
759 return -ENOMEM;
760 }
761
762 /* make event */
763 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
764 e->event.base.length = sizeof(e->event);
765 e->event.user_data = qbuf->user_data;
766 e->event.prop_id = qbuf->prop_id;
767 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
768 e->base.event = &e->event.base;
769 e->base.file_priv = file;
770 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200771 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900772 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200773 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900774
775 return 0;
776}
777
778static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
779 struct drm_exynos_ipp_queue_buf *qbuf)
780{
781 struct drm_exynos_ipp_send_event *e, *te;
782 int count = 0;
783
YoungJun Cho4d520762014-05-26 10:17:21 +0200784 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900785 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900786 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900787
788 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530789 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900790 * stop operations want to delete all event list.
791 * another case delete only same buf id.
792 */
793 if (!qbuf) {
794 /* delete list */
795 list_del(&e->base.link);
796 kfree(e);
797 }
798
799 /* compare buffer id */
800 if (qbuf && (qbuf->buf_id ==
801 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
802 /* delete list */
803 list_del(&e->base.link);
804 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200805 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900806 }
807 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200808
809out_unlock:
810 mutex_unlock(&c_node->event_lock);
811 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900812}
813
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530814static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900815 struct exynos_drm_ippdrv *ippdrv,
816 struct drm_exynos_ipp_cmd_work *cmd_work,
817 struct drm_exynos_ipp_cmd_node *c_node)
818{
819 struct ipp_context *ctx = get_ipp_context(dev);
820
821 cmd_work->ippdrv = ippdrv;
822 cmd_work->c_node = c_node;
823 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
824}
825
826static int ipp_queue_buf_with_run(struct device *dev,
827 struct drm_exynos_ipp_cmd_node *c_node,
828 struct drm_exynos_ipp_mem_node *m_node,
829 struct drm_exynos_ipp_queue_buf *qbuf)
830{
831 struct exynos_drm_ippdrv *ippdrv;
832 struct drm_exynos_ipp_property *property;
833 struct exynos_drm_ipp_ops *ops;
834 int ret;
835
Eunchul Kimcb471f142012-12-14 18:10:31 +0900836 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530837 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900838 DRM_ERROR("failed to get ipp driver.\n");
839 return -EFAULT;
840 }
841
842 ops = ippdrv->ops[qbuf->ops_id];
843 if (!ops) {
844 DRM_ERROR("failed to get ops.\n");
845 return -EFAULT;
846 }
847
848 property = &c_node->property;
849
850 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900851 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900852 return 0;
853 }
854
YoungJun Cho220db6f2014-05-26 10:17:20 +0200855 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900856 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200857 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900858 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900859 return 0;
860 }
861
862 /*
863 * If set destination buffer and enabled clock,
864 * then m2m operations need start operations at queue_buf
865 */
866 if (ipp_is_m2m_cmd(property->cmd)) {
867 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
868
869 cmd_work->ctrl = IPP_CTRL_PLAY;
870 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
871 } else {
872 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
873 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200874 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900875 DRM_ERROR("failed to set m node.\n");
876 return ret;
877 }
878 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200879 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900880
881 return 0;
882}
883
884static void ipp_clean_queue_buf(struct drm_device *drm_dev,
885 struct drm_exynos_ipp_cmd_node *c_node,
886 struct drm_exynos_ipp_queue_buf *qbuf)
887{
888 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
889
YoungJun Choc66ce402014-05-26 10:17:15 +0200890 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200891 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200892 list_for_each_entry_safe(m_node, tm_node,
893 &c_node->mem_list[qbuf->ops_id], list) {
894 if (m_node->buf_id == qbuf->buf_id &&
895 m_node->ops_id == qbuf->ops_id)
896 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900897 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200898 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900899}
900
901int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
902 struct drm_file *file)
903{
904 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200905 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900906 struct ipp_context *ctx = get_ipp_context(dev);
907 struct drm_exynos_ipp_queue_buf *qbuf = data;
908 struct drm_exynos_ipp_cmd_node *c_node;
909 struct drm_exynos_ipp_mem_node *m_node;
910 int ret;
911
Eunchul Kimcb471f142012-12-14 18:10:31 +0900912 if (!qbuf) {
913 DRM_ERROR("invalid buf parameter.\n");
914 return -EINVAL;
915 }
916
917 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
918 DRM_ERROR("invalid ops parameter.\n");
919 return -EINVAL;
920 }
921
YoungJun Chocbc4c332013-06-12 10:44:40 +0900922 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
923 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900924 qbuf->buf_id, qbuf->buf_type);
925
926 /* find command node */
927 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
928 qbuf->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800929 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900930 DRM_ERROR("failed to get command node.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +0800931 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900932 }
933
934 /* buffer control */
935 switch (qbuf->buf_type) {
936 case IPP_BUF_ENQUEUE:
937 /* get memory node */
938 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
939 if (IS_ERR(m_node)) {
940 DRM_ERROR("failed to get m_node.\n");
941 return PTR_ERR(m_node);
942 }
943
944 /*
945 * first step get event for destination buffer.
946 * and second step when M2M case run with destination buffer
947 * if needed.
948 */
949 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
950 /* get event for destination buffer */
951 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
952 if (ret) {
953 DRM_ERROR("failed to get event.\n");
954 goto err_clean_node;
955 }
956
957 /*
958 * M2M case run play control for streaming feature.
959 * other case set address and waiting.
960 */
961 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
962 if (ret) {
963 DRM_ERROR("failed to run command.\n");
964 goto err_clean_node;
965 }
966 }
967 break;
968 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200969 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900970
971 /* put event for destination buffer */
972 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
973 ipp_put_event(c_node, qbuf);
974
975 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
976
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200977 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900978 break;
979 default:
980 DRM_ERROR("invalid buffer control.\n");
981 return -EINVAL;
982 }
983
984 return 0;
985
986err_clean_node:
987 DRM_ERROR("clean memory nodes.\n");
988
989 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
990 return ret;
991}
992
993static bool exynos_drm_ipp_check_valid(struct device *dev,
994 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
995{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900996 if (ctrl != IPP_CTRL_PLAY) {
997 if (pm_runtime_suspended(dev)) {
998 DRM_ERROR("pm:runtime_suspended.\n");
999 goto err_status;
1000 }
1001 }
1002
1003 switch (ctrl) {
1004 case IPP_CTRL_PLAY:
1005 if (state != IPP_STATE_IDLE)
1006 goto err_status;
1007 break;
1008 case IPP_CTRL_STOP:
1009 if (state == IPP_STATE_STOP)
1010 goto err_status;
1011 break;
1012 case IPP_CTRL_PAUSE:
1013 if (state != IPP_STATE_START)
1014 goto err_status;
1015 break;
1016 case IPP_CTRL_RESUME:
1017 if (state != IPP_STATE_STOP)
1018 goto err_status;
1019 break;
1020 default:
1021 DRM_ERROR("invalid state.\n");
1022 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001023 }
1024
1025 return true;
1026
1027err_status:
1028 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1029 return false;
1030}
1031
1032int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1033 struct drm_file *file)
1034{
1035 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001036 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001037 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001038 struct ipp_context *ctx = get_ipp_context(dev);
1039 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1040 struct drm_exynos_ipp_cmd_work *cmd_work;
1041 struct drm_exynos_ipp_cmd_node *c_node;
1042
Eunchul Kimcb471f142012-12-14 18:10:31 +09001043 if (!ctx) {
1044 DRM_ERROR("invalid context.\n");
1045 return -EINVAL;
1046 }
1047
1048 if (!cmd_ctrl) {
1049 DRM_ERROR("invalid control parameter.\n");
1050 return -EINVAL;
1051 }
1052
YoungJun Chocbc4c332013-06-12 10:44:40 +09001053 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001054 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1055
1056 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1057 if (IS_ERR(ippdrv)) {
1058 DRM_ERROR("failed to get ipp driver.\n");
1059 return PTR_ERR(ippdrv);
1060 }
1061
1062 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1063 cmd_ctrl->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +08001064 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001065 DRM_ERROR("invalid command node list.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +08001066 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001067 }
1068
1069 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1070 c_node->state)) {
1071 DRM_ERROR("invalid state.\n");
1072 return -EINVAL;
1073 }
1074
1075 switch (cmd_ctrl->ctrl) {
1076 case IPP_CTRL_PLAY:
1077 if (pm_runtime_suspended(ippdrv->dev))
1078 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001079
Eunchul Kimcb471f142012-12-14 18:10:31 +09001080 c_node->state = IPP_STATE_START;
1081
1082 cmd_work = c_node->start_work;
1083 cmd_work->ctrl = cmd_ctrl->ctrl;
1084 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001085 break;
1086 case IPP_CTRL_STOP:
1087 cmd_work = c_node->stop_work;
1088 cmd_work->ctrl = cmd_ctrl->ctrl;
1089 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1090
1091 if (!wait_for_completion_timeout(&c_node->stop_complete,
1092 msecs_to_jiffies(300))) {
1093 DRM_ERROR("timeout stop:prop_id[%d]\n",
1094 c_node->property.prop_id);
1095 }
1096
1097 c_node->state = IPP_STATE_STOP;
1098 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001099 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001100 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001101
1102 if (list_empty(&ippdrv->cmd_list))
1103 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001104 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001105 break;
1106 case IPP_CTRL_PAUSE:
1107 cmd_work = c_node->stop_work;
1108 cmd_work->ctrl = cmd_ctrl->ctrl;
1109 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1110
1111 if (!wait_for_completion_timeout(&c_node->stop_complete,
1112 msecs_to_jiffies(200))) {
1113 DRM_ERROR("timeout stop:prop_id[%d]\n",
1114 c_node->property.prop_id);
1115 }
1116
1117 c_node->state = IPP_STATE_STOP;
1118 break;
1119 case IPP_CTRL_RESUME:
1120 c_node->state = IPP_STATE_START;
1121 cmd_work = c_node->start_work;
1122 cmd_work->ctrl = cmd_ctrl->ctrl;
1123 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1124 break;
1125 default:
1126 DRM_ERROR("could not support this state currently.\n");
1127 return -EINVAL;
1128 }
1129
YoungJun Chocbc4c332013-06-12 10:44:40 +09001130 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001131 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1132
1133 return 0;
1134}
1135
1136int exynos_drm_ippnb_register(struct notifier_block *nb)
1137{
1138 return blocking_notifier_chain_register(
1139 &exynos_drm_ippnb_list, nb);
1140}
1141
1142int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1143{
1144 return blocking_notifier_chain_unregister(
1145 &exynos_drm_ippnb_list, nb);
1146}
1147
1148int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1149{
1150 return blocking_notifier_call_chain(
1151 &exynos_drm_ippnb_list, val, v);
1152}
1153
1154static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1155 struct drm_exynos_ipp_property *property)
1156{
1157 struct exynos_drm_ipp_ops *ops = NULL;
1158 bool swap = false;
1159 int ret, i;
1160
1161 if (!property) {
1162 DRM_ERROR("invalid property parameter.\n");
1163 return -EINVAL;
1164 }
1165
YoungJun Chocbc4c332013-06-12 10:44:40 +09001166 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001167
1168 /* reset h/w block */
1169 if (ippdrv->reset &&
1170 ippdrv->reset(ippdrv->dev)) {
1171 DRM_ERROR("failed to reset.\n");
1172 return -EINVAL;
1173 }
1174
1175 /* set source,destination operations */
1176 for_each_ipp_ops(i) {
1177 struct drm_exynos_ipp_config *config =
1178 &property->config[i];
1179
1180 ops = ippdrv->ops[i];
1181 if (!ops || !config) {
1182 DRM_ERROR("not support ops and config.\n");
1183 return -EINVAL;
1184 }
1185
1186 /* set format */
1187 if (ops->set_fmt) {
1188 ret = ops->set_fmt(ippdrv->dev, config->fmt);
1189 if (ret) {
1190 DRM_ERROR("not support format.\n");
1191 return ret;
1192 }
1193 }
1194
1195 /* set transform for rotation, flip */
1196 if (ops->set_transf) {
1197 ret = ops->set_transf(ippdrv->dev, config->degree,
1198 config->flip, &swap);
1199 if (ret) {
1200 DRM_ERROR("not support tranf.\n");
1201 return -EINVAL;
1202 }
1203 }
1204
1205 /* set size */
1206 if (ops->set_size) {
1207 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1208 &config->sz);
1209 if (ret) {
1210 DRM_ERROR("not support size.\n");
1211 return ret;
1212 }
1213 }
1214 }
1215
1216 return 0;
1217}
1218
1219static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1220 struct drm_exynos_ipp_cmd_node *c_node)
1221{
1222 struct drm_exynos_ipp_mem_node *m_node;
1223 struct drm_exynos_ipp_property *property = &c_node->property;
1224 struct list_head *head;
1225 int ret, i;
1226
YoungJun Chocbc4c332013-06-12 10:44:40 +09001227 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001228
1229 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001230 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001231
YoungJun Cho220db6f2014-05-26 10:17:20 +02001232 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001233 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001234 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001235 ret = -ENOMEM;
1236 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001237 }
1238
1239 /* set current property in ippdrv */
1240 ret = ipp_set_property(ippdrv, property);
1241 if (ret) {
1242 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001243 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001244 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001245 }
1246
1247 /* check command */
1248 switch (property->cmd) {
1249 case IPP_CMD_M2M:
1250 for_each_ipp_ops(i) {
1251 /* source/destination memory list */
1252 head = &c_node->mem_list[i];
1253
1254 m_node = list_first_entry(head,
1255 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001256
YoungJun Chocbc4c332013-06-12 10:44:40 +09001257 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001258
1259 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1260 if (ret) {
1261 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001262 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001263 }
1264 }
1265 break;
1266 case IPP_CMD_WB:
1267 /* destination memory list */
1268 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1269
1270 list_for_each_entry(m_node, head, list) {
1271 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1272 if (ret) {
1273 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001274 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001275 }
1276 }
1277 break;
1278 case IPP_CMD_OUTPUT:
1279 /* source memory list */
1280 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1281
1282 list_for_each_entry(m_node, head, list) {
1283 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1284 if (ret) {
1285 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001286 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001287 }
1288 }
1289 break;
1290 default:
1291 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001292 ret = -EINVAL;
1293 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001294 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001295 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001296
YoungJun Chocbc4c332013-06-12 10:44:40 +09001297 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001298
1299 /* start operations */
1300 if (ippdrv->start) {
1301 ret = ippdrv->start(ippdrv->dev, property->cmd);
1302 if (ret) {
1303 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001304 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001305 return ret;
1306 }
1307 }
1308
1309 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001310
1311err_unlock:
1312 mutex_unlock(&c_node->mem_lock);
1313 ippdrv->c_node = NULL;
1314 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001315}
1316
1317static int ipp_stop_property(struct drm_device *drm_dev,
1318 struct exynos_drm_ippdrv *ippdrv,
1319 struct drm_exynos_ipp_cmd_node *c_node)
1320{
1321 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1322 struct drm_exynos_ipp_property *property = &c_node->property;
1323 struct list_head *head;
1324 int ret = 0, i;
1325
YoungJun Chocbc4c332013-06-12 10:44:40 +09001326 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001327
1328 /* put event */
1329 ipp_put_event(c_node, NULL);
1330
YoungJun Cho220db6f2014-05-26 10:17:20 +02001331 mutex_lock(&c_node->mem_lock);
1332
Eunchul Kimcb471f142012-12-14 18:10:31 +09001333 /* check command */
1334 switch (property->cmd) {
1335 case IPP_CMD_M2M:
1336 for_each_ipp_ops(i) {
1337 /* source/destination memory list */
1338 head = &c_node->mem_list[i];
1339
Eunchul Kimcb471f142012-12-14 18:10:31 +09001340 list_for_each_entry_safe(m_node, tm_node,
1341 head, list) {
1342 ret = ipp_put_mem_node(drm_dev, c_node,
1343 m_node);
1344 if (ret) {
1345 DRM_ERROR("failed to put m_node.\n");
1346 goto err_clear;
1347 }
1348 }
1349 }
1350 break;
1351 case IPP_CMD_WB:
1352 /* destination memory list */
1353 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1354
Eunchul Kimcb471f142012-12-14 18:10:31 +09001355 list_for_each_entry_safe(m_node, tm_node, head, list) {
1356 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1357 if (ret) {
1358 DRM_ERROR("failed to put m_node.\n");
1359 goto err_clear;
1360 }
1361 }
1362 break;
1363 case IPP_CMD_OUTPUT:
1364 /* source memory list */
1365 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1366
Eunchul Kimcb471f142012-12-14 18:10:31 +09001367 list_for_each_entry_safe(m_node, tm_node, head, list) {
1368 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1369 if (ret) {
1370 DRM_ERROR("failed to put m_node.\n");
1371 goto err_clear;
1372 }
1373 }
1374 break;
1375 default:
1376 DRM_ERROR("invalid operations.\n");
1377 ret = -EINVAL;
1378 goto err_clear;
1379 }
1380
1381err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001382 mutex_unlock(&c_node->mem_lock);
1383
Eunchul Kimcb471f142012-12-14 18:10:31 +09001384 /* stop operations */
1385 if (ippdrv->stop)
1386 ippdrv->stop(ippdrv->dev, property->cmd);
1387
1388 return ret;
1389}
1390
1391void ipp_sched_cmd(struct work_struct *work)
1392{
1393 struct drm_exynos_ipp_cmd_work *cmd_work =
1394 (struct drm_exynos_ipp_cmd_work *)work;
1395 struct exynos_drm_ippdrv *ippdrv;
1396 struct drm_exynos_ipp_cmd_node *c_node;
1397 struct drm_exynos_ipp_property *property;
1398 int ret;
1399
Eunchul Kimcb471f142012-12-14 18:10:31 +09001400 ippdrv = cmd_work->ippdrv;
1401 if (!ippdrv) {
1402 DRM_ERROR("invalid ippdrv list.\n");
1403 return;
1404 }
1405
1406 c_node = cmd_work->c_node;
1407 if (!c_node) {
1408 DRM_ERROR("invalid command node list.\n");
1409 return;
1410 }
1411
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001412 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001413
1414 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001415
1416 switch (cmd_work->ctrl) {
1417 case IPP_CTRL_PLAY:
1418 case IPP_CTRL_RESUME:
1419 ret = ipp_start_property(ippdrv, c_node);
1420 if (ret) {
1421 DRM_ERROR("failed to start property:prop_id[%d]\n",
1422 c_node->property.prop_id);
1423 goto err_unlock;
1424 }
1425
1426 /*
1427 * M2M case supports wait_completion of transfer.
1428 * because M2M case supports single unit operation
1429 * with multiple queue.
1430 * M2M need to wait completion of data transfer.
1431 */
1432 if (ipp_is_m2m_cmd(property->cmd)) {
1433 if (!wait_for_completion_timeout
1434 (&c_node->start_complete, msecs_to_jiffies(200))) {
1435 DRM_ERROR("timeout event:prop_id[%d]\n",
1436 c_node->property.prop_id);
1437 goto err_unlock;
1438 }
1439 }
1440 break;
1441 case IPP_CTRL_STOP:
1442 case IPP_CTRL_PAUSE:
1443 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1444 c_node);
1445 if (ret) {
1446 DRM_ERROR("failed to stop property.\n");
1447 goto err_unlock;
1448 }
1449
1450 complete(&c_node->stop_complete);
1451 break;
1452 default:
1453 DRM_ERROR("unknown control type\n");
1454 break;
1455 }
1456
YoungJun Chocbc4c332013-06-12 10:44:40 +09001457 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001458
1459err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001460 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001461}
1462
1463static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1464 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1465{
1466 struct drm_device *drm_dev = ippdrv->drm_dev;
1467 struct drm_exynos_ipp_property *property = &c_node->property;
1468 struct drm_exynos_ipp_mem_node *m_node;
1469 struct drm_exynos_ipp_queue_buf qbuf;
1470 struct drm_exynos_ipp_send_event *e;
1471 struct list_head *head;
1472 struct timeval now;
1473 unsigned long flags;
1474 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1475 int ret, i;
1476
1477 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001478 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001479
1480 if (!drm_dev) {
1481 DRM_ERROR("failed to get drm_dev.\n");
1482 return -EINVAL;
1483 }
1484
1485 if (!property) {
1486 DRM_ERROR("failed to get property.\n");
1487 return -EINVAL;
1488 }
1489
YoungJun Cho4d520762014-05-26 10:17:21 +02001490 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001491 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001492 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001493 ret = 0;
1494 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001495 }
1496
YoungJun Cho220db6f2014-05-26 10:17:20 +02001497 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001498 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001499 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001500 ret = 0;
1501 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001502 }
1503
1504 /* check command */
1505 switch (property->cmd) {
1506 case IPP_CMD_M2M:
1507 for_each_ipp_ops(i) {
1508 /* source/destination memory list */
1509 head = &c_node->mem_list[i];
1510
1511 m_node = list_first_entry(head,
1512 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001513
1514 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001515 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001516 i ? "dst" : "src", tbuf_id[i]);
1517
1518 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1519 if (ret)
1520 DRM_ERROR("failed to put m_node.\n");
1521 }
1522 break;
1523 case IPP_CMD_WB:
1524 /* clear buf for finding */
1525 memset(&qbuf, 0x0, sizeof(qbuf));
1526 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1527 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1528
1529 /* get memory node entry */
1530 m_node = ipp_find_mem_node(c_node, &qbuf);
1531 if (!m_node) {
1532 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001533 ret = -ENOMEM;
1534 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001535 }
1536
1537 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1538
1539 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1540 if (ret)
1541 DRM_ERROR("failed to put m_node.\n");
1542 break;
1543 case IPP_CMD_OUTPUT:
1544 /* source memory list */
1545 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1546
1547 m_node = list_first_entry(head,
1548 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001549
1550 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1551
1552 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1553 if (ret)
1554 DRM_ERROR("failed to put m_node.\n");
1555 break;
1556 default:
1557 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001558 ret = -EINVAL;
1559 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001560 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001561 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001562
1563 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1564 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1565 tbuf_id[1], buf_id[1], property->prop_id);
1566
1567 /*
1568 * command node have event list of destination buffer
1569 * If destination buffer enqueue to mem list,
1570 * then we make event and link to event list tail.
1571 * so, we get first event for first enqueued buffer.
1572 */
1573 e = list_first_entry(&c_node->event_list,
1574 struct drm_exynos_ipp_send_event, base.link);
1575
Eunchul Kimcb471f142012-12-14 18:10:31 +09001576 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001577 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001578 e->event.tv_sec = now.tv_sec;
1579 e->event.tv_usec = now.tv_usec;
1580 e->event.prop_id = property->prop_id;
1581
1582 /* set buffer id about source destination */
1583 for_each_ipp_ops(i)
1584 e->event.buf_id[i] = tbuf_id[i];
1585
1586 spin_lock_irqsave(&drm_dev->event_lock, flags);
1587 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1588 wake_up_interruptible(&e->base.file_priv->event_wait);
1589 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001590 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001591
YoungJun Chocbc4c332013-06-12 10:44:40 +09001592 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001593 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1594
1595 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001596
1597err_mem_unlock:
1598 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001599err_event_unlock:
1600 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001601 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001602}
1603
1604void ipp_sched_event(struct work_struct *work)
1605{
1606 struct drm_exynos_ipp_event_work *event_work =
1607 (struct drm_exynos_ipp_event_work *)work;
1608 struct exynos_drm_ippdrv *ippdrv;
1609 struct drm_exynos_ipp_cmd_node *c_node;
1610 int ret;
1611
1612 if (!event_work) {
1613 DRM_ERROR("failed to get event_work.\n");
1614 return;
1615 }
1616
YoungJun Chocbc4c332013-06-12 10:44:40 +09001617 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001618
1619 ippdrv = event_work->ippdrv;
1620 if (!ippdrv) {
1621 DRM_ERROR("failed to get ipp driver.\n");
1622 return;
1623 }
1624
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001625 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001626 if (!c_node) {
1627 DRM_ERROR("failed to get command node.\n");
1628 return;
1629 }
1630
1631 /*
1632 * IPP supports command thread, event thread synchronization.
1633 * If IPP close immediately from user land, then IPP make
1634 * synchronization with command thread, so make complete event.
1635 * or going out operations.
1636 */
1637 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001638 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1639 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001640 goto err_completion;
1641 }
1642
Eunchul Kimcb471f142012-12-14 18:10:31 +09001643 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1644 if (ret) {
1645 DRM_ERROR("failed to send event.\n");
1646 goto err_completion;
1647 }
1648
1649err_completion:
1650 if (ipp_is_m2m_cmd(c_node->property.cmd))
1651 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001652}
1653
1654static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1655{
1656 struct ipp_context *ctx = get_ipp_context(dev);
1657 struct exynos_drm_ippdrv *ippdrv;
1658 int ret, count = 0;
1659
Eunchul Kimcb471f142012-12-14 18:10:31 +09001660 /* get ipp driver entry */
1661 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001662 u32 ipp_id;
1663
Eunchul Kimcb471f142012-12-14 18:10:31 +09001664 ippdrv->drm_dev = drm_dev;
1665
1666 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001667 &ipp_id);
1668 if (ret || ipp_id == 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001669 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001670 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001671 }
1672
YoungJun Chocbc4c332013-06-12 10:44:40 +09001673 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001674 count++, (int)ippdrv, ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001675
Andrzej Hajda31646052014-05-19 12:54:05 +02001676 ippdrv->prop_list.ipp_id = ipp_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001677
1678 /* store parent device for node */
1679 ippdrv->parent_dev = dev;
1680
1681 /* store event work queue and handler */
1682 ippdrv->event_workq = ctx->event_workq;
1683 ippdrv->sched_event = ipp_sched_event;
1684 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001685 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001686
1687 if (is_drm_iommu_supported(drm_dev)) {
1688 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1689 if (ret) {
1690 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001691 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001692 }
1693 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001694 }
1695
1696 return 0;
1697
YoungJun Cho075436b2014-05-26 10:17:19 +02001698err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001699 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001700 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1701 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001702 if (is_drm_iommu_supported(drm_dev))
1703 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1704
YoungJun Cho075436b2014-05-26 10:17:19 +02001705 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1706 ippdrv->prop_list.ipp_id);
1707 }
1708
Eunchul Kimcb471f142012-12-14 18:10:31 +09001709 return ret;
1710}
1711
1712static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1713{
1714 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001715 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001716
Eunchul Kimcb471f142012-12-14 18:10:31 +09001717 /* get ipp driver entry */
1718 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001719 if (is_drm_iommu_supported(drm_dev))
1720 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1721
YoungJun Cho075436b2014-05-26 10:17:19 +02001722 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1723 ippdrv->prop_list.ipp_id);
1724
Eunchul Kimcb471f142012-12-14 18:10:31 +09001725 ippdrv->drm_dev = NULL;
1726 exynos_drm_ippdrv_unregister(ippdrv);
1727 }
1728}
1729
1730static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1731 struct drm_file *file)
1732{
1733 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001734
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001735 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001736
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001737 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001738
1739 return 0;
1740}
1741
1742static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1743 struct drm_file *file)
1744{
1745 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001746 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001747 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001748 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1749 int count = 0;
1750
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001751 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)file_priv->ipp_dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001752
Eunchul Kimcb471f142012-12-14 18:10:31 +09001753 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001754 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001755 list_for_each_entry_safe(c_node, tc_node,
1756 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001757 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1758 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001759
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001760 if (c_node->dev == file_priv->ipp_dev) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001761 /*
1762 * userland goto unnormal state. process killed.
1763 * and close the file.
1764 * so, IPP didn't called stop cmd ctrl.
1765 * so, we are make stop operation in this state.
1766 */
1767 if (c_node->state == IPP_STATE_START) {
1768 ipp_stop_property(drm_dev, ippdrv,
1769 c_node);
1770 c_node->state = IPP_STATE_STOP;
1771 }
1772
1773 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001774 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001775 if (list_empty(&ippdrv->cmd_list))
1776 pm_runtime_put_sync(ippdrv->dev);
1777 }
1778 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001779 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001780 }
1781
Eunchul Kimcb471f142012-12-14 18:10:31 +09001782 return;
1783}
1784
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001785static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001786{
1787 struct device *dev = &pdev->dev;
1788 struct ipp_context *ctx;
1789 struct exynos_drm_subdrv *subdrv;
1790 int ret;
1791
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001792 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001793 if (!ctx)
1794 return -ENOMEM;
1795
Eunchul Kimcb471f142012-12-14 18:10:31 +09001796 mutex_init(&ctx->ipp_lock);
1797 mutex_init(&ctx->prop_lock);
1798
1799 idr_init(&ctx->ipp_idr);
1800 idr_init(&ctx->prop_idr);
1801
1802 /*
1803 * create single thread for ipp event
1804 * IPP supports event thread for IPP drivers.
1805 * IPP driver send event_work to this thread.
1806 * and IPP event thread send event to user process.
1807 */
1808 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1809 if (!ctx->event_workq) {
1810 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301811 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001812 }
1813
1814 /*
1815 * create single thread for ipp command
1816 * IPP supports command thread for user process.
1817 * user process make command node using set property ioctl.
1818 * and make start_work and send this work to command thread.
1819 * and then this command thread start property.
1820 */
1821 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1822 if (!ctx->cmd_workq) {
1823 dev_err(dev, "failed to create cmd workqueue\n");
1824 ret = -EINVAL;
1825 goto err_event_workq;
1826 }
1827
1828 /* set sub driver informations */
1829 subdrv = &ctx->subdrv;
1830 subdrv->dev = dev;
1831 subdrv->probe = ipp_subdrv_probe;
1832 subdrv->remove = ipp_subdrv_remove;
1833 subdrv->open = ipp_subdrv_open;
1834 subdrv->close = ipp_subdrv_close;
1835
1836 platform_set_drvdata(pdev, ctx);
1837
1838 ret = exynos_drm_subdrv_register(subdrv);
1839 if (ret < 0) {
1840 DRM_ERROR("failed to register drm ipp device.\n");
1841 goto err_cmd_workq;
1842 }
1843
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001844 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001845
1846 return 0;
1847
1848err_cmd_workq:
1849 destroy_workqueue(ctx->cmd_workq);
1850err_event_workq:
1851 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001852 return ret;
1853}
1854
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001855static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001856{
1857 struct ipp_context *ctx = platform_get_drvdata(pdev);
1858
Eunchul Kimcb471f142012-12-14 18:10:31 +09001859 /* unregister sub driver */
1860 exynos_drm_subdrv_unregister(&ctx->subdrv);
1861
1862 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001863 idr_destroy(&ctx->ipp_idr);
1864 idr_destroy(&ctx->prop_idr);
1865
1866 mutex_destroy(&ctx->ipp_lock);
1867 mutex_destroy(&ctx->prop_lock);
1868
1869 /* destroy command, event work queue */
1870 destroy_workqueue(ctx->cmd_workq);
1871 destroy_workqueue(ctx->event_workq);
1872
Eunchul Kimcb471f142012-12-14 18:10:31 +09001873 return 0;
1874}
1875
1876static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1877{
YoungJun Chocbc4c332013-06-12 10:44:40 +09001878 DRM_DEBUG_KMS("enable[%d]\n", enable);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001879
1880 return 0;
1881}
1882
1883#ifdef CONFIG_PM_SLEEP
1884static int ipp_suspend(struct device *dev)
1885{
1886 struct ipp_context *ctx = get_ipp_context(dev);
1887
Eunchul Kimcb471f142012-12-14 18:10:31 +09001888 if (pm_runtime_suspended(dev))
1889 return 0;
1890
1891 return ipp_power_ctrl(ctx, false);
1892}
1893
1894static int ipp_resume(struct device *dev)
1895{
1896 struct ipp_context *ctx = get_ipp_context(dev);
1897
Eunchul Kimcb471f142012-12-14 18:10:31 +09001898 if (!pm_runtime_suspended(dev))
1899 return ipp_power_ctrl(ctx, true);
1900
1901 return 0;
1902}
1903#endif
1904
1905#ifdef CONFIG_PM_RUNTIME
1906static int ipp_runtime_suspend(struct device *dev)
1907{
1908 struct ipp_context *ctx = get_ipp_context(dev);
1909
Eunchul Kimcb471f142012-12-14 18:10:31 +09001910 return ipp_power_ctrl(ctx, false);
1911}
1912
1913static int ipp_runtime_resume(struct device *dev)
1914{
1915 struct ipp_context *ctx = get_ipp_context(dev);
1916
Eunchul Kimcb471f142012-12-14 18:10:31 +09001917 return ipp_power_ctrl(ctx, true);
1918}
1919#endif
1920
1921static const struct dev_pm_ops ipp_pm_ops = {
1922 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1923 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1924};
1925
1926struct platform_driver ipp_driver = {
1927 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001928 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001929 .driver = {
1930 .name = "exynos-drm-ipp",
1931 .owner = THIS_MODULE,
1932 .pm = &ipp_pm_ops,
1933 },
1934};
1935