dma-fence-unwrap.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2022 Advanced Micro Devices, Inc.
  4. * Authors:
  5. * Christian König <[email protected]>
  6. */
  7. #ifndef __LINUX_DMA_FENCE_UNWRAP_H
  8. #define __LINUX_DMA_FENCE_UNWRAP_H
  9. struct dma_fence;
  10. /**
  11. * struct dma_fence_unwrap - cursor into the container structure
  12. *
  13. * Should be used with dma_fence_unwrap_for_each() iterator macro.
  14. */
  15. struct dma_fence_unwrap {
  16. /**
  17. * @chain: potential dma_fence_chain, but can be other fence as well
  18. */
  19. struct dma_fence *chain;
  20. /**
  21. * @array: potential dma_fence_array, but can be other fence as well
  22. */
  23. struct dma_fence *array;
  24. /**
  25. * @index: last returned index if @array is really a dma_fence_array
  26. */
  27. unsigned int index;
  28. };
  29. struct dma_fence *dma_fence_unwrap_first(struct dma_fence *head,
  30. struct dma_fence_unwrap *cursor);
  31. struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor);
  32. /**
  33. * dma_fence_unwrap_for_each - iterate over all fences in containers
  34. * @fence: current fence
  35. * @cursor: current position inside the containers
  36. * @head: starting point for the iterator
  37. *
  38. * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all
  39. * potential fences in them. If @head is just a normal fence only that one is
  40. * returned.
  41. */
  42. #define dma_fence_unwrap_for_each(fence, cursor, head) \
  43. for (fence = dma_fence_unwrap_first(head, cursor); fence; \
  44. fence = dma_fence_unwrap_next(cursor))
  45. struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
  46. struct dma_fence **fences,
  47. struct dma_fence_unwrap *cursors);
  48. /**
  49. * dma_fence_unwrap_merge - unwrap and merge fences
  50. *
  51. * All fences given as parameters are unwrapped and merged back together as flat
  52. * dma_fence_array. Useful if multiple containers need to be merged together.
  53. *
  54. * Implemented as a macro to allocate the necessary arrays on the stack and
  55. * account the stack frame size to the caller.
  56. *
  57. * Returns NULL on memory allocation failure, a dma_fence object representing
  58. * all the given fences otherwise.
  59. */
  60. #define dma_fence_unwrap_merge(...) \
  61. ({ \
  62. struct dma_fence *__f[] = { __VA_ARGS__ }; \
  63. struct dma_fence_unwrap __c[ARRAY_SIZE(__f)]; \
  64. \
  65. __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c); \
  66. })
  67. #endif