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