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> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame^] | 13 | #include <asm/cache.h> |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 14 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 15 | static int addr_aligned(struct bounce_buffer *state) |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 16 | { |
| 17 | const ulong align_mask = ARCH_DMA_MINALIGN - 1; |
| 18 | |
| 19 | /* Check if start is aligned */ |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 20 | if ((ulong)state->user_buffer & align_mask) { |
| 21 | debug("Unaligned buffer address %p\n", state->user_buffer); |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 25 | /* Check if length is aligned */ |
| 26 | if (state->len != state->len_aligned) { |
Vasili Galka | 38c3fff | 2014-08-26 13:45:48 +0300 | [diff] [blame] | 27 | debug("Unaligned buffer length %zu\n", state->len); |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | /* Aligned */ |
| 32 | return 1; |
| 33 | } |
| 34 | |
Marek Vasut | 393de78 | 2020-04-04 12:45:02 +0200 | [diff] [blame] | 35 | int bounce_buffer_start_extalign(struct bounce_buffer *state, void *data, |
| 36 | size_t len, unsigned int flags, |
| 37 | size_t alignment, |
| 38 | int (*addr_is_aligned)(struct bounce_buffer *state)) |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 39 | { |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 40 | state->user_buffer = data; |
| 41 | state->bounce_buffer = data; |
| 42 | state->len = len; |
Marek Vasut | 393de78 | 2020-04-04 12:45:02 +0200 | [diff] [blame] | 43 | state->len_aligned = roundup(len, alignment); |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 44 | state->flags = flags; |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 45 | |
Marek Vasut | 393de78 | 2020-04-04 12:45:02 +0200 | [diff] [blame] | 46 | if (!addr_is_aligned(state)) { |
| 47 | state->bounce_buffer = memalign(alignment, |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 48 | state->len_aligned); |
| 49 | if (!state->bounce_buffer) |
| 50 | return -ENOMEM; |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 51 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 52 | if (state->flags & GEN_BB_READ) |
| 53 | memcpy(state->bounce_buffer, state->user_buffer, |
| 54 | state->len); |
| 55 | } |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 56 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Flush data to RAM so DMA reads can pick it up, |
| 59 | * and any CPU writebacks don't race with DMA writes |
| 60 | */ |
| 61 | flush_dcache_range((unsigned long)state->bounce_buffer, |
| 62 | (unsigned long)(state->bounce_buffer) + |
| 63 | state->len_aligned); |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Marek Vasut | 393de78 | 2020-04-04 12:45:02 +0200 | [diff] [blame] | 68 | int bounce_buffer_start(struct bounce_buffer *state, void *data, |
| 69 | size_t len, unsigned int flags) |
| 70 | { |
| 71 | return bounce_buffer_start_extalign(state, data, len, flags, |
| 72 | ARCH_DMA_MINALIGN, |
| 73 | addr_aligned); |
| 74 | } |
| 75 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 76 | int bounce_buffer_stop(struct bounce_buffer *state) |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 77 | { |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 78 | if (state->flags & GEN_BB_WRITE) { |
| 79 | /* Invalidate cache so that CPU can see any newly DMA'd data */ |
| 80 | invalidate_dcache_range((unsigned long)state->bounce_buffer, |
| 81 | (unsigned long)(state->bounce_buffer) + |
| 82 | state->len_aligned); |
| 83 | } |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 84 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 85 | if (state->bounce_buffer == state->user_buffer) |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 88 | if (state->flags & GEN_BB_WRITE) |
| 89 | memcpy(state->user_buffer, state->bounce_buffer, state->len); |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 90 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 91 | free(state->bounce_buffer); |
Marek Vasut | ae723e1 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |