blob: f594dd78963aa5f10953f7733cfd90e6a5661d6e [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
48/*
49 * A structure of event.
50 *
51 * @base: base of event.
52 * @event: ipp event.
53 */
54struct drm_exynos_ipp_send_event {
55 struct drm_pending_event base;
56 struct drm_exynos_ipp_event event;
57};
58
59/*
60 * A structure of memory node.
61 *
62 * @list: list head to memory queue information.
63 * @ops_id: id of operations.
64 * @prop_id: id of property.
65 * @buf_id: id of buffer.
66 * @buf_info: gem objects and dma address, size.
67 * @filp: a pointer to drm_file.
68 */
69struct drm_exynos_ipp_mem_node {
70 struct list_head list;
71 enum drm_exynos_ops_id ops_id;
72 u32 prop_id;
73 u32 buf_id;
74 struct drm_exynos_ipp_buf_info buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +090075};
76
77/*
78 * A structure of ipp context.
79 *
80 * @subdrv: prepare initialization using subdrv.
81 * @ipp_lock: lock for synchronization of access to ipp_idr.
82 * @prop_lock: lock for synchronization of access to prop_idr.
83 * @ipp_idr: ipp driver idr.
84 * @prop_idr: property idr.
85 * @event_workq: event work queue.
86 * @cmd_workq: command work queue.
87 */
88struct ipp_context {
89 struct exynos_drm_subdrv subdrv;
90 struct mutex ipp_lock;
91 struct mutex prop_lock;
92 struct idr ipp_idr;
93 struct idr prop_idr;
94 struct workqueue_struct *event_workq;
95 struct workqueue_struct *cmd_workq;
96};
97
98static LIST_HEAD(exynos_drm_ippdrv_list);
99static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
100static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
101
102int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
103{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900104 mutex_lock(&exynos_drm_ippdrv_lock);
105 list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
106 mutex_unlock(&exynos_drm_ippdrv_lock);
107
108 return 0;
109}
110
111int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
112{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900113 mutex_lock(&exynos_drm_ippdrv_lock);
114 list_del(&ippdrv->drv_list);
115 mutex_unlock(&exynos_drm_ippdrv_lock);
116
117 return 0;
118}
119
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200120static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900121{
122 int ret;
123
Eunchul Kimcb471f142012-12-14 18:10:31 +0900124 mutex_lock(lock);
Tejun Heo8550cb22013-02-27 17:04:09 -0800125 ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900126 mutex_unlock(lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900127
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200128 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900129}
130
YoungJun Cho075436b2014-05-26 10:17:19 +0200131static void ipp_remove_id(struct idr *id_idr, struct mutex *lock, u32 id)
132{
133 mutex_lock(lock);
134 idr_remove(id_idr, id);
135 mutex_unlock(lock);
136}
137
Eunchul Kimcb471f142012-12-14 18:10:31 +0900138static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
139{
140 void *obj;
141
Eunchul Kimcb471f142012-12-14 18:10:31 +0900142 mutex_lock(lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900143 obj = idr_find(id_idr, id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900144 mutex_unlock(lock);
145
146 return obj;
147}
148
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200149static int ipp_check_driver(struct exynos_drm_ippdrv *ippdrv,
150 struct drm_exynos_ipp_property *property)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900151{
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200152 if (ippdrv->dedicated || (!ipp_is_m2m_cmd(property->cmd) &&
153 !pm_runtime_suspended(ippdrv->dev)))
154 return -EBUSY;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900155
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200156 if (ippdrv->check_property &&
157 ippdrv->check_property(ippdrv->dev, property))
158 return -EINVAL;
159
160 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900161}
162
163static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
164 struct drm_exynos_ipp_property *property)
165{
166 struct exynos_drm_ippdrv *ippdrv;
167 u32 ipp_id = property->ipp_id;
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200168 int ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900169
170 if (ipp_id) {
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200171 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock, ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200172 if (!ippdrv) {
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200173 DRM_DEBUG("ipp%d driver not found\n", ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200174 return ERR_PTR(-ENODEV);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900175 }
176
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200177 ret = ipp_check_driver(ippdrv, property);
178 if (ret < 0) {
179 DRM_DEBUG("ipp%d driver check error %d\n", ipp_id, ret);
180 return ERR_PTR(ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900181 }
182
183 return ippdrv;
184 } else {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900185 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200186 ret = ipp_check_driver(ippdrv, property);
187 if (ret == 0)
188 return ippdrv;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900189 }
190
Andrzej Hajda9cc7d852014-07-03 15:10:37 +0200191 DRM_DEBUG("cannot find driver suitable for given property.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900192 }
193
194 return ERR_PTR(-ENODEV);
195}
196
197static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
198{
199 struct exynos_drm_ippdrv *ippdrv;
200 struct drm_exynos_ipp_cmd_node *c_node;
201 int count = 0;
202
YoungJun Chocbc4c332013-06-12 10:44:40 +0900203 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900204
Eunchul Kimcb471f142012-12-14 18:10:31 +0900205 /*
206 * This case is search ipp driver by prop_id handle.
207 * sometimes, ipp subsystem find driver by prop_id.
Geert Uytterhoeven9fca9ac2014-03-11 11:23:37 +0100208 * e.g PAUSE state, queue buf, command control.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900209 */
210 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900211 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900212
YoungJun Cho7f5af052014-05-26 10:17:18 +0200213 mutex_lock(&ippdrv->cmd_lock);
214 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
215 if (c_node->property.prop_id == prop_id) {
216 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200217 return ippdrv;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200218 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900219 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200220 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900221 }
222
223 return ERR_PTR(-ENODEV);
224}
225
226int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
227 struct drm_file *file)
228{
229 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200230 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900231 struct ipp_context *ctx = get_ipp_context(dev);
232 struct drm_exynos_ipp_prop_list *prop_list = data;
233 struct exynos_drm_ippdrv *ippdrv;
234 int count = 0;
235
Eunchul Kimcb471f142012-12-14 18:10:31 +0900236 if (!ctx) {
237 DRM_ERROR("invalid context.\n");
238 return -EINVAL;
239 }
240
241 if (!prop_list) {
242 DRM_ERROR("invalid property parameter.\n");
243 return -EINVAL;
244 }
245
YoungJun Chocbc4c332013-06-12 10:44:40 +0900246 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900247
248 if (!prop_list->ipp_id) {
249 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
250 count++;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200251
Eunchul Kimcb471f142012-12-14 18:10:31 +0900252 /*
253 * Supports ippdrv list count for user application.
254 * First step user application getting ippdrv count.
255 * and second step getting ippdrv capability using ipp_id.
256 */
257 prop_list->count = count;
258 } else {
259 /*
260 * Getting ippdrv capability by ipp_id.
Masanari Iidac6b78bc2013-10-24 16:02:57 +0900261 * some device not supported wb, output interface.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900262 * so, user application detect correct ipp driver
263 * using this ioctl.
264 */
265 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
266 prop_list->ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200267 if (!ippdrv) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900268 DRM_ERROR("not found ipp%d driver.\n",
269 prop_list->ipp_id);
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200270 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900271 }
272
Andrzej Hajda31646052014-05-19 12:54:05 +0200273 *prop_list = ippdrv->prop_list;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900274 }
275
276 return 0;
277}
278
279static void ipp_print_property(struct drm_exynos_ipp_property *property,
280 int idx)
281{
282 struct drm_exynos_ipp_config *config = &property->config[idx];
283 struct drm_exynos_pos *pos = &config->pos;
284 struct drm_exynos_sz *sz = &config->sz;
285
YoungJun Chocbc4c332013-06-12 10:44:40 +0900286 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
287 property->prop_id, idx ? "dst" : "src", config->fmt);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900288
YoungJun Chocbc4c332013-06-12 10:44:40 +0900289 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
290 pos->x, pos->y, pos->w, pos->h,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900291 sz->hsize, sz->vsize, config->flip, config->degree);
292}
293
Eunchul Kimcb471f142012-12-14 18:10:31 +0900294static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
295{
296 struct drm_exynos_ipp_cmd_work *cmd_work;
297
Eunchul Kimcb471f142012-12-14 18:10:31 +0900298 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900299 if (!cmd_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900300 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900301
302 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
303
304 return cmd_work;
305}
306
307static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
308{
309 struct drm_exynos_ipp_event_work *event_work;
310
Eunchul Kimcb471f142012-12-14 18:10:31 +0900311 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900312 if (!event_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900313 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900314
Andrzej Hajda60b61c22014-07-03 15:10:26 +0200315 INIT_WORK(&event_work->work, ipp_sched_event);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900316
317 return event_work;
318}
319
320int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
321 struct drm_file *file)
322{
323 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200324 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900325 struct ipp_context *ctx = get_ipp_context(dev);
326 struct drm_exynos_ipp_property *property = data;
327 struct exynos_drm_ippdrv *ippdrv;
328 struct drm_exynos_ipp_cmd_node *c_node;
Andrzej Hajda18383cb2014-09-02 14:56:21 +0200329 u32 prop_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900330 int ret, i;
331
Eunchul Kimcb471f142012-12-14 18:10:31 +0900332 if (!ctx) {
333 DRM_ERROR("invalid context.\n");
334 return -EINVAL;
335 }
336
337 if (!property) {
338 DRM_ERROR("invalid property parameter.\n");
339 return -EINVAL;
340 }
341
Andrzej Hajda18383cb2014-09-02 14:56:21 +0200342 prop_id = property->prop_id;
343
Eunchul Kimcb471f142012-12-14 18:10:31 +0900344 /*
345 * This is log print for user application property.
346 * user application set various property.
347 */
348 for_each_ipp_ops(i)
349 ipp_print_property(property, i);
350
351 /*
Andrzej Hajda18383cb2014-09-02 14:56:21 +0200352 * In case prop_id is not zero try to set existing property.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900353 */
Andrzej Hajda18383cb2014-09-02 14:56:21 +0200354 if (prop_id) {
355 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock, prop_id);
356
357 if (!c_node || c_node->filp != file) {
358 DRM_DEBUG_KMS("prop_id[%d] not found\n", prop_id);
359 return -EINVAL;
360 }
361
362 if (c_node->state != IPP_STATE_STOP) {
363 DRM_DEBUG_KMS("prop_id[%d] not stopped\n", prop_id);
364 return -EINVAL;
365 }
366
367 c_node->property = *property;
368
369 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900370 }
371
372 /* find ipp driver using ipp id */
373 ippdrv = ipp_find_driver(ctx, property);
Sachin Kamatf0250452013-04-29 12:27:06 +0530374 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900375 DRM_ERROR("failed to get ipp driver.\n");
376 return -EINVAL;
377 }
378
379 /* allocate command node */
380 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900381 if (!c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900382 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900383
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200384 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node);
385 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900386 DRM_ERROR("failed to create id.\n");
387 goto err_clear;
388 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +0200389 property->prop_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900390
YoungJun Chocbc4c332013-06-12 10:44:40 +0900391 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
392 property->prop_id, property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900393
394 /* stored property information and ippdrv in private data */
Eunchul Kimcb471f142012-12-14 18:10:31 +0900395 c_node->property = *property;
396 c_node->state = IPP_STATE_IDLE;
Andrzej Hajda945a0aa2014-08-28 11:07:27 +0200397 c_node->filp = file;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900398
399 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530400 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900401 DRM_ERROR("failed to create start work.\n");
Julia Lawallbe19d932014-11-23 14:11:15 +0100402 ret = PTR_ERR(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200403 goto err_remove_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900404 }
405
406 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530407 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900408 DRM_ERROR("failed to create stop work.\n");
Julia Lawallbe19d932014-11-23 14:11:15 +0100409 ret = PTR_ERR(c_node->stop_work);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900410 goto err_free_start;
411 }
412
413 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530414 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900415 DRM_ERROR("failed to create event work.\n");
Julia Lawallbe19d932014-11-23 14:11:15 +0100416 ret = PTR_ERR(c_node->event_work);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900417 goto err_free_stop;
418 }
419
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200420 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900421 mutex_init(&c_node->mem_lock);
422 mutex_init(&c_node->event_lock);
423
424 init_completion(&c_node->start_complete);
425 init_completion(&c_node->stop_complete);
426
427 for_each_ipp_ops(i)
428 INIT_LIST_HEAD(&c_node->mem_list[i]);
429
430 INIT_LIST_HEAD(&c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200431 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900432 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200433 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900434
435 /* make dedicated state without m2m */
436 if (!ipp_is_m2m_cmd(property->cmd))
437 ippdrv->dedicated = true;
438
439 return 0;
440
441err_free_stop:
442 kfree(c_node->stop_work);
443err_free_start:
444 kfree(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200445err_remove_id:
446 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900447err_clear:
448 kfree(c_node);
449 return ret;
450}
451
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100452static int ipp_validate_mem_node(struct drm_device *drm_dev,
453 struct drm_exynos_ipp_mem_node *m_node,
454 struct drm_exynos_ipp_cmd_node *c_node)
455{
456 struct drm_exynos_ipp_config *ipp_cfg;
457 unsigned int num_plane;
458 unsigned long min_size, size;
459 unsigned int bpp;
460 int i;
461
462 /* The property id should already be varified */
463 ipp_cfg = &c_node->property.config[m_node->prop_id];
464 num_plane = drm_format_num_planes(ipp_cfg->fmt);
465
466 /**
467 * This is a rather simplified validation of a memory node.
468 * It basically verifies provided gem object handles
469 * and the buffer sizes with respect to current configuration.
470 * This is not the best that can be done
471 * but it seems more than enough
472 */
473 for (i = 0; i < num_plane; ++i) {
474 if (!m_node->buf_info.handles[i]) {
475 DRM_ERROR("invalid handle for plane %d\n", i);
476 return -EINVAL;
477 }
478 bpp = drm_format_plane_cpp(ipp_cfg->fmt, i);
479 min_size = (ipp_cfg->sz.hsize * ipp_cfg->sz.vsize * bpp) >> 3;
480 size = exynos_drm_gem_get_size(drm_dev,
481 m_node->buf_info.handles[i],
482 c_node->filp);
483 if (min_size > size) {
484 DRM_ERROR("invalid size for plane %d\n", i);
485 return -EINVAL;
486 }
487 }
488 return 0;
489}
490
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200491static int ipp_put_mem_node(struct drm_device *drm_dev,
492 struct drm_exynos_ipp_cmd_node *c_node,
493 struct drm_exynos_ipp_mem_node *m_node)
494{
495 int i;
496
497 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
498
499 if (!m_node) {
500 DRM_ERROR("invalid dequeue node.\n");
501 return -EFAULT;
502 }
503
504 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
505
506 /* put gem buffer */
507 for_each_ipp_planar(i) {
508 unsigned long handle = m_node->buf_info.handles[i];
509 if (handle)
510 exynos_drm_gem_put_dma_addr(drm_dev, handle,
511 c_node->filp);
512 }
513
514 list_del(&m_node->list);
515 kfree(m_node);
516
517 return 0;
518}
519
520static struct drm_exynos_ipp_mem_node
521 *ipp_get_mem_node(struct drm_device *drm_dev,
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200522 struct drm_exynos_ipp_cmd_node *c_node,
523 struct drm_exynos_ipp_queue_buf *qbuf)
524{
525 struct drm_exynos_ipp_mem_node *m_node;
526 struct drm_exynos_ipp_buf_info *buf_info;
527 int i;
528
529 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
530 if (!m_node)
531 return ERR_PTR(-ENOMEM);
532
533 buf_info = &m_node->buf_info;
534
535 /* operations, buffer id */
536 m_node->ops_id = qbuf->ops_id;
537 m_node->prop_id = qbuf->prop_id;
538 m_node->buf_id = qbuf->buf_id;
539 INIT_LIST_HEAD(&m_node->list);
540
541 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
542 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
543
544 for_each_ipp_planar(i) {
545 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
546
547 /* get dma address by handle */
548 if (qbuf->handle[i]) {
549 dma_addr_t *addr;
550
551 addr = exynos_drm_gem_get_dma_addr(drm_dev,
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200552 qbuf->handle[i], c_node->filp);
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200553 if (IS_ERR(addr)) {
554 DRM_ERROR("failed to get addr.\n");
555 ipp_put_mem_node(drm_dev, c_node, m_node);
556 return ERR_PTR(-EFAULT);
557 }
558
559 buf_info->handles[i] = qbuf->handle[i];
560 buf_info->base[i] = *addr;
561 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
562 buf_info->base[i], buf_info->handles[i]);
563 }
564 }
565
566 mutex_lock(&c_node->mem_lock);
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100567 if (ipp_validate_mem_node(drm_dev, m_node, c_node)) {
568 ipp_put_mem_node(drm_dev, c_node, m_node);
569 mutex_unlock(&c_node->mem_lock);
570 return ERR_PTR(-EFAULT);
571 }
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200572 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
573 mutex_unlock(&c_node->mem_lock);
574
575 return m_node;
576}
577
578static void ipp_clean_mem_nodes(struct drm_device *drm_dev,
579 struct drm_exynos_ipp_cmd_node *c_node, int ops)
580{
581 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
582 struct list_head *head = &c_node->mem_list[ops];
583
584 mutex_lock(&c_node->mem_lock);
585
586 list_for_each_entry_safe(m_node, tm_node, head, list) {
587 int ret;
588
589 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
590 if (ret)
591 DRM_ERROR("failed to put m_node.\n");
592 }
593
594 mutex_unlock(&c_node->mem_lock);
595}
596
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200597static void ipp_free_event(struct drm_pending_event *event)
598{
599 kfree(event);
600}
601
602static int ipp_get_event(struct drm_device *drm_dev,
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200603 struct drm_exynos_ipp_cmd_node *c_node,
604 struct drm_exynos_ipp_queue_buf *qbuf)
605{
606 struct drm_exynos_ipp_send_event *e;
607 unsigned long flags;
608
609 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
610
611 e = kzalloc(sizeof(*e), GFP_KERNEL);
612 if (!e) {
613 spin_lock_irqsave(&drm_dev->event_lock, flags);
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200614 c_node->filp->event_space += sizeof(e->event);
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200615 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
616 return -ENOMEM;
617 }
618
619 /* make event */
620 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
621 e->event.base.length = sizeof(e->event);
622 e->event.user_data = qbuf->user_data;
623 e->event.prop_id = qbuf->prop_id;
624 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
625 e->base.event = &e->event.base;
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200626 e->base.file_priv = c_node->filp;
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200627 e->base.destroy = ipp_free_event;
628 mutex_lock(&c_node->event_lock);
629 list_add_tail(&e->base.link, &c_node->event_list);
630 mutex_unlock(&c_node->event_lock);
631
632 return 0;
633}
634
635static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
636 struct drm_exynos_ipp_queue_buf *qbuf)
637{
638 struct drm_exynos_ipp_send_event *e, *te;
639 int count = 0;
640
641 mutex_lock(&c_node->event_lock);
642 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
643 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
644
645 /*
646 * qbuf == NULL condition means all event deletion.
647 * stop operations want to delete all event list.
648 * another case delete only same buf id.
649 */
650 if (!qbuf) {
651 /* delete list */
652 list_del(&e->base.link);
653 kfree(e);
654 }
655
656 /* compare buffer id */
657 if (qbuf && (qbuf->buf_id ==
658 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
659 /* delete list */
660 list_del(&e->base.link);
661 kfree(e);
662 goto out_unlock;
663 }
664 }
665
666out_unlock:
667 mutex_unlock(&c_node->event_lock);
668 return;
669}
670
YoungJun Cho075436b2014-05-26 10:17:19 +0200671static void ipp_clean_cmd_node(struct ipp_context *ctx,
672 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900673{
Andrzej Hajda22e816f2014-08-28 11:07:32 +0200674 int i;
675
Andrzej Hajda6f7d48e2014-08-28 11:07:26 +0200676 /* cancel works */
677 cancel_work_sync(&c_node->start_work->work);
678 cancel_work_sync(&c_node->stop_work->work);
679 cancel_work_sync(&c_node->event_work->work);
680
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200681 /* put event */
682 ipp_put_event(c_node, NULL);
683
Andrzej Hajda22e816f2014-08-28 11:07:32 +0200684 for_each_ipp_ops(i)
685 ipp_clean_mem_nodes(ctx->subdrv.drm_dev, c_node, i);
686
Eunchul Kimcb471f142012-12-14 18:10:31 +0900687 /* delete list */
688 list_del(&c_node->list);
689
YoungJun Cho075436b2014-05-26 10:17:19 +0200690 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
691 c_node->property.prop_id);
692
Eunchul Kimcb471f142012-12-14 18:10:31 +0900693 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200694 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900695 mutex_destroy(&c_node->mem_lock);
696 mutex_destroy(&c_node->event_lock);
697
698 /* free command node */
699 kfree(c_node->start_work);
700 kfree(c_node->stop_work);
701 kfree(c_node->event_work);
702 kfree(c_node);
703}
704
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200705static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900706{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200707 switch (c_node->property.cmd) {
708 case IPP_CMD_WB:
709 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
710 case IPP_CMD_OUTPUT:
711 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
712 case IPP_CMD_M2M:
713 default:
714 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
715 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900716 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900717}
718
719static struct drm_exynos_ipp_mem_node
720 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
721 struct drm_exynos_ipp_queue_buf *qbuf)
722{
723 struct drm_exynos_ipp_mem_node *m_node;
724 struct list_head *head;
725 int count = 0;
726
YoungJun Chocbc4c332013-06-12 10:44:40 +0900727 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900728
729 /* source/destination memory list */
730 head = &c_node->mem_list[qbuf->ops_id];
731
732 /* find memory node from memory list */
733 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900734 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900735
736 /* compare buffer id */
737 if (m_node->buf_id == qbuf->buf_id)
738 return m_node;
739 }
740
741 return NULL;
742}
743
744static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
745 struct drm_exynos_ipp_cmd_node *c_node,
746 struct drm_exynos_ipp_mem_node *m_node)
747{
748 struct exynos_drm_ipp_ops *ops = NULL;
749 int ret = 0;
750
YoungJun Chocbc4c332013-06-12 10:44:40 +0900751 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900752
753 if (!m_node) {
754 DRM_ERROR("invalid queue node.\n");
755 return -EFAULT;
756 }
757
YoungJun Chocbc4c332013-06-12 10:44:40 +0900758 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900759
760 /* get operations callback */
761 ops = ippdrv->ops[m_node->ops_id];
762 if (!ops) {
763 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200764 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900765 }
766
767 /* set address and enable irq */
768 if (ops->set_addr) {
769 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
770 m_node->buf_id, IPP_BUF_ENQUEUE);
771 if (ret) {
772 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200773 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900774 }
775 }
776
Eunchul Kimcb471f142012-12-14 18:10:31 +0900777 return ret;
778}
779
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530780static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900781 struct exynos_drm_ippdrv *ippdrv,
782 struct drm_exynos_ipp_cmd_work *cmd_work,
783 struct drm_exynos_ipp_cmd_node *c_node)
784{
785 struct ipp_context *ctx = get_ipp_context(dev);
786
787 cmd_work->ippdrv = ippdrv;
788 cmd_work->c_node = c_node;
Andrzej Hajda05afb1a2014-08-28 11:07:33 +0200789 queue_work(ctx->cmd_workq, &cmd_work->work);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900790}
791
792static int ipp_queue_buf_with_run(struct device *dev,
793 struct drm_exynos_ipp_cmd_node *c_node,
794 struct drm_exynos_ipp_mem_node *m_node,
795 struct drm_exynos_ipp_queue_buf *qbuf)
796{
797 struct exynos_drm_ippdrv *ippdrv;
798 struct drm_exynos_ipp_property *property;
799 struct exynos_drm_ipp_ops *ops;
800 int ret;
801
Eunchul Kimcb471f142012-12-14 18:10:31 +0900802 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530803 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900804 DRM_ERROR("failed to get ipp driver.\n");
805 return -EFAULT;
806 }
807
808 ops = ippdrv->ops[qbuf->ops_id];
809 if (!ops) {
810 DRM_ERROR("failed to get ops.\n");
811 return -EFAULT;
812 }
813
814 property = &c_node->property;
815
816 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900817 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900818 return 0;
819 }
820
YoungJun Cho220db6f2014-05-26 10:17:20 +0200821 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900822 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200823 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900824 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900825 return 0;
826 }
827
828 /*
829 * If set destination buffer and enabled clock,
830 * then m2m operations need start operations at queue_buf
831 */
832 if (ipp_is_m2m_cmd(property->cmd)) {
833 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
834
835 cmd_work->ctrl = IPP_CTRL_PLAY;
836 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
837 } else {
838 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
839 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200840 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900841 DRM_ERROR("failed to set m node.\n");
842 return ret;
843 }
844 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200845 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900846
847 return 0;
848}
849
850static void ipp_clean_queue_buf(struct drm_device *drm_dev,
851 struct drm_exynos_ipp_cmd_node *c_node,
852 struct drm_exynos_ipp_queue_buf *qbuf)
853{
854 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
855
YoungJun Choc66ce402014-05-26 10:17:15 +0200856 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200857 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200858 list_for_each_entry_safe(m_node, tm_node,
859 &c_node->mem_list[qbuf->ops_id], list) {
860 if (m_node->buf_id == qbuf->buf_id &&
861 m_node->ops_id == qbuf->ops_id)
862 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900863 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200864 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900865}
866
867int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
868 struct drm_file *file)
869{
870 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200871 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900872 struct ipp_context *ctx = get_ipp_context(dev);
873 struct drm_exynos_ipp_queue_buf *qbuf = data;
874 struct drm_exynos_ipp_cmd_node *c_node;
875 struct drm_exynos_ipp_mem_node *m_node;
876 int ret;
877
Eunchul Kimcb471f142012-12-14 18:10:31 +0900878 if (!qbuf) {
879 DRM_ERROR("invalid buf parameter.\n");
880 return -EINVAL;
881 }
882
883 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
884 DRM_ERROR("invalid ops parameter.\n");
885 return -EINVAL;
886 }
887
YoungJun Chocbc4c332013-06-12 10:44:40 +0900888 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
889 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900890 qbuf->buf_id, qbuf->buf_type);
891
892 /* find command node */
893 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
894 qbuf->prop_id);
Andrzej Hajda18383cb2014-09-02 14:56:21 +0200895 if (!c_node || c_node->filp != file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900896 DRM_ERROR("failed to get command node.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200897 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900898 }
899
900 /* buffer control */
901 switch (qbuf->buf_type) {
902 case IPP_BUF_ENQUEUE:
903 /* get memory node */
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200904 m_node = ipp_get_mem_node(drm_dev, c_node, qbuf);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900905 if (IS_ERR(m_node)) {
906 DRM_ERROR("failed to get m_node.\n");
907 return PTR_ERR(m_node);
908 }
909
910 /*
911 * first step get event for destination buffer.
912 * and second step when M2M case run with destination buffer
913 * if needed.
914 */
915 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
916 /* get event for destination buffer */
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200917 ret = ipp_get_event(drm_dev, c_node, qbuf);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900918 if (ret) {
919 DRM_ERROR("failed to get event.\n");
920 goto err_clean_node;
921 }
922
923 /*
924 * M2M case run play control for streaming feature.
925 * other case set address and waiting.
926 */
927 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
928 if (ret) {
929 DRM_ERROR("failed to run command.\n");
930 goto err_clean_node;
931 }
932 }
933 break;
934 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200935 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900936
937 /* put event for destination buffer */
938 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
939 ipp_put_event(c_node, qbuf);
940
941 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
942
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200943 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900944 break;
945 default:
946 DRM_ERROR("invalid buffer control.\n");
947 return -EINVAL;
948 }
949
950 return 0;
951
952err_clean_node:
953 DRM_ERROR("clean memory nodes.\n");
954
955 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
956 return ret;
957}
958
959static bool exynos_drm_ipp_check_valid(struct device *dev,
960 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
961{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900962 if (ctrl != IPP_CTRL_PLAY) {
963 if (pm_runtime_suspended(dev)) {
964 DRM_ERROR("pm:runtime_suspended.\n");
965 goto err_status;
966 }
967 }
968
969 switch (ctrl) {
970 case IPP_CTRL_PLAY:
971 if (state != IPP_STATE_IDLE)
972 goto err_status;
973 break;
974 case IPP_CTRL_STOP:
975 if (state == IPP_STATE_STOP)
976 goto err_status;
977 break;
978 case IPP_CTRL_PAUSE:
979 if (state != IPP_STATE_START)
980 goto err_status;
981 break;
982 case IPP_CTRL_RESUME:
983 if (state != IPP_STATE_STOP)
984 goto err_status;
985 break;
986 default:
987 DRM_ERROR("invalid state.\n");
988 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900989 }
990
991 return true;
992
993err_status:
994 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
995 return false;
996}
997
998int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
999 struct drm_file *file)
1000{
1001 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001002 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001003 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001004 struct ipp_context *ctx = get_ipp_context(dev);
1005 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1006 struct drm_exynos_ipp_cmd_work *cmd_work;
1007 struct drm_exynos_ipp_cmd_node *c_node;
1008
Eunchul Kimcb471f142012-12-14 18:10:31 +09001009 if (!ctx) {
1010 DRM_ERROR("invalid context.\n");
1011 return -EINVAL;
1012 }
1013
1014 if (!cmd_ctrl) {
1015 DRM_ERROR("invalid control parameter.\n");
1016 return -EINVAL;
1017 }
1018
YoungJun Chocbc4c332013-06-12 10:44:40 +09001019 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001020 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1021
1022 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1023 if (IS_ERR(ippdrv)) {
1024 DRM_ERROR("failed to get ipp driver.\n");
1025 return PTR_ERR(ippdrv);
1026 }
1027
1028 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1029 cmd_ctrl->prop_id);
Andrzej Hajda18383cb2014-09-02 14:56:21 +02001030 if (!c_node || c_node->filp != file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001031 DRM_ERROR("invalid command node list.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001032 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001033 }
1034
1035 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1036 c_node->state)) {
1037 DRM_ERROR("invalid state.\n");
1038 return -EINVAL;
1039 }
1040
1041 switch (cmd_ctrl->ctrl) {
1042 case IPP_CTRL_PLAY:
1043 if (pm_runtime_suspended(ippdrv->dev))
1044 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001045
Eunchul Kimcb471f142012-12-14 18:10:31 +09001046 c_node->state = IPP_STATE_START;
1047
1048 cmd_work = c_node->start_work;
1049 cmd_work->ctrl = cmd_ctrl->ctrl;
1050 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001051 break;
1052 case IPP_CTRL_STOP:
1053 cmd_work = c_node->stop_work;
1054 cmd_work->ctrl = cmd_ctrl->ctrl;
1055 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1056
1057 if (!wait_for_completion_timeout(&c_node->stop_complete,
1058 msecs_to_jiffies(300))) {
1059 DRM_ERROR("timeout stop:prop_id[%d]\n",
1060 c_node->property.prop_id);
1061 }
1062
1063 c_node->state = IPP_STATE_STOP;
1064 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001065 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001066 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001067
1068 if (list_empty(&ippdrv->cmd_list))
1069 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001070 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001071 break;
1072 case IPP_CTRL_PAUSE:
1073 cmd_work = c_node->stop_work;
1074 cmd_work->ctrl = cmd_ctrl->ctrl;
1075 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1076
1077 if (!wait_for_completion_timeout(&c_node->stop_complete,
1078 msecs_to_jiffies(200))) {
1079 DRM_ERROR("timeout stop:prop_id[%d]\n",
1080 c_node->property.prop_id);
1081 }
1082
1083 c_node->state = IPP_STATE_STOP;
1084 break;
1085 case IPP_CTRL_RESUME:
1086 c_node->state = IPP_STATE_START;
1087 cmd_work = c_node->start_work;
1088 cmd_work->ctrl = cmd_ctrl->ctrl;
1089 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1090 break;
1091 default:
1092 DRM_ERROR("could not support this state currently.\n");
1093 return -EINVAL;
1094 }
1095
YoungJun Chocbc4c332013-06-12 10:44:40 +09001096 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001097 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1098
1099 return 0;
1100}
1101
1102int exynos_drm_ippnb_register(struct notifier_block *nb)
1103{
1104 return blocking_notifier_chain_register(
1105 &exynos_drm_ippnb_list, nb);
1106}
1107
1108int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1109{
1110 return blocking_notifier_chain_unregister(
1111 &exynos_drm_ippnb_list, nb);
1112}
1113
1114int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1115{
1116 return blocking_notifier_call_chain(
1117 &exynos_drm_ippnb_list, val, v);
1118}
1119
1120static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1121 struct drm_exynos_ipp_property *property)
1122{
1123 struct exynos_drm_ipp_ops *ops = NULL;
1124 bool swap = false;
1125 int ret, i;
1126
1127 if (!property) {
1128 DRM_ERROR("invalid property parameter.\n");
1129 return -EINVAL;
1130 }
1131
YoungJun Chocbc4c332013-06-12 10:44:40 +09001132 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001133
1134 /* reset h/w block */
1135 if (ippdrv->reset &&
1136 ippdrv->reset(ippdrv->dev)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001137 return -EINVAL;
1138 }
1139
1140 /* set source,destination operations */
1141 for_each_ipp_ops(i) {
1142 struct drm_exynos_ipp_config *config =
1143 &property->config[i];
1144
1145 ops = ippdrv->ops[i];
1146 if (!ops || !config) {
1147 DRM_ERROR("not support ops and config.\n");
1148 return -EINVAL;
1149 }
1150
1151 /* set format */
1152 if (ops->set_fmt) {
1153 ret = ops->set_fmt(ippdrv->dev, config->fmt);
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 /* set transform for rotation, flip */
1159 if (ops->set_transf) {
1160 ret = ops->set_transf(ippdrv->dev, config->degree,
1161 config->flip, &swap);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001162 if (ret)
1163 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001164 }
1165
1166 /* set size */
1167 if (ops->set_size) {
1168 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1169 &config->sz);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001170 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001171 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001172 }
1173 }
1174
1175 return 0;
1176}
1177
1178static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1179 struct drm_exynos_ipp_cmd_node *c_node)
1180{
1181 struct drm_exynos_ipp_mem_node *m_node;
1182 struct drm_exynos_ipp_property *property = &c_node->property;
1183 struct list_head *head;
1184 int ret, i;
1185
YoungJun Chocbc4c332013-06-12 10:44:40 +09001186 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001187
1188 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001189 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001190
YoungJun Cho220db6f2014-05-26 10:17:20 +02001191 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001192 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001193 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001194 ret = -ENOMEM;
1195 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001196 }
1197
1198 /* set current property in ippdrv */
1199 ret = ipp_set_property(ippdrv, property);
1200 if (ret) {
1201 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001202 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001203 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001204 }
1205
1206 /* check command */
1207 switch (property->cmd) {
1208 case IPP_CMD_M2M:
1209 for_each_ipp_ops(i) {
1210 /* source/destination memory list */
1211 head = &c_node->mem_list[i];
1212
1213 m_node = list_first_entry(head,
1214 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001215
YoungJun Chocbc4c332013-06-12 10:44:40 +09001216 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001217
1218 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1219 if (ret) {
1220 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001221 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001222 }
1223 }
1224 break;
1225 case IPP_CMD_WB:
1226 /* destination memory list */
1227 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1228
1229 list_for_each_entry(m_node, head, list) {
1230 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1231 if (ret) {
1232 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001233 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001234 }
1235 }
1236 break;
1237 case IPP_CMD_OUTPUT:
1238 /* source memory list */
1239 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1240
1241 list_for_each_entry(m_node, head, list) {
1242 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1243 if (ret) {
1244 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001245 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001246 }
1247 }
1248 break;
1249 default:
1250 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001251 ret = -EINVAL;
1252 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001253 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001254 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001255
YoungJun Chocbc4c332013-06-12 10:44:40 +09001256 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001257
1258 /* start operations */
1259 if (ippdrv->start) {
1260 ret = ippdrv->start(ippdrv->dev, property->cmd);
1261 if (ret) {
1262 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001263 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001264 return ret;
1265 }
1266 }
1267
1268 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001269
1270err_unlock:
1271 mutex_unlock(&c_node->mem_lock);
1272 ippdrv->c_node = NULL;
1273 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001274}
1275
1276static int ipp_stop_property(struct drm_device *drm_dev,
1277 struct exynos_drm_ippdrv *ippdrv,
1278 struct drm_exynos_ipp_cmd_node *c_node)
1279{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001280 struct drm_exynos_ipp_property *property = &c_node->property;
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001281 int i;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001282
YoungJun Chocbc4c332013-06-12 10:44:40 +09001283 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001284
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001285 /* stop operations */
1286 if (ippdrv->stop)
1287 ippdrv->stop(ippdrv->dev, property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001288
1289 /* check command */
1290 switch (property->cmd) {
1291 case IPP_CMD_M2M:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001292 for_each_ipp_ops(i)
1293 ipp_clean_mem_nodes(drm_dev, c_node, i);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001294 break;
1295 case IPP_CMD_WB:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001296 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_DST);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001297 break;
1298 case IPP_CMD_OUTPUT:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001299 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_SRC);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001300 break;
1301 default:
1302 DRM_ERROR("invalid operations.\n");
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001303 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001304 }
1305
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001306 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001307}
1308
1309void ipp_sched_cmd(struct work_struct *work)
1310{
1311 struct drm_exynos_ipp_cmd_work *cmd_work =
Andrzej Hajda05afb1a2014-08-28 11:07:33 +02001312 container_of(work, struct drm_exynos_ipp_cmd_work, work);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001313 struct exynos_drm_ippdrv *ippdrv;
1314 struct drm_exynos_ipp_cmd_node *c_node;
1315 struct drm_exynos_ipp_property *property;
1316 int ret;
1317
Eunchul Kimcb471f142012-12-14 18:10:31 +09001318 ippdrv = cmd_work->ippdrv;
1319 if (!ippdrv) {
1320 DRM_ERROR("invalid ippdrv list.\n");
1321 return;
1322 }
1323
1324 c_node = cmd_work->c_node;
1325 if (!c_node) {
1326 DRM_ERROR("invalid command node list.\n");
1327 return;
1328 }
1329
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001330 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001331
1332 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001333
1334 switch (cmd_work->ctrl) {
1335 case IPP_CTRL_PLAY:
1336 case IPP_CTRL_RESUME:
1337 ret = ipp_start_property(ippdrv, c_node);
1338 if (ret) {
1339 DRM_ERROR("failed to start property:prop_id[%d]\n",
1340 c_node->property.prop_id);
1341 goto err_unlock;
1342 }
1343
1344 /*
1345 * M2M case supports wait_completion of transfer.
1346 * because M2M case supports single unit operation
1347 * with multiple queue.
1348 * M2M need to wait completion of data transfer.
1349 */
1350 if (ipp_is_m2m_cmd(property->cmd)) {
1351 if (!wait_for_completion_timeout
1352 (&c_node->start_complete, msecs_to_jiffies(200))) {
1353 DRM_ERROR("timeout event:prop_id[%d]\n",
1354 c_node->property.prop_id);
1355 goto err_unlock;
1356 }
1357 }
1358 break;
1359 case IPP_CTRL_STOP:
1360 case IPP_CTRL_PAUSE:
1361 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1362 c_node);
1363 if (ret) {
1364 DRM_ERROR("failed to stop property.\n");
1365 goto err_unlock;
1366 }
1367
1368 complete(&c_node->stop_complete);
1369 break;
1370 default:
1371 DRM_ERROR("unknown control type\n");
1372 break;
1373 }
1374
YoungJun Chocbc4c332013-06-12 10:44:40 +09001375 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001376
1377err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001378 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001379}
1380
1381static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1382 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1383{
1384 struct drm_device *drm_dev = ippdrv->drm_dev;
1385 struct drm_exynos_ipp_property *property = &c_node->property;
1386 struct drm_exynos_ipp_mem_node *m_node;
1387 struct drm_exynos_ipp_queue_buf qbuf;
1388 struct drm_exynos_ipp_send_event *e;
1389 struct list_head *head;
1390 struct timeval now;
1391 unsigned long flags;
1392 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1393 int ret, i;
1394
1395 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001396 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001397
1398 if (!drm_dev) {
1399 DRM_ERROR("failed to get drm_dev.\n");
1400 return -EINVAL;
1401 }
1402
1403 if (!property) {
1404 DRM_ERROR("failed to get property.\n");
1405 return -EINVAL;
1406 }
1407
YoungJun Cho4d520762014-05-26 10:17:21 +02001408 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001409 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001410 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001411 ret = 0;
1412 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001413 }
1414
YoungJun Cho220db6f2014-05-26 10:17:20 +02001415 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001416 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001417 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001418 ret = 0;
1419 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001420 }
1421
1422 /* check command */
1423 switch (property->cmd) {
1424 case IPP_CMD_M2M:
1425 for_each_ipp_ops(i) {
1426 /* source/destination memory list */
1427 head = &c_node->mem_list[i];
1428
1429 m_node = list_first_entry(head,
1430 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001431
1432 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001433 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001434 i ? "dst" : "src", tbuf_id[i]);
1435
1436 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1437 if (ret)
1438 DRM_ERROR("failed to put m_node.\n");
1439 }
1440 break;
1441 case IPP_CMD_WB:
1442 /* clear buf for finding */
1443 memset(&qbuf, 0x0, sizeof(qbuf));
1444 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1445 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1446
1447 /* get memory node entry */
1448 m_node = ipp_find_mem_node(c_node, &qbuf);
1449 if (!m_node) {
1450 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001451 ret = -ENOMEM;
1452 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001453 }
1454
1455 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1456
1457 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1458 if (ret)
1459 DRM_ERROR("failed to put m_node.\n");
1460 break;
1461 case IPP_CMD_OUTPUT:
1462 /* source memory list */
1463 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1464
1465 m_node = list_first_entry(head,
1466 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001467
1468 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1469
1470 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1471 if (ret)
1472 DRM_ERROR("failed to put m_node.\n");
1473 break;
1474 default:
1475 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001476 ret = -EINVAL;
1477 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001478 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001479 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001480
1481 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1482 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1483 tbuf_id[1], buf_id[1], property->prop_id);
1484
1485 /*
1486 * command node have event list of destination buffer
1487 * If destination buffer enqueue to mem list,
1488 * then we make event and link to event list tail.
1489 * so, we get first event for first enqueued buffer.
1490 */
1491 e = list_first_entry(&c_node->event_list,
1492 struct drm_exynos_ipp_send_event, base.link);
1493
Eunchul Kimcb471f142012-12-14 18:10:31 +09001494 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001495 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001496 e->event.tv_sec = now.tv_sec;
1497 e->event.tv_usec = now.tv_usec;
1498 e->event.prop_id = property->prop_id;
1499
1500 /* set buffer id about source destination */
1501 for_each_ipp_ops(i)
1502 e->event.buf_id[i] = tbuf_id[i];
1503
1504 spin_lock_irqsave(&drm_dev->event_lock, flags);
1505 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1506 wake_up_interruptible(&e->base.file_priv->event_wait);
1507 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001508 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001509
YoungJun Chocbc4c332013-06-12 10:44:40 +09001510 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001511 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1512
1513 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001514
1515err_mem_unlock:
1516 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001517err_event_unlock:
1518 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001519 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001520}
1521
1522void ipp_sched_event(struct work_struct *work)
1523{
1524 struct drm_exynos_ipp_event_work *event_work =
Andrzej Hajda05afb1a2014-08-28 11:07:33 +02001525 container_of(work, struct drm_exynos_ipp_event_work, work);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001526 struct exynos_drm_ippdrv *ippdrv;
1527 struct drm_exynos_ipp_cmd_node *c_node;
1528 int ret;
1529
1530 if (!event_work) {
1531 DRM_ERROR("failed to get event_work.\n");
1532 return;
1533 }
1534
YoungJun Chocbc4c332013-06-12 10:44:40 +09001535 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001536
1537 ippdrv = event_work->ippdrv;
1538 if (!ippdrv) {
1539 DRM_ERROR("failed to get ipp driver.\n");
1540 return;
1541 }
1542
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001543 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001544 if (!c_node) {
1545 DRM_ERROR("failed to get command node.\n");
1546 return;
1547 }
1548
1549 /*
1550 * IPP supports command thread, event thread synchronization.
1551 * If IPP close immediately from user land, then IPP make
1552 * synchronization with command thread, so make complete event.
1553 * or going out operations.
1554 */
1555 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001556 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1557 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001558 goto err_completion;
1559 }
1560
Eunchul Kimcb471f142012-12-14 18:10:31 +09001561 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1562 if (ret) {
1563 DRM_ERROR("failed to send event.\n");
1564 goto err_completion;
1565 }
1566
1567err_completion:
1568 if (ipp_is_m2m_cmd(c_node->property.cmd))
1569 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001570}
1571
1572static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1573{
1574 struct ipp_context *ctx = get_ipp_context(dev);
1575 struct exynos_drm_ippdrv *ippdrv;
1576 int ret, count = 0;
1577
Eunchul Kimcb471f142012-12-14 18:10:31 +09001578 /* get ipp driver entry */
1579 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1580 ippdrv->drm_dev = drm_dev;
1581
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001582 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
1583 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001584 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001585 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001586 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001587 ippdrv->prop_list.ipp_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001588
YoungJun Chocbc4c332013-06-12 10:44:40 +09001589 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001590 count++, (int)ippdrv, ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001591
1592 /* store parent device for node */
1593 ippdrv->parent_dev = dev;
1594
1595 /* store event work queue and handler */
1596 ippdrv->event_workq = ctx->event_workq;
1597 ippdrv->sched_event = ipp_sched_event;
1598 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001599 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001600
1601 if (is_drm_iommu_supported(drm_dev)) {
1602 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1603 if (ret) {
1604 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001605 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001606 }
1607 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001608 }
1609
1610 return 0;
1611
YoungJun Cho075436b2014-05-26 10:17:19 +02001612err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001613 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001614 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1615 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001616 if (is_drm_iommu_supported(drm_dev))
1617 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1618
YoungJun Cho075436b2014-05-26 10:17:19 +02001619 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1620 ippdrv->prop_list.ipp_id);
1621 }
1622
Eunchul Kimcb471f142012-12-14 18:10:31 +09001623 return ret;
1624}
1625
1626static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1627{
Andrzej Hajdaa36ed462014-09-09 15:16:05 +02001628 struct exynos_drm_ippdrv *ippdrv, *t;
YoungJun Cho075436b2014-05-26 10:17:19 +02001629 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001630
Eunchul Kimcb471f142012-12-14 18:10:31 +09001631 /* get ipp driver entry */
Andrzej Hajdaa36ed462014-09-09 15:16:05 +02001632 list_for_each_entry_safe(ippdrv, t, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001633 if (is_drm_iommu_supported(drm_dev))
1634 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1635
YoungJun Cho075436b2014-05-26 10:17:19 +02001636 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1637 ippdrv->prop_list.ipp_id);
1638
Eunchul Kimcb471f142012-12-14 18:10:31 +09001639 ippdrv->drm_dev = NULL;
1640 exynos_drm_ippdrv_unregister(ippdrv);
1641 }
1642}
1643
1644static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1645 struct drm_file *file)
1646{
1647 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001648
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001649 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001650
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001651 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001652
1653 return 0;
1654}
1655
1656static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1657 struct drm_file *file)
1658{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001659 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001660 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001661 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1662 int count = 0;
1663
Eunchul Kimcb471f142012-12-14 18:10:31 +09001664 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001665 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001666 list_for_each_entry_safe(c_node, tc_node,
1667 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001668 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1669 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001670
Andrzej Hajda21a825e2014-08-28 11:07:28 +02001671 if (c_node->filp == file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001672 /*
1673 * userland goto unnormal state. process killed.
1674 * and close the file.
1675 * so, IPP didn't called stop cmd ctrl.
1676 * so, we are make stop operation in this state.
1677 */
1678 if (c_node->state == IPP_STATE_START) {
1679 ipp_stop_property(drm_dev, ippdrv,
1680 c_node);
1681 c_node->state = IPP_STATE_STOP;
1682 }
1683
1684 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001685 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001686 if (list_empty(&ippdrv->cmd_list))
1687 pm_runtime_put_sync(ippdrv->dev);
1688 }
1689 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001690 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001691 }
1692
Eunchul Kimcb471f142012-12-14 18:10:31 +09001693 return;
1694}
1695
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001696static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001697{
1698 struct device *dev = &pdev->dev;
1699 struct ipp_context *ctx;
1700 struct exynos_drm_subdrv *subdrv;
1701 int ret;
1702
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001703 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001704 if (!ctx)
1705 return -ENOMEM;
1706
Eunchul Kimcb471f142012-12-14 18:10:31 +09001707 mutex_init(&ctx->ipp_lock);
1708 mutex_init(&ctx->prop_lock);
1709
1710 idr_init(&ctx->ipp_idr);
1711 idr_init(&ctx->prop_idr);
1712
1713 /*
1714 * create single thread for ipp event
1715 * IPP supports event thread for IPP drivers.
1716 * IPP driver send event_work to this thread.
1717 * and IPP event thread send event to user process.
1718 */
1719 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1720 if (!ctx->event_workq) {
1721 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301722 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001723 }
1724
1725 /*
1726 * create single thread for ipp command
1727 * IPP supports command thread for user process.
1728 * user process make command node using set property ioctl.
1729 * and make start_work and send this work to command thread.
1730 * and then this command thread start property.
1731 */
1732 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1733 if (!ctx->cmd_workq) {
1734 dev_err(dev, "failed to create cmd workqueue\n");
1735 ret = -EINVAL;
1736 goto err_event_workq;
1737 }
1738
1739 /* set sub driver informations */
1740 subdrv = &ctx->subdrv;
1741 subdrv->dev = dev;
1742 subdrv->probe = ipp_subdrv_probe;
1743 subdrv->remove = ipp_subdrv_remove;
1744 subdrv->open = ipp_subdrv_open;
1745 subdrv->close = ipp_subdrv_close;
1746
1747 platform_set_drvdata(pdev, ctx);
1748
1749 ret = exynos_drm_subdrv_register(subdrv);
1750 if (ret < 0) {
1751 DRM_ERROR("failed to register drm ipp device.\n");
1752 goto err_cmd_workq;
1753 }
1754
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001755 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001756
1757 return 0;
1758
1759err_cmd_workq:
1760 destroy_workqueue(ctx->cmd_workq);
1761err_event_workq:
1762 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001763 return ret;
1764}
1765
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001766static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001767{
1768 struct ipp_context *ctx = platform_get_drvdata(pdev);
1769
Eunchul Kimcb471f142012-12-14 18:10:31 +09001770 /* unregister sub driver */
1771 exynos_drm_subdrv_unregister(&ctx->subdrv);
1772
1773 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001774 idr_destroy(&ctx->ipp_idr);
1775 idr_destroy(&ctx->prop_idr);
1776
1777 mutex_destroy(&ctx->ipp_lock);
1778 mutex_destroy(&ctx->prop_lock);
1779
1780 /* destroy command, event work queue */
1781 destroy_workqueue(ctx->cmd_workq);
1782 destroy_workqueue(ctx->event_workq);
1783
Eunchul Kimcb471f142012-12-14 18:10:31 +09001784 return 0;
1785}
1786
Eunchul Kimcb471f142012-12-14 18:10:31 +09001787struct platform_driver ipp_driver = {
1788 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001789 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001790 .driver = {
1791 .name = "exynos-drm-ipp",
1792 .owner = THIS_MODULE,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001793 },
1794};
1795