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