memalign.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. */
  5. #ifndef __ALIGNMEM_H
  6. #define __ALIGNMEM_H
  7. /*
  8. * ARCH_DMA_MINALIGN is defined in asm/cache.h for each architecture. It
  9. * is used to align DMA buffers.
  10. */
  11. #ifndef __ASSEMBLY__
  12. #include <asm/cache.h>
  13. #include <malloc.h>
  14. /*
  15. * The ALLOC_CACHE_ALIGN_BUFFER macro is used to allocate a buffer on the
  16. * stack that meets the minimum architecture alignment requirements for DMA.
  17. * Such a buffer is useful for DMA operations where flushing and invalidating
  18. * the cache before and after a read and/or write operation is required for
  19. * correct operations.
  20. *
  21. * When called the macro creates an array on the stack that is sized such
  22. * that:
  23. *
  24. * 1) The beginning of the array can be advanced enough to be aligned.
  25. *
  26. * 2) The size of the aligned portion of the array is a multiple of the minimum
  27. * architecture alignment required for DMA.
  28. *
  29. * 3) The aligned portion contains enough space for the original number of
  30. * elements requested.
  31. *
  32. * The macro then creates a pointer to the aligned portion of this array and
  33. * assigns to the pointer the address of the first element in the aligned
  34. * portion of the array.
  35. *
  36. * Calling the macro as:
  37. *
  38. * ALLOC_CACHE_ALIGN_BUFFER(uint32_t, buffer, 1024);
  39. *
  40. * Will result in something similar to saying:
  41. *
  42. * uint32_t buffer[1024];
  43. *
  44. * The following differences exist:
  45. *
  46. * 1) The resulting buffer is guaranteed to be aligned to the value of
  47. * ARCH_DMA_MINALIGN.
  48. *
  49. * 2) The buffer variable created by the macro is a pointer to the specified
  50. * type, and NOT an array of the specified type. This can be very important
  51. * if you want the address of the buffer, which you probably do, to pass it
  52. * to the DMA hardware. The value of &buffer is different in the two cases.
  53. * In the macro case it will be the address of the pointer, not the address
  54. * of the space reserved for the buffer. However, in the second case it
  55. * would be the address of the buffer. So if you are replacing hard coded
  56. * stack buffers with this macro you need to make sure you remove the & from
  57. * the locations where you are taking the address of the buffer.
  58. *
  59. * Note that the size parameter is the number of array elements to allocate,
  60. * not the number of bytes.
  61. *
  62. * This macro can not be used outside of function scope, or for the creation
  63. * of a function scoped static buffer. It can not be used to create a cache
  64. * line aligned global buffer.
  65. */
  66. #define PAD_COUNT(s, pad) (((s) - 1) / (pad) + 1)
  67. #define PAD_SIZE(s, pad) (PAD_COUNT(s, pad) * pad)
  68. #define ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, pad) \
  69. char __##name[ROUND(PAD_SIZE((size) * sizeof(type), pad), align) \
  70. + (align - 1)]; \
  71. \
  72. type *name = (type *)ALIGN((uintptr_t)__##name, align)
  73. #define ALLOC_ALIGN_BUFFER(type, name, size, align) \
  74. ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, 1)
  75. #define ALLOC_CACHE_ALIGN_BUFFER_PAD(type, name, size, pad) \
  76. ALLOC_ALIGN_BUFFER_PAD(type, name, size, ARCH_DMA_MINALIGN, pad)
  77. #define ALLOC_CACHE_ALIGN_BUFFER(type, name, size) \
  78. ALLOC_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN)
  79. /*
  80. * DEFINE_CACHE_ALIGN_BUFFER() is similar to ALLOC_CACHE_ALIGN_BUFFER, but it's
  81. * purpose is to allow allocating aligned buffers outside of function scope.
  82. * Usage of this macro shall be avoided or used with extreme care!
  83. */
  84. #define DEFINE_ALIGN_BUFFER(type, name, size, align) \
  85. static char __##name[ALIGN(size * sizeof(type), align)] \
  86. __aligned(align); \
  87. \
  88. static type *name = (type *)__##name
  89. #define DEFINE_CACHE_ALIGN_BUFFER(type, name, size) \
  90. DEFINE_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN)
  91. /**
  92. * malloc_cache_aligned() - allocate a memory region aligned to cache line size
  93. *
  94. * This allocates memory at a cache-line boundary. The amount allocated may
  95. * be larger than requested as it is rounded up to the nearest multiple of the
  96. * cache-line size. This ensured that subsequent cache operations on this
  97. * memory (flush, invalidate) will not affect subsequently allocated regions.
  98. *
  99. * @size: Minimum number of bytes to allocate
  100. *
  101. * @return pointer to new memory region, or NULL if there is no more memory
  102. * available.
  103. */
  104. static inline void *malloc_cache_aligned(size_t size)
  105. {
  106. return memalign(ARCH_DMA_MINALIGN, ALIGN(size, ARCH_DMA_MINALIGN));
  107. }
  108. #endif
  109. #endif /* __ALIGNMEM_H */