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