blob: 67d24236e745c4dd1cc014ba0c5386784d3441f8 [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;
Hyungwon Hwang3c104732015-06-09 12:45:15 +0900458 unsigned long size, buf_size = 0, plane_size, img_size = 0;
459 unsigned int bpp, width, height;
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100460 int i;
461
Hyungwon Hwangb224fa92015-06-09 12:45:14 +0900462 ipp_cfg = &c_node->property.config[m_node->ops_id];
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100463 num_plane = drm_format_num_planes(ipp_cfg->fmt);
464
465 /**
466 * This is a rather simplified validation of a memory node.
467 * It basically verifies provided gem object handles
468 * and the buffer sizes with respect to current configuration.
469 * This is not the best that can be done
470 * but it seems more than enough
471 */
472 for (i = 0; i < num_plane; ++i) {
Hyungwon Hwang3c104732015-06-09 12:45:15 +0900473 width = ipp_cfg->sz.hsize;
474 height = ipp_cfg->sz.vsize;
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100475 bpp = drm_format_plane_cpp(ipp_cfg->fmt, i);
Hyungwon Hwang3c104732015-06-09 12:45:15 +0900476
477 /*
478 * The result of drm_format_plane_cpp() for chroma planes must
479 * be used with drm_format_xxxx_chroma_subsampling() for
480 * correct result.
481 */
482 if (i > 0) {
483 width /= drm_format_horz_chroma_subsampling(
484 ipp_cfg->fmt);
485 height /= drm_format_vert_chroma_subsampling(
486 ipp_cfg->fmt);
487 }
488 plane_size = width * height * bpp;
489 img_size += plane_size;
490
491 if (m_node->buf_info.handles[i]) {
492 size = exynos_drm_gem_get_size(drm_dev,
493 m_node->buf_info.handles[i],
494 c_node->filp);
495 if (plane_size > size) {
496 DRM_ERROR(
497 "buffer %d is smaller than required\n",
498 i);
499 return -EINVAL;
500 }
501
502 buf_size += size;
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100503 }
504 }
Hyungwon Hwang3c104732015-06-09 12:45:15 +0900505
506 if (buf_size < img_size) {
507 DRM_ERROR("size of buffers(%lu) is smaller than image(%lu)\n",
508 buf_size, img_size);
509 return -EINVAL;
510 }
511
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100512 return 0;
513}
514
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200515static int ipp_put_mem_node(struct drm_device *drm_dev,
516 struct drm_exynos_ipp_cmd_node *c_node,
517 struct drm_exynos_ipp_mem_node *m_node)
518{
519 int i;
520
521 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
522
523 if (!m_node) {
524 DRM_ERROR("invalid dequeue node.\n");
525 return -EFAULT;
526 }
527
528 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
529
530 /* put gem buffer */
531 for_each_ipp_planar(i) {
532 unsigned long handle = m_node->buf_info.handles[i];
533 if (handle)
534 exynos_drm_gem_put_dma_addr(drm_dev, handle,
535 c_node->filp);
536 }
537
538 list_del(&m_node->list);
539 kfree(m_node);
540
541 return 0;
542}
543
544static struct drm_exynos_ipp_mem_node
545 *ipp_get_mem_node(struct drm_device *drm_dev,
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200546 struct drm_exynos_ipp_cmd_node *c_node,
547 struct drm_exynos_ipp_queue_buf *qbuf)
548{
549 struct drm_exynos_ipp_mem_node *m_node;
550 struct drm_exynos_ipp_buf_info *buf_info;
551 int i;
552
553 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
554 if (!m_node)
555 return ERR_PTR(-ENOMEM);
556
557 buf_info = &m_node->buf_info;
558
559 /* operations, buffer id */
560 m_node->ops_id = qbuf->ops_id;
561 m_node->prop_id = qbuf->prop_id;
562 m_node->buf_id = qbuf->buf_id;
563 INIT_LIST_HEAD(&m_node->list);
564
565 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
566 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
567
568 for_each_ipp_planar(i) {
569 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
570
571 /* get dma address by handle */
572 if (qbuf->handle[i]) {
573 dma_addr_t *addr;
574
575 addr = exynos_drm_gem_get_dma_addr(drm_dev,
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200576 qbuf->handle[i], c_node->filp);
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200577 if (IS_ERR(addr)) {
578 DRM_ERROR("failed to get addr.\n");
579 ipp_put_mem_node(drm_dev, c_node, m_node);
580 return ERR_PTR(-EFAULT);
581 }
582
583 buf_info->handles[i] = qbuf->handle[i];
584 buf_info->base[i] = *addr;
585 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
586 buf_info->base[i], buf_info->handles[i]);
587 }
588 }
589
590 mutex_lock(&c_node->mem_lock);
Beata Michalskae44bf6b2015-03-04 15:02:49 +0100591 if (ipp_validate_mem_node(drm_dev, m_node, c_node)) {
592 ipp_put_mem_node(drm_dev, c_node, m_node);
593 mutex_unlock(&c_node->mem_lock);
594 return ERR_PTR(-EFAULT);
595 }
Andrzej Hajdac4a856a2014-08-28 11:07:31 +0200596 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
597 mutex_unlock(&c_node->mem_lock);
598
599 return m_node;
600}
601
602static void ipp_clean_mem_nodes(struct drm_device *drm_dev,
603 struct drm_exynos_ipp_cmd_node *c_node, int ops)
604{
605 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
606 struct list_head *head = &c_node->mem_list[ops];
607
608 mutex_lock(&c_node->mem_lock);
609
610 list_for_each_entry_safe(m_node, tm_node, head, list) {
611 int ret;
612
613 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
614 if (ret)
615 DRM_ERROR("failed to put m_node.\n");
616 }
617
618 mutex_unlock(&c_node->mem_lock);
619}
620
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200621static void ipp_free_event(struct drm_pending_event *event)
622{
623 kfree(event);
624}
625
626static int ipp_get_event(struct drm_device *drm_dev,
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200627 struct drm_exynos_ipp_cmd_node *c_node,
628 struct drm_exynos_ipp_queue_buf *qbuf)
629{
630 struct drm_exynos_ipp_send_event *e;
631 unsigned long flags;
632
633 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
634
635 e = kzalloc(sizeof(*e), GFP_KERNEL);
636 if (!e) {
637 spin_lock_irqsave(&drm_dev->event_lock, flags);
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200638 c_node->filp->event_space += sizeof(e->event);
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200639 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
640 return -ENOMEM;
641 }
642
643 /* make event */
644 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
645 e->event.base.length = sizeof(e->event);
646 e->event.user_data = qbuf->user_data;
647 e->event.prop_id = qbuf->prop_id;
648 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
649 e->base.event = &e->event.base;
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200650 e->base.file_priv = c_node->filp;
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200651 e->base.destroy = ipp_free_event;
652 mutex_lock(&c_node->event_lock);
653 list_add_tail(&e->base.link, &c_node->event_list);
654 mutex_unlock(&c_node->event_lock);
655
656 return 0;
657}
658
659static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
660 struct drm_exynos_ipp_queue_buf *qbuf)
661{
662 struct drm_exynos_ipp_send_event *e, *te;
663 int count = 0;
664
665 mutex_lock(&c_node->event_lock);
666 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
667 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
668
669 /*
670 * qbuf == NULL condition means all event deletion.
671 * stop operations want to delete all event list.
672 * another case delete only same buf id.
673 */
674 if (!qbuf) {
675 /* delete list */
676 list_del(&e->base.link);
677 kfree(e);
678 }
679
680 /* compare buffer id */
681 if (qbuf && (qbuf->buf_id ==
682 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
683 /* delete list */
684 list_del(&e->base.link);
685 kfree(e);
686 goto out_unlock;
687 }
688 }
689
690out_unlock:
691 mutex_unlock(&c_node->event_lock);
692 return;
693}
694
YoungJun Cho075436b2014-05-26 10:17:19 +0200695static void ipp_clean_cmd_node(struct ipp_context *ctx,
696 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900697{
Andrzej Hajda22e816f2014-08-28 11:07:32 +0200698 int i;
699
Andrzej Hajda6f7d48e2014-08-28 11:07:26 +0200700 /* cancel works */
701 cancel_work_sync(&c_node->start_work->work);
702 cancel_work_sync(&c_node->stop_work->work);
703 cancel_work_sync(&c_node->event_work->work);
704
Andrzej Hajdac0592c82014-08-28 11:07:35 +0200705 /* put event */
706 ipp_put_event(c_node, NULL);
707
Andrzej Hajda22e816f2014-08-28 11:07:32 +0200708 for_each_ipp_ops(i)
709 ipp_clean_mem_nodes(ctx->subdrv.drm_dev, c_node, i);
710
Eunchul Kimcb471f142012-12-14 18:10:31 +0900711 /* delete list */
712 list_del(&c_node->list);
713
YoungJun Cho075436b2014-05-26 10:17:19 +0200714 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
715 c_node->property.prop_id);
716
Eunchul Kimcb471f142012-12-14 18:10:31 +0900717 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200718 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900719 mutex_destroy(&c_node->mem_lock);
720 mutex_destroy(&c_node->event_lock);
721
722 /* free command node */
723 kfree(c_node->start_work);
724 kfree(c_node->stop_work);
725 kfree(c_node->event_work);
726 kfree(c_node);
727}
728
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200729static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900730{
Andrzej Hajdafb5ee012014-07-03 15:10:32 +0200731 switch (c_node->property.cmd) {
732 case IPP_CMD_WB:
733 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
734 case IPP_CMD_OUTPUT:
735 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
736 case IPP_CMD_M2M:
737 default:
738 return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
739 !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900740 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900741}
742
743static struct drm_exynos_ipp_mem_node
744 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
745 struct drm_exynos_ipp_queue_buf *qbuf)
746{
747 struct drm_exynos_ipp_mem_node *m_node;
748 struct list_head *head;
749 int count = 0;
750
YoungJun Chocbc4c332013-06-12 10:44:40 +0900751 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900752
753 /* source/destination memory list */
754 head = &c_node->mem_list[qbuf->ops_id];
755
756 /* find memory node from memory list */
757 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900758 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900759
760 /* compare buffer id */
761 if (m_node->buf_id == qbuf->buf_id)
762 return m_node;
763 }
764
765 return NULL;
766}
767
768static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
769 struct drm_exynos_ipp_cmd_node *c_node,
770 struct drm_exynos_ipp_mem_node *m_node)
771{
772 struct exynos_drm_ipp_ops *ops = NULL;
773 int ret = 0;
774
YoungJun Chocbc4c332013-06-12 10:44:40 +0900775 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900776
777 if (!m_node) {
778 DRM_ERROR("invalid queue node.\n");
779 return -EFAULT;
780 }
781
YoungJun Chocbc4c332013-06-12 10:44:40 +0900782 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900783
784 /* get operations callback */
785 ops = ippdrv->ops[m_node->ops_id];
786 if (!ops) {
787 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200788 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900789 }
790
791 /* set address and enable irq */
792 if (ops->set_addr) {
793 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
794 m_node->buf_id, IPP_BUF_ENQUEUE);
795 if (ret) {
796 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200797 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900798 }
799 }
800
Eunchul Kimcb471f142012-12-14 18:10:31 +0900801 return ret;
802}
803
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530804static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900805 struct exynos_drm_ippdrv *ippdrv,
806 struct drm_exynos_ipp_cmd_work *cmd_work,
807 struct drm_exynos_ipp_cmd_node *c_node)
808{
809 struct ipp_context *ctx = get_ipp_context(dev);
810
811 cmd_work->ippdrv = ippdrv;
812 cmd_work->c_node = c_node;
Andrzej Hajda05afb1a2014-08-28 11:07:33 +0200813 queue_work(ctx->cmd_workq, &cmd_work->work);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900814}
815
816static int ipp_queue_buf_with_run(struct device *dev,
817 struct drm_exynos_ipp_cmd_node *c_node,
818 struct drm_exynos_ipp_mem_node *m_node,
819 struct drm_exynos_ipp_queue_buf *qbuf)
820{
821 struct exynos_drm_ippdrv *ippdrv;
822 struct drm_exynos_ipp_property *property;
823 struct exynos_drm_ipp_ops *ops;
824 int ret;
825
Eunchul Kimcb471f142012-12-14 18:10:31 +0900826 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530827 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900828 DRM_ERROR("failed to get ipp driver.\n");
829 return -EFAULT;
830 }
831
832 ops = ippdrv->ops[qbuf->ops_id];
833 if (!ops) {
834 DRM_ERROR("failed to get ops.\n");
835 return -EFAULT;
836 }
837
838 property = &c_node->property;
839
840 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900841 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900842 return 0;
843 }
844
YoungJun Cho220db6f2014-05-26 10:17:20 +0200845 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900846 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200847 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900848 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900849 return 0;
850 }
851
852 /*
853 * If set destination buffer and enabled clock,
854 * then m2m operations need start operations at queue_buf
855 */
856 if (ipp_is_m2m_cmd(property->cmd)) {
857 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
858
859 cmd_work->ctrl = IPP_CTRL_PLAY;
860 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
861 } else {
862 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
863 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200864 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900865 DRM_ERROR("failed to set m node.\n");
866 return ret;
867 }
868 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200869 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900870
871 return 0;
872}
873
874static void ipp_clean_queue_buf(struct drm_device *drm_dev,
875 struct drm_exynos_ipp_cmd_node *c_node,
876 struct drm_exynos_ipp_queue_buf *qbuf)
877{
878 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
879
YoungJun Choc66ce402014-05-26 10:17:15 +0200880 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200881 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200882 list_for_each_entry_safe(m_node, tm_node,
883 &c_node->mem_list[qbuf->ops_id], list) {
884 if (m_node->buf_id == qbuf->buf_id &&
885 m_node->ops_id == qbuf->ops_id)
886 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900887 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200888 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900889}
890
891int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
892 struct drm_file *file)
893{
894 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200895 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900896 struct ipp_context *ctx = get_ipp_context(dev);
897 struct drm_exynos_ipp_queue_buf *qbuf = data;
898 struct drm_exynos_ipp_cmd_node *c_node;
899 struct drm_exynos_ipp_mem_node *m_node;
900 int ret;
901
Eunchul Kimcb471f142012-12-14 18:10:31 +0900902 if (!qbuf) {
903 DRM_ERROR("invalid buf parameter.\n");
904 return -EINVAL;
905 }
906
907 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
908 DRM_ERROR("invalid ops parameter.\n");
909 return -EINVAL;
910 }
911
YoungJun Chocbc4c332013-06-12 10:44:40 +0900912 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
913 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900914 qbuf->buf_id, qbuf->buf_type);
915
916 /* find command node */
917 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
918 qbuf->prop_id);
Andrzej Hajda18383cb2014-09-02 14:56:21 +0200919 if (!c_node || c_node->filp != file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900920 DRM_ERROR("failed to get command node.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +0200921 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900922 }
923
924 /* buffer control */
925 switch (qbuf->buf_type) {
926 case IPP_BUF_ENQUEUE:
927 /* get memory node */
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200928 m_node = ipp_get_mem_node(drm_dev, c_node, qbuf);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900929 if (IS_ERR(m_node)) {
930 DRM_ERROR("failed to get m_node.\n");
931 return PTR_ERR(m_node);
932 }
933
934 /*
935 * first step get event for destination buffer.
936 * and second step when M2M case run with destination buffer
937 * if needed.
938 */
939 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
940 /* get event for destination buffer */
Andrzej Hajdad9b97342014-09-02 14:55:06 +0200941 ret = ipp_get_event(drm_dev, c_node, qbuf);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900942 if (ret) {
943 DRM_ERROR("failed to get event.\n");
944 goto err_clean_node;
945 }
946
947 /*
948 * M2M case run play control for streaming feature.
949 * other case set address and waiting.
950 */
951 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
952 if (ret) {
953 DRM_ERROR("failed to run command.\n");
954 goto err_clean_node;
955 }
956 }
957 break;
958 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200959 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900960
961 /* put event for destination buffer */
962 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
963 ipp_put_event(c_node, qbuf);
964
965 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
966
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200967 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900968 break;
969 default:
970 DRM_ERROR("invalid buffer control.\n");
971 return -EINVAL;
972 }
973
974 return 0;
975
976err_clean_node:
977 DRM_ERROR("clean memory nodes.\n");
978
979 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
980 return ret;
981}
982
983static bool exynos_drm_ipp_check_valid(struct device *dev,
984 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
985{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900986 if (ctrl != IPP_CTRL_PLAY) {
987 if (pm_runtime_suspended(dev)) {
988 DRM_ERROR("pm:runtime_suspended.\n");
989 goto err_status;
990 }
991 }
992
993 switch (ctrl) {
994 case IPP_CTRL_PLAY:
995 if (state != IPP_STATE_IDLE)
996 goto err_status;
997 break;
998 case IPP_CTRL_STOP:
999 if (state == IPP_STATE_STOP)
1000 goto err_status;
1001 break;
1002 case IPP_CTRL_PAUSE:
1003 if (state != IPP_STATE_START)
1004 goto err_status;
1005 break;
1006 case IPP_CTRL_RESUME:
1007 if (state != IPP_STATE_STOP)
1008 goto err_status;
1009 break;
1010 default:
1011 DRM_ERROR("invalid state.\n");
1012 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001013 }
1014
1015 return true;
1016
1017err_status:
1018 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1019 return false;
1020}
1021
1022int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1023 struct drm_file *file)
1024{
1025 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001026 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001027 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001028 struct ipp_context *ctx = get_ipp_context(dev);
1029 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1030 struct drm_exynos_ipp_cmd_work *cmd_work;
1031 struct drm_exynos_ipp_cmd_node *c_node;
1032
Eunchul Kimcb471f142012-12-14 18:10:31 +09001033 if (!ctx) {
1034 DRM_ERROR("invalid context.\n");
1035 return -EINVAL;
1036 }
1037
1038 if (!cmd_ctrl) {
1039 DRM_ERROR("invalid control parameter.\n");
1040 return -EINVAL;
1041 }
1042
YoungJun Chocbc4c332013-06-12 10:44:40 +09001043 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001044 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1045
1046 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1047 if (IS_ERR(ippdrv)) {
1048 DRM_ERROR("failed to get ipp driver.\n");
1049 return PTR_ERR(ippdrv);
1050 }
1051
1052 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1053 cmd_ctrl->prop_id);
Andrzej Hajda18383cb2014-09-02 14:56:21 +02001054 if (!c_node || c_node->filp != file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001055 DRM_ERROR("invalid command node list.\n");
Andrzej Hajda134f0e92014-07-03 15:10:34 +02001056 return -ENODEV;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001057 }
1058
1059 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1060 c_node->state)) {
1061 DRM_ERROR("invalid state.\n");
1062 return -EINVAL;
1063 }
1064
1065 switch (cmd_ctrl->ctrl) {
1066 case IPP_CTRL_PLAY:
1067 if (pm_runtime_suspended(ippdrv->dev))
1068 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001069
Eunchul Kimcb471f142012-12-14 18:10:31 +09001070 c_node->state = IPP_STATE_START;
1071
1072 cmd_work = c_node->start_work;
1073 cmd_work->ctrl = cmd_ctrl->ctrl;
1074 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001075 break;
1076 case IPP_CTRL_STOP:
1077 cmd_work = c_node->stop_work;
1078 cmd_work->ctrl = cmd_ctrl->ctrl;
1079 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1080
1081 if (!wait_for_completion_timeout(&c_node->stop_complete,
1082 msecs_to_jiffies(300))) {
1083 DRM_ERROR("timeout stop:prop_id[%d]\n",
1084 c_node->property.prop_id);
1085 }
1086
1087 c_node->state = IPP_STATE_STOP;
1088 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001089 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001090 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001091
1092 if (list_empty(&ippdrv->cmd_list))
1093 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001094 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001095 break;
1096 case IPP_CTRL_PAUSE:
1097 cmd_work = c_node->stop_work;
1098 cmd_work->ctrl = cmd_ctrl->ctrl;
1099 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1100
1101 if (!wait_for_completion_timeout(&c_node->stop_complete,
1102 msecs_to_jiffies(200))) {
1103 DRM_ERROR("timeout stop:prop_id[%d]\n",
1104 c_node->property.prop_id);
1105 }
1106
1107 c_node->state = IPP_STATE_STOP;
1108 break;
1109 case IPP_CTRL_RESUME:
1110 c_node->state = IPP_STATE_START;
1111 cmd_work = c_node->start_work;
1112 cmd_work->ctrl = cmd_ctrl->ctrl;
1113 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1114 break;
1115 default:
1116 DRM_ERROR("could not support this state currently.\n");
1117 return -EINVAL;
1118 }
1119
YoungJun Chocbc4c332013-06-12 10:44:40 +09001120 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001121 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1122
1123 return 0;
1124}
1125
1126int exynos_drm_ippnb_register(struct notifier_block *nb)
1127{
1128 return blocking_notifier_chain_register(
1129 &exynos_drm_ippnb_list, nb);
1130}
1131
1132int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1133{
1134 return blocking_notifier_chain_unregister(
1135 &exynos_drm_ippnb_list, nb);
1136}
1137
1138int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1139{
1140 return blocking_notifier_call_chain(
1141 &exynos_drm_ippnb_list, val, v);
1142}
1143
1144static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1145 struct drm_exynos_ipp_property *property)
1146{
1147 struct exynos_drm_ipp_ops *ops = NULL;
1148 bool swap = false;
1149 int ret, i;
1150
1151 if (!property) {
1152 DRM_ERROR("invalid property parameter.\n");
1153 return -EINVAL;
1154 }
1155
YoungJun Chocbc4c332013-06-12 10:44:40 +09001156 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001157
1158 /* reset h/w block */
1159 if (ippdrv->reset &&
1160 ippdrv->reset(ippdrv->dev)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001161 return -EINVAL;
1162 }
1163
1164 /* set source,destination operations */
1165 for_each_ipp_ops(i) {
1166 struct drm_exynos_ipp_config *config =
1167 &property->config[i];
1168
1169 ops = ippdrv->ops[i];
1170 if (!ops || !config) {
1171 DRM_ERROR("not support ops and config.\n");
1172 return -EINVAL;
1173 }
1174
1175 /* set format */
1176 if (ops->set_fmt) {
1177 ret = ops->set_fmt(ippdrv->dev, config->fmt);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001178 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001179 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001180 }
1181
1182 /* set transform for rotation, flip */
1183 if (ops->set_transf) {
1184 ret = ops->set_transf(ippdrv->dev, config->degree,
1185 config->flip, &swap);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001186 if (ret)
1187 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001188 }
1189
1190 /* set size */
1191 if (ops->set_size) {
1192 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1193 &config->sz);
Andrzej Hajda57ace332014-07-03 15:10:35 +02001194 if (ret)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001195 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001196 }
1197 }
1198
1199 return 0;
1200}
1201
1202static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1203 struct drm_exynos_ipp_cmd_node *c_node)
1204{
1205 struct drm_exynos_ipp_mem_node *m_node;
1206 struct drm_exynos_ipp_property *property = &c_node->property;
1207 struct list_head *head;
1208 int ret, i;
1209
YoungJun Chocbc4c332013-06-12 10:44:40 +09001210 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001211
1212 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001213 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001214
YoungJun Cho220db6f2014-05-26 10:17:20 +02001215 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001216 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001217 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001218 ret = -ENOMEM;
1219 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001220 }
1221
1222 /* set current property in ippdrv */
1223 ret = ipp_set_property(ippdrv, property);
1224 if (ret) {
1225 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001226 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001227 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001228 }
1229
1230 /* check command */
1231 switch (property->cmd) {
1232 case IPP_CMD_M2M:
1233 for_each_ipp_ops(i) {
1234 /* source/destination memory list */
1235 head = &c_node->mem_list[i];
1236
1237 m_node = list_first_entry(head,
1238 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001239
YoungJun Chocbc4c332013-06-12 10:44:40 +09001240 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001241
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 case IPP_CMD_WB:
1250 /* destination memory list */
1251 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1252
1253 list_for_each_entry(m_node, head, list) {
1254 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1255 if (ret) {
1256 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001257 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001258 }
1259 }
1260 break;
1261 case IPP_CMD_OUTPUT:
1262 /* source memory list */
1263 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1264
1265 list_for_each_entry(m_node, head, list) {
1266 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1267 if (ret) {
1268 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001269 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001270 }
1271 }
1272 break;
1273 default:
1274 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001275 ret = -EINVAL;
1276 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001277 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001278 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001279
YoungJun Chocbc4c332013-06-12 10:44:40 +09001280 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001281
1282 /* start operations */
1283 if (ippdrv->start) {
1284 ret = ippdrv->start(ippdrv->dev, property->cmd);
1285 if (ret) {
1286 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001287 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001288 return ret;
1289 }
1290 }
1291
1292 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001293
1294err_unlock:
1295 mutex_unlock(&c_node->mem_lock);
1296 ippdrv->c_node = NULL;
1297 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001298}
1299
1300static int ipp_stop_property(struct drm_device *drm_dev,
1301 struct exynos_drm_ippdrv *ippdrv,
1302 struct drm_exynos_ipp_cmd_node *c_node)
1303{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001304 struct drm_exynos_ipp_property *property = &c_node->property;
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001305 int i;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001306
YoungJun Chocbc4c332013-06-12 10:44:40 +09001307 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001308
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001309 /* stop operations */
1310 if (ippdrv->stop)
1311 ippdrv->stop(ippdrv->dev, property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001312
1313 /* check command */
1314 switch (property->cmd) {
1315 case IPP_CMD_M2M:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001316 for_each_ipp_ops(i)
1317 ipp_clean_mem_nodes(drm_dev, c_node, i);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001318 break;
1319 case IPP_CMD_WB:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001320 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_DST);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001321 break;
1322 case IPP_CMD_OUTPUT:
Andrzej Hajdac4a856a2014-08-28 11:07:31 +02001323 ipp_clean_mem_nodes(drm_dev, c_node, EXYNOS_DRM_OPS_SRC);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001324 break;
1325 default:
1326 DRM_ERROR("invalid operations.\n");
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001327 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001328 }
1329
Andrzej Hajda8aa99dd2014-08-28 11:07:34 +02001330 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001331}
1332
1333void ipp_sched_cmd(struct work_struct *work)
1334{
1335 struct drm_exynos_ipp_cmd_work *cmd_work =
Andrzej Hajda05afb1a2014-08-28 11:07:33 +02001336 container_of(work, struct drm_exynos_ipp_cmd_work, work);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001337 struct exynos_drm_ippdrv *ippdrv;
1338 struct drm_exynos_ipp_cmd_node *c_node;
1339 struct drm_exynos_ipp_property *property;
1340 int ret;
1341
Eunchul Kimcb471f142012-12-14 18:10:31 +09001342 ippdrv = cmd_work->ippdrv;
1343 if (!ippdrv) {
1344 DRM_ERROR("invalid ippdrv list.\n");
1345 return;
1346 }
1347
1348 c_node = cmd_work->c_node;
1349 if (!c_node) {
1350 DRM_ERROR("invalid command node list.\n");
1351 return;
1352 }
1353
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001354 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001355
1356 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001357
1358 switch (cmd_work->ctrl) {
1359 case IPP_CTRL_PLAY:
1360 case IPP_CTRL_RESUME:
1361 ret = ipp_start_property(ippdrv, c_node);
1362 if (ret) {
1363 DRM_ERROR("failed to start property:prop_id[%d]\n",
1364 c_node->property.prop_id);
1365 goto err_unlock;
1366 }
1367
1368 /*
1369 * M2M case supports wait_completion of transfer.
1370 * because M2M case supports single unit operation
1371 * with multiple queue.
1372 * M2M need to wait completion of data transfer.
1373 */
1374 if (ipp_is_m2m_cmd(property->cmd)) {
1375 if (!wait_for_completion_timeout
1376 (&c_node->start_complete, msecs_to_jiffies(200))) {
1377 DRM_ERROR("timeout event:prop_id[%d]\n",
1378 c_node->property.prop_id);
1379 goto err_unlock;
1380 }
1381 }
1382 break;
1383 case IPP_CTRL_STOP:
1384 case IPP_CTRL_PAUSE:
1385 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1386 c_node);
1387 if (ret) {
1388 DRM_ERROR("failed to stop property.\n");
1389 goto err_unlock;
1390 }
1391
1392 complete(&c_node->stop_complete);
1393 break;
1394 default:
1395 DRM_ERROR("unknown control type\n");
1396 break;
1397 }
1398
YoungJun Chocbc4c332013-06-12 10:44:40 +09001399 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001400
1401err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001402 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001403}
1404
1405static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1406 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1407{
1408 struct drm_device *drm_dev = ippdrv->drm_dev;
1409 struct drm_exynos_ipp_property *property = &c_node->property;
1410 struct drm_exynos_ipp_mem_node *m_node;
1411 struct drm_exynos_ipp_queue_buf qbuf;
1412 struct drm_exynos_ipp_send_event *e;
1413 struct list_head *head;
1414 struct timeval now;
1415 unsigned long flags;
1416 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1417 int ret, i;
1418
1419 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001420 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001421
1422 if (!drm_dev) {
1423 DRM_ERROR("failed to get drm_dev.\n");
1424 return -EINVAL;
1425 }
1426
1427 if (!property) {
1428 DRM_ERROR("failed to get property.\n");
1429 return -EINVAL;
1430 }
1431
YoungJun Cho4d520762014-05-26 10:17:21 +02001432 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001433 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001434 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001435 ret = 0;
1436 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001437 }
1438
YoungJun Cho220db6f2014-05-26 10:17:20 +02001439 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001440 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001441 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001442 ret = 0;
1443 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001444 }
1445
1446 /* check command */
1447 switch (property->cmd) {
1448 case IPP_CMD_M2M:
1449 for_each_ipp_ops(i) {
1450 /* source/destination memory list */
1451 head = &c_node->mem_list[i];
1452
1453 m_node = list_first_entry(head,
1454 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001455
1456 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001457 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001458 i ? "dst" : "src", tbuf_id[i]);
1459
1460 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1461 if (ret)
1462 DRM_ERROR("failed to put m_node.\n");
1463 }
1464 break;
1465 case IPP_CMD_WB:
1466 /* clear buf for finding */
1467 memset(&qbuf, 0x0, sizeof(qbuf));
1468 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1469 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1470
1471 /* get memory node entry */
1472 m_node = ipp_find_mem_node(c_node, &qbuf);
1473 if (!m_node) {
1474 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001475 ret = -ENOMEM;
1476 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001477 }
1478
1479 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1480
1481 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1482 if (ret)
1483 DRM_ERROR("failed to put m_node.\n");
1484 break;
1485 case IPP_CMD_OUTPUT:
1486 /* source memory list */
1487 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1488
1489 m_node = list_first_entry(head,
1490 struct drm_exynos_ipp_mem_node, list);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001491
1492 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1493
1494 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1495 if (ret)
1496 DRM_ERROR("failed to put m_node.\n");
1497 break;
1498 default:
1499 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001500 ret = -EINVAL;
1501 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001502 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001503 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001504
1505 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1506 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1507 tbuf_id[1], buf_id[1], property->prop_id);
1508
1509 /*
1510 * command node have event list of destination buffer
1511 * If destination buffer enqueue to mem list,
1512 * then we make event and link to event list tail.
1513 * so, we get first event for first enqueued buffer.
1514 */
1515 e = list_first_entry(&c_node->event_list,
1516 struct drm_exynos_ipp_send_event, base.link);
1517
Eunchul Kimcb471f142012-12-14 18:10:31 +09001518 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001519 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001520 e->event.tv_sec = now.tv_sec;
1521 e->event.tv_usec = now.tv_usec;
1522 e->event.prop_id = property->prop_id;
1523
1524 /* set buffer id about source destination */
1525 for_each_ipp_ops(i)
1526 e->event.buf_id[i] = tbuf_id[i];
1527
1528 spin_lock_irqsave(&drm_dev->event_lock, flags);
1529 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1530 wake_up_interruptible(&e->base.file_priv->event_wait);
1531 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001532 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001533
YoungJun Chocbc4c332013-06-12 10:44:40 +09001534 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001535 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1536
1537 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001538
1539err_mem_unlock:
1540 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001541err_event_unlock:
1542 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001543 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001544}
1545
1546void ipp_sched_event(struct work_struct *work)
1547{
1548 struct drm_exynos_ipp_event_work *event_work =
Andrzej Hajda05afb1a2014-08-28 11:07:33 +02001549 container_of(work, struct drm_exynos_ipp_event_work, work);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001550 struct exynos_drm_ippdrv *ippdrv;
1551 struct drm_exynos_ipp_cmd_node *c_node;
1552 int ret;
1553
1554 if (!event_work) {
1555 DRM_ERROR("failed to get event_work.\n");
1556 return;
1557 }
1558
YoungJun Chocbc4c332013-06-12 10:44:40 +09001559 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001560
1561 ippdrv = event_work->ippdrv;
1562 if (!ippdrv) {
1563 DRM_ERROR("failed to get ipp driver.\n");
1564 return;
1565 }
1566
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001567 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001568 if (!c_node) {
1569 DRM_ERROR("failed to get command node.\n");
1570 return;
1571 }
1572
1573 /*
1574 * IPP supports command thread, event thread synchronization.
1575 * If IPP close immediately from user land, then IPP make
1576 * synchronization with command thread, so make complete event.
1577 * or going out operations.
1578 */
1579 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001580 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1581 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001582 goto err_completion;
1583 }
1584
Eunchul Kimcb471f142012-12-14 18:10:31 +09001585 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1586 if (ret) {
1587 DRM_ERROR("failed to send event.\n");
1588 goto err_completion;
1589 }
1590
1591err_completion:
1592 if (ipp_is_m2m_cmd(c_node->property.cmd))
1593 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001594}
1595
1596static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1597{
1598 struct ipp_context *ctx = get_ipp_context(dev);
1599 struct exynos_drm_ippdrv *ippdrv;
1600 int ret, count = 0;
1601
Eunchul Kimcb471f142012-12-14 18:10:31 +09001602 /* get ipp driver entry */
1603 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1604 ippdrv->drm_dev = drm_dev;
1605
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001606 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
1607 if (ret < 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001608 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001609 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001610 }
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001611 ippdrv->prop_list.ipp_id = ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001612
YoungJun Chocbc4c332013-06-12 10:44:40 +09001613 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajda12ff54d2014-07-03 15:10:36 +02001614 count++, (int)ippdrv, ret);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001615
1616 /* store parent device for node */
1617 ippdrv->parent_dev = dev;
1618
1619 /* store event work queue and handler */
1620 ippdrv->event_workq = ctx->event_workq;
1621 ippdrv->sched_event = ipp_sched_event;
1622 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001623 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001624
Joonyoung Shimbf566082015-07-02 21:49:38 +09001625 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1626 if (ret) {
1627 DRM_ERROR("failed to activate iommu\n");
1628 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001629 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001630 }
1631
1632 return 0;
1633
YoungJun Cho075436b2014-05-26 10:17:19 +02001634err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001635 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001636 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1637 drv_list) {
Joonyoung Shimbf566082015-07-02 21:49:38 +09001638 drm_iommu_detach_device(drm_dev, ippdrv->dev);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001639
YoungJun Cho075436b2014-05-26 10:17:19 +02001640 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1641 ippdrv->prop_list.ipp_id);
1642 }
1643
Eunchul Kimcb471f142012-12-14 18:10:31 +09001644 return ret;
1645}
1646
1647static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1648{
Andrzej Hajdaa36ed462014-09-09 15:16:05 +02001649 struct exynos_drm_ippdrv *ippdrv, *t;
YoungJun Cho075436b2014-05-26 10:17:19 +02001650 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001651
Eunchul Kimcb471f142012-12-14 18:10:31 +09001652 /* get ipp driver entry */
Andrzej Hajdaa36ed462014-09-09 15:16:05 +02001653 list_for_each_entry_safe(ippdrv, t, &exynos_drm_ippdrv_list, drv_list) {
Joonyoung Shimbf566082015-07-02 21:49:38 +09001654 drm_iommu_detach_device(drm_dev, ippdrv->dev);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001655
YoungJun Cho075436b2014-05-26 10:17:19 +02001656 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1657 ippdrv->prop_list.ipp_id);
1658
Eunchul Kimcb471f142012-12-14 18:10:31 +09001659 ippdrv->drm_dev = NULL;
1660 exynos_drm_ippdrv_unregister(ippdrv);
1661 }
1662}
1663
1664static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1665 struct drm_file *file)
1666{
1667 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001668
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001669 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001670
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001671 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001672
1673 return 0;
1674}
1675
1676static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1677 struct drm_file *file)
1678{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001679 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001680 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001681 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1682 int count = 0;
1683
Eunchul Kimcb471f142012-12-14 18:10:31 +09001684 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001685 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001686 list_for_each_entry_safe(c_node, tc_node,
1687 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001688 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1689 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001690
Andrzej Hajda21a825e2014-08-28 11:07:28 +02001691 if (c_node->filp == file) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001692 /*
1693 * userland goto unnormal state. process killed.
1694 * and close the file.
1695 * so, IPP didn't called stop cmd ctrl.
1696 * so, we are make stop operation in this state.
1697 */
1698 if (c_node->state == IPP_STATE_START) {
1699 ipp_stop_property(drm_dev, ippdrv,
1700 c_node);
1701 c_node->state = IPP_STATE_STOP;
1702 }
1703
1704 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001705 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001706 if (list_empty(&ippdrv->cmd_list))
1707 pm_runtime_put_sync(ippdrv->dev);
1708 }
1709 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001710 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001711 }
1712
Eunchul Kimcb471f142012-12-14 18:10:31 +09001713 return;
1714}
1715
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001716static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001717{
1718 struct device *dev = &pdev->dev;
1719 struct ipp_context *ctx;
1720 struct exynos_drm_subdrv *subdrv;
1721 int ret;
1722
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001723 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001724 if (!ctx)
1725 return -ENOMEM;
1726
Eunchul Kimcb471f142012-12-14 18:10:31 +09001727 mutex_init(&ctx->ipp_lock);
1728 mutex_init(&ctx->prop_lock);
1729
1730 idr_init(&ctx->ipp_idr);
1731 idr_init(&ctx->prop_idr);
1732
1733 /*
1734 * create single thread for ipp event
1735 * IPP supports event thread for IPP drivers.
1736 * IPP driver send event_work to this thread.
1737 * and IPP event thread send event to user process.
1738 */
1739 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1740 if (!ctx->event_workq) {
1741 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301742 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001743 }
1744
1745 /*
1746 * create single thread for ipp command
1747 * IPP supports command thread for user process.
1748 * user process make command node using set property ioctl.
1749 * and make start_work and send this work to command thread.
1750 * and then this command thread start property.
1751 */
1752 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1753 if (!ctx->cmd_workq) {
1754 dev_err(dev, "failed to create cmd workqueue\n");
1755 ret = -EINVAL;
1756 goto err_event_workq;
1757 }
1758
1759 /* set sub driver informations */
1760 subdrv = &ctx->subdrv;
1761 subdrv->dev = dev;
1762 subdrv->probe = ipp_subdrv_probe;
1763 subdrv->remove = ipp_subdrv_remove;
1764 subdrv->open = ipp_subdrv_open;
1765 subdrv->close = ipp_subdrv_close;
1766
1767 platform_set_drvdata(pdev, ctx);
1768
1769 ret = exynos_drm_subdrv_register(subdrv);
1770 if (ret < 0) {
1771 DRM_ERROR("failed to register drm ipp device.\n");
1772 goto err_cmd_workq;
1773 }
1774
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001775 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001776
1777 return 0;
1778
1779err_cmd_workq:
1780 destroy_workqueue(ctx->cmd_workq);
1781err_event_workq:
1782 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001783 return ret;
1784}
1785
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001786static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001787{
1788 struct ipp_context *ctx = platform_get_drvdata(pdev);
1789
Eunchul Kimcb471f142012-12-14 18:10:31 +09001790 /* unregister sub driver */
1791 exynos_drm_subdrv_unregister(&ctx->subdrv);
1792
1793 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001794 idr_destroy(&ctx->ipp_idr);
1795 idr_destroy(&ctx->prop_idr);
1796
1797 mutex_destroy(&ctx->ipp_lock);
1798 mutex_destroy(&ctx->prop_lock);
1799
1800 /* destroy command, event work queue */
1801 destroy_workqueue(ctx->cmd_workq);
1802 destroy_workqueue(ctx->event_workq);
1803
Eunchul Kimcb471f142012-12-14 18:10:31 +09001804 return 0;
1805}
1806
Eunchul Kimcb471f142012-12-14 18:10:31 +09001807struct platform_driver ipp_driver = {
1808 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001809 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001810 .driver = {
1811 .name = "exynos-drm-ipp",
1812 .owner = THIS_MODULE,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001813 },
1814};
1815