From 0f41950564b5a8e2bdf1649aff01fa30fcbdc417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mo=C5=84?= Date: Mon, 19 Aug 2024 10:47:54 +0200 Subject: [PATCH] usb: device_next: Respect granularity in UDC buf pool define MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round up buffer size used in UDC_BUF_POOL_DEFINE() to respect the required buffer granularity. The issue was observed with UAC2 explicit feedback data, but the problem applies to any UDC_BUF_POOL_DEFINE() use. In order for every buffer returned by UDC_BUF_POOL_DEFINE() to be both aligned and to have required granurality, it is required to allocate the buffers for ROUND_UP(size, LCM(UDC_BUF_GRANULARITY, UDC_BUF_ALIGN)). Because we do not have Least Common Multiple nor Greatest Common Divisor compile time macros, assume that granularity is multiple of alignment. Validate the assumption with a build time assert. When we get a target where this assumption fails we would have to come up with a solution to compute LCM and/or GCD at compile time. Signed-off-by: Tomasz Moń --- include/zephyr/drivers/usb/udc_buf.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/zephyr/drivers/usb/udc_buf.h b/include/zephyr/drivers/usb/udc_buf.h index aae4cf28fba..69b85da94db 100644 --- a/include/zephyr/drivers/usb/udc_buf.h +++ b/include/zephyr/drivers/usb/udc_buf.h @@ -132,15 +132,17 @@ extern const struct net_buf_data_cb net_buf_dma_cb; */ #define UDC_BUF_POOL_DEFINE(pname, count, size, ud_size, fdestroy) \ _NET_BUF_ARRAY_DEFINE(pname, count, ud_size); \ + BUILD_ASSERT((UDC_BUF_GRANULARITY) % (UDC_BUF_ALIGN) == 0, \ + "Code assumes granurality is multiple of alignment"); \ static uint8_t __nocache __aligned(UDC_BUF_ALIGN) \ - net_buf_data_##pname[count][size]; \ + net_buf_data_##pname[count][ROUND_UP(size, UDC_BUF_GRANULARITY)];\ static const struct net_buf_pool_fixed net_buf_fixed_##pname = { \ .data_pool = (uint8_t *)net_buf_data_##pname, \ }; \ static const struct net_buf_data_alloc net_buf_fixed_alloc_##pname = { \ .cb = &net_buf_fixed_cb, \ .alloc_data = (void *)&net_buf_fixed_##pname, \ - .max_alloc_size = size, \ + .max_alloc_size = ROUND_UP(size, UDC_BUF_GRANULARITY), \ }; \ static STRUCT_SECTION_ITERABLE(net_buf_pool, pname) = \ NET_BUF_POOL_INITIALIZER(pname, &net_buf_fixed_alloc_##pname, \