]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/gpu/drm/exynos/exynos_drm_ipp.c
drm/exynos/ipp: remove unused field from exynos_drm_ipp_private
[linux-beck.git] / drivers / gpu / drm / exynos / exynos_drm_ipp.c
1 /*
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>
15 #include <linux/platform_device.h>
16 #include <linux/types.h>
17 #include <linux/clk.h>
18 #include <linux/pm_runtime.h>
19
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"
25 #include "exynos_drm_iommu.h"
26
27 /*
28  * IPP stands for Image Post Processing and
29  * supports image scaler/rotator and input/output DMA operations.
30  * using FIMC, GSC, Rotator, so on.
31  * IPP is integration device driver of same attribute h/w
32  */
33
34 /*
35  * TODO
36  * 1. expand command control id.
37  * 2. integrate property and config.
38  * 3. removed send_event id check routine.
39  * 4. compare send_event id if needed.
40  * 5. free subdrv_remove notifier callback list if needed.
41  * 6. need to check subdrv_open about multi-open.
42  * 7. need to power_on implement power and sysmmu ctrl.
43  */
44
45 #define get_ipp_context(dev)    platform_get_drvdata(to_platform_device(dev))
46 #define ipp_is_m2m_cmd(c)       (c == IPP_CMD_M2M)
47
48 /* platform device pointer for ipp device. */
49 static struct platform_device *exynos_drm_ipp_pdev;
50
51 /*
52  * A structure of event.
53  *
54  * @base: base of event.
55  * @event: ipp event.
56  */
57 struct 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  */
72 struct 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  */
92 struct 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
102 static LIST_HEAD(exynos_drm_ippdrv_list);
103 static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
104 static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
105
106 int 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
122 void 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
130 int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
131 {
132         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
142 int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
143 {
144         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
154 static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj,
155                 u32 *idp)
156 {
157         int ret;
158
159         /* do the allocation under our mutexlock */
160         mutex_lock(lock);
161         ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
162         mutex_unlock(lock);
163         if (ret < 0)
164                 return ret;
165
166         *idp = ret;
167         return 0;
168 }
169
170 static 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
177 static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
178 {
179         void *obj;
180
181         DRM_DEBUG_KMS("id[%d]\n", id);
182
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
198 static 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
212 static 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
218         DRM_DEBUG_KMS("ipp_id[%d]\n", ipp_id);
219
220         if (ipp_id) {
221                 /* find ipp driver using idr */
222                 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
223                         ipp_id);
224                 if (IS_ERR(ippdrv)) {
225                         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)) {
259                                 DRM_DEBUG_KMS("used device.\n");
260                                 continue;
261                         }
262
263                         if (ippdrv->check_property &&
264                             ippdrv->check_property(ippdrv->dev, property)) {
265                                 DRM_DEBUG_KMS("not support property.\n");
266                                 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
278 static 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
284         DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
285
286         /*
287          * This case is search ipp driver by prop_id handle.
288          * sometimes, ipp subsystem find driver by prop_id.
289          * e.g PAUSE state, queue buf, command control.
290          */
291         list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
292                 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
293
294                 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);
298                                 return ippdrv;
299                         }
300                 }
301                 mutex_unlock(&ippdrv->cmd_lock);
302         }
303
304         return ERR_PTR(-ENODEV);
305 }
306
307 int 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;
311         struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
312         struct device *dev = priv->dev;
313         struct ipp_context *ctx = get_ipp_context(dev);
314         struct drm_exynos_ipp_prop_list *prop_list = data;
315         struct exynos_drm_ippdrv *ippdrv;
316         int count = 0;
317
318         if (!ctx) {
319                 DRM_ERROR("invalid context.\n");
320                 return -EINVAL;
321         }
322
323         if (!prop_list) {
324                 DRM_ERROR("invalid property parameter.\n");
325                 return -EINVAL;
326         }
327
328         DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
329
330         if (!prop_list->ipp_id) {
331                 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
332                         count++;
333
334                 /*
335                  * Supports ippdrv list count for user application.
336                  * First step user application getting ippdrv count.
337                  * and second step getting ippdrv capability using ipp_id.
338                  */
339                 prop_list->count = count;
340         } else {
341                 /*
342                  * Getting ippdrv capability by ipp_id.
343                  * some device not supported wb, output interface.
344                  * so, user application detect correct ipp driver
345                  * using this ioctl.
346                  */
347                 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
348                                                 prop_list->ipp_id);
349                 if (IS_ERR(ippdrv)) {
350                         DRM_ERROR("not found ipp%d driver.\n",
351                                         prop_list->ipp_id);
352                         return PTR_ERR(ippdrv);
353                 }
354
355                 *prop_list = ippdrv->prop_list;
356         }
357
358         return 0;
359 }
360
361 static void ipp_print_property(struct drm_exynos_ipp_property *property,
362                 int idx)
363 {
364         struct drm_exynos_ipp_config *config = &property->config[idx];
365         struct drm_exynos_pos *pos = &config->pos;
366         struct drm_exynos_sz *sz = &config->sz;
367
368         DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
369                 property->prop_id, idx ? "dst" : "src", config->fmt);
370
371         DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
372                 pos->x, pos->y, pos->w, pos->h,
373                 sz->hsize, sz->vsize, config->flip, config->degree);
374 }
375
376 static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
377 {
378         struct exynos_drm_ippdrv *ippdrv;
379         struct drm_exynos_ipp_cmd_node *c_node;
380         u32 prop_id = property->prop_id;
381
382         DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
383
384         ippdrv = ipp_find_drv_by_handle(prop_id);
385         if (IS_ERR(ippdrv)) {
386                 DRM_ERROR("failed to get ipp driver.\n");
387                 return -EINVAL;
388         }
389
390         /*
391          * Find command node using command list in ippdrv.
392          * when we find this command no using prop_id.
393          * return property information set in this command node.
394          */
395         mutex_lock(&ippdrv->cmd_lock);
396         list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
397                 if ((c_node->property.prop_id == prop_id) &&
398                     (c_node->state == IPP_STATE_STOP)) {
399                         mutex_unlock(&ippdrv->cmd_lock);
400                         DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
401                                 property->cmd, (int)ippdrv);
402
403                         c_node->property = *property;
404                         return 0;
405                 }
406         }
407         mutex_unlock(&ippdrv->cmd_lock);
408
409         DRM_ERROR("failed to search property.\n");
410
411         return -EINVAL;
412 }
413
414 static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
415 {
416         struct drm_exynos_ipp_cmd_work *cmd_work;
417
418         cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
419         if (!cmd_work)
420                 return ERR_PTR(-ENOMEM);
421
422         INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
423
424         return cmd_work;
425 }
426
427 static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
428 {
429         struct drm_exynos_ipp_event_work *event_work;
430
431         event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
432         if (!event_work)
433                 return ERR_PTR(-ENOMEM);
434
435         INIT_WORK(&event_work->work, ipp_sched_event);
436
437         return event_work;
438 }
439
440 int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
441                 struct drm_file *file)
442 {
443         struct drm_exynos_file_private *file_priv = file->driver_priv;
444         struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
445         struct device *dev = priv->dev;
446         struct ipp_context *ctx = get_ipp_context(dev);
447         struct drm_exynos_ipp_property *property = data;
448         struct exynos_drm_ippdrv *ippdrv;
449         struct drm_exynos_ipp_cmd_node *c_node;
450         int ret, i;
451
452         if (!ctx) {
453                 DRM_ERROR("invalid context.\n");
454                 return -EINVAL;
455         }
456
457         if (!property) {
458                 DRM_ERROR("invalid property parameter.\n");
459                 return -EINVAL;
460         }
461
462         /*
463          * This is log print for user application property.
464          * user application set various property.
465          */
466         for_each_ipp_ops(i)
467                 ipp_print_property(property, i);
468
469         /*
470          * set property ioctl generated new prop_id.
471          * but in this case already asigned prop_id using old set property.
472          * e.g PAUSE state. this case supports find current prop_id and use it
473          * instead of allocation.
474          */
475         if (property->prop_id) {
476                 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
477                 return ipp_find_and_set_property(property);
478         }
479
480         /* find ipp driver using ipp id */
481         ippdrv = ipp_find_driver(ctx, property);
482         if (IS_ERR(ippdrv)) {
483                 DRM_ERROR("failed to get ipp driver.\n");
484                 return -EINVAL;
485         }
486
487         /* allocate command node */
488         c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
489         if (!c_node)
490                 return -ENOMEM;
491
492         /* create property id */
493         ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node,
494                 &property->prop_id);
495         if (ret) {
496                 DRM_ERROR("failed to create id.\n");
497                 goto err_clear;
498         }
499
500         DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
501                 property->prop_id, property->cmd, (int)ippdrv);
502
503         /* stored property information and ippdrv in private data */
504         c_node->priv = priv;
505         c_node->property = *property;
506         c_node->state = IPP_STATE_IDLE;
507
508         c_node->start_work = ipp_create_cmd_work();
509         if (IS_ERR(c_node->start_work)) {
510                 DRM_ERROR("failed to create start work.\n");
511                 goto err_remove_id;
512         }
513
514         c_node->stop_work = ipp_create_cmd_work();
515         if (IS_ERR(c_node->stop_work)) {
516                 DRM_ERROR("failed to create stop work.\n");
517                 goto err_free_start;
518         }
519
520         c_node->event_work = ipp_create_event_work();
521         if (IS_ERR(c_node->event_work)) {
522                 DRM_ERROR("failed to create event work.\n");
523                 goto err_free_stop;
524         }
525
526         mutex_init(&c_node->lock);
527         mutex_init(&c_node->mem_lock);
528         mutex_init(&c_node->event_lock);
529
530         init_completion(&c_node->start_complete);
531         init_completion(&c_node->stop_complete);
532
533         for_each_ipp_ops(i)
534                 INIT_LIST_HEAD(&c_node->mem_list[i]);
535
536         INIT_LIST_HEAD(&c_node->event_list);
537         mutex_lock(&ippdrv->cmd_lock);
538         list_add_tail(&c_node->list, &ippdrv->cmd_list);
539         mutex_unlock(&ippdrv->cmd_lock);
540
541         /* make dedicated state without m2m */
542         if (!ipp_is_m2m_cmd(property->cmd))
543                 ippdrv->dedicated = true;
544
545         return 0;
546
547 err_free_stop:
548         kfree(c_node->stop_work);
549 err_free_start:
550         kfree(c_node->start_work);
551 err_remove_id:
552         ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
553 err_clear:
554         kfree(c_node);
555         return ret;
556 }
557
558 static void ipp_clean_cmd_node(struct ipp_context *ctx,
559                                 struct drm_exynos_ipp_cmd_node *c_node)
560 {
561         /* delete list */
562         list_del(&c_node->list);
563
564         ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
565                         c_node->property.prop_id);
566
567         /* destroy mutex */
568         mutex_destroy(&c_node->lock);
569         mutex_destroy(&c_node->mem_lock);
570         mutex_destroy(&c_node->event_lock);
571
572         /* free command node */
573         kfree(c_node->start_work);
574         kfree(c_node->stop_work);
575         kfree(c_node->event_work);
576         kfree(c_node);
577 }
578
579 static int ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
580 {
581         struct drm_exynos_ipp_property *property = &c_node->property;
582         struct drm_exynos_ipp_mem_node *m_node;
583         struct list_head *head;
584         int ret, i, count[EXYNOS_DRM_OPS_MAX] = { 0, };
585
586         for_each_ipp_ops(i) {
587                 /* source/destination memory list */
588                 head = &c_node->mem_list[i];
589
590                 /* find memory node entry */
591                 list_for_each_entry(m_node, head, list) {
592                         DRM_DEBUG_KMS("%s,count[%d]m_node[0x%x]\n",
593                                 i ? "dst" : "src", count[i], (int)m_node);
594                         count[i]++;
595                 }
596         }
597
598         DRM_DEBUG_KMS("min[%d]max[%d]\n",
599                 min(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]),
600                 max(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]));
601
602         /*
603          * M2M operations should be need paired memory address.
604          * so, need to check minimum count about src, dst.
605          * other case not use paired memory, so use maximum count
606          */
607         if (ipp_is_m2m_cmd(property->cmd))
608                 ret = min(count[EXYNOS_DRM_OPS_SRC],
609                         count[EXYNOS_DRM_OPS_DST]);
610         else
611                 ret = max(count[EXYNOS_DRM_OPS_SRC],
612                         count[EXYNOS_DRM_OPS_DST]);
613
614         return ret;
615 }
616
617 static struct drm_exynos_ipp_mem_node
618                 *ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
619                 struct drm_exynos_ipp_queue_buf *qbuf)
620 {
621         struct drm_exynos_ipp_mem_node *m_node;
622         struct list_head *head;
623         int count = 0;
624
625         DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
626
627         /* source/destination memory list */
628         head = &c_node->mem_list[qbuf->ops_id];
629
630         /* find memory node from memory list */
631         list_for_each_entry(m_node, head, list) {
632                 DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
633
634                 /* compare buffer id */
635                 if (m_node->buf_id == qbuf->buf_id)
636                         return m_node;
637         }
638
639         return NULL;
640 }
641
642 static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
643                 struct drm_exynos_ipp_cmd_node *c_node,
644                 struct drm_exynos_ipp_mem_node *m_node)
645 {
646         struct exynos_drm_ipp_ops *ops = NULL;
647         int ret = 0;
648
649         DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
650
651         if (!m_node) {
652                 DRM_ERROR("invalid queue node.\n");
653                 return -EFAULT;
654         }
655
656         DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
657
658         /* get operations callback */
659         ops = ippdrv->ops[m_node->ops_id];
660         if (!ops) {
661                 DRM_ERROR("not support ops.\n");
662                 return -EFAULT;
663         }
664
665         /* set address and enable irq */
666         if (ops->set_addr) {
667                 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
668                         m_node->buf_id, IPP_BUF_ENQUEUE);
669                 if (ret) {
670                         DRM_ERROR("failed to set addr.\n");
671                         return ret;
672                 }
673         }
674
675         return ret;
676 }
677
678 static struct drm_exynos_ipp_mem_node
679                 *ipp_get_mem_node(struct drm_device *drm_dev,
680                 struct drm_file *file,
681                 struct drm_exynos_ipp_cmd_node *c_node,
682                 struct drm_exynos_ipp_queue_buf *qbuf)
683 {
684         struct drm_exynos_ipp_mem_node *m_node;
685         struct drm_exynos_ipp_buf_info buf_info;
686         void *addr;
687         int i;
688
689         m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
690         if (!m_node)
691                 return ERR_PTR(-ENOMEM);
692
693         /* clear base address for error handling */
694         memset(&buf_info, 0x0, sizeof(buf_info));
695
696         /* operations, buffer id */
697         m_node->ops_id = qbuf->ops_id;
698         m_node->prop_id = qbuf->prop_id;
699         m_node->buf_id = qbuf->buf_id;
700
701         DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
702         DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
703
704         for_each_ipp_planar(i) {
705                 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
706
707                 /* get dma address by handle */
708                 if (qbuf->handle[i]) {
709                         addr = exynos_drm_gem_get_dma_addr(drm_dev,
710                                         qbuf->handle[i], file);
711                         if (IS_ERR(addr)) {
712                                 DRM_ERROR("failed to get addr.\n");
713                                 goto err_clear;
714                         }
715
716                         buf_info.handles[i] = qbuf->handle[i];
717                         buf_info.base[i] = *(dma_addr_t *) addr;
718                         DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%x]\n",
719                                 i, buf_info.base[i], (int)buf_info.handles[i]);
720                 }
721         }
722
723         m_node->filp = file;
724         m_node->buf_info = buf_info;
725         mutex_lock(&c_node->mem_lock);
726         list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
727         mutex_unlock(&c_node->mem_lock);
728
729         return m_node;
730
731 err_clear:
732         kfree(m_node);
733         return ERR_PTR(-EFAULT);
734 }
735
736 static 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
742         DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
743
744         if (!m_node) {
745                 DRM_ERROR("invalid dequeue node.\n");
746                 return -EFAULT;
747         }
748
749         DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
750
751         /* put gem buffer */
752         for_each_ipp_planar(i) {
753                 unsigned long handle = m_node->buf_info.handles[i];
754                 if (handle)
755                         exynos_drm_gem_put_dma_addr(drm_dev, handle,
756                                                         m_node->filp);
757         }
758
759         /* delete list in queue */
760         list_del(&m_node->list);
761         kfree(m_node);
762
763         return 0;
764 }
765
766 static void ipp_free_event(struct drm_pending_event *event)
767 {
768         kfree(event);
769 }
770
771 static int ipp_get_event(struct drm_device *drm_dev,
772                 struct drm_file *file,
773                 struct drm_exynos_ipp_cmd_node *c_node,
774                 struct drm_exynos_ipp_queue_buf *qbuf)
775 {
776         struct drm_exynos_ipp_send_event *e;
777         unsigned long flags;
778
779         DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
780
781         e = kzalloc(sizeof(*e), GFP_KERNEL);
782         if (!e) {
783                 spin_lock_irqsave(&drm_dev->event_lock, flags);
784                 file->event_space += sizeof(e->event);
785                 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
786                 return -ENOMEM;
787         }
788
789         /* make event */
790         e->event.base.type = DRM_EXYNOS_IPP_EVENT;
791         e->event.base.length = sizeof(e->event);
792         e->event.user_data = qbuf->user_data;
793         e->event.prop_id = qbuf->prop_id;
794         e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
795         e->base.event = &e->event.base;
796         e->base.file_priv = file;
797         e->base.destroy = ipp_free_event;
798         mutex_lock(&c_node->event_lock);
799         list_add_tail(&e->base.link, &c_node->event_list);
800         mutex_unlock(&c_node->event_lock);
801
802         return 0;
803 }
804
805 static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
806                 struct drm_exynos_ipp_queue_buf *qbuf)
807 {
808         struct drm_exynos_ipp_send_event *e, *te;
809         int count = 0;
810
811         mutex_lock(&c_node->event_lock);
812         list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
813                 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
814
815                 /*
816                  * qbuf == NULL condition means all event deletion.
817                  * stop operations want to delete all event list.
818                  * another case delete only same buf id.
819                  */
820                 if (!qbuf) {
821                         /* delete list */
822                         list_del(&e->base.link);
823                         kfree(e);
824                 }
825
826                 /* compare buffer id */
827                 if (qbuf && (qbuf->buf_id ==
828                     e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
829                         /* delete list */
830                         list_del(&e->base.link);
831                         kfree(e);
832                         goto out_unlock;
833                 }
834         }
835
836 out_unlock:
837         mutex_unlock(&c_node->event_lock);
838         return;
839 }
840
841 static void ipp_handle_cmd_work(struct device *dev,
842                 struct exynos_drm_ippdrv *ippdrv,
843                 struct drm_exynos_ipp_cmd_work *cmd_work,
844                 struct drm_exynos_ipp_cmd_node *c_node)
845 {
846         struct ipp_context *ctx = get_ipp_context(dev);
847
848         cmd_work->ippdrv = ippdrv;
849         cmd_work->c_node = c_node;
850         queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
851 }
852
853 static int ipp_queue_buf_with_run(struct device *dev,
854                 struct drm_exynos_ipp_cmd_node *c_node,
855                 struct drm_exynos_ipp_mem_node *m_node,
856                 struct drm_exynos_ipp_queue_buf *qbuf)
857 {
858         struct exynos_drm_ippdrv *ippdrv;
859         struct drm_exynos_ipp_property *property;
860         struct exynos_drm_ipp_ops *ops;
861         int ret;
862
863         ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
864         if (IS_ERR(ippdrv)) {
865                 DRM_ERROR("failed to get ipp driver.\n");
866                 return -EFAULT;
867         }
868
869         ops = ippdrv->ops[qbuf->ops_id];
870         if (!ops) {
871                 DRM_ERROR("failed to get ops.\n");
872                 return -EFAULT;
873         }
874
875         property = &c_node->property;
876
877         if (c_node->state != IPP_STATE_START) {
878                 DRM_DEBUG_KMS("bypass for invalid state.\n");
879                 return 0;
880         }
881
882         mutex_lock(&c_node->mem_lock);
883         if (!ipp_check_mem_list(c_node)) {
884                 mutex_unlock(&c_node->mem_lock);
885                 DRM_DEBUG_KMS("empty memory.\n");
886                 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                         mutex_unlock(&c_node->mem_lock);
902                         DRM_ERROR("failed to set m node.\n");
903                         return ret;
904                 }
905         }
906         mutex_unlock(&c_node->mem_lock);
907
908         return 0;
909 }
910
911 static void ipp_clean_queue_buf(struct drm_device *drm_dev,
912                 struct drm_exynos_ipp_cmd_node *c_node,
913                 struct drm_exynos_ipp_queue_buf *qbuf)
914 {
915         struct drm_exynos_ipp_mem_node *m_node, *tm_node;
916
917         /* delete list */
918         mutex_lock(&c_node->mem_lock);
919         list_for_each_entry_safe(m_node, tm_node,
920                 &c_node->mem_list[qbuf->ops_id], list) {
921                 if (m_node->buf_id == qbuf->buf_id &&
922                     m_node->ops_id == qbuf->ops_id)
923                         ipp_put_mem_node(drm_dev, c_node, m_node);
924         }
925         mutex_unlock(&c_node->mem_lock);
926 }
927
928 int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
929                 struct drm_file *file)
930 {
931         struct drm_exynos_file_private *file_priv = file->driver_priv;
932         struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
933         struct device *dev = priv->dev;
934         struct ipp_context *ctx = get_ipp_context(dev);
935         struct drm_exynos_ipp_queue_buf *qbuf = data;
936         struct drm_exynos_ipp_cmd_node *c_node;
937         struct drm_exynos_ipp_mem_node *m_node;
938         int ret;
939
940         if (!qbuf) {
941                 DRM_ERROR("invalid buf parameter.\n");
942                 return -EINVAL;
943         }
944
945         if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
946                 DRM_ERROR("invalid ops parameter.\n");
947                 return -EINVAL;
948         }
949
950         DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
951                 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
952                 qbuf->buf_id, qbuf->buf_type);
953
954         /* find command node */
955         c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
956                 qbuf->prop_id);
957         if (IS_ERR(c_node)) {
958                 DRM_ERROR("failed to get command node.\n");
959                 return PTR_ERR(c_node);
960         }
961
962         /* buffer control */
963         switch (qbuf->buf_type) {
964         case IPP_BUF_ENQUEUE:
965                 /* get memory node */
966                 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
967                 if (IS_ERR(m_node)) {
968                         DRM_ERROR("failed to get m_node.\n");
969                         return PTR_ERR(m_node);
970                 }
971
972                 /*
973                  * first step get event for destination buffer.
974                  * and second step when M2M case run with destination buffer
975                  * if needed.
976                  */
977                 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
978                         /* get event for destination buffer */
979                         ret = ipp_get_event(drm_dev, file, c_node, qbuf);
980                         if (ret) {
981                                 DRM_ERROR("failed to get event.\n");
982                                 goto err_clean_node;
983                         }
984
985                         /*
986                          * M2M case run play control for streaming feature.
987                          * other case set address and waiting.
988                          */
989                         ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
990                         if (ret) {
991                                 DRM_ERROR("failed to run command.\n");
992                                 goto err_clean_node;
993                         }
994                 }
995                 break;
996         case IPP_BUF_DEQUEUE:
997                 mutex_lock(&c_node->lock);
998
999                 /* put event for destination buffer */
1000                 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
1001                         ipp_put_event(c_node, qbuf);
1002
1003                 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1004
1005                 mutex_unlock(&c_node->lock);
1006                 break;
1007         default:
1008                 DRM_ERROR("invalid buffer control.\n");
1009                 return -EINVAL;
1010         }
1011
1012         return 0;
1013
1014 err_clean_node:
1015         DRM_ERROR("clean memory nodes.\n");
1016
1017         ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1018         return ret;
1019 }
1020
1021 static bool exynos_drm_ipp_check_valid(struct device *dev,
1022                 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
1023 {
1024         if (ctrl != IPP_CTRL_PLAY) {
1025                 if (pm_runtime_suspended(dev)) {
1026                         DRM_ERROR("pm:runtime_suspended.\n");
1027                         goto err_status;
1028                 }
1029         }
1030
1031         switch (ctrl) {
1032         case IPP_CTRL_PLAY:
1033                 if (state != IPP_STATE_IDLE)
1034                         goto err_status;
1035                 break;
1036         case IPP_CTRL_STOP:
1037                 if (state == IPP_STATE_STOP)
1038                         goto err_status;
1039                 break;
1040         case IPP_CTRL_PAUSE:
1041                 if (state != IPP_STATE_START)
1042                         goto err_status;
1043                 break;
1044         case IPP_CTRL_RESUME:
1045                 if (state != IPP_STATE_STOP)
1046                         goto err_status;
1047                 break;
1048         default:
1049                 DRM_ERROR("invalid state.\n");
1050                 goto err_status;
1051         }
1052
1053         return true;
1054
1055 err_status:
1056         DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1057         return false;
1058 }
1059
1060 int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1061                 struct drm_file *file)
1062 {
1063         struct drm_exynos_file_private *file_priv = file->driver_priv;
1064         struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1065         struct exynos_drm_ippdrv *ippdrv = NULL;
1066         struct device *dev = priv->dev;
1067         struct ipp_context *ctx = get_ipp_context(dev);
1068         struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1069         struct drm_exynos_ipp_cmd_work *cmd_work;
1070         struct drm_exynos_ipp_cmd_node *c_node;
1071
1072         if (!ctx) {
1073                 DRM_ERROR("invalid context.\n");
1074                 return -EINVAL;
1075         }
1076
1077         if (!cmd_ctrl) {
1078                 DRM_ERROR("invalid control parameter.\n");
1079                 return -EINVAL;
1080         }
1081
1082         DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
1083                 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1084
1085         ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1086         if (IS_ERR(ippdrv)) {
1087                 DRM_ERROR("failed to get ipp driver.\n");
1088                 return PTR_ERR(ippdrv);
1089         }
1090
1091         c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1092                 cmd_ctrl->prop_id);
1093         if (IS_ERR(c_node)) {
1094                 DRM_ERROR("invalid command node list.\n");
1095                 return PTR_ERR(c_node);
1096         }
1097
1098         if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1099             c_node->state)) {
1100                 DRM_ERROR("invalid state.\n");
1101                 return -EINVAL;
1102         }
1103
1104         switch (cmd_ctrl->ctrl) {
1105         case IPP_CTRL_PLAY:
1106                 if (pm_runtime_suspended(ippdrv->dev))
1107                         pm_runtime_get_sync(ippdrv->dev);
1108
1109                 c_node->state = IPP_STATE_START;
1110
1111                 cmd_work = c_node->start_work;
1112                 cmd_work->ctrl = cmd_ctrl->ctrl;
1113                 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1114                 break;
1115         case IPP_CTRL_STOP:
1116                 cmd_work = c_node->stop_work;
1117                 cmd_work->ctrl = cmd_ctrl->ctrl;
1118                 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1119
1120                 if (!wait_for_completion_timeout(&c_node->stop_complete,
1121                     msecs_to_jiffies(300))) {
1122                         DRM_ERROR("timeout stop:prop_id[%d]\n",
1123                                 c_node->property.prop_id);
1124                 }
1125
1126                 c_node->state = IPP_STATE_STOP;
1127                 ippdrv->dedicated = false;
1128                 mutex_lock(&ippdrv->cmd_lock);
1129                 ipp_clean_cmd_node(ctx, c_node);
1130
1131                 if (list_empty(&ippdrv->cmd_list))
1132                         pm_runtime_put_sync(ippdrv->dev);
1133                 mutex_unlock(&ippdrv->cmd_lock);
1134                 break;
1135         case IPP_CTRL_PAUSE:
1136                 cmd_work = c_node->stop_work;
1137                 cmd_work->ctrl = cmd_ctrl->ctrl;
1138                 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1139
1140                 if (!wait_for_completion_timeout(&c_node->stop_complete,
1141                     msecs_to_jiffies(200))) {
1142                         DRM_ERROR("timeout stop:prop_id[%d]\n",
1143                                 c_node->property.prop_id);
1144                 }
1145
1146                 c_node->state = IPP_STATE_STOP;
1147                 break;
1148         case IPP_CTRL_RESUME:
1149                 c_node->state = IPP_STATE_START;
1150                 cmd_work = c_node->start_work;
1151                 cmd_work->ctrl = cmd_ctrl->ctrl;
1152                 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1153                 break;
1154         default:
1155                 DRM_ERROR("could not support this state currently.\n");
1156                 return -EINVAL;
1157         }
1158
1159         DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
1160                 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1161
1162         return 0;
1163 }
1164
1165 int exynos_drm_ippnb_register(struct notifier_block *nb)
1166 {
1167         return blocking_notifier_chain_register(
1168                 &exynos_drm_ippnb_list, nb);
1169 }
1170
1171 int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1172 {
1173         return blocking_notifier_chain_unregister(
1174                 &exynos_drm_ippnb_list, nb);
1175 }
1176
1177 int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1178 {
1179         return blocking_notifier_call_chain(
1180                 &exynos_drm_ippnb_list, val, v);
1181 }
1182
1183 static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1184                 struct drm_exynos_ipp_property *property)
1185 {
1186         struct exynos_drm_ipp_ops *ops = NULL;
1187         bool swap = false;
1188         int ret, i;
1189
1190         if (!property) {
1191                 DRM_ERROR("invalid property parameter.\n");
1192                 return -EINVAL;
1193         }
1194
1195         DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
1196
1197         /* reset h/w block */
1198         if (ippdrv->reset &&
1199             ippdrv->reset(ippdrv->dev)) {
1200                 DRM_ERROR("failed to reset.\n");
1201                 return -EINVAL;
1202         }
1203
1204         /* set source,destination operations */
1205         for_each_ipp_ops(i) {
1206                 struct drm_exynos_ipp_config *config =
1207                         &property->config[i];
1208
1209                 ops = ippdrv->ops[i];
1210                 if (!ops || !config) {
1211                         DRM_ERROR("not support ops and config.\n");
1212                         return -EINVAL;
1213                 }
1214
1215                 /* set format */
1216                 if (ops->set_fmt) {
1217                         ret = ops->set_fmt(ippdrv->dev, config->fmt);
1218                         if (ret) {
1219                                 DRM_ERROR("not support format.\n");
1220                                 return ret;
1221                         }
1222                 }
1223
1224                 /* set transform for rotation, flip */
1225                 if (ops->set_transf) {
1226                         ret = ops->set_transf(ippdrv->dev, config->degree,
1227                                 config->flip, &swap);
1228                         if (ret) {
1229                                 DRM_ERROR("not support tranf.\n");
1230                                 return -EINVAL;
1231                         }
1232                 }
1233
1234                 /* set size */
1235                 if (ops->set_size) {
1236                         ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1237                                 &config->sz);
1238                         if (ret) {
1239                                 DRM_ERROR("not support size.\n");
1240                                 return ret;
1241                         }
1242                 }
1243         }
1244
1245         return 0;
1246 }
1247
1248 static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1249                 struct drm_exynos_ipp_cmd_node *c_node)
1250 {
1251         struct drm_exynos_ipp_mem_node *m_node;
1252         struct drm_exynos_ipp_property *property = &c_node->property;
1253         struct list_head *head;
1254         int ret, i;
1255
1256         DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
1257
1258         /* store command info in ippdrv */
1259         ippdrv->c_node = c_node;
1260
1261         mutex_lock(&c_node->mem_lock);
1262         if (!ipp_check_mem_list(c_node)) {
1263                 DRM_DEBUG_KMS("empty memory.\n");
1264                 ret = -ENOMEM;
1265                 goto err_unlock;
1266         }
1267
1268         /* set current property in ippdrv */
1269         ret = ipp_set_property(ippdrv, property);
1270         if (ret) {
1271                 DRM_ERROR("failed to set property.\n");
1272                 ippdrv->c_node = NULL;
1273                 goto err_unlock;
1274         }
1275
1276         /* check command */
1277         switch (property->cmd) {
1278         case IPP_CMD_M2M:
1279                 for_each_ipp_ops(i) {
1280                         /* source/destination memory list */
1281                         head = &c_node->mem_list[i];
1282
1283                         m_node = list_first_entry(head,
1284                                 struct drm_exynos_ipp_mem_node, list);
1285                         if (!m_node) {
1286                                 DRM_ERROR("failed to get node.\n");
1287                                 ret = -EFAULT;
1288                                 goto err_unlock;
1289                         }
1290
1291                         DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
1292
1293                         ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1294                         if (ret) {
1295                                 DRM_ERROR("failed to set m node.\n");
1296                                 goto err_unlock;
1297                         }
1298                 }
1299                 break;
1300         case IPP_CMD_WB:
1301                 /* destination memory list */
1302                 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1303
1304                 list_for_each_entry(m_node, head, list) {
1305                         ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1306                         if (ret) {
1307                                 DRM_ERROR("failed to set m node.\n");
1308                                 goto err_unlock;
1309                         }
1310                 }
1311                 break;
1312         case IPP_CMD_OUTPUT:
1313                 /* source memory list */
1314                 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1315
1316                 list_for_each_entry(m_node, head, list) {
1317                         ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1318                         if (ret) {
1319                                 DRM_ERROR("failed to set m node.\n");
1320                                 goto err_unlock;
1321                         }
1322                 }
1323                 break;
1324         default:
1325                 DRM_ERROR("invalid operations.\n");
1326                 ret = -EINVAL;
1327                 goto err_unlock;
1328         }
1329         mutex_unlock(&c_node->mem_lock);
1330
1331         DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
1332
1333         /* start operations */
1334         if (ippdrv->start) {
1335                 ret = ippdrv->start(ippdrv->dev, property->cmd);
1336                 if (ret) {
1337                         DRM_ERROR("failed to start ops.\n");
1338                         ippdrv->c_node = NULL;
1339                         return ret;
1340                 }
1341         }
1342
1343         return 0;
1344
1345 err_unlock:
1346         mutex_unlock(&c_node->mem_lock);
1347         ippdrv->c_node = NULL;
1348         return ret;
1349 }
1350
1351 static int ipp_stop_property(struct drm_device *drm_dev,
1352                 struct exynos_drm_ippdrv *ippdrv,
1353                 struct drm_exynos_ipp_cmd_node *c_node)
1354 {
1355         struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1356         struct drm_exynos_ipp_property *property = &c_node->property;
1357         struct list_head *head;
1358         int ret = 0, i;
1359
1360         DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
1361
1362         /* put event */
1363         ipp_put_event(c_node, NULL);
1364
1365         mutex_lock(&c_node->mem_lock);
1366
1367         /* check command */
1368         switch (property->cmd) {
1369         case IPP_CMD_M2M:
1370                 for_each_ipp_ops(i) {
1371                         /* source/destination memory list */
1372                         head = &c_node->mem_list[i];
1373
1374                         list_for_each_entry_safe(m_node, tm_node,
1375                                 head, list) {
1376                                 ret = ipp_put_mem_node(drm_dev, c_node,
1377                                         m_node);
1378                                 if (ret) {
1379                                         DRM_ERROR("failed to put m_node.\n");
1380                                         goto err_clear;
1381                                 }
1382                         }
1383                 }
1384                 break;
1385         case IPP_CMD_WB:
1386                 /* destination memory list */
1387                 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1388
1389                 list_for_each_entry_safe(m_node, tm_node, head, list) {
1390                         ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1391                         if (ret) {
1392                                 DRM_ERROR("failed to put m_node.\n");
1393                                 goto err_clear;
1394                         }
1395                 }
1396                 break;
1397         case IPP_CMD_OUTPUT:
1398                 /* source memory list */
1399                 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1400
1401                 list_for_each_entry_safe(m_node, tm_node, head, list) {
1402                         ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1403                         if (ret) {
1404                                 DRM_ERROR("failed to put m_node.\n");
1405                                 goto err_clear;
1406                         }
1407                 }
1408                 break;
1409         default:
1410                 DRM_ERROR("invalid operations.\n");
1411                 ret = -EINVAL;
1412                 goto err_clear;
1413         }
1414
1415 err_clear:
1416         mutex_unlock(&c_node->mem_lock);
1417
1418         /* stop operations */
1419         if (ippdrv->stop)
1420                 ippdrv->stop(ippdrv->dev, property->cmd);
1421
1422         return ret;
1423 }
1424
1425 void ipp_sched_cmd(struct work_struct *work)
1426 {
1427         struct drm_exynos_ipp_cmd_work *cmd_work =
1428                 (struct drm_exynos_ipp_cmd_work *)work;
1429         struct exynos_drm_ippdrv *ippdrv;
1430         struct drm_exynos_ipp_cmd_node *c_node;
1431         struct drm_exynos_ipp_property *property;
1432         int ret;
1433
1434         ippdrv = cmd_work->ippdrv;
1435         if (!ippdrv) {
1436                 DRM_ERROR("invalid ippdrv list.\n");
1437                 return;
1438         }
1439
1440         c_node = cmd_work->c_node;
1441         if (!c_node) {
1442                 DRM_ERROR("invalid command node list.\n");
1443                 return;
1444         }
1445
1446         mutex_lock(&c_node->lock);
1447
1448         property = &c_node->property;
1449
1450         switch (cmd_work->ctrl) {
1451         case IPP_CTRL_PLAY:
1452         case IPP_CTRL_RESUME:
1453                 ret = ipp_start_property(ippdrv, c_node);
1454                 if (ret) {
1455                         DRM_ERROR("failed to start property:prop_id[%d]\n",
1456                                 c_node->property.prop_id);
1457                         goto err_unlock;
1458                 }
1459
1460                 /*
1461                  * M2M case supports wait_completion of transfer.
1462                  * because M2M case supports single unit operation
1463                  * with multiple queue.
1464                  * M2M need to wait completion of data transfer.
1465                  */
1466                 if (ipp_is_m2m_cmd(property->cmd)) {
1467                         if (!wait_for_completion_timeout
1468                             (&c_node->start_complete, msecs_to_jiffies(200))) {
1469                                 DRM_ERROR("timeout event:prop_id[%d]\n",
1470                                         c_node->property.prop_id);
1471                                 goto err_unlock;
1472                         }
1473                 }
1474                 break;
1475         case IPP_CTRL_STOP:
1476         case IPP_CTRL_PAUSE:
1477                 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1478                         c_node);
1479                 if (ret) {
1480                         DRM_ERROR("failed to stop property.\n");
1481                         goto err_unlock;
1482                 }
1483
1484                 complete(&c_node->stop_complete);
1485                 break;
1486         default:
1487                 DRM_ERROR("unknown control type\n");
1488                 break;
1489         }
1490
1491         DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
1492
1493 err_unlock:
1494         mutex_unlock(&c_node->lock);
1495 }
1496
1497 static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1498                 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1499 {
1500         struct drm_device *drm_dev = ippdrv->drm_dev;
1501         struct drm_exynos_ipp_property *property = &c_node->property;
1502         struct drm_exynos_ipp_mem_node *m_node;
1503         struct drm_exynos_ipp_queue_buf qbuf;
1504         struct drm_exynos_ipp_send_event *e;
1505         struct list_head *head;
1506         struct timeval now;
1507         unsigned long flags;
1508         u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1509         int ret, i;
1510
1511         for_each_ipp_ops(i)
1512                 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
1513
1514         if (!drm_dev) {
1515                 DRM_ERROR("failed to get drm_dev.\n");
1516                 return -EINVAL;
1517         }
1518
1519         if (!property) {
1520                 DRM_ERROR("failed to get property.\n");
1521                 return -EINVAL;
1522         }
1523
1524         mutex_lock(&c_node->event_lock);
1525         if (list_empty(&c_node->event_list)) {
1526                 DRM_DEBUG_KMS("event list is empty.\n");
1527                 ret = 0;
1528                 goto err_event_unlock;
1529         }
1530
1531         mutex_lock(&c_node->mem_lock);
1532         if (!ipp_check_mem_list(c_node)) {
1533                 DRM_DEBUG_KMS("empty memory.\n");
1534                 ret = 0;
1535                 goto err_mem_unlock;
1536         }
1537
1538         /* check command */
1539         switch (property->cmd) {
1540         case IPP_CMD_M2M:
1541                 for_each_ipp_ops(i) {
1542                         /* source/destination memory list */
1543                         head = &c_node->mem_list[i];
1544
1545                         m_node = list_first_entry(head,
1546                                 struct drm_exynos_ipp_mem_node, list);
1547                         if (!m_node) {
1548                                 DRM_ERROR("empty memory node.\n");
1549                                 ret = -ENOMEM;
1550                                 goto err_mem_unlock;
1551                         }
1552
1553                         tbuf_id[i] = m_node->buf_id;
1554                         DRM_DEBUG_KMS("%s buf_id[%d]\n",
1555                                 i ? "dst" : "src", tbuf_id[i]);
1556
1557                         ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1558                         if (ret)
1559                                 DRM_ERROR("failed to put m_node.\n");
1560                 }
1561                 break;
1562         case IPP_CMD_WB:
1563                 /* clear buf for finding */
1564                 memset(&qbuf, 0x0, sizeof(qbuf));
1565                 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1566                 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1567
1568                 /* get memory node entry */
1569                 m_node = ipp_find_mem_node(c_node, &qbuf);
1570                 if (!m_node) {
1571                         DRM_ERROR("empty memory node.\n");
1572                         ret = -ENOMEM;
1573                         goto err_mem_unlock;
1574                 }
1575
1576                 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1577
1578                 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1579                 if (ret)
1580                         DRM_ERROR("failed to put m_node.\n");
1581                 break;
1582         case IPP_CMD_OUTPUT:
1583                 /* source memory list */
1584                 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1585
1586                 m_node = list_first_entry(head,
1587                         struct drm_exynos_ipp_mem_node, list);
1588                 if (!m_node) {
1589                         DRM_ERROR("empty memory node.\n");
1590                         ret = -ENOMEM;
1591                         goto err_mem_unlock;
1592                 }
1593
1594                 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1595
1596                 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1597                 if (ret)
1598                         DRM_ERROR("failed to put m_node.\n");
1599                 break;
1600         default:
1601                 DRM_ERROR("invalid operations.\n");
1602                 ret = -EINVAL;
1603                 goto err_mem_unlock;
1604         }
1605         mutex_unlock(&c_node->mem_lock);
1606
1607         if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1608                 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1609                         tbuf_id[1], buf_id[1], property->prop_id);
1610
1611         /*
1612          * command node have event list of destination buffer
1613          * If destination buffer enqueue to mem list,
1614          * then we make event and link to event list tail.
1615          * so, we get first event for first enqueued buffer.
1616          */
1617         e = list_first_entry(&c_node->event_list,
1618                 struct drm_exynos_ipp_send_event, base.link);
1619
1620         do_gettimeofday(&now);
1621         DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
1622         e->event.tv_sec = now.tv_sec;
1623         e->event.tv_usec = now.tv_usec;
1624         e->event.prop_id = property->prop_id;
1625
1626         /* set buffer id about source destination */
1627         for_each_ipp_ops(i)
1628                 e->event.buf_id[i] = tbuf_id[i];
1629
1630         spin_lock_irqsave(&drm_dev->event_lock, flags);
1631         list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1632         wake_up_interruptible(&e->base.file_priv->event_wait);
1633         spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1634         mutex_unlock(&c_node->event_lock);
1635
1636         DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
1637                 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1638
1639         return 0;
1640
1641 err_mem_unlock:
1642         mutex_unlock(&c_node->mem_lock);
1643 err_event_unlock:
1644         mutex_unlock(&c_node->event_lock);
1645         return ret;
1646 }
1647
1648 void ipp_sched_event(struct work_struct *work)
1649 {
1650         struct drm_exynos_ipp_event_work *event_work =
1651                 (struct drm_exynos_ipp_event_work *)work;
1652         struct exynos_drm_ippdrv *ippdrv;
1653         struct drm_exynos_ipp_cmd_node *c_node;
1654         int ret;
1655
1656         if (!event_work) {
1657                 DRM_ERROR("failed to get event_work.\n");
1658                 return;
1659         }
1660
1661         DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
1662
1663         ippdrv = event_work->ippdrv;
1664         if (!ippdrv) {
1665                 DRM_ERROR("failed to get ipp driver.\n");
1666                 return;
1667         }
1668
1669         c_node = ippdrv->c_node;
1670         if (!c_node) {
1671                 DRM_ERROR("failed to get command node.\n");
1672                 return;
1673         }
1674
1675         /*
1676          * IPP supports command thread, event thread synchronization.
1677          * If IPP close immediately from user land, then IPP make
1678          * synchronization with command thread, so make complete event.
1679          * or going out operations.
1680          */
1681         if (c_node->state != IPP_STATE_START) {
1682                 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1683                         c_node->state, c_node->property.prop_id);
1684                 goto err_completion;
1685         }
1686
1687         ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1688         if (ret) {
1689                 DRM_ERROR("failed to send event.\n");
1690                 goto err_completion;
1691         }
1692
1693 err_completion:
1694         if (ipp_is_m2m_cmd(c_node->property.cmd))
1695                 complete(&c_node->start_complete);
1696 }
1697
1698 static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1699 {
1700         struct ipp_context *ctx = get_ipp_context(dev);
1701         struct exynos_drm_ippdrv *ippdrv;
1702         int ret, count = 0;
1703
1704         /* get ipp driver entry */
1705         list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1706                 u32 ipp_id;
1707
1708                 ippdrv->drm_dev = drm_dev;
1709
1710                 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
1711                                     &ipp_id);
1712                 if (ret || ipp_id == 0) {
1713                         DRM_ERROR("failed to create id.\n");
1714                         goto err;
1715                 }
1716
1717                 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
1718                         count++, (int)ippdrv, ipp_id);
1719
1720                 ippdrv->prop_list.ipp_id = ipp_id;
1721
1722                 /* store parent device for node */
1723                 ippdrv->parent_dev = dev;
1724
1725                 /* store event work queue and handler */
1726                 ippdrv->event_workq = ctx->event_workq;
1727                 ippdrv->sched_event = ipp_sched_event;
1728                 INIT_LIST_HEAD(&ippdrv->cmd_list);
1729                 mutex_init(&ippdrv->cmd_lock);
1730
1731                 if (is_drm_iommu_supported(drm_dev)) {
1732                         ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1733                         if (ret) {
1734                                 DRM_ERROR("failed to activate iommu\n");
1735                                 goto err;
1736                         }
1737                 }
1738         }
1739
1740         return 0;
1741
1742 err:
1743         /* get ipp driver entry */
1744         list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
1745                                                 drv_list) {
1746                 if (is_drm_iommu_supported(drm_dev))
1747                         drm_iommu_detach_device(drm_dev, ippdrv->dev);
1748
1749                 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1750                                 ippdrv->prop_list.ipp_id);
1751         }
1752
1753         return ret;
1754 }
1755
1756 static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1757 {
1758         struct exynos_drm_ippdrv *ippdrv;
1759         struct ipp_context *ctx = get_ipp_context(dev);
1760
1761         /* get ipp driver entry */
1762         list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1763                 if (is_drm_iommu_supported(drm_dev))
1764                         drm_iommu_detach_device(drm_dev, ippdrv->dev);
1765
1766                 ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
1767                                 ippdrv->prop_list.ipp_id);
1768
1769                 ippdrv->drm_dev = NULL;
1770                 exynos_drm_ippdrv_unregister(ippdrv);
1771         }
1772 }
1773
1774 static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1775                 struct drm_file *file)
1776 {
1777         struct drm_exynos_file_private *file_priv = file->driver_priv;
1778         struct exynos_drm_ipp_private *priv;
1779
1780         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1781         if (!priv)
1782                 return -ENOMEM;
1783         priv->dev = dev;
1784         file_priv->ipp_priv = priv;
1785
1786
1787         DRM_DEBUG_KMS("done priv[0x%x]\n", (int)priv);
1788
1789         return 0;
1790 }
1791
1792 static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1793                 struct drm_file *file)
1794 {
1795         struct drm_exynos_file_private *file_priv = file->driver_priv;
1796         struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1797         struct exynos_drm_ippdrv *ippdrv = NULL;
1798         struct ipp_context *ctx = get_ipp_context(dev);
1799         struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1800         int count = 0;
1801
1802         DRM_DEBUG_KMS("for priv[0x%x]\n", (int)priv);
1803
1804         list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1805                 mutex_lock(&ippdrv->cmd_lock);
1806                 list_for_each_entry_safe(c_node, tc_node,
1807                         &ippdrv->cmd_list, list) {
1808                         DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1809                                 count++, (int)ippdrv);
1810
1811                         if (c_node->priv == priv) {
1812                                 /*
1813                                  * userland goto unnormal state. process killed.
1814                                  * and close the file.
1815                                  * so, IPP didn't called stop cmd ctrl.
1816                                  * so, we are make stop operation in this state.
1817                                  */
1818                                 if (c_node->state == IPP_STATE_START) {
1819                                         ipp_stop_property(drm_dev, ippdrv,
1820                                                 c_node);
1821                                         c_node->state = IPP_STATE_STOP;
1822                                 }
1823
1824                                 ippdrv->dedicated = false;
1825                                 ipp_clean_cmd_node(ctx, c_node);
1826                                 if (list_empty(&ippdrv->cmd_list))
1827                                         pm_runtime_put_sync(ippdrv->dev);
1828                         }
1829                 }
1830                 mutex_unlock(&ippdrv->cmd_lock);
1831         }
1832
1833         kfree(priv);
1834         return;
1835 }
1836
1837 static int ipp_probe(struct platform_device *pdev)
1838 {
1839         struct device *dev = &pdev->dev;
1840         struct ipp_context *ctx;
1841         struct exynos_drm_subdrv *subdrv;
1842         int ret;
1843
1844         ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1845         if (!ctx)
1846                 return -ENOMEM;
1847
1848         mutex_init(&ctx->ipp_lock);
1849         mutex_init(&ctx->prop_lock);
1850
1851         idr_init(&ctx->ipp_idr);
1852         idr_init(&ctx->prop_idr);
1853
1854         /*
1855          * create single thread for ipp event
1856          * IPP supports event thread for IPP drivers.
1857          * IPP driver send event_work to this thread.
1858          * and IPP event thread send event to user process.
1859          */
1860         ctx->event_workq = create_singlethread_workqueue("ipp_event");
1861         if (!ctx->event_workq) {
1862                 dev_err(dev, "failed to create event workqueue\n");
1863                 return -EINVAL;
1864         }
1865
1866         /*
1867          * create single thread for ipp command
1868          * IPP supports command thread for user process.
1869          * user process make command node using set property ioctl.
1870          * and make start_work and send this work to command thread.
1871          * and then this command thread start property.
1872          */
1873         ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1874         if (!ctx->cmd_workq) {
1875                 dev_err(dev, "failed to create cmd workqueue\n");
1876                 ret = -EINVAL;
1877                 goto err_event_workq;
1878         }
1879
1880         /* set sub driver informations */
1881         subdrv = &ctx->subdrv;
1882         subdrv->dev = dev;
1883         subdrv->probe = ipp_subdrv_probe;
1884         subdrv->remove = ipp_subdrv_remove;
1885         subdrv->open = ipp_subdrv_open;
1886         subdrv->close = ipp_subdrv_close;
1887
1888         platform_set_drvdata(pdev, ctx);
1889
1890         ret = exynos_drm_subdrv_register(subdrv);
1891         if (ret < 0) {
1892                 DRM_ERROR("failed to register drm ipp device.\n");
1893                 goto err_cmd_workq;
1894         }
1895
1896         dev_info(dev, "drm ipp registered successfully.\n");
1897
1898         return 0;
1899
1900 err_cmd_workq:
1901         destroy_workqueue(ctx->cmd_workq);
1902 err_event_workq:
1903         destroy_workqueue(ctx->event_workq);
1904         return ret;
1905 }
1906
1907 static int ipp_remove(struct platform_device *pdev)
1908 {
1909         struct ipp_context *ctx = platform_get_drvdata(pdev);
1910
1911         /* unregister sub driver */
1912         exynos_drm_subdrv_unregister(&ctx->subdrv);
1913
1914         /* remove,destroy ipp idr */
1915         idr_destroy(&ctx->ipp_idr);
1916         idr_destroy(&ctx->prop_idr);
1917
1918         mutex_destroy(&ctx->ipp_lock);
1919         mutex_destroy(&ctx->prop_lock);
1920
1921         /* destroy command, event work queue */
1922         destroy_workqueue(ctx->cmd_workq);
1923         destroy_workqueue(ctx->event_workq);
1924
1925         return 0;
1926 }
1927
1928 static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1929 {
1930         DRM_DEBUG_KMS("enable[%d]\n", enable);
1931
1932         return 0;
1933 }
1934
1935 #ifdef CONFIG_PM_SLEEP
1936 static int ipp_suspend(struct device *dev)
1937 {
1938         struct ipp_context *ctx = get_ipp_context(dev);
1939
1940         if (pm_runtime_suspended(dev))
1941                 return 0;
1942
1943         return ipp_power_ctrl(ctx, false);
1944 }
1945
1946 static int ipp_resume(struct device *dev)
1947 {
1948         struct ipp_context *ctx = get_ipp_context(dev);
1949
1950         if (!pm_runtime_suspended(dev))
1951                 return ipp_power_ctrl(ctx, true);
1952
1953         return 0;
1954 }
1955 #endif
1956
1957 #ifdef CONFIG_PM_RUNTIME
1958 static int ipp_runtime_suspend(struct device *dev)
1959 {
1960         struct ipp_context *ctx = get_ipp_context(dev);
1961
1962         return ipp_power_ctrl(ctx, false);
1963 }
1964
1965 static int ipp_runtime_resume(struct device *dev)
1966 {
1967         struct ipp_context *ctx = get_ipp_context(dev);
1968
1969         return ipp_power_ctrl(ctx, true);
1970 }
1971 #endif
1972
1973 static const struct dev_pm_ops ipp_pm_ops = {
1974         SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1975         SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1976 };
1977
1978 struct platform_driver ipp_driver = {
1979         .probe          = ipp_probe,
1980         .remove         = ipp_remove,
1981         .driver         = {
1982                 .name   = "exynos-drm-ipp",
1983                 .owner  = THIS_MODULE,
1984                 .pm     = &ipp_pm_ops,
1985         },
1986 };
1987