blob: ab7b74cd4daad20f755b673b766b5e2f23b1efc6 [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;
Eunchul Kimcb471f142012-12-14 18:10:31 +090078};
79
80/*
81 * A structure of ipp context.
82 *
83 * @subdrv: prepare initialization using subdrv.
84 * @ipp_lock: lock for synchronization of access to ipp_idr.
85 * @prop_lock: lock for synchronization of access to prop_idr.
86 * @ipp_idr: ipp driver idr.
87 * @prop_idr: property idr.
88 * @event_workq: event work queue.
89 * @cmd_workq: command work queue.
90 */
91struct ipp_context {
92 struct exynos_drm_subdrv subdrv;
93 struct mutex ipp_lock;
94 struct mutex prop_lock;
95 struct idr ipp_idr;
96 struct idr prop_idr;
97 struct workqueue_struct *event_workq;
98 struct workqueue_struct *cmd_workq;
99};
100
101static LIST_HEAD(exynos_drm_ippdrv_list);
102static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
103static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
104
Seung-Woo Kim43f41902013-04-23 14:02:53 +0900105int exynos_platform_device_ipp_register(void)
106{
107 struct platform_device *pdev;
108
109 if (exynos_drm_ipp_pdev)
110 return -EEXIST;
111
112 pdev = platform_device_register_simple("exynos-drm-ipp", -1, NULL, 0);
113 if (IS_ERR(pdev))
114 return PTR_ERR(pdev);
115
116 exynos_drm_ipp_pdev = pdev;
117
118 return 0;
119}
120
121void exynos_platform_device_ipp_unregister(void)
122{
123 if (exynos_drm_ipp_pdev) {
124 platform_device_unregister(exynos_drm_ipp_pdev);
125 exynos_drm_ipp_pdev = NULL;
126 }
127}
128
Eunchul Kimcb471f142012-12-14 18:10:31 +0900129int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
130{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900131 mutex_lock(&exynos_drm_ippdrv_lock);
132 list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
133 mutex_unlock(&exynos_drm_ippdrv_lock);
134
135 return 0;
136}
137
138int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
139{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900140 mutex_lock(&exynos_drm_ippdrv_lock);
141 list_del(&ippdrv->drv_list);
142 mutex_unlock(&exynos_drm_ippdrv_lock);
143
144 return 0;
145}
146
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200147static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900148{
149 int ret;
150
Eunchul Kimcb471f142012-12-14 18:10:31 +0900151 mutex_lock(lock);
Tejun Heo8550cb22013-02-27 17:04:09 -0800152 ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900153 mutex_unlock(lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900154
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200155 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900156}
157
YoungJun Cho075436b2014-05-26 10:17:19 +0200158static void ipp_remove_id(struct idr *id_idr, struct mutex *lock, u32 id)
159{
160 mutex_lock(lock);
161 idr_remove(id_idr, id);
162 mutex_unlock(lock);
163}
164
Eunchul Kimcb471f142012-12-14 18:10:31 +0900165static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
166{
167 void *obj;
168
Eunchul Kimcb471f142012-12-14 18:10:31 +0900169 mutex_lock(lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900170 obj = idr_find(id_idr, id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900171 mutex_unlock(lock);
172
173 return obj;
174}
175
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200176static int ipp_check_driver(struct exynos_drm_ippdrv *ippdrv,
177 struct drm_exynos_ipp_property *property)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900178{
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200179 if (ippdrv->dedicated || (!ipp_is_m2m_cmd(property->cmd) &&
180 !pm_runtime_suspended(ippdrv->dev)))
181 return -EBUSY;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900182
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200183 if (ippdrv->check_property &&
184 ippdrv->check_property(ippdrv->dev, property))
185 return -EINVAL;
186
187 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900188}
189
190static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
191 struct drm_exynos_ipp_property *property)
192{
193 struct exynos_drm_ippdrv *ippdrv;
194 u32 ipp_id = property->ipp_id;
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200195 int ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900196
197 if (ipp_id) {
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200198 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock, ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200199 if (!ippdrv) {
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200200 DRM_DEBUG("ipp%d driver not found\n", ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200201 return ERR_PTR(-ENODEV);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900202 }
203
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200204 ret = ipp_check_driver(ippdrv, property);
205 if (ret < 0) {
206 DRM_DEBUG("ipp%d driver check error %d\n", ipp_id, ret);
207 return ERR_PTR(ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900208 }
209
210 return ippdrv;
211 } else {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900212 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200213 ret = ipp_check_driver(ippdrv, property);
214 if (ret == 0)
215 return ippdrv;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900216 }
217
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200218 DRM_DEBUG("cannot find driver suitable for given property.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900219 }
220
221 return ERR_PTR(-ENODEV);
222}
223
224static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
225{
226 struct exynos_drm_ippdrv *ippdrv;
227 struct drm_exynos_ipp_cmd_node *c_node;
228 int count = 0;
229
YoungJun Chocbc4c332013-06-12 10:44:40 +0900230 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900231
Eunchul Kimcb471f142012-12-14 18:10:31 +0900232 /*
233 * This case is search ipp driver by prop_id handle.
234 * sometimes, ipp subsystem find driver by prop_id.
Geert Uytterhoeven9fca9ac2014-03-11 11:23:37 +0100235 * e.g PAUSE state, queue buf, command control.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900236 */
237 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900238 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900239
YoungJun Cho7f5af052014-05-26 10:17:18 +0200240 mutex_lock(&ippdrv->cmd_lock);
241 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
242 if (c_node->property.prop_id == prop_id) {
243 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200244 return ippdrv;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200245 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900246 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200247 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900248 }
249
250 return ERR_PTR(-ENODEV);
251}
252
253int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
254 struct drm_file *file)
255{
256 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200257 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900258 struct ipp_context *ctx = get_ipp_context(dev);
259 struct drm_exynos_ipp_prop_list *prop_list = data;
260 struct exynos_drm_ippdrv *ippdrv;
261 int count = 0;
262
Eunchul Kimcb471f142012-12-14 18:10:31 +0900263 if (!ctx) {
264 DRM_ERROR("invalid context.\n");
265 return -EINVAL;
266 }
267
268 if (!prop_list) {
269 DRM_ERROR("invalid property parameter.\n");
270 return -EINVAL;
271 }
272
YoungJun Chocbc4c332013-06-12 10:44:40 +0900273 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900274
275 if (!prop_list->ipp_id) {
276 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
277 count++;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200278
Eunchul Kimcb471f142012-12-14 18:10:31 +0900279 /*
280 * Supports ippdrv list count for user application.
281 * First step user application getting ippdrv count.
282 * and second step getting ippdrv capability using ipp_id.
283 */
284 prop_list->count = count;
285 } else {
286 /*
287 * Getting ippdrv capability by ipp_id.
Masanari Iidac6b78bc2013-10-24 16:02:57 +0900288 * some device not supported wb, output interface.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900289 * so, user application detect correct ipp driver
290 * using this ioctl.
291 */
292 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
293 prop_list->ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200294 if (!ippdrv) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900295 DRM_ERROR("not found ipp%d driver.\n",
296 prop_list->ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200297 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900298 }
299
Andrzej Hajda31646052014-05-19 12:54:05 +0200300 *prop_list = ippdrv->prop_list;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900301 }
302
303 return 0;
304}
305
306static void ipp_print_property(struct drm_exynos_ipp_property *property,
307 int idx)
308{
309 struct drm_exynos_ipp_config *config = &property->config[idx];
310 struct drm_exynos_pos *pos = &config->pos;
311 struct drm_exynos_sz *sz = &config->sz;
312
YoungJun Chocbc4c332013-06-12 10:44:40 +0900313 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
314 property->prop_id, idx ? "dst" : "src", config->fmt);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900315
YoungJun Chocbc4c332013-06-12 10:44:40 +0900316 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
317 pos->x, pos->y, pos->w, pos->h,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900318 sz->hsize, sz->vsize, config->flip, config->degree);
319}
320
321static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
322{
323 struct exynos_drm_ippdrv *ippdrv;
324 struct drm_exynos_ipp_cmd_node *c_node;
325 u32 prop_id = property->prop_id;
326
YoungJun Chocbc4c332013-06-12 10:44:40 +0900327 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900328
329 ippdrv = ipp_find_drv_by_handle(prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530330 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900331 DRM_ERROR("failed to get ipp driver.\n");
332 return -EINVAL;
333 }
334
335 /*
336 * Find command node using command list in ippdrv.
337 * when we find this command no using prop_id.
338 * return property information set in this command node.
339 */
YoungJun Cho7f5af052014-05-26 10:17:18 +0200340 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900341 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
342 if ((c_node->property.prop_id == prop_id) &&
343 (c_node->state == IPP_STATE_STOP)) {
YoungJun Cho7f5af052014-05-26 10:17:18 +0200344 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900345 DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
346 property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900347
348 c_node->property = *property;
349 return 0;
350 }
351 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200352 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900353
354 DRM_ERROR("failed to search property.\n");
355
356 return -EINVAL;
357}
358
359static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
360{
361 struct drm_exynos_ipp_cmd_work *cmd_work;
362
Eunchul Kimcb471f142012-12-14 18:10:31 +0900363 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900364 if (!cmd_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900365 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900366
367 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
368
369 return cmd_work;
370}
371
372static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
373{
374 struct drm_exynos_ipp_event_work *event_work;
375
Eunchul Kimcb471f142012-12-14 18:10:31 +0900376 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900377 if (!event_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900378 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900379
Andrzej Hajda60b61c22014-07-03 15:10:26 +0200380 INIT_WORK(&event_work->work, ipp_sched_event);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900381
382 return event_work;
383}
384
385int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
386 struct drm_file *file)
387{
388 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200389 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900390 struct ipp_context *ctx = get_ipp_context(dev);
391 struct drm_exynos_ipp_property *property = data;
392 struct exynos_drm_ippdrv *ippdrv;
393 struct drm_exynos_ipp_cmd_node *c_node;
394 int ret, i;
395
Eunchul Kimcb471f142012-12-14 18:10:31 +0900396 if (!ctx) {
397 DRM_ERROR("invalid context.\n");
398 return -EINVAL;
399 }
400
401 if (!property) {
402 DRM_ERROR("invalid property parameter.\n");
403 return -EINVAL;
404 }
405
406 /*
407 * This is log print for user application property.
408 * user application set various property.
409 */
410 for_each_ipp_ops(i)
411 ipp_print_property(property, i);
412
413 /*
414 * set property ioctl generated new prop_id.
415 * but in this case already asigned prop_id using old set property.
416 * e.g PAUSE state. this case supports find current prop_id and use it
417 * instead of allocation.
418 */
419 if (property->prop_id) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900420 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900421 return ipp_find_and_set_property(property);
422 }
423
424 /* find ipp driver using ipp id */
425 ippdrv = ipp_find_driver(ctx, property);
Sachin Kamatf0250452013-04-29 12:27:06 +0530426 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900427 DRM_ERROR("failed to get ipp driver.\n");
428 return -EINVAL;
429 }
430
431 /* allocate command node */
432 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900433 if (!c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900434 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900435
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200436 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node);
437 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900438 DRM_ERROR("failed to create id.\n");
439 goto err_clear;
440 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200441 property->prop_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900442
YoungJun Chocbc4c332013-06-12 10:44:40 +0900443 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
444 property->prop_id, property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900445
446 /* stored property information and ippdrv in private data */
Eunchul Kimcb471f142012-12-14 18:10:31 +0900447 c_node->property = *property;
448 c_node->state = IPP_STATE_IDLE;
Andrzej Hajda945a0aa2014-08-28 11:07:27 +0200449 c_node->filp = file;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900450
451 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530452 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900453 DRM_ERROR("failed to create start work.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +0200454 goto err_remove_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900455 }
456
457 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530458 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900459 DRM_ERROR("failed to create stop work.\n");
460 goto err_free_start;
461 }
462
463 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530464 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900465 DRM_ERROR("failed to create event work.\n");
466 goto err_free_stop;
467 }
468
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200469 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900470 mutex_init(&c_node->mem_lock);
471 mutex_init(&c_node->event_lock);
472
473 init_completion(&c_node->start_complete);
474 init_completion(&c_node->stop_complete);
475
476 for_each_ipp_ops(i)
477 INIT_LIST_HEAD(&c_node->mem_list[i]);
478
479 INIT_LIST_HEAD(&c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200480 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900481 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200482 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900483
484 /* make dedicated state without m2m */
485 if (!ipp_is_m2m_cmd(property->cmd))
486 ippdrv->dedicated = true;
487
488 return 0;
489
490err_free_stop:
491 kfree(c_node->stop_work);
492err_free_start:
493 kfree(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200494err_remove_id:
495 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900496err_clear:
497 kfree(c_node);
498 return ret;
499}
500
YoungJun Cho075436b2014-05-26 10:17:19 +0200501static void ipp_clean_cmd_node(struct ipp_context *ctx,
502 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900503{
Andrzej Hajda6f7d48e2014-08-28 11:07:26 +0200504 /* cancel works */
505 cancel_work_sync(&c_node->start_work->work);
506 cancel_work_sync(&c_node->stop_work->work);
507 cancel_work_sync(&c_node->event_work->work);
508
Eunchul Kimcb471f142012-12-14 18:10:31 +0900509 /* delete list */
510 list_del(&c_node->list);
511
YoungJun Cho075436b2014-05-26 10:17:19 +0200512 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
513 c_node->property.prop_id);
514
Eunchul Kimcb471f142012-12-14 18:10:31 +0900515 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200516 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900517 mutex_destroy(&c_node->mem_lock);
518 mutex_destroy(&c_node->event_lock);
519
520 /* free command node */
521 kfree(c_node->start_work);
522 kfree(c_node->stop_work);
523 kfree(c_node->event_work);
524 kfree(c_node);
525}
526
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200527static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900528{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200529 switch (c_node->property.cmd) {
530 case IPP_CMD_WB:
531 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
532 case IPP_CMD_OUTPUT:
533 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
534 case IPP_CMD_M2M:
535 default:
536 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
537 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900538 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900539}
540
541static struct drm_exynos_ipp_mem_node
542 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
543 struct drm_exynos_ipp_queue_buf *qbuf)
544{
545 struct drm_exynos_ipp_mem_node *m_node;
546 struct list_head *head;
547 int count = 0;
548
YoungJun Chocbc4c332013-06-12 10:44:40 +0900549 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900550
551 /* source/destination memory list */
552 head = &c_node->mem_list[qbuf->ops_id];
553
554 /* find memory node from memory list */
555 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900556 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900557
558 /* compare buffer id */
559 if (m_node->buf_id == qbuf->buf_id)
560 return m_node;
561 }
562
563 return NULL;
564}
565
566static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
567 struct drm_exynos_ipp_cmd_node *c_node,
568 struct drm_exynos_ipp_mem_node *m_node)
569{
570 struct exynos_drm_ipp_ops *ops = NULL;
571 int ret = 0;
572
YoungJun Chocbc4c332013-06-12 10:44:40 +0900573 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900574
575 if (!m_node) {
576 DRM_ERROR("invalid queue node.\n");
577 return -EFAULT;
578 }
579
YoungJun Chocbc4c332013-06-12 10:44:40 +0900580 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900581
582 /* get operations callback */
583 ops = ippdrv->ops[m_node->ops_id];
584 if (!ops) {
585 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200586 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900587 }
588
589 /* set address and enable irq */
590 if (ops->set_addr) {
591 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
592 m_node->buf_id, IPP_BUF_ENQUEUE);
593 if (ret) {
594 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200595 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900596 }
597 }
598
Eunchul Kimcb471f142012-12-14 18:10:31 +0900599 return ret;
600}
601
Eunchul Kimcb471f142012-12-14 18:10:31 +0900602static int ipp_put_mem_node(struct drm_device *drm_dev,
603 struct drm_exynos_ipp_cmd_node *c_node,
604 struct drm_exynos_ipp_mem_node *m_node)
605{
606 int i;
607
YoungJun Chocbc4c332013-06-12 10:44:40 +0900608 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900609
610 if (!m_node) {
611 DRM_ERROR("invalid dequeue node.\n");
612 return -EFAULT;
613 }
614
YoungJun Chocbc4c332013-06-12 10:44:40 +0900615 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900616
617 /* put gem buffer */
618 for_each_ipp_planar(i) {
619 unsigned long handle = m_node->buf_info.handles[i];
620 if (handle)
621 exynos_drm_gem_put_dma_addr(drm_dev, handle,
Andrzej Hajda945a0aa2014-08-28 11:07:27 +0200622 c_node->filp);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900623 }
624
Eunchul Kimcb471f142012-12-14 18:10:31 +0900625 list_del(&m_node->list);
626 kfree(m_node);
627
Eunchul Kimcb471f142012-12-14 18:10:31 +0900628 return 0;
629}
630
Andrzej Hajda6602ffb2014-08-28 11:07:30 +0200631static struct drm_exynos_ipp_mem_node
632 *ipp_get_mem_node(struct drm_device *drm_dev,
633 struct drm_file *file,
634 struct drm_exynos_ipp_cmd_node *c_node,
635 struct drm_exynos_ipp_queue_buf *qbuf)
636{
637 struct drm_exynos_ipp_mem_node *m_node;
638 struct drm_exynos_ipp_buf_info *buf_info;
639 int i;
640
641 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
642 if (!m_node)
643 return ERR_PTR(-ENOMEM);
644
645 buf_info = &m_node->buf_info;
646
647 /* operations, buffer id */
648 m_node->ops_id = qbuf->ops_id;
649 m_node->prop_id = qbuf->prop_id;
650 m_node->buf_id = qbuf->buf_id;
651 INIT_LIST_HEAD(&m_node->list);
652
653 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
654 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
655
656 for_each_ipp_planar(i) {
657 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
658
659 /* get dma address by handle */
660 if (qbuf->handle[i]) {
661 dma_addr_t *addr;
662
663 addr = exynos_drm_gem_get_dma_addr(drm_dev,
664 qbuf->handle[i], file);
665 if (IS_ERR(addr)) {
666 DRM_ERROR("failed to get addr.\n");
667 ipp_put_mem_node(drm_dev, c_node, m_node);
668 return ERR_PTR(-EFAULT);
669 }
670
671 buf_info->handles[i] = qbuf->handle[i];
672 buf_info->base[i] = *addr;
673 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
674 buf_info->base[i], buf_info->handles[i]);
675 }
676 }
677
678 mutex_lock(&c_node->mem_lock);
679 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
680 mutex_unlock(&c_node->mem_lock);
681
682 return m_node;
683}
684
Eunchul Kimcb471f142012-12-14 18:10:31 +0900685static void ipp_free_event(struct drm_pending_event *event)
686{
687 kfree(event);
688}
689
690static int ipp_get_event(struct drm_device *drm_dev,
691 struct drm_file *file,
692 struct drm_exynos_ipp_cmd_node *c_node,
693 struct drm_exynos_ipp_queue_buf *qbuf)
694{
695 struct drm_exynos_ipp_send_event *e;
696 unsigned long flags;
697
YoungJun Chocbc4c332013-06-12 10:44:40 +0900698 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900699
700 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900701 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900702 spin_lock_irqsave(&drm_dev->event_lock, flags);
703 file->event_space += sizeof(e->event);
704 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
705 return -ENOMEM;
706 }
707
708 /* make event */
709 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
710 e->event.base.length = sizeof(e->event);
711 e->event.user_data = qbuf->user_data;
712 e->event.prop_id = qbuf->prop_id;
713 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
714 e->base.event = &e->event.base;
715 e->base.file_priv = file;
716 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200717 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900718 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200719 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900720
721 return 0;
722}
723
724static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
725 struct drm_exynos_ipp_queue_buf *qbuf)
726{
727 struct drm_exynos_ipp_send_event *e, *te;
728 int count = 0;
729
YoungJun Cho4d520762014-05-26 10:17:21 +0200730 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900731 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900732 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900733
734 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530735 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900736 * stop operations want to delete all event list.
737 * another case delete only same buf id.
738 */
739 if (!qbuf) {
740 /* delete list */
741 list_del(&e->base.link);
742 kfree(e);
743 }
744
745 /* compare buffer id */
746 if (qbuf && (qbuf->buf_id ==
747 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
748 /* delete list */
749 list_del(&e->base.link);
750 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200751 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900752 }
753 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200754
755out_unlock:
756 mutex_unlock(&c_node->event_lock);
757 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900758}
759
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530760static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900761 struct exynos_drm_ippdrv *ippdrv,
762 struct drm_exynos_ipp_cmd_work *cmd_work,
763 struct drm_exynos_ipp_cmd_node *c_node)
764{
765 struct ipp_context *ctx = get_ipp_context(dev);
766
767 cmd_work->ippdrv = ippdrv;
768 cmd_work->c_node = c_node;
769 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
770}
771
772static int ipp_queue_buf_with_run(struct device *dev,
773 struct drm_exynos_ipp_cmd_node *c_node,
774 struct drm_exynos_ipp_mem_node *m_node,
775 struct drm_exynos_ipp_queue_buf *qbuf)
776{
777 struct exynos_drm_ippdrv *ippdrv;
778 struct drm_exynos_ipp_property *property;
779 struct exynos_drm_ipp_ops *ops;
780 int ret;
781
Eunchul Kimcb471f142012-12-14 18:10:31 +0900782 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530783 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900784 DRM_ERROR("failed to get ipp driver.\n");
785 return -EFAULT;
786 }
787
788 ops = ippdrv->ops[qbuf->ops_id];
789 if (!ops) {
790 DRM_ERROR("failed to get ops.\n");
791 return -EFAULT;
792 }
793
794 property = &c_node->property;
795
796 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900797 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900798 return 0;
799 }
800
YoungJun Cho220db6f2014-05-26 10:17:20 +0200801 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900802 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200803 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900804 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900805 return 0;
806 }
807
808 /*
809 * If set destination buffer and enabled clock,
810 * then m2m operations need start operations at queue_buf
811 */
812 if (ipp_is_m2m_cmd(property->cmd)) {
813 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
814
815 cmd_work->ctrl = IPP_CTRL_PLAY;
816 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
817 } else {
818 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
819 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200820 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900821 DRM_ERROR("failed to set m node.\n");
822 return ret;
823 }
824 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200825 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900826
827 return 0;
828}
829
830static void ipp_clean_queue_buf(struct drm_device *drm_dev,
831 struct drm_exynos_ipp_cmd_node *c_node,
832 struct drm_exynos_ipp_queue_buf *qbuf)
833{
834 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
835
YoungJun Choc66ce402014-05-26 10:17:15 +0200836 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200837 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200838 list_for_each_entry_safe(m_node, tm_node,
839 &c_node->mem_list[qbuf->ops_id], list) {
840 if (m_node->buf_id == qbuf->buf_id &&
841 m_node->ops_id == qbuf->ops_id)
842 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900843 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200844 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900845}
846
847int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
848 struct drm_file *file)
849{
850 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200851 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900852 struct ipp_context *ctx = get_ipp_context(dev);
853 struct drm_exynos_ipp_queue_buf *qbuf = data;
854 struct drm_exynos_ipp_cmd_node *c_node;
855 struct drm_exynos_ipp_mem_node *m_node;
856 int ret;
857
Eunchul Kimcb471f142012-12-14 18:10:31 +0900858 if (!qbuf) {
859 DRM_ERROR("invalid buf parameter.\n");
860 return -EINVAL;
861 }
862
863 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
864 DRM_ERROR("invalid ops parameter.\n");
865 return -EINVAL;
866 }
867
YoungJun Chocbc4c332013-06-12 10:44:40 +0900868 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
869 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900870 qbuf->buf_id, qbuf->buf_type);
871
872 /* find command node */
873 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
874 qbuf->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200875 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900876 DRM_ERROR("failed to get command node.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200877 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900878 }
879
880 /* buffer control */
881 switch (qbuf->buf_type) {
882 case IPP_BUF_ENQUEUE:
883 /* get memory node */
884 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
885 if (IS_ERR(m_node)) {
886 DRM_ERROR("failed to get m_node.\n");
887 return PTR_ERR(m_node);
888 }
889
890 /*
891 * first step get event for destination buffer.
892 * and second step when M2M case run with destination buffer
893 * if needed.
894 */
895 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
896 /* get event for destination buffer */
897 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
898 if (ret) {
899 DRM_ERROR("failed to get event.\n");
900 goto err_clean_node;
901 }
902
903 /*
904 * M2M case run play control for streaming feature.
905 * other case set address and waiting.
906 */
907 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
908 if (ret) {
909 DRM_ERROR("failed to run command.\n");
910 goto err_clean_node;
911 }
912 }
913 break;
914 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200915 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900916
917 /* put event for destination buffer */
918 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
919 ipp_put_event(c_node, qbuf);
920
921 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
922
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200923 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900924 break;
925 default:
926 DRM_ERROR("invalid buffer control.\n");
927 return -EINVAL;
928 }
929
930 return 0;
931
932err_clean_node:
933 DRM_ERROR("clean memory nodes.\n");
934
935 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
936 return ret;
937}
938
939static bool exynos_drm_ipp_check_valid(struct device *dev,
940 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
941{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900942 if (ctrl != IPP_CTRL_PLAY) {
943 if (pm_runtime_suspended(dev)) {
944 DRM_ERROR("pm:runtime_suspended.\n");
945 goto err_status;
946 }
947 }
948
949 switch (ctrl) {
950 case IPP_CTRL_PLAY:
951 if (state != IPP_STATE_IDLE)
952 goto err_status;
953 break;
954 case IPP_CTRL_STOP:
955 if (state == IPP_STATE_STOP)
956 goto err_status;
957 break;
958 case IPP_CTRL_PAUSE:
959 if (state != IPP_STATE_START)
960 goto err_status;
961 break;
962 case IPP_CTRL_RESUME:
963 if (state != IPP_STATE_STOP)
964 goto err_status;
965 break;
966 default:
967 DRM_ERROR("invalid state.\n");
968 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900969 }
970
971 return true;
972
973err_status:
974 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
975 return false;
976}
977
978int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
979 struct drm_file *file)
980{
981 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900982 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200983 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900984 struct ipp_context *ctx = get_ipp_context(dev);
985 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
986 struct drm_exynos_ipp_cmd_work *cmd_work;
987 struct drm_exynos_ipp_cmd_node *c_node;
988
Eunchul Kimcb471f142012-12-14 18:10:31 +0900989 if (!ctx) {
990 DRM_ERROR("invalid context.\n");
991 return -EINVAL;
992 }
993
994 if (!cmd_ctrl) {
995 DRM_ERROR("invalid control parameter.\n");
996 return -EINVAL;
997 }
998
YoungJun Chocbc4c332013-06-12 10:44:40 +0900999 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001000 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1001
1002 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1003 if (IS_ERR(ippdrv)) {
1004 DRM_ERROR("failed to get ipp driver.\n");
1005 return PTR_ERR(ippdrv);
1006 }
1007
1008 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1009 cmd_ctrl->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001010 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001011 DRM_ERROR("invalid command node list.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001012 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001013 }
1014
1015 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1016 c_node->state)) {
1017 DRM_ERROR("invalid state.\n");
1018 return -EINVAL;
1019 }
1020
1021 switch (cmd_ctrl->ctrl) {
1022 case IPP_CTRL_PLAY:
1023 if (pm_runtime_suspended(ippdrv->dev))
1024 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001025
Eunchul Kimcb471f142012-12-14 18:10:31 +09001026 c_node->state = IPP_STATE_START;
1027
1028 cmd_work = c_node->start_work;
1029 cmd_work->ctrl = cmd_ctrl->ctrl;
1030 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001031 break;
1032 case IPP_CTRL_STOP:
1033 cmd_work = c_node->stop_work;
1034 cmd_work->ctrl = cmd_ctrl->ctrl;
1035 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1036
1037 if (!wait_for_completion_timeout(&c_node->stop_complete,
1038 msecs_to_jiffies(300))) {
1039 DRM_ERROR("timeout stop:prop_id[%d]\n",
1040 c_node->property.prop_id);
1041 }
1042
1043 c_node->state = IPP_STATE_STOP;
1044 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001045 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001046 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001047
1048 if (list_empty(&ippdrv->cmd_list))
1049 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001050 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001051 break;
1052 case IPP_CTRL_PAUSE:
1053 cmd_work = c_node->stop_work;
1054 cmd_work->ctrl = cmd_ctrl->ctrl;
1055 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1056
1057 if (!wait_for_completion_timeout(&c_node->stop_complete,
1058 msecs_to_jiffies(200))) {
1059 DRM_ERROR("timeout stop:prop_id[%d]\n",
1060 c_node->property.prop_id);
1061 }
1062
1063 c_node->state = IPP_STATE_STOP;
1064 break;
1065 case IPP_CTRL_RESUME:
1066 c_node->state = IPP_STATE_START;
1067 cmd_work = c_node->start_work;
1068 cmd_work->ctrl = cmd_ctrl->ctrl;
1069 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1070 break;
1071 default:
1072 DRM_ERROR("could not support this state currently.\n");
1073 return -EINVAL;
1074 }
1075
YoungJun Chocbc4c332013-06-12 10:44:40 +09001076 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001077 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1078
1079 return 0;
1080}
1081
1082int exynos_drm_ippnb_register(struct notifier_block *nb)
1083{
1084 return blocking_notifier_chain_register(
1085 &exynos_drm_ippnb_list, nb);
1086}
1087
1088int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1089{
1090 return blocking_notifier_chain_unregister(
1091 &exynos_drm_ippnb_list, nb);
1092}
1093
1094int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1095{
1096 return blocking_notifier_call_chain(
1097 &exynos_drm_ippnb_list, val, v);
1098}
1099
1100static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1101 struct drm_exynos_ipp_property *property)
1102{
1103 struct exynos_drm_ipp_ops *ops = NULL;
1104 bool swap = false;
1105 int ret, i;
1106
1107 if (!property) {
1108 DRM_ERROR("invalid property parameter.\n");
1109 return -EINVAL;
1110 }
1111
YoungJun Chocbc4c332013-06-12 10:44:40 +09001112 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001113
1114 /* reset h/w block */
1115 if (ippdrv->reset &&
1116 ippdrv->reset(ippdrv->dev)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001117 return -EINVAL;
1118 }
1119
1120 /* set source,destination operations */
1121 for_each_ipp_ops(i) {
1122 struct drm_exynos_ipp_config *config =
1123 &property->config[i];
1124
1125 ops = ippdrv->ops[i];
1126 if (!ops || !config) {
1127 DRM_ERROR("not support ops and config.\n");
1128 return -EINVAL;
1129 }
1130
1131 /* set format */
1132 if (ops->set_fmt) {
1133 ret = ops->set_fmt(ippdrv->dev, config->fmt);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001134 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001135 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001136 }
1137
1138 /* set transform for rotation, flip */
1139 if (ops->set_transf) {
1140 ret = ops->set_transf(ippdrv->dev, config->degree,
1141 config->flip, &swap);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001142 if (ret)
1143 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001144 }
1145
1146 /* set size */
1147 if (ops->set_size) {
1148 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1149 &config->sz);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001150 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001151 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001152 }
1153 }
1154
1155 return 0;
1156}
1157
1158static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1159 struct drm_exynos_ipp_cmd_node *c_node)
1160{
1161 struct drm_exynos_ipp_mem_node *m_node;
1162 struct drm_exynos_ipp_property *property = &c_node->property;
1163 struct list_head *head;
1164 int ret, i;
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 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001169 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001170
YoungJun Cho220db6f2014-05-26 10:17:20 +02001171 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001172 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001173 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001174 ret = -ENOMEM;
1175 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001176 }
1177
1178 /* set current property in ippdrv */
1179 ret = ipp_set_property(ippdrv, property);
1180 if (ret) {
1181 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001182 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001183 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001184 }
1185
1186 /* check command */
1187 switch (property->cmd) {
1188 case IPP_CMD_M2M:
1189 for_each_ipp_ops(i) {
1190 /* source/destination memory list */
1191 head = &c_node->mem_list[i];
1192
1193 m_node = list_first_entry(head,
1194 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001195
YoungJun Chocbc4c332013-06-12 10:44:40 +09001196 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001197
1198 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1199 if (ret) {
1200 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001201 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001202 }
1203 }
1204 break;
1205 case IPP_CMD_WB:
1206 /* destination memory list */
1207 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1208
1209 list_for_each_entry(m_node, head, list) {
1210 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1211 if (ret) {
1212 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001213 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001214 }
1215 }
1216 break;
1217 case IPP_CMD_OUTPUT:
1218 /* source memory list */
1219 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1220
1221 list_for_each_entry(m_node, head, list) {
1222 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1223 if (ret) {
1224 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001225 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001226 }
1227 }
1228 break;
1229 default:
1230 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001231 ret = -EINVAL;
1232 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001233 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001234 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001235
YoungJun Chocbc4c332013-06-12 10:44:40 +09001236 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001237
1238 /* start operations */
1239 if (ippdrv->start) {
1240 ret = ippdrv->start(ippdrv->dev, property->cmd);
1241 if (ret) {
1242 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001243 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001244 return ret;
1245 }
1246 }
1247
1248 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001249
1250err_unlock:
1251 mutex_unlock(&c_node->mem_lock);
1252 ippdrv->c_node = NULL;
1253 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001254}
1255
1256static int ipp_stop_property(struct drm_device *drm_dev,
1257 struct exynos_drm_ippdrv *ippdrv,
1258 struct drm_exynos_ipp_cmd_node *c_node)
1259{
1260 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1261 struct drm_exynos_ipp_property *property = &c_node->property;
1262 struct list_head *head;
1263 int ret = 0, i;
1264
YoungJun Chocbc4c332013-06-12 10:44:40 +09001265 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001266
1267 /* put event */
1268 ipp_put_event(c_node, NULL);
1269
YoungJun Cho220db6f2014-05-26 10:17:20 +02001270 mutex_lock(&c_node->mem_lock);
1271
Eunchul Kimcb471f142012-12-14 18:10:31 +09001272 /* check command */
1273 switch (property->cmd) {
1274 case IPP_CMD_M2M:
1275 for_each_ipp_ops(i) {
1276 /* source/destination memory list */
1277 head = &c_node->mem_list[i];
1278
Eunchul Kimcb471f142012-12-14 18:10:31 +09001279 list_for_each_entry_safe(m_node, tm_node,
1280 head, list) {
1281 ret = ipp_put_mem_node(drm_dev, c_node,
1282 m_node);
1283 if (ret) {
1284 DRM_ERROR("failed to put m_node.\n");
1285 goto err_clear;
1286 }
1287 }
1288 }
1289 break;
1290 case IPP_CMD_WB:
1291 /* destination memory list */
1292 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1293
Eunchul Kimcb471f142012-12-14 18:10:31 +09001294 list_for_each_entry_safe(m_node, tm_node, head, list) {
1295 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1296 if (ret) {
1297 DRM_ERROR("failed to put m_node.\n");
1298 goto err_clear;
1299 }
1300 }
1301 break;
1302 case IPP_CMD_OUTPUT:
1303 /* source memory list */
1304 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1305
Eunchul Kimcb471f142012-12-14 18:10:31 +09001306 list_for_each_entry_safe(m_node, tm_node, head, list) {
1307 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1308 if (ret) {
1309 DRM_ERROR("failed to put m_node.\n");
1310 goto err_clear;
1311 }
1312 }
1313 break;
1314 default:
1315 DRM_ERROR("invalid operations.\n");
1316 ret = -EINVAL;
1317 goto err_clear;
1318 }
1319
1320err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001321 mutex_unlock(&c_node->mem_lock);
1322
Eunchul Kimcb471f142012-12-14 18:10:31 +09001323 /* stop operations */
1324 if (ippdrv->stop)
1325 ippdrv->stop(ippdrv->dev, property->cmd);
1326
1327 return ret;
1328}
1329
1330void ipp_sched_cmd(struct work_struct *work)
1331{
1332 struct drm_exynos_ipp_cmd_work *cmd_work =
1333 (struct drm_exynos_ipp_cmd_work *)work;
1334 struct exynos_drm_ippdrv *ippdrv;
1335 struct drm_exynos_ipp_cmd_node *c_node;
1336 struct drm_exynos_ipp_property *property;
1337 int ret;
1338
Eunchul Kimcb471f142012-12-14 18:10:31 +09001339 ippdrv = cmd_work->ippdrv;
1340 if (!ippdrv) {
1341 DRM_ERROR("invalid ippdrv list.\n");
1342 return;
1343 }
1344
1345 c_node = cmd_work->c_node;
1346 if (!c_node) {
1347 DRM_ERROR("invalid command node list.\n");
1348 return;
1349 }
1350
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001351 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001352
1353 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001354
1355 switch (cmd_work->ctrl) {
1356 case IPP_CTRL_PLAY:
1357 case IPP_CTRL_RESUME:
1358 ret = ipp_start_property(ippdrv, c_node);
1359 if (ret) {
1360 DRM_ERROR("failed to start property:prop_id[%d]\n",
1361 c_node->property.prop_id);
1362 goto err_unlock;
1363 }
1364
1365 /*
1366 * M2M case supports wait_completion of transfer.
1367 * because M2M case supports single unit operation
1368 * with multiple queue.
1369 * M2M need to wait completion of data transfer.
1370 */
1371 if (ipp_is_m2m_cmd(property->cmd)) {
1372 if (!wait_for_completion_timeout
1373 (&c_node->start_complete, msecs_to_jiffies(200))) {
1374 DRM_ERROR("timeout event:prop_id[%d]\n",
1375 c_node->property.prop_id);
1376 goto err_unlock;
1377 }
1378 }
1379 break;
1380 case IPP_CTRL_STOP:
1381 case IPP_CTRL_PAUSE:
1382 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1383 c_node);
1384 if (ret) {
1385 DRM_ERROR("failed to stop property.\n");
1386 goto err_unlock;
1387 }
1388
1389 complete(&c_node->stop_complete);
1390 break;
1391 default:
1392 DRM_ERROR("unknown control type\n");
1393 break;
1394 }
1395
YoungJun Chocbc4c332013-06-12 10:44:40 +09001396 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001397
1398err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001399 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001400}
1401
1402static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1403 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1404{
1405 struct drm_device *drm_dev = ippdrv->drm_dev;
1406 struct drm_exynos_ipp_property *property = &c_node->property;
1407 struct drm_exynos_ipp_mem_node *m_node;
1408 struct drm_exynos_ipp_queue_buf qbuf;
1409 struct drm_exynos_ipp_send_event *e;
1410 struct list_head *head;
1411 struct timeval now;
1412 unsigned long flags;
1413 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1414 int ret, i;
1415
1416 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001417 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001418
1419 if (!drm_dev) {
1420 DRM_ERROR("failed to get drm_dev.\n");
1421 return -EINVAL;
1422 }
1423
1424 if (!property) {
1425 DRM_ERROR("failed to get property.\n");
1426 return -EINVAL;
1427 }
1428
YoungJun Cho4d520762014-05-26 10:17:21 +02001429 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001430 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001431 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001432 ret = 0;
1433 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001434 }
1435
YoungJun Cho220db6f2014-05-26 10:17:20 +02001436 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001437 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001438 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001439 ret = 0;
1440 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001441 }
1442
1443 /* check command */
1444 switch (property->cmd) {
1445 case IPP_CMD_M2M:
1446 for_each_ipp_ops(i) {
1447 /* source/destination memory list */
1448 head = &c_node->mem_list[i];
1449
1450 m_node = list_first_entry(head,
1451 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001452
1453 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001454 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001455 i ? "dst" : "src", tbuf_id[i]);
1456
1457 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1458 if (ret)
1459 DRM_ERROR("failed to put m_node.\n");
1460 }
1461 break;
1462 case IPP_CMD_WB:
1463 /* clear buf for finding */
1464 memset(&qbuf, 0x0, sizeof(qbuf));
1465 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1466 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1467
1468 /* get memory node entry */
1469 m_node = ipp_find_mem_node(c_node, &qbuf);
1470 if (!m_node) {
1471 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001472 ret = -ENOMEM;
1473 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001474 }
1475
1476 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1477
1478 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1479 if (ret)
1480 DRM_ERROR("failed to put m_node.\n");
1481 break;
1482 case IPP_CMD_OUTPUT:
1483 /* source memory list */
1484 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1485
1486 m_node = list_first_entry(head,
1487 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001488
1489 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1490
1491 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1492 if (ret)
1493 DRM_ERROR("failed to put m_node.\n");
1494 break;
1495 default:
1496 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001497 ret = -EINVAL;
1498 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001499 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001500 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001501
1502 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1503 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1504 tbuf_id[1], buf_id[1], property->prop_id);
1505
1506 /*
1507 * command node have event list of destination buffer
1508 * If destination buffer enqueue to mem list,
1509 * then we make event and link to event list tail.
1510 * so, we get first event for first enqueued buffer.
1511 */
1512 e = list_first_entry(&c_node->event_list,
1513 struct drm_exynos_ipp_send_event, base.link);
1514
Eunchul Kimcb471f142012-12-14 18:10:31 +09001515 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001516 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001517 e->event.tv_sec = now.tv_sec;
1518 e->event.tv_usec = now.tv_usec;
1519 e->event.prop_id = property->prop_id;
1520
1521 /* set buffer id about source destination */
1522 for_each_ipp_ops(i)
1523 e->event.buf_id[i] = tbuf_id[i];
1524
1525 spin_lock_irqsave(&drm_dev->event_lock, flags);
1526 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1527 wake_up_interruptible(&e->base.file_priv->event_wait);
1528 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001529 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001530
YoungJun Chocbc4c332013-06-12 10:44:40 +09001531 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001532 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1533
1534 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001535
1536err_mem_unlock:
1537 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001538err_event_unlock:
1539 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001540 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001541}
1542
1543void ipp_sched_event(struct work_struct *work)
1544{
1545 struct drm_exynos_ipp_event_work *event_work =
1546 (struct drm_exynos_ipp_event_work *)work;
1547 struct exynos_drm_ippdrv *ippdrv;
1548 struct drm_exynos_ipp_cmd_node *c_node;
1549 int ret;
1550
1551 if (!event_work) {
1552 DRM_ERROR("failed to get event_work.\n");
1553 return;
1554 }
1555
YoungJun Chocbc4c332013-06-12 10:44:40 +09001556 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001557
1558 ippdrv = event_work->ippdrv;
1559 if (!ippdrv) {
1560 DRM_ERROR("failed to get ipp driver.\n");
1561 return;
1562 }
1563
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001564 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001565 if (!c_node) {
1566 DRM_ERROR("failed to get command node.\n");
1567 return;
1568 }
1569
1570 /*
1571 * IPP supports command thread, event thread synchronization.
1572 * If IPP close immediately from user land, then IPP make
1573 * synchronization with command thread, so make complete event.
1574 * or going out operations.
1575 */
1576 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001577 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1578 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001579 goto err_completion;
1580 }
1581
Eunchul Kimcb471f142012-12-14 18:10:31 +09001582 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1583 if (ret) {
1584 DRM_ERROR("failed to send event.\n");
1585 goto err_completion;
1586 }
1587
1588err_completion:
1589 if (ipp_is_m2m_cmd(c_node->property.cmd))
1590 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001591}
1592
1593static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1594{
1595 struct ipp_context *ctx = get_ipp_context(dev);
1596 struct exynos_drm_ippdrv *ippdrv;
1597 int ret, count = 0;
1598
Eunchul Kimcb471f142012-12-14 18:10:31 +09001599 /* get ipp driver entry */
1600 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1601 ippdrv->drm_dev = drm_dev;
1602
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001603 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
1604 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001605 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001606 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001607 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001608 ippdrv->prop_list.ipp_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001609
YoungJun Chocbc4c332013-06-12 10:44:40 +09001610 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001611 count++, (int)ippdrv, ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001612
1613 /* store parent device for node */
1614 ippdrv->parent_dev = dev;
1615
1616 /* store event work queue and handler */
1617 ippdrv->event_workq = ctx->event_workq;
1618 ippdrv->sched_event = ipp_sched_event;
1619 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001620 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001621
1622 if (is_drm_iommu_supported(drm_dev)) {
1623 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1624 if (ret) {
1625 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001626 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001627 }
1628 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001629 }
1630
1631 return 0;
1632
YoungJun Cho075436b2014-05-26 10:17:19 +02001633err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001634 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001635 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1636 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001637 if (is_drm_iommu_supported(drm_dev))
1638 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1639
YoungJun Cho075436b2014-05-26 10:17:19 +02001640 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1641 ippdrv->prop_list.ipp_id);
1642 }
1643
Eunchul Kimcb471f142012-12-14 18:10:31 +09001644 return ret;
1645}
1646
1647static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1648{
1649 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001650 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001651
Eunchul Kimcb471f142012-12-14 18:10:31 +09001652 /* get ipp driver entry */
1653 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001654 if (is_drm_iommu_supported(drm_dev))
1655 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1656
YoungJun Cho075436b2014-05-26 10:17:19 +02001657 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1658 ippdrv->prop_list.ipp_id);
1659
Eunchul Kimcb471f142012-12-14 18:10:31 +09001660 ippdrv->drm_dev = NULL;
1661 exynos_drm_ippdrv_unregister(ippdrv);
1662 }
1663}
1664
1665static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1666 struct drm_file *file)
1667{
1668 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001669
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001670 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001671
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001672 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001673
1674 return 0;
1675}
1676
1677static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1678 struct drm_file *file)
1679{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001680 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001681 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001682 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1683 int count = 0;
1684
Eunchul Kimcb471f142012-12-14 18:10:31 +09001685 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001686 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001687 list_for_each_entry_safe(c_node, tc_node,
1688 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001689 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1690 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001691
Andrzej Hajda21a825e2014-08-28 11:07:28 +02001692 if (c_node->filp == file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001693 /*
1694 * userland goto unnormal state. process killed.
1695 * and close the file.
1696 * so, IPP didn't called stop cmd ctrl.
1697 * so, we are make stop operation in this state.
1698 */
1699 if (c_node->state == IPP_STATE_START) {
1700 ipp_stop_property(drm_dev, ippdrv,
1701 c_node);
1702 c_node->state = IPP_STATE_STOP;
1703 }
1704
1705 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001706 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001707 if (list_empty(&ippdrv->cmd_list))
1708 pm_runtime_put_sync(ippdrv->dev);
1709 }
1710 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001711 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001712 }
1713
Eunchul Kimcb471f142012-12-14 18:10:31 +09001714 return;
1715}
1716
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001717static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001718{
1719 struct device *dev = &pdev->dev;
1720 struct ipp_context *ctx;
1721 struct exynos_drm_subdrv *subdrv;
1722 int ret;
1723
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001724 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001725 if (!ctx)
1726 return -ENOMEM;
1727
Eunchul Kimcb471f142012-12-14 18:10:31 +09001728 mutex_init(&ctx->ipp_lock);
1729 mutex_init(&ctx->prop_lock);
1730
1731 idr_init(&ctx->ipp_idr);
1732 idr_init(&ctx->prop_idr);
1733
1734 /*
1735 * create single thread for ipp event
1736 * IPP supports event thread for IPP drivers.
1737 * IPP driver send event_work to this thread.
1738 * and IPP event thread send event to user process.
1739 */
1740 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1741 if (!ctx->event_workq) {
1742 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301743 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001744 }
1745
1746 /*
1747 * create single thread for ipp command
1748 * IPP supports command thread for user process.
1749 * user process make command node using set property ioctl.
1750 * and make start_work and send this work to command thread.
1751 * and then this command thread start property.
1752 */
1753 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1754 if (!ctx->cmd_workq) {
1755 dev_err(dev, "failed to create cmd workqueue\n");
1756 ret = -EINVAL;
1757 goto err_event_workq;
1758 }
1759
1760 /* set sub driver informations */
1761 subdrv = &ctx->subdrv;
1762 subdrv->dev = dev;
1763 subdrv->probe = ipp_subdrv_probe;
1764 subdrv->remove = ipp_subdrv_remove;
1765 subdrv->open = ipp_subdrv_open;
1766 subdrv->close = ipp_subdrv_close;
1767
1768 platform_set_drvdata(pdev, ctx);
1769
1770 ret = exynos_drm_subdrv_register(subdrv);
1771 if (ret < 0) {
1772 DRM_ERROR("failed to register drm ipp device.\n");
1773 goto err_cmd_workq;
1774 }
1775
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001776 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001777
1778 return 0;
1779
1780err_cmd_workq:
1781 destroy_workqueue(ctx->cmd_workq);
1782err_event_workq:
1783 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001784 return ret;
1785}
1786
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001787static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001788{
1789 struct ipp_context *ctx = platform_get_drvdata(pdev);
1790
Eunchul Kimcb471f142012-12-14 18:10:31 +09001791 /* unregister sub driver */
1792 exynos_drm_subdrv_unregister(&ctx->subdrv);
1793
1794 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001795 idr_destroy(&ctx->ipp_idr);
1796 idr_destroy(&ctx->prop_idr);
1797
1798 mutex_destroy(&ctx->ipp_lock);
1799 mutex_destroy(&ctx->prop_lock);
1800
1801 /* destroy command, event work queue */
1802 destroy_workqueue(ctx->cmd_workq);
1803 destroy_workqueue(ctx->event_workq);
1804
Eunchul Kimcb471f142012-12-14 18:10:31 +09001805 return 0;
1806}
1807
Eunchul Kimcb471f142012-12-14 18:10:31 +09001808struct platform_driver ipp_driver = {
1809 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001810 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001811 .driver = {
1812 .name = "exynos-drm-ipp",
1813 .owner = THIS_MODULE,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001814 },
1815};
1816