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