1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Generic bounce buffer implementation 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Copyright (C) 2012 Marek Vasut <marex@denx.de> 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #ifndef __INCLUDE_BOUNCEBUF_H__ 10*4882a593Smuzhiyun #define __INCLUDE_BOUNCEBUF_H__ 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun #include <linux/types.h> 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun /* 15*4882a593Smuzhiyun * GEN_BB_READ -- Data are read from the buffer eg. by DMA hardware. 16*4882a593Smuzhiyun * The source buffer is copied into the bounce buffer (if unaligned, otherwise 17*4882a593Smuzhiyun * the source buffer is used directly) upon start() call, then the operation 18*4882a593Smuzhiyun * requiring the aligned transfer happens, then the bounce buffer is lost upon 19*4882a593Smuzhiyun * stop() call. 20*4882a593Smuzhiyun */ 21*4882a593Smuzhiyun #define GEN_BB_READ (1 << 0) 22*4882a593Smuzhiyun /* 23*4882a593Smuzhiyun * GEN_BB_WRITE -- Data are written into the buffer eg. by DMA hardware. 24*4882a593Smuzhiyun * The source buffer starts in an undefined state upon start() call, then the 25*4882a593Smuzhiyun * operation requiring the aligned transfer happens, then the bounce buffer is 26*4882a593Smuzhiyun * copied into the destination buffer (if unaligned, otherwise destination 27*4882a593Smuzhiyun * buffer is used directly) upon stop() call. 28*4882a593Smuzhiyun */ 29*4882a593Smuzhiyun #define GEN_BB_WRITE (1 << 1) 30*4882a593Smuzhiyun /* 31*4882a593Smuzhiyun * GEN_BB_RW -- Data are read and written into the buffer eg. by DMA hardware. 32*4882a593Smuzhiyun * The source buffer is copied into the bounce buffer (if unaligned, otherwise 33*4882a593Smuzhiyun * the source buffer is used directly) upon start() call, then the operation 34*4882a593Smuzhiyun * requiring the aligned transfer happens, then the bounce buffer is copied 35*4882a593Smuzhiyun * into the destination buffer (if unaligned, otherwise destination buffer is 36*4882a593Smuzhiyun * used directly) upon stop() call. 37*4882a593Smuzhiyun */ 38*4882a593Smuzhiyun #define GEN_BB_RW (GEN_BB_READ | GEN_BB_WRITE) 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun struct bounce_buffer { 41*4882a593Smuzhiyun /* Copy of data parameter passed to start() */ 42*4882a593Smuzhiyun void *user_buffer; 43*4882a593Smuzhiyun /* 44*4882a593Smuzhiyun * DMA-aligned buffer. This field is always set to the value that 45*4882a593Smuzhiyun * should be used for DMA; either equal to .user_buffer, or to a 46*4882a593Smuzhiyun * freshly allocated aligned buffer. 47*4882a593Smuzhiyun */ 48*4882a593Smuzhiyun void *bounce_buffer; 49*4882a593Smuzhiyun /* Copy of len parameter passed to start() */ 50*4882a593Smuzhiyun size_t len; 51*4882a593Smuzhiyun /* DMA-aligned buffer length */ 52*4882a593Smuzhiyun size_t len_aligned; 53*4882a593Smuzhiyun /* Copy of flags parameter passed to start() */ 54*4882a593Smuzhiyun unsigned int flags; 55*4882a593Smuzhiyun }; 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun /** 58*4882a593Smuzhiyun * bounce_buffer_start() -- Start the bounce buffer session 59*4882a593Smuzhiyun * state: stores state passed between bounce_buffer_{start,stop} 60*4882a593Smuzhiyun * data: pointer to buffer to be aligned 61*4882a593Smuzhiyun * len: length of the buffer 62*4882a593Smuzhiyun * flags: flags describing the transaction, see above. 63*4882a593Smuzhiyun */ 64*4882a593Smuzhiyun int bounce_buffer_start(struct bounce_buffer *state, void *data, 65*4882a593Smuzhiyun size_t len, unsigned int flags); 66*4882a593Smuzhiyun /** 67*4882a593Smuzhiyun * bounce_buffer_stop() -- Finish the bounce buffer session 68*4882a593Smuzhiyun * state: stores state passed between bounce_buffer_{start,stop} 69*4882a593Smuzhiyun */ 70*4882a593Smuzhiyun int bounce_buffer_stop(struct bounce_buffer *state); 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun #endif 73