blob: 05f103e9306df99e53640dc7a0c441e20c9d6347 [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,
532 struct drm_file *file,
533 struct drm_exynos_ipp_cmd_node *c_node,
534 struct drm_exynos_ipp_queue_buf *qbuf)
535{
536 struct drm_exynos_ipp_mem_node *m_node;
537 struct drm_exynos_ipp_buf_info *buf_info;
538 int i;
539
540 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
541 if (!m_node)
542 return ERR_PTR(-ENOMEM);
543
544 buf_info = &m_node->buf_info;
545
546 /* operations, buffer id */
547 m_node->ops_id = qbuf->ops_id;
548 m_node->prop_id = qbuf->prop_id;
549 m_node->buf_id = qbuf->buf_id;
550 INIT_LIST_HEAD(&m_node->list);
551
552 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
553 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
554
555 for_each_ipp_planar(i) {
556 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
557
558 /* get dma address by handle */
559 if (qbuf->handle[i]) {
560 dma_addr_t *addr;
561
562 addr = exynos_drm_gem_get_dma_addr(drm_dev,
563 qbuf->handle[i], file);
564 if (IS_ERR(addr)) {
565 DRM_ERROR("failed to get addr.\n");
566 ipp_put_mem_node(drm_dev, c_node, m_node);
567 return ERR_PTR(-EFAULT);
568 }
569
570 buf_info->handles[i] = qbuf->handle[i];
571 buf_info->base[i] = *addr;
572 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
573 buf_info->base[i], buf_info->handles[i]);
574 }
575 }
576
577 mutex_lock(&c_node->mem_lock);
578 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
579 mutex_unlock(&c_node->mem_lock);
580
581 return m_node;
582}
583
584static void ipp_clean_mem_nodes(struct drm_device *drm_dev,
585 struct drm_exynos_ipp_cmd_node *c_node, int ops)
586{
587 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
588 struct list_head *head = &c_node->mem_list[ops];
589
590 mutex_lock(&c_node->mem_lock);
591
592 list_for_each_entry_safe(m_node, tm_node, head, list) {
593 int ret;
594
595 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
596 if (ret)
597 DRM_ERROR("failed to put m_node.\n");
598 }
599
600 mutex_unlock(&c_node->mem_lock);
601}
602
YoungJun Cho075436b2014-05-26 10:17:19 +0200603static void ipp_clean_cmd_node(struct ipp_context *ctx,
604 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900605{
Andrzej Hajda6f7d48e2014-08-28 11:07:26 +0200606 /* cancel works */
607 cancel_work_sync(&c_node->start_work->work);
608 cancel_work_sync(&c_node->stop_work->work);
609 cancel_work_sync(&c_node->event_work->work);
610
Eunchul Kimcb471f142012-12-14 18:10:31 +0900611 /* delete list */
612 list_del(&c_node->list);
613
YoungJun Cho075436b2014-05-26 10:17:19 +0200614 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
615 c_node->property.prop_id);
616
Eunchul Kimcb471f142012-12-14 18:10:31 +0900617 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200618 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900619 mutex_destroy(&c_node->mem_lock);
620 mutex_destroy(&c_node->event_lock);
621
622 /* free command node */
623 kfree(c_node->start_work);
624 kfree(c_node->stop_work);
625 kfree(c_node->event_work);
626 kfree(c_node);
627}
628
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200629static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900630{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200631 switch (c_node->property.cmd) {
632 case IPP_CMD_WB:
633 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
634 case IPP_CMD_OUTPUT:
635 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
636 case IPP_CMD_M2M:
637 default:
638 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
639 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900640 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900641}
642
643static struct drm_exynos_ipp_mem_node
644 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
645 struct drm_exynos_ipp_queue_buf *qbuf)
646{
647 struct drm_exynos_ipp_mem_node *m_node;
648 struct list_head *head;
649 int count = 0;
650
YoungJun Chocbc4c332013-06-12 10:44:40 +0900651 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900652
653 /* source/destination memory list */
654 head = &c_node->mem_list[qbuf->ops_id];
655
656 /* find memory node from memory list */
657 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900658 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900659
660 /* compare buffer id */
661 if (m_node->buf_id == qbuf->buf_id)
662 return m_node;
663 }
664
665 return NULL;
666}
667
668static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
669 struct drm_exynos_ipp_cmd_node *c_node,
670 struct drm_exynos_ipp_mem_node *m_node)
671{
672 struct exynos_drm_ipp_ops *ops = NULL;
673 int ret = 0;
674
YoungJun Chocbc4c332013-06-12 10:44:40 +0900675 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900676
677 if (!m_node) {
678 DRM_ERROR("invalid queue node.\n");
679 return -EFAULT;
680 }
681
YoungJun Chocbc4c332013-06-12 10:44:40 +0900682 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900683
684 /* get operations callback */
685 ops = ippdrv->ops[m_node->ops_id];
686 if (!ops) {
687 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200688 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900689 }
690
691 /* set address and enable irq */
692 if (ops->set_addr) {
693 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
694 m_node->buf_id, IPP_BUF_ENQUEUE);
695 if (ret) {
696 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200697 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900698 }
699 }
700
Eunchul Kimcb471f142012-12-14 18:10:31 +0900701 return ret;
702}
703
Eunchul Kimcb471f142012-12-14 18:10:31 +0900704static void ipp_free_event(struct drm_pending_event *event)
705{
706 kfree(event);
707}
708
709static int ipp_get_event(struct drm_device *drm_dev,
710 struct drm_file *file,
711 struct drm_exynos_ipp_cmd_node *c_node,
712 struct drm_exynos_ipp_queue_buf *qbuf)
713{
714 struct drm_exynos_ipp_send_event *e;
715 unsigned long flags;
716
YoungJun Chocbc4c332013-06-12 10:44:40 +0900717 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900718
719 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900720 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900721 spin_lock_irqsave(&drm_dev->event_lock, flags);
722 file->event_space += sizeof(e->event);
723 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
724 return -ENOMEM;
725 }
726
727 /* make event */
728 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
729 e->event.base.length = sizeof(e->event);
730 e->event.user_data = qbuf->user_data;
731 e->event.prop_id = qbuf->prop_id;
732 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
733 e->base.event = &e->event.base;
734 e->base.file_priv = file;
735 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200736 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900737 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200738 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900739
740 return 0;
741}
742
743static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
744 struct drm_exynos_ipp_queue_buf *qbuf)
745{
746 struct drm_exynos_ipp_send_event *e, *te;
747 int count = 0;
748
YoungJun Cho4d520762014-05-26 10:17:21 +0200749 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900750 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900751 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900752
753 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530754 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900755 * stop operations want to delete all event list.
756 * another case delete only same buf id.
757 */
758 if (!qbuf) {
759 /* delete list */
760 list_del(&e->base.link);
761 kfree(e);
762 }
763
764 /* compare buffer id */
765 if (qbuf && (qbuf->buf_id ==
766 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
767 /* delete list */
768 list_del(&e->base.link);
769 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200770 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900771 }
772 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200773
774out_unlock:
775 mutex_unlock(&c_node->event_lock);
776 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900777}
778
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530779static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900780 struct exynos_drm_ippdrv *ippdrv,
781 struct drm_exynos_ipp_cmd_work *cmd_work,
782 struct drm_exynos_ipp_cmd_node *c_node)
783{
784 struct ipp_context *ctx = get_ipp_context(dev);
785
786 cmd_work->ippdrv = ippdrv;
787 cmd_work->c_node = c_node;
788 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
789}
790
791static int ipp_queue_buf_with_run(struct device *dev,
792 struct drm_exynos_ipp_cmd_node *c_node,
793 struct drm_exynos_ipp_mem_node *m_node,
794 struct drm_exynos_ipp_queue_buf *qbuf)
795{
796 struct exynos_drm_ippdrv *ippdrv;
797 struct drm_exynos_ipp_property *property;
798 struct exynos_drm_ipp_ops *ops;
799 int ret;
800
Eunchul Kimcb471f142012-12-14 18:10:31 +0900801 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530802 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900803 DRM_ERROR("failed to get ipp driver.\n");
804 return -EFAULT;
805 }
806
807 ops = ippdrv->ops[qbuf->ops_id];
808 if (!ops) {
809 DRM_ERROR("failed to get ops.\n");
810 return -EFAULT;
811 }
812
813 property = &c_node->property;
814
815 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900816 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900817 return 0;
818 }
819
YoungJun Cho220db6f2014-05-26 10:17:20 +0200820 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900821 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200822 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900823 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900824 return 0;
825 }
826
827 /*
828 * If set destination buffer and enabled clock,
829 * then m2m operations need start operations at queue_buf
830 */
831 if (ipp_is_m2m_cmd(property->cmd)) {
832 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
833
834 cmd_work->ctrl = IPP_CTRL_PLAY;
835 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
836 } else {
837 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
838 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200839 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900840 DRM_ERROR("failed to set m node.\n");
841 return ret;
842 }
843 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200844 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900845
846 return 0;
847}
848
849static void ipp_clean_queue_buf(struct drm_device *drm_dev,
850 struct drm_exynos_ipp_cmd_node *c_node,
851 struct drm_exynos_ipp_queue_buf *qbuf)
852{
853 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
854
YoungJun Choc66ce402014-05-26 10:17:15 +0200855 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200856 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200857 list_for_each_entry_safe(m_node, tm_node,
858 &c_node->mem_list[qbuf->ops_id], list) {
859 if (m_node->buf_id == qbuf->buf_id &&
860 m_node->ops_id == qbuf->ops_id)
861 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900862 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200863 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900864}
865
866int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
867 struct drm_file *file)
868{
869 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200870 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900871 struct ipp_context *ctx = get_ipp_context(dev);
872 struct drm_exynos_ipp_queue_buf *qbuf = data;
873 struct drm_exynos_ipp_cmd_node *c_node;
874 struct drm_exynos_ipp_mem_node *m_node;
875 int ret;
876
Eunchul Kimcb471f142012-12-14 18:10:31 +0900877 if (!qbuf) {
878 DRM_ERROR("invalid buf parameter.\n");
879 return -EINVAL;
880 }
881
882 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
883 DRM_ERROR("invalid ops parameter.\n");
884 return -EINVAL;
885 }
886
YoungJun Chocbc4c332013-06-12 10:44:40 +0900887 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
888 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900889 qbuf->buf_id, qbuf->buf_type);
890
891 /* find command node */
892 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
893 qbuf->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200894 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900895 DRM_ERROR("failed to get command node.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200896 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900897 }
898
899 /* buffer control */
900 switch (qbuf->buf_type) {
901 case IPP_BUF_ENQUEUE:
902 /* get memory node */
903 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
904 if (IS_ERR(m_node)) {
905 DRM_ERROR("failed to get m_node.\n");
906 return PTR_ERR(m_node);
907 }
908
909 /*
910 * first step get event for destination buffer.
911 * and second step when M2M case run with destination buffer
912 * if needed.
913 */
914 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
915 /* get event for destination buffer */
916 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
917 if (ret) {
918 DRM_ERROR("failed to get event.\n");
919 goto err_clean_node;
920 }
921
922 /*
923 * M2M case run play control for streaming feature.
924 * other case set address and waiting.
925 */
926 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
927 if (ret) {
928 DRM_ERROR("failed to run command.\n");
929 goto err_clean_node;
930 }
931 }
932 break;
933 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200934 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900935
936 /* put event for destination buffer */
937 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
938 ipp_put_event(c_node, qbuf);
939
940 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
941
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200942 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900943 break;
944 default:
945 DRM_ERROR("invalid buffer control.\n");
946 return -EINVAL;
947 }
948
949 return 0;
950
951err_clean_node:
952 DRM_ERROR("clean memory nodes.\n");
953
954 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
955 return ret;
956}
957
958static bool exynos_drm_ipp_check_valid(struct device *dev,
959 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
960{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900961 if (ctrl != IPP_CTRL_PLAY) {
962 if (pm_runtime_suspended(dev)) {
963 DRM_ERROR("pm:runtime_suspended.\n");
964 goto err_status;
965 }
966 }
967
968 switch (ctrl) {
969 case IPP_CTRL_PLAY:
970 if (state != IPP_STATE_IDLE)
971 goto err_status;
972 break;
973 case IPP_CTRL_STOP:
974 if (state == IPP_STATE_STOP)
975 goto err_status;
976 break;
977 case IPP_CTRL_PAUSE:
978 if (state != IPP_STATE_START)
979 goto err_status;
980 break;
981 case IPP_CTRL_RESUME:
982 if (state != IPP_STATE_STOP)
983 goto err_status;
984 break;
985 default:
986 DRM_ERROR("invalid state.\n");
987 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900988 }
989
990 return true;
991
992err_status:
993 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
994 return false;
995}
996
997int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
998 struct drm_file *file)
999{
1000 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001001 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001002 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001003 struct ipp_context *ctx = get_ipp_context(dev);
1004 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1005 struct drm_exynos_ipp_cmd_work *cmd_work;
1006 struct drm_exynos_ipp_cmd_node *c_node;
1007
Eunchul Kimcb471f142012-12-14 18:10:31 +09001008 if (!ctx) {
1009 DRM_ERROR("invalid context.\n");
1010 return -EINVAL;
1011 }
1012
1013 if (!cmd_ctrl) {
1014 DRM_ERROR("invalid control parameter.\n");
1015 return -EINVAL;
1016 }
1017
YoungJun Chocbc4c332013-06-12 10:44:40 +09001018 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001019 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1020
1021 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1022 if (IS_ERR(ippdrv)) {
1023 DRM_ERROR("failed to get ipp driver.\n");
1024 return PTR_ERR(ippdrv);
1025 }
1026
1027 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1028 cmd_ctrl->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001029 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001030 DRM_ERROR("invalid command node list.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001031 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001032 }
1033
1034 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1035 c_node->state)) {
1036 DRM_ERROR("invalid state.\n");
1037 return -EINVAL;
1038 }
1039
1040 switch (cmd_ctrl->ctrl) {
1041 case IPP_CTRL_PLAY:
1042 if (pm_runtime_suspended(ippdrv->dev))
1043 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001044
Eunchul Kimcb471f142012-12-14 18:10:31 +09001045 c_node->state = IPP_STATE_START;
1046
1047 cmd_work = c_node->start_work;
1048 cmd_work->ctrl = cmd_ctrl->ctrl;
1049 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001050 break;
1051 case IPP_CTRL_STOP:
1052 cmd_work = c_node->stop_work;
1053 cmd_work->ctrl = cmd_ctrl->ctrl;
1054 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1055
1056 if (!wait_for_completion_timeout(&c_node->stop_complete,
1057 msecs_to_jiffies(300))) {
1058 DRM_ERROR("timeout stop:prop_id[%d]\n",
1059 c_node->property.prop_id);
1060 }
1061
1062 c_node->state = IPP_STATE_STOP;
1063 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001064 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001065 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001066
1067 if (list_empty(&ippdrv->cmd_list))
1068 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001069 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001070 break;
1071 case IPP_CTRL_PAUSE:
1072 cmd_work = c_node->stop_work;
1073 cmd_work->ctrl = cmd_ctrl->ctrl;
1074 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1075
1076 if (!wait_for_completion_timeout(&c_node->stop_complete,
1077 msecs_to_jiffies(200))) {
1078 DRM_ERROR("timeout stop:prop_id[%d]\n",
1079 c_node->property.prop_id);
1080 }
1081
1082 c_node->state = IPP_STATE_STOP;
1083 break;
1084 case IPP_CTRL_RESUME:
1085 c_node->state = IPP_STATE_START;
1086 cmd_work = c_node->start_work;
1087 cmd_work->ctrl = cmd_ctrl->ctrl;
1088 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1089 break;
1090 default:
1091 DRM_ERROR("could not support this state currently.\n");
1092 return -EINVAL;
1093 }
1094
YoungJun Chocbc4c332013-06-12 10:44:40 +09001095 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001096 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1097
1098 return 0;
1099}
1100
1101int exynos_drm_ippnb_register(struct notifier_block *nb)
1102{
1103 return blocking_notifier_chain_register(
1104 &exynos_drm_ippnb_list, nb);
1105}
1106
1107int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1108{
1109 return blocking_notifier_chain_unregister(
1110 &exynos_drm_ippnb_list, nb);
1111}
1112
1113int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1114{
1115 return blocking_notifier_call_chain(
1116 &exynos_drm_ippnb_list, val, v);
1117}
1118
1119static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1120 struct drm_exynos_ipp_property *property)
1121{
1122 struct exynos_drm_ipp_ops *ops = NULL;
1123 bool swap = false;
1124 int ret, i;
1125
1126 if (!property) {
1127 DRM_ERROR("invalid property parameter.\n");
1128 return -EINVAL;
1129 }
1130
YoungJun Chocbc4c332013-06-12 10:44:40 +09001131 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001132
1133 /* reset h/w block */
1134 if (ippdrv->reset &&
1135 ippdrv->reset(ippdrv->dev)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001136 return -EINVAL;
1137 }
1138
1139 /* set source,destination operations */
1140 for_each_ipp_ops(i) {
1141 struct drm_exynos_ipp_config *config =
1142 &property->config[i];
1143
1144 ops = ippdrv->ops[i];
1145 if (!ops || !config) {
1146 DRM_ERROR("not support ops and config.\n");
1147 return -EINVAL;
1148 }
1149
1150 /* set format */
1151 if (ops->set_fmt) {
1152 ret = ops->set_fmt(ippdrv->dev, config->fmt);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001153 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001154 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001155 }
1156
1157 /* set transform for rotation, flip */
1158 if (ops->set_transf) {
1159 ret = ops->set_transf(ippdrv->dev, config->degree,
1160 config->flip, &swap);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001161 if (ret)
1162 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001163 }
1164
1165 /* set size */
1166 if (ops->set_size) {
1167 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1168 &config->sz);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001169 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001170 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001171 }
1172 }
1173
1174 return 0;
1175}
1176
1177static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1178 struct drm_exynos_ipp_cmd_node *c_node)
1179{
1180 struct drm_exynos_ipp_mem_node *m_node;
1181 struct drm_exynos_ipp_property *property = &c_node->property;
1182 struct list_head *head;
1183 int ret, i;
1184
YoungJun Chocbc4c332013-06-12 10:44:40 +09001185 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001186
1187 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001188 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001189
YoungJun Cho220db6f2014-05-26 10:17:20 +02001190 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001191 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001192 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001193 ret = -ENOMEM;
1194 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001195 }
1196
1197 /* set current property in ippdrv */
1198 ret = ipp_set_property(ippdrv, property);
1199 if (ret) {
1200 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001201 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001202 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001203 }
1204
1205 /* check command */
1206 switch (property->cmd) {
1207 case IPP_CMD_M2M:
1208 for_each_ipp_ops(i) {
1209 /* source/destination memory list */
1210 head = &c_node->mem_list[i];
1211
1212 m_node = list_first_entry(head,
1213 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001214
YoungJun Chocbc4c332013-06-12 10:44:40 +09001215 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001216
1217 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1218 if (ret) {
1219 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001220 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001221 }
1222 }
1223 break;
1224 case IPP_CMD_WB:
1225 /* destination memory list */
1226 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1227
1228 list_for_each_entry(m_node, head, list) {
1229 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1230 if (ret) {
1231 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001232 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001233 }
1234 }
1235 break;
1236 case IPP_CMD_OUTPUT:
1237 /* source memory list */
1238 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1239
1240 list_for_each_entry(m_node, head, list) {
1241 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1242 if (ret) {
1243 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001244 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001245 }
1246 }
1247 break;
1248 default:
1249 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001250 ret = -EINVAL;
1251 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001252 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001253 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001254
YoungJun Chocbc4c332013-06-12 10:44:40 +09001255 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001256
1257 /* start operations */
1258 if (ippdrv->start) {
1259 ret = ippdrv->start(ippdrv->dev, property->cmd);
1260 if (ret) {
1261 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001262 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001263 return ret;
1264 }
1265 }
1266
1267 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001268
1269err_unlock:
1270 mutex_unlock(&c_node->mem_lock);
1271 ippdrv->c_node = NULL;
1272 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001273}
1274
1275static int ipp_stop_property(struct drm_device *drm_dev,
1276 struct exynos_drm_ippdrv *ippdrv,
1277 struct drm_exynos_ipp_cmd_node *c_node)
1278{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001279 struct drm_exynos_ipp_property *property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001280 int ret = 0, i;
1281
YoungJun Chocbc4c332013-06-12 10:44:40 +09001282 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001283
1284 /* put event */
1285 ipp_put_event(c_node, NULL);
1286
1287 /* check command */
1288 switch (property->cmd) {
1289 case IPP_CMD_M2M:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001290 for_each_ipp_ops(i)
1291 ipp_clean_mem_nodes(drm_dev, c_node, i);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001292 break;
1293 case IPP_CMD_WB:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001294 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_DST);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001295 break;
1296 case IPP_CMD_OUTPUT:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001297 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_SRC);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001298 break;
1299 default:
1300 DRM_ERROR("invalid operations.\n");
1301 ret = -EINVAL;
1302 goto err_clear;
1303 }
1304
1305err_clear:
1306 /* stop operations */
1307 if (ippdrv->stop)
1308 ippdrv->stop(ippdrv->dev, property->cmd);
1309
1310 return ret;
1311}
1312
1313void ipp_sched_cmd(struct work_struct *work)
1314{
1315 struct drm_exynos_ipp_cmd_work *cmd_work =
1316 (struct drm_exynos_ipp_cmd_work *)work;
1317 struct exynos_drm_ippdrv *ippdrv;
1318 struct drm_exynos_ipp_cmd_node *c_node;
1319 struct drm_exynos_ipp_property *property;
1320 int ret;
1321
Eunchul Kimcb471f142012-12-14 18:10:31 +09001322 ippdrv = cmd_work->ippdrv;
1323 if (!ippdrv) {
1324 DRM_ERROR("invalid ippdrv list.\n");
1325 return;
1326 }
1327
1328 c_node = cmd_work->c_node;
1329 if (!c_node) {
1330 DRM_ERROR("invalid command node list.\n");
1331 return;
1332 }
1333
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001334 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001335
1336 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001337
1338 switch (cmd_work->ctrl) {
1339 case IPP_CTRL_PLAY:
1340 case IPP_CTRL_RESUME:
1341 ret = ipp_start_property(ippdrv, c_node);
1342 if (ret) {
1343 DRM_ERROR("failed to start property:prop_id[%d]\n",
1344 c_node->property.prop_id);
1345 goto err_unlock;
1346 }
1347
1348 /*
1349 * M2M case supports wait_completion of transfer.
1350 * because M2M case supports single unit operation
1351 * with multiple queue.
1352 * M2M need to wait completion of data transfer.
1353 */
1354 if (ipp_is_m2m_cmd(property->cmd)) {
1355 if (!wait_for_completion_timeout
1356 (&c_node->start_complete, msecs_to_jiffies(200))) {
1357 DRM_ERROR("timeout event:prop_id[%d]\n",
1358 c_node->property.prop_id);
1359 goto err_unlock;
1360 }
1361 }
1362 break;
1363 case IPP_CTRL_STOP:
1364 case IPP_CTRL_PAUSE:
1365 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1366 c_node);
1367 if (ret) {
1368 DRM_ERROR("failed to stop property.\n");
1369 goto err_unlock;
1370 }
1371
1372 complete(&c_node->stop_complete);
1373 break;
1374 default:
1375 DRM_ERROR("unknown control type\n");
1376 break;
1377 }
1378
YoungJun Chocbc4c332013-06-12 10:44:40 +09001379 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001380
1381err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001382 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001383}
1384
1385static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1386 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1387{
1388 struct drm_device *drm_dev = ippdrv->drm_dev;
1389 struct drm_exynos_ipp_property *property = &c_node->property;
1390 struct drm_exynos_ipp_mem_node *m_node;
1391 struct drm_exynos_ipp_queue_buf qbuf;
1392 struct drm_exynos_ipp_send_event *e;
1393 struct list_head *head;
1394 struct timeval now;
1395 unsigned long flags;
1396 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1397 int ret, i;
1398
1399 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001400 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001401
1402 if (!drm_dev) {
1403 DRM_ERROR("failed to get drm_dev.\n");
1404 return -EINVAL;
1405 }
1406
1407 if (!property) {
1408 DRM_ERROR("failed to get property.\n");
1409 return -EINVAL;
1410 }
1411
YoungJun Cho4d520762014-05-26 10:17:21 +02001412 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001413 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001414 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001415 ret = 0;
1416 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001417 }
1418
YoungJun Cho220db6f2014-05-26 10:17:20 +02001419 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001420 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001421 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001422 ret = 0;
1423 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001424 }
1425
1426 /* check command */
1427 switch (property->cmd) {
1428 case IPP_CMD_M2M:
1429 for_each_ipp_ops(i) {
1430 /* source/destination memory list */
1431 head = &c_node->mem_list[i];
1432
1433 m_node = list_first_entry(head,
1434 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001435
1436 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001437 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001438 i ? "dst" : "src", tbuf_id[i]);
1439
1440 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1441 if (ret)
1442 DRM_ERROR("failed to put m_node.\n");
1443 }
1444 break;
1445 case IPP_CMD_WB:
1446 /* clear buf for finding */
1447 memset(&qbuf, 0x0, sizeof(qbuf));
1448 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1449 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1450
1451 /* get memory node entry */
1452 m_node = ipp_find_mem_node(c_node, &qbuf);
1453 if (!m_node) {
1454 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001455 ret = -ENOMEM;
1456 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001457 }
1458
1459 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1460
1461 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1462 if (ret)
1463 DRM_ERROR("failed to put m_node.\n");
1464 break;
1465 case IPP_CMD_OUTPUT:
1466 /* source memory list */
1467 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1468
1469 m_node = list_first_entry(head,
1470 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001471
1472 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1473
1474 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1475 if (ret)
1476 DRM_ERROR("failed to put m_node.\n");
1477 break;
1478 default:
1479 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001480 ret = -EINVAL;
1481 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001482 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001483 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001484
1485 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1486 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1487 tbuf_id[1], buf_id[1], property->prop_id);
1488
1489 /*
1490 * command node have event list of destination buffer
1491 * If destination buffer enqueue to mem list,
1492 * then we make event and link to event list tail.
1493 * so, we get first event for first enqueued buffer.
1494 */
1495 e = list_first_entry(&c_node->event_list,
1496 struct drm_exynos_ipp_send_event, base.link);
1497
Eunchul Kimcb471f142012-12-14 18:10:31 +09001498 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001499 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001500 e->event.tv_sec = now.tv_sec;
1501 e->event.tv_usec = now.tv_usec;
1502 e->event.prop_id = property->prop_id;
1503
1504 /* set buffer id about source destination */
1505 for_each_ipp_ops(i)
1506 e->event.buf_id[i] = tbuf_id[i];
1507
1508 spin_lock_irqsave(&drm_dev->event_lock, flags);
1509 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1510 wake_up_interruptible(&e->base.file_priv->event_wait);
1511 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001512 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001513
YoungJun Chocbc4c332013-06-12 10:44:40 +09001514 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001515 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1516
1517 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001518
1519err_mem_unlock:
1520 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001521err_event_unlock:
1522 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001523 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001524}
1525
1526void ipp_sched_event(struct work_struct *work)
1527{
1528 struct drm_exynos_ipp_event_work *event_work =
1529 (struct drm_exynos_ipp_event_work *)work;
1530 struct exynos_drm_ippdrv *ippdrv;
1531 struct drm_exynos_ipp_cmd_node *c_node;
1532 int ret;
1533
1534 if (!event_work) {
1535 DRM_ERROR("failed to get event_work.\n");
1536 return;
1537 }
1538
YoungJun Chocbc4c332013-06-12 10:44:40 +09001539 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001540
1541 ippdrv = event_work->ippdrv;
1542 if (!ippdrv) {
1543 DRM_ERROR("failed to get ipp driver.\n");
1544 return;
1545 }
1546
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001547 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001548 if (!c_node) {
1549 DRM_ERROR("failed to get command node.\n");
1550 return;
1551 }
1552
1553 /*
1554 * IPP supports command thread, event thread synchronization.
1555 * If IPP close immediately from user land, then IPP make
1556 * synchronization with command thread, so make complete event.
1557 * or going out operations.
1558 */
1559 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001560 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1561 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001562 goto err_completion;
1563 }
1564
Eunchul Kimcb471f142012-12-14 18:10:31 +09001565 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1566 if (ret) {
1567 DRM_ERROR("failed to send event.\n");
1568 goto err_completion;
1569 }
1570
1571err_completion:
1572 if (ipp_is_m2m_cmd(c_node->property.cmd))
1573 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001574}
1575
1576static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1577{
1578 struct ipp_context *ctx = get_ipp_context(dev);
1579 struct exynos_drm_ippdrv *ippdrv;
1580 int ret, count = 0;
1581
Eunchul Kimcb471f142012-12-14 18:10:31 +09001582 /* get ipp driver entry */
1583 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1584 ippdrv->drm_dev = drm_dev;
1585
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001586 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
1587 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001588 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001589 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001590 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001591 ippdrv->prop_list.ipp_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001592
YoungJun Chocbc4c332013-06-12 10:44:40 +09001593 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001594 count++, (int)ippdrv, ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001595
1596 /* store parent device for node */
1597 ippdrv->parent_dev = dev;
1598
1599 /* store event work queue and handler */
1600 ippdrv->event_workq = ctx->event_workq;
1601 ippdrv->sched_event = ipp_sched_event;
1602 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001603 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001604
1605 if (is_drm_iommu_supported(drm_dev)) {
1606 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1607 if (ret) {
1608 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001609 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001610 }
1611 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001612 }
1613
1614 return 0;
1615
YoungJun Cho075436b2014-05-26 10:17:19 +02001616err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001617 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001618 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1619 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001620 if (is_drm_iommu_supported(drm_dev))
1621 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1622
YoungJun Cho075436b2014-05-26 10:17:19 +02001623 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1624 ippdrv->prop_list.ipp_id);
1625 }
1626
Eunchul Kimcb471f142012-12-14 18:10:31 +09001627 return ret;
1628}
1629
1630static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1631{
1632 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001633 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001634
Eunchul Kimcb471f142012-12-14 18:10:31 +09001635 /* get ipp driver entry */
1636 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, 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
Eunchul Kimcb471f142012-12-14 18:10:31 +09001643 ippdrv->drm_dev = NULL;
1644 exynos_drm_ippdrv_unregister(ippdrv);
1645 }
1646}
1647
1648static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1649 struct drm_file *file)
1650{
1651 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001652
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001653 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001654
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001655 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001656
1657 return 0;
1658}
1659
1660static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1661 struct drm_file *file)
1662{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001663 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001664 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001665 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1666 int count = 0;
1667
Eunchul Kimcb471f142012-12-14 18:10:31 +09001668 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001669 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001670 list_for_each_entry_safe(c_node, tc_node,
1671 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001672 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1673 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001674
Andrzej Hajda21a825e2014-08-28 11:07:28 +02001675 if (c_node->filp == file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001676 /*
1677 * userland goto unnormal state. process killed.
1678 * and close the file.
1679 * so, IPP didn't called stop cmd ctrl.
1680 * so, we are make stop operation in this state.
1681 */
1682 if (c_node->state == IPP_STATE_START) {
1683 ipp_stop_property(drm_dev, ippdrv,
1684 c_node);
1685 c_node->state = IPP_STATE_STOP;
1686 }
1687
1688 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001689 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001690 if (list_empty(&ippdrv->cmd_list))
1691 pm_runtime_put_sync(ippdrv->dev);
1692 }
1693 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001694 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001695 }
1696
Eunchul Kimcb471f142012-12-14 18:10:31 +09001697 return;
1698}
1699
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001700static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001701{
1702 struct device *dev = &pdev->dev;
1703 struct ipp_context *ctx;
1704 struct exynos_drm_subdrv *subdrv;
1705 int ret;
1706
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001707 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001708 if (!ctx)
1709 return -ENOMEM;
1710
Eunchul Kimcb471f142012-12-14 18:10:31 +09001711 mutex_init(&ctx->ipp_lock);
1712 mutex_init(&ctx->prop_lock);
1713
1714 idr_init(&ctx->ipp_idr);
1715 idr_init(&ctx->prop_idr);
1716
1717 /*
1718 * create single thread for ipp event
1719 * IPP supports event thread for IPP drivers.
1720 * IPP driver send event_work to this thread.
1721 * and IPP event thread send event to user process.
1722 */
1723 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1724 if (!ctx->event_workq) {
1725 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301726 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001727 }
1728
1729 /*
1730 * create single thread for ipp command
1731 * IPP supports command thread for user process.
1732 * user process make command node using set property ioctl.
1733 * and make start_work and send this work to command thread.
1734 * and then this command thread start property.
1735 */
1736 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1737 if (!ctx->cmd_workq) {
1738 dev_err(dev, "failed to create cmd workqueue\n");
1739 ret = -EINVAL;
1740 goto err_event_workq;
1741 }
1742
1743 /* set sub driver informations */
1744 subdrv = &ctx->subdrv;
1745 subdrv->dev = dev;
1746 subdrv->probe = ipp_subdrv_probe;
1747 subdrv->remove = ipp_subdrv_remove;
1748 subdrv->open = ipp_subdrv_open;
1749 subdrv->close = ipp_subdrv_close;
1750
1751 platform_set_drvdata(pdev, ctx);
1752
1753 ret = exynos_drm_subdrv_register(subdrv);
1754 if (ret < 0) {
1755 DRM_ERROR("failed to register drm ipp device.\n");
1756 goto err_cmd_workq;
1757 }
1758
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001759 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001760
1761 return 0;
1762
1763err_cmd_workq:
1764 destroy_workqueue(ctx->cmd_workq);
1765err_event_workq:
1766 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001767 return ret;
1768}
1769
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001770static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001771{
1772 struct ipp_context *ctx = platform_get_drvdata(pdev);
1773
Eunchul Kimcb471f142012-12-14 18:10:31 +09001774 /* unregister sub driver */
1775 exynos_drm_subdrv_unregister(&ctx->subdrv);
1776
1777 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001778 idr_destroy(&ctx->ipp_idr);
1779 idr_destroy(&ctx->prop_idr);
1780
1781 mutex_destroy(&ctx->ipp_lock);
1782 mutex_destroy(&ctx->prop_lock);
1783
1784 /* destroy command, event work queue */
1785 destroy_workqueue(ctx->cmd_workq);
1786 destroy_workqueue(ctx->event_workq);
1787
Eunchul Kimcb471f142012-12-14 18:10:31 +09001788 return 0;
1789}
1790
Eunchul Kimcb471f142012-12-14 18:10:31 +09001791struct platform_driver ipp_driver = {
1792 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001793 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001794 .driver = {
1795 .name = "exynos-drm-ipp",
1796 .owner = THIS_MODULE,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001797 },
1798};
1799