Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Generic bounce buffer implementation |
| 4 | * |
| 5 | * Copyright (C) 2012 Marek Vasut <marex@denx.de> |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <errno.h> |
| 11 | #include <bouncebuf.h> |
| 12 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 13 | static int addr_aligned(struct bounce_buffer *state) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 14 | { |
| 15 | const ulong align_mask = ARCH_DMA_MINALIGN - 1; |
| 16 | |
| 17 | /* Check if start is aligned */ |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 18 | if ((ulong)state->user_buffer & align_mask) { |
| 19 | debug("Unaligned buffer address %p\n", state->user_buffer); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 20 | return 0; |
| 21 | } |
| 22 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 23 | /* Check if length is aligned */ |
| 24 | if (state->len != state->len_aligned) { |
Vasili Galka | 5d69a5d | 2014-08-26 13:45:48 +0300 | [diff] [blame] | 25 | debug("Unaligned buffer length %zu\n", state->len); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | /* Aligned */ |
| 30 | return 1; |
| 31 | } |
| 32 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 33 | int bounce_buffer_start(struct bounce_buffer *state, void *data, |
| 34 | size_t len, unsigned int flags) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 35 | { |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 36 | state->user_buffer = data; |
| 37 | state->bounce_buffer = data; |
| 38 | state->len = len; |
| 39 | state->len_aligned = roundup(len, ARCH_DMA_MINALIGN); |
| 40 | state->flags = flags; |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 41 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 42 | if (!addr_aligned(state)) { |
| 43 | state->bounce_buffer = memalign(ARCH_DMA_MINALIGN, |
| 44 | state->len_aligned); |
| 45 | if (!state->bounce_buffer) |
| 46 | return -ENOMEM; |
| 47 | |
| 48 | if (state->flags & GEN_BB_READ) |
| 49 | memcpy(state->bounce_buffer, state->user_buffer, |
| 50 | state->len); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 53 | /* |
| 54 | * Flush data to RAM so DMA reads can pick it up, |
| 55 | * and any CPU writebacks don't race with DMA writes |
| 56 | */ |
| 57 | flush_dcache_range((unsigned long)state->bounce_buffer, |
| 58 | (unsigned long)(state->bounce_buffer) + |
| 59 | state->len_aligned); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 64 | int bounce_buffer_stop(struct bounce_buffer *state) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 65 | { |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 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) + |
| 70 | state->len_aligned); |
| 71 | } |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 72 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 73 | if (state->bounce_buffer == state->user_buffer) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 76 | if (state->flags & GEN_BB_WRITE) |
| 77 | memcpy(state->user_buffer, state->bounce_buffer, state->len); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 78 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 79 | free(state->bounce_buffer); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |