Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

David Lechner and committed by
Jonathan Cameron
63fc5352 fa19c303

+32
+32
include/linux/iio/iio.h
··· 7 7 #ifndef _INDUSTRIAL_IO_H_ 8 8 #define _INDUSTRIAL_IO_H_ 9 9 10 + #include <linux/align.h> 10 11 #include <linux/device.h> 11 12 #include <linux/cdev.h> 12 13 #include <linux/compiler_types.h> ··· 784 783 * 8 bytes instead. 785 784 */ 786 785 #define IIO_DMA_MINALIGN MAX(ARCH_DMA_MINALIGN, sizeof(s64)) 786 + 787 + #define __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 788 + type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)] 789 + 790 + /** 791 + * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp 792 + * @type: element type of the buffer 793 + * @name: identifier name of the buffer 794 + * @count: number of elements in the buffer 795 + * 796 + * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). In 797 + * addition to allocating enough space for @count elements of @type, it also 798 + * allocates space for a s64 timestamp at the end of the buffer and ensures 799 + * proper alignment of the timestamp. 800 + */ 801 + #define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 802 + __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) 803 + 804 + /** 805 + * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp 806 + * @type: element type of the buffer 807 + * @name: identifier name of the buffer 808 + * @count: number of elements in the buffer 809 + * 810 + * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN) 811 + * to ensure that the buffer doesn't share cachelines with anything that comes 812 + * before it in a struct. This should not be used for stack-allocated buffers 813 + * as stack memory cannot generally be used for DMA. 814 + */ 815 + #define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ 816 + __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN) 787 817 788 818 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv); 789 819