File Index
Files and directories
Sign in
FILE

exynos_drm_ipp.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/exynos/exynos_drm_ipp.c C
  1/*
  2 * Copyright (C) 2017 Samsung Electronics Co.Ltd
  3 * Authors:
  4 *	Marek Szyprowski <m.szyprowski@samsung.com>
  5 *
  6 * Exynos DRM Image Post Processing (IPP) related functions
  7 *
  8 * Permission is hereby granted, free of charge, to any person obtaining a
  9 * copy of this software and associated documentation files (the "Software"),
 10 * to deal in the Software without restriction, including without limitation
 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 12 * and/or sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following conditions:
 14 *
 15 * The above copyright notice and this permission notice shall be included in
 16 * all copies or substantial portions of the Software.
 17 */
 18
 19#include <linux/uaccess.h>
 20
 21#include <drm/drm_blend.h>
 22#include <drm/drm_file.h>
 23#include <drm/drm_fourcc.h>
 24#include <drm/drm_mode.h>
 25#include <drm/drm_print.h>
 26#include <drm/exynos_drm.h>
 27
 28#include "exynos_drm_drv.h"
 29#include "exynos_drm_gem.h"
 30#include "exynos_drm_ipp.h"
 31
 32static int num_ipp;
 33static LIST_HEAD(ipp_list);
 34
 35/**
 36 * exynos_drm_ipp_register - Register a new picture processor hardware module
 37 * @dev: DRM device
 38 * @ipp: ipp module to init
 39 * @funcs: callbacks for the new ipp object
 40 * @caps: bitmask of ipp capabilities (%DRM_EXYNOS_IPP_CAP_*)
 41 * @formats: array of supported formats
 42 * @num_formats: size of the supported formats array
 43 * @name: name (for debugging purposes)
 44 *
 45 * Initializes a ipp module.
 46 *
 47 * Returns:
 48 * Zero on success, error code on failure.
 49 */
 50int exynos_drm_ipp_register(struct device *dev, struct exynos_drm_ipp *ipp,
 51		const struct exynos_drm_ipp_funcs *funcs, unsigned int caps,
 52		const struct exynos_drm_ipp_formats *formats,
 53		unsigned int num_formats, const char *name)
 54{
 55	WARN_ON(!ipp);
 56	WARN_ON(!funcs);
 57	WARN_ON(!formats);
 58	WARN_ON(!num_formats);
 59
 60	spin_lock_init(&ipp->lock);
 61	INIT_LIST_HEAD(&ipp->todo_list);
 62	init_waitqueue_head(&ipp->done_wq);
 63	ipp->dev = dev;
 64	ipp->funcs = funcs;
 65	ipp->capabilities = caps;
 66	ipp->name = name;
 67	ipp->formats = formats;
 68	ipp->num_formats = num_formats;
 69
 70	/* ipp_list modification is serialized by component framework */
 71	list_add_tail(&ipp->head, &ipp_list);
 72	ipp->id = num_ipp++;
 73
 74	DRM_DEV_DEBUG_DRIVER(dev, "Registered ipp %d\n", ipp->id);
 75
 76	return 0;
 77}
 78
 79/**
 80 * exynos_drm_ipp_unregister - Unregister the picture processor module
 81 * @dev: DRM device
 82 * @ipp: ipp module
 83 */
 84void exynos_drm_ipp_unregister(struct device *dev,
 85			       struct exynos_drm_ipp *ipp)
 86{
 87	WARN_ON(ipp->task);
 88	WARN_ON(!list_empty(&ipp->todo_list));
 89	list_del(&ipp->head);
 90}
 91
 92/**
 93 * exynos_drm_ipp_get_res_ioctl - enumerate all ipp modules
 94 * @dev: DRM device
 95 * @data: ioctl data
 96 * @file_priv: DRM file info
 97 *
 98 * Construct a list of ipp ids.
 99 *
100 * Called by the user via ioctl.
101 *
102 * Returns:
103 * Zero on success, negative errno on failure.
104 */
105int exynos_drm_ipp_get_res_ioctl(struct drm_device *dev, void *data,
106				 struct drm_file *file_priv)
107{
108	struct drm_exynos_ioctl_ipp_get_res *resp = data;
109	struct exynos_drm_ipp *ipp;
110	uint32_t __user *ipp_ptr = (uint32_t __user *)
111						(unsigned long)resp->ipp_id_ptr;
112	unsigned int count = num_ipp, copied = 0;
113
114	/*
115	 * This ioctl is called twice, once to determine how much space is
116	 * needed, and the 2nd time to fill it.
117	 */
118	if (count && resp->count_ipps >= count) {
119		list_for_each_entry(ipp, &ipp_list, head) {
120			if (put_user(ipp->id, ipp_ptr + copied))
121				return -EFAULT;
122			copied++;
123		}
124	}
125	resp->count_ipps = count;
126
127	return 0;
128}
129
130static inline struct exynos_drm_ipp *__ipp_get(uint32_t id)
131{
132	struct exynos_drm_ipp *ipp;
133
134	list_for_each_entry(ipp, &ipp_list, head)
135		if (ipp->id == id)
136			return ipp;
137	return NULL;
138}
139
140/**
141 * exynos_drm_ipp_get_caps_ioctl - get ipp module capabilities and formats
142 * @dev: DRM device
143 * @data: ioctl data
144 * @file_priv: DRM file info
145 *
146 * Construct a structure describing ipp module capabilities.
147 *
148 * Called by the user via ioctl.
149 *
150 * Returns:
151 * Zero on success, negative errno on failure.
152 */
153int exynos_drm_ipp_get_caps_ioctl(struct drm_device *dev, void *data,
154				  struct drm_file *file_priv)
155{
156	struct drm_exynos_ioctl_ipp_get_caps *resp = data;
157	void __user *ptr = (void __user *)(unsigned long)resp->formats_ptr;
158	struct exynos_drm_ipp *ipp;
159	int i;
160
161	ipp = __ipp_get(resp->ipp_id);
162	if (!ipp)
163		return -ENOENT;
164
165	resp->ipp_id = ipp->id;
166	resp->capabilities = ipp->capabilities;
167
168	/*
169	 * This ioctl is called twice, once to determine how much space is
170	 * needed, and the 2nd time to fill it.
171	 */
172	if (resp->formats_count >= ipp->num_formats) {
173		for (i = 0; i < ipp->num_formats; i++) {
174			struct drm_exynos_ipp_format tmp = {
175				.fourcc = ipp->formats[i].fourcc,
176				.type = ipp->formats[i].type,
177				.modifier = ipp->formats[i].modifier,
178			};
179
180			if (copy_to_user(ptr, &tmp, sizeof(tmp)))
181				return -EFAULT;
182			ptr += sizeof(tmp);
183		}
184	}
185	resp->formats_count = ipp->num_formats;
186
187	return 0;
188}
189
190static inline const struct exynos_drm_ipp_formats *__ipp_format_get(
191				struct exynos_drm_ipp *ipp, uint32_t fourcc,
192				uint64_t mod, unsigned int type)
193{
194	int i;
195
196	for (i = 0; i < ipp->num_formats; i++) {
197		if ((ipp->formats[i].type & type) &&
198		    ipp->formats[i].fourcc == fourcc &&
199		    ipp->formats[i].modifier == mod)
200			return &ipp->formats[i];
201	}
202	return NULL;
203}
204
205/**
206 * exynos_drm_ipp_get_limits_ioctl - get ipp module limits
207 * @dev: DRM device
208 * @data: ioctl data
209 * @file_priv: DRM file info
210 *
211 * Construct a structure describing ipp module limitations for provided
212 * picture format.
213 *
214 * Called by the user via ioctl.
215 *
216 * Returns:
217 * Zero on success, negative errno on failure.
218 */
219int exynos_drm_ipp_get_limits_ioctl(struct drm_device *dev, void *data,
220				    struct drm_file *file_priv)
221{
222	struct drm_exynos_ioctl_ipp_get_limits *resp = data;
223	void __user *ptr = (void __user *)(unsigned long)resp->limits_ptr;
224	const struct exynos_drm_ipp_formats *format;
225	struct exynos_drm_ipp *ipp;
226
227	if (resp->type != DRM_EXYNOS_IPP_FORMAT_SOURCE &&
228	    resp->type != DRM_EXYNOS_IPP_FORMAT_DESTINATION)
229		return -EINVAL;
230
231	ipp = __ipp_get(resp->ipp_id);
232	if (!ipp)
233		return -ENOENT;
234
235	format = __ipp_format_get(ipp, resp->fourcc, resp->modifier,
236				  resp->type);
237	if (!format)
238		return -EINVAL;
239
240	/*
241	 * This ioctl is called twice, once to determine how much space is
242	 * needed, and the 2nd time to fill it.
243	 */
244	if (format->num_limits && resp->limits_count >= format->num_limits)
245		if (copy_to_user((void __user *)ptr, format->limits,
246				 sizeof(*format->limits) * format->num_limits))
247			return -EFAULT;
248	resp->limits_count = format->num_limits;
249
250	return 0;
251}
252
253struct drm_pending_exynos_ipp_event {
254	struct drm_pending_event base;
255	struct drm_exynos_ipp_event event;
256};
257
258static inline struct exynos_drm_ipp_task *
259			exynos_drm_ipp_task_alloc(struct exynos_drm_ipp *ipp)
260{
261	struct exynos_drm_ipp_task *task;
262
263	task = kzalloc(sizeof(*task), GFP_KERNEL);
264	if (!task)
265		return NULL;
266
267	task->dev = ipp->dev;
268	task->ipp = ipp;
269
270	/* some defaults */
271	task->src.rect.w = task->dst.rect.w = UINT_MAX;
272	task->src.rect.h = task->dst.rect.h = UINT_MAX;
273	task->transform.rotation = DRM_MODE_ROTATE_0;
274
275	DRM_DEV_DEBUG_DRIVER(task->dev, "Allocated task %p\n", task);
276
277	return task;
278}
279
280static const struct exynos_drm_param_map {
281	unsigned int id;
282	unsigned int size;
283	unsigned int offset;
284} exynos_drm_ipp_params_maps[] = {
285	{
286		DRM_EXYNOS_IPP_TASK_BUFFER | DRM_EXYNOS_IPP_TASK_TYPE_SOURCE,
287		sizeof(struct drm_exynos_ipp_task_buffer),
288		offsetof(struct exynos_drm_ipp_task, src.buf),
289	}, {
290		DRM_EXYNOS_IPP_TASK_BUFFER |
291			DRM_EXYNOS_IPP_TASK_TYPE_DESTINATION,
292		sizeof(struct drm_exynos_ipp_task_buffer),
293		offsetof(struct exynos_drm_ipp_task, dst.buf),
294	}, {
295		DRM_EXYNOS_IPP_TASK_RECTANGLE | DRM_EXYNOS_IPP_TASK_TYPE_SOURCE,
296		sizeof(struct drm_exynos_ipp_task_rect),
297		offsetof(struct exynos_drm_ipp_task, src.rect),
298	}, {
299		DRM_EXYNOS_IPP_TASK_RECTANGLE |
300			DRM_EXYNOS_IPP_TASK_TYPE_DESTINATION,
301		sizeof(struct drm_exynos_ipp_task_rect),
302		offsetof(struct exynos_drm_ipp_task, dst.rect),
303	}, {
304		DRM_EXYNOS_IPP_TASK_TRANSFORM,
305		sizeof(struct drm_exynos_ipp_task_transform),
306		offsetof(struct exynos_drm_ipp_task, transform),
307	}, {
308		DRM_EXYNOS_IPP_TASK_ALPHA,
309		sizeof(struct drm_exynos_ipp_task_alpha),
310		offsetof(struct exynos_drm_ipp_task, alpha),
311	},
312};
313
314static int exynos_drm_ipp_task_set(struct exynos_drm_ipp_task *task,
315				   struct drm_exynos_ioctl_ipp_commit *arg)
316{
317	const struct exynos_drm_param_map *map = exynos_drm_ipp_params_maps;
318	void __user *params = (void __user *)(unsigned long)arg->params_ptr;
319	unsigned int size = arg->params_size;
320	uint32_t id;
321	int i;
322
323	while (size) {
324		if (get_user(id, (uint32_t __user *)params))
325			return -EFAULT;
326
327		for (i = 0; i < ARRAY_SIZE(exynos_drm_ipp_params_maps); i++)
328			if (map[i].id == id)
329				break;
330		if (i == ARRAY_SIZE(exynos_drm_ipp_params_maps) ||
331		    map[i].size > size)
332			return -EINVAL;
333
334		if (copy_from_user((void *)task + map[i].offset, params,
335				   map[i].size))
336			return -EFAULT;
337
338		params += map[i].size;
339		size -= map[i].size;
340	}
341
342	DRM_DEV_DEBUG_DRIVER(task->dev,
343			     "Got task %p configuration from userspace\n",
344			     task);
345	return 0;
346}
347
348static int exynos_drm_ipp_task_setup_buffer(struct exynos_drm_ipp_buffer *buf,
349					    struct drm_file *filp)
350{
351	int ret = 0;
352	int i;
353
354	/* get GEM buffers and check their size */
355	for (i = 0; i < buf->format->num_planes; i++) {
356		unsigned int height = (i == 0) ? buf->buf.height :
357			     DIV_ROUND_UP(buf->buf.height, buf->format->vsub);
358		unsigned long size = height * buf->buf.pitch[i];
359		struct exynos_drm_gem *gem = exynos_drm_gem_get(filp,
360							    buf->buf.gem_id[i]);
361		if (!gem) {
362			ret = -ENOENT;
363			goto gem_free;
364		}
365		buf->exynos_gem[i] = gem;
366
367		if (size + buf->buf.offset[i] > buf->exynos_gem[i]->size) {
368			i++;
369			ret = -EINVAL;
370			goto gem_free;
371		}
372		buf->dma_addr[i] = buf->exynos_gem[i]->dma_addr +
373				   buf->buf.offset[i];
374	}
375
376	return 0;
377gem_free:
378	while (i--) {
379		exynos_drm_gem_put(buf->exynos_gem[i]);
380		buf->exynos_gem[i] = NULL;
381	}
382	return ret;
383}
384
385static void exynos_drm_ipp_task_release_buf(struct exynos_drm_ipp_buffer *buf)
386{
387	int i;
388
389	if (!buf->exynos_gem[0])
390		return;
391	for (i = 0; i < buf->format->num_planes; i++)
392		exynos_drm_gem_put(buf->exynos_gem[i]);
393}
394
395static void exynos_drm_ipp_task_free(struct exynos_drm_ipp *ipp,
396				 struct exynos_drm_ipp_task *task)
397{
398	DRM_DEV_DEBUG_DRIVER(task->dev, "Freeing task %p\n", task);
399
400	exynos_drm_ipp_task_release_buf(&task->src);
401	exynos_drm_ipp_task_release_buf(&task->dst);
402	if (task->event)
403		drm_event_cancel_free(ipp->drm_dev, &task->event->base);
404	kfree(task);
405}
406
407struct drm_ipp_limit {
408	struct drm_exynos_ipp_limit_val h;
409	struct drm_exynos_ipp_limit_val v;
410};
411
412enum drm_ipp_size_id {
413	IPP_LIMIT_BUFFER, IPP_LIMIT_AREA, IPP_LIMIT_ROTATED, IPP_LIMIT_MAX
414};
415
416static const enum drm_exynos_ipp_limit_type limit_id_fallback[IPP_LIMIT_MAX][4] = {
417	[IPP_LIMIT_BUFFER]  = { DRM_EXYNOS_IPP_LIMIT_SIZE_BUFFER },
418	[IPP_LIMIT_AREA]    = { DRM_EXYNOS_IPP_LIMIT_SIZE_AREA,
419				DRM_EXYNOS_IPP_LIMIT_SIZE_BUFFER },
420	[IPP_LIMIT_ROTATED] = { DRM_EXYNOS_IPP_LIMIT_SIZE_ROTATED,
421				DRM_EXYNOS_IPP_LIMIT_SIZE_AREA,
422				DRM_EXYNOS_IPP_LIMIT_SIZE_BUFFER },
423};
424
425static inline void __limit_set_val(unsigned int *ptr, unsigned int val)
426{
427	if (!*ptr)
428		*ptr = val;
429}
430
431static void __get_size_limit(const struct drm_exynos_ipp_limit *limits,
432			     unsigned int num_limits, enum drm_ipp_size_id id,
433			     struct drm_ipp_limit *res)
434{
435	const struct drm_exynos_ipp_limit *l = limits;
436	int i = 0;
437
438	memset(res, 0, sizeof(*res));
439	for (i = 0; limit_id_fallback[id][i]; i++)
440		for (l = limits; l - limits < num_limits; l++) {
441			if (((l->type & DRM_EXYNOS_IPP_LIMIT_TYPE_MASK) !=
442			      DRM_EXYNOS_IPP_LIMIT_TYPE_SIZE) ||
443			    ((l->type & DRM_EXYNOS_IPP_LIMIT_SIZE_MASK) !=
444						     limit_id_fallback[id][i]))
445				continue;
446			__limit_set_val(&res->h.min, l->h.min);
447			__limit_set_val(&res->h.max, l->h.max);
448			__limit_set_val(&res->h.align, l->h.align);
449			__limit_set_val(&res->v.min, l->v.min);
450			__limit_set_val(&res->v.max, l->v.max);
451			__limit_set_val(&res->v.align, l->v.align);
452		}
453}
454
455static inline bool __align_check(unsigned int val, unsigned int align)
456{
457	if (align && (val & (align - 1))) {
458		DRM_DEBUG_DRIVER("Value %d exceeds HW limits (align %d)\n",
459				 val, align);
460		return false;
461	}
462	return true;
463}
464
465static inline bool __size_limit_check(unsigned int val,
466				 struct drm_exynos_ipp_limit_val *l)
467{
468	if ((l->min && val < l->min) || (l->max && val > l->max)) {
469		DRM_DEBUG_DRIVER("Value %d exceeds HW limits (min %d, max %d)\n",
470				 val, l->min, l->max);
471		return false;
472	}
473	return __align_check(val, l->align);
474}
475
476static int exynos_drm_ipp_check_size_limits(struct exynos_drm_ipp_buffer *buf,
477	const struct drm_exynos_ipp_limit *limits, unsigned int num_limits,
478	bool rotate, bool swap)
479{
480	enum drm_ipp_size_id id = rotate ? IPP_LIMIT_ROTATED : IPP_LIMIT_AREA;
481	struct drm_ipp_limit l;
482	struct drm_exynos_ipp_limit_val *lh = &l.h, *lv = &l.v;
483	int real_width = buf->buf.pitch[0] / buf->format->cpp[0];
484
485	if (!limits)
486		return 0;
487
488	__get_size_limit(limits, num_limits, IPP_LIMIT_BUFFER, &l);
489	if (!__size_limit_check(real_width, &l.h) ||
490	    !__size_limit_check(buf->buf.height, &l.v))
491		return -EINVAL;
492
493	if (swap) {
494		lv = &l.h;
495		lh = &l.v;
496	}
497	__get_size_limit(limits, num_limits, id, &l);
498	if (!__size_limit_check(buf->rect.w, lh) ||
499	    !__align_check(buf->rect.x, lh->align) ||
500	    !__size_limit_check(buf->rect.h, lv) ||
501	    !__align_check(buf->rect.y, lv->align))
502		return -EINVAL;
503
504	return 0;
505}
506
507static inline bool __scale_limit_check(unsigned int src, unsigned int dst,
508				       unsigned int min, unsigned int max)
509{
510	if ((max && (dst << 16) > src * max) ||
511	    (min && (dst << 16) < src * min)) {
512		DRM_DEBUG_DRIVER("Scale from %d to %d exceeds HW limits (ratio min %d.%05d, max %d.%05d)\n",
513			 src, dst,
514			 min >> 16, 100000 * (min & 0xffff) / (1 << 16),
515			 max >> 16, 100000 * (max & 0xffff) / (1 << 16));
516		return false;
517	}
518	return true;
519}
520
521static int exynos_drm_ipp_check_scale_limits(
522				struct drm_exynos_ipp_task_rect *src,
523				struct drm_exynos_ipp_task_rect *dst,
524				const struct drm_exynos_ipp_limit *limits,
525				unsigned int num_limits, bool swap)
526{
527	const struct drm_exynos_ipp_limit_val *lh, *lv;
528	int dw, dh;
529
530	for (; num_limits; limits++, num_limits--)
531		if ((limits->type & DRM_EXYNOS_IPP_LIMIT_TYPE_MASK) ==
532		    DRM_EXYNOS_IPP_LIMIT_TYPE_SCALE)
533			break;
534	if (!num_limits)
535		return 0;
536
537	lh = (!swap) ? &limits->h : &limits->v;
538	lv = (!swap) ? &limits->v : &limits->h;
539	dw = (!swap) ? dst->w : dst->h;
540	dh = (!swap) ? dst->h : dst->w;
541
542	if (!__scale_limit_check(src->w, dw, lh->min, lh->max) ||
543	    !__scale_limit_check(src->h, dh, lv->min, lv->max))
544		return -EINVAL;
545
546	return 0;
547}
548
549static int exynos_drm_ipp_check_format(struct exynos_drm_ipp_task *task,
550				       struct exynos_drm_ipp_buffer *buf,
551				       struct exynos_drm_ipp_buffer *src,
552				       struct exynos_drm_ipp_buffer *dst,
553				       bool rotate, bool swap)
554{
555	const struct exynos_drm_ipp_formats *fmt;
556	int ret, i;
557
558	fmt = __ipp_format_get(task->ipp, buf->buf.fourcc, buf->buf.modifier,
559			       buf == src ? DRM_EXYNOS_IPP_FORMAT_SOURCE :
560					    DRM_EXYNOS_IPP_FORMAT_DESTINATION);
561	if (!fmt) {
562		DRM_DEV_DEBUG_DRIVER(task->dev,
563				     "Task %p: %s format not supported\n",
564				     task, buf == src ? "src" : "dst");
565		return -EINVAL;
566	}
567
568	/* basic checks */
569	if (buf->buf.width == 0 || buf->buf.height == 0)
570		return -EINVAL;
571
572	buf->format = drm_format_info(buf->buf.fourcc);
573	for (i = 0; i < buf->format->num_planes; i++) {
574		unsigned int width = (i == 0) ? buf->buf.width :
575			     DIV_ROUND_UP(buf->buf.width, buf->format->hsub);
576
577		if (buf->buf.pitch[i] == 0)
578			buf->buf.pitch[i] = width * buf->format->cpp[i];
579		if (buf->buf.pitch[i] < width * buf->format->cpp[i])
580			return -EINVAL;
581		if (!buf->buf.gem_id[i])
582			return -ENOENT;
583	}
584
585	/* pitch for additional planes must match */
586	if (buf->format->num_planes > 2 &&
587	    buf->buf.pitch[1] != buf->buf.pitch[2])
588		return -EINVAL;
589
590	/* check driver limits */
591	ret = exynos_drm_ipp_check_size_limits(buf, fmt->limits,
592					       fmt->num_limits,
593					       rotate,
594					       buf == dst ? swap : false);
595	if (ret)
596		return ret;
597	ret = exynos_drm_ipp_check_scale_limits(&src->rect, &dst->rect,
598						fmt->limits,
599						fmt->num_limits, swap);
600	return ret;
601}
602
603static int exynos_drm_ipp_task_check(struct exynos_drm_ipp_task *task)
604{
605	struct exynos_drm_ipp *ipp = task->ipp;
606	struct exynos_drm_ipp_buffer *src = &task->src, *dst = &task->dst;
607	unsigned int rotation = task->transform.rotation;
608	int ret = 0;
609	bool swap = drm_rotation_90_or_270(rotation);
610	bool rotate = (rotation != DRM_MODE_ROTATE_0);
611	bool scale = false;
612
613	DRM_DEV_DEBUG_DRIVER(task->dev, "Checking task %p\n", task);
614
615	if (src->rect.w == UINT_MAX)
616		src->rect.w = src->buf.width;
617	if (src->rect.h == UINT_MAX)
618		src->rect.h = src->buf.height;
619	if (dst->rect.w == UINT_MAX)
620		dst->rect.w = dst->buf.width;
621	if (dst->rect.h == UINT_MAX)
622		dst->rect.h = dst->buf.height;
623
624	if (src->rect.x + src->rect.w > (src->buf.width) ||
625	    src->rect.y + src->rect.h > (src->buf.height) ||
626	    dst->rect.x + dst->rect.w > (dst->buf.width) ||
627	    dst->rect.y + dst->rect.h > (dst->buf.height)) {
628		DRM_DEV_DEBUG_DRIVER(task->dev,
629				     "Task %p: defined area is outside provided buffers\n",
630				     task);
631		return -EINVAL;
632	}
633
634	if ((!swap && (src->rect.w != dst->rect.w ||
635		       src->rect.h != dst->rect.h)) ||
636	    (swap && (src->rect.w != dst->rect.h ||
637		      src->rect.h != dst->rect.w)))
638		scale = true;
639
640	if ((!(ipp->capabilities & DRM_EXYNOS_IPP_CAP_CROP) &&
641	     (src->rect.x || src->rect.y || dst->rect.x || dst->rect.y)) ||
642	    (!(ipp->capabilities & DRM_EXYNOS_IPP_CAP_ROTATE) && rotate) ||
643	    (!(ipp->capabilities & DRM_EXYNOS_IPP_CAP_SCALE) && scale) ||
644	    (!(ipp->capabilities & DRM_EXYNOS_IPP_CAP_CONVERT) &&
645	     src->buf.fourcc != dst->buf.fourcc)) {
646		DRM_DEV_DEBUG_DRIVER(task->dev, "Task %p: hw capabilities exceeded\n",
647				     task);
648		return -EINVAL;
649	}
650
651	ret = exynos_drm_ipp_check_format(task, src, src, dst, rotate, swap);
652	if (ret)
653		return ret;
654
655	ret = exynos_drm_ipp_check_format(task, dst, src, dst, false, swap);
656	if (ret)
657		return ret;
658
659	DRM_DEV_DEBUG_DRIVER(ipp->dev, "Task %p: all checks done.\n",
660			     task);
661
662	return ret;
663}
664
665static int exynos_drm_ipp_task_setup_buffers(struct exynos_drm_ipp_task *task,
666				     struct drm_file *filp)
667{
668	struct exynos_drm_ipp_buffer *src = &task->src, *dst = &task->dst;
669	int ret = 0;
670
671	DRM_DEV_DEBUG_DRIVER(task->dev, "Setting buffer for task %p\n",
672			     task);
673
674	ret = exynos_drm_ipp_task_setup_buffer(src, filp);
675	if (ret) {
676		DRM_DEV_DEBUG_DRIVER(task->dev,
677				     "Task %p: src buffer setup failed\n",
678				     task);
679		return ret;
680	}
681	ret = exynos_drm_ipp_task_setup_buffer(dst, filp);
682	if (ret) {
683		DRM_DEV_DEBUG_DRIVER(task->dev,
684				     "Task %p: dst buffer setup failed\n",
685				     task);
686		return ret;
687	}
688
689	DRM_DEV_DEBUG_DRIVER(task->dev, "Task %p: buffers prepared.\n",
690			     task);
691
692	return ret;
693}
694
695
696static int exynos_drm_ipp_event_create(struct exynos_drm_ipp_task *task,
697				 struct drm_file *file_priv, uint64_t user_data)
698{
699	struct drm_pending_exynos_ipp_event *e = NULL;
700	int ret;
701
702	e = kzalloc(sizeof(*e), GFP_KERNEL);
703	if (!e)
704		return -ENOMEM;
705
706	e->event.base.type = DRM_EXYNOS_IPP_EVENT;
707	e->event.base.length = sizeof(e->event);
708	e->event.user_data = user_data;
709
710	ret = drm_event_reserve_init(task->ipp->drm_dev, file_priv, &e->base,
711				     &e->event.base);
712	if (ret)
713		goto free;
714
715	task->event = e;
716	return 0;
717free:
718	kfree(e);
719	return ret;
720}
721
722static void exynos_drm_ipp_event_send(struct exynos_drm_ipp_task *task)
723{
724	struct timespec64 now;
725
726	ktime_get_ts64(&now);
727	task->event->event.tv_sec = now.tv_sec;
728	task->event->event.tv_usec = now.tv_nsec / NSEC_PER_USEC;
729	task->event->event.sequence = atomic_inc_return(&task->ipp->sequence);
730
731	drm_send_event(task->ipp->drm_dev, &task->event->base);
732}
733
734static int exynos_drm_ipp_task_cleanup(struct exynos_drm_ipp_task *task)
735{
736	int ret = task->ret;
737
738	if (ret == 0 && task->event) {
739		exynos_drm_ipp_event_send(task);
740		/* ensure event won't be canceled on task free */
741		task->event = NULL;
742	}
743
744	exynos_drm_ipp_task_free(task->ipp, task);
745	return ret;
746}
747
748static void exynos_drm_ipp_cleanup_work(struct work_struct *work)
749{
750	struct exynos_drm_ipp_task *task = container_of(work,
751				      struct exynos_drm_ipp_task, cleanup_work);
752
753	exynos_drm_ipp_task_cleanup(task);
754}
755
756static void exynos_drm_ipp_next_task(struct exynos_drm_ipp *ipp);
757
758/**
759 * exynos_drm_ipp_task_done - finish given task and set return code
760 * @task: ipp task to finish
761 * @ret: error code or 0 if operation has been performed successfully
762 */
763void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
764{
765	struct exynos_drm_ipp *ipp = task->ipp;
766	unsigned long flags;
767
768	DRM_DEV_DEBUG_DRIVER(task->dev, "ipp: %d, task %p done: %d\n",
769			     ipp->id, task, ret);
770
771	spin_lock_irqsave(&ipp->lock, flags);
772	if (ipp->task == task)
773		ipp->task = NULL;
774	task->flags |= DRM_EXYNOS_IPP_TASK_DONE;
775	task->ret = ret;
776	spin_unlock_irqrestore(&ipp->lock, flags);
777
778	exynos_drm_ipp_next_task(ipp);
779	wake_up(&ipp->done_wq);
780
781	if (task->flags & DRM_EXYNOS_IPP_TASK_ASYNC) {
782		INIT_WORK(&task->cleanup_work, exynos_drm_ipp_cleanup_work);
783		schedule_work(&task->cleanup_work);
784	}
785}
786
787static void exynos_drm_ipp_next_task(struct exynos_drm_ipp *ipp)
788{
789	struct exynos_drm_ipp_task *task;
790	unsigned long flags;
791	int ret;
792
793	DRM_DEV_DEBUG_DRIVER(ipp->dev, "ipp: %d, try to run new task\n",
794			     ipp->id);
795
796	spin_lock_irqsave(&ipp->lock, flags);
797
798	if (ipp->task || list_empty(&ipp->todo_list)) {
799		spin_unlock_irqrestore(&ipp->lock, flags);
800		return;
801	}
802
803	task = list_first_entry(&ipp->todo_list, struct exynos_drm_ipp_task,
804				head);
805	list_del_init(&task->head);
806	ipp->task = task;
807
808	spin_unlock_irqrestore(&ipp->lock, flags);
809
810	DRM_DEV_DEBUG_DRIVER(ipp->dev,
811			     "ipp: %d, selected task %p to run\n", ipp->id,
812			     task);
813
814	ret = ipp->funcs->commit(ipp, task);
815	if (ret)
816		exynos_drm_ipp_task_done(task, ret);
817}
818
819static void exynos_drm_ipp_schedule_task(struct exynos_drm_ipp *ipp,
820					 struct exynos_drm_ipp_task *task)
821{
822	unsigned long flags;
823
824	spin_lock_irqsave(&ipp->lock, flags);
825	list_add(&task->head, &ipp->todo_list);
826	spin_unlock_irqrestore(&ipp->lock, flags);
827
828	exynos_drm_ipp_next_task(ipp);
829}
830
831static void exynos_drm_ipp_task_abort(struct exynos_drm_ipp *ipp,
832				      struct exynos_drm_ipp_task *task)
833{
834	unsigned long flags;
835
836	spin_lock_irqsave(&ipp->lock, flags);
837	if (task->flags & DRM_EXYNOS_IPP_TASK_DONE) {
838		/* already completed task */
839		exynos_drm_ipp_task_cleanup(task);
840	} else if (ipp->task != task) {
841		/* task has not been scheduled for execution yet */
842		list_del_init(&task->head);
843		exynos_drm_ipp_task_cleanup(task);
844	} else {
845		/*
846		 * currently processed task, call abort() and perform
847		 * cleanup with async worker
848		 */
849		task->flags |= DRM_EXYNOS_IPP_TASK_ASYNC;
850		spin_unlock_irqrestore(&ipp->lock, flags);
851		if (ipp->funcs->abort)
852			ipp->funcs->abort(ipp, task);
853		return;
854	}
855	spin_unlock_irqrestore(&ipp->lock, flags);
856}
857
858/**
859 * exynos_drm_ipp_commit_ioctl - perform image processing operation
860 * @dev: DRM device
861 * @data: ioctl data
862 * @file_priv: DRM file info
863 *
864 * Construct a ipp task from the set of properties provided from the user
865 * and try to schedule it to framebuffer processor hardware.
866 *
867 * Called by the user via ioctl.
868 *
869 * Returns:
870 * Zero on success, negative errno on failure.
871 */
872int exynos_drm_ipp_commit_ioctl(struct drm_device *dev, void *data,
873				struct drm_file *file_priv)
874{
875	struct drm_exynos_ioctl_ipp_commit *arg = data;
876	struct exynos_drm_ipp *ipp;
877	struct exynos_drm_ipp_task *task;
878	int ret = 0;
879
880	if ((arg->flags & ~DRM_EXYNOS_IPP_FLAGS) || arg->reserved)
881		return -EINVAL;
882
883	/* can't test and expect an event at the same time */
884	if ((arg->flags & DRM_EXYNOS_IPP_FLAG_TEST_ONLY) &&
885			(arg->flags & DRM_EXYNOS_IPP_FLAG_EVENT))
886		return -EINVAL;
887
888	ipp = __ipp_get(arg->ipp_id);
889	if (!ipp)
890		return -ENOENT;
891
892	task = exynos_drm_ipp_task_alloc(ipp);
893	if (!task)
894		return -ENOMEM;
895
896	ret = exynos_drm_ipp_task_set(task, arg);
897	if (ret)
898		goto free;
899
900	ret = exynos_drm_ipp_task_check(task);
901	if (ret)
902		goto free;
903
904	ret = exynos_drm_ipp_task_setup_buffers(task, file_priv);
905	if (ret || arg->flags & DRM_EXYNOS_IPP_FLAG_TEST_ONLY)
906		goto free;
907
908	if (arg->flags & DRM_EXYNOS_IPP_FLAG_EVENT) {
909		ret = exynos_drm_ipp_event_create(task, file_priv,
910						 arg->user_data);
911		if (ret)
912			goto free;
913	}
914
915	/*
916	 * Queue task for processing on the hardware. task object will be
917	 * then freed after exynos_drm_ipp_task_done()
918	 */
919	if (arg->flags & DRM_EXYNOS_IPP_FLAG_NONBLOCK) {
920		DRM_DEV_DEBUG_DRIVER(ipp->dev,
921				     "ipp: %d, nonblocking processing task %p\n",
922				     ipp->id, task);
923
924		task->flags |= DRM_EXYNOS_IPP_TASK_ASYNC;
925		exynos_drm_ipp_schedule_task(task->ipp, task);
926		ret = 0;
927	} else {
928		DRM_DEV_DEBUG_DRIVER(ipp->dev, "ipp: %d, processing task %p\n",
929				     ipp->id, task);
930		exynos_drm_ipp_schedule_task(ipp, task);
931		ret = wait_event_interruptible(ipp->done_wq,
932					task->flags & DRM_EXYNOS_IPP_TASK_DONE);
933		if (ret)
934			exynos_drm_ipp_task_abort(ipp, task);
935		else
936			ret = exynos_drm_ipp_task_cleanup(task);
937	}
938	return ret;
939free:
940	exynos_drm_ipp_task_free(ipp, task);
941
942	return ret;
943}
~/ Read-only file preview.