xref: /openbmc/u-boot/common/bouncebuf.c (revision dcfe8638)
1 /*
2  * Generic bounce buffer implementation
3  *
4  * Copyright (C) 2012 Marek Vasut <marex@denx.de>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24 
25 #include <common.h>
26 #include <malloc.h>
27 #include <errno.h>
28 #include <bouncebuf.h>
29 
30 static int addr_aligned(struct bounce_buffer *state)
31 {
32 	const ulong align_mask = ARCH_DMA_MINALIGN - 1;
33 
34 	/* Check if start is aligned */
35 	if ((ulong)state->user_buffer & align_mask) {
36 		debug("Unaligned buffer address %p\n", state->user_buffer);
37 		return 0;
38 	}
39 
40 	/* Check if length is aligned */
41 	if (state->len != state->len_aligned) {
42 		debug("Unaligned buffer length %d\n", state->len);
43 		return 0;
44 	}
45 
46 	/* Aligned */
47 	return 1;
48 }
49 
50 int bounce_buffer_start(struct bounce_buffer *state, void *data,
51 			size_t len, unsigned int flags)
52 {
53 	state->user_buffer = data;
54 	state->bounce_buffer = data;
55 	state->len = len;
56 	state->len_aligned = roundup(len, ARCH_DMA_MINALIGN);
57 	state->flags = flags;
58 
59 	if (!addr_aligned(state)) {
60 		state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
61 						state->len_aligned);
62 		if (!state->bounce_buffer)
63 			return -ENOMEM;
64 
65 		if (state->flags & GEN_BB_READ)
66 			memcpy(state->bounce_buffer, state->user_buffer,
67 				state->len);
68 	}
69 
70 	/*
71 	 * Flush data to RAM so DMA reads can pick it up,
72 	 * and any CPU writebacks don't race with DMA writes
73 	 */
74 	flush_dcache_range((unsigned long)state->bounce_buffer,
75 				(unsigned long)(state->bounce_buffer) +
76 					state->len_aligned);
77 
78 	return 0;
79 }
80 
81 int bounce_buffer_stop(struct bounce_buffer *state)
82 {
83 	if (state->flags & GEN_BB_WRITE) {
84 		/* Invalidate cache so that CPU can see any newly DMA'd data */
85 		invalidate_dcache_range((unsigned long)state->bounce_buffer,
86 					(unsigned long)(state->bounce_buffer) +
87 						state->len_aligned);
88 	}
89 
90 	if (state->bounce_buffer == state->user_buffer)
91 		return 0;
92 
93 	if (state->flags & GEN_BB_WRITE)
94 		memcpy(state->user_buffer, state->bounce_buffer, state->len);
95 
96 	free(state->bounce_buffer);
97 
98 	return 0;
99 }
100