exynos_drm_ipp.c 44.2 KB
Newer Older
E
Eunchul Kim 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2012 Samsung Electronics Co.Ltd
 * Authors:
 *	Eunchul Kim <chulspro.kim@samsung.com>
 *	Jinyoung Jeon <jy0.jeon@samsung.com>
 *	Sangmin Lee <lsmin.lee@samsung.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/clk.h>
#include <linux/pm_runtime.h>

#include <drm/drmP.h>
#include <drm/exynos_drm.h>
#include "exynos_drm_drv.h"
#include "exynos_drm_gem.h"
#include "exynos_drm_ipp.h"
25
#include "exynos_drm_iommu.h"
E
Eunchul Kim 已提交
26 27

/*
28
 * IPP stands for Image Post Processing and
E
Eunchul Kim 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * supports image scaler/rotator and input/output DMA operations.
 * using FIMC, GSC, Rotator, so on.
 * IPP is integration device driver of same attribute h/w
 */

/*
 * TODO
 * 1. expand command control id.
 * 2. integrate	property and config.
 * 3. removed send_event id check routine.
 * 4. compare send_event id if needed.
 * 5. free subdrv_remove notifier callback list if needed.
 * 6. need to check subdrv_open about multi-open.
 * 7. need to power_on implement power and sysmmu ctrl.
 */

#define get_ipp_context(dev)	platform_get_drvdata(to_platform_device(dev))
#define ipp_is_m2m_cmd(c)	(c == IPP_CMD_M2M)

48 49 50
/* platform device pointer for ipp device. */
static struct platform_device *exynos_drm_ipp_pdev;

E
Eunchul Kim 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
 * A structure of event.
 *
 * @base: base of event.
 * @event: ipp event.
 */
struct drm_exynos_ipp_send_event {
	struct drm_pending_event	base;
	struct drm_exynos_ipp_event	event;
};

/*
 * A structure of memory node.
 *
 * @list: list head to memory queue information.
 * @ops_id: id of operations.
 * @prop_id: id of property.
 * @buf_id: id of buffer.
 * @buf_info: gem objects and dma address, size.
 * @filp: a pointer to drm_file.
 */
struct drm_exynos_ipp_mem_node {
	struct list_head	list;
	enum drm_exynos_ops_id	ops_id;
	u32	prop_id;
	u32	buf_id;
	struct drm_exynos_ipp_buf_info	buf_info;
	struct drm_file		*filp;
};

/*
 * A structure of ipp context.
 *
 * @subdrv: prepare initialization using subdrv.
 * @ipp_lock: lock for synchronization of access to ipp_idr.
 * @prop_lock: lock for synchronization of access to prop_idr.
 * @ipp_idr: ipp driver idr.
 * @prop_idr: property idr.
 * @event_workq: event work queue.
 * @cmd_workq: command work queue.
 */
struct ipp_context {
	struct exynos_drm_subdrv	subdrv;
	struct mutex	ipp_lock;
	struct mutex	prop_lock;
	struct idr	ipp_idr;
	struct idr	prop_idr;
	struct workqueue_struct	*event_workq;
	struct workqueue_struct	*cmd_workq;
};

static LIST_HEAD(exynos_drm_ippdrv_list);
static DEFINE_MUTEX(exynos_drm_ippdrv_lock);
static BLOCKING_NOTIFIER_HEAD(exynos_drm_ippnb_list);

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
int exynos_platform_device_ipp_register(void)
{
	struct platform_device *pdev;

	if (exynos_drm_ipp_pdev)
		return -EEXIST;

	pdev = platform_device_register_simple("exynos-drm-ipp", -1, NULL, 0);
	if (IS_ERR(pdev))
		return PTR_ERR(pdev);

	exynos_drm_ipp_pdev = pdev;

	return 0;
}

void exynos_platform_device_ipp_unregister(void)
{
	if (exynos_drm_ipp_pdev) {
		platform_device_unregister(exynos_drm_ipp_pdev);
		exynos_drm_ipp_pdev = NULL;
	}
}

E
Eunchul Kim 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
int exynos_drm_ippdrv_register(struct exynos_drm_ippdrv *ippdrv)
{
	mutex_lock(&exynos_drm_ippdrv_lock);
	list_add_tail(&ippdrv->drv_list, &exynos_drm_ippdrv_list);
	mutex_unlock(&exynos_drm_ippdrv_lock);

	return 0;
}

int exynos_drm_ippdrv_unregister(struct exynos_drm_ippdrv *ippdrv)
{
	mutex_lock(&exynos_drm_ippdrv_lock);
	list_del(&ippdrv->drv_list);
	mutex_unlock(&exynos_drm_ippdrv_lock);

	return 0;
}

148
static int ipp_create_id(struct idr *id_idr, struct mutex *lock, void *obj)
E
Eunchul Kim 已提交
149 150 151 152
{
	int ret;

	mutex_lock(lock);
T
Tejun Heo 已提交
153
	ret = idr_alloc(id_idr, obj, 1, 0, GFP_KERNEL);
E
Eunchul Kim 已提交
154 155
	mutex_unlock(lock);

156
	return ret;
E
Eunchul Kim 已提交
157 158
}

159 160 161 162 163 164 165
static void ipp_remove_id(struct idr *id_idr, struct mutex *lock, u32 id)
{
	mutex_lock(lock);
	idr_remove(id_idr, id);
	mutex_unlock(lock);
}

E
Eunchul Kim 已提交
166 167 168 169 170 171 172 173 174 175 176
static void *ipp_find_obj(struct idr *id_idr, struct mutex *lock, u32 id)
{
	void *obj;

	mutex_lock(lock);
	obj = idr_find(id_idr, id);
	mutex_unlock(lock);

	return obj;
}

177 178
static int ipp_check_driver(struct exynos_drm_ippdrv *ippdrv,
			    struct drm_exynos_ipp_property *property)
E
Eunchul Kim 已提交
179
{
180 181 182
	if (ippdrv->dedicated || (!ipp_is_m2m_cmd(property->cmd) &&
				  !pm_runtime_suspended(ippdrv->dev)))
		return -EBUSY;
E
Eunchul Kim 已提交
183

184 185 186 187 188
	if (ippdrv->check_property &&
	    ippdrv->check_property(ippdrv->dev, property))
		return -EINVAL;

	return 0;
E
Eunchul Kim 已提交
189 190 191 192 193 194 195
}

static struct exynos_drm_ippdrv *ipp_find_driver(struct ipp_context *ctx,
		struct drm_exynos_ipp_property *property)
{
	struct exynos_drm_ippdrv *ippdrv;
	u32 ipp_id = property->ipp_id;
196
	int ret;
E
Eunchul Kim 已提交
197 198

	if (ipp_id) {
199
		ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock, ipp_id);
200
		if (!ippdrv) {
201
			DRM_DEBUG("ipp%d driver not found\n", ipp_id);
202
			return ERR_PTR(-ENODEV);
E
Eunchul Kim 已提交
203 204
		}

205 206 207 208
		ret = ipp_check_driver(ippdrv, property);
		if (ret < 0) {
			DRM_DEBUG("ipp%d driver check error %d\n", ipp_id, ret);
			return ERR_PTR(ret);
E
Eunchul Kim 已提交
209 210 211 212 213
		}

		return ippdrv;
	} else {
		list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
214 215 216
			ret = ipp_check_driver(ippdrv, property);
			if (ret == 0)
				return ippdrv;
E
Eunchul Kim 已提交
217 218
		}

219
		DRM_DEBUG("cannot find driver suitable for given property.\n");
E
Eunchul Kim 已提交
220 221 222 223 224 225 226 227 228 229 230
	}

	return ERR_PTR(-ENODEV);
}

static struct exynos_drm_ippdrv *ipp_find_drv_by_handle(u32 prop_id)
{
	struct exynos_drm_ippdrv *ippdrv;
	struct drm_exynos_ipp_cmd_node *c_node;
	int count = 0;

231
	DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
E
Eunchul Kim 已提交
232 233 234 235

	/*
	 * This case is search ipp driver by prop_id handle.
	 * sometimes, ipp subsystem find driver by prop_id.
236
	 * e.g PAUSE state, queue buf, command control.
E
Eunchul Kim 已提交
237 238
	 */
	list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
239
		DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n", count++, (int)ippdrv);
E
Eunchul Kim 已提交
240

241 242 243 244
		mutex_lock(&ippdrv->cmd_lock);
		list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
			if (c_node->property.prop_id == prop_id) {
				mutex_unlock(&ippdrv->cmd_lock);
245
				return ippdrv;
246
			}
E
Eunchul Kim 已提交
247
		}
248
		mutex_unlock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
249 250 251 252 253 254 255 256 257
	}

	return ERR_PTR(-ENODEV);
}

int exynos_drm_ipp_get_property(struct drm_device *drm_dev, void *data,
		struct drm_file *file)
{
	struct drm_exynos_file_private *file_priv = file->driver_priv;
258
	struct device *dev = file_priv->ipp_dev;
E
Eunchul Kim 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	struct ipp_context *ctx = get_ipp_context(dev);
	struct drm_exynos_ipp_prop_list *prop_list = data;
	struct exynos_drm_ippdrv *ippdrv;
	int count = 0;

	if (!ctx) {
		DRM_ERROR("invalid context.\n");
		return -EINVAL;
	}

	if (!prop_list) {
		DRM_ERROR("invalid property parameter.\n");
		return -EINVAL;
	}

274
	DRM_DEBUG_KMS("ipp_id[%d]\n", prop_list->ipp_id);
E
Eunchul Kim 已提交
275 276 277 278

	if (!prop_list->ipp_id) {
		list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list)
			count++;
279

E
Eunchul Kim 已提交
280 281 282 283 284 285 286 287 288
		/*
		 * Supports ippdrv list count for user application.
		 * First step user application getting ippdrv count.
		 * and second step getting ippdrv capability using ipp_id.
		 */
		prop_list->count = count;
	} else {
		/*
		 * Getting ippdrv capability by ipp_id.
289
		 * some device not supported wb, output interface.
E
Eunchul Kim 已提交
290 291 292 293 294
		 * so, user application detect correct ipp driver
		 * using this ioctl.
		 */
		ippdrv = ipp_find_obj(&ctx->ipp_idr, &ctx->ipp_lock,
						prop_list->ipp_id);
295
		if (!ippdrv) {
E
Eunchul Kim 已提交
296 297
			DRM_ERROR("not found ipp%d driver.\n",
					prop_list->ipp_id);
298
			return -ENODEV;
E
Eunchul Kim 已提交
299 300
		}

301
		*prop_list = ippdrv->prop_list;
E
Eunchul Kim 已提交
302 303 304 305 306 307 308 309 310 311 312 313
	}

	return 0;
}

static void ipp_print_property(struct drm_exynos_ipp_property *property,
		int idx)
{
	struct drm_exynos_ipp_config *config = &property->config[idx];
	struct drm_exynos_pos *pos = &config->pos;
	struct drm_exynos_sz *sz = &config->sz;

314 315
	DRM_DEBUG_KMS("prop_id[%d]ops[%s]fmt[0x%x]\n",
		property->prop_id, idx ? "dst" : "src", config->fmt);
E
Eunchul Kim 已提交
316

317 318
	DRM_DEBUG_KMS("pos[%d %d %d %d]sz[%d %d]f[%d]r[%d]\n",
		pos->x, pos->y, pos->w, pos->h,
E
Eunchul Kim 已提交
319 320 321 322 323 324 325 326 327
		sz->hsize, sz->vsize, config->flip, config->degree);
}

static int ipp_find_and_set_property(struct drm_exynos_ipp_property *property)
{
	struct exynos_drm_ippdrv *ippdrv;
	struct drm_exynos_ipp_cmd_node *c_node;
	u32 prop_id = property->prop_id;

328
	DRM_DEBUG_KMS("prop_id[%d]\n", prop_id);
E
Eunchul Kim 已提交
329 330

	ippdrv = ipp_find_drv_by_handle(prop_id);
331
	if (IS_ERR(ippdrv)) {
E
Eunchul Kim 已提交
332 333 334 335 336 337 338 339 340
		DRM_ERROR("failed to get ipp driver.\n");
		return -EINVAL;
	}

	/*
	 * Find command node using command list in ippdrv.
	 * when we find this command no using prop_id.
	 * return property information set in this command node.
	 */
341
	mutex_lock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
342 343 344
	list_for_each_entry(c_node, &ippdrv->cmd_list, list) {
		if ((c_node->property.prop_id == prop_id) &&
		    (c_node->state == IPP_STATE_STOP)) {
345
			mutex_unlock(&ippdrv->cmd_lock);
346 347
			DRM_DEBUG_KMS("found cmd[%d]ippdrv[0x%x]\n",
				property->cmd, (int)ippdrv);
E
Eunchul Kim 已提交
348 349 350 351 352

			c_node->property = *property;
			return 0;
		}
	}
353
	mutex_unlock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
354 355 356 357 358 359 360 361 362 363 364

	DRM_ERROR("failed to search property.\n");

	return -EINVAL;
}

static struct drm_exynos_ipp_cmd_work *ipp_create_cmd_work(void)
{
	struct drm_exynos_ipp_cmd_work *cmd_work;

	cmd_work = kzalloc(sizeof(*cmd_work), GFP_KERNEL);
365
	if (!cmd_work)
E
Eunchul Kim 已提交
366 367 368 369 370 371 372 373 374 375 376 377
		return ERR_PTR(-ENOMEM);

	INIT_WORK((struct work_struct *)cmd_work, ipp_sched_cmd);

	return cmd_work;
}

static struct drm_exynos_ipp_event_work *ipp_create_event_work(void)
{
	struct drm_exynos_ipp_event_work *event_work;

	event_work = kzalloc(sizeof(*event_work), GFP_KERNEL);
378
	if (!event_work)
E
Eunchul Kim 已提交
379 380
		return ERR_PTR(-ENOMEM);

381
	INIT_WORK(&event_work->work, ipp_sched_event);
E
Eunchul Kim 已提交
382 383 384 385 386 387 388 389

	return event_work;
}

int exynos_drm_ipp_set_property(struct drm_device *drm_dev, void *data,
		struct drm_file *file)
{
	struct drm_exynos_file_private *file_priv = file->driver_priv;
390
	struct device *dev = file_priv->ipp_dev;
E
Eunchul Kim 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	struct ipp_context *ctx = get_ipp_context(dev);
	struct drm_exynos_ipp_property *property = data;
	struct exynos_drm_ippdrv *ippdrv;
	struct drm_exynos_ipp_cmd_node *c_node;
	int ret, i;

	if (!ctx) {
		DRM_ERROR("invalid context.\n");
		return -EINVAL;
	}

	if (!property) {
		DRM_ERROR("invalid property parameter.\n");
		return -EINVAL;
	}

	/*
	 * This is log print for user application property.
	 * user application set various property.
	 */
	for_each_ipp_ops(i)
		ipp_print_property(property, i);

	/*
	 * set property ioctl generated new prop_id.
	 * but in this case already asigned prop_id using old set property.
	 * e.g PAUSE state. this case supports find current prop_id and use it
	 * instead of allocation.
	 */
	if (property->prop_id) {
421
		DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
E
Eunchul Kim 已提交
422 423 424 425 426
		return ipp_find_and_set_property(property);
	}

	/* find ipp driver using ipp id */
	ippdrv = ipp_find_driver(ctx, property);
427
	if (IS_ERR(ippdrv)) {
E
Eunchul Kim 已提交
428 429 430 431 432 433
		DRM_ERROR("failed to get ipp driver.\n");
		return -EINVAL;
	}

	/* allocate command node */
	c_node = kzalloc(sizeof(*c_node), GFP_KERNEL);
434
	if (!c_node)
E
Eunchul Kim 已提交
435 436
		return -ENOMEM;

437 438
	ret = ipp_create_id(&ctx->prop_idr, &ctx->prop_lock, c_node);
	if (ret < 0) {
E
Eunchul Kim 已提交
439 440 441
		DRM_ERROR("failed to create id.\n");
		goto err_clear;
	}
442
	property->prop_id = ret;
E
Eunchul Kim 已提交
443

444 445
	DRM_DEBUG_KMS("created prop_id[%d]cmd[%d]ippdrv[0x%x]\n",
		property->prop_id, property->cmd, (int)ippdrv);
E
Eunchul Kim 已提交
446 447

	/* stored property information and ippdrv in private data */
448
	c_node->dev = dev;
E
Eunchul Kim 已提交
449 450 451 452
	c_node->property = *property;
	c_node->state = IPP_STATE_IDLE;

	c_node->start_work = ipp_create_cmd_work();
453
	if (IS_ERR(c_node->start_work)) {
E
Eunchul Kim 已提交
454
		DRM_ERROR("failed to create start work.\n");
455
		goto err_remove_id;
E
Eunchul Kim 已提交
456 457 458
	}

	c_node->stop_work = ipp_create_cmd_work();
459
	if (IS_ERR(c_node->stop_work)) {
E
Eunchul Kim 已提交
460 461 462 463 464
		DRM_ERROR("failed to create stop work.\n");
		goto err_free_start;
	}

	c_node->event_work = ipp_create_event_work();
465
	if (IS_ERR(c_node->event_work)) {
E
Eunchul Kim 已提交
466 467 468 469
		DRM_ERROR("failed to create event work.\n");
		goto err_free_stop;
	}

470
	mutex_init(&c_node->lock);
E
Eunchul Kim 已提交
471 472 473 474 475 476 477 478 479 480
	mutex_init(&c_node->mem_lock);
	mutex_init(&c_node->event_lock);

	init_completion(&c_node->start_complete);
	init_completion(&c_node->stop_complete);

	for_each_ipp_ops(i)
		INIT_LIST_HEAD(&c_node->mem_list[i]);

	INIT_LIST_HEAD(&c_node->event_list);
481
	mutex_lock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
482
	list_add_tail(&c_node->list, &ippdrv->cmd_list);
483
	mutex_unlock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
484 485 486 487 488 489 490 491 492 493 494

	/* make dedicated state without m2m */
	if (!ipp_is_m2m_cmd(property->cmd))
		ippdrv->dedicated = true;

	return 0;

err_free_stop:
	kfree(c_node->stop_work);
err_free_start:
	kfree(c_node->start_work);
495 496
err_remove_id:
	ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock, property->prop_id);
E
Eunchul Kim 已提交
497 498 499 500 501
err_clear:
	kfree(c_node);
	return ret;
}

502 503
static void ipp_clean_cmd_node(struct ipp_context *ctx,
				struct drm_exynos_ipp_cmd_node *c_node)
E
Eunchul Kim 已提交
504 505 506 507
{
	/* delete list */
	list_del(&c_node->list);

508 509 510
	ipp_remove_id(&ctx->prop_idr, &ctx->prop_lock,
			c_node->property.prop_id);

E
Eunchul Kim 已提交
511
	/* destroy mutex */
512
	mutex_destroy(&c_node->lock);
E
Eunchul Kim 已提交
513 514 515 516 517 518 519 520 521 522
	mutex_destroy(&c_node->mem_lock);
	mutex_destroy(&c_node->event_lock);

	/* free command node */
	kfree(c_node->start_work);
	kfree(c_node->stop_work);
	kfree(c_node->event_work);
	kfree(c_node);
}

523
static bool ipp_check_mem_list(struct drm_exynos_ipp_cmd_node *c_node)
E
Eunchul Kim 已提交
524
{
525 526 527 528 529 530 531 532 533
	switch (c_node->property.cmd) {
	case IPP_CMD_WB:
		return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
	case IPP_CMD_OUTPUT:
		return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]);
	case IPP_CMD_M2M:
	default:
		return !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_SRC]) &&
		       !list_empty(&c_node->mem_list[EXYNOS_DRM_OPS_DST]);
E
Eunchul Kim 已提交
534 535 536 537 538 539 540 541 542 543 544
	}
}

static struct drm_exynos_ipp_mem_node
		*ipp_find_mem_node(struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_queue_buf *qbuf)
{
	struct drm_exynos_ipp_mem_node *m_node;
	struct list_head *head;
	int count = 0;

545
	DRM_DEBUG_KMS("buf_id[%d]\n", qbuf->buf_id);
E
Eunchul Kim 已提交
546 547 548 549 550 551

	/* source/destination memory list */
	head = &c_node->mem_list[qbuf->ops_id];

	/* find memory node from memory list */
	list_for_each_entry(m_node, head, list) {
552
		DRM_DEBUG_KMS("count[%d]m_node[0x%x]\n", count++, (int)m_node);
E
Eunchul Kim 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

		/* compare buffer id */
		if (m_node->buf_id == qbuf->buf_id)
			return m_node;
	}

	return NULL;
}

static int ipp_set_mem_node(struct exynos_drm_ippdrv *ippdrv,
		struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_mem_node *m_node)
{
	struct exynos_drm_ipp_ops *ops = NULL;
	int ret = 0;

569
	DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
E
Eunchul Kim 已提交
570 571 572 573 574 575

	if (!m_node) {
		DRM_ERROR("invalid queue node.\n");
		return -EFAULT;
	}

576
	DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
E
Eunchul Kim 已提交
577 578 579 580 581

	/* get operations callback */
	ops = ippdrv->ops[m_node->ops_id];
	if (!ops) {
		DRM_ERROR("not support ops.\n");
582
		return -EFAULT;
E
Eunchul Kim 已提交
583 584 585 586 587 588 589 590
	}

	/* set address and enable irq */
	if (ops->set_addr) {
		ret = ops->set_addr(ippdrv->dev, &m_node->buf_info,
			m_node->buf_id, IPP_BUF_ENQUEUE);
		if (ret) {
			DRM_ERROR("failed to set addr.\n");
591
			return ret;
E
Eunchul Kim 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604
		}
	}

	return ret;
}

static struct drm_exynos_ipp_mem_node
		*ipp_get_mem_node(struct drm_device *drm_dev,
		struct drm_file *file,
		struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_queue_buf *qbuf)
{
	struct drm_exynos_ipp_mem_node *m_node;
605
	struct drm_exynos_ipp_buf_info *buf_info;
E
Eunchul Kim 已提交
606 607 608
	int i;

	m_node = kzalloc(sizeof(*m_node), GFP_KERNEL);
609
	if (!m_node)
610
		return ERR_PTR(-ENOMEM);
E
Eunchul Kim 已提交
611

612
	buf_info = &m_node->buf_info;
E
Eunchul Kim 已提交
613 614 615 616 617 618

	/* operations, buffer id */
	m_node->ops_id = qbuf->ops_id;
	m_node->prop_id = qbuf->prop_id;
	m_node->buf_id = qbuf->buf_id;

619 620
	DRM_DEBUG_KMS("m_node[0x%x]ops_id[%d]\n", (int)m_node, qbuf->ops_id);
	DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]\n", qbuf->prop_id, m_node->buf_id);
E
Eunchul Kim 已提交
621 622

	for_each_ipp_planar(i) {
623
		DRM_DEBUG_KMS("i[%d]handle[0x%x]\n", i, qbuf->handle[i]);
E
Eunchul Kim 已提交
624 625 626

		/* get dma address by handle */
		if (qbuf->handle[i]) {
627 628
			dma_addr_t *addr;

E
Eunchul Kim 已提交
629 630 631 632 633 634 635
			addr = exynos_drm_gem_get_dma_addr(drm_dev,
					qbuf->handle[i], file);
			if (IS_ERR(addr)) {
				DRM_ERROR("failed to get addr.\n");
				goto err_clear;
			}

636 637 638 639
			buf_info->handles[i] = qbuf->handle[i];
			buf_info->base[i] = *addr;
			DRM_DEBUG_KMS("i[%d]base[0x%x]hd[0x%lx]\n", i,
				      buf_info->base[i], buf_info->handles[i]);
E
Eunchul Kim 已提交
640 641 642 643
		}
	}

	m_node->filp = file;
644
	mutex_lock(&c_node->mem_lock);
E
Eunchul Kim 已提交
645 646
	list_add_tail(&m_node->list, &c_node->mem_list[qbuf->ops_id]);
	mutex_unlock(&c_node->mem_lock);
647

E
Eunchul Kim 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660
	return m_node;

err_clear:
	kfree(m_node);
	return ERR_PTR(-EFAULT);
}

static int ipp_put_mem_node(struct drm_device *drm_dev,
		struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_mem_node *m_node)
{
	int i;

661
	DRM_DEBUG_KMS("node[0x%x]\n", (int)m_node);
E
Eunchul Kim 已提交
662 663 664 665 666 667

	if (!m_node) {
		DRM_ERROR("invalid dequeue node.\n");
		return -EFAULT;
	}

668
	DRM_DEBUG_KMS("ops_id[%d]\n", m_node->ops_id);
E
Eunchul Kim 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

	/* put gem buffer */
	for_each_ipp_planar(i) {
		unsigned long handle = m_node->buf_info.handles[i];
		if (handle)
			exynos_drm_gem_put_dma_addr(drm_dev, handle,
							m_node->filp);
	}

	/* delete list in queue */
	list_del(&m_node->list);
	kfree(m_node);

	return 0;
}

static void ipp_free_event(struct drm_pending_event *event)
{
	kfree(event);
}

static int ipp_get_event(struct drm_device *drm_dev,
		struct drm_file *file,
		struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_queue_buf *qbuf)
{
	struct drm_exynos_ipp_send_event *e;
	unsigned long flags;

698
	DRM_DEBUG_KMS("ops_id[%d]buf_id[%d]\n", qbuf->ops_id, qbuf->buf_id);
E
Eunchul Kim 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

	e = kzalloc(sizeof(*e), GFP_KERNEL);
	if (!e) {
		spin_lock_irqsave(&drm_dev->event_lock, flags);
		file->event_space += sizeof(e->event);
		spin_unlock_irqrestore(&drm_dev->event_lock, flags);
		return -ENOMEM;
	}

	/* make event */
	e->event.base.type = DRM_EXYNOS_IPP_EVENT;
	e->event.base.length = sizeof(e->event);
	e->event.user_data = qbuf->user_data;
	e->event.prop_id = qbuf->prop_id;
	e->event.buf_id[EXYNOS_DRM_OPS_DST] = qbuf->buf_id;
	e->base.event = &e->event.base;
	e->base.file_priv = file;
	e->base.destroy = ipp_free_event;
717
	mutex_lock(&c_node->event_lock);
E
Eunchul Kim 已提交
718
	list_add_tail(&e->base.link, &c_node->event_list);
719
	mutex_unlock(&c_node->event_lock);
E
Eunchul Kim 已提交
720 721 722 723 724 725 726 727 728 729

	return 0;
}

static void ipp_put_event(struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_queue_buf *qbuf)
{
	struct drm_exynos_ipp_send_event *e, *te;
	int count = 0;

730
	mutex_lock(&c_node->event_lock);
E
Eunchul Kim 已提交
731
	list_for_each_entry_safe(e, te, &c_node->event_list, base.link) {
732
		DRM_DEBUG_KMS("count[%d]e[0x%x]\n", count++, (int)e);
E
Eunchul Kim 已提交
733 734

		/*
S
Sachin Kamat 已提交
735
		 * qbuf == NULL condition means all event deletion.
E
Eunchul Kim 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
		 * stop operations want to delete all event list.
		 * another case delete only same buf id.
		 */
		if (!qbuf) {
			/* delete list */
			list_del(&e->base.link);
			kfree(e);
		}

		/* compare buffer id */
		if (qbuf && (qbuf->buf_id ==
		    e->event.buf_id[EXYNOS_DRM_OPS_DST])) {
			/* delete list */
			list_del(&e->base.link);
			kfree(e);
751
			goto out_unlock;
E
Eunchul Kim 已提交
752 753
		}
	}
754 755 756 757

out_unlock:
	mutex_unlock(&c_node->event_lock);
	return;
E
Eunchul Kim 已提交
758 759
}

760
static void ipp_handle_cmd_work(struct device *dev,
E
Eunchul Kim 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
		struct exynos_drm_ippdrv *ippdrv,
		struct drm_exynos_ipp_cmd_work *cmd_work,
		struct drm_exynos_ipp_cmd_node *c_node)
{
	struct ipp_context *ctx = get_ipp_context(dev);

	cmd_work->ippdrv = ippdrv;
	cmd_work->c_node = c_node;
	queue_work(ctx->cmd_workq, (struct work_struct *)cmd_work);
}

static int ipp_queue_buf_with_run(struct device *dev,
		struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_mem_node *m_node,
		struct drm_exynos_ipp_queue_buf *qbuf)
{
	struct exynos_drm_ippdrv *ippdrv;
	struct drm_exynos_ipp_property *property;
	struct exynos_drm_ipp_ops *ops;
	int ret;

	ippdrv = ipp_find_drv_by_handle(qbuf->prop_id);
783
	if (IS_ERR(ippdrv)) {
E
Eunchul Kim 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796
		DRM_ERROR("failed to get ipp driver.\n");
		return -EFAULT;
	}

	ops = ippdrv->ops[qbuf->ops_id];
	if (!ops) {
		DRM_ERROR("failed to get ops.\n");
		return -EFAULT;
	}

	property = &c_node->property;

	if (c_node->state != IPP_STATE_START) {
797
		DRM_DEBUG_KMS("bypass for invalid state.\n");
E
Eunchul Kim 已提交
798 799 800
		return 0;
	}

801
	mutex_lock(&c_node->mem_lock);
E
Eunchul Kim 已提交
802
	if (!ipp_check_mem_list(c_node)) {
803
		mutex_unlock(&c_node->mem_lock);
804
		DRM_DEBUG_KMS("empty memory.\n");
E
Eunchul Kim 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		return 0;
	}

	/*
	 * If set destination buffer and enabled clock,
	 * then m2m operations need start operations at queue_buf
	 */
	if (ipp_is_m2m_cmd(property->cmd)) {
		struct drm_exynos_ipp_cmd_work *cmd_work = c_node->start_work;

		cmd_work->ctrl = IPP_CTRL_PLAY;
		ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
	} else {
		ret = ipp_set_mem_node(ippdrv, c_node, m_node);
		if (ret) {
820
			mutex_unlock(&c_node->mem_lock);
E
Eunchul Kim 已提交
821 822 823 824
			DRM_ERROR("failed to set m node.\n");
			return ret;
		}
	}
825
	mutex_unlock(&c_node->mem_lock);
E
Eunchul Kim 已提交
826 827 828 829 830 831 832 833 834 835

	return 0;
}

static void ipp_clean_queue_buf(struct drm_device *drm_dev,
		struct drm_exynos_ipp_cmd_node *c_node,
		struct drm_exynos_ipp_queue_buf *qbuf)
{
	struct drm_exynos_ipp_mem_node *m_node, *tm_node;

836
	/* delete list */
837
	mutex_lock(&c_node->mem_lock);
838 839 840 841 842
	list_for_each_entry_safe(m_node, tm_node,
		&c_node->mem_list[qbuf->ops_id], list) {
		if (m_node->buf_id == qbuf->buf_id &&
		    m_node->ops_id == qbuf->ops_id)
			ipp_put_mem_node(drm_dev, c_node, m_node);
E
Eunchul Kim 已提交
843
	}
844
	mutex_unlock(&c_node->mem_lock);
E
Eunchul Kim 已提交
845 846 847 848 849 850
}

int exynos_drm_ipp_queue_buf(struct drm_device *drm_dev, void *data,
		struct drm_file *file)
{
	struct drm_exynos_file_private *file_priv = file->driver_priv;
851
	struct device *dev = file_priv->ipp_dev;
E
Eunchul Kim 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	struct ipp_context *ctx = get_ipp_context(dev);
	struct drm_exynos_ipp_queue_buf *qbuf = data;
	struct drm_exynos_ipp_cmd_node *c_node;
	struct drm_exynos_ipp_mem_node *m_node;
	int ret;

	if (!qbuf) {
		DRM_ERROR("invalid buf parameter.\n");
		return -EINVAL;
	}

	if (qbuf->ops_id >= EXYNOS_DRM_OPS_MAX) {
		DRM_ERROR("invalid ops parameter.\n");
		return -EINVAL;
	}

868 869
	DRM_DEBUG_KMS("prop_id[%d]ops_id[%s]buf_id[%d]buf_type[%d]\n",
		qbuf->prop_id, qbuf->ops_id ? "dst" : "src",
E
Eunchul Kim 已提交
870 871 872 873 874
		qbuf->buf_id, qbuf->buf_type);

	/* find command node */
	c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
		qbuf->prop_id);
875
	if (!c_node) {
E
Eunchul Kim 已提交
876
		DRM_ERROR("failed to get command node.\n");
877
		return -ENODEV;
E
Eunchul Kim 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	}

	/* buffer control */
	switch (qbuf->buf_type) {
	case IPP_BUF_ENQUEUE:
		/* get memory node */
		m_node = ipp_get_mem_node(drm_dev, file, c_node, qbuf);
		if (IS_ERR(m_node)) {
			DRM_ERROR("failed to get m_node.\n");
			return PTR_ERR(m_node);
		}

		/*
		 * first step get event for destination buffer.
		 * and second step when M2M case run with destination buffer
		 * if needed.
		 */
		if (qbuf->ops_id == EXYNOS_DRM_OPS_DST) {
			/* get event for destination buffer */
			ret = ipp_get_event(drm_dev, file, c_node, qbuf);
			if (ret) {
				DRM_ERROR("failed to get event.\n");
				goto err_clean_node;
			}

			/*
			 * M2M case run play control for streaming feature.
			 * other case set address and waiting.
			 */
			ret = ipp_queue_buf_with_run(dev, c_node, m_node, qbuf);
			if (ret) {
				DRM_ERROR("failed to run command.\n");
				goto err_clean_node;
			}
		}
		break;
	case IPP_BUF_DEQUEUE:
915
		mutex_lock(&c_node->lock);
E
Eunchul Kim 已提交
916 917 918 919 920 921 922

		/* put event for destination buffer */
		if (qbuf->ops_id == EXYNOS_DRM_OPS_DST)
			ipp_put_event(c_node, qbuf);

		ipp_clean_queue_buf(drm_dev, c_node, qbuf);

923
		mutex_unlock(&c_node->lock);
E
Eunchul Kim 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
		break;
	default:
		DRM_ERROR("invalid buffer control.\n");
		return -EINVAL;
	}

	return 0;

err_clean_node:
	DRM_ERROR("clean memory nodes.\n");

	ipp_clean_queue_buf(drm_dev, c_node, qbuf);
	return ret;
}

static bool exynos_drm_ipp_check_valid(struct device *dev,
		enum drm_exynos_ipp_ctrl ctrl, enum drm_exynos_ipp_state state)
{
	if (ctrl != IPP_CTRL_PLAY) {
		if (pm_runtime_suspended(dev)) {
			DRM_ERROR("pm:runtime_suspended.\n");
			goto err_status;
		}
	}

	switch (ctrl) {
	case IPP_CTRL_PLAY:
		if (state != IPP_STATE_IDLE)
			goto err_status;
		break;
	case IPP_CTRL_STOP:
		if (state == IPP_STATE_STOP)
			goto err_status;
		break;
	case IPP_CTRL_PAUSE:
		if (state != IPP_STATE_START)
			goto err_status;
		break;
	case IPP_CTRL_RESUME:
		if (state != IPP_STATE_STOP)
			goto err_status;
		break;
	default:
		DRM_ERROR("invalid state.\n");
		goto err_status;
	}

	return true;

err_status:
	DRM_ERROR("invalid status:ctrl[%d]state[%d]\n", ctrl, state);
	return false;
}

int exynos_drm_ipp_cmd_ctrl(struct drm_device *drm_dev, void *data,
		struct drm_file *file)
{
	struct drm_exynos_file_private *file_priv = file->driver_priv;
	struct exynos_drm_ippdrv *ippdrv = NULL;
983
	struct device *dev = file_priv->ipp_dev;
E
Eunchul Kim 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	struct ipp_context *ctx = get_ipp_context(dev);
	struct drm_exynos_ipp_cmd_ctrl *cmd_ctrl = data;
	struct drm_exynos_ipp_cmd_work *cmd_work;
	struct drm_exynos_ipp_cmd_node *c_node;

	if (!ctx) {
		DRM_ERROR("invalid context.\n");
		return -EINVAL;
	}

	if (!cmd_ctrl) {
		DRM_ERROR("invalid control parameter.\n");
		return -EINVAL;
	}

999
	DRM_DEBUG_KMS("ctrl[%d]prop_id[%d]\n",
E
Eunchul Kim 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		cmd_ctrl->ctrl, cmd_ctrl->prop_id);

	ippdrv = ipp_find_drv_by_handle(cmd_ctrl->prop_id);
	if (IS_ERR(ippdrv)) {
		DRM_ERROR("failed to get ipp driver.\n");
		return PTR_ERR(ippdrv);
	}

	c_node = ipp_find_obj(&ctx->prop_idr, &ctx->prop_lock,
		cmd_ctrl->prop_id);
1010
	if (!c_node) {
E
Eunchul Kim 已提交
1011
		DRM_ERROR("invalid command node list.\n");
1012
		return -ENODEV;
E
Eunchul Kim 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	}

	if (!exynos_drm_ipp_check_valid(ippdrv->dev, cmd_ctrl->ctrl,
	    c_node->state)) {
		DRM_ERROR("invalid state.\n");
		return -EINVAL;
	}

	switch (cmd_ctrl->ctrl) {
	case IPP_CTRL_PLAY:
		if (pm_runtime_suspended(ippdrv->dev))
			pm_runtime_get_sync(ippdrv->dev);
1025

E
Eunchul Kim 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
		c_node->state = IPP_STATE_START;

		cmd_work = c_node->start_work;
		cmd_work->ctrl = cmd_ctrl->ctrl;
		ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
		break;
	case IPP_CTRL_STOP:
		cmd_work = c_node->stop_work;
		cmd_work->ctrl = cmd_ctrl->ctrl;
		ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);

		if (!wait_for_completion_timeout(&c_node->stop_complete,
		    msecs_to_jiffies(300))) {
			DRM_ERROR("timeout stop:prop_id[%d]\n",
				c_node->property.prop_id);
		}

		c_node->state = IPP_STATE_STOP;
		ippdrv->dedicated = false;
1045
		mutex_lock(&ippdrv->cmd_lock);
1046
		ipp_clean_cmd_node(ctx, c_node);
E
Eunchul Kim 已提交
1047 1048 1049

		if (list_empty(&ippdrv->cmd_list))
			pm_runtime_put_sync(ippdrv->dev);
1050
		mutex_unlock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		break;
	case IPP_CTRL_PAUSE:
		cmd_work = c_node->stop_work;
		cmd_work->ctrl = cmd_ctrl->ctrl;
		ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);

		if (!wait_for_completion_timeout(&c_node->stop_complete,
		    msecs_to_jiffies(200))) {
			DRM_ERROR("timeout stop:prop_id[%d]\n",
				c_node->property.prop_id);
		}

		c_node->state = IPP_STATE_STOP;
		break;
	case IPP_CTRL_RESUME:
		c_node->state = IPP_STATE_START;
		cmd_work = c_node->start_work;
		cmd_work->ctrl = cmd_ctrl->ctrl;
		ipp_handle_cmd_work(dev, ippdrv, cmd_work, c_node);
		break;
	default:
		DRM_ERROR("could not support this state currently.\n");
		return -EINVAL;
	}

1076
	DRM_DEBUG_KMS("done ctrl[%d]prop_id[%d]\n",
E
Eunchul Kim 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
		cmd_ctrl->ctrl, cmd_ctrl->prop_id);

	return 0;
}

int exynos_drm_ippnb_register(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(
		&exynos_drm_ippnb_list, nb);
}

int exynos_drm_ippnb_unregister(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(
		&exynos_drm_ippnb_list, nb);
}

int exynos_drm_ippnb_send_event(unsigned long val, void *v)
{
	return blocking_notifier_call_chain(
		&exynos_drm_ippnb_list, val, v);
}

static int ipp_set_property(struct exynos_drm_ippdrv *ippdrv,
		struct drm_exynos_ipp_property *property)
{
	struct exynos_drm_ipp_ops *ops = NULL;
	bool swap = false;
	int ret, i;

	if (!property) {
		DRM_ERROR("invalid property parameter.\n");
		return -EINVAL;
	}

1112
	DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
E
Eunchul Kim 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

	/* reset h/w block */
	if (ippdrv->reset &&
	    ippdrv->reset(ippdrv->dev)) {
		return -EINVAL;
	}

	/* set source,destination operations */
	for_each_ipp_ops(i) {
		struct drm_exynos_ipp_config *config =
			&property->config[i];

		ops = ippdrv->ops[i];
		if (!ops || !config) {
			DRM_ERROR("not support ops and config.\n");
			return -EINVAL;
		}

		/* set format */
		if (ops->set_fmt) {
			ret = ops->set_fmt(ippdrv->dev, config->fmt);
1134
			if (ret)
E
Eunchul Kim 已提交
1135 1136 1137 1138 1139 1140 1141
				return ret;
		}

		/* set transform for rotation, flip */
		if (ops->set_transf) {
			ret = ops->set_transf(ippdrv->dev, config->degree,
				config->flip, &swap);
1142 1143
			if (ret)
				return ret;
E
Eunchul Kim 已提交
1144 1145 1146 1147 1148 1149
		}

		/* set size */
		if (ops->set_size) {
			ret = ops->set_size(ippdrv->dev, swap, &config->pos,
				&config->sz);
1150
			if (ret)
E
Eunchul Kim 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
				return ret;
		}
	}

	return 0;
}

static int ipp_start_property(struct exynos_drm_ippdrv *ippdrv,
		struct drm_exynos_ipp_cmd_node *c_node)
{
	struct drm_exynos_ipp_mem_node *m_node;
	struct drm_exynos_ipp_property *property = &c_node->property;
	struct list_head *head;
	int ret, i;

1166
	DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
E
Eunchul Kim 已提交
1167 1168

	/* store command info in ippdrv */
1169
	ippdrv->c_node = c_node;
E
Eunchul Kim 已提交
1170

1171
	mutex_lock(&c_node->mem_lock);
E
Eunchul Kim 已提交
1172
	if (!ipp_check_mem_list(c_node)) {
1173
		DRM_DEBUG_KMS("empty memory.\n");
1174 1175
		ret = -ENOMEM;
		goto err_unlock;
E
Eunchul Kim 已提交
1176 1177 1178 1179 1180 1181
	}

	/* set current property in ippdrv */
	ret = ipp_set_property(ippdrv, property);
	if (ret) {
		DRM_ERROR("failed to set property.\n");
1182
		ippdrv->c_node = NULL;
1183
		goto err_unlock;
E
Eunchul Kim 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	}

	/* check command */
	switch (property->cmd) {
	case IPP_CMD_M2M:
		for_each_ipp_ops(i) {
			/* source/destination memory list */
			head = &c_node->mem_list[i];

			m_node = list_first_entry(head,
				struct drm_exynos_ipp_mem_node, list);

1196
			DRM_DEBUG_KMS("m_node[0x%x]\n", (int)m_node);
E
Eunchul Kim 已提交
1197 1198 1199 1200

			ret = ipp_set_mem_node(ippdrv, c_node, m_node);
			if (ret) {
				DRM_ERROR("failed to set m node.\n");
1201
				goto err_unlock;
E
Eunchul Kim 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
			}
		}
		break;
	case IPP_CMD_WB:
		/* destination memory list */
		head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];

		list_for_each_entry(m_node, head, list) {
			ret = ipp_set_mem_node(ippdrv, c_node, m_node);
			if (ret) {
				DRM_ERROR("failed to set m node.\n");
1213
				goto err_unlock;
E
Eunchul Kim 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
			}
		}
		break;
	case IPP_CMD_OUTPUT:
		/* source memory list */
		head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];

		list_for_each_entry(m_node, head, list) {
			ret = ipp_set_mem_node(ippdrv, c_node, m_node);
			if (ret) {
				DRM_ERROR("failed to set m node.\n");
1225
				goto err_unlock;
E
Eunchul Kim 已提交
1226 1227 1228 1229 1230
			}
		}
		break;
	default:
		DRM_ERROR("invalid operations.\n");
1231 1232
		ret = -EINVAL;
		goto err_unlock;
E
Eunchul Kim 已提交
1233
	}
1234
	mutex_unlock(&c_node->mem_lock);
E
Eunchul Kim 已提交
1235

1236
	DRM_DEBUG_KMS("cmd[%d]\n", property->cmd);
E
Eunchul Kim 已提交
1237 1238 1239 1240 1241 1242

	/* start operations */
	if (ippdrv->start) {
		ret = ippdrv->start(ippdrv->dev, property->cmd);
		if (ret) {
			DRM_ERROR("failed to start ops.\n");
1243
			ippdrv->c_node = NULL;
E
Eunchul Kim 已提交
1244 1245 1246 1247 1248
			return ret;
		}
	}

	return 0;
1249 1250 1251 1252 1253

err_unlock:
	mutex_unlock(&c_node->mem_lock);
	ippdrv->c_node = NULL;
	return ret;
E
Eunchul Kim 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
}

static int ipp_stop_property(struct drm_device *drm_dev,
		struct exynos_drm_ippdrv *ippdrv,
		struct drm_exynos_ipp_cmd_node *c_node)
{
	struct drm_exynos_ipp_mem_node *m_node, *tm_node;
	struct drm_exynos_ipp_property *property = &c_node->property;
	struct list_head *head;
	int ret = 0, i;

1265
	DRM_DEBUG_KMS("prop_id[%d]\n", property->prop_id);
E
Eunchul Kim 已提交
1266 1267 1268 1269

	/* put event */
	ipp_put_event(c_node, NULL);

1270 1271
	mutex_lock(&c_node->mem_lock);

E
Eunchul Kim 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	/* check command */
	switch (property->cmd) {
	case IPP_CMD_M2M:
		for_each_ipp_ops(i) {
			/* source/destination memory list */
			head = &c_node->mem_list[i];

			list_for_each_entry_safe(m_node, tm_node,
				head, list) {
				ret = ipp_put_mem_node(drm_dev, c_node,
					m_node);
				if (ret) {
					DRM_ERROR("failed to put m_node.\n");
					goto err_clear;
				}
			}
		}
		break;
	case IPP_CMD_WB:
		/* destination memory list */
		head = &c_node->mem_list[EXYNOS_DRM_OPS_DST];

		list_for_each_entry_safe(m_node, tm_node, head, list) {
			ret = ipp_put_mem_node(drm_dev, c_node, m_node);
			if (ret) {
				DRM_ERROR("failed to put m_node.\n");
				goto err_clear;
			}
		}
		break;
	case IPP_CMD_OUTPUT:
		/* source memory list */
		head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];

		list_for_each_entry_safe(m_node, tm_node, head, list) {
			ret = ipp_put_mem_node(drm_dev, c_node, m_node);
			if (ret) {
				DRM_ERROR("failed to put m_node.\n");
				goto err_clear;
			}
		}
		break;
	default:
		DRM_ERROR("invalid operations.\n");
		ret = -EINVAL;
		goto err_clear;
	}

err_clear:
1321 1322
	mutex_unlock(&c_node->mem_lock);

E
Eunchul Kim 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	/* stop operations */
	if (ippdrv->stop)
		ippdrv->stop(ippdrv->dev, property->cmd);

	return ret;
}

void ipp_sched_cmd(struct work_struct *work)
{
	struct drm_exynos_ipp_cmd_work *cmd_work =
		(struct drm_exynos_ipp_cmd_work *)work;
	struct exynos_drm_ippdrv *ippdrv;
	struct drm_exynos_ipp_cmd_node *c_node;
	struct drm_exynos_ipp_property *property;
	int ret;

	ippdrv = cmd_work->ippdrv;
	if (!ippdrv) {
		DRM_ERROR("invalid ippdrv list.\n");
		return;
	}

	c_node = cmd_work->c_node;
	if (!c_node) {
		DRM_ERROR("invalid command node list.\n");
		return;
	}

1351
	mutex_lock(&c_node->lock);
E
Eunchul Kim 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

	property = &c_node->property;

	switch (cmd_work->ctrl) {
	case IPP_CTRL_PLAY:
	case IPP_CTRL_RESUME:
		ret = ipp_start_property(ippdrv, c_node);
		if (ret) {
			DRM_ERROR("failed to start property:prop_id[%d]\n",
				c_node->property.prop_id);
			goto err_unlock;
		}

		/*
		 * M2M case supports wait_completion of transfer.
		 * because M2M case supports single unit operation
		 * with multiple queue.
		 * M2M need to wait completion of data transfer.
		 */
		if (ipp_is_m2m_cmd(property->cmd)) {
			if (!wait_for_completion_timeout
			    (&c_node->start_complete, msecs_to_jiffies(200))) {
				DRM_ERROR("timeout event:prop_id[%d]\n",
					c_node->property.prop_id);
				goto err_unlock;
			}
		}
		break;
	case IPP_CTRL_STOP:
	case IPP_CTRL_PAUSE:
		ret = ipp_stop_property(ippdrv->drm_dev, ippdrv,
			c_node);
		if (ret) {
			DRM_ERROR("failed to stop property.\n");
			goto err_unlock;
		}

		complete(&c_node->stop_complete);
		break;
	default:
		DRM_ERROR("unknown control type\n");
		break;
	}

1396
	DRM_DEBUG_KMS("ctrl[%d] done.\n", cmd_work->ctrl);
E
Eunchul Kim 已提交
1397 1398

err_unlock:
1399
	mutex_unlock(&c_node->lock);
E
Eunchul Kim 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
}

static int ipp_send_event(struct exynos_drm_ippdrv *ippdrv,
		struct drm_exynos_ipp_cmd_node *c_node, int *buf_id)
{
	struct drm_device *drm_dev = ippdrv->drm_dev;
	struct drm_exynos_ipp_property *property = &c_node->property;
	struct drm_exynos_ipp_mem_node *m_node;
	struct drm_exynos_ipp_queue_buf qbuf;
	struct drm_exynos_ipp_send_event *e;
	struct list_head *head;
	struct timeval now;
	unsigned long flags;
	u32 tbuf_id[EXYNOS_DRM_OPS_MAX] = {0, };
	int ret, i;

	for_each_ipp_ops(i)
1417
		DRM_DEBUG_KMS("%s buf_id[%d]\n", i ? "dst" : "src", buf_id[i]);
E
Eunchul Kim 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

	if (!drm_dev) {
		DRM_ERROR("failed to get drm_dev.\n");
		return -EINVAL;
	}

	if (!property) {
		DRM_ERROR("failed to get property.\n");
		return -EINVAL;
	}

1429
	mutex_lock(&c_node->event_lock);
E
Eunchul Kim 已提交
1430
	if (list_empty(&c_node->event_list)) {
1431
		DRM_DEBUG_KMS("event list is empty.\n");
1432 1433
		ret = 0;
		goto err_event_unlock;
E
Eunchul Kim 已提交
1434 1435
	}

1436
	mutex_lock(&c_node->mem_lock);
E
Eunchul Kim 已提交
1437
	if (!ipp_check_mem_list(c_node)) {
1438
		DRM_DEBUG_KMS("empty memory.\n");
1439 1440
		ret = 0;
		goto err_mem_unlock;
E
Eunchul Kim 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	}

	/* check command */
	switch (property->cmd) {
	case IPP_CMD_M2M:
		for_each_ipp_ops(i) {
			/* source/destination memory list */
			head = &c_node->mem_list[i];

			m_node = list_first_entry(head,
				struct drm_exynos_ipp_mem_node, list);

			tbuf_id[i] = m_node->buf_id;
1454
			DRM_DEBUG_KMS("%s buf_id[%d]\n",
E
Eunchul Kim 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
				i ? "dst" : "src", tbuf_id[i]);

			ret = ipp_put_mem_node(drm_dev, c_node, m_node);
			if (ret)
				DRM_ERROR("failed to put m_node.\n");
		}
		break;
	case IPP_CMD_WB:
		/* clear buf for finding */
		memset(&qbuf, 0x0, sizeof(qbuf));
		qbuf.ops_id = EXYNOS_DRM_OPS_DST;
		qbuf.buf_id = buf_id[EXYNOS_DRM_OPS_DST];

		/* get memory node entry */
		m_node = ipp_find_mem_node(c_node, &qbuf);
		if (!m_node) {
			DRM_ERROR("empty memory node.\n");
1472 1473
			ret = -ENOMEM;
			goto err_mem_unlock;
E
Eunchul Kim 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
		}

		tbuf_id[EXYNOS_DRM_OPS_DST] = m_node->buf_id;

		ret = ipp_put_mem_node(drm_dev, c_node, m_node);
		if (ret)
			DRM_ERROR("failed to put m_node.\n");
		break;
	case IPP_CMD_OUTPUT:
		/* source memory list */
		head = &c_node->mem_list[EXYNOS_DRM_OPS_SRC];

		m_node = list_first_entry(head,
			struct drm_exynos_ipp_mem_node, list);

		tbuf_id[EXYNOS_DRM_OPS_SRC] = m_node->buf_id;

		ret = ipp_put_mem_node(drm_dev, c_node, m_node);
		if (ret)
			DRM_ERROR("failed to put m_node.\n");
		break;
	default:
		DRM_ERROR("invalid operations.\n");
1497 1498
		ret = -EINVAL;
		goto err_mem_unlock;
E
Eunchul Kim 已提交
1499
	}
1500
	mutex_unlock(&c_node->mem_lock);
E
Eunchul Kim 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

	if (tbuf_id[EXYNOS_DRM_OPS_DST] != buf_id[EXYNOS_DRM_OPS_DST])
		DRM_ERROR("failed to match buf_id[%d %d]prop_id[%d]\n",
			tbuf_id[1], buf_id[1], property->prop_id);

	/*
	 * command node have event list of destination buffer
	 * If destination buffer enqueue to mem list,
	 * then we make event and link to event list tail.
	 * so, we get first event for first enqueued buffer.
	 */
	e = list_first_entry(&c_node->event_list,
		struct drm_exynos_ipp_send_event, base.link);

	do_gettimeofday(&now);
1516
	DRM_DEBUG_KMS("tv_sec[%ld]tv_usec[%ld]\n", now.tv_sec, now.tv_usec);
E
Eunchul Kim 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	e->event.tv_sec = now.tv_sec;
	e->event.tv_usec = now.tv_usec;
	e->event.prop_id = property->prop_id;

	/* set buffer id about source destination */
	for_each_ipp_ops(i)
		e->event.buf_id[i] = tbuf_id[i];

	spin_lock_irqsave(&drm_dev->event_lock, flags);
	list_move_tail(&e->base.link, &e->base.file_priv->event_list);
	wake_up_interruptible(&e->base.file_priv->event_wait);
	spin_unlock_irqrestore(&drm_dev->event_lock, flags);
1529
	mutex_unlock(&c_node->event_lock);
E
Eunchul Kim 已提交
1530

1531
	DRM_DEBUG_KMS("done cmd[%d]prop_id[%d]buf_id[%d]\n",
E
Eunchul Kim 已提交
1532 1533 1534
		property->cmd, property->prop_id, tbuf_id[EXYNOS_DRM_OPS_DST]);

	return 0;
1535 1536 1537

err_mem_unlock:
	mutex_unlock(&c_node->mem_lock);
1538 1539
err_event_unlock:
	mutex_unlock(&c_node->event_lock);
1540
	return ret;
E
Eunchul Kim 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
}

void ipp_sched_event(struct work_struct *work)
{
	struct drm_exynos_ipp_event_work *event_work =
		(struct drm_exynos_ipp_event_work *)work;
	struct exynos_drm_ippdrv *ippdrv;
	struct drm_exynos_ipp_cmd_node *c_node;
	int ret;

	if (!event_work) {
		DRM_ERROR("failed to get event_work.\n");
		return;
	}

1556
	DRM_DEBUG_KMS("buf_id[%d]\n", event_work->buf_id[EXYNOS_DRM_OPS_DST]);
E
Eunchul Kim 已提交
1557 1558 1559 1560 1561 1562 1563

	ippdrv = event_work->ippdrv;
	if (!ippdrv) {
		DRM_ERROR("failed to get ipp driver.\n");
		return;
	}

1564
	c_node = ippdrv->c_node;
E
Eunchul Kim 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
	if (!c_node) {
		DRM_ERROR("failed to get command node.\n");
		return;
	}

	/*
	 * IPP supports command thread, event thread synchronization.
	 * If IPP close immediately from user land, then IPP make
	 * synchronization with command thread, so make complete event.
	 * or going out operations.
	 */
	if (c_node->state != IPP_STATE_START) {
1577 1578
		DRM_DEBUG_KMS("bypass state[%d]prop_id[%d]\n",
			c_node->state, c_node->property.prop_id);
E
Eunchul Kim 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
		goto err_completion;
	}

	ret = ipp_send_event(ippdrv, c_node, event_work->buf_id);
	if (ret) {
		DRM_ERROR("failed to send event.\n");
		goto err_completion;
	}

err_completion:
	if (ipp_is_m2m_cmd(c_node->property.cmd))
		complete(&c_node->start_complete);
}

static int ipp_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
{
	struct ipp_context *ctx = get_ipp_context(dev);
	struct exynos_drm_ippdrv *ippdrv;
	int ret, count = 0;

	/* get ipp driver entry */
	list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
		ippdrv->drm_dev = drm_dev;

1603 1604
		ret = ipp_create_id(&ctx->ipp_idr, &ctx->ipp_lock, ippdrv);
		if (ret < 0) {
E
Eunchul Kim 已提交
1605
			DRM_ERROR("failed to create id.\n");
1606
			goto err;
E
Eunchul Kim 已提交
1607
		}
1608
		ippdrv->prop_list.ipp_id = ret;
E
Eunchul Kim 已提交
1609

1610
		DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]ipp_id[%d]\n",
1611
			count++, (int)ippdrv, ret);
E
Eunchul Kim 已提交
1612 1613 1614 1615 1616 1617 1618 1619

		/* store parent device for node */
		ippdrv->parent_dev = dev;

		/* store event work queue and handler */
		ippdrv->event_workq = ctx->event_workq;
		ippdrv->sched_event = ipp_sched_event;
		INIT_LIST_HEAD(&ippdrv->cmd_list);
1620
		mutex_init(&ippdrv->cmd_lock);
1621 1622 1623 1624 1625

		if (is_drm_iommu_supported(drm_dev)) {
			ret = drm_iommu_attach_device(drm_dev, ippdrv->dev);
			if (ret) {
				DRM_ERROR("failed to activate iommu\n");
1626
				goto err;
1627 1628
			}
		}
E
Eunchul Kim 已提交
1629 1630 1631 1632
	}

	return 0;

1633
err:
1634
	/* get ipp driver entry */
1635 1636
	list_for_each_entry_continue_reverse(ippdrv, &exynos_drm_ippdrv_list,
						drv_list) {
1637 1638 1639
		if (is_drm_iommu_supported(drm_dev))
			drm_iommu_detach_device(drm_dev, ippdrv->dev);

1640 1641 1642 1643
		ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
				ippdrv->prop_list.ipp_id);
	}

E
Eunchul Kim 已提交
1644 1645 1646 1647 1648 1649
	return ret;
}

static void ipp_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
{
	struct exynos_drm_ippdrv *ippdrv;
1650
	struct ipp_context *ctx = get_ipp_context(dev);
E
Eunchul Kim 已提交
1651 1652 1653

	/* get ipp driver entry */
	list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1654 1655 1656
		if (is_drm_iommu_supported(drm_dev))
			drm_iommu_detach_device(drm_dev, ippdrv->dev);

1657 1658 1659
		ipp_remove_id(&ctx->ipp_idr, &ctx->ipp_lock,
				ippdrv->prop_list.ipp_id);

E
Eunchul Kim 已提交
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
		ippdrv->drm_dev = NULL;
		exynos_drm_ippdrv_unregister(ippdrv);
	}
}

static int ipp_subdrv_open(struct drm_device *drm_dev, struct device *dev,
		struct drm_file *file)
{
	struct drm_exynos_file_private *file_priv = file->driver_priv;

1670
	file_priv->ipp_dev = dev;
E
Eunchul Kim 已提交
1671

1672
	DRM_DEBUG_KMS("done priv[0x%x]\n", (int)dev);
E
Eunchul Kim 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681

	return 0;
}

static void ipp_subdrv_close(struct drm_device *drm_dev, struct device *dev,
		struct drm_file *file)
{
	struct drm_exynos_file_private *file_priv = file->driver_priv;
	struct exynos_drm_ippdrv *ippdrv = NULL;
1682
	struct ipp_context *ctx = get_ipp_context(dev);
E
Eunchul Kim 已提交
1683 1684 1685
	struct drm_exynos_ipp_cmd_node *c_node, *tc_node;
	int count = 0;

1686
	DRM_DEBUG_KMS("for priv[0x%x]\n", (int)file_priv->ipp_dev);
E
Eunchul Kim 已提交
1687 1688

	list_for_each_entry(ippdrv, &exynos_drm_ippdrv_list, drv_list) {
1689
		mutex_lock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
1690 1691
		list_for_each_entry_safe(c_node, tc_node,
			&ippdrv->cmd_list, list) {
1692 1693
			DRM_DEBUG_KMS("count[%d]ippdrv[0x%x]\n",
				count++, (int)ippdrv);
E
Eunchul Kim 已提交
1694

1695
			if (c_node->dev == file_priv->ipp_dev) {
E
Eunchul Kim 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
				/*
				 * userland goto unnormal state. process killed.
				 * and close the file.
				 * so, IPP didn't called stop cmd ctrl.
				 * so, we are make stop operation in this state.
				 */
				if (c_node->state == IPP_STATE_START) {
					ipp_stop_property(drm_dev, ippdrv,
						c_node);
					c_node->state = IPP_STATE_STOP;
				}

				ippdrv->dedicated = false;
1709
				ipp_clean_cmd_node(ctx, c_node);
E
Eunchul Kim 已提交
1710 1711 1712 1713
				if (list_empty(&ippdrv->cmd_list))
					pm_runtime_put_sync(ippdrv->dev);
			}
		}
1714
		mutex_unlock(&ippdrv->cmd_lock);
E
Eunchul Kim 已提交
1715 1716 1717 1718 1719
	}

	return;
}

1720
static int ipp_probe(struct platform_device *pdev)
E
Eunchul Kim 已提交
1721 1722 1723 1724 1725 1726
{
	struct device *dev = &pdev->dev;
	struct ipp_context *ctx;
	struct exynos_drm_subdrv *subdrv;
	int ret;

1727
	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
E
Eunchul Kim 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
	if (!ctx)
		return -ENOMEM;

	mutex_init(&ctx->ipp_lock);
	mutex_init(&ctx->prop_lock);

	idr_init(&ctx->ipp_idr);
	idr_init(&ctx->prop_idr);

	/*
	 * create single thread for ipp event
	 * IPP supports event thread for IPP drivers.
	 * IPP driver send event_work to this thread.
	 * and IPP event thread send event to user process.
	 */
	ctx->event_workq = create_singlethread_workqueue("ipp_event");
	if (!ctx->event_workq) {
		dev_err(dev, "failed to create event workqueue\n");
1746
		return -EINVAL;
E
Eunchul Kim 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
	}

	/*
	 * create single thread for ipp command
	 * IPP supports command thread for user process.
	 * user process make command node using set property ioctl.
	 * and make start_work and send this work to command thread.
	 * and then this command thread start property.
	 */
	ctx->cmd_workq = create_singlethread_workqueue("ipp_cmd");
	if (!ctx->cmd_workq) {
		dev_err(dev, "failed to create cmd workqueue\n");
		ret = -EINVAL;
		goto err_event_workq;
	}

	/* set sub driver informations */
	subdrv = &ctx->subdrv;
	subdrv->dev = dev;
	subdrv->probe = ipp_subdrv_probe;
	subdrv->remove = ipp_subdrv_remove;
	subdrv->open = ipp_subdrv_open;
	subdrv->close = ipp_subdrv_close;

	platform_set_drvdata(pdev, ctx);

	ret = exynos_drm_subdrv_register(subdrv);
	if (ret < 0) {
		DRM_ERROR("failed to register drm ipp device.\n");
		goto err_cmd_workq;
	}

1779
	dev_info(dev, "drm ipp registered successfully.\n");
E
Eunchul Kim 已提交
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789

	return 0;

err_cmd_workq:
	destroy_workqueue(ctx->cmd_workq);
err_event_workq:
	destroy_workqueue(ctx->event_workq);
	return ret;
}

1790
static int ipp_remove(struct platform_device *pdev)
E
Eunchul Kim 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
{
	struct ipp_context *ctx = platform_get_drvdata(pdev);

	/* unregister sub driver */
	exynos_drm_subdrv_unregister(&ctx->subdrv);

	/* remove,destroy ipp idr */
	idr_destroy(&ctx->ipp_idr);
	idr_destroy(&ctx->prop_idr);

	mutex_destroy(&ctx->ipp_lock);
	mutex_destroy(&ctx->prop_lock);

	/* destroy command, event work queue */
	destroy_workqueue(ctx->cmd_workq);
	destroy_workqueue(ctx->event_workq);

	return 0;
}

static int ipp_power_ctrl(struct ipp_context *ctx, bool enable)
{
1813
	DRM_DEBUG_KMS("enable[%d]\n", enable);
E
Eunchul Kim 已提交
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int ipp_suspend(struct device *dev)
{
	struct ipp_context *ctx = get_ipp_context(dev);

	if (pm_runtime_suspended(dev))
		return 0;

	return ipp_power_ctrl(ctx, false);
}

static int ipp_resume(struct device *dev)
{
	struct ipp_context *ctx = get_ipp_context(dev);

	if (!pm_runtime_suspended(dev))
		return ipp_power_ctrl(ctx, true);

	return 0;
}
#endif

#ifdef CONFIG_PM_RUNTIME
static int ipp_runtime_suspend(struct device *dev)
{
	struct ipp_context *ctx = get_ipp_context(dev);

	return ipp_power_ctrl(ctx, false);
}

static int ipp_runtime_resume(struct device *dev)
{
	struct ipp_context *ctx = get_ipp_context(dev);

	return ipp_power_ctrl(ctx, true);
}
#endif

static const struct dev_pm_ops ipp_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(ipp_suspend, ipp_resume)
	SET_RUNTIME_PM_OPS(ipp_runtime_suspend, ipp_runtime_resume, NULL)
};

struct platform_driver ipp_driver = {
	.probe		= ipp_probe,
1863
	.remove		= ipp_remove,
E
Eunchul Kim 已提交
1864 1865 1866 1867 1868 1869 1870
	.driver		= {
		.name	= "exynos-drm-ipp",
		.owner	= THIS_MODULE,
		.pm	= &ipp_pm_ops,
	},
};