2 * Copyright (C) 2012 Samsung Electronics Co.Ltd
4 * Eunchul Kim <chulspro.kim@samsung.com>
5 * Jinyoung Jeon <jy0.jeon@samsung.com>
6 * Sangmin Lee <lsmin.lee@samsung.com>
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.
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 #include <plat/map-base.h>
22 #include <drm/exynos_drm.h>
23 #include "exynos_drm_drv.h"
24 #include "exynos_drm_gem.h"
25 #include "exynos_drm_ipp.h"
26 #include "exynos_drm_iommu.h"
29 * IPP stands for Image Post Processing and
30 * supports image scaler/rotator and input/output DMA operations.
31 * using FIMC, GSC, Rotator, so on.
32 * IPP is integration device driver of same attribute h/w
37 * 1. expand command control id.
38 * 2. integrate property and config.
39 * 3. removed send_event id check routine.
40 * 4. compare send_event id if needed.
41 * 5. free subdrv_remove notifier callback list if needed.
42 * 6. need to check subdrv_open about multi-open.
43 * 7. need to power_on implement power and sysmmu ctrl.
46 #define get_ipp_context(dev) platform_get_drvdata(to_platform_device(dev))
47 #define ipp_is_m2m_cmd(c) (c == IPP_CMD_M2M)
49 /* platform device pointer for ipp device. */
50 static struct platform_device *exynos_drm_ipp_pdev;
53 * A structure of event.
55 * @base: base of event.
58 struct drm_exynos_ipp_send_event {
59 struct drm_pending_event base;
60 struct drm_exynos_ipp_event event;
64 * A structure of memory node.
66 * @list: list head to memory queue information.
67 * @ops_id: id of operations.
68 * @prop_id: id of property.
69 * @buf_id: id of buffer.
70 * @buf_info: gem objects and dma address, size.
71 * @filp: a pointer to drm_file.
73 struct drm_exynos_ipp_mem_node {
74 struct list_head list;
75 enum drm_exynos_ops_id ops_id;
78 struct drm_exynos_ipp_buf_info buf_info;
79 struct drm_file *filp;
83 * A structure of ipp context.
85 * @subdrv: prepare initialization using subdrv.
86 * @ipp_lock: lock for synchronization of access to ipp_idr.
87 * @prop_lock: lock for synchronization of access to prop_idr.
88 * @ipp_idr: ipp driver idr.
89 * @prop_idr: property idr.
90 * @event_workq: event work queue.
91 * @cmd_workq: command work queue.
94 struct exynos_drm_subdrv subdrv;
95 struct mutex ipp_lock;
96 struct mutex prop_lock;
99 struct workqueue_struct *event_workq;
100 struct workqueue_struct *cmd_workq;
103 static LIST_HEAD(exynos_drm_ippdrv_list);
104 static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
105 static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);
107 int exynos_platform_device_ipp_register(void)
109 struct platform_device *pdev;
111 if (exynos_drm_ipp_pdev)
114 pdev = platform_device_register_simple("exynos-drm-ipp", -1, NULL, 0);
116 return PTR_ERR(pdev);
118 exynos_drm_ipp_pdev = pdev;
123 void exynos_platform_device_ipp_unregister(void)
125 if (exynos_drm_ipp_pdev) {
126 platform_device_unregister(exynos_drm_ipp_pdev);
127 exynos_drm_ipp_pdev = NULL;
131 int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
136 mutex_lock(&exynos_drm_ippdrv_lock);
137 list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
138 mutex_unlock(&exynos_drm_ippdrv_lock);
143 int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
148 mutex_lock(&exynos_drm_ippdrv_lock);
149 list_del(&ippdrv->drv_list);
150 mutex_unlock(&exynos_drm_ippdrv_lock);
155 static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj,
160 /* do the allocation under our mutexlock */
162 ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
171 static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
175 DRM_DEBUG_KMS("id[%d]\n", id);
179 /* find object using handle */
180 obj = idr_find(id_idr, id);
182 DRM_ERROR("failed to find object.\n");
184 return ERR_PTR(-ENODEV);
192 static inline bool ipp_check_dedicated(struct exynos_drm_ippdrv *ippdrv,
193 enum drm_exynos_ipp_cmd cmd)
196 * check dedicated flag and WB, OUTPUT operation with
199 if (ippdrv->dedicated || (!ipp_is_m2m_cmd(cmd) &&
200 !pm_runtime_suspended(ippdrv->dev)))
206 static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
207 struct drm_exynos_ipp_property *property)
209 struct exynos_drm_ippdrv *ippdrv;
210 u32 ipp_id = property->ipp_id;
212 DRM_DEBUG_KMS("ipp_id[%d]\n", ipp_id);
215 /* find ipp driver using idr */
216 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
218 if (IS_ERR(ippdrv)) {
219 DRM_ERROR("not found ipp%d driver.\n", ipp_id);
224 * WB, OUTPUT opertion not supported multi-operation.
225 * so, make dedicated state at set property ioctl.
226 * when ipp driver finished operations, clear dedicated flags.
228 if (ipp_check_dedicated(ippdrv, property->cmd)) {
229 DRM_ERROR("already used choose device.\n");
230 return ERR_PTR(-EBUSY);
234 * This is necessary to find correct device in ipp drivers.
235 * ipp drivers have different abilities,
236 * so need to check property.
238 if (ippdrv->check_property &&
239 ippdrv->check_property(ippdrv->dev, property)) {
240 DRM_ERROR("not support property.\n");
241 return ERR_PTR(-EINVAL);
247 * This case is search all ipp driver for finding.
248 * user application don't set ipp_id in this case,
249 * so ipp subsystem search correct driver in driver list.
251 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
252 if (ipp_check_dedicated(ippdrv, property->cmd)) {
253 DRM_DEBUG_KMS("used device.\n");
257 if (ippdrv->check_property &&
258 ippdrv->check_property(ippdrv->dev, property)) {
259 DRM_DEBUG_KMS("not support property.\n");
266 DRM_ERROR("not support ipp driver operations.\n");
269 return ERR_PTR(-ENODEV);
272 static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
274 struct exynos_drm_ippdrv *ippdrv;
275 struct drm_exynos_ipp_cmd_node *c_node;
278 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
280 if (list_empty(&exynos_drm_ippdrv_list)) {
281 DRM_DEBUG_KMS("ippdrv_list is empty.\n");
282 return ERR_PTR(-ENODEV);
286 * This case is search ipp driver by prop_id handle.
287 * sometimes, ipp subsystem find driver by prop_id.
288 * e.g PAUSE state, queue buf, command contro.
290 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
291 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
293 if (!list_empty(&ippdrv->cmd_list)) {
294 list_for_each_entry(c_node, &ippdrv->cmd_list, list)
295 if (c_node->property.prop_id == prop_id)
300 return ERR_PTR(-ENODEV);
303 int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
304 struct drm_file *file)
306 struct drm_exynos_file_private *file_priv = file->driver_priv;
307 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
308 struct device *dev = priv->dev;
309 struct ipp_context *ctx = get_ipp_context(dev);
310 struct drm_exynos_ipp_prop_list *prop_list = data;
311 struct exynos_drm_ippdrv *ippdrv;
315 DRM_ERROR("invalid context.\n");
320 DRM_ERROR("invalid property parameter.\n");
324 DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
326 if (!prop_list->ipp_id) {
327 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
330 * Supports ippdrv list count for user application.
331 * First step user application getting ippdrv count.
332 * and second step getting ippdrv capability using ipp_id.
334 prop_list->count = count;
337 * Getting ippdrv capability by ipp_id.
338 * some deivce not supported wb, output interface.
339 * so, user application detect correct ipp driver
342 ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
344 if (IS_ERR(ippdrv)) {
345 DRM_ERROR("not found ipp%d driver.\n",
347 return PTR_ERR(ippdrv);
350 prop_list = ippdrv->prop_list;
356 static void ipp_print_property(struct drm_exynos_ipp_property *property,
359 struct drm_exynos_ipp_config *config = &property->config[idx];
360 struct drm_exynos_pos *pos = &config->pos;
361 struct drm_exynos_sz *sz = &config->sz;
363 DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
364 property->prop_id, idx ? "dst" : "src", config->fmt);
366 DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
367 pos->x, pos->y, pos->w, pos->h,
368 sz->hsize, sz->vsize, config->flip, config->degree);
371 static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
373 struct exynos_drm_ippdrv *ippdrv;
374 struct drm_exynos_ipp_cmd_node *c_node;
375 u32 prop_id = property->prop_id;
377 DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
379 ippdrv = ipp_find_drv_by_handle(prop_id);
380 if (IS_ERR(ippdrv)) {
381 DRM_ERROR("failed to get ipp driver.\n");
386 * Find command node using command list in ippdrv.
387 * when we find this command no using prop_id.
388 * return property information set in this command node.
390 list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
391 if ((c_node->property.prop_id == prop_id) &&
392 (c_node->state == IPP_STATE_STOP)) {
393 DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
394 property->cmd, (int)ippdrv);
396 c_node->property = *property;
401 DRM_ERROR("failed to search property.\n");
406 static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
408 struct drm_exynos_ipp_cmd_work *cmd_work;
410 cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
412 DRM_ERROR("failed to alloc cmd_work.\n");
413 return ERR_PTR(-ENOMEM);
416 INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);
421 static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
423 struct drm_exynos_ipp_event_work *event_work;
425 event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
427 DRM_ERROR("failed to alloc event_work.\n");
428 return ERR_PTR(-ENOMEM);
431 INIT_WORK((struct work_struct *)event_work, ipp_sched_event);
436 int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
437 struct drm_file *file)
439 struct drm_exynos_file_private *file_priv = file->driver_priv;
440 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
441 struct device *dev = priv->dev;
442 struct ipp_context *ctx = get_ipp_context(dev);
443 struct drm_exynos_ipp_property *property = data;
444 struct exynos_drm_ippdrv *ippdrv;
445 struct drm_exynos_ipp_cmd_node *c_node;
449 DRM_ERROR("invalid context.\n");
454 DRM_ERROR("invalid property parameter.\n");
459 * This is log print for user application property.
460 * user application set various property.
463 ipp_print_property(property, i);
466 * set property ioctl generated new prop_id.
467 * but in this case already asigned prop_id using old set property.
468 * e.g PAUSE state. this case supports find current prop_id and use it
469 * instead of allocation.
471 if (property->prop_id) {
472 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
473 return ipp_find_and_set_property(property);
476 /* find ipp driver using ipp id */
477 ippdrv = ipp_find_driver(ctx, property);
478 if (IS_ERR(ippdrv)) {
479 DRM_ERROR("failed to get ipp driver.\n");
483 /* allocate command node */
484 c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
486 DRM_ERROR("failed to allocate map node.\n");
490 /* create property id */
491 ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node,
494 DRM_ERROR("failed to create id.\n");
498 DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
499 property->prop_id, property->cmd, (int)ippdrv);
501 /* stored property information and ippdrv in private data */
503 c_node->property = *property;
504 c_node->state = IPP_STATE_IDLE;
506 c_node->start_work = ipp_create_cmd_work();
507 if (IS_ERR(c_node->start_work)) {
508 DRM_ERROR("failed to create start work.\n");
512 c_node->stop_work = ipp_create_cmd_work();
513 if (IS_ERR(c_node->stop_work)) {
514 DRM_ERROR("failed to create stop work.\n");
518 c_node->event_work = ipp_create_event_work();
519 if (IS_ERR(c_node->event_work)) {
520 DRM_ERROR("failed to create event work.\n");
524 mutex_init(&c_node->cmd_lock);
525 mutex_init(&c_node->mem_lock);
526 mutex_init(&c_node->event_lock);
528 init_completion(&c_node->start_complete);
529 init_completion(&c_node->stop_complete);
532 INIT_LIST_HEAD(&c_node->mem_list[i]);
534 INIT_LIST_HEAD(&c_node->event_list);
535 list_splice_init(&priv->event_list, &c_node->event_list);
536 list_add_tail(&c_node->list, &ippdrv->cmd_list);
538 /* make dedicated state without m2m */
539 if (!ipp_is_m2m_cmd(property->cmd))
540 ippdrv->dedicated = true;
545 kfree(c_node->stop_work);
547 kfree(c_node->start_work);
553 static void ipp_clean_cmd_node(struct drm_exynos_ipp_cmd_node *c_node)
556 list_del(&c_node->list);
559 mutex_destroy(&c_node->cmd_lock);
560 mutex_destroy(&c_node->mem_lock);
561 mutex_destroy(&c_node->event_lock);
563 /* free command node */
564 kfree(c_node->start_work);
565 kfree(c_node->stop_work);
566 kfree(c_node->event_work);
570 static int ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
572 struct drm_exynos_ipp_property *property = &c_node->property;
573 struct drm_exynos_ipp_mem_node *m_node;
574 struct list_head *head;
575 int ret, i, count[EXYNOS_DRM_OPS_MAX] = { 0, };
577 mutex_lock(&c_node->mem_lock);
579 for_each_ipp_ops(i) {
580 /* source/destination memory list */
581 head = &c_node->mem_list[i];
583 if (list_empty(head)) {
584 DRM_DEBUG_KMS("%s memory empty.\n", i ? "dst" : "src");
588 /* find memory node entry */
589 list_for_each_entry(m_node, head, list) {
590 DRM_DEBUG_KMS("%s,count[%d]m_node[0x%x]\n",
591 i ? "dst" : "src", count[i], (int)m_node);
596 DRM_DEBUG_KMS("min[%d]max[%d]\n",
597 min(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]),
598 max(count[EXYNOS_DRM_OPS_SRC], count[EXYNOS_DRM_OPS_DST]));
601 * M2M operations should be need paired memory address.
602 * so, need to check minimum count about src, dst.
603 * other case not use paired memory, so use maximum count
605 if (ipp_is_m2m_cmd(property->cmd))
606 ret = min(count[EXYNOS_DRM_OPS_SRC],
607 count[EXYNOS_DRM_OPS_DST]);
609 ret = max(count[EXYNOS_DRM_OPS_SRC],
610 count[EXYNOS_DRM_OPS_DST]);
612 mutex_unlock(&c_node->mem_lock);
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)
621 struct drm_exynos_ipp_mem_node *m_node;
622 struct list_head *head;
625 DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
627 /* source/destination memory list */
628 head = &c_node->mem_list[qbuf->ops_id];
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);
634 /* compare buffer id */
635 if (m_node->buf_id == qbuf->buf_id)
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)
646 struct exynos_drm_ipp_ops *ops = NULL;
649 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
652 DRM_ERROR("invalid queue node.\n");
656 mutex_lock(&c_node->mem_lock);
658 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
660 /* get operations callback */
661 ops = ippdrv->ops[m_node->ops_id];
663 DRM_ERROR("not support ops.\n");
668 /* set address and enable irq */
670 ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
671 m_node->buf_id, IPP_BUF_ENQUEUE);
673 DRM_ERROR("failed to set addr.\n");
679 mutex_unlock(&c_node->mem_lock);
683 static struct drm_exynos_ipp_mem_node
684 *ipp_get_mem_node(struct drm_device *drm_dev,
685 struct drm_file *file,
686 struct drm_exynos_ipp_cmd_node *c_node,
687 struct drm_exynos_ipp_queue_buf *qbuf)
689 struct drm_exynos_ipp_mem_node *m_node;
690 struct drm_exynos_ipp_buf_info buf_info;
694 mutex_lock(&c_node->mem_lock);
696 m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
698 DRM_ERROR("failed to allocate queue node.\n");
702 /* clear base address for error handling */
703 memset(&buf_info, 0x0, sizeof(buf_info));
705 /* operations, buffer id */
706 m_node->ops_id = qbuf->ops_id;
707 m_node->prop_id = qbuf->prop_id;
708 m_node->buf_id = qbuf->buf_id;
710 DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
711 DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
713 for_each_ipp_planar(i) {
714 DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
716 /* get dma address by handle */
717 if (qbuf->handle[i]) {
718 addr = exynos_drm_gem_get_dma_addr(drm_dev,
719 qbuf->handle[i], file);
721 DRM_ERROR("failed to get addr.\n");
725 buf_info.handles[i] = qbuf->handle[i];
726 buf_info.base[i] = *(dma_addr_t *) addr;
727 DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%x]\n",
728 i, buf_info.base[i], (int)buf_info.handles[i]);
733 m_node->buf_info = buf_info;
734 list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
736 mutex_unlock(&c_node->mem_lock);
742 mutex_unlock(&c_node->mem_lock);
743 return ERR_PTR(-EFAULT);
746 static int ipp_put_mem_node(struct drm_device *drm_dev,
747 struct drm_exynos_ipp_cmd_node *c_node,
748 struct drm_exynos_ipp_mem_node *m_node)
752 DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
755 DRM_ERROR("invalid dequeue node.\n");
759 if (list_empty(&m_node->list)) {
760 DRM_ERROR("empty memory node.\n");
764 mutex_lock(&c_node->mem_lock);
766 DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
769 for_each_ipp_planar(i) {
770 unsigned long handle = m_node->buf_info.handles[i];
772 exynos_drm_gem_put_dma_addr(drm_dev, handle,
776 /* delete list in queue */
777 list_del(&m_node->list);
780 mutex_unlock(&c_node->mem_lock);
785 static void ipp_free_event(struct drm_pending_event *event)
790 static int ipp_get_event(struct drm_device *drm_dev,
791 struct drm_file *file,
792 struct drm_exynos_ipp_cmd_node *c_node,
793 struct drm_exynos_ipp_queue_buf *qbuf)
795 struct drm_exynos_ipp_send_event *e;
798 DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
800 e = kzalloc(sizeof(*e), GFP_KERNEL);
803 DRM_ERROR("failed to allocate event.\n");
804 spin_lock_irqsave(&drm_dev->event_lock, flags);
805 file->event_space += sizeof(e->event);
806 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
811 e->event.base.type = DRM_EXYNOS_IPP_EVENT;
812 e->event.base.length = sizeof(e->event);
813 e->event.user_data = qbuf->user_data;
814 e->event.prop_id = qbuf->prop_id;
815 e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
816 e->base.event = &e->event.base;
817 e->base.file_priv = file;
818 e->base.destroy = ipp_free_event;
819 list_add_tail(&e->base.link, &c_node->event_list);
824 static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
825 struct drm_exynos_ipp_queue_buf *qbuf)
827 struct drm_exynos_ipp_send_event *e, *te;
830 if (list_empty(&c_node->event_list)) {
831 DRM_DEBUG_KMS("event_list is empty.\n");
835 list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
836 DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
839 * quf == NULL condition means all event deletion.
840 * stop operations want to delete all event list.
841 * another case delete only same buf id.
845 list_del(&e->base.link);
849 /* compare buffer id */
850 if (qbuf && (qbuf->buf_id ==
851 e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
853 list_del(&e->base.link);
860 static void ipp_handle_cmd_work(struct device *dev,
861 struct exynos_drm_ippdrv *ippdrv,
862 struct drm_exynos_ipp_cmd_work *cmd_work,
863 struct drm_exynos_ipp_cmd_node *c_node)
865 struct ipp_context *ctx = get_ipp_context(dev);
867 cmd_work->ippdrv = ippdrv;
868 cmd_work->c_node = c_node;
869 queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
872 static int ipp_queue_buf_with_run(struct device *dev,
873 struct drm_exynos_ipp_cmd_node *c_node,
874 struct drm_exynos_ipp_mem_node *m_node,
875 struct drm_exynos_ipp_queue_buf *qbuf)
877 struct exynos_drm_ippdrv *ippdrv;
878 struct drm_exynos_ipp_property *property;
879 struct exynos_drm_ipp_ops *ops;
882 ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
883 if (IS_ERR(ippdrv)) {
884 DRM_ERROR("failed to get ipp driver.\n");
888 ops = ippdrv->ops[qbuf->ops_id];
890 DRM_ERROR("failed to get ops.\n");
894 property = &c_node->property;
896 if (c_node->state != IPP_STATE_START) {
897 DRM_DEBUG_KMS("bypass for invalid state.\n");
901 if (!ipp_check_mem_list(c_node)) {
902 DRM_DEBUG_KMS("empty memory.\n");
907 * If set destination buffer and enabled clock,
908 * then m2m operations need start operations at queue_buf
910 if (ipp_is_m2m_cmd(property->cmd)) {
911 struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;
913 cmd_work->ctrl = IPP_CTRL_PLAY;
914 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
916 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
918 DRM_ERROR("failed to set m node.\n");
926 static void ipp_clean_queue_buf(struct drm_device *drm_dev,
927 struct drm_exynos_ipp_cmd_node *c_node,
928 struct drm_exynos_ipp_queue_buf *qbuf)
930 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
932 if (!list_empty(&c_node->mem_list[qbuf->ops_id])) {
934 list_for_each_entry_safe(m_node, tm_node,
935 &c_node->mem_list[qbuf->ops_id], list) {
936 if (m_node->buf_id == qbuf->buf_id &&
937 m_node->ops_id == qbuf->ops_id)
938 ipp_put_mem_node(drm_dev, c_node, m_node);
943 int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
944 struct drm_file *file)
946 struct drm_exynos_file_private *file_priv = file->driver_priv;
947 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
948 struct device *dev = priv->dev;
949 struct ipp_context *ctx = get_ipp_context(dev);
950 struct drm_exynos_ipp_queue_buf *qbuf = data;
951 struct drm_exynos_ipp_cmd_node *c_node;
952 struct drm_exynos_ipp_mem_node *m_node;
956 DRM_ERROR("invalid buf parameter.\n");
960 if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
961 DRM_ERROR("invalid ops parameter.\n");
965 DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
966 qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
967 qbuf->buf_id, qbuf->buf_type);
969 /* find command node */
970 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
972 if (IS_ERR(c_node)) {
973 DRM_ERROR("failed to get command node.\n");
974 return PTR_ERR(c_node);
978 switch (qbuf->buf_type) {
979 case IPP_BUF_ENQUEUE:
980 /* get memory node */
981 m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
982 if (IS_ERR(m_node)) {
983 DRM_ERROR("failed to get m_node.\n");
984 return PTR_ERR(m_node);
988 * first step get event for destination buffer.
989 * and second step when M2M case run with destination buffer
992 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
993 /* get event for destination buffer */
994 ret = ipp_get_event(drm_dev, file, c_node, qbuf);
996 DRM_ERROR("failed to get event.\n");
1001 * M2M case run play control for streaming feature.
1002 * other case set address and waiting.
1004 ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
1006 DRM_ERROR("failed to run command.\n");
1007 goto err_clean_node;
1011 case IPP_BUF_DEQUEUE:
1012 mutex_lock(&c_node->cmd_lock);
1014 /* put event for destination buffer */
1015 if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
1016 ipp_put_event(c_node, qbuf);
1018 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1020 mutex_unlock(&c_node->cmd_lock);
1023 DRM_ERROR("invalid buffer control.\n");
1030 DRM_ERROR("clean memory nodes.\n");
1032 ipp_clean_queue_buf(drm_dev, c_node, qbuf);
1036 static bool exynos_drm_ipp_check_valid(struct device *dev,
1037 enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
1039 if (ctrl != IPP_CTRL_PLAY) {
1040 if (pm_runtime_suspended(dev)) {
1041 DRM_ERROR("pm:runtime_suspended.\n");
1048 if (state != IPP_STATE_IDLE)
1052 if (state == IPP_STATE_STOP)
1055 case IPP_CTRL_PAUSE:
1056 if (state != IPP_STATE_START)
1059 case IPP_CTRL_RESUME:
1060 if (state != IPP_STATE_STOP)
1064 DRM_ERROR("invalid state.\n");
1071 DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
1075 int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
1076 struct drm_file *file)
1078 struct drm_exynos_file_private *file_priv = file->driver_priv;
1079 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1080 struct exynos_drm_ippdrv *ippdrv = NULL;
1081 struct device *dev = priv->dev;
1082 struct ipp_context *ctx = get_ipp_context(dev);
1083 struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
1084 struct drm_exynos_ipp_cmd_work *cmd_work;
1085 struct drm_exynos_ipp_cmd_node *c_node;
1088 DRM_ERROR("invalid context.\n");
1093 DRM_ERROR("invalid control parameter.\n");
1097 DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
1098 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1100 ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
1101 if (IS_ERR(ippdrv)) {
1102 DRM_ERROR("failed to get ipp driver.\n");
1103 return PTR_ERR(ippdrv);
1106 c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
1108 if (IS_ERR(c_node)) {
1109 DRM_ERROR("invalid command node list.\n");
1110 return PTR_ERR(c_node);
1113 if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
1115 DRM_ERROR("invalid state.\n");
1119 switch (cmd_ctrl->ctrl) {
1121 if (pm_runtime_suspended(ippdrv->dev))
1122 pm_runtime_get_sync(ippdrv->dev);
1123 c_node->state = IPP_STATE_START;
1125 cmd_work = c_node->start_work;
1126 cmd_work->ctrl = cmd_ctrl->ctrl;
1127 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1128 c_node->state = IPP_STATE_START;
1131 cmd_work = c_node->stop_work;
1132 cmd_work->ctrl = cmd_ctrl->ctrl;
1133 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1135 if (!wait_for_completion_timeout(&c_node->stop_complete,
1136 msecs_to_jiffies(300))) {
1137 DRM_ERROR("timeout stop:prop_id[%d]\n",
1138 c_node->property.prop_id);
1141 c_node->state = IPP_STATE_STOP;
1142 ippdrv->dedicated = false;
1143 ipp_clean_cmd_node(c_node);
1145 if (list_empty(&ippdrv->cmd_list))
1146 pm_runtime_put_sync(ippdrv->dev);
1148 case IPP_CTRL_PAUSE:
1149 cmd_work = c_node->stop_work;
1150 cmd_work->ctrl = cmd_ctrl->ctrl;
1151 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1153 if (!wait_for_completion_timeout(&c_node->stop_complete,
1154 msecs_to_jiffies(200))) {
1155 DRM_ERROR("timeout stop:prop_id[%d]\n",
1156 c_node->property.prop_id);
1159 c_node->state = IPP_STATE_STOP;
1161 case IPP_CTRL_RESUME:
1162 c_node->state = IPP_STATE_START;
1163 cmd_work = c_node->start_work;
1164 cmd_work->ctrl = cmd_ctrl->ctrl;
1165 ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
1168 DRM_ERROR("could not support this state currently.\n");
1172 DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
1173 cmd_ctrl->ctrl, cmd_ctrl->prop_id);
1178 int exynos_drm_ippnb_register(struct notifier_block *nb)
1180 return blocking_notifier_chain_register(
1181 &exynos_drm_ippnb_list, nb);
1184 int exynos_drm_ippnb_unregister(struct notifier_block *nb)
1186 return blocking_notifier_chain_unregister(
1187 &exynos_drm_ippnb_list, nb);
1190 int exynos_drm_ippnb_send_event(unsigned long val, void *v)
1192 return blocking_notifier_call_chain(
1193 &exynos_drm_ippnb_list, val, v);
1196 static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
1197 struct drm_exynos_ipp_property *property)
1199 struct exynos_drm_ipp_ops *ops = NULL;
1204 DRM_ERROR("invalid property parameter.\n");
1208 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
1210 /* reset h/w block */
1211 if (ippdrv->reset &&
1212 ippdrv->reset(ippdrv->dev)) {
1213 DRM_ERROR("failed to reset.\n");
1217 /* set source,destination operations */
1218 for_each_ipp_ops(i) {
1219 struct drm_exynos_ipp_config *config =
1220 &property->config[i];
1222 ops = ippdrv->ops[i];
1223 if (!ops || !config) {
1224 DRM_ERROR("not support ops and config.\n");
1230 ret = ops->set_fmt(ippdrv->dev, config->fmt);
1232 DRM_ERROR("not support format.\n");
1237 /* set transform for rotation, flip */
1238 if (ops->set_transf) {
1239 ret = ops->set_transf(ippdrv->dev, config->degree,
1240 config->flip, &swap);
1242 DRM_ERROR("not support tranf.\n");
1248 if (ops->set_size) {
1249 ret = ops->set_size(ippdrv->dev, swap, &config->pos,
1252 DRM_ERROR("not support size.\n");
1261 static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
1262 struct drm_exynos_ipp_cmd_node *c_node)
1264 struct drm_exynos_ipp_mem_node *m_node;
1265 struct drm_exynos_ipp_property *property = &c_node->property;
1266 struct list_head *head;
1269 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
1271 /* store command info in ippdrv */
1272 ippdrv->c_node = c_node;
1274 if (!ipp_check_mem_list(c_node)) {
1275 DRM_DEBUG_KMS("empty memory.\n");
1279 /* set current property in ippdrv */
1280 ret = ipp_set_property(ippdrv, property);
1282 DRM_ERROR("failed to set property.\n");
1283 ippdrv->c_node = NULL;
1288 switch (property->cmd) {
1290 for_each_ipp_ops(i) {
1291 /* source/destination memory list */
1292 head = &c_node->mem_list[i];
1294 m_node = list_first_entry(head,
1295 struct drm_exynos_ipp_mem_node, list);
1297 DRM_ERROR("failed to get node.\n");
1302 DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
1304 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1306 DRM_ERROR("failed to set m node.\n");
1312 /* destination memory list */
1313 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1315 list_for_each_entry(m_node, head, list) {
1316 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1318 DRM_ERROR("failed to set m node.\n");
1323 case IPP_CMD_OUTPUT:
1324 /* source memory list */
1325 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1327 list_for_each_entry(m_node, head, list) {
1328 ret = ipp_set_mem_node(ippdrv, c_node, m_node);
1330 DRM_ERROR("failed to set m node.\n");
1336 DRM_ERROR("invalid operations.\n");
1340 DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
1342 /* start operations */
1343 if (ippdrv->start) {
1344 ret = ippdrv->start(ippdrv->dev, property->cmd);
1346 DRM_ERROR("failed to start ops.\n");
1354 static int ipp_stop_property(struct drm_device *drm_dev,
1355 struct exynos_drm_ippdrv *ippdrv,
1356 struct drm_exynos_ipp_cmd_node *c_node)
1358 struct drm_exynos_ipp_mem_node *m_node, *tm_node;
1359 struct drm_exynos_ipp_property *property = &c_node->property;
1360 struct list_head *head;
1363 DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
1366 ipp_put_event(c_node, NULL);
1369 switch (property->cmd) {
1371 for_each_ipp_ops(i) {
1372 /* source/destination memory list */
1373 head = &c_node->mem_list[i];
1375 if (list_empty(head)) {
1376 DRM_DEBUG_KMS("mem_list is empty.\n");
1380 list_for_each_entry_safe(m_node, tm_node,
1382 ret = ipp_put_mem_node(drm_dev, c_node,
1385 DRM_ERROR("failed to put m_node.\n");
1392 /* destination memory list */
1393 head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];
1395 if (list_empty(head)) {
1396 DRM_DEBUG_KMS("mem_list is empty.\n");
1400 list_for_each_entry_safe(m_node, tm_node, head, list) {
1401 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1403 DRM_ERROR("failed to put m_node.\n");
1408 case IPP_CMD_OUTPUT:
1409 /* source memory list */
1410 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1412 if (list_empty(head)) {
1413 DRM_DEBUG_KMS("mem_list is empty.\n");
1417 list_for_each_entry_safe(m_node, tm_node, head, list) {
1418 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1420 DRM_ERROR("failed to put m_node.\n");
1426 DRM_ERROR("invalid operations.\n");
1432 /* stop operations */
1434 ippdrv->stop(ippdrv->dev, property->cmd);
1439 void ipp_sched_cmd(struct work_struct *work)
1441 struct drm_exynos_ipp_cmd_work *cmd_work =
1442 (struct drm_exynos_ipp_cmd_work *)work;
1443 struct exynos_drm_ippdrv *ippdrv;
1444 struct drm_exynos_ipp_cmd_node *c_node;
1445 struct drm_exynos_ipp_property *property;
1448 ippdrv = cmd_work->ippdrv;
1450 DRM_ERROR("invalid ippdrv list.\n");
1454 c_node = cmd_work->c_node;
1456 DRM_ERROR("invalid command node list.\n");
1460 mutex_lock(&c_node->cmd_lock);
1462 property = &c_node->property;
1464 switch (cmd_work->ctrl) {
1466 case IPP_CTRL_RESUME:
1467 ret = ipp_start_property(ippdrv, c_node);
1469 DRM_ERROR("failed to start property:prop_id[%d]\n",
1470 c_node->property.prop_id);
1475 * M2M case supports wait_completion of transfer.
1476 * because M2M case supports single unit operation
1477 * with multiple queue.
1478 * M2M need to wait completion of data transfer.
1480 if (ipp_is_m2m_cmd(property->cmd)) {
1481 if (!wait_for_completion_timeout
1482 (&c_node->start_complete, msecs_to_jiffies(200))) {
1483 DRM_ERROR("timeout event:prop_id[%d]\n",
1484 c_node->property.prop_id);
1490 case IPP_CTRL_PAUSE:
1491 ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
1494 DRM_ERROR("failed to stop property.\n");
1498 complete(&c_node->stop_complete);
1501 DRM_ERROR("unknown control type\n");
1505 DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
1508 mutex_unlock(&c_node->cmd_lock);
1511 static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
1512 struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
1514 struct drm_device *drm_dev = ippdrv->drm_dev;
1515 struct drm_exynos_ipp_property *property = &c_node->property;
1516 struct drm_exynos_ipp_mem_node *m_node;
1517 struct drm_exynos_ipp_queue_buf qbuf;
1518 struct drm_exynos_ipp_send_event *e;
1519 struct list_head *head;
1521 unsigned long flags;
1522 u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
1526 DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
1529 DRM_ERROR("failed to get drm_dev.\n");
1534 DRM_ERROR("failed to get property.\n");
1538 if (list_empty(&c_node->event_list)) {
1539 DRM_DEBUG_KMS("event list is empty.\n");
1543 if (!ipp_check_mem_list(c_node)) {
1544 DRM_DEBUG_KMS("empty memory.\n");
1549 switch (property->cmd) {
1551 for_each_ipp_ops(i) {
1552 /* source/destination memory list */
1553 head = &c_node->mem_list[i];
1555 m_node = list_first_entry(head,
1556 struct drm_exynos_ipp_mem_node, list);
1558 DRM_ERROR("empty memory node.\n");
1562 tbuf_id[i] = m_node->buf_id;
1563 DRM_DEBUG_KMS("%s buf_id[%d]\n",
1564 i ? "dst" : "src", tbuf_id[i]);
1566 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1568 DRM_ERROR("failed to put m_node.\n");
1572 /* clear buf for finding */
1573 memset(&qbuf, 0x0, sizeof(qbuf));
1574 qbuf.ops_id = EXYNOS_DRM_OPS_DST;
1575 qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];
1577 /* get memory node entry */
1578 m_node = ipp_find_mem_node(c_node, &qbuf);
1580 DRM_ERROR("empty memory node.\n");
1584 tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;
1586 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1588 DRM_ERROR("failed to put m_node.\n");
1590 case IPP_CMD_OUTPUT:
1591 /* source memory list */
1592 head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];
1594 m_node = list_first_entry(head,
1595 struct drm_exynos_ipp_mem_node, list);
1597 DRM_ERROR("empty memory node.\n");
1601 tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;
1603 ret = ipp_put_mem_node(drm_dev, c_node, m_node);
1605 DRM_ERROR("failed to put m_node.\n");
1608 DRM_ERROR("invalid operations.\n");
1612 if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
1613 DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
1614 tbuf_id[1], buf_id[1], property->prop_id);
1617 * command node have event list of destination buffer
1618 * If destination buffer enqueue to mem list,
1619 * then we make event and link to event list tail.
1620 * so, we get first event for first enqueued buffer.
1622 e = list_first_entry(&c_node->event_list,
1623 struct drm_exynos_ipp_send_event, base.link);
1626 DRM_ERROR("empty event.\n");
1630 do_gettimeofday(&now);
1631 DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
1632 e->event.tv_sec = now.tv_sec;
1633 e->event.tv_usec = now.tv_usec;
1634 e->event.prop_id = property->prop_id;
1636 /* set buffer id about source destination */
1638 e->event.buf_id[i] = tbuf_id[i];
1640 spin_lock_irqsave(&drm_dev->event_lock, flags);
1641 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1642 wake_up_interruptible(&e->base.file_priv->event_wait);
1643 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1645 DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
1646 property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);
1651 void ipp_sched_event(struct work_struct *work)
1653 struct drm_exynos_ipp_event_work *event_work =
1654 (struct drm_exynos_ipp_event_work *)work;
1655 struct exynos_drm_ippdrv *ippdrv;
1656 struct drm_exynos_ipp_cmd_node *c_node;
1660 DRM_ERROR("failed to get event_work.\n");
1664 DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
1666 ippdrv = event_work->ippdrv;
1668 DRM_ERROR("failed to get ipp driver.\n");
1672 c_node = ippdrv->c_node;
1674 DRM_ERROR("failed to get command node.\n");
1679 * IPP supports command thread, event thread synchronization.
1680 * If IPP close immediately from user land, then IPP make
1681 * synchronization with command thread, so make complete event.
1682 * or going out operations.
1684 if (c_node->state != IPP_STATE_START) {
1685 DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
1686 c_node->state, c_node->property.prop_id);
1687 goto err_completion;
1690 mutex_lock(&c_node->event_lock);
1692 ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
1694 DRM_ERROR("failed to send event.\n");
1695 goto err_completion;
1699 if (ipp_is_m2m_cmd(c_node->property.cmd))
1700 complete(&c_node->start_complete);
1702 mutex_unlock(&c_node->event_lock);
1705 static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
1707 struct ipp_context *ctx = get_ipp_context(dev);
1708 struct exynos_drm_ippdrv *ippdrv;
1711 /* get ipp driver entry */
1712 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1713 ippdrv->drm_dev = drm_dev;
1715 ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv,
1718 DRM_ERROR("failed to create id.\n");
1722 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
1723 count++, (int)ippdrv, ippdrv->ipp_id);
1725 if (ippdrv->ipp_id == 0) {
1726 DRM_ERROR("failed to get ipp_id[%d]\n",
1731 /* store parent device for node */
1732 ippdrv->parent_dev = dev;
1734 /* store event work queue and handler */
1735 ippdrv->event_workq = ctx->event_workq;
1736 ippdrv->sched_event = ipp_sched_event;
1737 INIT_LIST_HEAD(&ippdrv->cmd_list);
1739 if (is_drm_iommu_supported(drm_dev)) {
1740 ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
1742 DRM_ERROR("failed to activate iommu\n");
1751 /* get ipp driver entry */
1752 list_for_each_entry_reverse(ippdrv, &exynos_drm_ippdrv_list, drv_list)
1753 if (is_drm_iommu_supported(drm_dev))
1754 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1757 idr_destroy(&ctx->ipp_idr);
1758 idr_destroy(&ctx->prop_idr);
1762 static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
1764 struct exynos_drm_ippdrv *ippdrv;
1766 /* get ipp driver entry */
1767 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1768 if (is_drm_iommu_supported(drm_dev))
1769 drm_iommu_detach_device(drm_dev, ippdrv->dev);
1771 ippdrv->drm_dev = NULL;
1772 exynos_drm_ippdrv_unregister(ippdrv);
1776 static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
1777 struct drm_file *file)
1779 struct drm_exynos_file_private *file_priv = file->driver_priv;
1780 struct exynos_drm_ipp_private *priv;
1782 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1784 DRM_ERROR("failed to allocate priv.\n");
1788 file_priv->ipp_priv = priv;
1790 INIT_LIST_HEAD(&priv->event_list);
1792 DRM_DEBUG_KMS("done priv[0x%x]\n", (int)priv);
1797 static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
1798 struct drm_file *file)
1800 struct drm_exynos_file_private *file_priv = file->driver_priv;
1801 struct exynos_drm_ipp_private *priv = file_priv->ipp_priv;
1802 struct exynos_drm_ippdrv *ippdrv = NULL;
1803 struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
1806 DRM_DEBUG_KMS("for priv[0x%x]\n", (int)priv);
1808 if (list_empty(&exynos_drm_ippdrv_list)) {
1809 DRM_DEBUG_KMS("ippdrv_list is empty.\n");
1813 list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1814 if (list_empty(&ippdrv->cmd_list))
1817 list_for_each_entry_safe(c_node, tc_node,
1818 &ippdrv->cmd_list, list) {
1819 DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
1820 count++, (int)ippdrv);
1822 if (c_node->priv == priv) {
1824 * userland goto unnormal state. process killed.
1825 * and close the file.
1826 * so, IPP didn't called stop cmd ctrl.
1827 * so, we are make stop operation in this state.
1829 if (c_node->state == IPP_STATE_START) {
1830 ipp_stop_property(drm_dev, ippdrv,
1832 c_node->state = IPP_STATE_STOP;
1835 ippdrv->dedicated = false;
1836 ipp_clean_cmd_node(c_node);
1837 if (list_empty(&ippdrv->cmd_list))
1838 pm_runtime_put_sync(ippdrv->dev);
1848 static int ipp_probe(struct platform_device *pdev)
1850 struct device *dev = &pdev->dev;
1851 struct ipp_context *ctx;
1852 struct exynos_drm_subdrv *subdrv;
1855 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1859 mutex_init(&ctx->ipp_lock);
1860 mutex_init(&ctx->prop_lock);
1862 idr_init(&ctx->ipp_idr);
1863 idr_init(&ctx->prop_idr);
1866 * create single thread for ipp event
1867 * IPP supports event thread for IPP drivers.
1868 * IPP driver send event_work to this thread.
1869 * and IPP event thread send event to user process.
1871 ctx->event_workq = create_singlethread_workqueue("ipp_event");
1872 if (!ctx->event_workq) {
1873 dev_err(dev, "failed to create event workqueue\n");
1878 * create single thread for ipp command
1879 * IPP supports command thread for user process.
1880 * user process make command node using set property ioctl.
1881 * and make start_work and send this work to command thread.
1882 * and then this command thread start property.
1884 ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
1885 if (!ctx->cmd_workq) {
1886 dev_err(dev, "failed to create cmd workqueue\n");
1888 goto err_event_workq;
1891 /* set sub driver informations */
1892 subdrv = &ctx->subdrv;
1894 subdrv->probe = ipp_subdrv_probe;
1895 subdrv->remove = ipp_subdrv_remove;
1896 subdrv->open = ipp_subdrv_open;
1897 subdrv->close = ipp_subdrv_close;
1899 platform_set_drvdata(pdev, ctx);
1901 ret = exynos_drm_subdrv_register(subdrv);
1903 DRM_ERROR("failed to register drm ipp device.\n");
1907 dev_info(dev, "drm ipp registered successfully.\n");
1912 destroy_workqueue(ctx->cmd_workq);
1914 destroy_workqueue(ctx->event_workq);
1918 static int ipp_remove(struct platform_device *pdev)
1920 struct ipp_context *ctx = platform_get_drvdata(pdev);
1922 /* unregister sub driver */
1923 exynos_drm_subdrv_unregister(&ctx->subdrv);
1925 /* remove,destroy ipp idr */
1926 idr_destroy(&ctx->ipp_idr);
1927 idr_destroy(&ctx->prop_idr);
1929 mutex_destroy(&ctx->ipp_lock);
1930 mutex_destroy(&ctx->prop_lock);
1932 /* destroy command, event work queue */
1933 destroy_workqueue(ctx->cmd_workq);
1934 destroy_workqueue(ctx->event_workq);
1939 static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
1941 DRM_DEBUG_KMS("enable[%d]\n", enable);
1946 #ifdef CONFIG_PM_SLEEP
1947 static int ipp_suspend(struct device *dev)
1949 struct ipp_context *ctx = get_ipp_context(dev);
1951 if (pm_runtime_suspended(dev))
1954 return ipp_power_ctrl(ctx, false);
1957 static int ipp_resume(struct device *dev)
1959 struct ipp_context *ctx = get_ipp_context(dev);
1961 if (!pm_runtime_suspended(dev))
1962 return ipp_power_ctrl(ctx, true);
1968 #ifdef CONFIG_PM_RUNTIME
1969 static int ipp_runtime_suspend(struct device *dev)
1971 struct ipp_context *ctx = get_ipp_context(dev);
1973 return ipp_power_ctrl(ctx, false);
1976 static int ipp_runtime_resume(struct device *dev)
1978 struct ipp_context *ctx = get_ipp_context(dev);
1980 return ipp_power_ctrl(ctx, true);
1984 static const struct dev_pm_ops ipp_pm_ops = {
1985 SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
1986 SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
1989 struct platform_driver ipp_driver = {
1991 .remove = ipp_remove,
1993 .name = "exynos-drm-ipp",
1994 .owner = THIS_MODULE,