Skip to content

Commit 63fc535

Browse files
dlechjic23
authored andcommitted
iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros
Add new macros to help with the common case of declaring a buffer that is safe to use with iio_push_to_buffers_with_ts(). This is not trivial to do correctly because of the alignment requirements of the timestamp. This will make it easier for both authors and reviewers. To avoid double __align() attributes in cases where we also need DMA alignment, add a 2nd variant IIO_DECLARE_DMA_BUFFER_WITH_TS(). Reviewed-by: Nuno Sá <nuno.sa@analog.com> Signed-off-by: David Lechner <dlechner@baylibre.com> Link: https://patch.msgid.link/20250507-iio-introduce-iio_declare_buffer_with_ts-v6-2-4aee1b9f1b89@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent fa19c30 commit 63fc535

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

include/linux/iio/iio.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef _INDUSTRIAL_IO_H_
88
#define _INDUSTRIAL_IO_H_
99

10+
#include <linux/align.h>
1011
#include <linux/device.h>
1112
#include <linux/cdev.h>
1213
#include <linux/compiler_types.h>
@@ -784,6 +785,37 @@ static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)
784785
*/
785786
#define IIO_DMA_MINALIGN MAX(ARCH_DMA_MINALIGN, sizeof(s64))
786787

788+
#define __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \
789+
type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)]
790+
791+
/**
792+
* IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp
793+
* @type: element type of the buffer
794+
* @name: identifier name of the buffer
795+
* @count: number of elements in the buffer
796+
*
797+
* Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). In
798+
* addition to allocating enough space for @count elements of @type, it also
799+
* allocates space for a s64 timestamp at the end of the buffer and ensures
800+
* proper alignment of the timestamp.
801+
*/
802+
#define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \
803+
__IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64))
804+
805+
/**
806+
* IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp
807+
* @type: element type of the buffer
808+
* @name: identifier name of the buffer
809+
* @count: number of elements in the buffer
810+
*
811+
* Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN)
812+
* to ensure that the buffer doesn't share cachelines with anything that comes
813+
* before it in a struct. This should not be used for stack-allocated buffers
814+
* as stack memory cannot generally be used for DMA.
815+
*/
816+
#define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \
817+
__IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN)
818+
787819
struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv);
788820

789821
/* The information at the returned address is guaranteed to be cacheline aligned */

0 commit comments

Comments
 (0)