blob: 34d185c280c060a472a6eb188267fbdd8294baae [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;
683 struct drm_exynos_ipp_buf_info buf_info;
684 void *addr;
685 int i;
686
Eunchul Kimcb471f142012-12-14 18:10:31 +0900687 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900688 if (!m_node)
YoungJun Cho220db6f2014-05-26 10:17:20 +0200689 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900690
691 /* clear base address for error handling */
692 memset(&buf_info, 0x0, sizeof(buf_info));
693
694 /* operations, buffer id */
695 m_node->ops_id = qbuf->ops_id;
696 m_node->prop_id = qbuf->prop_id;
697 m_node->buf_id = qbuf->buf_id;
698
YoungJun Chocbc4c332013-06-12 10:44:40 +0900699 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
700 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900701
702 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900703 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900704
705 /* get dma address by handle */
706 if (qbuf->handle[i]) {
707 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
714 buf_info.handles[i] = qbuf->handle[i];
715 buf_info.base[i] = *(dma_addr_t *) addr;
YoungJun Chocbc4c332013-06-12 10:44:40 +0900716 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%x]\n",
717 i, buf_info.base[i], (int)buf_info.handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900718 }
719 }
720
721 m_node->filp = file;
722 m_node->buf_info = buf_info;
YoungJun Cho220db6f2014-05-26 10:17:20 +0200723 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900724 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900725 mutex_unlock(&c_node->mem_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +0200726
Eunchul Kimcb471f142012-12-14 18:10:31 +0900727 return m_node;
728
729err_clear:
730 kfree(m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900731 return ERR_PTR(-EFAULT);
732}
733
734static int ipp_put_mem_node(struct drm_device *drm_dev,
735 struct drm_exynos_ipp_cmd_node *c_node,
736 struct drm_exynos_ipp_mem_node *m_node)
737{
738 int i;
739
YoungJun Chocbc4c332013-06-12 10:44:40 +0900740 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900741
742 if (!m_node) {
743 DRM_ERROR("invalid dequeue node.\n");
744 return -EFAULT;
745 }
746
YoungJun Chocbc4c332013-06-12 10:44:40 +0900747 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900748
749 /* put gem buffer */
750 for_each_ipp_planar(i) {
751 unsigned long handle = m_node->buf_info.handles[i];
752 if (handle)
753 exynos_drm_gem_put_dma_addr(drm_dev, handle,
754 m_node->filp);
755 }
756
757 /* delete list in queue */
758 list_del(&m_node->list);
759 kfree(m_node);
760
Eunchul Kimcb471f142012-12-14 18:10:31 +0900761 return 0;
762}
763
764static void ipp_free_event(struct drm_pending_event *event)
765{
766 kfree(event);
767}
768
769static int ipp_get_event(struct drm_device *drm_dev,
770 struct drm_file *file,
771 struct drm_exynos_ipp_cmd_node *c_node,
772 struct drm_exynos_ipp_queue_buf *qbuf)
773{
774 struct drm_exynos_ipp_send_event *e;
775 unsigned long flags;
776
YoungJun Chocbc4c332013-06-12 10:44:40 +0900777 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900778
779 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900780 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900781 spin_lock_irqsave(&drm_dev->event_lock, flags);
782 file->event_space += sizeof(e->event);
783 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
784 return -ENOMEM;
785 }
786
787 /* make event */
788 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
789 e->event.base.length = sizeof(e->event);
790 e->event.user_data = qbuf->user_data;
791 e->event.prop_id = qbuf->prop_id;
792 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
793 e->base.event = &e->event.base;
794 e->base.file_priv = file;
795 e->base.destroy = ipp_free_event;
YoungJun Cho4d520762014-05-26 10:17:21 +0200796 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900797 list_add_tail(&e->base.link, &c_node->event_list);
YoungJun Cho4d520762014-05-26 10:17:21 +0200798 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900799
800 return 0;
801}
802
803static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
804 struct drm_exynos_ipp_queue_buf *qbuf)
805{
806 struct drm_exynos_ipp_send_event *e, *te;
807 int count = 0;
808
YoungJun Cho4d520762014-05-26 10:17:21 +0200809 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900810 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900811 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900812
813 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530814 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900815 * stop operations want to delete all event list.
816 * another case delete only same buf id.
817 */
818 if (!qbuf) {
819 /* delete list */
820 list_del(&e->base.link);
821 kfree(e);
822 }
823
824 /* compare buffer id */
825 if (qbuf && (qbuf->buf_id ==
826 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
827 /* delete list */
828 list_del(&e->base.link);
829 kfree(e);
YoungJun Cho4d520762014-05-26 10:17:21 +0200830 goto out_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900831 }
832 }
YoungJun Cho4d520762014-05-26 10:17:21 +0200833
834out_unlock:
835 mutex_unlock(&c_node->event_lock);
836 return;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900837}
838
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530839static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900840 struct exynos_drm_ippdrv *ippdrv,
841 struct drm_exynos_ipp_cmd_work *cmd_work,
842 struct drm_exynos_ipp_cmd_node *c_node)
843{
844 struct ipp_context *ctx = get_ipp_context(dev);
845
846 cmd_work->ippdrv = ippdrv;
847 cmd_work->c_node = c_node;
848 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
849}
850
851static int ipp_queue_buf_with_run(struct device *dev,
852 struct drm_exynos_ipp_cmd_node *c_node,
853 struct drm_exynos_ipp_mem_node *m_node,
854 struct drm_exynos_ipp_queue_buf *qbuf)
855{
856 struct exynos_drm_ippdrv *ippdrv;
857 struct drm_exynos_ipp_property *property;
858 struct exynos_drm_ipp_ops *ops;
859 int ret;
860
Eunchul Kimcb471f142012-12-14 18:10:31 +0900861 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530862 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900863 DRM_ERROR("failed to get ipp driver.\n");
864 return -EFAULT;
865 }
866
867 ops = ippdrv->ops[qbuf->ops_id];
868 if (!ops) {
869 DRM_ERROR("failed to get ops.\n");
870 return -EFAULT;
871 }
872
873 property = &c_node->property;
874
875 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900876 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900877 return 0;
878 }
879
YoungJun Cho220db6f2014-05-26 10:17:20 +0200880 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900881 if (!ipp_check_mem_list(c_node)) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200882 mutex_unlock(&c_node->mem_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900883 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900884 return 0;
885 }
886
887 /*
888 * If set destination buffer and enabled clock,
889 * then m2m operations need start operations at queue_buf
890 */
891 if (ipp_is_m2m_cmd(property->cmd)) {
892 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
893
894 cmd_work->ctrl = IPP_CTRL_PLAY;
895 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
896 } else {
897 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
898 if (ret) {
YoungJun Cho220db6f2014-05-26 10:17:20 +0200899 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900900 DRM_ERROR("failed to set m node.\n");
901 return ret;
902 }
903 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200904 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900905
906 return 0;
907}
908
909static void ipp_clean_queue_buf(struct drm_device *drm_dev,
910 struct drm_exynos_ipp_cmd_node *c_node,
911 struct drm_exynos_ipp_queue_buf *qbuf)
912{
913 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
914
YoungJun Choc66ce402014-05-26 10:17:15 +0200915 /* delete list */
YoungJun Cho220db6f2014-05-26 10:17:20 +0200916 mutex_lock(&c_node->mem_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200917 list_for_each_entry_safe(m_node, tm_node,
918 &c_node->mem_list[qbuf->ops_id], list) {
919 if (m_node->buf_id == qbuf->buf_id &&
920 m_node->ops_id == qbuf->ops_id)
921 ipp_put_mem_node(drm_dev, c_node, m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900922 }
YoungJun Cho220db6f2014-05-26 10:17:20 +0200923 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900924}
925
926int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
927 struct drm_file *file)
928{
929 struct drm_exynos_file_private *file_priv = file->driver_priv;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +0200930 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900931 struct ipp_context *ctx = get_ipp_context(dev);
932 struct drm_exynos_ipp_queue_buf *qbuf = data;
933 struct drm_exynos_ipp_cmd_node *c_node;
934 struct drm_exynos_ipp_mem_node *m_node;
935 int ret;
936
Eunchul Kimcb471f142012-12-14 18:10:31 +0900937 if (!qbuf) {
938 DRM_ERROR("invalid buf parameter.\n");
939 return -EINVAL;
940 }
941
942 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
943 DRM_ERROR("invalid ops parameter.\n");
944 return -EINVAL;
945 }
946
YoungJun Chocbc4c332013-06-12 10:44:40 +0900947 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
948 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900949 qbuf->buf_id, qbuf->buf_type);
950
951 /* find command node */
952 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
953 qbuf->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800954 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900955 DRM_ERROR("failed to get command node.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +0800956 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900957 }
958
959 /* buffer control */
960 switch (qbuf->buf_type) {
961 case IPP_BUF_ENQUEUE:
962 /* get memory node */
963 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
964 if (IS_ERR(m_node)) {
965 DRM_ERROR("failed to get m_node.\n");
966 return PTR_ERR(m_node);
967 }
968
969 /*
970 * first step get event for destination buffer.
971 * and second step when M2M case run with destination buffer
972 * if needed.
973 */
974 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
975 /* get event for destination buffer */
976 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
977 if (ret) {
978 DRM_ERROR("failed to get event.\n");
979 goto err_clean_node;
980 }
981
982 /*
983 * M2M case run play control for streaming feature.
984 * other case set address and waiting.
985 */
986 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
987 if (ret) {
988 DRM_ERROR("failed to run command.\n");
989 goto err_clean_node;
990 }
991 }
992 break;
993 case IPP_BUF_DEQUEUE:
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200994 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900995
996 /* put event for destination buffer */
997 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
998 ipp_put_event(c_node, qbuf);
999
1000 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1001
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001002 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001003 break;
1004 default:
1005 DRM_ERROR("invalid buffer control.\n");
1006 return -EINVAL;
1007 }
1008
1009 return 0;
1010
1011err_clean_node:
1012 DRM_ERROR("clean memory nodes.\n");
1013
1014 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1015 return ret;
1016}
1017
1018static bool exynos_drm_ipp_check_valid(struct device *dev,
1019 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
1020{
Eunchul Kimcb471f142012-12-14 18:10:31 +09001021 if (ctrl != IPP_CTRL_PLAY) {
1022 if (pm_runtime_suspended(dev)) {
1023 DRM_ERROR("pm:runtime_suspended.\n");
1024 goto err_status;
1025 }
1026 }
1027
1028 switch (ctrl) {
1029 case IPP_CTRL_PLAY:
1030 if (state != IPP_STATE_IDLE)
1031 goto err_status;
1032 break;
1033 case IPP_CTRL_STOP:
1034 if (state == IPP_STATE_STOP)
1035 goto err_status;
1036 break;
1037 case IPP_CTRL_PAUSE:
1038 if (state != IPP_STATE_START)
1039 goto err_status;
1040 break;
1041 case IPP_CTRL_RESUME:
1042 if (state != IPP_STATE_STOP)
1043 goto err_status;
1044 break;
1045 default:
1046 DRM_ERROR("invalid state.\n");
1047 goto err_status;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001048 }
1049
1050 return true;
1051
1052err_status:
1053 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1054 return false;
1055}
1056
1057int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1058 struct drm_file *file)
1059{
1060 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001061 struct exynos_drm_ippdrv *ippdrv = NULL;
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001062 struct device *dev = file_priv->ipp_dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001063 struct ipp_context *ctx = get_ipp_context(dev);
1064 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1065 struct drm_exynos_ipp_cmd_work *cmd_work;
1066 struct drm_exynos_ipp_cmd_node *c_node;
1067
Eunchul Kimcb471f142012-12-14 18:10:31 +09001068 if (!ctx) {
1069 DRM_ERROR("invalid context.\n");
1070 return -EINVAL;
1071 }
1072
1073 if (!cmd_ctrl) {
1074 DRM_ERROR("invalid control parameter.\n");
1075 return -EINVAL;
1076 }
1077
YoungJun Chocbc4c332013-06-12 10:44:40 +09001078 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001079 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1080
1081 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1082 if (IS_ERR(ippdrv)) {
1083 DRM_ERROR("failed to get ipp driver.\n");
1084 return PTR_ERR(ippdrv);
1085 }
1086
1087 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1088 cmd_ctrl->prop_id);
Wei Yongjunbe348792013-07-04 21:35:00 +08001089 if (IS_ERR(c_node)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001090 DRM_ERROR("invalid command node list.\n");
Wei Yongjunbe348792013-07-04 21:35:00 +08001091 return PTR_ERR(c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001092 }
1093
1094 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1095 c_node->state)) {
1096 DRM_ERROR("invalid state.\n");
1097 return -EINVAL;
1098 }
1099
1100 switch (cmd_ctrl->ctrl) {
1101 case IPP_CTRL_PLAY:
1102 if (pm_runtime_suspended(ippdrv->dev))
1103 pm_runtime_get_sync(ippdrv->dev);
YoungJun Choebaf05c2014-05-26 10:17:16 +02001104
Eunchul Kimcb471f142012-12-14 18:10:31 +09001105 c_node->state = IPP_STATE_START;
1106
1107 cmd_work = c_node->start_work;
1108 cmd_work->ctrl = cmd_ctrl->ctrl;
1109 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001110 break;
1111 case IPP_CTRL_STOP:
1112 cmd_work = c_node->stop_work;
1113 cmd_work->ctrl = cmd_ctrl->ctrl;
1114 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1115
1116 if (!wait_for_completion_timeout(&c_node->stop_complete,
1117 msecs_to_jiffies(300))) {
1118 DRM_ERROR("timeout stop:prop_id[%d]\n",
1119 c_node->property.prop_id);
1120 }
1121
1122 c_node->state = IPP_STATE_STOP;
1123 ippdrv->dedicated = false;
YoungJun Cho7f5af052014-05-26 10:17:18 +02001124 mutex_lock(&ippdrv->cmd_lock);
YoungJun Cho075436b2014-05-26 10:17:19 +02001125 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001126
1127 if (list_empty(&ippdrv->cmd_list))
1128 pm_runtime_put_sync(ippdrv->dev);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001129 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001130 break;
1131 case IPP_CTRL_PAUSE:
1132 cmd_work = c_node->stop_work;
1133 cmd_work->ctrl = cmd_ctrl->ctrl;
1134 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1135
1136 if (!wait_for_completion_timeout(&c_node->stop_complete,
1137 msecs_to_jiffies(200))) {
1138 DRM_ERROR("timeout stop:prop_id[%d]\n",
1139 c_node->property.prop_id);
1140 }
1141
1142 c_node->state = IPP_STATE_STOP;
1143 break;
1144 case IPP_CTRL_RESUME:
1145 c_node->state = IPP_STATE_START;
1146 cmd_work = c_node->start_work;
1147 cmd_work->ctrl = cmd_ctrl->ctrl;
1148 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1149 break;
1150 default:
1151 DRM_ERROR("could not support this state currently.\n");
1152 return -EINVAL;
1153 }
1154
YoungJun Chocbc4c332013-06-12 10:44:40 +09001155 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001156 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1157
1158 return 0;
1159}
1160
1161int exynos_drm_ippnb_register(struct notifier_block *nb)
1162{
1163 return blocking_notifier_chain_register(
1164 &exynos_drm_ippnb_list, nb);
1165}
1166
1167int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1168{
1169 return blocking_notifier_chain_unregister(
1170 &exynos_drm_ippnb_list, nb);
1171}
1172
1173int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1174{
1175 return blocking_notifier_call_chain(
1176 &exynos_drm_ippnb_list, val, v);
1177}
1178
1179static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1180 struct drm_exynos_ipp_property *property)
1181{
1182 struct exynos_drm_ipp_ops *ops = NULL;
1183 bool swap = false;
1184 int ret, i;
1185
1186 if (!property) {
1187 DRM_ERROR("invalid property parameter.\n");
1188 return -EINVAL;
1189 }
1190
YoungJun Chocbc4c332013-06-12 10:44:40 +09001191 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001192
1193 /* reset h/w block */
1194 if (ippdrv->reset &&
1195 ippdrv->reset(ippdrv->dev)) {
1196 DRM_ERROR("failed to reset.\n");
1197 return -EINVAL;
1198 }
1199
1200 /* set source,destination operations */
1201 for_each_ipp_ops(i) {
1202 struct drm_exynos_ipp_config *config =
1203 &property->config[i];
1204
1205 ops = ippdrv->ops[i];
1206 if (!ops || !config) {
1207 DRM_ERROR("not support ops and config.\n");
1208 return -EINVAL;
1209 }
1210
1211 /* set format */
1212 if (ops->set_fmt) {
1213 ret = ops->set_fmt(ippdrv->dev, config->fmt);
1214 if (ret) {
1215 DRM_ERROR("not support format.\n");
1216 return ret;
1217 }
1218 }
1219
1220 /* set transform for rotation, flip */
1221 if (ops->set_transf) {
1222 ret = ops->set_transf(ippdrv->dev, config->degree,
1223 config->flip, &swap);
1224 if (ret) {
1225 DRM_ERROR("not support tranf.\n");
1226 return -EINVAL;
1227 }
1228 }
1229
1230 /* set size */
1231 if (ops->set_size) {
1232 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1233 &config->sz);
1234 if (ret) {
1235 DRM_ERROR("not support size.\n");
1236 return ret;
1237 }
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1245 struct drm_exynos_ipp_cmd_node *c_node)
1246{
1247 struct drm_exynos_ipp_mem_node *m_node;
1248 struct drm_exynos_ipp_property *property = &c_node->property;
1249 struct list_head *head;
1250 int ret, i;
1251
YoungJun Chocbc4c332013-06-12 10:44:40 +09001252 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001253
1254 /* store command info in ippdrv */
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001255 ippdrv->c_node = c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001256
YoungJun Cho220db6f2014-05-26 10:17:20 +02001257 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001258 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001259 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001260 ret = -ENOMEM;
1261 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001262 }
1263
1264 /* set current property in ippdrv */
1265 ret = ipp_set_property(ippdrv, property);
1266 if (ret) {
1267 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001268 ippdrv->c_node = NULL;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001269 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001270 }
1271
1272 /* check command */
1273 switch (property->cmd) {
1274 case IPP_CMD_M2M:
1275 for_each_ipp_ops(i) {
1276 /* source/destination memory list */
1277 head = &c_node->mem_list[i];
1278
1279 m_node = list_first_entry(head,
1280 struct drm_exynos_ipp_mem_node, list);
1281 if (!m_node) {
1282 DRM_ERROR("failed to get node.\n");
1283 ret = -EFAULT;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001284 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001285 }
1286
YoungJun Chocbc4c332013-06-12 10:44:40 +09001287 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001288
1289 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1290 if (ret) {
1291 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001292 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001293 }
1294 }
1295 break;
1296 case IPP_CMD_WB:
1297 /* destination memory list */
1298 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1299
1300 list_for_each_entry(m_node, head, list) {
1301 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1302 if (ret) {
1303 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001304 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001305 }
1306 }
1307 break;
1308 case IPP_CMD_OUTPUT:
1309 /* source memory list */
1310 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1311
1312 list_for_each_entry(m_node, head, list) {
1313 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1314 if (ret) {
1315 DRM_ERROR("failed to set m node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001316 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001317 }
1318 }
1319 break;
1320 default:
1321 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001322 ret = -EINVAL;
1323 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001324 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001325 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001326
YoungJun Chocbc4c332013-06-12 10:44:40 +09001327 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001328
1329 /* start operations */
1330 if (ippdrv->start) {
1331 ret = ippdrv->start(ippdrv->dev, property->cmd);
1332 if (ret) {
1333 DRM_ERROR("failed to start ops.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001334 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001335 return ret;
1336 }
1337 }
1338
1339 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001340
1341err_unlock:
1342 mutex_unlock(&c_node->mem_lock);
1343 ippdrv->c_node = NULL;
1344 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001345}
1346
1347static int ipp_stop_property(struct drm_device *drm_dev,
1348 struct exynos_drm_ippdrv *ippdrv,
1349 struct drm_exynos_ipp_cmd_node *c_node)
1350{
1351 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1352 struct drm_exynos_ipp_property *property = &c_node->property;
1353 struct list_head *head;
1354 int ret = 0, i;
1355
YoungJun Chocbc4c332013-06-12 10:44:40 +09001356 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001357
1358 /* put event */
1359 ipp_put_event(c_node, NULL);
1360
YoungJun Cho220db6f2014-05-26 10:17:20 +02001361 mutex_lock(&c_node->mem_lock);
1362
Eunchul Kimcb471f142012-12-14 18:10:31 +09001363 /* check command */
1364 switch (property->cmd) {
1365 case IPP_CMD_M2M:
1366 for_each_ipp_ops(i) {
1367 /* source/destination memory list */
1368 head = &c_node->mem_list[i];
1369
Eunchul Kimcb471f142012-12-14 18:10:31 +09001370 list_for_each_entry_safe(m_node, tm_node,
1371 head, list) {
1372 ret = ipp_put_mem_node(drm_dev, c_node,
1373 m_node);
1374 if (ret) {
1375 DRM_ERROR("failed to put m_node.\n");
1376 goto err_clear;
1377 }
1378 }
1379 }
1380 break;
1381 case IPP_CMD_WB:
1382 /* destination memory list */
1383 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1384
Eunchul Kimcb471f142012-12-14 18:10:31 +09001385 list_for_each_entry_safe(m_node, tm_node, head, list) {
1386 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1387 if (ret) {
1388 DRM_ERROR("failed to put m_node.\n");
1389 goto err_clear;
1390 }
1391 }
1392 break;
1393 case IPP_CMD_OUTPUT:
1394 /* source memory list */
1395 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1396
Eunchul Kimcb471f142012-12-14 18:10:31 +09001397 list_for_each_entry_safe(m_node, tm_node, head, list) {
1398 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1399 if (ret) {
1400 DRM_ERROR("failed to put m_node.\n");
1401 goto err_clear;
1402 }
1403 }
1404 break;
1405 default:
1406 DRM_ERROR("invalid operations.\n");
1407 ret = -EINVAL;
1408 goto err_clear;
1409 }
1410
1411err_clear:
YoungJun Cho220db6f2014-05-26 10:17:20 +02001412 mutex_unlock(&c_node->mem_lock);
1413
Eunchul Kimcb471f142012-12-14 18:10:31 +09001414 /* stop operations */
1415 if (ippdrv->stop)
1416 ippdrv->stop(ippdrv->dev, property->cmd);
1417
1418 return ret;
1419}
1420
1421void ipp_sched_cmd(struct work_struct *work)
1422{
1423 struct drm_exynos_ipp_cmd_work *cmd_work =
1424 (struct drm_exynos_ipp_cmd_work *)work;
1425 struct exynos_drm_ippdrv *ippdrv;
1426 struct drm_exynos_ipp_cmd_node *c_node;
1427 struct drm_exynos_ipp_property *property;
1428 int ret;
1429
Eunchul Kimcb471f142012-12-14 18:10:31 +09001430 ippdrv = cmd_work->ippdrv;
1431 if (!ippdrv) {
1432 DRM_ERROR("invalid ippdrv list.\n");
1433 return;
1434 }
1435
1436 c_node = cmd_work->c_node;
1437 if (!c_node) {
1438 DRM_ERROR("invalid command node list.\n");
1439 return;
1440 }
1441
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001442 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001443
1444 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001445
1446 switch (cmd_work->ctrl) {
1447 case IPP_CTRL_PLAY:
1448 case IPP_CTRL_RESUME:
1449 ret = ipp_start_property(ippdrv, c_node);
1450 if (ret) {
1451 DRM_ERROR("failed to start property:prop_id[%d]\n",
1452 c_node->property.prop_id);
1453 goto err_unlock;
1454 }
1455
1456 /*
1457 * M2M case supports wait_completion of transfer.
1458 * because M2M case supports single unit operation
1459 * with multiple queue.
1460 * M2M need to wait completion of data transfer.
1461 */
1462 if (ipp_is_m2m_cmd(property->cmd)) {
1463 if (!wait_for_completion_timeout
1464 (&c_node->start_complete, msecs_to_jiffies(200))) {
1465 DRM_ERROR("timeout event:prop_id[%d]\n",
1466 c_node->property.prop_id);
1467 goto err_unlock;
1468 }
1469 }
1470 break;
1471 case IPP_CTRL_STOP:
1472 case IPP_CTRL_PAUSE:
1473 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1474 c_node);
1475 if (ret) {
1476 DRM_ERROR("failed to stop property.\n");
1477 goto err_unlock;
1478 }
1479
1480 complete(&c_node->stop_complete);
1481 break;
1482 default:
1483 DRM_ERROR("unknown control type\n");
1484 break;
1485 }
1486
YoungJun Chocbc4c332013-06-12 10:44:40 +09001487 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001488
1489err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001490 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001491}
1492
1493static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1494 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1495{
1496 struct drm_device *drm_dev = ippdrv->drm_dev;
1497 struct drm_exynos_ipp_property *property = &c_node->property;
1498 struct drm_exynos_ipp_mem_node *m_node;
1499 struct drm_exynos_ipp_queue_buf qbuf;
1500 struct drm_exynos_ipp_send_event *e;
1501 struct list_head *head;
1502 struct timeval now;
1503 unsigned long flags;
1504 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1505 int ret, i;
1506
1507 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001508 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001509
1510 if (!drm_dev) {
1511 DRM_ERROR("failed to get drm_dev.\n");
1512 return -EINVAL;
1513 }
1514
1515 if (!property) {
1516 DRM_ERROR("failed to get property.\n");
1517 return -EINVAL;
1518 }
1519
YoungJun Cho4d520762014-05-26 10:17:21 +02001520 mutex_lock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001521 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001522 DRM_DEBUG_KMS("event list is empty.\n");
YoungJun Cho4d520762014-05-26 10:17:21 +02001523 ret = 0;
1524 goto err_event_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001525 }
1526
YoungJun Cho220db6f2014-05-26 10:17:20 +02001527 mutex_lock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001528 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001529 DRM_DEBUG_KMS("empty memory.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001530 ret = 0;
1531 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001532 }
1533
1534 /* check command */
1535 switch (property->cmd) {
1536 case IPP_CMD_M2M:
1537 for_each_ipp_ops(i) {
1538 /* source/destination memory list */
1539 head = &c_node->mem_list[i];
1540
1541 m_node = list_first_entry(head,
1542 struct drm_exynos_ipp_mem_node, list);
1543 if (!m_node) {
1544 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001545 ret = -ENOMEM;
1546 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001547 }
1548
1549 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001550 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001551 i ? "dst" : "src", tbuf_id[i]);
1552
1553 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1554 if (ret)
1555 DRM_ERROR("failed to put m_node.\n");
1556 }
1557 break;
1558 case IPP_CMD_WB:
1559 /* clear buf for finding */
1560 memset(&qbuf, 0x0, sizeof(qbuf));
1561 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1562 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1563
1564 /* get memory node entry */
1565 m_node = ipp_find_mem_node(c_node, &qbuf);
1566 if (!m_node) {
1567 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001568 ret = -ENOMEM;
1569 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001570 }
1571
1572 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1573
1574 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1575 if (ret)
1576 DRM_ERROR("failed to put m_node.\n");
1577 break;
1578 case IPP_CMD_OUTPUT:
1579 /* source memory list */
1580 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1581
1582 m_node = list_first_entry(head,
1583 struct drm_exynos_ipp_mem_node, list);
1584 if (!m_node) {
1585 DRM_ERROR("empty memory node.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001586 ret = -ENOMEM;
1587 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001588 }
1589
1590 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1591
1592 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1593 if (ret)
1594 DRM_ERROR("failed to put m_node.\n");
1595 break;
1596 default:
1597 DRM_ERROR("invalid operations.\n");
YoungJun Cho220db6f2014-05-26 10:17:20 +02001598 ret = -EINVAL;
1599 goto err_mem_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001600 }
YoungJun Cho220db6f2014-05-26 10:17:20 +02001601 mutex_unlock(&c_node->mem_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001602
1603 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1604 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1605 tbuf_id[1], buf_id[1], property->prop_id);
1606
1607 /*
1608 * command node have event list of destination buffer
1609 * If destination buffer enqueue to mem list,
1610 * then we make event and link to event list tail.
1611 * so, we get first event for first enqueued buffer.
1612 */
1613 e = list_first_entry(&c_node->event_list,
1614 struct drm_exynos_ipp_send_event, base.link);
1615
Eunchul Kimcb471f142012-12-14 18:10:31 +09001616 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001617 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001618 e->event.tv_sec = now.tv_sec;
1619 e->event.tv_usec = now.tv_usec;
1620 e->event.prop_id = property->prop_id;
1621
1622 /* set buffer id about source destination */
1623 for_each_ipp_ops(i)
1624 e->event.buf_id[i] = tbuf_id[i];
1625
1626 spin_lock_irqsave(&drm_dev->event_lock, flags);
1627 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1628 wake_up_interruptible(&e->base.file_priv->event_wait);
1629 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
YoungJun Cho4d520762014-05-26 10:17:21 +02001630 mutex_unlock(&c_node->event_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001631
YoungJun Chocbc4c332013-06-12 10:44:40 +09001632 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001633 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1634
1635 return 0;
YoungJun Cho220db6f2014-05-26 10:17:20 +02001636
1637err_mem_unlock:
1638 mutex_unlock(&c_node->mem_lock);
YoungJun Cho4d520762014-05-26 10:17:21 +02001639err_event_unlock:
1640 mutex_unlock(&c_node->event_lock);
YoungJun Cho220db6f2014-05-26 10:17:20 +02001641 return ret;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001642}
1643
1644void ipp_sched_event(struct work_struct *work)
1645{
1646 struct drm_exynos_ipp_event_work *event_work =
1647 (struct drm_exynos_ipp_event_work *)work;
1648 struct exynos_drm_ippdrv *ippdrv;
1649 struct drm_exynos_ipp_cmd_node *c_node;
1650 int ret;
1651
1652 if (!event_work) {
1653 DRM_ERROR("failed to get event_work.\n");
1654 return;
1655 }
1656
YoungJun Chocbc4c332013-06-12 10:44:40 +09001657 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001658
1659 ippdrv = event_work->ippdrv;
1660 if (!ippdrv) {
1661 DRM_ERROR("failed to get ipp driver.\n");
1662 return;
1663 }
1664
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001665 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001666 if (!c_node) {
1667 DRM_ERROR("failed to get command node.\n");
1668 return;
1669 }
1670
1671 /*
1672 * IPP supports command thread, event thread synchronization.
1673 * If IPP close immediately from user land, then IPP make
1674 * synchronization with command thread, so make complete event.
1675 * or going out operations.
1676 */
1677 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001678 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1679 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001680 goto err_completion;
1681 }
1682
Eunchul Kimcb471f142012-12-14 18:10:31 +09001683 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1684 if (ret) {
1685 DRM_ERROR("failed to send event.\n");
1686 goto err_completion;
1687 }
1688
1689err_completion:
1690 if (ipp_is_m2m_cmd(c_node->property.cmd))
1691 complete(&c_node->start_complete);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001692}
1693
1694static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1695{
1696 struct ipp_context *ctx = get_ipp_context(dev);
1697 struct exynos_drm_ippdrv *ippdrv;
1698 int ret, count = 0;
1699
Eunchul Kimcb471f142012-12-14 18:10:31 +09001700 /* get ipp driver entry */
1701 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001702 u32 ipp_id;
1703
Eunchul Kimcb471f142012-12-14 18:10:31 +09001704 ippdrv->drm_dev = drm_dev;
1705
1706 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001707 &ipp_id);
1708 if (ret || ipp_id == 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001709 DRM_ERROR("failed to create id.\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001710 goto err;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001711 }
1712
YoungJun Chocbc4c332013-06-12 10:44:40 +09001713 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001714 count++, (int)ippdrv, ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001715
Andrzej Hajda31646052014-05-19 12:54:05 +02001716 ippdrv->prop_list.ipp_id = ipp_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001717
1718 /* store parent device for node */
1719 ippdrv->parent_dev = dev;
1720
1721 /* store event work queue and handler */
1722 ippdrv->event_workq = ctx->event_workq;
1723 ippdrv->sched_event = ipp_sched_event;
1724 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001725 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001726
1727 if (is_drm_iommu_supported(drm_dev)) {
1728 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1729 if (ret) {
1730 DRM_ERROR("failed to activate iommu\n");
YoungJun Cho075436b2014-05-26 10:17:19 +02001731 goto err;
Eunchul Kimc12e2612012-12-14 17:58:54 +09001732 }
1733 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001734 }
1735
1736 return 0;
1737
YoungJun Cho075436b2014-05-26 10:17:19 +02001738err:
Eunchul Kimc12e2612012-12-14 17:58:54 +09001739 /* get ipp driver entry */
YoungJun Cho075436b2014-05-26 10:17:19 +02001740 list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1741 drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001742 if (is_drm_iommu_supported(drm_dev))
1743 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1744
YoungJun Cho075436b2014-05-26 10:17:19 +02001745 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1746 ippdrv->prop_list.ipp_id);
1747 }
1748
Eunchul Kimcb471f142012-12-14 18:10:31 +09001749 return ret;
1750}
1751
1752static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1753{
1754 struct exynos_drm_ippdrv *ippdrv;
YoungJun Cho075436b2014-05-26 10:17:19 +02001755 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001756
Eunchul Kimcb471f142012-12-14 18:10:31 +09001757 /* get ipp driver entry */
1758 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001759 if (is_drm_iommu_supported(drm_dev))
1760 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1761
YoungJun Cho075436b2014-05-26 10:17:19 +02001762 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1763 ippdrv->prop_list.ipp_id);
1764
Eunchul Kimcb471f142012-12-14 18:10:31 +09001765 ippdrv->drm_dev = NULL;
1766 exynos_drm_ippdrv_unregister(ippdrv);
1767 }
1768}
1769
1770static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1771 struct drm_file *file)
1772{
1773 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001774
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001775 file_priv->ipp_dev = dev;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001776
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001777 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001778
1779 return 0;
1780}
1781
1782static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1783 struct drm_file *file)
1784{
1785 struct drm_exynos_file_private *file_priv = file->driver_priv;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001786 struct exynos_drm_ippdrv *ippdrv = NULL;
YoungJun Cho075436b2014-05-26 10:17:19 +02001787 struct ipp_context *ctx = get_ipp_context(dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001788 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1789 int count = 0;
1790
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001791 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)file_priv->ipp_dev);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001792
Eunchul Kimcb471f142012-12-14 18:10:31 +09001793 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001794 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001795 list_for_each_entry_safe(c_node, tc_node,
1796 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001797 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1798 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001799
Andrzej Hajda5c76c5b2014-07-03 15:10:28 +02001800 if (c_node->dev == file_priv->ipp_dev) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001801 /*
1802 * userland goto unnormal state. process killed.
1803 * and close the file.
1804 * so, IPP didn't called stop cmd ctrl.
1805 * so, we are make stop operation in this state.
1806 */
1807 if (c_node->state == IPP_STATE_START) {
1808 ipp_stop_property(drm_dev, ippdrv,
1809 c_node);
1810 c_node->state = IPP_STATE_STOP;
1811 }
1812
1813 ippdrv->dedicated = false;
YoungJun Cho075436b2014-05-26 10:17:19 +02001814 ipp_clean_cmd_node(ctx, c_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001815 if (list_empty(&ippdrv->cmd_list))
1816 pm_runtime_put_sync(ippdrv->dev);
1817 }
1818 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001819 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001820 }
1821
Eunchul Kimcb471f142012-12-14 18:10:31 +09001822 return;
1823}
1824
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001825static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001826{
1827 struct device *dev = &pdev->dev;
1828 struct ipp_context *ctx;
1829 struct exynos_drm_subdrv *subdrv;
1830 int ret;
1831
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001832 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001833 if (!ctx)
1834 return -ENOMEM;
1835
Eunchul Kimcb471f142012-12-14 18:10:31 +09001836 mutex_init(&ctx->ipp_lock);
1837 mutex_init(&ctx->prop_lock);
1838
1839 idr_init(&ctx->ipp_idr);
1840 idr_init(&ctx->prop_idr);
1841
1842 /*
1843 * create single thread for ipp event
1844 * IPP supports event thread for IPP drivers.
1845 * IPP driver send event_work to this thread.
1846 * and IPP event thread send event to user process.
1847 */
1848 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1849 if (!ctx->event_workq) {
1850 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301851 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001852 }
1853
1854 /*
1855 * create single thread for ipp command
1856 * IPP supports command thread for user process.
1857 * user process make command node using set property ioctl.
1858 * and make start_work and send this work to command thread.
1859 * and then this command thread start property.
1860 */
1861 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1862 if (!ctx->cmd_workq) {
1863 dev_err(dev, "failed to create cmd workqueue\n");
1864 ret = -EINVAL;
1865 goto err_event_workq;
1866 }
1867
1868 /* set sub driver informations */
1869 subdrv = &ctx->subdrv;
1870 subdrv->dev = dev;
1871 subdrv->probe = ipp_subdrv_probe;
1872 subdrv->remove = ipp_subdrv_remove;
1873 subdrv->open = ipp_subdrv_open;
1874 subdrv->close = ipp_subdrv_close;
1875
1876 platform_set_drvdata(pdev, ctx);
1877
1878 ret = exynos_drm_subdrv_register(subdrv);
1879 if (ret < 0) {
1880 DRM_ERROR("failed to register drm ipp device.\n");
1881 goto err_cmd_workq;
1882 }
1883
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001884 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001885
1886 return 0;
1887
1888err_cmd_workq:
1889 destroy_workqueue(ctx->cmd_workq);
1890err_event_workq:
1891 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001892 return ret;
1893}
1894
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001895static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001896{
1897 struct ipp_context *ctx = platform_get_drvdata(pdev);
1898
Eunchul Kimcb471f142012-12-14 18:10:31 +09001899 /* unregister sub driver */
1900 exynos_drm_subdrv_unregister(&ctx->subdrv);
1901
1902 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001903 idr_destroy(&ctx->ipp_idr);
1904 idr_destroy(&ctx->prop_idr);
1905
1906 mutex_destroy(&ctx->ipp_lock);
1907 mutex_destroy(&ctx->prop_lock);
1908
1909 /* destroy command, event work queue */
1910 destroy_workqueue(ctx->cmd_workq);
1911 destroy_workqueue(ctx->event_workq);
1912
Eunchul Kimcb471f142012-12-14 18:10:31 +09001913 return 0;
1914}
1915
1916static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1917{
YoungJun Chocbc4c332013-06-12 10:44:40 +09001918 DRM_DEBUG_KMS("enable[%d]\n", enable);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001919
1920 return 0;
1921}
1922
1923#ifdef CONFIG_PM_SLEEP
1924static int ipp_suspend(struct device *dev)
1925{
1926 struct ipp_context *ctx = get_ipp_context(dev);
1927
Eunchul Kimcb471f142012-12-14 18:10:31 +09001928 if (pm_runtime_suspended(dev))
1929 return 0;
1930
1931 return ipp_power_ctrl(ctx, false);
1932}
1933
1934static int ipp_resume(struct device *dev)
1935{
1936 struct ipp_context *ctx = get_ipp_context(dev);
1937
Eunchul Kimcb471f142012-12-14 18:10:31 +09001938 if (!pm_runtime_suspended(dev))
1939 return ipp_power_ctrl(ctx, true);
1940
1941 return 0;
1942}
1943#endif
1944
1945#ifdef CONFIG_PM_RUNTIME
1946static int ipp_runtime_suspend(struct device *dev)
1947{
1948 struct ipp_context *ctx = get_ipp_context(dev);
1949
Eunchul Kimcb471f142012-12-14 18:10:31 +09001950 return ipp_power_ctrl(ctx, false);
1951}
1952
1953static int ipp_runtime_resume(struct device *dev)
1954{
1955 struct ipp_context *ctx = get_ipp_context(dev);
1956
Eunchul Kimcb471f142012-12-14 18:10:31 +09001957 return ipp_power_ctrl(ctx, true);
1958}
1959#endif
1960
1961static const struct dev_pm_ops ipp_pm_ops = {
1962 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1963 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1964};
1965
1966struct platform_driver ipp_driver = {
1967 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001968 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001969 .driver = {
1970 .name = "exynos-drm-ipp",
1971 .owner = THIS_MODULE,
1972 .pm = &ipp_pm_ops,
1973 },
1974};
1975