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