blob: 9e9714afca1dadb9be4a19fcd3d5c197eee18833 [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
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200501static int ipp_put_mem_node(struct drm_device *drm_dev,
502 struct drm_exynos_ipp_cmd_node *c_node,
503 struct drm_exynos_ipp_mem_node *m_node)
504{
505 int i;
506
507 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
508
509 if (!m_node) {
510 DRM_ERROR("invalid dequeue node.\n");
511 return -EFAULT;
512 }
513
514 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
515
516 /* put gem buffer */
517 for_each_ipp_planar(i) {
518 unsigned long handle = m_node->buf_info.handles[i];
519 if (handle)
520 exynos_drm_gem_put_dma_addr(drm_dev, handle,
521 c_node->filp);
522 }
523
524 list_del(&m_node->list);
525 kfree(m_node);
526
527 return 0;
528}
529
530static struct drm_exynos_ipp_mem_node
531 *ipp_get_mem_node(struct drm_device *drm_dev,
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200532 struct drm_exynos_ipp_cmd_node *c_node,
533 struct drm_exynos_ipp_queue_buf *qbuf)
534{
535 struct drm_exynos_ipp_mem_node *m_node;
536 struct drm_exynos_ipp_buf_info *buf_info;
537 int i;
538
539 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
540 if (!m_node)
541 return ERR_PTR(-ENOMEM);
542
543 buf_info = &m_node->buf_info;
544
545 /* operations, buffer id */
546 m_node->ops_id = qbuf->ops_id;
547 m_node->prop_id = qbuf->prop_id;
548 m_node->buf_id = qbuf->buf_id;
549 INIT_LIST_HEAD(&m_node->list);
550
551 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
552 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
553
554 for_each_ipp_planar(i) {
555 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
556
557 /* get dma address by handle */
558 if (qbuf->handle[i]) {
559 dma_addr_t *addr;
560
561 addr = exynos_drm_gem_get_dma_addr(drm_dev,
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200562 qbuf->handle[i], c_node->filp);
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200563 if (IS_ERR(addr)) {
564 DRM_ERROR("failed to get addr.\n");
565 ipp_put_mem_node(drm_dev, c_node, m_node);
566 return ERR_PTR(-EFAULT);
567 }
568
569 buf_info->handles[i] = qbuf->handle[i];
570 buf_info->base[i] = *addr;
571 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
572 buf_info->base[i], buf_info->handles[i]);
573 }
574 }
575
576 mutex_lock(&c_node->mem_lock);
577 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
578 mutex_unlock(&c_node->mem_lock);
579
580 return m_node;
581}
582
583static void ipp_clean_mem_nodes(struct drm_device *drm_dev,
584 struct drm_exynos_ipp_cmd_node *c_node, int ops)
585{
586 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
587 struct list_head *head = &c_node->mem_list[ops];
588
589 mutex_lock(&c_node->mem_lock);
590
591 list_for_each_entry_safe(m_node, tm_node, head, list) {
592 int ret;
593
594 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
595 if (ret)
596 DRM_ERROR("failed to put m_node.\n");
597 }
598
599 mutex_unlock(&c_node->mem_lock);
600}
601
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200602static void ipp_free_event(struct drm_pending_event *event)
603{
604 kfree(event);
605}
606
607static int ipp_get_event(struct drm_device *drm_dev,
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200608 struct drm_exynos_ipp_cmd_node *c_node,
609 struct drm_exynos_ipp_queue_buf *qbuf)
610{
611 struct drm_exynos_ipp_send_event *e;
612 unsigned long flags;
613
614 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
615
616 e = kzalloc(sizeof(*e), GFP_KERNEL);
617 if (!e) {
618 spin_lock_irqsave(&drm_dev->event_lock, flags);
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200619 c_node->filp->event_space += sizeof(e->event);
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200620 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
621 return -ENOMEM;
622 }
623
624 /* make event */
625 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
626 e->event.base.length = sizeof(e->event);
627 e->event.user_data = qbuf->user_data;
628 e->event.prop_id = qbuf->prop_id;
629 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
630 e->base.event = &e->event.base;
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200631 e->base.file_priv = c_node->filp;
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200632 e->base.destroy = ipp_free_event;
633 mutex_lock(&c_node->event_lock);
634 list_add_tail(&e->base.link, &c_node->event_list);
635 mutex_unlock(&c_node->event_lock);
636
637 return 0;
638}
639
640static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
641 struct drm_exynos_ipp_queue_buf *qbuf)
642{
643 struct drm_exynos_ipp_send_event *e, *te;
644 int count = 0;
645
646 mutex_lock(&c_node->event_lock);
647 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
648 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
649
650 /*
651 * qbuf == NULL condition means all event deletion.
652 * stop operations want to delete all event list.
653 * another case delete only same buf id.
654 */
655 if (!qbuf) {
656 /* delete list */
657 list_del(&e->base.link);
658 kfree(e);
659 }
660
661 /* compare buffer id */
662 if (qbuf && (qbuf->buf_id ==
663 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
664 /* delete list */
665 list_del(&e->base.link);
666 kfree(e);
667 goto out_unlock;
668 }
669 }
670
671out_unlock:
672 mutex_unlock(&c_node->event_lock);
673 return;
674}
675
YoungJun Cho075436b2014-05-26 10:17:19 +0200676static void ipp_clean_cmd_node(struct ipp_context *ctx,
677 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900678{
Andrzej Hajda22e816f2014-08-28 11:07:32 +0200679 int i;
680
Andrzej Hajda6f7d48e2014-08-28 11:07:26 +0200681 /* cancel works */
682 cancel_work_sync(&c_node->start_work->work);
683 cancel_work_sync(&c_node->stop_work->work);
684 cancel_work_sync(&c_node->event_work->work);
685
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200686 /* put event */
687 ipp_put_event(c_node, NULL);
688
Andrzej Hajda22e816f2014-08-28 11:07:32 +0200689 for_each_ipp_ops(i)
690 ipp_clean_mem_nodes(ctx->subdrv.drm_dev, c_node, i);
691
Eunchul Kimcb471f142012-12-14 18:10:31 +0900692 /* delete list */
693 list_del(&c_node->list);
694
YoungJun Cho075436b2014-05-26 10:17:19 +0200695 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
696 c_node->property.prop_id);
697
Eunchul Kimcb471f142012-12-14 18:10:31 +0900698 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200699 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900700 mutex_destroy(&c_node->mem_lock);
701 mutex_destroy(&c_node->event_lock);
702
703 /* free command node */
704 kfree(c_node->start_work);
705 kfree(c_node->stop_work);
706 kfree(c_node->event_work);
707 kfree(c_node);
708}
709
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200710static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900711{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200712 switch (c_node->property.cmd) {
713 case IPP_CMD_WB:
714 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
715 case IPP_CMD_OUTPUT:
716 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
717 case IPP_CMD_M2M:
718 default:
719 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
720 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900721 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900722}
723
724static struct drm_exynos_ipp_mem_node
725 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
726 struct drm_exynos_ipp_queue_buf *qbuf)
727{
728 struct drm_exynos_ipp_mem_node *m_node;
729 struct list_head *head;
730 int count = 0;
731
YoungJun Chocbc4c332013-06-12 10:44:40 +0900732 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900733
734 /* source/destination memory list */
735 head = &c_node->mem_list[qbuf->ops_id];
736
737 /* find memory node from memory list */
738 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900739 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900740
741 /* compare buffer id */
742 if (m_node->buf_id == qbuf->buf_id)
743 return m_node;
744 }
745
746 return NULL;
747}
748
749static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
750 struct drm_exynos_ipp_cmd_node *c_node,
751 struct drm_exynos_ipp_mem_node *m_node)
752{
753 struct exynos_drm_ipp_ops *ops = NULL;
754 int ret = 0;
755
YoungJun Chocbc4c332013-06-12 10:44:40 +0900756 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900757
758 if (!m_node) {
759 DRM_ERROR("invalid queue node.\n");
760 return -EFAULT;
761 }
762
YoungJun Chocbc4c332013-06-12 10:44:40 +0900763 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900764
765 /* get operations callback */
766 ops = ippdrv->ops[m_node->ops_id];
767 if (!ops) {
768 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200769 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900770 }
771
772 /* set address and enable irq */
773 if (ops->set_addr) {
774 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
775 m_node->buf_id, IPP_BUF_ENQUEUE);
776 if (ret) {
777 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200778 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900779 }
780 }
781
Eunchul Kimcb471f142012-12-14 18:10:31 +0900782 return ret;
783}
784
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530785static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900786 struct exynos_drm_ippdrv *ippdrv,
787 struct drm_exynos_ipp_cmd_work *cmd_work,
788 struct drm_exynos_ipp_cmd_node *c_node)
789{
790 struct ipp_context *ctx = get_ipp_context(dev);
791
792 cmd_work->ippdrv = ippdrv;
793 cmd_work->c_node = c_node;
Andrzej Hajda05afb1a2014-08-28 11:07:33 +0200794 queue_work(ctx->cmd_workq, &cmd_work->work);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900795}
796
797static int ipp_queue_buf_with_run(struct device *dev,
798 struct drm_exynos_ipp_cmd_node *c_node,
799 struct drm_exynos_ipp_mem_node *m_node,
800 struct drm_exynos_ipp_queue_buf *qbuf)
801{
802 struct exynos_drm_ippdrv *ippdrv;
803 struct drm_exynos_ipp_property *property;
804 struct exynos_drm_ipp_ops *ops;
805 int ret;
806
Eunchul Kimcb471f142012-12-14 18:10:31 +0900807 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530808 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900809 DRM_ERROR("failed to get ipp driver.\n");
810 return -EFAULT;
811 }
812
813 ops = ippdrv->ops[qbuf->ops_id];
814 if (!ops) {
815 DRM_ERROR("failed to get ops.\n");
816 return -EFAULT;
817 }
818
819 property = &c_node->property;
820
821 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900822 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900823 return 0;
824 }
825
YoungJun Cho220db6f2014-05-26 10:17:20 +0200826 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900827 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200828 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900829 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900830 return 0;
831 }
832
833 /*
834 * If set destination buffer and enabled clock,
835 * then m2m operations need start operations at queue_buf
836 */
837 if (ipp_is_m2m_cmd(property->cmd)) {
838 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
839
840 cmd_work->ctrl = IPP_CTRL_PLAY;
841 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
842 } else {
843 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
844 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200845 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900846 DRM_ERROR("failed to set m node.\n");
847 return ret;
848 }
849 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200850 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900851
852 return 0;
853}
854
855static void ipp_clean_queue_buf(struct drm_device *drm_dev,
856 struct drm_exynos_ipp_cmd_node *c_node,
857 struct drm_exynos_ipp_queue_buf *qbuf)
858{
859 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
860
YoungJun Choc66ce402014-05-26 10:17:15 +0200861 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200862 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200863 list_for_each_entry_safe(m_node, tm_node,
864 &c_node->mem_list[qbuf->ops_id], list) {
865 if (m_node->buf_id == qbuf->buf_id &&
866 m_node->ops_id == qbuf->ops_id)
867 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900868 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200869 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900870}
871
872int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
873 struct drm_file *file)
874{
875 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200876 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900877 struct ipp_context *ctx = get_ipp_context(dev);
878 struct drm_exynos_ipp_queue_buf *qbuf = data;
879 struct drm_exynos_ipp_cmd_node *c_node;
880 struct drm_exynos_ipp_mem_node *m_node;
881 int ret;
882
Eunchul Kimcb471f142012-12-14 18:10:31 +0900883 if (!qbuf) {
884 DRM_ERROR("invalid buf parameter.\n");
885 return -EINVAL;
886 }
887
888 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
889 DRM_ERROR("invalid ops parameter.\n");
890 return -EINVAL;
891 }
892
YoungJun Chocbc4c332013-06-12 10:44:40 +0900893 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
894 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900895 qbuf->buf_id, qbuf->buf_type);
896
897 /* find command node */
898 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
899 qbuf->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200900 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900901 DRM_ERROR("failed to get command node.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200902 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900903 }
904
905 /* buffer control */
906 switch (qbuf->buf_type) {
907 case IPP_BUF_ENQUEUE:
908 /* get memory node */
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200909 m_node = ipp_get_mem_node(drm_dev, c_node, qbuf);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900910 if (IS_ERR(m_node)) {
911 DRM_ERROR("failed to get m_node.\n");
912 return PTR_ERR(m_node);
913 }
914
915 /*
916 * first step get event for destination buffer.
917 * and second step when M2M case run with destination buffer
918 * if needed.
919 */
920 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
921 /* get event for destination buffer */
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200922 ret = ipp_get_event(drm_dev, c_node, qbuf);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900923 if (ret) {
924 DRM_ERROR("failed to get event.\n");
925 goto err_clean_node;
926 }
927
928 /*
929 * M2M case run play control for streaming feature.
930 * other case set address and waiting.
931 */
932 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
933 if (ret) {
934 DRM_ERROR("failed to run command.\n");
935 goto err_clean_node;
936 }
937 }
938 break;
939 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200940 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900941
942 /* put event for destination buffer */
943 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
944 ipp_put_event(c_node, qbuf);
945
946 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
947
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200948 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900949 break;
950 default:
951 DRM_ERROR("invalid buffer control.\n");
952 return -EINVAL;
953 }
954
955 return 0;
956
957err_clean_node:
958 DRM_ERROR("clean memory nodes.\n");
959
960 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
961 return ret;
962}
963
964static bool exynos_drm_ipp_check_valid(struct device *dev,
965 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
966{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900967 if (ctrl != IPP_CTRL_PLAY) {
968 if (pm_runtime_suspended(dev)) {
969 DRM_ERROR("pm:runtime_suspended.\n");
970 goto err_status;
971 }
972 }
973
974 switch (ctrl) {
975 case IPP_CTRL_PLAY:
976 if (state != IPP_STATE_IDLE)
977 goto err_status;
978 break;
979 case IPP_CTRL_STOP:
980 if (state == IPP_STATE_STOP)
981 goto err_status;
982 break;
983 case IPP_CTRL_PAUSE:
984 if (state != IPP_STATE_START)
985 goto err_status;
986 break;
987 case IPP_CTRL_RESUME:
988 if (state != IPP_STATE_STOP)
989 goto err_status;
990 break;
991 default:
992 DRM_ERROR("invalid state.\n");
993 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900994 }
995
996 return true;
997
998err_status:
999 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1000 return false;
1001}
1002
1003int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1004 struct drm_file *file)
1005{
1006 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001007 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001008 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001009 struct ipp_context *ctx = get_ipp_context(dev);
1010 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1011 struct drm_exynos_ipp_cmd_work *cmd_work;
1012 struct drm_exynos_ipp_cmd_node *c_node;
1013
Eunchul Kimcb471f142012-12-14 18:10:31 +09001014 if (!ctx) {
1015 DRM_ERROR("invalid context.\n");
1016 return -EINVAL;
1017 }
1018
1019 if (!cmd_ctrl) {
1020 DRM_ERROR("invalid control parameter.\n");
1021 return -EINVAL;
1022 }
1023
YoungJun Chocbc4c332013-06-12 10:44:40 +09001024 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001025 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1026
1027 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1028 if (IS_ERR(ippdrv)) {
1029 DRM_ERROR("failed to get ipp driver.\n");
1030 return PTR_ERR(ippdrv);
1031 }
1032
1033 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1034 cmd_ctrl->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001035 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001036 DRM_ERROR("invalid command node list.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001037 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001038 }
1039
1040 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1041 c_node->state)) {
1042 DRM_ERROR("invalid state.\n");
1043 return -EINVAL;
1044 }
1045
1046 switch (cmd_ctrl->ctrl) {
1047 case IPP_CTRL_PLAY:
1048 if (pm_runtime_suspended(ippdrv->dev))
1049 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001050
Eunchul Kimcb471f142012-12-14 18:10:31 +09001051 c_node->state = IPP_STATE_START;
1052
1053 cmd_work = c_node->start_work;
1054 cmd_work->ctrl = cmd_ctrl->ctrl;
1055 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001056 break;
1057 case IPP_CTRL_STOP:
1058 cmd_work = c_node->stop_work;
1059 cmd_work->ctrl = cmd_ctrl->ctrl;
1060 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1061
1062 if (!wait_for_completion_timeout(&c_node->stop_complete,
1063 msecs_to_jiffies(300))) {
1064 DRM_ERROR("timeout stop:prop_id[%d]\n",
1065 c_node->property.prop_id);
1066 }
1067
1068 c_node->state = IPP_STATE_STOP;
1069 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001070 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001071 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001072
1073 if (list_empty(&ippdrv->cmd_list))
1074 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001075 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001076 break;
1077 case IPP_CTRL_PAUSE:
1078 cmd_work = c_node->stop_work;
1079 cmd_work->ctrl = cmd_ctrl->ctrl;
1080 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1081
1082 if (!wait_for_completion_timeout(&c_node->stop_complete,
1083 msecs_to_jiffies(200))) {
1084 DRM_ERROR("timeout stop:prop_id[%d]\n",
1085 c_node->property.prop_id);
1086 }
1087
1088 c_node->state = IPP_STATE_STOP;
1089 break;
1090 case IPP_CTRL_RESUME:
1091 c_node->state = IPP_STATE_START;
1092 cmd_work = c_node->start_work;
1093 cmd_work->ctrl = cmd_ctrl->ctrl;
1094 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1095 break;
1096 default:
1097 DRM_ERROR("could not support this state currently.\n");
1098 return -EINVAL;
1099 }
1100
YoungJun Chocbc4c332013-06-12 10:44:40 +09001101 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001102 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1103
1104 return 0;
1105}
1106
1107int exynos_drm_ippnb_register(struct notifier_block *nb)
1108{
1109 return blocking_notifier_chain_register(
1110 &exynos_drm_ippnb_list, nb);
1111}
1112
1113int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1114{
1115 return blocking_notifier_chain_unregister(
1116 &exynos_drm_ippnb_list, nb);
1117}
1118
1119int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1120{
1121 return blocking_notifier_call_chain(
1122 &exynos_drm_ippnb_list, val, v);
1123}
1124
1125static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1126 struct drm_exynos_ipp_property *property)
1127{
1128 struct exynos_drm_ipp_ops *ops = NULL;
1129 bool swap = false;
1130 int ret, i;
1131
1132 if (!property) {
1133 DRM_ERROR("invalid property parameter.\n");
1134 return -EINVAL;
1135 }
1136
YoungJun Chocbc4c332013-06-12 10:44:40 +09001137 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001138
1139 /* reset h/w block */
1140 if (ippdrv->reset &&
1141 ippdrv->reset(ippdrv->dev)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001142 return -EINVAL;
1143 }
1144
1145 /* set source,destination operations */
1146 for_each_ipp_ops(i) {
1147 struct drm_exynos_ipp_config *config =
1148 &property->config[i];
1149
1150 ops = ippdrv->ops[i];
1151 if (!ops || !config) {
1152 DRM_ERROR("not support ops and config.\n");
1153 return -EINVAL;
1154 }
1155
1156 /* set format */
1157 if (ops->set_fmt) {
1158 ret = ops->set_fmt(ippdrv->dev, config->fmt);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001159 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001160 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001161 }
1162
1163 /* set transform for rotation, flip */
1164 if (ops->set_transf) {
1165 ret = ops->set_transf(ippdrv->dev, config->degree,
1166 config->flip, &swap);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001167 if (ret)
1168 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001169 }
1170
1171 /* set size */
1172 if (ops->set_size) {
1173 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1174 &config->sz);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001175 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001176 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001177 }
1178 }
1179
1180 return 0;
1181}
1182
1183static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1184 struct drm_exynos_ipp_cmd_node *c_node)
1185{
1186 struct drm_exynos_ipp_mem_node *m_node;
1187 struct drm_exynos_ipp_property *property = &c_node->property;
1188 struct list_head *head;
1189 int ret, i;
1190
YoungJun Chocbc4c332013-06-12 10:44:40 +09001191 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001192
1193 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001194 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001195
YoungJun Cho220db6f2014-05-26 10:17:20 +02001196 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001197 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001198 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001199 ret = -ENOMEM;
1200 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001201 }
1202
1203 /* set current property in ippdrv */
1204 ret = ipp_set_property(ippdrv, property);
1205 if (ret) {
1206 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001207 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001208 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001209 }
1210
1211 /* check command */
1212 switch (property->cmd) {
1213 case IPP_CMD_M2M:
1214 for_each_ipp_ops(i) {
1215 /* source/destination memory list */
1216 head = &c_node->mem_list[i];
1217
1218 m_node = list_first_entry(head,
1219 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001220
YoungJun Chocbc4c332013-06-12 10:44:40 +09001221 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001222
1223 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1224 if (ret) {
1225 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001226 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001227 }
1228 }
1229 break;
1230 case IPP_CMD_WB:
1231 /* destination memory list */
1232 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1233
1234 list_for_each_entry(m_node, head, list) {
1235 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1236 if (ret) {
1237 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001238 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001239 }
1240 }
1241 break;
1242 case IPP_CMD_OUTPUT:
1243 /* source memory list */
1244 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1245
1246 list_for_each_entry(m_node, head, list) {
1247 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1248 if (ret) {
1249 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001250 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001251 }
1252 }
1253 break;
1254 default:
1255 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001256 ret = -EINVAL;
1257 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001258 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001259 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001260
YoungJun Chocbc4c332013-06-12 10:44:40 +09001261 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001262
1263 /* start operations */
1264 if (ippdrv->start) {
1265 ret = ippdrv->start(ippdrv->dev, property->cmd);
1266 if (ret) {
1267 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001268 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001269 return ret;
1270 }
1271 }
1272
1273 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001274
1275err_unlock:
1276 mutex_unlock(&c_node->mem_lock);
1277 ippdrv->c_node = NULL;
1278 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001279}
1280
1281static int ipp_stop_property(struct drm_device *drm_dev,
1282 struct exynos_drm_ippdrv *ippdrv,
1283 struct drm_exynos_ipp_cmd_node *c_node)
1284{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001285 struct drm_exynos_ipp_property *property = &c_node->property;
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001286 int i;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001287
YoungJun Chocbc4c332013-06-12 10:44:40 +09001288 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001289
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001290 /* stop operations */
1291 if (ippdrv->stop)
1292 ippdrv->stop(ippdrv->dev, property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001293
1294 /* check command */
1295 switch (property->cmd) {
1296 case IPP_CMD_M2M:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001297 for_each_ipp_ops(i)
1298 ipp_clean_mem_nodes(drm_dev, c_node, i);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001299 break;
1300 case IPP_CMD_WB:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001301 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_DST);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001302 break;
1303 case IPP_CMD_OUTPUT:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001304 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_SRC);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001305 break;
1306 default:
1307 DRM_ERROR("invalid operations.\n");
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001308 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001309 }
1310
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001311 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001312}
1313
1314void ipp_sched_cmd(struct work_struct *work)
1315{
1316 struct drm_exynos_ipp_cmd_work *cmd_work =
Andrzej Hajda05afb1a2014-08-28 11:07:33 +02001317 container_of(work, struct drm_exynos_ipp_cmd_work, work);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001318 struct exynos_drm_ippdrv *ippdrv;
1319 struct drm_exynos_ipp_cmd_node *c_node;
1320 struct drm_exynos_ipp_property *property;
1321 int ret;
1322
Eunchul Kimcb471f142012-12-14 18:10:31 +09001323 ippdrv = cmd_work->ippdrv;
1324 if (!ippdrv) {
1325 DRM_ERROR("invalid ippdrv list.\n");
1326 return;
1327 }
1328
1329 c_node = cmd_work->c_node;
1330 if (!c_node) {
1331 DRM_ERROR("invalid command node list.\n");
1332 return;
1333 }
1334
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001335 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001336
1337 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001338
1339 switch (cmd_work->ctrl) {
1340 case IPP_CTRL_PLAY:
1341 case IPP_CTRL_RESUME:
1342 ret = ipp_start_property(ippdrv, c_node);
1343 if (ret) {
1344 DRM_ERROR("failed to start property:prop_id[%d]\n",
1345 c_node->property.prop_id);
1346 goto err_unlock;
1347 }
1348
1349 /*
1350 * M2M case supports wait_completion of transfer.
1351 * because M2M case supports single unit operation
1352 * with multiple queue.
1353 * M2M need to wait completion of data transfer.
1354 */
1355 if (ipp_is_m2m_cmd(property->cmd)) {
1356 if (!wait_for_completion_timeout
1357 (&c_node->start_complete, msecs_to_jiffies(200))) {
1358 DRM_ERROR("timeout event:prop_id[%d]\n",
1359 c_node->property.prop_id);
1360 goto err_unlock;
1361 }
1362 }
1363 break;
1364 case IPP_CTRL_STOP:
1365 case IPP_CTRL_PAUSE:
1366 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1367 c_node);
1368 if (ret) {
1369 DRM_ERROR("failed to stop property.\n");
1370 goto err_unlock;
1371 }
1372
1373 complete(&c_node->stop_complete);
1374 break;
1375 default:
1376 DRM_ERROR("unknown control type\n");
1377 break;
1378 }
1379
YoungJun Chocbc4c332013-06-12 10:44:40 +09001380 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001381
1382err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001383 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001384}
1385
1386static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1387 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1388{
1389 struct drm_device *drm_dev = ippdrv->drm_dev;
1390 struct drm_exynos_ipp_property *property = &c_node->property;
1391 struct drm_exynos_ipp_mem_node *m_node;
1392 struct drm_exynos_ipp_queue_buf qbuf;
1393 struct drm_exynos_ipp_send_event *e;
1394 struct list_head *head;
1395 struct timeval now;
1396 unsigned long flags;
1397 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1398 int ret, i;
1399
1400 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001401 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001402
1403 if (!drm_dev) {
1404 DRM_ERROR("failed to get drm_dev.\n");
1405 return -EINVAL;
1406 }
1407
1408 if (!property) {
1409 DRM_ERROR("failed to get property.\n");
1410 return -EINVAL;
1411 }
1412
YoungJun Cho4d520762014-05-26 10:17:21 +02001413 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001414 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001415 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001416 ret = 0;
1417 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001418 }
1419
YoungJun Cho220db6f2014-05-26 10:17:20 +02001420 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001421 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001422 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001423 ret = 0;
1424 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001425 }
1426
1427 /* check command */
1428 switch (property->cmd) {
1429 case IPP_CMD_M2M:
1430 for_each_ipp_ops(i) {
1431 /* source/destination memory list */
1432 head = &c_node->mem_list[i];
1433
1434 m_node = list_first_entry(head,
1435 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001436
1437 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001438 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001439 i ? "dst" : "src", tbuf_id[i]);
1440
1441 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1442 if (ret)
1443 DRM_ERROR("failed to put m_node.\n");
1444 }
1445 break;
1446 case IPP_CMD_WB:
1447 /* clear buf for finding */
1448 memset(&qbuf, 0x0, sizeof(qbuf));
1449 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1450 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1451
1452 /* get memory node entry */
1453 m_node = ipp_find_mem_node(c_node, &qbuf);
1454 if (!m_node) {
1455 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001456 ret = -ENOMEM;
1457 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001458 }
1459
1460 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1461
1462 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1463 if (ret)
1464 DRM_ERROR("failed to put m_node.\n");
1465 break;
1466 case IPP_CMD_OUTPUT:
1467 /* source memory list */
1468 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1469
1470 m_node = list_first_entry(head,
1471 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001472
1473 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1474
1475 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1476 if (ret)
1477 DRM_ERROR("failed to put m_node.\n");
1478 break;
1479 default:
1480 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001481 ret = -EINVAL;
1482 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001483 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001484 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001485
1486 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1487 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1488 tbuf_id[1], buf_id[1], property->prop_id);
1489
1490 /*
1491 * command node have event list of destination buffer
1492 * If destination buffer enqueue to mem list,
1493 * then we make event and link to event list tail.
1494 * so, we get first event for first enqueued buffer.
1495 */
1496 e = list_first_entry(&c_node->event_list,
1497 struct drm_exynos_ipp_send_event, base.link);
1498
Eunchul Kimcb471f142012-12-14 18:10:31 +09001499 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001500 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001501 e->event.tv_sec = now.tv_sec;
1502 e->event.tv_usec = now.tv_usec;
1503 e->event.prop_id = property->prop_id;
1504
1505 /* set buffer id about source destination */
1506 for_each_ipp_ops(i)
1507 e->event.buf_id[i] = tbuf_id[i];
1508
1509 spin_lock_irqsave(&drm_dev->event_lock, flags);
1510 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1511 wake_up_interruptible(&e->base.file_priv->event_wait);
1512 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001513 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001514
YoungJun Chocbc4c332013-06-12 10:44:40 +09001515 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001516 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1517
1518 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001519
1520err_mem_unlock:
1521 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001522err_event_unlock:
1523 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001524 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001525}
1526
1527void ipp_sched_event(struct work_struct *work)
1528{
1529 struct drm_exynos_ipp_event_work *event_work =
Andrzej Hajda05afb1a2014-08-28 11:07:33 +02001530 container_of(work, struct drm_exynos_ipp_event_work, work);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001531 struct exynos_drm_ippdrv *ippdrv;
1532 struct drm_exynos_ipp_cmd_node *c_node;
1533 int ret;
1534
1535 if (!event_work) {
1536 DRM_ERROR("failed to get event_work.\n");
1537 return;
1538 }
1539
YoungJun Chocbc4c332013-06-12 10:44:40 +09001540 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001541
1542 ippdrv = event_work->ippdrv;
1543 if (!ippdrv) {
1544 DRM_ERROR("failed to get ipp driver.\n");
1545 return;
1546 }
1547
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001548 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001549 if (!c_node) {
1550 DRM_ERROR("failed to get command node.\n");
1551 return;
1552 }
1553
1554 /*
1555 * IPP supports command thread, event thread synchronization.
1556 * If IPP close immediately from user land, then IPP make
1557 * synchronization with command thread, so make complete event.
1558 * or going out operations.
1559 */
1560 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001561 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1562 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001563 goto err_completion;
1564 }
1565
Eunchul Kimcb471f142012-12-14 18:10:31 +09001566 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1567 if (ret) {
1568 DRM_ERROR("failed to send event.\n");
1569 goto err_completion;
1570 }
1571
1572err_completion:
1573 if (ipp_is_m2m_cmd(c_node->property.cmd))
1574 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001575}
1576
1577static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1578{
1579 struct ipp_context *ctx = get_ipp_context(dev);
1580 struct exynos_drm_ippdrv *ippdrv;
1581 int ret, count = 0;
1582
Eunchul Kimcb471f142012-12-14 18:10:31 +09001583 /* get ipp driver entry */
1584 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1585 ippdrv->drm_dev = drm_dev;
1586
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001587 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
1588 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001589 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001590 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001591 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001592 ippdrv->prop_list.ipp_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001593
YoungJun Chocbc4c332013-06-12 10:44:40 +09001594 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001595 count++, (int)ippdrv, ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001596
1597 /* store parent device for node */
1598 ippdrv->parent_dev = dev;
1599
1600 /* store event work queue and handler */
1601 ippdrv->event_workq = ctx->event_workq;
1602 ippdrv->sched_event = ipp_sched_event;
1603 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001604 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001605
1606 if (is_drm_iommu_supported(drm_dev)) {
1607 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1608 if (ret) {
1609 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001610 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001611 }
1612 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001613 }
1614
1615 return 0;
1616
YoungJun Cho075436b2014-05-26 10:17:19 +02001617err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001618 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001619 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1620 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001621 if (is_drm_iommu_supported(drm_dev))
1622 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1623
YoungJun Cho075436b2014-05-26 10:17:19 +02001624 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1625 ippdrv->prop_list.ipp_id);
1626 }
1627
Eunchul Kimcb471f142012-12-14 18:10:31 +09001628 return ret;
1629}
1630
1631static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1632{
1633 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001634 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001635
Eunchul Kimcb471f142012-12-14 18:10:31 +09001636 /* get ipp driver entry */
1637 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001638 if (is_drm_iommu_supported(drm_dev))
1639 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1640
YoungJun Cho075436b2014-05-26 10:17:19 +02001641 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1642 ippdrv->prop_list.ipp_id);
1643
Eunchul Kimcb471f142012-12-14 18:10:31 +09001644 ippdrv->drm_dev = NULL;
1645 exynos_drm_ippdrv_unregister(ippdrv);
1646 }
1647}
1648
1649static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1650 struct drm_file *file)
1651{
1652 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001653
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001654 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001655
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001656 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001657
1658 return 0;
1659}
1660
1661static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1662 struct drm_file *file)
1663{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001664 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001665 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001666 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1667 int count = 0;
1668
Eunchul Kimcb471f142012-12-14 18:10:31 +09001669 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001670 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001671 list_for_each_entry_safe(c_node, tc_node,
1672 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001673 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1674 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001675
Andrzej Hajda21a825e2014-08-28 11:07:28 +02001676 if (c_node->filp == file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001677 /*
1678 * userland goto unnormal state. process killed.
1679 * and close the file.
1680 * so, IPP didn't called stop cmd ctrl.
1681 * so, we are make stop operation in this state.
1682 */
1683 if (c_node->state == IPP_STATE_START) {
1684 ipp_stop_property(drm_dev, ippdrv,
1685 c_node);
1686 c_node->state = IPP_STATE_STOP;
1687 }
1688
1689 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001690 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001691 if (list_empty(&ippdrv->cmd_list))
1692 pm_runtime_put_sync(ippdrv->dev);
1693 }
1694 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001695 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001696 }
1697
Eunchul Kimcb471f142012-12-14 18:10:31 +09001698 return;
1699}
1700
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001701static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001702{
1703 struct device *dev = &pdev->dev;
1704 struct ipp_context *ctx;
1705 struct exynos_drm_subdrv *subdrv;
1706 int ret;
1707
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001708 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001709 if (!ctx)
1710 return -ENOMEM;
1711
Eunchul Kimcb471f142012-12-14 18:10:31 +09001712 mutex_init(&ctx->ipp_lock);
1713 mutex_init(&ctx->prop_lock);
1714
1715 idr_init(&ctx->ipp_idr);
1716 idr_init(&ctx->prop_idr);
1717
1718 /*
1719 * create single thread for ipp event
1720 * IPP supports event thread for IPP drivers.
1721 * IPP driver send event_work to this thread.
1722 * and IPP event thread send event to user process.
1723 */
1724 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1725 if (!ctx->event_workq) {
1726 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301727 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001728 }
1729
1730 /*
1731 * create single thread for ipp command
1732 * IPP supports command thread for user process.
1733 * user process make command node using set property ioctl.
1734 * and make start_work and send this work to command thread.
1735 * and then this command thread start property.
1736 */
1737 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1738 if (!ctx->cmd_workq) {
1739 dev_err(dev, "failed to create cmd workqueue\n");
1740 ret = -EINVAL;
1741 goto err_event_workq;
1742 }
1743
1744 /* set sub driver informations */
1745 subdrv = &ctx->subdrv;
1746 subdrv->dev = dev;
1747 subdrv->probe = ipp_subdrv_probe;
1748 subdrv->remove = ipp_subdrv_remove;
1749 subdrv->open = ipp_subdrv_open;
1750 subdrv->close = ipp_subdrv_close;
1751
1752 platform_set_drvdata(pdev, ctx);
1753
1754 ret = exynos_drm_subdrv_register(subdrv);
1755 if (ret < 0) {
1756 DRM_ERROR("failed to register drm ipp device.\n");
1757 goto err_cmd_workq;
1758 }
1759
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001760 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001761
1762 return 0;
1763
1764err_cmd_workq:
1765 destroy_workqueue(ctx->cmd_workq);
1766err_event_workq:
1767 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001768 return ret;
1769}
1770
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001771static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001772{
1773 struct ipp_context *ctx = platform_get_drvdata(pdev);
1774
Eunchul Kimcb471f142012-12-14 18:10:31 +09001775 /* unregister sub driver */
1776 exynos_drm_subdrv_unregister(&ctx->subdrv);
1777
1778 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001779 idr_destroy(&ctx->ipp_idr);
1780 idr_destroy(&ctx->prop_idr);
1781
1782 mutex_destroy(&ctx->ipp_lock);
1783 mutex_destroy(&ctx->prop_lock);
1784
1785 /* destroy command, event work queue */
1786 destroy_workqueue(ctx->cmd_workq);
1787 destroy_workqueue(ctx->event_workq);
1788
Eunchul Kimcb471f142012-12-14 18:10:31 +09001789 return 0;
1790}
1791
Eunchul Kimcb471f142012-12-14 18:10:31 +09001792struct platform_driver ipp_driver = {
1793 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001794 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001795 .driver = {
1796 .name = "exynos-drm-ipp",
1797 .owner = THIS_MODULE,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001798 },
1799};
1800