blob: b60ae54ef82beca2ced83b8befdd1d1d65eadbd4 [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
170static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
171{
172 void *obj;
173
YoungJun Chocbc4c332013-06-12 10:44:40 +0900174 DRM_DEBUG_KMS("id[%d]\n", id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900175
176 mutex_lock(lock);
177
178 /* find object using handle */
179 obj = idr_find(id_idr, id);
180 if (!obj) {
181 DRM_ERROR("failed to find object.\n");
182 mutex_unlock(lock);
183 return ERR_PTR(-ENODEV);
184 }
185
186 mutex_unlock(lock);
187
188 return obj;
189}
190
191static inline bool ipp_check_dedicated(struct exynos_drm_ippdrv *ippdrv,
192 enum drm_exynos_ipp_cmd cmd)
193{
194 /*
195 * check dedicated flag and WB, OUTPUT operation with
196 * power on state.
197 */
198 if (ippdrv->dedicated || (!ipp_is_m2m_cmd(cmd) &&
199 !pm_runtime_suspended(ippdrv->dev)))
200 return true;
201
202 return false;
203}
204
205static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
206 struct drm_exynos_ipp_property *property)
207{
208 struct exynos_drm_ippdrv *ippdrv;
209 u32 ipp_id = property->ipp_id;
210
YoungJun Chocbc4c332013-06-12 10:44:40 +0900211 DRM_DEBUG_KMS("ipp_id[%d]\n", ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900212
213 if (ipp_id) {
214 /* find ipp driver using idr */
215 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
216 ipp_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530217 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900218 DRM_ERROR("not found ipp%d driver.\n", ipp_id);
219 return ippdrv;
220 }
221
222 /*
223 * WB, OUTPUT opertion not supported multi-operation.
224 * so, make dedicated state at set property ioctl.
225 * when ipp driver finished operations, clear dedicated flags.
226 */
227 if (ipp_check_dedicated(ippdrv, property->cmd)) {
228 DRM_ERROR("already used choose device.\n");
229 return ERR_PTR(-EBUSY);
230 }
231
232 /*
233 * This is necessary to find correct device in ipp drivers.
234 * ipp drivers have different abilities,
235 * so need to check property.
236 */
237 if (ippdrv->check_property &&
238 ippdrv->check_property(ippdrv->dev, property)) {
239 DRM_ERROR("not support property.\n");
240 return ERR_PTR(-EINVAL);
241 }
242
243 return ippdrv;
244 } else {
245 /*
246 * This case is search all ipp driver for finding.
247 * user application don't set ipp_id in this case,
248 * so ipp subsystem search correct driver in driver list.
249 */
250 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
251 if (ipp_check_dedicated(ippdrv, property->cmd)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900252 DRM_DEBUG_KMS("used device.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900253 continue;
254 }
255
256 if (ippdrv->check_property &&
257 ippdrv->check_property(ippdrv->dev, property)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900258 DRM_DEBUG_KMS("not support property.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900259 continue;
260 }
261
262 return ippdrv;
263 }
264
265 DRM_ERROR("not support ipp driver operations.\n");
266 }
267
268 return ERR_PTR(-ENODEV);
269}
270
271static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
272{
273 struct exynos_drm_ippdrv *ippdrv;
274 struct drm_exynos_ipp_cmd_node *c_node;
275 int count = 0;
276
YoungJun Chocbc4c332013-06-12 10:44:40 +0900277 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900278
Eunchul Kimcb471f142012-12-14 18:10:31 +0900279 /*
280 * This case is search ipp driver by prop_id handle.
281 * sometimes, ipp subsystem find driver by prop_id.
282 * e.g PAUSE state, queue buf, command contro.
283 */
284 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900285 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900286
YoungJun Cho7f5af052014-05-26 10:17:18 +0200287 mutex_lock(&ippdrv->cmd_lock);
288 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
289 if (c_node->property.prop_id == prop_id) {
290 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Choc66ce402014-05-26 10:17:15 +0200291 return ippdrv;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200292 }
293 }
294 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900295 }
296
297 return ERR_PTR(-ENODEV);
298}
299
300int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
301 struct drm_file *file)
302{
303 struct drm_exynos_file_private *file_priv = file->driver_priv;
304 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
305 struct device *dev = priv->dev;
306 struct ipp_context *ctx = get_ipp_context(dev);
307 struct drm_exynos_ipp_prop_list *prop_list = data;
308 struct exynos_drm_ippdrv *ippdrv;
309 int count = 0;
310
Eunchul Kimcb471f142012-12-14 18:10:31 +0900311 if (!ctx) {
312 DRM_ERROR("invalid context.\n");
313 return -EINVAL;
314 }
315
316 if (!prop_list) {
317 DRM_ERROR("invalid property parameter.\n");
318 return -EINVAL;
319 }
320
YoungJun Chocbc4c332013-06-12 10:44:40 +0900321 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900322
323 if (!prop_list->ipp_id) {
324 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
325 count++;
YoungJun Cho7f5af052014-05-26 10:17:18 +0200326
Eunchul Kimcb471f142012-12-14 18:10:31 +0900327 /*
328 * Supports ippdrv list count for user application.
329 * First step user application getting ippdrv count.
330 * and second step getting ippdrv capability using ipp_id.
331 */
332 prop_list->count = count;
333 } else {
334 /*
335 * Getting ippdrv capability by ipp_id.
Masanari Iidac6b78bc2013-10-24 16:02:57 +0900336 * some device not supported wb, output interface.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900337 * so, user application detect correct ipp driver
338 * using this ioctl.
339 */
340 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
341 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800342 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900343 DRM_ERROR("not found ipp%d driver.\n",
344 prop_list->ipp_id);
Wei Yongjunbe348792013-07-04 21:35:00 +0800345 return PTR_ERR(ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900346 }
347
Andrzej Hajda31646052014-05-19 12:54:05 +0200348 *prop_list = ippdrv->prop_list;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900349 }
350
351 return 0;
352}
353
354static void ipp_print_property(struct drm_exynos_ipp_property *property,
355 int idx)
356{
357 struct drm_exynos_ipp_config *config = &property->config[idx];
358 struct drm_exynos_pos *pos = &config->pos;
359 struct drm_exynos_sz *sz = &config->sz;
360
YoungJun Chocbc4c332013-06-12 10:44:40 +0900361 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
362 property->prop_id, idx ? "dst" : "src", config->fmt);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900363
YoungJun Chocbc4c332013-06-12 10:44:40 +0900364 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
365 pos->x, pos->y, pos->w, pos->h,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900366 sz->hsize, sz->vsize, config->flip, config->degree);
367}
368
369static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
370{
371 struct exynos_drm_ippdrv *ippdrv;
372 struct drm_exynos_ipp_cmd_node *c_node;
373 u32 prop_id = property->prop_id;
374
YoungJun Chocbc4c332013-06-12 10:44:40 +0900375 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900376
377 ippdrv = ipp_find_drv_by_handle(prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530378 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900379 DRM_ERROR("failed to get ipp driver.\n");
380 return -EINVAL;
381 }
382
383 /*
384 * Find command node using command list in ippdrv.
385 * when we find this command no using prop_id.
386 * return property information set in this command node.
387 */
YoungJun Cho7f5af052014-05-26 10:17:18 +0200388 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900389 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
390 if ((c_node->property.prop_id == prop_id) &&
391 (c_node->state == IPP_STATE_STOP)) {
YoungJun Cho7f5af052014-05-26 10:17:18 +0200392 mutex_unlock(&ippdrv->cmd_lock);
YoungJun Chocbc4c332013-06-12 10:44:40 +0900393 DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
394 property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900395
396 c_node->property = *property;
397 return 0;
398 }
399 }
YoungJun Cho7f5af052014-05-26 10:17:18 +0200400 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900401
402 DRM_ERROR("failed to search property.\n");
403
404 return -EINVAL;
405}
406
407static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
408{
409 struct drm_exynos_ipp_cmd_work *cmd_work;
410
Eunchul Kimcb471f142012-12-14 18:10:31 +0900411 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900412 if (!cmd_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900413 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900414
415 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
416
417 return cmd_work;
418}
419
420static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
421{
422 struct drm_exynos_ipp_event_work *event_work;
423
Eunchul Kimcb471f142012-12-14 18:10:31 +0900424 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900425 if (!event_work)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900426 return ERR_PTR(-ENOMEM);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900427
428 INIT_WORK((struct work_struct *)event_work, ipp_sched_event);
429
430 return event_work;
431}
432
433int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
434 struct drm_file *file)
435{
436 struct drm_exynos_file_private *file_priv = file->driver_priv;
437 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
438 struct device *dev = priv->dev;
439 struct ipp_context *ctx = get_ipp_context(dev);
440 struct drm_exynos_ipp_property *property = data;
441 struct exynos_drm_ippdrv *ippdrv;
442 struct drm_exynos_ipp_cmd_node *c_node;
443 int ret, i;
444
Eunchul Kimcb471f142012-12-14 18:10:31 +0900445 if (!ctx) {
446 DRM_ERROR("invalid context.\n");
447 return -EINVAL;
448 }
449
450 if (!property) {
451 DRM_ERROR("invalid property parameter.\n");
452 return -EINVAL;
453 }
454
455 /*
456 * This is log print for user application property.
457 * user application set various property.
458 */
459 for_each_ipp_ops(i)
460 ipp_print_property(property, i);
461
462 /*
463 * set property ioctl generated new prop_id.
464 * but in this case already asigned prop_id using old set property.
465 * e.g PAUSE state. this case supports find current prop_id and use it
466 * instead of allocation.
467 */
468 if (property->prop_id) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900469 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900470 return ipp_find_and_set_property(property);
471 }
472
473 /* find ipp driver using ipp id */
474 ippdrv = ipp_find_driver(ctx, property);
Sachin Kamatf0250452013-04-29 12:27:06 +0530475 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900476 DRM_ERROR("failed to get ipp driver.\n");
477 return -EINVAL;
478 }
479
480 /* allocate command node */
481 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900482 if (!c_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900483 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900484
485 /* create property id */
486 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node,
487 &property->prop_id);
488 if (ret) {
489 DRM_ERROR("failed to create id.\n");
490 goto err_clear;
491 }
492
YoungJun Chocbc4c332013-06-12 10:44:40 +0900493 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
494 property->prop_id, property->cmd, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900495
496 /* stored property information and ippdrv in private data */
497 c_node->priv = priv;
498 c_node->property = *property;
499 c_node->state = IPP_STATE_IDLE;
500
501 c_node->start_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530502 if (IS_ERR(c_node->start_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900503 DRM_ERROR("failed to create start work.\n");
504 goto err_clear;
505 }
506
507 c_node->stop_work = ipp_create_cmd_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530508 if (IS_ERR(c_node->stop_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900509 DRM_ERROR("failed to create stop work.\n");
510 goto err_free_start;
511 }
512
513 c_node->event_work = ipp_create_event_work();
Sachin Kamatf0250452013-04-29 12:27:06 +0530514 if (IS_ERR(c_node->event_work)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900515 DRM_ERROR("failed to create event work.\n");
516 goto err_free_stop;
517 }
518
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200519 mutex_init(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900520 mutex_init(&c_node->mem_lock);
521 mutex_init(&c_node->event_lock);
522
523 init_completion(&c_node->start_complete);
524 init_completion(&c_node->stop_complete);
525
526 for_each_ipp_ops(i)
527 INIT_LIST_HEAD(&c_node->mem_list[i]);
528
529 INIT_LIST_HEAD(&c_node->event_list);
530 list_splice_init(&priv->event_list, &c_node->event_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200531 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900532 list_add_tail(&c_node->list, &ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +0200533 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900534
535 /* make dedicated state without m2m */
536 if (!ipp_is_m2m_cmd(property->cmd))
537 ippdrv->dedicated = true;
538
539 return 0;
540
541err_free_stop:
542 kfree(c_node->stop_work);
543err_free_start:
544 kfree(c_node->start_work);
545err_clear:
546 kfree(c_node);
547 return ret;
548}
549
550static void ipp_clean_cmd_node(struct drm_exynos_ipp_cmd_node *c_node)
551{
Eunchul Kimcb471f142012-12-14 18:10:31 +0900552 /* delete list */
553 list_del(&c_node->list);
554
555 /* destroy mutex */
YoungJun Cho4e4fe552014-05-26 10:17:17 +0200556 mutex_destroy(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900557 mutex_destroy(&c_node->mem_lock);
558 mutex_destroy(&c_node->event_lock);
559
560 /* free command node */
561 kfree(c_node->start_work);
562 kfree(c_node->stop_work);
563 kfree(c_node->event_work);
564 kfree(c_node);
565}
566
567static int ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
568{
569 struct drm_exynos_ipp_property *property = &c_node->property;
570 struct drm_exynos_ipp_mem_node *m_node;
571 struct list_head *head;
572 int ret, i, count[EXYNOS_DRM_OPS_MAX] = { 0, };
573
Eunchul Kimcb471f142012-12-14 18:10:31 +0900574 mutex_lock(&c_node->mem_lock);
575
576 for_each_ipp_ops(i) {
577 /* source/destination memory list */
578 head = &c_node->mem_list[i];
579
Eunchul Kimcb471f142012-12-14 18:10:31 +0900580 /* find memory node entry */
581 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900582 DRM_DEBUG_KMS("%s,count[%d]m_node[0x%x]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900583 i ? "dst" : "src", count[i], (int)m_node);
584 count[i]++;
585 }
586 }
587
YoungJun Chocbc4c332013-06-12 10:44:40 +0900588 DRM_DEBUG_KMS("min[%d]max[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +0900589 min(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]),
590 max(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]));
591
592 /*
593 * M2M operations should be need paired memory address.
594 * so, need to check minimum count about src, dst.
595 * other case not use paired memory, so use maximum count
596 */
597 if (ipp_is_m2m_cmd(property->cmd))
598 ret = min(count[EXYNOS_DRM_OPS_SRC],
599 count[EXYNOS_DRM_OPS_DST]);
600 else
601 ret = max(count[EXYNOS_DRM_OPS_SRC],
602 count[EXYNOS_DRM_OPS_DST]);
603
604 mutex_unlock(&c_node->mem_lock);
605
606 return ret;
607}
608
609static struct drm_exynos_ipp_mem_node
610 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
611 struct drm_exynos_ipp_queue_buf *qbuf)
612{
613 struct drm_exynos_ipp_mem_node *m_node;
614 struct list_head *head;
615 int count = 0;
616
YoungJun Chocbc4c332013-06-12 10:44:40 +0900617 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900618
619 /* source/destination memory list */
620 head = &c_node->mem_list[qbuf->ops_id];
621
622 /* find memory node from memory list */
623 list_for_each_entry(m_node, head, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900624 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900625
626 /* compare buffer id */
627 if (m_node->buf_id == qbuf->buf_id)
628 return m_node;
629 }
630
631 return NULL;
632}
633
634static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
635 struct drm_exynos_ipp_cmd_node *c_node,
636 struct drm_exynos_ipp_mem_node *m_node)
637{
638 struct exynos_drm_ipp_ops *ops = NULL;
639 int ret = 0;
640
YoungJun Chocbc4c332013-06-12 10:44:40 +0900641 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900642
643 if (!m_node) {
644 DRM_ERROR("invalid queue node.\n");
645 return -EFAULT;
646 }
647
648 mutex_lock(&c_node->mem_lock);
649
YoungJun Chocbc4c332013-06-12 10:44:40 +0900650 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900651
652 /* get operations callback */
653 ops = ippdrv->ops[m_node->ops_id];
654 if (!ops) {
655 DRM_ERROR("not support ops.\n");
656 ret = -EFAULT;
657 goto err_unlock;
658 }
659
660 /* set address and enable irq */
661 if (ops->set_addr) {
662 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
663 m_node->buf_id, IPP_BUF_ENQUEUE);
664 if (ret) {
665 DRM_ERROR("failed to set addr.\n");
666 goto err_unlock;
667 }
668 }
669
670err_unlock:
671 mutex_unlock(&c_node->mem_lock);
672 return ret;
673}
674
675static struct drm_exynos_ipp_mem_node
676 *ipp_get_mem_node(struct drm_device *drm_dev,
677 struct drm_file *file,
678 struct drm_exynos_ipp_cmd_node *c_node,
679 struct drm_exynos_ipp_queue_buf *qbuf)
680{
681 struct drm_exynos_ipp_mem_node *m_node;
682 struct drm_exynos_ipp_buf_info buf_info;
683 void *addr;
684 int i;
685
Eunchul Kimcb471f142012-12-14 18:10:31 +0900686 mutex_lock(&c_node->mem_lock);
687
688 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900689 if (!m_node)
Eunchul Kimcb471f142012-12-14 18:10:31 +0900690 goto err_unlock;
Eunchul Kimcb471f142012-12-14 18:10:31 +0900691
692 /* clear base address for error handling */
693 memset(&buf_info, 0x0, sizeof(buf_info));
694
695 /* operations, buffer id */
696 m_node->ops_id = qbuf->ops_id;
697 m_node->prop_id = qbuf->prop_id;
698 m_node->buf_id = qbuf->buf_id;
699
YoungJun Chocbc4c332013-06-12 10:44:40 +0900700 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
701 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900702
703 for_each_ipp_planar(i) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900704 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900705
706 /* get dma address by handle */
707 if (qbuf->handle[i]) {
708 addr = exynos_drm_gem_get_dma_addr(drm_dev,
709 qbuf->handle[i], file);
710 if (IS_ERR(addr)) {
711 DRM_ERROR("failed to get addr.\n");
712 goto err_clear;
713 }
714
715 buf_info.handles[i] = qbuf->handle[i];
716 buf_info.base[i] = *(dma_addr_t *) addr;
YoungJun Chocbc4c332013-06-12 10:44:40 +0900717 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%x]\n",
718 i, buf_info.base[i], (int)buf_info.handles[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900719 }
720 }
721
722 m_node->filp = file;
723 m_node->buf_info = buf_info;
724 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
725
726 mutex_unlock(&c_node->mem_lock);
727 return m_node;
728
729err_clear:
730 kfree(m_node);
731err_unlock:
732 mutex_unlock(&c_node->mem_lock);
733 return ERR_PTR(-EFAULT);
734}
735
736static int ipp_put_mem_node(struct drm_device *drm_dev,
737 struct drm_exynos_ipp_cmd_node *c_node,
738 struct drm_exynos_ipp_mem_node *m_node)
739{
740 int i;
741
YoungJun Chocbc4c332013-06-12 10:44:40 +0900742 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900743
744 if (!m_node) {
745 DRM_ERROR("invalid dequeue node.\n");
746 return -EFAULT;
747 }
748
749 if (list_empty(&m_node->list)) {
750 DRM_ERROR("empty memory node.\n");
751 return -ENOMEM;
752 }
753
754 mutex_lock(&c_node->mem_lock);
755
YoungJun Chocbc4c332013-06-12 10:44:40 +0900756 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900757
758 /* put gem buffer */
759 for_each_ipp_planar(i) {
760 unsigned long handle = m_node->buf_info.handles[i];
761 if (handle)
762 exynos_drm_gem_put_dma_addr(drm_dev, handle,
763 m_node->filp);
764 }
765
766 /* delete list in queue */
767 list_del(&m_node->list);
768 kfree(m_node);
769
770 mutex_unlock(&c_node->mem_lock);
771
772 return 0;
773}
774
775static void ipp_free_event(struct drm_pending_event *event)
776{
777 kfree(event);
778}
779
780static int ipp_get_event(struct drm_device *drm_dev,
781 struct drm_file *file,
782 struct drm_exynos_ipp_cmd_node *c_node,
783 struct drm_exynos_ipp_queue_buf *qbuf)
784{
785 struct drm_exynos_ipp_send_event *e;
786 unsigned long flags;
787
YoungJun Chocbc4c332013-06-12 10:44:40 +0900788 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900789
790 e = kzalloc(sizeof(*e), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900791 if (!e) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900792 spin_lock_irqsave(&drm_dev->event_lock, flags);
793 file->event_space += sizeof(e->event);
794 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
795 return -ENOMEM;
796 }
797
798 /* make event */
799 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
800 e->event.base.length = sizeof(e->event);
801 e->event.user_data = qbuf->user_data;
802 e->event.prop_id = qbuf->prop_id;
803 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
804 e->base.event = &e->event.base;
805 e->base.file_priv = file;
806 e->base.destroy = ipp_free_event;
807 list_add_tail(&e->base.link, &c_node->event_list);
808
809 return 0;
810}
811
812static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
813 struct drm_exynos_ipp_queue_buf *qbuf)
814{
815 struct drm_exynos_ipp_send_event *e, *te;
816 int count = 0;
817
Eunchul Kimcb471f142012-12-14 18:10:31 +0900818 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900819 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
Eunchul Kimcb471f142012-12-14 18:10:31 +0900820
821 /*
Sachin Kamat4fe25b82014-01-16 10:00:23 +0530822 * qbuf == NULL condition means all event deletion.
Eunchul Kimcb471f142012-12-14 18:10:31 +0900823 * stop operations want to delete all event list.
824 * another case delete only same buf id.
825 */
826 if (!qbuf) {
827 /* delete list */
828 list_del(&e->base.link);
829 kfree(e);
830 }
831
832 /* compare buffer id */
833 if (qbuf && (qbuf->buf_id ==
834 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
835 /* delete list */
836 list_del(&e->base.link);
837 kfree(e);
838 return;
839 }
840 }
841}
842
Sachin Kamat0bc4a0a2013-01-14 12:29:10 +0530843static void ipp_handle_cmd_work(struct device *dev,
Eunchul Kimcb471f142012-12-14 18:10:31 +0900844 struct exynos_drm_ippdrv *ippdrv,
845 struct drm_exynos_ipp_cmd_work *cmd_work,
846 struct drm_exynos_ipp_cmd_node *c_node)
847{
848 struct ipp_context *ctx = get_ipp_context(dev);
849
850 cmd_work->ippdrv = ippdrv;
851 cmd_work->c_node = c_node;
852 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
853}
854
855static int ipp_queue_buf_with_run(struct device *dev,
856 struct drm_exynos_ipp_cmd_node *c_node,
857 struct drm_exynos_ipp_mem_node *m_node,
858 struct drm_exynos_ipp_queue_buf *qbuf)
859{
860 struct exynos_drm_ippdrv *ippdrv;
861 struct drm_exynos_ipp_property *property;
862 struct exynos_drm_ipp_ops *ops;
863 int ret;
864
Eunchul Kimcb471f142012-12-14 18:10:31 +0900865 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
Sachin Kamatf0250452013-04-29 12:27:06 +0530866 if (IS_ERR(ippdrv)) {
Eunchul Kimcb471f142012-12-14 18:10:31 +0900867 DRM_ERROR("failed to get ipp driver.\n");
868 return -EFAULT;
869 }
870
871 ops = ippdrv->ops[qbuf->ops_id];
872 if (!ops) {
873 DRM_ERROR("failed to get ops.\n");
874 return -EFAULT;
875 }
876
877 property = &c_node->property;
878
879 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900880 DRM_DEBUG_KMS("bypass for invalid state.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900881 return 0;
882 }
883
884 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +0900885 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +0900886 return 0;
887 }
888
889 /*
890 * If set destination buffer and enabled clock,
891 * then m2m operations need start operations at queue_buf
892 */
893 if (ipp_is_m2m_cmd(property->cmd)) {
894 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
895
896 cmd_work->ctrl = IPP_CTRL_PLAY;
897 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
898 } else {
899 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
900 if (ret) {
901 DRM_ERROR("failed to set m node.\n");
902 return ret;
903 }
904 }
905
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 */
916 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 }
922}
923
924int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
925 struct drm_file *file)
926{
927 struct drm_exynos_file_private *file_priv = file->driver_priv;
928 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
929 struct device *dev = priv->dev;
930 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;
1060 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1061 struct exynos_drm_ippdrv *ippdrv = NULL;
1062 struct device *dev = priv->dev;
1063 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);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001125 ipp_clean_cmd_node(c_node);
1126
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
1257 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001258 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001259 return -ENOMEM;
1260 }
1261
1262 /* set current property in ippdrv */
1263 ret = ipp_set_property(ippdrv, property);
1264 if (ret) {
1265 DRM_ERROR("failed to set property.\n");
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001266 ippdrv->c_node = NULL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001267 return ret;
1268 }
1269
1270 /* check command */
1271 switch (property->cmd) {
1272 case IPP_CMD_M2M:
1273 for_each_ipp_ops(i) {
1274 /* source/destination memory list */
1275 head = &c_node->mem_list[i];
1276
1277 m_node = list_first_entry(head,
1278 struct drm_exynos_ipp_mem_node, list);
1279 if (!m_node) {
1280 DRM_ERROR("failed to get node.\n");
1281 ret = -EFAULT;
1282 return ret;
1283 }
1284
YoungJun Chocbc4c332013-06-12 10:44:40 +09001285 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001286
1287 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1288 if (ret) {
1289 DRM_ERROR("failed to set m node.\n");
1290 return ret;
1291 }
1292 }
1293 break;
1294 case IPP_CMD_WB:
1295 /* destination memory list */
1296 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1297
1298 list_for_each_entry(m_node, head, list) {
1299 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1300 if (ret) {
1301 DRM_ERROR("failed to set m node.\n");
1302 return ret;
1303 }
1304 }
1305 break;
1306 case IPP_CMD_OUTPUT:
1307 /* source memory list */
1308 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1309
1310 list_for_each_entry(m_node, head, list) {
1311 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1312 if (ret) {
1313 DRM_ERROR("failed to set m node.\n");
1314 return ret;
1315 }
1316 }
1317 break;
1318 default:
1319 DRM_ERROR("invalid operations.\n");
1320 return -EINVAL;
1321 }
1322
YoungJun Chocbc4c332013-06-12 10:44:40 +09001323 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001324
1325 /* start operations */
1326 if (ippdrv->start) {
1327 ret = ippdrv->start(ippdrv->dev, property->cmd);
1328 if (ret) {
1329 DRM_ERROR("failed to start ops.\n");
1330 return ret;
1331 }
1332 }
1333
1334 return 0;
1335}
1336
1337static int ipp_stop_property(struct drm_device *drm_dev,
1338 struct exynos_drm_ippdrv *ippdrv,
1339 struct drm_exynos_ipp_cmd_node *c_node)
1340{
1341 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1342 struct drm_exynos_ipp_property *property = &c_node->property;
1343 struct list_head *head;
1344 int ret = 0, i;
1345
YoungJun Chocbc4c332013-06-12 10:44:40 +09001346 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001347
1348 /* put event */
1349 ipp_put_event(c_node, NULL);
1350
1351 /* check command */
1352 switch (property->cmd) {
1353 case IPP_CMD_M2M:
1354 for_each_ipp_ops(i) {
1355 /* source/destination memory list */
1356 head = &c_node->mem_list[i];
1357
Eunchul Kimcb471f142012-12-14 18:10:31 +09001358 list_for_each_entry_safe(m_node, tm_node,
1359 head, list) {
1360 ret = ipp_put_mem_node(drm_dev, c_node,
1361 m_node);
1362 if (ret) {
1363 DRM_ERROR("failed to put m_node.\n");
1364 goto err_clear;
1365 }
1366 }
1367 }
1368 break;
1369 case IPP_CMD_WB:
1370 /* destination memory list */
1371 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1372
Eunchul Kimcb471f142012-12-14 18:10:31 +09001373 list_for_each_entry_safe(m_node, tm_node, head, list) {
1374 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1375 if (ret) {
1376 DRM_ERROR("failed to put m_node.\n");
1377 goto err_clear;
1378 }
1379 }
1380 break;
1381 case IPP_CMD_OUTPUT:
1382 /* source memory list */
1383 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
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 default:
1394 DRM_ERROR("invalid operations.\n");
1395 ret = -EINVAL;
1396 goto err_clear;
1397 }
1398
1399err_clear:
1400 /* stop operations */
1401 if (ippdrv->stop)
1402 ippdrv->stop(ippdrv->dev, property->cmd);
1403
1404 return ret;
1405}
1406
1407void ipp_sched_cmd(struct work_struct *work)
1408{
1409 struct drm_exynos_ipp_cmd_work *cmd_work =
1410 (struct drm_exynos_ipp_cmd_work *)work;
1411 struct exynos_drm_ippdrv *ippdrv;
1412 struct drm_exynos_ipp_cmd_node *c_node;
1413 struct drm_exynos_ipp_property *property;
1414 int ret;
1415
Eunchul Kimcb471f142012-12-14 18:10:31 +09001416 ippdrv = cmd_work->ippdrv;
1417 if (!ippdrv) {
1418 DRM_ERROR("invalid ippdrv list.\n");
1419 return;
1420 }
1421
1422 c_node = cmd_work->c_node;
1423 if (!c_node) {
1424 DRM_ERROR("invalid command node list.\n");
1425 return;
1426 }
1427
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001428 mutex_lock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001429
1430 property = &c_node->property;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001431
1432 switch (cmd_work->ctrl) {
1433 case IPP_CTRL_PLAY:
1434 case IPP_CTRL_RESUME:
1435 ret = ipp_start_property(ippdrv, c_node);
1436 if (ret) {
1437 DRM_ERROR("failed to start property:prop_id[%d]\n",
1438 c_node->property.prop_id);
1439 goto err_unlock;
1440 }
1441
1442 /*
1443 * M2M case supports wait_completion of transfer.
1444 * because M2M case supports single unit operation
1445 * with multiple queue.
1446 * M2M need to wait completion of data transfer.
1447 */
1448 if (ipp_is_m2m_cmd(property->cmd)) {
1449 if (!wait_for_completion_timeout
1450 (&c_node->start_complete, msecs_to_jiffies(200))) {
1451 DRM_ERROR("timeout event:prop_id[%d]\n",
1452 c_node->property.prop_id);
1453 goto err_unlock;
1454 }
1455 }
1456 break;
1457 case IPP_CTRL_STOP:
1458 case IPP_CTRL_PAUSE:
1459 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1460 c_node);
1461 if (ret) {
1462 DRM_ERROR("failed to stop property.\n");
1463 goto err_unlock;
1464 }
1465
1466 complete(&c_node->stop_complete);
1467 break;
1468 default:
1469 DRM_ERROR("unknown control type\n");
1470 break;
1471 }
1472
YoungJun Chocbc4c332013-06-12 10:44:40 +09001473 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001474
1475err_unlock:
YoungJun Cho4e4fe552014-05-26 10:17:17 +02001476 mutex_unlock(&c_node->lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001477}
1478
1479static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1480 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1481{
1482 struct drm_device *drm_dev = ippdrv->drm_dev;
1483 struct drm_exynos_ipp_property *property = &c_node->property;
1484 struct drm_exynos_ipp_mem_node *m_node;
1485 struct drm_exynos_ipp_queue_buf qbuf;
1486 struct drm_exynos_ipp_send_event *e;
1487 struct list_head *head;
1488 struct timeval now;
1489 unsigned long flags;
1490 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1491 int ret, i;
1492
1493 for_each_ipp_ops(i)
YoungJun Chocbc4c332013-06-12 10:44:40 +09001494 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001495
1496 if (!drm_dev) {
1497 DRM_ERROR("failed to get drm_dev.\n");
1498 return -EINVAL;
1499 }
1500
1501 if (!property) {
1502 DRM_ERROR("failed to get property.\n");
1503 return -EINVAL;
1504 }
1505
1506 if (list_empty(&c_node->event_list)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001507 DRM_DEBUG_KMS("event list is empty.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001508 return 0;
1509 }
1510
1511 if (!ipp_check_mem_list(c_node)) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001512 DRM_DEBUG_KMS("empty memory.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001513 return 0;
1514 }
1515
1516 /* check command */
1517 switch (property->cmd) {
1518 case IPP_CMD_M2M:
1519 for_each_ipp_ops(i) {
1520 /* source/destination memory list */
1521 head = &c_node->mem_list[i];
1522
1523 m_node = list_first_entry(head,
1524 struct drm_exynos_ipp_mem_node, list);
1525 if (!m_node) {
1526 DRM_ERROR("empty memory node.\n");
1527 return -ENOMEM;
1528 }
1529
1530 tbuf_id[i] = m_node->buf_id;
YoungJun Chocbc4c332013-06-12 10:44:40 +09001531 DRM_DEBUG_KMS("%s buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001532 i ? "dst" : "src", tbuf_id[i]);
1533
1534 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1535 if (ret)
1536 DRM_ERROR("failed to put m_node.\n");
1537 }
1538 break;
1539 case IPP_CMD_WB:
1540 /* clear buf for finding */
1541 memset(&qbuf, 0x0, sizeof(qbuf));
1542 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1543 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1544
1545 /* get memory node entry */
1546 m_node = ipp_find_mem_node(c_node, &qbuf);
1547 if (!m_node) {
1548 DRM_ERROR("empty memory node.\n");
1549 return -ENOMEM;
1550 }
1551
1552 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1553
1554 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1555 if (ret)
1556 DRM_ERROR("failed to put m_node.\n");
1557 break;
1558 case IPP_CMD_OUTPUT:
1559 /* source memory list */
1560 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1561
1562 m_node = list_first_entry(head,
1563 struct drm_exynos_ipp_mem_node, list);
1564 if (!m_node) {
1565 DRM_ERROR("empty memory node.\n");
1566 return -ENOMEM;
1567 }
1568
1569 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1570
1571 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1572 if (ret)
1573 DRM_ERROR("failed to put m_node.\n");
1574 break;
1575 default:
1576 DRM_ERROR("invalid operations.\n");
1577 return -EINVAL;
1578 }
1579
1580 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1581 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1582 tbuf_id[1], buf_id[1], property->prop_id);
1583
1584 /*
1585 * command node have event list of destination buffer
1586 * If destination buffer enqueue to mem list,
1587 * then we make event and link to event list tail.
1588 * so, we get first event for first enqueued buffer.
1589 */
1590 e = list_first_entry(&c_node->event_list,
1591 struct drm_exynos_ipp_send_event, base.link);
1592
1593 if (!e) {
1594 DRM_ERROR("empty event.\n");
1595 return -EINVAL;
1596 }
1597
1598 do_gettimeofday(&now);
YoungJun Chocbc4c332013-06-12 10:44:40 +09001599 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001600 e->event.tv_sec = now.tv_sec;
1601 e->event.tv_usec = now.tv_usec;
1602 e->event.prop_id = property->prop_id;
1603
1604 /* set buffer id about source destination */
1605 for_each_ipp_ops(i)
1606 e->event.buf_id[i] = tbuf_id[i];
1607
1608 spin_lock_irqsave(&drm_dev->event_lock, flags);
1609 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1610 wake_up_interruptible(&e->base.file_priv->event_wait);
1611 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1612
YoungJun Chocbc4c332013-06-12 10:44:40 +09001613 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
Eunchul Kimcb471f142012-12-14 18:10:31 +09001614 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1615
1616 return 0;
1617}
1618
1619void ipp_sched_event(struct work_struct *work)
1620{
1621 struct drm_exynos_ipp_event_work *event_work =
1622 (struct drm_exynos_ipp_event_work *)work;
1623 struct exynos_drm_ippdrv *ippdrv;
1624 struct drm_exynos_ipp_cmd_node *c_node;
1625 int ret;
1626
1627 if (!event_work) {
1628 DRM_ERROR("failed to get event_work.\n");
1629 return;
1630 }
1631
YoungJun Chocbc4c332013-06-12 10:44:40 +09001632 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001633
1634 ippdrv = event_work->ippdrv;
1635 if (!ippdrv) {
1636 DRM_ERROR("failed to get ipp driver.\n");
1637 return;
1638 }
1639
Eunchul Kim7259c3d2012-12-22 17:49:22 +09001640 c_node = ippdrv->c_node;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001641 if (!c_node) {
1642 DRM_ERROR("failed to get command node.\n");
1643 return;
1644 }
1645
1646 /*
1647 * IPP supports command thread, event thread synchronization.
1648 * If IPP close immediately from user land, then IPP make
1649 * synchronization with command thread, so make complete event.
1650 * or going out operations.
1651 */
1652 if (c_node->state != IPP_STATE_START) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001653 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1654 c_node->state, c_node->property.prop_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001655 goto err_completion;
1656 }
1657
1658 mutex_lock(&c_node->event_lock);
1659
1660 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1661 if (ret) {
1662 DRM_ERROR("failed to send event.\n");
1663 goto err_completion;
1664 }
1665
1666err_completion:
1667 if (ipp_is_m2m_cmd(c_node->property.cmd))
1668 complete(&c_node->start_complete);
1669
1670 mutex_unlock(&c_node->event_lock);
1671}
1672
1673static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1674{
1675 struct ipp_context *ctx = get_ipp_context(dev);
1676 struct exynos_drm_ippdrv *ippdrv;
1677 int ret, count = 0;
1678
Eunchul Kimcb471f142012-12-14 18:10:31 +09001679 /* get ipp driver entry */
1680 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001681 u32 ipp_id;
1682
Eunchul Kimcb471f142012-12-14 18:10:31 +09001683 ippdrv->drm_dev = drm_dev;
1684
1685 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001686 &ipp_id);
1687 if (ret || ipp_id == 0) {
Eunchul Kimcb471f142012-12-14 18:10:31 +09001688 DRM_ERROR("failed to create id.\n");
1689 goto err_idr;
1690 }
1691
YoungJun Chocbc4c332013-06-12 10:44:40 +09001692 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
Andrzej Hajdaf51bcee2014-05-19 12:54:04 +02001693 count++, (int)ippdrv, ipp_id);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001694
Andrzej Hajda31646052014-05-19 12:54:05 +02001695 ippdrv->prop_list.ipp_id = ipp_id;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001696
1697 /* store parent device for node */
1698 ippdrv->parent_dev = dev;
1699
1700 /* store event work queue and handler */
1701 ippdrv->event_workq = ctx->event_workq;
1702 ippdrv->sched_event = ipp_sched_event;
1703 INIT_LIST_HEAD(&ippdrv->cmd_list);
YoungJun Cho7f5af052014-05-26 10:17:18 +02001704 mutex_init(&ippdrv->cmd_lock);
Eunchul Kimc12e2612012-12-14 17:58:54 +09001705
1706 if (is_drm_iommu_supported(drm_dev)) {
1707 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1708 if (ret) {
1709 DRM_ERROR("failed to activate iommu\n");
1710 goto err_iommu;
1711 }
1712 }
Eunchul Kimcb471f142012-12-14 18:10:31 +09001713 }
1714
1715 return 0;
1716
Eunchul Kimc12e2612012-12-14 17:58:54 +09001717err_iommu:
1718 /* get ipp driver entry */
1719 list_for_each_entry_reverse(ippdrv, &exynos_drm_ippdrv_list, drv_list)
1720 if (is_drm_iommu_supported(drm_dev))
1721 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1722
Eunchul Kimcb471f142012-12-14 18:10:31 +09001723err_idr:
Eunchul Kimcb471f142012-12-14 18:10:31 +09001724 idr_destroy(&ctx->ipp_idr);
1725 idr_destroy(&ctx->prop_idr);
1726 return ret;
1727}
1728
1729static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1730{
1731 struct exynos_drm_ippdrv *ippdrv;
1732
Eunchul Kimcb471f142012-12-14 18:10:31 +09001733 /* get ipp driver entry */
1734 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
Eunchul Kimc12e2612012-12-14 17:58:54 +09001735 if (is_drm_iommu_supported(drm_dev))
1736 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1737
Eunchul Kimcb471f142012-12-14 18:10:31 +09001738 ippdrv->drm_dev = NULL;
1739 exynos_drm_ippdrv_unregister(ippdrv);
1740 }
1741}
1742
1743static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1744 struct drm_file *file)
1745{
1746 struct drm_exynos_file_private *file_priv = file->driver_priv;
1747 struct exynos_drm_ipp_private *priv;
1748
Eunchul Kimcb471f142012-12-14 18:10:31 +09001749 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +09001750 if (!priv)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001751 return -ENOMEM;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001752 priv->dev = dev;
1753 file_priv->ipp_priv = priv;
1754
1755 INIT_LIST_HEAD(&priv->event_list);
1756
YoungJun Chocbc4c332013-06-12 10:44:40 +09001757 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)priv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001758
1759 return 0;
1760}
1761
1762static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1763 struct drm_file *file)
1764{
1765 struct drm_exynos_file_private *file_priv = file->driver_priv;
1766 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1767 struct exynos_drm_ippdrv *ippdrv = NULL;
1768 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1769 int count = 0;
1770
YoungJun Chocbc4c332013-06-12 10:44:40 +09001771 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)priv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001772
Eunchul Kimcb471f142012-12-14 18:10:31 +09001773 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
YoungJun Cho7f5af052014-05-26 10:17:18 +02001774 mutex_lock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001775 list_for_each_entry_safe(c_node, tc_node,
1776 &ippdrv->cmd_list, list) {
YoungJun Chocbc4c332013-06-12 10:44:40 +09001777 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1778 count++, (int)ippdrv);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001779
1780 if (c_node->priv == priv) {
1781 /*
1782 * userland goto unnormal state. process killed.
1783 * and close the file.
1784 * so, IPP didn't called stop cmd ctrl.
1785 * so, we are make stop operation in this state.
1786 */
1787 if (c_node->state == IPP_STATE_START) {
1788 ipp_stop_property(drm_dev, ippdrv,
1789 c_node);
1790 c_node->state = IPP_STATE_STOP;
1791 }
1792
1793 ippdrv->dedicated = false;
1794 ipp_clean_cmd_node(c_node);
1795 if (list_empty(&ippdrv->cmd_list))
1796 pm_runtime_put_sync(ippdrv->dev);
1797 }
1798 }
YoungJun Cho7f5af052014-05-26 10:17:18 +02001799 mutex_unlock(&ippdrv->cmd_lock);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001800 }
1801
Eunchul Kimcb471f142012-12-14 18:10:31 +09001802 kfree(priv);
1803 return;
1804}
1805
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001806static int ipp_probe(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001807{
1808 struct device *dev = &pdev->dev;
1809 struct ipp_context *ctx;
1810 struct exynos_drm_subdrv *subdrv;
1811 int ret;
1812
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001813 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001814 if (!ctx)
1815 return -ENOMEM;
1816
Eunchul Kimcb471f142012-12-14 18:10:31 +09001817 mutex_init(&ctx->ipp_lock);
1818 mutex_init(&ctx->prop_lock);
1819
1820 idr_init(&ctx->ipp_idr);
1821 idr_init(&ctx->prop_idr);
1822
1823 /*
1824 * create single thread for ipp event
1825 * IPP supports event thread for IPP drivers.
1826 * IPP driver send event_work to this thread.
1827 * and IPP event thread send event to user process.
1828 */
1829 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1830 if (!ctx->event_workq) {
1831 dev_err(dev, "failed to create event workqueue\n");
Sachin Kamatbfb6ed22012-12-24 14:03:42 +05301832 return -EINVAL;
Eunchul Kimcb471f142012-12-14 18:10:31 +09001833 }
1834
1835 /*
1836 * create single thread for ipp command
1837 * IPP supports command thread for user process.
1838 * user process make command node using set property ioctl.
1839 * and make start_work and send this work to command thread.
1840 * and then this command thread start property.
1841 */
1842 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1843 if (!ctx->cmd_workq) {
1844 dev_err(dev, "failed to create cmd workqueue\n");
1845 ret = -EINVAL;
1846 goto err_event_workq;
1847 }
1848
1849 /* set sub driver informations */
1850 subdrv = &ctx->subdrv;
1851 subdrv->dev = dev;
1852 subdrv->probe = ipp_subdrv_probe;
1853 subdrv->remove = ipp_subdrv_remove;
1854 subdrv->open = ipp_subdrv_open;
1855 subdrv->close = ipp_subdrv_close;
1856
1857 platform_set_drvdata(pdev, ctx);
1858
1859 ret = exynos_drm_subdrv_register(subdrv);
1860 if (ret < 0) {
1861 DRM_ERROR("failed to register drm ipp device.\n");
1862 goto err_cmd_workq;
1863 }
1864
Seung-Woo Kimd873ab92013-05-22 21:14:14 +09001865 dev_info(dev, "drm ipp registered successfully.\n");
Eunchul Kimcb471f142012-12-14 18:10:31 +09001866
1867 return 0;
1868
1869err_cmd_workq:
1870 destroy_workqueue(ctx->cmd_workq);
1871err_event_workq:
1872 destroy_workqueue(ctx->event_workq);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001873 return ret;
1874}
1875
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001876static int ipp_remove(struct platform_device *pdev)
Eunchul Kimcb471f142012-12-14 18:10:31 +09001877{
1878 struct ipp_context *ctx = platform_get_drvdata(pdev);
1879
Eunchul Kimcb471f142012-12-14 18:10:31 +09001880 /* unregister sub driver */
1881 exynos_drm_subdrv_unregister(&ctx->subdrv);
1882
1883 /* remove,destroy ipp idr */
Eunchul Kimcb471f142012-12-14 18:10:31 +09001884 idr_destroy(&ctx->ipp_idr);
1885 idr_destroy(&ctx->prop_idr);
1886
1887 mutex_destroy(&ctx->ipp_lock);
1888 mutex_destroy(&ctx->prop_lock);
1889
1890 /* destroy command, event work queue */
1891 destroy_workqueue(ctx->cmd_workq);
1892 destroy_workqueue(ctx->event_workq);
1893
Eunchul Kimcb471f142012-12-14 18:10:31 +09001894 return 0;
1895}
1896
1897static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1898{
YoungJun Chocbc4c332013-06-12 10:44:40 +09001899 DRM_DEBUG_KMS("enable[%d]\n", enable);
Eunchul Kimcb471f142012-12-14 18:10:31 +09001900
1901 return 0;
1902}
1903
1904#ifdef CONFIG_PM_SLEEP
1905static int ipp_suspend(struct device *dev)
1906{
1907 struct ipp_context *ctx = get_ipp_context(dev);
1908
Eunchul Kimcb471f142012-12-14 18:10:31 +09001909 if (pm_runtime_suspended(dev))
1910 return 0;
1911
1912 return ipp_power_ctrl(ctx, false);
1913}
1914
1915static int ipp_resume(struct device *dev)
1916{
1917 struct ipp_context *ctx = get_ipp_context(dev);
1918
Eunchul Kimcb471f142012-12-14 18:10:31 +09001919 if (!pm_runtime_suspended(dev))
1920 return ipp_power_ctrl(ctx, true);
1921
1922 return 0;
1923}
1924#endif
1925
1926#ifdef CONFIG_PM_RUNTIME
1927static int ipp_runtime_suspend(struct device *dev)
1928{
1929 struct ipp_context *ctx = get_ipp_context(dev);
1930
Eunchul Kimcb471f142012-12-14 18:10:31 +09001931 return ipp_power_ctrl(ctx, false);
1932}
1933
1934static int ipp_runtime_resume(struct device *dev)
1935{
1936 struct ipp_context *ctx = get_ipp_context(dev);
1937
Eunchul Kimcb471f142012-12-14 18:10:31 +09001938 return ipp_power_ctrl(ctx, true);
1939}
1940#endif
1941
1942static const struct dev_pm_ops ipp_pm_ops = {
1943 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1944 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1945};
1946
1947struct platform_driver ipp_driver = {
1948 .probe = ipp_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -08001949 .remove = ipp_remove,
Eunchul Kimcb471f142012-12-14 18:10:31 +09001950 .driver = {
1951 .name = "exynos-drm-ipp",
1952 .owner = THIS_MODULE,
1953 .pm = &ipp_pm_ops,
1954 },
1955};
1956