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