blob: f3d8b5cf3438b6d82a706506942cd8348358faed [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
577static int ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
578{
579 struct drm_exynos_ipp_property *property = &c_node->property;
580 struct drm_exynos_ipp_mem_node *m_node;
581 struct list_head *head;
582 int ret, i, count[EXYNOS_DRM_OPS_MAX] = { 0, };
583
Eunchul Kimcb471f142012-12-14 18:10:31 +0900584 for_each_ipp_ops(i) {
585 /* source/destination memory list */
586 head = &c_node->mem_list[i];
587
Eunchul Kimcb471f142012-12-14 18:10:31 +0900588 /* find memory node entry */
589 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900590 DRM_DEBUG_KMS("%s,count[%d]m_node[0x%x]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900591 i ? "dst" : "src", count[i], (int)m_node);
592 count[i]++;
593 }
594 }
595
YoungJun Chocbc4c332013-06-12 10:44:40 +0900596 DRM_DEBUG_KMS("min[%d]max[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900597 min(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]),
598 max(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]));
599
600 /*
601 * M2M operations should be need paired memory address.
602 * so, need to check minimum count about src, dst.
603 * other case not use paired memory, so use maximum count
604 */
605 if (ipp_is_m2m_cmd(property->cmd))
606 ret = min(count[EXYNOS_DRM_OPS_SRC],
607 count[EXYNOS_DRM_OPS_DST]);
608 else
609 ret = max(count[EXYNOS_DRM_OPS_SRC],
610 count[EXYNOS_DRM_OPS_DST]);
611
Eunchul Kimcb471f142012-12-14 18:10:31 +0900612 return ret;
613}
614
615static struct drm_exynos_ipp_mem_node
616 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
617 struct drm_exynos_ipp_queue_buf *qbuf)
618{
619 struct drm_exynos_ipp_mem_node *m_node;
620 struct list_head *head;
621 int count = 0;
622
YoungJun Chocbc4c332013-06-12 10:44:40 +0900623 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900624
625 /* source/destination memory list */
626 head = &c_node->mem_list[qbuf->ops_id];
627
628 /* find memory node from memory list */
629 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900630 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900631
632 /* compare buffer id */
633 if (m_node->buf_id == qbuf->buf_id)
634 return m_node;
635 }
636
637 return NULL;
638}
639
640static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
641 struct drm_exynos_ipp_cmd_node *c_node,
642 struct drm_exynos_ipp_mem_node *m_node)
643{
644 struct exynos_drm_ipp_ops *ops = NULL;
645 int ret = 0;
646
YoungJun Chocbc4c332013-06-12 10:44:40 +0900647 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900648
649 if (!m_node) {
650 DRM_ERROR("invalid queue node.\n");
651 return -EFAULT;
652 }
653
YoungJun Chocbc4c332013-06-12 10:44:40 +0900654 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900655
656 /* get operations callback */
657 ops = ippdrv->ops[m_node->ops_id];
658 if (!ops) {
659 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200660 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900661 }
662
663 /* set address and enable irq */
664 if (ops->set_addr) {
665 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
666 m_node->buf_id, IPP_BUF_ENQUEUE);
667 if (ret) {
668 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200669 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900670 }
671 }
672
Eunchul Kimcb471f142012-12-14 18:10:31 +0900673 return ret;
674}
675
676static struct drm_exynos_ipp_mem_node
677 *ipp_get_mem_node(struct drm_device *drm_dev,
678 struct drm_file *file,
679 struct drm_exynos_ipp_cmd_node *c_node,
680 struct drm_exynos_ipp_queue_buf *qbuf)
681{
682 struct drm_exynos_ipp_mem_node *m_node;
683 struct drm_exynos_ipp_buf_info buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900684 int i;
685
Eunchul Kimcb471f142012-12-14 18:10:31 +0900686 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900687 if (!m_node)
YoungJun Cho220db6f2014-05-26 10:17:20 +0200688 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900689
690 /* clear base address for error handling */
691 memset(&buf_info, 0x0, sizeof(buf_info));
692
693 /* operations, buffer id */
694 m_node->ops_id = qbuf->ops_id;
695 m_node->prop_id = qbuf->prop_id;
696 m_node->buf_id = qbuf->buf_id;
697
YoungJun Chocbc4c332013-06-12 10:44:40 +0900698 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
699 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900700
701 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900702 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900703
704 /* get dma address by handle */
705 if (qbuf->handle[i]) {
Andrzej Hajdaa8ea17f2014-07-03 15:10:29 +0200706 dma_addr_t *addr;
707
Eunchul Kimcb471f142012-12-14 18:10:31 +0900708 addr = exynos_drm_gem_get_dma_addr(drm_dev,
709 qbuf->handle[i], file);
710 if (IS_ERR(addr)) {
711 DRM_ERROR("failed to get addr.\n");
712 goto err_clear;
713 }
714
715 buf_info.handles[i] = qbuf->handle[i];
Andrzej Hajdaa8ea17f2014-07-03 15:10:29 +0200716 buf_info.base[i] = *addr;
YoungJun Chocbc4c332013-06-12 10:44:40 +0900717 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%x]\n",
718 i, buf_info.base[i], (int)buf_info.handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900719 }
720 }
721
722 m_node->filp = file;
723 m_node->buf_info = buf_info;
YoungJun Cho220db6f2014-05-26 10:17:20 +0200724 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900725 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900726 mutex_unlock(&c_node->mem_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +0200727
Eunchul Kimcb471f142012-12-14 18:10:31 +0900728 return m_node;
729
730err_clear:
731 kfree(m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900732 return ERR_PTR(-EFAULT);
733}
734
735static int ipp_put_mem_node(struct drm_device *drm_dev,
736 struct drm_exynos_ipp_cmd_node *c_node,
737 struct drm_exynos_ipp_mem_node *m_node)
738{
739 int i;
740
YoungJun Chocbc4c332013-06-12 10:44:40 +0900741 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900742
743 if (!m_node) {
744 DRM_ERROR("invalid dequeue node.\n");
745 return -EFAULT;
746 }
747
YoungJun Chocbc4c332013-06-12 10:44:40 +0900748 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900749
750 /* put gem buffer */
751 for_each_ipp_planar(i) {
752 unsigned long handle = m_node->buf_info.handles[i];
753 if (handle)
754 exynos_drm_gem_put_dma_addr(drm_dev, handle,
755 m_node->filp);
756 }
757
758 /* delete list in queue */
759 list_del(&m_node->list);
760 kfree(m_node);
761
Eunchul Kimcb471f142012-12-14 18:10:31 +0900762 return 0;
763}
764
765static void ipp_free_event(struct drm_pending_event *event)
766{
767 kfree(event);
768}
769
770static int ipp_get_event(struct drm_device *drm_dev,
771 struct drm_file *file,
772 struct drm_exynos_ipp_cmd_node *c_node,
773 struct drm_exynos_ipp_queue_buf *qbuf)
774{
775 struct drm_exynos_ipp_send_event *e;
776 unsigned long flags;
777
YoungJun Chocbc4c332013-06-12 10:44:40 +0900778 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900779
780 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900781 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900782 spin_lock_irqsave(&drm_dev->event_lock, flags);
783 file->event_space += sizeof(e->event);
784 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
785 return -ENOMEM;
786 }
787
788 /* make event */
789 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
790 e->event.base.length = sizeof(e->event);
791 e->event.user_data = qbuf->user_data;
792 e->event.prop_id = qbuf->prop_id;
793 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
794 e->base.event = &e->event.base;
795 e->base.file_priv = file;
796 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200797 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900798 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200799 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900800
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
YoungJun Cho4d520762014-05-26 10:17:21 +0200810 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900811 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900812 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900813
814 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530815 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900816 * stop operations want to delete all event list.
817 * another case delete only same buf id.
818 */
819 if (!qbuf) {
820 /* delete list */
821 list_del(&e->base.link);
822 kfree(e);
823 }
824
825 /* compare buffer id */
826 if (qbuf && (qbuf->buf_id ==
827 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
828 /* delete list */
829 list_del(&e->base.link);
830 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200831 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900832 }
833 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200834
835out_unlock:
836 mutex_unlock(&c_node->event_lock);
837 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900838}
839
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530840static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900841 struct exynos_drm_ippdrv *ippdrv,
842 struct drm_exynos_ipp_cmd_work *cmd_work,
843 struct drm_exynos_ipp_cmd_node *c_node)
844{
845 struct ipp_context *ctx = get_ipp_context(dev);
846
847 cmd_work->ippdrv = ippdrv;
848 cmd_work->c_node = c_node;
849 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
850}
851
852static int ipp_queue_buf_with_run(struct device *dev,
853 struct drm_exynos_ipp_cmd_node *c_node,
854 struct drm_exynos_ipp_mem_node *m_node,
855 struct drm_exynos_ipp_queue_buf *qbuf)
856{
857 struct exynos_drm_ippdrv *ippdrv;
858 struct drm_exynos_ipp_property *property;
859 struct exynos_drm_ipp_ops *ops;
860 int ret;
861
Eunchul Kimcb471f142012-12-14 18:10:31 +0900862 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530863 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900864 DRM_ERROR("failed to get ipp driver.\n");
865 return -EFAULT;
866 }
867
868 ops = ippdrv->ops[qbuf->ops_id];
869 if (!ops) {
870 DRM_ERROR("failed to get ops.\n");
871 return -EFAULT;
872 }
873
874 property = &c_node->property;
875
876 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900877 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900878 return 0;
879 }
880
YoungJun Cho220db6f2014-05-26 10:17:20 +0200881 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900882 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200883 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900884 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900885 return 0;
886 }
887
888 /*
889 * If set destination buffer and enabled clock,
890 * then m2m operations need start operations at queue_buf
891 */
892 if (ipp_is_m2m_cmd(property->cmd)) {
893 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
894
895 cmd_work->ctrl = IPP_CTRL_PLAY;
896 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
897 } else {
898 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
899 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200900 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900901 DRM_ERROR("failed to set m node.\n");
902 return ret;
903 }
904 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200905 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900906
907 return 0;
908}
909
910static void ipp_clean_queue_buf(struct drm_device *drm_dev,
911 struct drm_exynos_ipp_cmd_node *c_node,
912 struct drm_exynos_ipp_queue_buf *qbuf)
913{
914 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
915
YoungJun Choc66ce402014-05-26 10:17:15 +0200916 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200917 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200918 list_for_each_entry_safe(m_node, tm_node,
919 &c_node->mem_list[qbuf->ops_id], list) {
920 if (m_node->buf_id == qbuf->buf_id &&
921 m_node->ops_id == qbuf->ops_id)
922 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900923 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200924 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900925}
926
927int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
928 struct drm_file *file)
929{
930 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200931 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900932 struct ipp_context *ctx = get_ipp_context(dev);
933 struct drm_exynos_ipp_queue_buf *qbuf = data;
934 struct drm_exynos_ipp_cmd_node *c_node;
935 struct drm_exynos_ipp_mem_node *m_node;
936 int ret;
937
Eunchul Kimcb471f142012-12-14 18:10:31 +0900938 if (!qbuf) {
939 DRM_ERROR("invalid buf parameter.\n");
940 return -EINVAL;
941 }
942
943 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
944 DRM_ERROR("invalid ops parameter.\n");
945 return -EINVAL;
946 }
947
YoungJun Chocbc4c332013-06-12 10:44:40 +0900948 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
949 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900950 qbuf->buf_id, qbuf->buf_type);
951
952 /* find command node */
953 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
954 qbuf->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800955 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900956 DRM_ERROR("failed to get command node.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +0800957 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900958 }
959
960 /* buffer control */
961 switch (qbuf->buf_type) {
962 case IPP_BUF_ENQUEUE:
963 /* get memory node */
964 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
965 if (IS_ERR(m_node)) {
966 DRM_ERROR("failed to get m_node.\n");
967 return PTR_ERR(m_node);
968 }
969
970 /*
971 * first step get event for destination buffer.
972 * and second step when M2M case run with destination buffer
973 * if needed.
974 */
975 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
976 /* get event for destination buffer */
977 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
978 if (ret) {
979 DRM_ERROR("failed to get event.\n");
980 goto err_clean_node;
981 }
982
983 /*
984 * M2M case run play control for streaming feature.
985 * other case set address and waiting.
986 */
987 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
988 if (ret) {
989 DRM_ERROR("failed to run command.\n");
990 goto err_clean_node;
991 }
992 }
993 break;
994 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200995 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900996
997 /* put event for destination buffer */
998 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
999 ipp_put_event(c_node, qbuf);
1000
1001 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1002
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001003 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001004 break;
1005 default:
1006 DRM_ERROR("invalid buffer control.\n");
1007 return -EINVAL;
1008 }
1009
1010 return 0;
1011
1012err_clean_node:
1013 DRM_ERROR("clean memory nodes.\n");
1014
1015 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1016 return ret;
1017}
1018
1019static bool exynos_drm_ipp_check_valid(struct device *dev,
1020 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
1021{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001022 if (ctrl != IPP_CTRL_PLAY) {
1023 if (pm_runtime_suspended(dev)) {
1024 DRM_ERROR("pm:runtime_suspended.\n");
1025 goto err_status;
1026 }
1027 }
1028
1029 switch (ctrl) {
1030 case IPP_CTRL_PLAY:
1031 if (state != IPP_STATE_IDLE)
1032 goto err_status;
1033 break;
1034 case IPP_CTRL_STOP:
1035 if (state == IPP_STATE_STOP)
1036 goto err_status;
1037 break;
1038 case IPP_CTRL_PAUSE:
1039 if (state != IPP_STATE_START)
1040 goto err_status;
1041 break;
1042 case IPP_CTRL_RESUME:
1043 if (state != IPP_STATE_STOP)
1044 goto err_status;
1045 break;
1046 default:
1047 DRM_ERROR("invalid state.\n");
1048 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001049 }
1050
1051 return true;
1052
1053err_status:
1054 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1055 return false;
1056}
1057
1058int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1059 struct drm_file *file)
1060{
1061 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001062 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001063 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001064 struct ipp_context *ctx = get_ipp_context(dev);
1065 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1066 struct drm_exynos_ipp_cmd_work *cmd_work;
1067 struct drm_exynos_ipp_cmd_node *c_node;
1068
Eunchul Kimcb471f142012-12-14 18:10:31 +09001069 if (!ctx) {
1070 DRM_ERROR("invalid context.\n");
1071 return -EINVAL;
1072 }
1073
1074 if (!cmd_ctrl) {
1075 DRM_ERROR("invalid control parameter.\n");
1076 return -EINVAL;
1077 }
1078
YoungJun Chocbc4c332013-06-12 10:44:40 +09001079 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001080 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1081
1082 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1083 if (IS_ERR(ippdrv)) {
1084 DRM_ERROR("failed to get ipp driver.\n");
1085 return PTR_ERR(ippdrv);
1086 }
1087
1088 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1089 cmd_ctrl->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +08001090 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001091 DRM_ERROR("invalid command node list.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +08001092 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001093 }
1094
1095 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1096 c_node->state)) {
1097 DRM_ERROR("invalid state.\n");
1098 return -EINVAL;
1099 }
1100
1101 switch (cmd_ctrl->ctrl) {
1102 case IPP_CTRL_PLAY:
1103 if (pm_runtime_suspended(ippdrv->dev))
1104 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001105
Eunchul Kimcb471f142012-12-14 18:10:31 +09001106 c_node->state = IPP_STATE_START;
1107
1108 cmd_work = c_node->start_work;
1109 cmd_work->ctrl = cmd_ctrl->ctrl;
1110 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001111 break;
1112 case IPP_CTRL_STOP:
1113 cmd_work = c_node->stop_work;
1114 cmd_work->ctrl = cmd_ctrl->ctrl;
1115 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1116
1117 if (!wait_for_completion_timeout(&c_node->stop_complete,
1118 msecs_to_jiffies(300))) {
1119 DRM_ERROR("timeout stop:prop_id[%d]\n",
1120 c_node->property.prop_id);
1121 }
1122
1123 c_node->state = IPP_STATE_STOP;
1124 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001125 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001126 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001127
1128 if (list_empty(&ippdrv->cmd_list))
1129 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001130 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001131 break;
1132 case IPP_CTRL_PAUSE:
1133 cmd_work = c_node->stop_work;
1134 cmd_work->ctrl = cmd_ctrl->ctrl;
1135 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1136
1137 if (!wait_for_completion_timeout(&c_node->stop_complete,
1138 msecs_to_jiffies(200))) {
1139 DRM_ERROR("timeout stop:prop_id[%d]\n",
1140 c_node->property.prop_id);
1141 }
1142
1143 c_node->state = IPP_STATE_STOP;
1144 break;
1145 case IPP_CTRL_RESUME:
1146 c_node->state = IPP_STATE_START;
1147 cmd_work = c_node->start_work;
1148 cmd_work->ctrl = cmd_ctrl->ctrl;
1149 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1150 break;
1151 default:
1152 DRM_ERROR("could not support this state currently.\n");
1153 return -EINVAL;
1154 }
1155
YoungJun Chocbc4c332013-06-12 10:44:40 +09001156 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001157 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1158
1159 return 0;
1160}
1161
1162int exynos_drm_ippnb_register(struct notifier_block *nb)
1163{
1164 return blocking_notifier_chain_register(
1165 &exynos_drm_ippnb_list, nb);
1166}
1167
1168int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1169{
1170 return blocking_notifier_chain_unregister(
1171 &exynos_drm_ippnb_list, nb);
1172}
1173
1174int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1175{
1176 return blocking_notifier_call_chain(
1177 &exynos_drm_ippnb_list, val, v);
1178}
1179
1180static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1181 struct drm_exynos_ipp_property *property)
1182{
1183 struct exynos_drm_ipp_ops *ops = NULL;
1184 bool swap = false;
1185 int ret, i;
1186
1187 if (!property) {
1188 DRM_ERROR("invalid property parameter.\n");
1189 return -EINVAL;
1190 }
1191
YoungJun Chocbc4c332013-06-12 10:44:40 +09001192 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001193
1194 /* reset h/w block */
1195 if (ippdrv->reset &&
1196 ippdrv->reset(ippdrv->dev)) {
1197 DRM_ERROR("failed to reset.\n");
1198 return -EINVAL;
1199 }
1200
1201 /* set source,destination operations */
1202 for_each_ipp_ops(i) {
1203 struct drm_exynos_ipp_config *config =
1204 &property->config[i];
1205
1206 ops = ippdrv->ops[i];
1207 if (!ops || !config) {
1208 DRM_ERROR("not support ops and config.\n");
1209 return -EINVAL;
1210 }
1211
1212 /* set format */
1213 if (ops->set_fmt) {
1214 ret = ops->set_fmt(ippdrv->dev, config->fmt);
1215 if (ret) {
1216 DRM_ERROR("not support format.\n");
1217 return ret;
1218 }
1219 }
1220
1221 /* set transform for rotation, flip */
1222 if (ops->set_transf) {
1223 ret = ops->set_transf(ippdrv->dev, config->degree,
1224 config->flip, &swap);
1225 if (ret) {
1226 DRM_ERROR("not support tranf.\n");
1227 return -EINVAL;
1228 }
1229 }
1230
1231 /* set size */
1232 if (ops->set_size) {
1233 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1234 &config->sz);
1235 if (ret) {
1236 DRM_ERROR("not support size.\n");
1237 return ret;
1238 }
1239 }
1240 }
1241
1242 return 0;
1243}
1244
1245static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1246 struct drm_exynos_ipp_cmd_node *c_node)
1247{
1248 struct drm_exynos_ipp_mem_node *m_node;
1249 struct drm_exynos_ipp_property *property = &c_node->property;
1250 struct list_head *head;
1251 int ret, i;
1252
YoungJun Chocbc4c332013-06-12 10:44:40 +09001253 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001254
1255 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001256 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001257
YoungJun Cho220db6f2014-05-26 10:17:20 +02001258 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001259 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001260 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001261 ret = -ENOMEM;
1262 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001263 }
1264
1265 /* set current property in ippdrv */
1266 ret = ipp_set_property(ippdrv, property);
1267 if (ret) {
1268 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001269 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001270 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001271 }
1272
1273 /* check command */
1274 switch (property->cmd) {
1275 case IPP_CMD_M2M:
1276 for_each_ipp_ops(i) {
1277 /* source/destination memory list */
1278 head = &c_node->mem_list[i];
1279
1280 m_node = list_first_entry(head,
1281 struct drm_exynos_ipp_mem_node, list);
1282 if (!m_node) {
1283 DRM_ERROR("failed to get node.\n");
1284 ret = -EFAULT;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001285 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001286 }
1287
YoungJun Chocbc4c332013-06-12 10:44:40 +09001288 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001289
1290 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1291 if (ret) {
1292 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001293 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001294 }
1295 }
1296 break;
1297 case IPP_CMD_WB:
1298 /* destination memory list */
1299 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1300
1301 list_for_each_entry(m_node, head, list) {
1302 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1303 if (ret) {
1304 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001305 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001306 }
1307 }
1308 break;
1309 case IPP_CMD_OUTPUT:
1310 /* source memory list */
1311 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1312
1313 list_for_each_entry(m_node, head, list) {
1314 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1315 if (ret) {
1316 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001317 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001318 }
1319 }
1320 break;
1321 default:
1322 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001323 ret = -EINVAL;
1324 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001325 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001326 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001327
YoungJun Chocbc4c332013-06-12 10:44:40 +09001328 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001329
1330 /* start operations */
1331 if (ippdrv->start) {
1332 ret = ippdrv->start(ippdrv->dev, property->cmd);
1333 if (ret) {
1334 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001335 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001336 return ret;
1337 }
1338 }
1339
1340 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001341
1342err_unlock:
1343 mutex_unlock(&c_node->mem_lock);
1344 ippdrv->c_node = NULL;
1345 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001346}
1347
1348static int ipp_stop_property(struct drm_device *drm_dev,
1349 struct exynos_drm_ippdrv *ippdrv,
1350 struct drm_exynos_ipp_cmd_node *c_node)
1351{
1352 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1353 struct drm_exynos_ipp_property *property = &c_node->property;
1354 struct list_head *head;
1355 int ret = 0, i;
1356
YoungJun Chocbc4c332013-06-12 10:44:40 +09001357 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001358
1359 /* put event */
1360 ipp_put_event(c_node, NULL);
1361
YoungJun Cho220db6f2014-05-26 10:17:20 +02001362 mutex_lock(&c_node->mem_lock);
1363
Eunchul Kimcb471f142012-12-14 18:10:31 +09001364 /* check command */
1365 switch (property->cmd) {
1366 case IPP_CMD_M2M:
1367 for_each_ipp_ops(i) {
1368 /* source/destination memory list */
1369 head = &c_node->mem_list[i];
1370
Eunchul Kimcb471f142012-12-14 18:10:31 +09001371 list_for_each_entry_safe(m_node, tm_node,
1372 head, list) {
1373 ret = ipp_put_mem_node(drm_dev, c_node,
1374 m_node);
1375 if (ret) {
1376 DRM_ERROR("failed to put m_node.\n");
1377 goto err_clear;
1378 }
1379 }
1380 }
1381 break;
1382 case IPP_CMD_WB:
1383 /* destination memory list */
1384 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1385
Eunchul Kimcb471f142012-12-14 18:10:31 +09001386 list_for_each_entry_safe(m_node, tm_node, head, list) {
1387 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1388 if (ret) {
1389 DRM_ERROR("failed to put m_node.\n");
1390 goto err_clear;
1391 }
1392 }
1393 break;
1394 case IPP_CMD_OUTPUT:
1395 /* source memory list */
1396 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1397
Eunchul Kimcb471f142012-12-14 18:10:31 +09001398 list_for_each_entry_safe(m_node, tm_node, head, list) {
1399 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1400 if (ret) {
1401 DRM_ERROR("failed to put m_node.\n");
1402 goto err_clear;
1403 }
1404 }
1405 break;
1406 default:
1407 DRM_ERROR("invalid operations.\n");
1408 ret = -EINVAL;
1409 goto err_clear;
1410 }
1411
1412err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001413 mutex_unlock(&c_node->mem_lock);
1414
Eunchul Kimcb471f142012-12-14 18:10:31 +09001415 /* stop operations */
1416 if (ippdrv->stop)
1417 ippdrv->stop(ippdrv->dev, property->cmd);
1418
1419 return ret;
1420}
1421
1422void ipp_sched_cmd(struct work_struct *work)
1423{
1424 struct drm_exynos_ipp_cmd_work *cmd_work =
1425 (struct drm_exynos_ipp_cmd_work *)work;
1426 struct exynos_drm_ippdrv *ippdrv;
1427 struct drm_exynos_ipp_cmd_node *c_node;
1428 struct drm_exynos_ipp_property *property;
1429 int ret;
1430
Eunchul Kimcb471f142012-12-14 18:10:31 +09001431 ippdrv = cmd_work->ippdrv;
1432 if (!ippdrv) {
1433 DRM_ERROR("invalid ippdrv list.\n");
1434 return;
1435 }
1436
1437 c_node = cmd_work->c_node;
1438 if (!c_node) {
1439 DRM_ERROR("invalid command node list.\n");
1440 return;
1441 }
1442
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001443 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001444
1445 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001446
1447 switch (cmd_work->ctrl) {
1448 case IPP_CTRL_PLAY:
1449 case IPP_CTRL_RESUME:
1450 ret = ipp_start_property(ippdrv, c_node);
1451 if (ret) {
1452 DRM_ERROR("failed to start property:prop_id[%d]\n",
1453 c_node->property.prop_id);
1454 goto err_unlock;
1455 }
1456
1457 /*
1458 * M2M case supports wait_completion of transfer.
1459 * because M2M case supports single unit operation
1460 * with multiple queue.
1461 * M2M need to wait completion of data transfer.
1462 */
1463 if (ipp_is_m2m_cmd(property->cmd)) {
1464 if (!wait_for_completion_timeout
1465 (&c_node->start_complete, msecs_to_jiffies(200))) {
1466 DRM_ERROR("timeout event:prop_id[%d]\n",
1467 c_node->property.prop_id);
1468 goto err_unlock;
1469 }
1470 }
1471 break;
1472 case IPP_CTRL_STOP:
1473 case IPP_CTRL_PAUSE:
1474 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1475 c_node);
1476 if (ret) {
1477 DRM_ERROR("failed to stop property.\n");
1478 goto err_unlock;
1479 }
1480
1481 complete(&c_node->stop_complete);
1482 break;
1483 default:
1484 DRM_ERROR("unknown control type\n");
1485 break;
1486 }
1487
YoungJun Chocbc4c332013-06-12 10:44:40 +09001488 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001489
1490err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001491 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001492}
1493
1494static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1495 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1496{
1497 struct drm_device *drm_dev = ippdrv->drm_dev;
1498 struct drm_exynos_ipp_property *property = &c_node->property;
1499 struct drm_exynos_ipp_mem_node *m_node;
1500 struct drm_exynos_ipp_queue_buf qbuf;
1501 struct drm_exynos_ipp_send_event *e;
1502 struct list_head *head;
1503 struct timeval now;
1504 unsigned long flags;
1505 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1506 int ret, i;
1507
1508 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001509 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001510
1511 if (!drm_dev) {
1512 DRM_ERROR("failed to get drm_dev.\n");
1513 return -EINVAL;
1514 }
1515
1516 if (!property) {
1517 DRM_ERROR("failed to get property.\n");
1518 return -EINVAL;
1519 }
1520
YoungJun Cho4d520762014-05-26 10:17:21 +02001521 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001522 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001523 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001524 ret = 0;
1525 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001526 }
1527
YoungJun Cho220db6f2014-05-26 10:17:20 +02001528 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001529 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001530 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001531 ret = 0;
1532 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001533 }
1534
1535 /* check command */
1536 switch (property->cmd) {
1537 case IPP_CMD_M2M:
1538 for_each_ipp_ops(i) {
1539 /* source/destination memory list */
1540 head = &c_node->mem_list[i];
1541
1542 m_node = list_first_entry(head,
1543 struct drm_exynos_ipp_mem_node, list);
1544 if (!m_node) {
1545 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001546 ret = -ENOMEM;
1547 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001548 }
1549
1550 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001551 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001552 i ? "dst" : "src", tbuf_id[i]);
1553
1554 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1555 if (ret)
1556 DRM_ERROR("failed to put m_node.\n");
1557 }
1558 break;
1559 case IPP_CMD_WB:
1560 /* clear buf for finding */
1561 memset(&qbuf, 0x0, sizeof(qbuf));
1562 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1563 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1564
1565 /* get memory node entry */
1566 m_node = ipp_find_mem_node(c_node, &qbuf);
1567 if (!m_node) {
1568 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001569 ret = -ENOMEM;
1570 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001571 }
1572
1573 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1574
1575 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1576 if (ret)
1577 DRM_ERROR("failed to put m_node.\n");
1578 break;
1579 case IPP_CMD_OUTPUT:
1580 /* source memory list */
1581 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1582
1583 m_node = list_first_entry(head,
1584 struct drm_exynos_ipp_mem_node, list);
1585 if (!m_node) {
1586 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001587 ret = -ENOMEM;
1588 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001589 }
1590
1591 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1592
1593 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1594 if (ret)
1595 DRM_ERROR("failed to put m_node.\n");
1596 break;
1597 default:
1598 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001599 ret = -EINVAL;
1600 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001601 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001602 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001603
1604 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1605 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1606 tbuf_id[1], buf_id[1], property->prop_id);
1607
1608 /*
1609 * command node have event list of destination buffer
1610 * If destination buffer enqueue to mem list,
1611 * then we make event and link to event list tail.
1612 * so, we get first event for first enqueued buffer.
1613 */
1614 e = list_first_entry(&c_node->event_list,
1615 struct drm_exynos_ipp_send_event, base.link);
1616
Eunchul Kimcb471f142012-12-14 18:10:31 +09001617 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);
YoungJun Cho4d520762014-05-26 10:17:21 +02001631 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001632
YoungJun Chocbc4c332013-06-12 10:44:40 +09001633 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001634 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1635
1636 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001637
1638err_mem_unlock:
1639 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001640err_event_unlock:
1641 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001642 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001643}
1644
1645void ipp_sched_event(struct work_struct *work)
1646{
1647 struct drm_exynos_ipp_event_work *event_work =
1648 (struct drm_exynos_ipp_event_work *)work;
1649 struct exynos_drm_ippdrv *ippdrv;
1650 struct drm_exynos_ipp_cmd_node *c_node;
1651 int ret;
1652
1653 if (!event_work) {
1654 DRM_ERROR("failed to get event_work.\n");
1655 return;
1656 }
1657
YoungJun Chocbc4c332013-06-12 10:44:40 +09001658 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001659
1660 ippdrv = event_work->ippdrv;
1661 if (!ippdrv) {
1662 DRM_ERROR("failed to get ipp driver.\n");
1663 return;
1664 }
1665
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001666 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001667 if (!c_node) {
1668 DRM_ERROR("failed to get command node.\n");
1669 return;
1670 }
1671
1672 /*
1673 * IPP supports command thread, event thread synchronization.
1674 * If IPP close immediately from user land, then IPP make
1675 * synchronization with command thread, so make complete event.
1676 * or going out operations.
1677 */
1678 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001679 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1680 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001681 goto err_completion;
1682 }
1683
Eunchul Kimcb471f142012-12-14 18:10:31 +09001684 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1685 if (ret) {
1686 DRM_ERROR("failed to send event.\n");
1687 goto err_completion;
1688 }
1689
1690err_completion:
1691 if (ipp_is_m2m_cmd(c_node->property.cmd))
1692 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001693}
1694
1695static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1696{
1697 struct ipp_context *ctx = get_ipp_context(dev);
1698 struct exynos_drm_ippdrv *ippdrv;
1699 int ret, count = 0;
1700
Eunchul Kimcb471f142012-12-14 18:10:31 +09001701 /* get ipp driver entry */
1702 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001703 u32 ipp_id;
1704
Eunchul Kimcb471f142012-12-14 18:10:31 +09001705 ippdrv->drm_dev = drm_dev;
1706
1707 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001708 &ipp_id);
1709 if (ret || ipp_id == 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001710 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001711 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001712 }
1713
YoungJun Chocbc4c332013-06-12 10:44:40 +09001714 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001715 count++, (int)ippdrv, ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001716
Andrzej Hajda31646052014-05-19 12:54:05 +02001717 ippdrv->prop_list.ipp_id = ipp_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001718
1719 /* store parent device for node */
1720 ippdrv->parent_dev = dev;
1721
1722 /* store event work queue and handler */
1723 ippdrv->event_workq = ctx->event_workq;
1724 ippdrv->sched_event = ipp_sched_event;
1725 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001726 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001727
1728 if (is_drm_iommu_supported(drm_dev)) {
1729 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1730 if (ret) {
1731 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001732 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001733 }
1734 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001735 }
1736
1737 return 0;
1738
YoungJun Cho075436b2014-05-26 10:17:19 +02001739err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001740 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001741 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1742 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001743 if (is_drm_iommu_supported(drm_dev))
1744 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1745
YoungJun Cho075436b2014-05-26 10:17:19 +02001746 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1747 ippdrv->prop_list.ipp_id);
1748 }
1749
Eunchul Kimcb471f142012-12-14 18:10:31 +09001750 return ret;
1751}
1752
1753static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1754{
1755 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001756 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001757
Eunchul Kimcb471f142012-12-14 18:10:31 +09001758 /* get ipp driver entry */
1759 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001760 if (is_drm_iommu_supported(drm_dev))
1761 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1762
YoungJun Cho075436b2014-05-26 10:17:19 +02001763 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1764 ippdrv->prop_list.ipp_id);
1765
Eunchul Kimcb471f142012-12-14 18:10:31 +09001766 ippdrv->drm_dev = NULL;
1767 exynos_drm_ippdrv_unregister(ippdrv);
1768 }
1769}
1770
1771static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1772 struct drm_file *file)
1773{
1774 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001775
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001776 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001777
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001778 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001779
1780 return 0;
1781}
1782
1783static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1784 struct drm_file *file)
1785{
1786 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001787 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001788 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001789 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1790 int count = 0;
1791
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001792 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)file_priv->ipp_dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001793
Eunchul Kimcb471f142012-12-14 18:10:31 +09001794 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001795 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001796 list_for_each_entry_safe(c_node, tc_node,
1797 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001798 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1799 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001800
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001801 if (c_node->dev == file_priv->ipp_dev) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001802 /*
1803 * userland goto unnormal state. process killed.
1804 * and close the file.
1805 * so, IPP didn't called stop cmd ctrl.
1806 * so, we are make stop operation in this state.
1807 */
1808 if (c_node->state == IPP_STATE_START) {
1809 ipp_stop_property(drm_dev, ippdrv,
1810 c_node);
1811 c_node->state = IPP_STATE_STOP;
1812 }
1813
1814 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001815 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001816 if (list_empty(&ippdrv->cmd_list))
1817 pm_runtime_put_sync(ippdrv->dev);
1818 }
1819 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001820 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001821 }
1822
Eunchul Kimcb471f142012-12-14 18:10:31 +09001823 return;
1824}
1825
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001826static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001827{
1828 struct device *dev = &pdev->dev;
1829 struct ipp_context *ctx;
1830 struct exynos_drm_subdrv *subdrv;
1831 int ret;
1832
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001833 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001834 if (!ctx)
1835 return -ENOMEM;
1836
Eunchul Kimcb471f142012-12-14 18:10:31 +09001837 mutex_init(&ctx->ipp_lock);
1838 mutex_init(&ctx->prop_lock);
1839
1840 idr_init(&ctx->ipp_idr);
1841 idr_init(&ctx->prop_idr);
1842
1843 /*
1844 * create single thread for ipp event
1845 * IPP supports event thread for IPP drivers.
1846 * IPP driver send event_work to this thread.
1847 * and IPP event thread send event to user process.
1848 */
1849 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1850 if (!ctx->event_workq) {
1851 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301852 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001853 }
1854
1855 /*
1856 * create single thread for ipp command
1857 * IPP supports command thread for user process.
1858 * user process make command node using set property ioctl.
1859 * and make start_work and send this work to command thread.
1860 * and then this command thread start property.
1861 */
1862 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1863 if (!ctx->cmd_workq) {
1864 dev_err(dev, "failed to create cmd workqueue\n");
1865 ret = -EINVAL;
1866 goto err_event_workq;
1867 }
1868
1869 /* set sub driver informations */
1870 subdrv = &ctx->subdrv;
1871 subdrv->dev = dev;
1872 subdrv->probe = ipp_subdrv_probe;
1873 subdrv->remove = ipp_subdrv_remove;
1874 subdrv->open = ipp_subdrv_open;
1875 subdrv->close = ipp_subdrv_close;
1876
1877 platform_set_drvdata(pdev, ctx);
1878
1879 ret = exynos_drm_subdrv_register(subdrv);
1880 if (ret < 0) {
1881 DRM_ERROR("failed to register drm ipp device.\n");
1882 goto err_cmd_workq;
1883 }
1884
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001885 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001886
1887 return 0;
1888
1889err_cmd_workq:
1890 destroy_workqueue(ctx->cmd_workq);
1891err_event_workq:
1892 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001893 return ret;
1894}
1895
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001896static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001897{
1898 struct ipp_context *ctx = platform_get_drvdata(pdev);
1899
Eunchul Kimcb471f142012-12-14 18:10:31 +09001900 /* unregister sub driver */
1901 exynos_drm_subdrv_unregister(&ctx->subdrv);
1902
1903 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001904 idr_destroy(&ctx->ipp_idr);
1905 idr_destroy(&ctx->prop_idr);
1906
1907 mutex_destroy(&ctx->ipp_lock);
1908 mutex_destroy(&ctx->prop_lock);
1909
1910 /* destroy command, event work queue */
1911 destroy_workqueue(ctx->cmd_workq);
1912 destroy_workqueue(ctx->event_workq);
1913
Eunchul Kimcb471f142012-12-14 18:10:31 +09001914 return 0;
1915}
1916
1917static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1918{
YoungJun Chocbc4c332013-06-12 10:44:40 +09001919 DRM_DEBUG_KMS("enable[%d]\n", enable);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001920
1921 return 0;
1922}
1923
1924#ifdef CONFIG_PM_SLEEP
1925static int ipp_suspend(struct device *dev)
1926{
1927 struct ipp_context *ctx = get_ipp_context(dev);
1928
Eunchul Kimcb471f142012-12-14 18:10:31 +09001929 if (pm_runtime_suspended(dev))
1930 return 0;
1931
1932 return ipp_power_ctrl(ctx, false);
1933}
1934
1935static int ipp_resume(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 ipp_power_ctrl(ctx, true);
1941
1942 return 0;
1943}
1944#endif
1945
1946#ifdef CONFIG_PM_RUNTIME
1947static int ipp_runtime_suspend(struct device *dev)
1948{
1949 struct ipp_context *ctx = get_ipp_context(dev);
1950
Eunchul Kimcb471f142012-12-14 18:10:31 +09001951 return ipp_power_ctrl(ctx, false);
1952}
1953
1954static int ipp_runtime_resume(struct device *dev)
1955{
1956 struct ipp_context *ctx = get_ipp_context(dev);
1957
Eunchul Kimcb471f142012-12-14 18:10:31 +09001958 return ipp_power_ctrl(ctx, true);
1959}
1960#endif
1961
1962static const struct dev_pm_ops ipp_pm_ops = {
1963 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1964 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1965};
1966
1967struct platform_driver ipp_driver = {
1968 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001969 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001970 .driver = {
1971 .name = "exynos-drm-ipp",
1972 .owner = THIS_MODULE,
1973 .pm = &ipp_pm_ops,
1974 },
1975};
1976