2 * Generic bounce buffer implementation
4 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <bouncebuf.h>
14 static int addr_aligned(struct bounce_buffer *state)
16 const ulong align_mask = ARCH_DMA_MINALIGN - 1;
18 /* Check if start is aligned */
19 if ((ulong)state->user_buffer & align_mask) {
20 debug("Unaligned buffer address %p\n", state->user_buffer);
24 /* Check if length is aligned */
25 if (state->len != state->len_aligned) {
26 debug("Unaligned buffer length %d\n", state->len);
34 int bounce_buffer_start(struct bounce_buffer *state, void *data,
35 size_t len, unsigned int flags)
37 state->user_buffer = data;
38 state->bounce_buffer = data;
40 state->len_aligned = roundup(len, ARCH_DMA_MINALIGN);
43 if (!addr_aligned(state)) {
44 state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
46 if (!state->bounce_buffer)
49 if (state->flags & GEN_BB_READ)
50 memcpy(state->bounce_buffer, state->user_buffer,
55 * Flush data to RAM so DMA reads can pick it up,
56 * and any CPU writebacks don't race with DMA writes
58 flush_dcache_range((unsigned long)state->bounce_buffer,
59 (unsigned long)(state->bounce_buffer) +
65 int bounce_buffer_stop(struct bounce_buffer *state)
67 if (state->flags & GEN_BB_WRITE) {
68 /* Invalidate cache so that CPU can see any newly DMA'd data */
69 invalidate_dcache_range((unsigned long)state->bounce_buffer,
70 (unsigned long)(state->bounce_buffer) +
74 if (state->bounce_buffer == state->user_buffer)
77 if (state->flags & GEN_BB_WRITE)
78 memcpy(state->user_buffer, state->bounce_buffer, state->len);
80 free(state->bounce_buffer);