blob: f84ef8a905923cfa4f7ae452f70ef8eae138b834 [file] [log] [blame]
Eunchul Kimcb471f142012-12-14 18:10:31 +09001/*
2 * Copyright (C) 2012 Samsung Electronics Co.Ltd
3 * Authors:
4 * Eunchul Kim <chulspro.kim@samsung.com>
5 * Jinyoung Jeon <jy0.jeon@samsung.com>
6 * Sangmin Lee <lsmin.lee@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14#include <linux/kernel.h>
Eunchul Kimcb471f142012-12-14 18:10:31 +090015#include <linux/platform_device.h>
16#include <linux/types.h>
17#include <linux/clk.h>
18#include <linux/pm_runtime.h>
Eunchul Kimcb471f142012-12-14 18:10:31 +090019
20#include <drm/drmP.h>
21#include <drm/exynos_drm.h>
22#include "exynos_drm_drv.h"
23#include "exynos_drm_gem.h"
24#include "exynos_drm_ipp.h"
Eunchul Kimc12e2612012-12-14 17:58:54 +090025#include "exynos_drm_iommu.h"
Eunchul Kimcb471f142012-12-14 18:10:31 +090026
27/*
Eunchul Kim6fe891f2012-12-22 17:49:26 +090028 * IPP stands for Image Post Processing and
Eunchul Kimcb471f142012-12-14 18:10:31 +090029 * supports image scaler/rotator and input/output DMA operations.
30 * using FIMC, GSC, Rotator, so on.
31 * IPP is integration device driver of same attribute h/w
32 */
33
34/*
35 * TODO
36 * 1. expand command control id.
37 * 2. integrate property and config.
38 * 3. removed send_event id check routine.
39 * 4. compare send_event id if needed.
40 * 5. free subdrv_remove notifier callback list if needed.
41 * 6. need to check subdrv_open about multi-open.
42 * 7. need to power_on implement power and sysmmu ctrl.
43 */
44
45#define get_ipp_context(dev) platform_get_drvdata(to_platform_device(dev))
46#define ipp_is_m2m_cmd(c) (c == IPP_CMD_M2M)
47
Seung-Woo Kim43f41902013-04-23 14:02:53 +090048/* platform device pointer for ipp device. */
49static struct platform_device *exynos_drm_ipp_pdev;
50
Eunchul Kimcb471f142012-12-14 18:10:31 +090051/*
52 * A structure of event.
53 *
54 * @base: base of event.
55 * @event: ipp event.
56 */
57struct drm_exynos_ipp_send_event {
58 struct drm_pending_event base;
59 struct drm_exynos_ipp_event event;
60};
61
62/*
63 * A structure of memory node.
64 *
65 * @list: list head to memory queue information.
66 * @ops_id: id of operations.
67 * @prop_id: id of property.
68 * @buf_id: id of buffer.
69 * @buf_info: gem objects and dma address, size.
70 * @filp: a pointer to drm_file.
71 */
72struct drm_exynos_ipp_mem_node {
73 struct list_head list;
74 enum drm_exynos_ops_id ops_id;
75 u32 prop_id;
76 u32 buf_id;
77 struct drm_exynos_ipp_buf_info buf_info;
78 struct drm_file *filp;
79};
80
81/*
82 * A structure of ipp context.
83 *
84 * @subdrv: prepare initialization using subdrv.
85 * @ipp_lock: lock for synchronization of access to ipp_idr.
86 * @prop_lock: lock for synchronization of access to prop_idr.
87 * @ipp_idr: ipp driver idr.
88 * @prop_idr: property idr.
89 * @event_workq: event work queue.
90 * @cmd_workq: command work queue.
91 */
92struct ipp_context {
93 struct exynos_drm_subdrv subdrv;
94 struct mutex ipp_lock;
95 struct mutex prop_lock;
96 struct idr ipp_idr;
97 struct idr prop_idr;
98 struct workqueue_struct *event_workq;
99 struct workqueue_struct *cmd_workq;
100};
101
102static LIST_HEAD(exynos_drm_ippdrv_list);
103static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
104static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
105
Seung-Woo Kim43f41902013-04-23 14:02:53 +0900106int exynos_platform_device_ipp_register(void)
107{
108 struct platform_device *pdev;
109
110 if (exynos_drm_ipp_pdev)
111 return -EEXIST;
112
113 pdev = platform_device_register_simple("exynos-drm-ipp", -1, NULL, 0);
114 if (IS_ERR(pdev))
115 return PTR_ERR(pdev);
116
117 exynos_drm_ipp_pdev = pdev;
118
119 return 0;
120}
121
122void exynos_platform_device_ipp_unregister(void)
123{
124 if (exynos_drm_ipp_pdev) {
125 platform_device_unregister(exynos_drm_ipp_pdev);
126 exynos_drm_ipp_pdev = NULL;
127 }
128}
129
Eunchul Kimcb471f142012-12-14 18:10:31 +0900130int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
131{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900132 if (!ippdrv)
133 return -EINVAL;
134
135 mutex_lock(&exynos_drm_ippdrv_lock);
136 list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
137 mutex_unlock(&exynos_drm_ippdrv_lock);
138
139 return 0;
140}
141
142int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
143{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900144 if (!ippdrv)
145 return -EINVAL;
146
147 mutex_lock(&exynos_drm_ippdrv_lock);
148 list_del(&ippdrv->drv_list);
149 mutex_unlock(&exynos_drm_ippdrv_lock);
150
151 return 0;
152}
153
154static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj,
155 u32 *idp)
156{
157 int ret;
158
Eunchul Kimcb471f142012-12-14 18:10:31 +0900159 /* do the allocation under our mutexlock */
160 mutex_lock(lock);
Tejun Heo8550cb22013-02-27 17:04:09 -0800161 ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900162 mutex_unlock(lock);
Tejun Heo8550cb22013-02-27 17:04:09 -0800163 if (ret < 0)
164 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900165
Tejun Heo8550cb22013-02-27 17:04:09 -0800166 *idp = ret;
167 return 0;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900168}
169
YoungJun Cho075436b2014-05-26 10:17:19 +0200170static void ipp_remove_id(struct idr *id_idr, struct mutex *lock, u32 id)
171{
172 mutex_lock(lock);
173 idr_remove(id_idr, id);
174 mutex_unlock(lock);
175}
176
Eunchul Kimcb471f142012-12-14 18:10:31 +0900177static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
178{
179 void *obj;
180
YoungJun Chocbc4c332013-06-12 10:44:40 +0900181 DRM_DEBUG_KMS("id[%d]\n", id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900182
183 mutex_lock(lock);
184
185 /* find object using handle */
186 obj = idr_find(id_idr, id);
187 if (!obj) {
188 DRM_ERROR("failed to find object.\n");
189 mutex_unlock(lock);
190 return ERR_PTR(-ENODEV);
191 }
192
193 mutex_unlock(lock);
194
195 return obj;
196}
197
198static inline bool ipp_check_dedicated(struct exynos_drm_ippdrv *ippdrv,
199 enum drm_exynos_ipp_cmd cmd)
200{
201 /*
202 * check dedicated flag and WB, OUTPUT operation with
203 * power on state.
204 */
205 if (ippdrv->dedicated || (!ipp_is_m2m_cmd(cmd) &&
206 !pm_runtime_suspended(ippdrv->dev)))
207 return true;
208
209 return false;
210}
211
212static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
213 struct drm_exynos_ipp_property *property)
214{
215 struct exynos_drm_ippdrv *ippdrv;
216 u32 ipp_id = property->ipp_id;
217
YoungJun Chocbc4c332013-06-12 10:44:40 +0900218 DRM_DEBUG_KMS("ipp_id[%d]\n", ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900219
220 if (ipp_id) {
221 /* find ipp driver using idr */
222 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
223 ipp_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530224 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900225 DRM_ERROR("not found ipp%d driver.\n", ipp_id);
226 return ippdrv;
227 }
228
229 /*
230 * WB, OUTPUT opertion not supported multi-operation.
231 * so, make dedicated state at set property ioctl.
232 * when ipp driver finished operations, clear dedicated flags.
233 */
234 if (ipp_check_dedicated(ippdrv, property->cmd)) {
235 DRM_ERROR("already used choose device.\n");
236 return ERR_PTR(-EBUSY);
237 }
238
239 /*
240 * This is necessary to find correct device in ipp drivers.
241 * ipp drivers have different abilities,
242 * so need to check property.
243 */
244 if (ippdrv->check_property &&
245 ippdrv->check_property(ippdrv->dev, property)) {
246 DRM_ERROR("not support property.\n");
247 return ERR_PTR(-EINVAL);
248 }
249
250 return ippdrv;
251 } else {
252 /*
253 * This case is search all ipp driver for finding.
254 * user application don't set ipp_id in this case,
255 * so ipp subsystem search correct driver in driver list.
256 */
257 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
258 if (ipp_check_dedicated(ippdrv, property->cmd)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900259 DRM_DEBUG_KMS("used device.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900260 continue;
261 }
262
263 if (ippdrv->check_property &&
264 ippdrv->check_property(ippdrv->dev, property)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900265 DRM_DEBUG_KMS("not support property.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900266 continue;
267 }
268
269 return ippdrv;
270 }
271
272 DRM_ERROR("not support ipp driver operations.\n");
273 }
274
275 return ERR_PTR(-ENODEV);
276}
277
278static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
279{
280 struct exynos_drm_ippdrv *ippdrv;
281 struct drm_exynos_ipp_cmd_node *c_node;
282 int count = 0;
283
YoungJun Chocbc4c332013-06-12 10:44:40 +0900284 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900285
Eunchul Kimcb471f142012-12-14 18:10:31 +0900286 /*
287 * This case is search ipp driver by prop_id handle.
288 * sometimes, ipp subsystem find driver by prop_id.
Geert Uytterhoeven9fca9ac2014-03-11 11:23:37 +0100289 * e.g PAUSE state, queue buf, command control.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900290 */
291 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900292 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900293
YoungJun Cho7f5af052014-05-26 10:17:18 +0200294 mutex_lock(&ippdrv->cmd_lock);
295 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
296 if (c_node->property.prop_id == prop_id) {
297 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200298 return ippdrv;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200299 }
Eunchul Kimcb471f142012-12-14 18:10:31 +0900300 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200301 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900302 }
303
304 return ERR_PTR(-ENODEV);
305}
306
307int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
308 struct drm_file *file)
309{
310 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200311 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900312 struct ipp_context *ctx = get_ipp_context(dev);
313 struct drm_exynos_ipp_prop_list *prop_list = data;
314 struct exynos_drm_ippdrv *ippdrv;
315 int count = 0;
316
Eunchul Kimcb471f142012-12-14 18:10:31 +0900317 if (!ctx) {
318 DRM_ERROR("invalid context.\n");
319 return -EINVAL;
320 }
321
322 if (!prop_list) {
323 DRM_ERROR("invalid property parameter.\n");
324 return -EINVAL;
325 }
326
YoungJun Chocbc4c332013-06-12 10:44:40 +0900327 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900328
329 if (!prop_list->ipp_id) {
330 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
331 count++;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200332
Eunchul Kimcb471f142012-12-14 18:10:31 +0900333 /*
334 * Supports ippdrv list count for user application.
335 * First step user application getting ippdrv count.
336 * and second step getting ippdrv capability using ipp_id.
337 */
338 prop_list->count = count;
339 } else {
340 /*
341 * Getting ippdrv capability by ipp_id.
Masanari Iidac6b78bc2013-10-24 16:02:57 +0900342 * some device not supported wb, output interface.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900343 * so, user application detect correct ipp driver
344 * using this ioctl.
345 */
346 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
347 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800348 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900349 DRM_ERROR("not found ipp%d driver.\n",
350 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800351 return PTR_ERR(ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900352 }
353
Andrzej Hajda31646052014-05-19 12:54:05 +0200354 *prop_list = ippdrv->prop_list;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900355 }
356
357 return 0;
358}
359
360static void ipp_print_property(struct drm_exynos_ipp_property *property,
361 int idx)
362{
363 struct drm_exynos_ipp_config *config = &property->config[idx];
364 struct drm_exynos_pos *pos = &config->pos;
365 struct drm_exynos_sz *sz = &config->sz;
366
YoungJun Chocbc4c332013-06-12 10:44:40 +0900367 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
368 property->prop_id, idx ? "dst" : "src", config->fmt);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900369
YoungJun Chocbc4c332013-06-12 10:44:40 +0900370 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
371 pos->x, pos->y, pos->w, pos->h,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900372 sz->hsize, sz->vsize, config->flip, config->degree);
373}
374
375static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
376{
377 struct exynos_drm_ippdrv *ippdrv;
378 struct drm_exynos_ipp_cmd_node *c_node;
379 u32 prop_id = property->prop_id;
380
YoungJun Chocbc4c332013-06-12 10:44:40 +0900381 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900382
383 ippdrv = ipp_find_drv_by_handle(prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530384 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900385 DRM_ERROR("failed to get ipp driver.\n");
386 return -EINVAL;
387 }
388
389 /*
390 * Find command node using command list in ippdrv.
391 * when we find this command no using prop_id.
392 * return property information set in this command node.
393 */
YoungJun Cho7f5af052014-05-26 10:17:18 +0200394 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900395 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
396 if ((c_node->property.prop_id == prop_id) &&
397 (c_node->state == IPP_STATE_STOP)) {
YoungJun Cho7f5af052014-05-26 10:17:18 +0200398 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900399 DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
400 property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900401
402 c_node->property = *property;
403 return 0;
404 }
405 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200406 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900407
408 DRM_ERROR("failed to search property.\n");
409
410 return -EINVAL;
411}
412
413static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
414{
415 struct drm_exynos_ipp_cmd_work *cmd_work;
416
Eunchul Kimcb471f142012-12-14 18:10:31 +0900417 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900418 if (!cmd_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900419 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900420
421 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
422
423 return cmd_work;
424}
425
426static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
427{
428 struct drm_exynos_ipp_event_work *event_work;
429
Eunchul Kimcb471f142012-12-14 18:10:31 +0900430 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900431 if (!event_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900432 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900433
Andrzej Hajda60b61c22014-07-03 15:10:26 +0200434 INIT_WORK(&event_work->work, ipp_sched_event);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900435
436 return event_work;
437}
438
439int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
440 struct drm_file *file)
441{
442 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200443 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900444 struct ipp_context *ctx = get_ipp_context(dev);
445 struct drm_exynos_ipp_property *property = data;
446 struct exynos_drm_ippdrv *ippdrv;
447 struct drm_exynos_ipp_cmd_node *c_node;
448 int ret, i;
449
Eunchul Kimcb471f142012-12-14 18:10:31 +0900450 if (!ctx) {
451 DRM_ERROR("invalid context.\n");
452 return -EINVAL;
453 }
454
455 if (!property) {
456 DRM_ERROR("invalid property parameter.\n");
457 return -EINVAL;
458 }
459
460 /*
461 * This is log print for user application property.
462 * user application set various property.
463 */
464 for_each_ipp_ops(i)
465 ipp_print_property(property, i);
466
467 /*
468 * set property ioctl generated new prop_id.
469 * but in this case already asigned prop_id using old set property.
470 * e.g PAUSE state. this case supports find current prop_id and use it
471 * instead of allocation.
472 */
473 if (property->prop_id) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900474 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900475 return ipp_find_and_set_property(property);
476 }
477
478 /* find ipp driver using ipp id */
479 ippdrv = ipp_find_driver(ctx, property);
Sachin Kamatf0250452013-04-29 12:27:06 +0530480 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900481 DRM_ERROR("failed to get ipp driver.\n");
482 return -EINVAL;
483 }
484
485 /* allocate command node */
486 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900487 if (!c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900488 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900489
490 /* create property id */
491 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node,
492 &property->prop_id);
493 if (ret) {
494 DRM_ERROR("failed to create id.\n");
495 goto err_clear;
496 }
497
YoungJun Chocbc4c332013-06-12 10:44:40 +0900498 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
499 property->prop_id, property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900500
501 /* stored property information and ippdrv in private data */
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200502 c_node->dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900503 c_node->property = *property;
504 c_node->state = IPP_STATE_IDLE;
505
506 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530507 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900508 DRM_ERROR("failed to create start work.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +0200509 goto err_remove_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900510 }
511
512 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530513 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900514 DRM_ERROR("failed to create stop work.\n");
515 goto err_free_start;
516 }
517
518 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530519 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900520 DRM_ERROR("failed to create event work.\n");
521 goto err_free_stop;
522 }
523
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200524 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900525 mutex_init(&c_node->mem_lock);
526 mutex_init(&c_node->event_lock);
527
528 init_completion(&c_node->start_complete);
529 init_completion(&c_node->stop_complete);
530
531 for_each_ipp_ops(i)
532 INIT_LIST_HEAD(&c_node->mem_list[i]);
533
534 INIT_LIST_HEAD(&c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200535 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900536 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200537 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900538
539 /* make dedicated state without m2m */
540 if (!ipp_is_m2m_cmd(property->cmd))
541 ippdrv->dedicated = true;
542
543 return 0;
544
545err_free_stop:
546 kfree(c_node->stop_work);
547err_free_start:
548 kfree(c_node->start_work);
YoungJun Cho075436b2014-05-26 10:17:19 +0200549err_remove_id:
550 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900551err_clear:
552 kfree(c_node);
553 return ret;
554}
555
YoungJun Cho075436b2014-05-26 10:17:19 +0200556static void ipp_clean_cmd_node(struct ipp_context *ctx,
557 struct drm_exynos_ipp_cmd_node *c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900558{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900559 /* delete list */
560 list_del(&c_node->list);
561
YoungJun Cho075436b2014-05-26 10:17:19 +0200562 ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
563 c_node->property.prop_id);
564
Eunchul Kimcb471f142012-12-14 18:10:31 +0900565 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200566 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900567 mutex_destroy(&c_node->mem_lock);
568 mutex_destroy(&c_node->event_lock);
569
570 /* free command node */
571 kfree(c_node->start_work);
572 kfree(c_node->stop_work);
573 kfree(c_node->event_work);
574 kfree(c_node);
575}
576
577static int ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
578{
579 struct drm_exynos_ipp_property *property = &c_node->property;
580 struct drm_exynos_ipp_mem_node *m_node;
581 struct list_head *head;
582 int ret, i, count[EXYNOS_DRM_OPS_MAX] = { 0, };
583
Eunchul Kimcb471f142012-12-14 18:10:31 +0900584 for_each_ipp_ops(i) {
585 /* source/destination memory list */
586 head = &c_node->mem_list[i];
587
Eunchul Kimcb471f142012-12-14 18:10:31 +0900588 /* find memory node entry */
589 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900590 DRM_DEBUG_KMS("%s,count[%d]m_node[0x%x]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900591 i ? "dst" : "src", count[i], (int)m_node);
592 count[i]++;
593 }
594 }
595
YoungJun Chocbc4c332013-06-12 10:44:40 +0900596 DRM_DEBUG_KMS("min[%d]max[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900597 min(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]),
598 max(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]));
599
600 /*
601 * M2M operations should be need paired memory address.
602 * so, need to check minimum count about src, dst.
603 * other case not use paired memory, so use maximum count
604 */
605 if (ipp_is_m2m_cmd(property->cmd))
606 ret = min(count[EXYNOS_DRM_OPS_SRC],
607 count[EXYNOS_DRM_OPS_DST]);
608 else
609 ret = max(count[EXYNOS_DRM_OPS_SRC],
610 count[EXYNOS_DRM_OPS_DST]);
611
Eunchul Kimcb471f142012-12-14 18:10:31 +0900612 return ret;
613}
614
615static struct drm_exynos_ipp_mem_node
616 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
617 struct drm_exynos_ipp_queue_buf *qbuf)
618{
619 struct drm_exynos_ipp_mem_node *m_node;
620 struct list_head *head;
621 int count = 0;
622
YoungJun Chocbc4c332013-06-12 10:44:40 +0900623 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900624
625 /* source/destination memory list */
626 head = &c_node->mem_list[qbuf->ops_id];
627
628 /* find memory node from memory list */
629 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900630 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900631
632 /* compare buffer id */
633 if (m_node->buf_id == qbuf->buf_id)
634 return m_node;
635 }
636
637 return NULL;
638}
639
640static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
641 struct drm_exynos_ipp_cmd_node *c_node,
642 struct drm_exynos_ipp_mem_node *m_node)
643{
644 struct exynos_drm_ipp_ops *ops = NULL;
645 int ret = 0;
646
YoungJun Chocbc4c332013-06-12 10:44:40 +0900647 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900648
649 if (!m_node) {
650 DRM_ERROR("invalid queue node.\n");
651 return -EFAULT;
652 }
653
YoungJun Chocbc4c332013-06-12 10:44:40 +0900654 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900655
656 /* get operations callback */
657 ops = ippdrv->ops[m_node->ops_id];
658 if (!ops) {
659 DRM_ERROR("not support ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200660 return -EFAULT;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900661 }
662
663 /* set address and enable irq */
664 if (ops->set_addr) {
665 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
666 m_node->buf_id, IPP_BUF_ENQUEUE);
667 if (ret) {
668 DRM_ERROR("failed to set addr.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +0200669 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900670 }
671 }
672
Eunchul Kimcb471f142012-12-14 18:10:31 +0900673 return ret;
674}
675
676static struct drm_exynos_ipp_mem_node
677 *ipp_get_mem_node(struct drm_device *drm_dev,
678 struct drm_file *file,
679 struct drm_exynos_ipp_cmd_node *c_node,
680 struct drm_exynos_ipp_queue_buf *qbuf)
681{
682 struct drm_exynos_ipp_mem_node *m_node;
Andrzej Hajda73b00232014-07-03 15:10:30 +0200683 struct drm_exynos_ipp_buf_info *buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900684 int i;
685
Eunchul Kimcb471f142012-12-14 18:10:31 +0900686 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900687 if (!m_node)
YoungJun Cho220db6f2014-05-26 10:17:20 +0200688 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900689
Andrzej Hajda73b00232014-07-03 15:10:30 +0200690 buf_info = &m_node->buf_info;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900691
692 /* operations, buffer id */
693 m_node->ops_id = qbuf->ops_id;
694 m_node->prop_id = qbuf->prop_id;
695 m_node->buf_id = qbuf->buf_id;
696
YoungJun Chocbc4c332013-06-12 10:44:40 +0900697 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
698 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900699
700 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900701 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900702
703 /* get dma address by handle */
704 if (qbuf->handle[i]) {
Andrzej Hajdaa8ea17f2014-07-03 15:10:29 +0200705 dma_addr_t *addr;
706
Eunchul Kimcb471f142012-12-14 18:10:31 +0900707 addr = exynos_drm_gem_get_dma_addr(drm_dev,
708 qbuf->handle[i], file);
709 if (IS_ERR(addr)) {
710 DRM_ERROR("failed to get addr.\n");
711 goto err_clear;
712 }
713
Andrzej Hajda73b00232014-07-03 15:10:30 +0200714 buf_info->handles[i] = qbuf->handle[i];
715 buf_info->base[i] = *addr;
716 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
717 buf_info->base[i], buf_info->handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900718 }
719 }
720
721 m_node->filp = file;
YoungJun Cho220db6f2014-05-26 10:17:20 +0200722 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900723 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900724 mutex_unlock(&c_node->mem_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +0200725
Eunchul Kimcb471f142012-12-14 18:10:31 +0900726 return m_node;
727
728err_clear:
729 kfree(m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900730 return ERR_PTR(-EFAULT);
731}
732
733static int ipp_put_mem_node(struct drm_device *drm_dev,
734 struct drm_exynos_ipp_cmd_node *c_node,
735 struct drm_exynos_ipp_mem_node *m_node)
736{
737 int i;
738
YoungJun Chocbc4c332013-06-12 10:44:40 +0900739 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900740
741 if (!m_node) {
742 DRM_ERROR("invalid dequeue node.\n");
743 return -EFAULT;
744 }
745
YoungJun Chocbc4c332013-06-12 10:44:40 +0900746 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900747
748 /* put gem buffer */
749 for_each_ipp_planar(i) {
750 unsigned long handle = m_node->buf_info.handles[i];
751 if (handle)
752 exynos_drm_gem_put_dma_addr(drm_dev, handle,
753 m_node->filp);
754 }
755
756 /* delete list in queue */
757 list_del(&m_node->list);
758 kfree(m_node);
759
Eunchul Kimcb471f142012-12-14 18:10:31 +0900760 return 0;
761}
762
763static void ipp_free_event(struct drm_pending_event *event)
764{
765 kfree(event);
766}
767
768static int ipp_get_event(struct drm_device *drm_dev,
769 struct drm_file *file,
770 struct drm_exynos_ipp_cmd_node *c_node,
771 struct drm_exynos_ipp_queue_buf *qbuf)
772{
773 struct drm_exynos_ipp_send_event *e;
774 unsigned long flags;
775
YoungJun Chocbc4c332013-06-12 10:44:40 +0900776 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900777
778 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900779 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900780 spin_lock_irqsave(&drm_dev->event_lock, flags);
781 file->event_space += sizeof(e->event);
782 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
783 return -ENOMEM;
784 }
785
786 /* make event */
787 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
788 e->event.base.length = sizeof(e->event);
789 e->event.user_data = qbuf->user_data;
790 e->event.prop_id = qbuf->prop_id;
791 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
792 e->base.event = &e->event.base;
793 e->base.file_priv = file;
794 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200795 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900796 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200797 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900798
799 return 0;
800}
801
802static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
803 struct drm_exynos_ipp_queue_buf *qbuf)
804{
805 struct drm_exynos_ipp_send_event *e, *te;
806 int count = 0;
807
YoungJun Cho4d520762014-05-26 10:17:21 +0200808 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900809 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900810 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900811
812 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530813 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900814 * stop operations want to delete all event list.
815 * another case delete only same buf id.
816 */
817 if (!qbuf) {
818 /* delete list */
819 list_del(&e->base.link);
820 kfree(e);
821 }
822
823 /* compare buffer id */
824 if (qbuf && (qbuf->buf_id ==
825 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
826 /* delete list */
827 list_del(&e->base.link);
828 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200829 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900830 }
831 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200832
833out_unlock:
834 mutex_unlock(&c_node->event_lock);
835 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900836}
837
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530838static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900839 struct exynos_drm_ippdrv *ippdrv,
840 struct drm_exynos_ipp_cmd_work *cmd_work,
841 struct drm_exynos_ipp_cmd_node *c_node)
842{
843 struct ipp_context *ctx = get_ipp_context(dev);
844
845 cmd_work->ippdrv = ippdrv;
846 cmd_work->c_node = c_node;
847 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
848}
849
850static int ipp_queue_buf_with_run(struct device *dev,
851 struct drm_exynos_ipp_cmd_node *c_node,
852 struct drm_exynos_ipp_mem_node *m_node,
853 struct drm_exynos_ipp_queue_buf *qbuf)
854{
855 struct exynos_drm_ippdrv *ippdrv;
856 struct drm_exynos_ipp_property *property;
857 struct exynos_drm_ipp_ops *ops;
858 int ret;
859
Eunchul Kimcb471f142012-12-14 18:10:31 +0900860 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530861 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900862 DRM_ERROR("failed to get ipp driver.\n");
863 return -EFAULT;
864 }
865
866 ops = ippdrv->ops[qbuf->ops_id];
867 if (!ops) {
868 DRM_ERROR("failed to get ops.\n");
869 return -EFAULT;
870 }
871
872 property = &c_node->property;
873
874 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900875 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900876 return 0;
877 }
878
YoungJun Cho220db6f2014-05-26 10:17:20 +0200879 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900880 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200881 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900882 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900883 return 0;
884 }
885
886 /*
887 * If set destination buffer and enabled clock,
888 * then m2m operations need start operations at queue_buf
889 */
890 if (ipp_is_m2m_cmd(property->cmd)) {
891 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
892
893 cmd_work->ctrl = IPP_CTRL_PLAY;
894 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
895 } else {
896 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
897 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200898 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900899 DRM_ERROR("failed to set m node.\n");
900 return ret;
901 }
902 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200903 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900904
905 return 0;
906}
907
908static void ipp_clean_queue_buf(struct drm_device *drm_dev,
909 struct drm_exynos_ipp_cmd_node *c_node,
910 struct drm_exynos_ipp_queue_buf *qbuf)
911{
912 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
913
YoungJun Choc66ce402014-05-26 10:17:15 +0200914 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200915 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200916 list_for_each_entry_safe(m_node, tm_node,
917 &c_node->mem_list[qbuf->ops_id], list) {
918 if (m_node->buf_id == qbuf->buf_id &&
919 m_node->ops_id == qbuf->ops_id)
920 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900921 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200922 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900923}
924
925int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
926 struct drm_file *file)
927{
928 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200929 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900930 struct ipp_context *ctx = get_ipp_context(dev);
931 struct drm_exynos_ipp_queue_buf *qbuf = data;
932 struct drm_exynos_ipp_cmd_node *c_node;
933 struct drm_exynos_ipp_mem_node *m_node;
934 int ret;
935
Eunchul Kimcb471f142012-12-14 18:10:31 +0900936 if (!qbuf) {
937 DRM_ERROR("invalid buf parameter.\n");
938 return -EINVAL;
939 }
940
941 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
942 DRM_ERROR("invalid ops parameter.\n");
943 return -EINVAL;
944 }
945
YoungJun Chocbc4c332013-06-12 10:44:40 +0900946 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
947 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900948 qbuf->buf_id, qbuf->buf_type);
949
950 /* find command node */
951 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
952 qbuf->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800953 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900954 DRM_ERROR("failed to get command node.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +0800955 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900956 }
957
958 /* buffer control */
959 switch (qbuf->buf_type) {
960 case IPP_BUF_ENQUEUE:
961 /* get memory node */
962 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
963 if (IS_ERR(m_node)) {
964 DRM_ERROR("failed to get m_node.\n");
965 return PTR_ERR(m_node);
966 }
967
968 /*
969 * first step get event for destination buffer.
970 * and second step when M2M case run with destination buffer
971 * if needed.
972 */
973 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
974 /* get event for destination buffer */
975 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
976 if (ret) {
977 DRM_ERROR("failed to get event.\n");
978 goto err_clean_node;
979 }
980
981 /*
982 * M2M case run play control for streaming feature.
983 * other case set address and waiting.
984 */
985 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
986 if (ret) {
987 DRM_ERROR("failed to run command.\n");
988 goto err_clean_node;
989 }
990 }
991 break;
992 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200993 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900994
995 /* put event for destination buffer */
996 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
997 ipp_put_event(c_node, qbuf);
998
999 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1000
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001001 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001002 break;
1003 default:
1004 DRM_ERROR("invalid buffer control.\n");
1005 return -EINVAL;
1006 }
1007
1008 return 0;
1009
1010err_clean_node:
1011 DRM_ERROR("clean memory nodes.\n");
1012
1013 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1014 return ret;
1015}
1016
1017static bool exynos_drm_ipp_check_valid(struct device *dev,
1018 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
1019{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001020 if (ctrl != IPP_CTRL_PLAY) {
1021 if (pm_runtime_suspended(dev)) {
1022 DRM_ERROR("pm:runtime_suspended.\n");
1023 goto err_status;
1024 }
1025 }
1026
1027 switch (ctrl) {
1028 case IPP_CTRL_PLAY:
1029 if (state != IPP_STATE_IDLE)
1030 goto err_status;
1031 break;
1032 case IPP_CTRL_STOP:
1033 if (state == IPP_STATE_STOP)
1034 goto err_status;
1035 break;
1036 case IPP_CTRL_PAUSE:
1037 if (state != IPP_STATE_START)
1038 goto err_status;
1039 break;
1040 case IPP_CTRL_RESUME:
1041 if (state != IPP_STATE_STOP)
1042 goto err_status;
1043 break;
1044 default:
1045 DRM_ERROR("invalid state.\n");
1046 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001047 }
1048
1049 return true;
1050
1051err_status:
1052 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1053 return false;
1054}
1055
1056int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1057 struct drm_file *file)
1058{
1059 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001060 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001061 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001062 struct ipp_context *ctx = get_ipp_context(dev);
1063 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1064 struct drm_exynos_ipp_cmd_work *cmd_work;
1065 struct drm_exynos_ipp_cmd_node *c_node;
1066
Eunchul Kimcb471f142012-12-14 18:10:31 +09001067 if (!ctx) {
1068 DRM_ERROR("invalid context.\n");
1069 return -EINVAL;
1070 }
1071
1072 if (!cmd_ctrl) {
1073 DRM_ERROR("invalid control parameter.\n");
1074 return -EINVAL;
1075 }
1076
YoungJun Chocbc4c332013-06-12 10:44:40 +09001077 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001078 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1079
1080 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1081 if (IS_ERR(ippdrv)) {
1082 DRM_ERROR("failed to get ipp driver.\n");
1083 return PTR_ERR(ippdrv);
1084 }
1085
1086 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1087 cmd_ctrl->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +08001088 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001089 DRM_ERROR("invalid command node list.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +08001090 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001091 }
1092
1093 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1094 c_node->state)) {
1095 DRM_ERROR("invalid state.\n");
1096 return -EINVAL;
1097 }
1098
1099 switch (cmd_ctrl->ctrl) {
1100 case IPP_CTRL_PLAY:
1101 if (pm_runtime_suspended(ippdrv->dev))
1102 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001103
Eunchul Kimcb471f142012-12-14 18:10:31 +09001104 c_node->state = IPP_STATE_START;
1105
1106 cmd_work = c_node->start_work;
1107 cmd_work->ctrl = cmd_ctrl->ctrl;
1108 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001109 break;
1110 case IPP_CTRL_STOP:
1111 cmd_work = c_node->stop_work;
1112 cmd_work->ctrl = cmd_ctrl->ctrl;
1113 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1114
1115 if (!wait_for_completion_timeout(&c_node->stop_complete,
1116 msecs_to_jiffies(300))) {
1117 DRM_ERROR("timeout stop:prop_id[%d]\n",
1118 c_node->property.prop_id);
1119 }
1120
1121 c_node->state = IPP_STATE_STOP;
1122 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001123 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001124 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001125
1126 if (list_empty(&ippdrv->cmd_list))
1127 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001128 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001129 break;
1130 case IPP_CTRL_PAUSE:
1131 cmd_work = c_node->stop_work;
1132 cmd_work->ctrl = cmd_ctrl->ctrl;
1133 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1134
1135 if (!wait_for_completion_timeout(&c_node->stop_complete,
1136 msecs_to_jiffies(200))) {
1137 DRM_ERROR("timeout stop:prop_id[%d]\n",
1138 c_node->property.prop_id);
1139 }
1140
1141 c_node->state = IPP_STATE_STOP;
1142 break;
1143 case IPP_CTRL_RESUME:
1144 c_node->state = IPP_STATE_START;
1145 cmd_work = c_node->start_work;
1146 cmd_work->ctrl = cmd_ctrl->ctrl;
1147 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1148 break;
1149 default:
1150 DRM_ERROR("could not support this state currently.\n");
1151 return -EINVAL;
1152 }
1153
YoungJun Chocbc4c332013-06-12 10:44:40 +09001154 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001155 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1156
1157 return 0;
1158}
1159
1160int exynos_drm_ippnb_register(struct notifier_block *nb)
1161{
1162 return blocking_notifier_chain_register(
1163 &exynos_drm_ippnb_list, nb);
1164}
1165
1166int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1167{
1168 return blocking_notifier_chain_unregister(
1169 &exynos_drm_ippnb_list, nb);
1170}
1171
1172int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1173{
1174 return blocking_notifier_call_chain(
1175 &exynos_drm_ippnb_list, val, v);
1176}
1177
1178static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1179 struct drm_exynos_ipp_property *property)
1180{
1181 struct exynos_drm_ipp_ops *ops = NULL;
1182 bool swap = false;
1183 int ret, i;
1184
1185 if (!property) {
1186 DRM_ERROR("invalid property parameter.\n");
1187 return -EINVAL;
1188 }
1189
YoungJun Chocbc4c332013-06-12 10:44:40 +09001190 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001191
1192 /* reset h/w block */
1193 if (ippdrv->reset &&
1194 ippdrv->reset(ippdrv->dev)) {
1195 DRM_ERROR("failed to reset.\n");
1196 return -EINVAL;
1197 }
1198
1199 /* set source,destination operations */
1200 for_each_ipp_ops(i) {
1201 struct drm_exynos_ipp_config *config =
1202 &property->config[i];
1203
1204 ops = ippdrv->ops[i];
1205 if (!ops || !config) {
1206 DRM_ERROR("not support ops and config.\n");
1207 return -EINVAL;
1208 }
1209
1210 /* set format */
1211 if (ops->set_fmt) {
1212 ret = ops->set_fmt(ippdrv->dev, config->fmt);
1213 if (ret) {
1214 DRM_ERROR("not support format.\n");
1215 return ret;
1216 }
1217 }
1218
1219 /* set transform for rotation, flip */
1220 if (ops->set_transf) {
1221 ret = ops->set_transf(ippdrv->dev, config->degree,
1222 config->flip, &swap);
1223 if (ret) {
1224 DRM_ERROR("not support tranf.\n");
1225 return -EINVAL;
1226 }
1227 }
1228
1229 /* set size */
1230 if (ops->set_size) {
1231 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1232 &config->sz);
1233 if (ret) {
1234 DRM_ERROR("not support size.\n");
1235 return ret;
1236 }
1237 }
1238 }
1239
1240 return 0;
1241}
1242
1243static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1244 struct drm_exynos_ipp_cmd_node *c_node)
1245{
1246 struct drm_exynos_ipp_mem_node *m_node;
1247 struct drm_exynos_ipp_property *property = &c_node->property;
1248 struct list_head *head;
1249 int ret, i;
1250
YoungJun Chocbc4c332013-06-12 10:44:40 +09001251 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001252
1253 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001254 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001255
YoungJun Cho220db6f2014-05-26 10:17:20 +02001256 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001257 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001258 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001259 ret = -ENOMEM;
1260 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001261 }
1262
1263 /* set current property in ippdrv */
1264 ret = ipp_set_property(ippdrv, property);
1265 if (ret) {
1266 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001267 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001268 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001269 }
1270
1271 /* check command */
1272 switch (property->cmd) {
1273 case IPP_CMD_M2M:
1274 for_each_ipp_ops(i) {
1275 /* source/destination memory list */
1276 head = &c_node->mem_list[i];
1277
1278 m_node = list_first_entry(head,
1279 struct drm_exynos_ipp_mem_node, list);
1280 if (!m_node) {
1281 DRM_ERROR("failed to get node.\n");
1282 ret = -EFAULT;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001283 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001284 }
1285
YoungJun Chocbc4c332013-06-12 10:44:40 +09001286 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001287
1288 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1289 if (ret) {
1290 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001291 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001292 }
1293 }
1294 break;
1295 case IPP_CMD_WB:
1296 /* destination memory list */
1297 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1298
1299 list_for_each_entry(m_node, head, list) {
1300 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1301 if (ret) {
1302 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001303 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001304 }
1305 }
1306 break;
1307 case IPP_CMD_OUTPUT:
1308 /* source memory list */
1309 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1310
1311 list_for_each_entry(m_node, head, list) {
1312 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1313 if (ret) {
1314 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001315 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001316 }
1317 }
1318 break;
1319 default:
1320 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001321 ret = -EINVAL;
1322 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001323 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001324 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001325
YoungJun Chocbc4c332013-06-12 10:44:40 +09001326 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001327
1328 /* start operations */
1329 if (ippdrv->start) {
1330 ret = ippdrv->start(ippdrv->dev, property->cmd);
1331 if (ret) {
1332 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001333 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001334 return ret;
1335 }
1336 }
1337
1338 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001339
1340err_unlock:
1341 mutex_unlock(&c_node->mem_lock);
1342 ippdrv->c_node = NULL;
1343 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001344}
1345
1346static int ipp_stop_property(struct drm_device *drm_dev,
1347 struct exynos_drm_ippdrv *ippdrv,
1348 struct drm_exynos_ipp_cmd_node *c_node)
1349{
1350 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1351 struct drm_exynos_ipp_property *property = &c_node->property;
1352 struct list_head *head;
1353 int ret = 0, i;
1354
YoungJun Chocbc4c332013-06-12 10:44:40 +09001355 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001356
1357 /* put event */
1358 ipp_put_event(c_node, NULL);
1359
YoungJun Cho220db6f2014-05-26 10:17:20 +02001360 mutex_lock(&c_node->mem_lock);
1361
Eunchul Kimcb471f142012-12-14 18:10:31 +09001362 /* check command */
1363 switch (property->cmd) {
1364 case IPP_CMD_M2M:
1365 for_each_ipp_ops(i) {
1366 /* source/destination memory list */
1367 head = &c_node->mem_list[i];
1368
Eunchul Kimcb471f142012-12-14 18:10:31 +09001369 list_for_each_entry_safe(m_node, tm_node,
1370 head, list) {
1371 ret = ipp_put_mem_node(drm_dev, c_node,
1372 m_node);
1373 if (ret) {
1374 DRM_ERROR("failed to put m_node.\n");
1375 goto err_clear;
1376 }
1377 }
1378 }
1379 break;
1380 case IPP_CMD_WB:
1381 /* destination memory list */
1382 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1383
Eunchul Kimcb471f142012-12-14 18:10:31 +09001384 list_for_each_entry_safe(m_node, tm_node, head, list) {
1385 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1386 if (ret) {
1387 DRM_ERROR("failed to put m_node.\n");
1388 goto err_clear;
1389 }
1390 }
1391 break;
1392 case IPP_CMD_OUTPUT:
1393 /* source memory list */
1394 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1395
Eunchul Kimcb471f142012-12-14 18:10:31 +09001396 list_for_each_entry_safe(m_node, tm_node, head, list) {
1397 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1398 if (ret) {
1399 DRM_ERROR("failed to put m_node.\n");
1400 goto err_clear;
1401 }
1402 }
1403 break;
1404 default:
1405 DRM_ERROR("invalid operations.\n");
1406 ret = -EINVAL;
1407 goto err_clear;
1408 }
1409
1410err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001411 mutex_unlock(&c_node->mem_lock);
1412
Eunchul Kimcb471f142012-12-14 18:10:31 +09001413 /* stop operations */
1414 if (ippdrv->stop)
1415 ippdrv->stop(ippdrv->dev, property->cmd);
1416
1417 return ret;
1418}
1419
1420void ipp_sched_cmd(struct work_struct *work)
1421{
1422 struct drm_exynos_ipp_cmd_work *cmd_work =
1423 (struct drm_exynos_ipp_cmd_work *)work;
1424 struct exynos_drm_ippdrv *ippdrv;
1425 struct drm_exynos_ipp_cmd_node *c_node;
1426 struct drm_exynos_ipp_property *property;
1427 int ret;
1428
Eunchul Kimcb471f142012-12-14 18:10:31 +09001429 ippdrv = cmd_work->ippdrv;
1430 if (!ippdrv) {
1431 DRM_ERROR("invalid ippdrv list.\n");
1432 return;
1433 }
1434
1435 c_node = cmd_work->c_node;
1436 if (!c_node) {
1437 DRM_ERROR("invalid command node list.\n");
1438 return;
1439 }
1440
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001441 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001442
1443 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001444
1445 switch (cmd_work->ctrl) {
1446 case IPP_CTRL_PLAY:
1447 case IPP_CTRL_RESUME:
1448 ret = ipp_start_property(ippdrv, c_node);
1449 if (ret) {
1450 DRM_ERROR("failed to start property:prop_id[%d]\n",
1451 c_node->property.prop_id);
1452 goto err_unlock;
1453 }
1454
1455 /*
1456 * M2M case supports wait_completion of transfer.
1457 * because M2M case supports single unit operation
1458 * with multiple queue.
1459 * M2M need to wait completion of data transfer.
1460 */
1461 if (ipp_is_m2m_cmd(property->cmd)) {
1462 if (!wait_for_completion_timeout
1463 (&c_node->start_complete, msecs_to_jiffies(200))) {
1464 DRM_ERROR("timeout event:prop_id[%d]\n",
1465 c_node->property.prop_id);
1466 goto err_unlock;
1467 }
1468 }
1469 break;
1470 case IPP_CTRL_STOP:
1471 case IPP_CTRL_PAUSE:
1472 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1473 c_node);
1474 if (ret) {
1475 DRM_ERROR("failed to stop property.\n");
1476 goto err_unlock;
1477 }
1478
1479 complete(&c_node->stop_complete);
1480 break;
1481 default:
1482 DRM_ERROR("unknown control type\n");
1483 break;
1484 }
1485
YoungJun Chocbc4c332013-06-12 10:44:40 +09001486 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001487
1488err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001489 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001490}
1491
1492static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1493 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1494{
1495 struct drm_device *drm_dev = ippdrv->drm_dev;
1496 struct drm_exynos_ipp_property *property = &c_node->property;
1497 struct drm_exynos_ipp_mem_node *m_node;
1498 struct drm_exynos_ipp_queue_buf qbuf;
1499 struct drm_exynos_ipp_send_event *e;
1500 struct list_head *head;
1501 struct timeval now;
1502 unsigned long flags;
1503 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1504 int ret, i;
1505
1506 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001507 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001508
1509 if (!drm_dev) {
1510 DRM_ERROR("failed to get drm_dev.\n");
1511 return -EINVAL;
1512 }
1513
1514 if (!property) {
1515 DRM_ERROR("failed to get property.\n");
1516 return -EINVAL;
1517 }
1518
YoungJun Cho4d520762014-05-26 10:17:21 +02001519 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001520 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001521 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001522 ret = 0;
1523 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001524 }
1525
YoungJun Cho220db6f2014-05-26 10:17:20 +02001526 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001527 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001528 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001529 ret = 0;
1530 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001531 }
1532
1533 /* check command */
1534 switch (property->cmd) {
1535 case IPP_CMD_M2M:
1536 for_each_ipp_ops(i) {
1537 /* source/destination memory list */
1538 head = &c_node->mem_list[i];
1539
1540 m_node = list_first_entry(head,
1541 struct drm_exynos_ipp_mem_node, list);
1542 if (!m_node) {
1543 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001544 ret = -ENOMEM;
1545 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001546 }
1547
1548 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001549 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001550 i ? "dst" : "src", tbuf_id[i]);
1551
1552 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1553 if (ret)
1554 DRM_ERROR("failed to put m_node.\n");
1555 }
1556 break;
1557 case IPP_CMD_WB:
1558 /* clear buf for finding */
1559 memset(&qbuf, 0x0, sizeof(qbuf));
1560 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1561 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1562
1563 /* get memory node entry */
1564 m_node = ipp_find_mem_node(c_node, &qbuf);
1565 if (!m_node) {
1566 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001567 ret = -ENOMEM;
1568 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001569 }
1570
1571 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1572
1573 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1574 if (ret)
1575 DRM_ERROR("failed to put m_node.\n");
1576 break;
1577 case IPP_CMD_OUTPUT:
1578 /* source memory list */
1579 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1580
1581 m_node = list_first_entry(head,
1582 struct drm_exynos_ipp_mem_node, list);
1583 if (!m_node) {
1584 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001585 ret = -ENOMEM;
1586 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001587 }
1588
1589 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1590
1591 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1592 if (ret)
1593 DRM_ERROR("failed to put m_node.\n");
1594 break;
1595 default:
1596 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001597 ret = -EINVAL;
1598 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001599 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001600 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001601
1602 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1603 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1604 tbuf_id[1], buf_id[1], property->prop_id);
1605
1606 /*
1607 * command node have event list of destination buffer
1608 * If destination buffer enqueue to mem list,
1609 * then we make event and link to event list tail.
1610 * so, we get first event for first enqueued buffer.
1611 */
1612 e = list_first_entry(&c_node->event_list,
1613 struct drm_exynos_ipp_send_event, base.link);
1614
Eunchul Kimcb471f142012-12-14 18:10:31 +09001615 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001616 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001617 e->event.tv_sec = now.tv_sec;
1618 e->event.tv_usec = now.tv_usec;
1619 e->event.prop_id = property->prop_id;
1620
1621 /* set buffer id about source destination */
1622 for_each_ipp_ops(i)
1623 e->event.buf_id[i] = tbuf_id[i];
1624
1625 spin_lock_irqsave(&drm_dev->event_lock, flags);
1626 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1627 wake_up_interruptible(&e->base.file_priv->event_wait);
1628 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001629 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001630
YoungJun Chocbc4c332013-06-12 10:44:40 +09001631 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001632 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1633
1634 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001635
1636err_mem_unlock:
1637 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001638err_event_unlock:
1639 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001640 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001641}
1642
1643void ipp_sched_event(struct work_struct *work)
1644{
1645 struct drm_exynos_ipp_event_work *event_work =
1646 (struct drm_exynos_ipp_event_work *)work;
1647 struct exynos_drm_ippdrv *ippdrv;
1648 struct drm_exynos_ipp_cmd_node *c_node;
1649 int ret;
1650
1651 if (!event_work) {
1652 DRM_ERROR("failed to get event_work.\n");
1653 return;
1654 }
1655
YoungJun Chocbc4c332013-06-12 10:44:40 +09001656 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001657
1658 ippdrv = event_work->ippdrv;
1659 if (!ippdrv) {
1660 DRM_ERROR("failed to get ipp driver.\n");
1661 return;
1662 }
1663
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001664 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001665 if (!c_node) {
1666 DRM_ERROR("failed to get command node.\n");
1667 return;
1668 }
1669
1670 /*
1671 * IPP supports command thread, event thread synchronization.
1672 * If IPP close immediately from user land, then IPP make
1673 * synchronization with command thread, so make complete event.
1674 * or going out operations.
1675 */
1676 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001677 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1678 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001679 goto err_completion;
1680 }
1681
Eunchul Kimcb471f142012-12-14 18:10:31 +09001682 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1683 if (ret) {
1684 DRM_ERROR("failed to send event.\n");
1685 goto err_completion;
1686 }
1687
1688err_completion:
1689 if (ipp_is_m2m_cmd(c_node->property.cmd))
1690 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001691}
1692
1693static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1694{
1695 struct ipp_context *ctx = get_ipp_context(dev);
1696 struct exynos_drm_ippdrv *ippdrv;
1697 int ret, count = 0;
1698
Eunchul Kimcb471f142012-12-14 18:10:31 +09001699 /* get ipp driver entry */
1700 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001701 u32 ipp_id;
1702
Eunchul Kimcb471f142012-12-14 18:10:31 +09001703 ippdrv->drm_dev = drm_dev;
1704
1705 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001706 &ipp_id);
1707 if (ret || ipp_id == 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001708 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001709 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001710 }
1711
YoungJun Chocbc4c332013-06-12 10:44:40 +09001712 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001713 count++, (int)ippdrv, ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001714
Andrzej Hajda31646052014-05-19 12:54:05 +02001715 ippdrv->prop_list.ipp_id = ipp_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001716
1717 /* store parent device for node */
1718 ippdrv->parent_dev = dev;
1719
1720 /* store event work queue and handler */
1721 ippdrv->event_workq = ctx->event_workq;
1722 ippdrv->sched_event = ipp_sched_event;
1723 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001724 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001725
1726 if (is_drm_iommu_supported(drm_dev)) {
1727 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1728 if (ret) {
1729 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001730 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001731 }
1732 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001733 }
1734
1735 return 0;
1736
YoungJun Cho075436b2014-05-26 10:17:19 +02001737err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001738 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001739 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1740 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001741 if (is_drm_iommu_supported(drm_dev))
1742 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1743
YoungJun Cho075436b2014-05-26 10:17:19 +02001744 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1745 ippdrv->prop_list.ipp_id);
1746 }
1747
Eunchul Kimcb471f142012-12-14 18:10:31 +09001748 return ret;
1749}
1750
1751static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1752{
1753 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001754 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001755
Eunchul Kimcb471f142012-12-14 18:10:31 +09001756 /* get ipp driver entry */
1757 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001758 if (is_drm_iommu_supported(drm_dev))
1759 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1760
YoungJun Cho075436b2014-05-26 10:17:19 +02001761 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1762 ippdrv->prop_list.ipp_id);
1763
Eunchul Kimcb471f142012-12-14 18:10:31 +09001764 ippdrv->drm_dev = NULL;
1765 exynos_drm_ippdrv_unregister(ippdrv);
1766 }
1767}
1768
1769static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1770 struct drm_file *file)
1771{
1772 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001773
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001774 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001775
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001776 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001777
1778 return 0;
1779}
1780
1781static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1782 struct drm_file *file)
1783{
1784 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001785 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001786 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001787 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1788 int count = 0;
1789
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001790 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)file_priv->ipp_dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001791
Eunchul Kimcb471f142012-12-14 18:10:31 +09001792 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001793 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001794 list_for_each_entry_safe(c_node, tc_node,
1795 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001796 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1797 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001798
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001799 if (c_node->dev == file_priv->ipp_dev) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001800 /*
1801 * userland goto unnormal state. process killed.
1802 * and close the file.
1803 * so, IPP didn't called stop cmd ctrl.
1804 * so, we are make stop operation in this state.
1805 */
1806 if (c_node->state == IPP_STATE_START) {
1807 ipp_stop_property(drm_dev, ippdrv,
1808 c_node);
1809 c_node->state = IPP_STATE_STOP;
1810 }
1811
1812 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001813 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001814 if (list_empty(&ippdrv->cmd_list))
1815 pm_runtime_put_sync(ippdrv->dev);
1816 }
1817 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001818 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001819 }
1820
Eunchul Kimcb471f142012-12-14 18:10:31 +09001821 return;
1822}
1823
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001824static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001825{
1826 struct device *dev = &pdev->dev;
1827 struct ipp_context *ctx;
1828 struct exynos_drm_subdrv *subdrv;
1829 int ret;
1830
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001831 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001832 if (!ctx)
1833 return -ENOMEM;
1834
Eunchul Kimcb471f142012-12-14 18:10:31 +09001835 mutex_init(&ctx->ipp_lock);
1836 mutex_init(&ctx->prop_lock);
1837
1838 idr_init(&ctx->ipp_idr);
1839 idr_init(&ctx->prop_idr);
1840
1841 /*
1842 * create single thread for ipp event
1843 * IPP supports event thread for IPP drivers.
1844 * IPP driver send event_work to this thread.
1845 * and IPP event thread send event to user process.
1846 */
1847 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1848 if (!ctx->event_workq) {
1849 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301850 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001851 }
1852
1853 /*
1854 * create single thread for ipp command
1855 * IPP supports command thread for user process.
1856 * user process make command node using set property ioctl.
1857 * and make start_work and send this work to command thread.
1858 * and then this command thread start property.
1859 */
1860 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1861 if (!ctx->cmd_workq) {
1862 dev_err(dev, "failed to create cmd workqueue\n");
1863 ret = -EINVAL;
1864 goto err_event_workq;
1865 }
1866
1867 /* set sub driver informations */
1868 subdrv = &ctx->subdrv;
1869 subdrv->dev = dev;
1870 subdrv->probe = ipp_subdrv_probe;
1871 subdrv->remove = ipp_subdrv_remove;
1872 subdrv->open = ipp_subdrv_open;
1873 subdrv->close = ipp_subdrv_close;
1874
1875 platform_set_drvdata(pdev, ctx);
1876
1877 ret = exynos_drm_subdrv_register(subdrv);
1878 if (ret < 0) {
1879 DRM_ERROR("failed to register drm ipp device.\n");
1880 goto err_cmd_workq;
1881 }
1882
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001883 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001884
1885 return 0;
1886
1887err_cmd_workq:
1888 destroy_workqueue(ctx->cmd_workq);
1889err_event_workq:
1890 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001891 return ret;
1892}
1893
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001894static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001895{
1896 struct ipp_context *ctx = platform_get_drvdata(pdev);
1897
Eunchul Kimcb471f142012-12-14 18:10:31 +09001898 /* unregister sub driver */
1899 exynos_drm_subdrv_unregister(&ctx->subdrv);
1900
1901 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001902 idr_destroy(&ctx->ipp_idr);
1903 idr_destroy(&ctx->prop_idr);
1904
1905 mutex_destroy(&ctx->ipp_lock);
1906 mutex_destroy(&ctx->prop_lock);
1907
1908 /* destroy command, event work queue */
1909 destroy_workqueue(ctx->cmd_workq);
1910 destroy_workqueue(ctx->event_workq);
1911
Eunchul Kimcb471f142012-12-14 18:10:31 +09001912 return 0;
1913}
1914
1915static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1916{
YoungJun Chocbc4c332013-06-12 10:44:40 +09001917 DRM_DEBUG_KMS("enable[%d]\n", enable);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001918
1919 return 0;
1920}
1921
1922#ifdef CONFIG_PM_SLEEP
1923static int ipp_suspend(struct device *dev)
1924{
1925 struct ipp_context *ctx = get_ipp_context(dev);
1926
Eunchul Kimcb471f142012-12-14 18:10:31 +09001927 if (pm_runtime_suspended(dev))
1928 return 0;
1929
1930 return ipp_power_ctrl(ctx, false);
1931}
1932
1933static int ipp_resume(struct device *dev)
1934{
1935 struct ipp_context *ctx = get_ipp_context(dev);
1936
Eunchul Kimcb471f142012-12-14 18:10:31 +09001937 if (!pm_runtime_suspended(dev))
1938 return ipp_power_ctrl(ctx, true);
1939
1940 return 0;
1941}
1942#endif
1943
1944#ifdef CONFIG_PM_RUNTIME
1945static int ipp_runtime_suspend(struct device *dev)
1946{
1947 struct ipp_context *ctx = get_ipp_context(dev);
1948
Eunchul Kimcb471f142012-12-14 18:10:31 +09001949 return ipp_power_ctrl(ctx, false);
1950}
1951
1952static int ipp_runtime_resume(struct device *dev)
1953{
1954 struct ipp_context *ctx = get_ipp_context(dev);
1955
Eunchul Kimcb471f142012-12-14 18:10:31 +09001956 return ipp_power_ctrl(ctx, true);
1957}
1958#endif
1959
1960static const struct dev_pm_ops ipp_pm_ops = {
1961 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1962 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1963};
1964
1965struct platform_driver ipp_driver = {
1966 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001967 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001968 .driver = {
1969 .name = "exynos-drm-ipp",
1970 .owner = THIS_MODULE,
1971 .pm = &ipp_pm_ops,
1972 },
1973};
1974