blob: 3e702fad2905e38fb636edc53ed9e520314775e3 [file] [log] [blame]
Lee Leahyeef40eb2017-03-23 10:54:57 -07001/*
Martin Roth0443ac22019-08-30 21:29:41 -06002 * This file is part of the coreboot project.
Lee Leahyeef40eb2017-03-23 10:54:57 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Martin Roth0443ac22019-08-30 21:29:41 -060013 *
14 * Generic bounce buffer implementation
Lee Leahyeef40eb2017-03-23 10:54:57 -070015 */
16
Lee Leahy48dbc662017-05-08 16:56:03 -070017#ifndef __COMMONLIB_STORAGE_BOUNCEBUF_H__
18#define __COMMONLIB_STORAGE_BOUNCEBUF_H__
Lee Leahyeef40eb2017-03-23 10:54:57 -070019
20#include <stddef.h>
21#include <stdint.h>
22#include <stdlib.h>
23
24/*
25 * GEN_BB_READ -- Data are read from the buffer eg. by DMA hardware.
26 * The source buffer is copied into the bounce buffer (if unaligned, otherwise
27 * the source buffer is used directly) upon start() call, then the operation
28 * requiring the aligned transfer happens, then the bounce buffer is lost upon
29 * stop() call.
30 */
31#define GEN_BB_READ (1 << 0)
32/*
33 * GEN_BB_WRITE -- Data are written into the buffer eg. by DMA hardware.
34 * The source buffer starts in an undefined state upon start() call, then the
35 * operation requiring the aligned transfer happens, then the bounce buffer is
36 * copied into the destination buffer (if unaligned, otherwise destination
37 * buffer is used directly) upon stop() call.
38 */
39#define GEN_BB_WRITE (1 << 1)
40/*
41 * GEN_BB_RW -- Data are read and written into the buffer eg. by DMA hardware.
42 * The source buffer is copied into the bounce buffer (if unaligned, otherwise
43 * the source buffer is used directly) upon start() call, then the operation
44 * requiring the aligned transfer happens, then the bounce buffer is copied
45 * into the destination buffer (if unaligned, otherwise destination buffer is
46 * used directly) upon stop() call.
47 */
48#define GEN_BB_RW (GEN_BB_READ | GEN_BB_WRITE)
49
50struct bounce_buffer {
51 /* Copy of data parameter passed to start() */
52 void *user_buffer;
53 /*
54 * DMA-aligned buffer. This field is always set to the value that
55 * should be used for DMA; either equal to .user_buffer, or to a
56 * freshly allocated aligned buffer.
57 */
58 void *bounce_buffer;
59 /* Copy of len parameter passed to start() */
60 size_t len;
61 /* DMA-aligned buffer length */
62 size_t len_aligned;
63 /* Copy of flags parameter passed to start() */
64 unsigned int flags;
65};
66
67/**
68 * bounce_buffer_start() -- Start the bounce buffer session
69 * state: stores state passed between bounce_buffer_{start,stop}
70 * data: pointer to buffer to be aligned
71 * len: length of the buffer
72 * flags: flags describing the transaction, see above.
73 */
74int bounce_buffer_start(struct bounce_buffer *state, void *data,
75 size_t len, unsigned int flags);
76/**
77 * bounce_buffer_stop() -- Finish the bounce buffer session
78 * state: stores state passed between bounce_buffer_{start,stop}
79 */
80int bounce_buffer_stop(struct bounce_buffer *state);
81
82// TODO(hungte) Eliminate the alignment stuff below and replace them with a
83// better and centralized way to handler non-cache/aligned memory.
84// Helper macros for alignment.
85#define DMA_MINALIGN (64)
86#define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
87#define ALLOC_CACHE_ALIGN_BUFFER(type, name, size) \
88 char __##name[ROUND(size * sizeof(type), DMA_MINALIGN) + \
89 DMA_MINALIGN - 1]; \
Felix Held8d2ec862019-06-20 14:26:09 +020090 type *name = (type *) ALIGN_UP((uintptr_t)__##name, DMA_MINALIGN)
Lee Leahyeef40eb2017-03-23 10:54:57 -070091#ifndef ARCH_DMA_MINALIGN
92#define ARCH_DMA_MINALIGN (DMA_MINALIGN)
93#endif
94
Lee Leahy48dbc662017-05-08 16:56:03 -070095#endif // __COMMONLIB_STORAGE_BOUNCEBUF_H__