1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic bounce buffer implementation
5 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
11 #include <bouncebuf.h>
13 static int addr_aligned(struct bounce_buffer *state)
15 const ulong align_mask = ARCH_DMA_MINALIGN - 1;
17 /* Check if start is aligned */
18 if ((ulong)state->user_buffer & align_mask) {
19 debug("Unaligned buffer address %p\n", state->user_buffer);
23 /* Check if length is aligned */
24 if (state->len != state->len_aligned) {
25 debug("Unaligned buffer length %zu\n", state->len);
33 int bounce_buffer_start(struct bounce_buffer *state, void *data,
34 size_t len, unsigned int flags)
36 state->user_buffer = data;
37 state->bounce_buffer = data;
39 state->len_aligned = roundup(len, ARCH_DMA_MINALIGN);
42 if (!addr_aligned(state)) {
43 state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
45 if (!state->bounce_buffer)
48 if (state->flags & GEN_BB_READ)
49 memcpy(state->bounce_buffer, state->user_buffer,
54 * Flush data to RAM so DMA reads can pick it up,
55 * and any CPU writebacks don't race with DMA writes
57 flush_dcache_range((unsigned long)state->bounce_buffer,
58 (unsigned long)(state->bounce_buffer) +
64 int bounce_buffer_stop(struct bounce_buffer *state)
66 if (state->flags & GEN_BB_WRITE) {
67 /* Invalidate cache so that CPU can see any newly DMA'd data */
68 invalidate_dcache_range((unsigned long)state->bounce_buffer,
69 (unsigned long)(state->bounce_buffer) +
73 if (state->bounce_buffer == state->user_buffer)
76 if (state->flags & GEN_BB_WRITE)
77 memcpy(state->user_buffer, state->bounce_buffer, state->len);
79 free(state->bounce_buffer);