blob: bbe99689d1dd731a2cb25ad856cf455eef92ea27 [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 */
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200447 c_node->dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900448 c_node->property = *property;
449 c_node->state = IPP_STATE_IDLE;
Andrzej Hajda945a0aa2014-08-28 11:07:27 +0200450 c_node->filp = file;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900451
452 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530453 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900454 DRM_ERROR("failed to create start work.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +0200455 goto err_remove_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900456 }
457
458 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530459 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900460 DRM_ERROR("failed to create stop work.\n");
461 goto err_free_start;
462 }
463
464 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530465 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900466 DRM_ERROR("failed to create event work.\n");
467 goto err_free_stop;
468 }
469
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200470 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900471 mutex_init(&c_node->mem_lock);
472 mutex_init(&c_node->event_lock);
473
474 init_completion(&c_node->start_complete);
475 init_completion(&c_node->stop_complete);
476
477 for_each_ipp_ops(i)
478 INIT_LIST_HEAD(&c_node->mem_list[i]);
479
480 INIT_LIST_HEAD(&c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200481 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900482 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200483 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900484
485 /* make dedicated state without m2m */
486 if (!ipp_is_m2m_cmd(property->cmd))
487 ippdrv->dedicated = true;
488
489 return 0;
490
491err_free_stop:
492 kfree(c_node->stop_work);
493err_free_start:
494 kfree(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200495err_remove_id:
496 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900497err_clear:
498 kfree(c_node);
499 return ret;
500}
501
YoungJun Cho075436b2014-05-26 10:17:19 +0200502static void ipp_clean_cmd_node(struct ipp_context *ctx,
503 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900504{
Andrzej Hajda6f7d48e2014-08-28 11:07:26 +0200505 /* cancel works */
506 cancel_work_sync(&c_node->start_work->work);
507 cancel_work_sync(&c_node->stop_work->work);
508 cancel_work_sync(&c_node->event_work->work);
509
Eunchul Kimcb471f142012-12-14 18:10:31 +0900510 /* delete list */
511 list_del(&c_node->list);
512
YoungJun Cho075436b2014-05-26 10:17:19 +0200513 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
514 c_node->property.prop_id);
515
Eunchul Kimcb471f142012-12-14 18:10:31 +0900516 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200517 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900518 mutex_destroy(&c_node->mem_lock);
519 mutex_destroy(&c_node->event_lock);
520
521 /* free command node */
522 kfree(c_node->start_work);
523 kfree(c_node->stop_work);
524 kfree(c_node->event_work);
525 kfree(c_node);
526}
527
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200528static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900529{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200530 switch (c_node->property.cmd) {
531 case IPP_CMD_WB:
532 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
533 case IPP_CMD_OUTPUT:
534 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
535 case IPP_CMD_M2M:
536 default:
537 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
538 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900539 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900540}
541
542static struct drm_exynos_ipp_mem_node
543 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
544 struct drm_exynos_ipp_queue_buf *qbuf)
545{
546 struct drm_exynos_ipp_mem_node *m_node;
547 struct list_head *head;
548 int count = 0;
549
YoungJun Chocbc4c332013-06-12 10:44:40 +0900550 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900551
552 /* source/destination memory list */
553 head = &c_node->mem_list[qbuf->ops_id];
554
555 /* find memory node from memory list */
556 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900557 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900558
559 /* compare buffer id */
560 if (m_node->buf_id == qbuf->buf_id)
561 return m_node;
562 }
563
564 return NULL;
565}
566
567static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
568 struct drm_exynos_ipp_cmd_node *c_node,
569 struct drm_exynos_ipp_mem_node *m_node)
570{
571 struct exynos_drm_ipp_ops *ops = NULL;
572 int ret = 0;
573
YoungJun Chocbc4c332013-06-12 10:44:40 +0900574 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900575
576 if (!m_node) {
577 DRM_ERROR("invalid queue node.\n");
578 return -EFAULT;
579 }
580
YoungJun Chocbc4c332013-06-12 10:44:40 +0900581 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900582
583 /* get operations callback */
584 ops = ippdrv->ops[m_node->ops_id];
585 if (!ops) {
586 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200587 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900588 }
589
590 /* set address and enable irq */
591 if (ops->set_addr) {
592 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
593 m_node->buf_id, IPP_BUF_ENQUEUE);
594 if (ret) {
595 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200596 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900597 }
598 }
599
Eunchul Kimcb471f142012-12-14 18:10:31 +0900600 return ret;
601}
602
603static struct drm_exynos_ipp_mem_node
604 *ipp_get_mem_node(struct drm_device *drm_dev,
605 struct drm_file *file,
606 struct drm_exynos_ipp_cmd_node *c_node,
607 struct drm_exynos_ipp_queue_buf *qbuf)
608{
609 struct drm_exynos_ipp_mem_node *m_node;
Andrzej Hajda73b00232014-07-03 15:10:30 +0200610 struct drm_exynos_ipp_buf_info *buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900611 int i;
612
Eunchul Kimcb471f142012-12-14 18:10:31 +0900613 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900614 if (!m_node)
YoungJun Cho220db6f2014-05-26 10:17:20 +0200615 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900616
Andrzej Hajda73b00232014-07-03 15:10:30 +0200617 buf_info = &m_node->buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900618
619 /* operations, buffer id */
620 m_node->ops_id = qbuf->ops_id;
621 m_node->prop_id = qbuf->prop_id;
622 m_node->buf_id = qbuf->buf_id;
623
YoungJun Chocbc4c332013-06-12 10:44:40 +0900624 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
625 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900626
627 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900628 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900629
630 /* get dma address by handle */
631 if (qbuf->handle[i]) {
Andrzej Hajdaa8ea17f2014-07-03 15:10:29 +0200632 dma_addr_t *addr;
633
Eunchul Kimcb471f142012-12-14 18:10:31 +0900634 addr = exynos_drm_gem_get_dma_addr(drm_dev,
635 qbuf->handle[i], file);
636 if (IS_ERR(addr)) {
637 DRM_ERROR("failed to get addr.\n");
638 goto err_clear;
639 }
640
Andrzej Hajda73b00232014-07-03 15:10:30 +0200641 buf_info->handles[i] = qbuf->handle[i];
642 buf_info->base[i] = *addr;
643 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
644 buf_info->base[i], buf_info->handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900645 }
646 }
647
YoungJun Cho220db6f2014-05-26 10:17:20 +0200648 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900649 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900650 mutex_unlock(&c_node->mem_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +0200651
Eunchul Kimcb471f142012-12-14 18:10:31 +0900652 return m_node;
653
654err_clear:
655 kfree(m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900656 return ERR_PTR(-EFAULT);
657}
658
659static int ipp_put_mem_node(struct drm_device *drm_dev,
660 struct drm_exynos_ipp_cmd_node *c_node,
661 struct drm_exynos_ipp_mem_node *m_node)
662{
663 int i;
664
YoungJun Chocbc4c332013-06-12 10:44:40 +0900665 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900666
667 if (!m_node) {
668 DRM_ERROR("invalid dequeue node.\n");
669 return -EFAULT;
670 }
671
YoungJun Chocbc4c332013-06-12 10:44:40 +0900672 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900673
674 /* put gem buffer */
675 for_each_ipp_planar(i) {
676 unsigned long handle = m_node->buf_info.handles[i];
677 if (handle)
678 exynos_drm_gem_put_dma_addr(drm_dev, handle,
Andrzej Hajda945a0aa2014-08-28 11:07:27 +0200679 c_node->filp);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900680 }
681
682 /* delete list in queue */
683 list_del(&m_node->list);
684 kfree(m_node);
685
Eunchul Kimcb471f142012-12-14 18:10:31 +0900686 return 0;
687}
688
689static void ipp_free_event(struct drm_pending_event *event)
690{
691 kfree(event);
692}
693
694static int ipp_get_event(struct drm_device *drm_dev,
695 struct drm_file *file,
696 struct drm_exynos_ipp_cmd_node *c_node,
697 struct drm_exynos_ipp_queue_buf *qbuf)
698{
699 struct drm_exynos_ipp_send_event *e;
700 unsigned long flags;
701
YoungJun Chocbc4c332013-06-12 10:44:40 +0900702 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900703
704 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900705 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900706 spin_lock_irqsave(&drm_dev->event_lock, flags);
707 file->event_space += sizeof(e->event);
708 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
709 return -ENOMEM;
710 }
711
712 /* make event */
713 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
714 e->event.base.length = sizeof(e->event);
715 e->event.user_data = qbuf->user_data;
716 e->event.prop_id = qbuf->prop_id;
717 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
718 e->base.event = &e->event.base;
719 e->base.file_priv = file;
720 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200721 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900722 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200723 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900724
725 return 0;
726}
727
728static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
729 struct drm_exynos_ipp_queue_buf *qbuf)
730{
731 struct drm_exynos_ipp_send_event *e, *te;
732 int count = 0;
733
YoungJun Cho4d520762014-05-26 10:17:21 +0200734 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900735 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900736 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900737
738 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530739 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900740 * stop operations want to delete all event list.
741 * another case delete only same buf id.
742 */
743 if (!qbuf) {
744 /* delete list */
745 list_del(&e->base.link);
746 kfree(e);
747 }
748
749 /* compare buffer id */
750 if (qbuf && (qbuf->buf_id ==
751 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
752 /* delete list */
753 list_del(&e->base.link);
754 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200755 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900756 }
757 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200758
759out_unlock:
760 mutex_unlock(&c_node->event_lock);
761 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900762}
763
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530764static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900765 struct exynos_drm_ippdrv *ippdrv,
766 struct drm_exynos_ipp_cmd_work *cmd_work,
767 struct drm_exynos_ipp_cmd_node *c_node)
768{
769 struct ipp_context *ctx = get_ipp_context(dev);
770
771 cmd_work->ippdrv = ippdrv;
772 cmd_work->c_node = c_node;
773 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
774}
775
776static int ipp_queue_buf_with_run(struct device *dev,
777 struct drm_exynos_ipp_cmd_node *c_node,
778 struct drm_exynos_ipp_mem_node *m_node,
779 struct drm_exynos_ipp_queue_buf *qbuf)
780{
781 struct exynos_drm_ippdrv *ippdrv;
782 struct drm_exynos_ipp_property *property;
783 struct exynos_drm_ipp_ops *ops;
784 int ret;
785
Eunchul Kimcb471f142012-12-14 18:10:31 +0900786 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530787 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900788 DRM_ERROR("failed to get ipp driver.\n");
789 return -EFAULT;
790 }
791
792 ops = ippdrv->ops[qbuf->ops_id];
793 if (!ops) {
794 DRM_ERROR("failed to get ops.\n");
795 return -EFAULT;
796 }
797
798 property = &c_node->property;
799
800 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900801 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900802 return 0;
803 }
804
YoungJun Cho220db6f2014-05-26 10:17:20 +0200805 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900806 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200807 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900808 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900809 return 0;
810 }
811
812 /*
813 * If set destination buffer and enabled clock,
814 * then m2m operations need start operations at queue_buf
815 */
816 if (ipp_is_m2m_cmd(property->cmd)) {
817 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
818
819 cmd_work->ctrl = IPP_CTRL_PLAY;
820 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
821 } else {
822 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
823 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200824 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900825 DRM_ERROR("failed to set m node.\n");
826 return ret;
827 }
828 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200829 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900830
831 return 0;
832}
833
834static void ipp_clean_queue_buf(struct drm_device *drm_dev,
835 struct drm_exynos_ipp_cmd_node *c_node,
836 struct drm_exynos_ipp_queue_buf *qbuf)
837{
838 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
839
YoungJun Choc66ce402014-05-26 10:17:15 +0200840 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200841 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200842 list_for_each_entry_safe(m_node, tm_node,
843 &c_node->mem_list[qbuf->ops_id], list) {
844 if (m_node->buf_id == qbuf->buf_id &&
845 m_node->ops_id == qbuf->ops_id)
846 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900847 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200848 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900849}
850
851int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
852 struct drm_file *file)
853{
854 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200855 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900856 struct ipp_context *ctx = get_ipp_context(dev);
857 struct drm_exynos_ipp_queue_buf *qbuf = data;
858 struct drm_exynos_ipp_cmd_node *c_node;
859 struct drm_exynos_ipp_mem_node *m_node;
860 int ret;
861
Eunchul Kimcb471f142012-12-14 18:10:31 +0900862 if (!qbuf) {
863 DRM_ERROR("invalid buf parameter.\n");
864 return -EINVAL;
865 }
866
867 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
868 DRM_ERROR("invalid ops parameter.\n");
869 return -EINVAL;
870 }
871
YoungJun Chocbc4c332013-06-12 10:44:40 +0900872 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
873 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900874 qbuf->buf_id, qbuf->buf_type);
875
876 /* find command node */
877 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
878 qbuf->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200879 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900880 DRM_ERROR("failed to get command node.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200881 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900882 }
883
884 /* buffer control */
885 switch (qbuf->buf_type) {
886 case IPP_BUF_ENQUEUE:
887 /* get memory node */
888 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
889 if (IS_ERR(m_node)) {
890 DRM_ERROR("failed to get m_node.\n");
891 return PTR_ERR(m_node);
892 }
893
894 /*
895 * first step get event for destination buffer.
896 * and second step when M2M case run with destination buffer
897 * if needed.
898 */
899 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
900 /* get event for destination buffer */
901 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
902 if (ret) {
903 DRM_ERROR("failed to get event.\n");
904 goto err_clean_node;
905 }
906
907 /*
908 * M2M case run play control for streaming feature.
909 * other case set address and waiting.
910 */
911 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
912 if (ret) {
913 DRM_ERROR("failed to run command.\n");
914 goto err_clean_node;
915 }
916 }
917 break;
918 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200919 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900920
921 /* put event for destination buffer */
922 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
923 ipp_put_event(c_node, qbuf);
924
925 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
926
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200927 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900928 break;
929 default:
930 DRM_ERROR("invalid buffer control.\n");
931 return -EINVAL;
932 }
933
934 return 0;
935
936err_clean_node:
937 DRM_ERROR("clean memory nodes.\n");
938
939 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
940 return ret;
941}
942
943static bool exynos_drm_ipp_check_valid(struct device *dev,
944 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
945{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900946 if (ctrl != IPP_CTRL_PLAY) {
947 if (pm_runtime_suspended(dev)) {
948 DRM_ERROR("pm:runtime_suspended.\n");
949 goto err_status;
950 }
951 }
952
953 switch (ctrl) {
954 case IPP_CTRL_PLAY:
955 if (state != IPP_STATE_IDLE)
956 goto err_status;
957 break;
958 case IPP_CTRL_STOP:
959 if (state == IPP_STATE_STOP)
960 goto err_status;
961 break;
962 case IPP_CTRL_PAUSE:
963 if (state != IPP_STATE_START)
964 goto err_status;
965 break;
966 case IPP_CTRL_RESUME:
967 if (state != IPP_STATE_STOP)
968 goto err_status;
969 break;
970 default:
971 DRM_ERROR("invalid state.\n");
972 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900973 }
974
975 return true;
976
977err_status:
978 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
979 return false;
980}
981
982int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
983 struct drm_file *file)
984{
985 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900986 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200987 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900988 struct ipp_context *ctx = get_ipp_context(dev);
989 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
990 struct drm_exynos_ipp_cmd_work *cmd_work;
991 struct drm_exynos_ipp_cmd_node *c_node;
992
Eunchul Kimcb471f142012-12-14 18:10:31 +0900993 if (!ctx) {
994 DRM_ERROR("invalid context.\n");
995 return -EINVAL;
996 }
997
998 if (!cmd_ctrl) {
999 DRM_ERROR("invalid control parameter.\n");
1000 return -EINVAL;
1001 }
1002
YoungJun Chocbc4c332013-06-12 10:44:40 +09001003 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001004 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1005
1006 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1007 if (IS_ERR(ippdrv)) {
1008 DRM_ERROR("failed to get ipp driver.\n");
1009 return PTR_ERR(ippdrv);
1010 }
1011
1012 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1013 cmd_ctrl->prop_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001014 if (!c_node) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001015 DRM_ERROR("invalid command node list.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001016 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001017 }
1018
1019 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1020 c_node->state)) {
1021 DRM_ERROR("invalid state.\n");
1022 return -EINVAL;
1023 }
1024
1025 switch (cmd_ctrl->ctrl) {
1026 case IPP_CTRL_PLAY:
1027 if (pm_runtime_suspended(ippdrv->dev))
1028 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001029
Eunchul Kimcb471f142012-12-14 18:10:31 +09001030 c_node->state = IPP_STATE_START;
1031
1032 cmd_work = c_node->start_work;
1033 cmd_work->ctrl = cmd_ctrl->ctrl;
1034 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001035 break;
1036 case IPP_CTRL_STOP:
1037 cmd_work = c_node->stop_work;
1038 cmd_work->ctrl = cmd_ctrl->ctrl;
1039 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1040
1041 if (!wait_for_completion_timeout(&c_node->stop_complete,
1042 msecs_to_jiffies(300))) {
1043 DRM_ERROR("timeout stop:prop_id[%d]\n",
1044 c_node->property.prop_id);
1045 }
1046
1047 c_node->state = IPP_STATE_STOP;
1048 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001049 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001050 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001051
1052 if (list_empty(&ippdrv->cmd_list))
1053 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001054 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001055 break;
1056 case IPP_CTRL_PAUSE:
1057 cmd_work = c_node->stop_work;
1058 cmd_work->ctrl = cmd_ctrl->ctrl;
1059 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1060
1061 if (!wait_for_completion_timeout(&c_node->stop_complete,
1062 msecs_to_jiffies(200))) {
1063 DRM_ERROR("timeout stop:prop_id[%d]\n",
1064 c_node->property.prop_id);
1065 }
1066
1067 c_node->state = IPP_STATE_STOP;
1068 break;
1069 case IPP_CTRL_RESUME:
1070 c_node->state = IPP_STATE_START;
1071 cmd_work = c_node->start_work;
1072 cmd_work->ctrl = cmd_ctrl->ctrl;
1073 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1074 break;
1075 default:
1076 DRM_ERROR("could not support this state currently.\n");
1077 return -EINVAL;
1078 }
1079
YoungJun Chocbc4c332013-06-12 10:44:40 +09001080 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001081 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1082
1083 return 0;
1084}
1085
1086int exynos_drm_ippnb_register(struct notifier_block *nb)
1087{
1088 return blocking_notifier_chain_register(
1089 &exynos_drm_ippnb_list, nb);
1090}
1091
1092int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1093{
1094 return blocking_notifier_chain_unregister(
1095 &exynos_drm_ippnb_list, nb);
1096}
1097
1098int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1099{
1100 return blocking_notifier_call_chain(
1101 &exynos_drm_ippnb_list, val, v);
1102}
1103
1104static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1105 struct drm_exynos_ipp_property *property)
1106{
1107 struct exynos_drm_ipp_ops *ops = NULL;
1108 bool swap = false;
1109 int ret, i;
1110
1111 if (!property) {
1112 DRM_ERROR("invalid property parameter.\n");
1113 return -EINVAL;
1114 }
1115
YoungJun Chocbc4c332013-06-12 10:44:40 +09001116 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001117
1118 /* reset h/w block */
1119 if (ippdrv->reset &&
1120 ippdrv->reset(ippdrv->dev)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001121 return -EINVAL;
1122 }
1123
1124 /* set source,destination operations */
1125 for_each_ipp_ops(i) {
1126 struct drm_exynos_ipp_config *config =
1127 &property->config[i];
1128
1129 ops = ippdrv->ops[i];
1130 if (!ops || !config) {
1131 DRM_ERROR("not support ops and config.\n");
1132 return -EINVAL;
1133 }
1134
1135 /* set format */
1136 if (ops->set_fmt) {
1137 ret = ops->set_fmt(ippdrv->dev, config->fmt);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001138 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001139 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001140 }
1141
1142 /* set transform for rotation, flip */
1143 if (ops->set_transf) {
1144 ret = ops->set_transf(ippdrv->dev, config->degree,
1145 config->flip, &swap);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001146 if (ret)
1147 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001148 }
1149
1150 /* set size */
1151 if (ops->set_size) {
1152 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1153 &config->sz);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001154 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001155 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001156 }
1157 }
1158
1159 return 0;
1160}
1161
1162static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1163 struct drm_exynos_ipp_cmd_node *c_node)
1164{
1165 struct drm_exynos_ipp_mem_node *m_node;
1166 struct drm_exynos_ipp_property *property = &c_node->property;
1167 struct list_head *head;
1168 int ret, i;
1169
YoungJun Chocbc4c332013-06-12 10:44:40 +09001170 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001171
1172 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001173 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001174
YoungJun Cho220db6f2014-05-26 10:17:20 +02001175 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001176 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001177 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001178 ret = -ENOMEM;
1179 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001180 }
1181
1182 /* set current property in ippdrv */
1183 ret = ipp_set_property(ippdrv, property);
1184 if (ret) {
1185 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001186 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001187 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001188 }
1189
1190 /* check command */
1191 switch (property->cmd) {
1192 case IPP_CMD_M2M:
1193 for_each_ipp_ops(i) {
1194 /* source/destination memory list */
1195 head = &c_node->mem_list[i];
1196
1197 m_node = list_first_entry(head,
1198 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001199
YoungJun Chocbc4c332013-06-12 10:44:40 +09001200 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001201
1202 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1203 if (ret) {
1204 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001205 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001206 }
1207 }
1208 break;
1209 case IPP_CMD_WB:
1210 /* destination memory list */
1211 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1212
1213 list_for_each_entry(m_node, head, list) {
1214 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1215 if (ret) {
1216 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001217 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001218 }
1219 }
1220 break;
1221 case IPP_CMD_OUTPUT:
1222 /* source memory list */
1223 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1224
1225 list_for_each_entry(m_node, head, list) {
1226 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1227 if (ret) {
1228 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001229 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001230 }
1231 }
1232 break;
1233 default:
1234 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001235 ret = -EINVAL;
1236 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001237 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001238 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001239
YoungJun Chocbc4c332013-06-12 10:44:40 +09001240 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001241
1242 /* start operations */
1243 if (ippdrv->start) {
1244 ret = ippdrv->start(ippdrv->dev, property->cmd);
1245 if (ret) {
1246 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001247 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001248 return ret;
1249 }
1250 }
1251
1252 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001253
1254err_unlock:
1255 mutex_unlock(&c_node->mem_lock);
1256 ippdrv->c_node = NULL;
1257 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001258}
1259
1260static int ipp_stop_property(struct drm_device *drm_dev,
1261 struct exynos_drm_ippdrv *ippdrv,
1262 struct drm_exynos_ipp_cmd_node *c_node)
1263{
1264 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1265 struct drm_exynos_ipp_property *property = &c_node->property;
1266 struct list_head *head;
1267 int ret = 0, i;
1268
YoungJun Chocbc4c332013-06-12 10:44:40 +09001269 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001270
1271 /* put event */
1272 ipp_put_event(c_node, NULL);
1273
YoungJun Cho220db6f2014-05-26 10:17:20 +02001274 mutex_lock(&c_node->mem_lock);
1275
Eunchul Kimcb471f142012-12-14 18:10:31 +09001276 /* check command */
1277 switch (property->cmd) {
1278 case IPP_CMD_M2M:
1279 for_each_ipp_ops(i) {
1280 /* source/destination memory list */
1281 head = &c_node->mem_list[i];
1282
Eunchul Kimcb471f142012-12-14 18:10:31 +09001283 list_for_each_entry_safe(m_node, tm_node,
1284 head, list) {
1285 ret = ipp_put_mem_node(drm_dev, c_node,
1286 m_node);
1287 if (ret) {
1288 DRM_ERROR("failed to put m_node.\n");
1289 goto err_clear;
1290 }
1291 }
1292 }
1293 break;
1294 case IPP_CMD_WB:
1295 /* destination memory list */
1296 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1297
Eunchul Kimcb471f142012-12-14 18:10:31 +09001298 list_for_each_entry_safe(m_node, tm_node, head, list) {
1299 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1300 if (ret) {
1301 DRM_ERROR("failed to put m_node.\n");
1302 goto err_clear;
1303 }
1304 }
1305 break;
1306 case IPP_CMD_OUTPUT:
1307 /* source memory list */
1308 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1309
Eunchul Kimcb471f142012-12-14 18:10:31 +09001310 list_for_each_entry_safe(m_node, tm_node, head, list) {
1311 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1312 if (ret) {
1313 DRM_ERROR("failed to put m_node.\n");
1314 goto err_clear;
1315 }
1316 }
1317 break;
1318 default:
1319 DRM_ERROR("invalid operations.\n");
1320 ret = -EINVAL;
1321 goto err_clear;
1322 }
1323
1324err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001325 mutex_unlock(&c_node->mem_lock);
1326
Eunchul Kimcb471f142012-12-14 18:10:31 +09001327 /* stop operations */
1328 if (ippdrv->stop)
1329 ippdrv->stop(ippdrv->dev, property->cmd);
1330
1331 return ret;
1332}
1333
1334void ipp_sched_cmd(struct work_struct *work)
1335{
1336 struct drm_exynos_ipp_cmd_work *cmd_work =
1337 (struct drm_exynos_ipp_cmd_work *)work;
1338 struct exynos_drm_ippdrv *ippdrv;
1339 struct drm_exynos_ipp_cmd_node *c_node;
1340 struct drm_exynos_ipp_property *property;
1341 int ret;
1342
Eunchul Kimcb471f142012-12-14 18:10:31 +09001343 ippdrv = cmd_work->ippdrv;
1344 if (!ippdrv) {
1345 DRM_ERROR("invalid ippdrv list.\n");
1346 return;
1347 }
1348
1349 c_node = cmd_work->c_node;
1350 if (!c_node) {
1351 DRM_ERROR("invalid command node list.\n");
1352 return;
1353 }
1354
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001355 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001356
1357 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001358
1359 switch (cmd_work->ctrl) {
1360 case IPP_CTRL_PLAY:
1361 case IPP_CTRL_RESUME:
1362 ret = ipp_start_property(ippdrv, c_node);
1363 if (ret) {
1364 DRM_ERROR("failed to start property:prop_id[%d]\n",
1365 c_node->property.prop_id);
1366 goto err_unlock;
1367 }
1368
1369 /*
1370 * M2M case supports wait_completion of transfer.
1371 * because M2M case supports single unit operation
1372 * with multiple queue.
1373 * M2M need to wait completion of data transfer.
1374 */
1375 if (ipp_is_m2m_cmd(property->cmd)) {
1376 if (!wait_for_completion_timeout
1377 (&c_node->start_complete, msecs_to_jiffies(200))) {
1378 DRM_ERROR("timeout event:prop_id[%d]\n",
1379 c_node->property.prop_id);
1380 goto err_unlock;
1381 }
1382 }
1383 break;
1384 case IPP_CTRL_STOP:
1385 case IPP_CTRL_PAUSE:
1386 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1387 c_node);
1388 if (ret) {
1389 DRM_ERROR("failed to stop property.\n");
1390 goto err_unlock;
1391 }
1392
1393 complete(&c_node->stop_complete);
1394 break;
1395 default:
1396 DRM_ERROR("unknown control type\n");
1397 break;
1398 }
1399
YoungJun Chocbc4c332013-06-12 10:44:40 +09001400 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001401
1402err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001403 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001404}
1405
1406static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1407 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1408{
1409 struct drm_device *drm_dev = ippdrv->drm_dev;
1410 struct drm_exynos_ipp_property *property = &c_node->property;
1411 struct drm_exynos_ipp_mem_node *m_node;
1412 struct drm_exynos_ipp_queue_buf qbuf;
1413 struct drm_exynos_ipp_send_event *e;
1414 struct list_head *head;
1415 struct timeval now;
1416 unsigned long flags;
1417 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1418 int ret, i;
1419
1420 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001421 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001422
1423 if (!drm_dev) {
1424 DRM_ERROR("failed to get drm_dev.\n");
1425 return -EINVAL;
1426 }
1427
1428 if (!property) {
1429 DRM_ERROR("failed to get property.\n");
1430 return -EINVAL;
1431 }
1432
YoungJun Cho4d520762014-05-26 10:17:21 +02001433 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001434 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001435 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001436 ret = 0;
1437 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001438 }
1439
YoungJun Cho220db6f2014-05-26 10:17:20 +02001440 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001441 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001442 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001443 ret = 0;
1444 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001445 }
1446
1447 /* check command */
1448 switch (property->cmd) {
1449 case IPP_CMD_M2M:
1450 for_each_ipp_ops(i) {
1451 /* source/destination memory list */
1452 head = &c_node->mem_list[i];
1453
1454 m_node = list_first_entry(head,
1455 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001456
1457 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001458 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001459 i ? "dst" : "src", tbuf_id[i]);
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 }
1465 break;
1466 case IPP_CMD_WB:
1467 /* clear buf for finding */
1468 memset(&qbuf, 0x0, sizeof(qbuf));
1469 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1470 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1471
1472 /* get memory node entry */
1473 m_node = ipp_find_mem_node(c_node, &qbuf);
1474 if (!m_node) {
1475 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001476 ret = -ENOMEM;
1477 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001478 }
1479
1480 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1481
1482 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1483 if (ret)
1484 DRM_ERROR("failed to put m_node.\n");
1485 break;
1486 case IPP_CMD_OUTPUT:
1487 /* source memory list */
1488 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1489
1490 m_node = list_first_entry(head,
1491 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001492
1493 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1494
1495 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1496 if (ret)
1497 DRM_ERROR("failed to put m_node.\n");
1498 break;
1499 default:
1500 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001501 ret = -EINVAL;
1502 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001503 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001504 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001505
1506 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1507 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1508 tbuf_id[1], buf_id[1], property->prop_id);
1509
1510 /*
1511 * command node have event list of destination buffer
1512 * If destination buffer enqueue to mem list,
1513 * then we make event and link to event list tail.
1514 * so, we get first event for first enqueued buffer.
1515 */
1516 e = list_first_entry(&c_node->event_list,
1517 struct drm_exynos_ipp_send_event, base.link);
1518
Eunchul Kimcb471f142012-12-14 18:10:31 +09001519 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001520 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001521 e->event.tv_sec = now.tv_sec;
1522 e->event.tv_usec = now.tv_usec;
1523 e->event.prop_id = property->prop_id;
1524
1525 /* set buffer id about source destination */
1526 for_each_ipp_ops(i)
1527 e->event.buf_id[i] = tbuf_id[i];
1528
1529 spin_lock_irqsave(&drm_dev->event_lock, flags);
1530 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1531 wake_up_interruptible(&e->base.file_priv->event_wait);
1532 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001533 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001534
YoungJun Chocbc4c332013-06-12 10:44:40 +09001535 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001536 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1537
1538 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001539
1540err_mem_unlock:
1541 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001542err_event_unlock:
1543 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001544 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001545}
1546
1547void ipp_sched_event(struct work_struct *work)
1548{
1549 struct drm_exynos_ipp_event_work *event_work =
1550 (struct drm_exynos_ipp_event_work *)work;
1551 struct exynos_drm_ippdrv *ippdrv;
1552 struct drm_exynos_ipp_cmd_node *c_node;
1553 int ret;
1554
1555 if (!event_work) {
1556 DRM_ERROR("failed to get event_work.\n");
1557 return;
1558 }
1559
YoungJun Chocbc4c332013-06-12 10:44:40 +09001560 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001561
1562 ippdrv = event_work->ippdrv;
1563 if (!ippdrv) {
1564 DRM_ERROR("failed to get ipp driver.\n");
1565 return;
1566 }
1567
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001568 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001569 if (!c_node) {
1570 DRM_ERROR("failed to get command node.\n");
1571 return;
1572 }
1573
1574 /*
1575 * IPP supports command thread, event thread synchronization.
1576 * If IPP close immediately from user land, then IPP make
1577 * synchronization with command thread, so make complete event.
1578 * or going out operations.
1579 */
1580 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001581 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1582 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001583 goto err_completion;
1584 }
1585
Eunchul Kimcb471f142012-12-14 18:10:31 +09001586 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1587 if (ret) {
1588 DRM_ERROR("failed to send event.\n");
1589 goto err_completion;
1590 }
1591
1592err_completion:
1593 if (ipp_is_m2m_cmd(c_node->property.cmd))
1594 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001595}
1596
1597static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1598{
1599 struct ipp_context *ctx = get_ipp_context(dev);
1600 struct exynos_drm_ippdrv *ippdrv;
1601 int ret, count = 0;
1602
Eunchul Kimcb471f142012-12-14 18:10:31 +09001603 /* get ipp driver entry */
1604 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1605 ippdrv->drm_dev = drm_dev;
1606
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001607 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
1608 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001609 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001610 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001611 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001612 ippdrv->prop_list.ipp_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001613
YoungJun Chocbc4c332013-06-12 10:44:40 +09001614 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001615 count++, (int)ippdrv, ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001616
1617 /* store parent device for node */
1618 ippdrv->parent_dev = dev;
1619
1620 /* store event work queue and handler */
1621 ippdrv->event_workq = ctx->event_workq;
1622 ippdrv->sched_event = ipp_sched_event;
1623 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001624 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001625
1626 if (is_drm_iommu_supported(drm_dev)) {
1627 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1628 if (ret) {
1629 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001630 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001631 }
1632 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001633 }
1634
1635 return 0;
1636
YoungJun Cho075436b2014-05-26 10:17:19 +02001637err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001638 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001639 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1640 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001641 if (is_drm_iommu_supported(drm_dev))
1642 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1643
YoungJun Cho075436b2014-05-26 10:17:19 +02001644 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1645 ippdrv->prop_list.ipp_id);
1646 }
1647
Eunchul Kimcb471f142012-12-14 18:10:31 +09001648 return ret;
1649}
1650
1651static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1652{
1653 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001654 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001655
Eunchul Kimcb471f142012-12-14 18:10:31 +09001656 /* get ipp driver entry */
1657 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001658 if (is_drm_iommu_supported(drm_dev))
1659 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1660
YoungJun Cho075436b2014-05-26 10:17:19 +02001661 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1662 ippdrv->prop_list.ipp_id);
1663
Eunchul Kimcb471f142012-12-14 18:10:31 +09001664 ippdrv->drm_dev = NULL;
1665 exynos_drm_ippdrv_unregister(ippdrv);
1666 }
1667}
1668
1669static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1670 struct drm_file *file)
1671{
1672 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001673
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001674 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001675
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001676 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001677
1678 return 0;
1679}
1680
1681static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1682 struct drm_file *file)
1683{
1684 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001685 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001686 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001687 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1688 int count = 0;
1689
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001690 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)file_priv->ipp_dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001691
Eunchul Kimcb471f142012-12-14 18:10:31 +09001692 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001693 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001694 list_for_each_entry_safe(c_node, tc_node,
1695 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001696 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1697 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001698
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001699 if (c_node->dev == file_priv->ipp_dev) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001700 /*
1701 * userland goto unnormal state. process killed.
1702 * and close the file.
1703 * so, IPP didn't called stop cmd ctrl.
1704 * so, we are make stop operation in this state.
1705 */
1706 if (c_node->state == IPP_STATE_START) {
1707 ipp_stop_property(drm_dev, ippdrv,
1708 c_node);
1709 c_node->state = IPP_STATE_STOP;
1710 }
1711
1712 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001713 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001714 if (list_empty(&ippdrv->cmd_list))
1715 pm_runtime_put_sync(ippdrv->dev);
1716 }
1717 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001718 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001719 }
1720
Eunchul Kimcb471f142012-12-14 18:10:31 +09001721 return;
1722}
1723
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001724static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001725{
1726 struct device *dev = &pdev->dev;
1727 struct ipp_context *ctx;
1728 struct exynos_drm_subdrv *subdrv;
1729 int ret;
1730
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001731 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001732 if (!ctx)
1733 return -ENOMEM;
1734
Eunchul Kimcb471f142012-12-14 18:10:31 +09001735 mutex_init(&ctx->ipp_lock);
1736 mutex_init(&ctx->prop_lock);
1737
1738 idr_init(&ctx->ipp_idr);
1739 idr_init(&ctx->prop_idr);
1740
1741 /*
1742 * create single thread for ipp event
1743 * IPP supports event thread for IPP drivers.
1744 * IPP driver send event_work to this thread.
1745 * and IPP event thread send event to user process.
1746 */
1747 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1748 if (!ctx->event_workq) {
1749 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301750 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001751 }
1752
1753 /*
1754 * create single thread for ipp command
1755 * IPP supports command thread for user process.
1756 * user process make command node using set property ioctl.
1757 * and make start_work and send this work to command thread.
1758 * and then this command thread start property.
1759 */
1760 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1761 if (!ctx->cmd_workq) {
1762 dev_err(dev, "failed to create cmd workqueue\n");
1763 ret = -EINVAL;
1764 goto err_event_workq;
1765 }
1766
1767 /* set sub driver informations */
1768 subdrv = &ctx->subdrv;
1769 subdrv->dev = dev;
1770 subdrv->probe = ipp_subdrv_probe;
1771 subdrv->remove = ipp_subdrv_remove;
1772 subdrv->open = ipp_subdrv_open;
1773 subdrv->close = ipp_subdrv_close;
1774
1775 platform_set_drvdata(pdev, ctx);
1776
1777 ret = exynos_drm_subdrv_register(subdrv);
1778 if (ret < 0) {
1779 DRM_ERROR("failed to register drm ipp device.\n");
1780 goto err_cmd_workq;
1781 }
1782
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001783 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001784
1785 return 0;
1786
1787err_cmd_workq:
1788 destroy_workqueue(ctx->cmd_workq);
1789err_event_workq:
1790 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001791 return ret;
1792}
1793
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001794static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001795{
1796 struct ipp_context *ctx = platform_get_drvdata(pdev);
1797
Eunchul Kimcb471f142012-12-14 18:10:31 +09001798 /* unregister sub driver */
1799 exynos_drm_subdrv_unregister(&ctx->subdrv);
1800
1801 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001802 idr_destroy(&ctx->ipp_idr);
1803 idr_destroy(&ctx->prop_idr);
1804
1805 mutex_destroy(&ctx->ipp_lock);
1806 mutex_destroy(&ctx->prop_lock);
1807
1808 /* destroy command, event work queue */
1809 destroy_workqueue(ctx->cmd_workq);
1810 destroy_workqueue(ctx->event_workq);
1811
Eunchul Kimcb471f142012-12-14 18:10:31 +09001812 return 0;
1813}
1814
Eunchul Kimcb471f142012-12-14 18:10:31 +09001815struct platform_driver ipp_driver = {
1816 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001817 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001818 .driver = {
1819 .name = "exynos-drm-ipp",
1820 .owner = THIS_MODULE,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001821 },
1822};
1823