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