Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Ahmad Byagowi
Thu, 12 Nov 2026 16:33
[PATCH net-next v2 2/3] i2c: xiic: add KUnit coverage for register access modes
Author: Ahmad Byagowi
Date: Thu, 12 Nov 2026 16:33
Date: Thu, 12 Nov 2026 16:33
175 lines
17555 bytes
17555 bytes
Exercise the driver's private register accessors with memory-backed
registers instead of physical hardware. Cover native byte/halfword and
forced 32-bit accesses in both register byte orders, across 22 register
offsets. Check payload truncation, high-bit values and neighboring
sentinels. These tests do not validate physical bus transaction widths
or hardware interrupt behavior.
Include the tests in the driver translation unit so no accessors need
to become exported interfaces. Require built-in KUnit to avoid a
built-in driver depending on a modular test framework. The tests follow
the driver into either its built-in image or module.
The four parameterized cases passed in an isolated x86-64 QEMU boot
with networking disabled and no physical-device passthrough.
Tool-origin disclosure, as required by generated-content.rst: Codex was
used to implement this test suite, run the KUnit build/boot and draft
this changelog, in response to a request for upstream-quality testing.
Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
---
MAINTAINERS | 1 +
drivers/i2c/busses/Kconfig | 14 ++++
drivers/i2c/busses/i2c-xiic.c | 4 +
drivers/i2c/busses/tests/i2c_xiic_kunit.c | 91 +++++++++++++++++++++++
4 files changed, 110 insertions(+)
create mode 100644 drivers/i2c/busses/tests/i2c_xiic_kunit.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 893b0670a..64fa74c40 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4018,6 +4018,7 @@ F: drivers/cpuidle/cpuidle-zynq.c
F: drivers/edac/synopsys_edac.c
F: drivers/i2c/busses/i2c-cadence.c
F: drivers/i2c/busses/i2c-xiic.c
+F: drivers/i2c/busses/tests/i2c_xiic_kunit.c
F: drivers/mmc/host/sdhci-of-arasan.c
N: zynq
N: xilinx
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 275aaa7f3..014281fa2 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -1322,6 +1322,20 @@ config I2C_XILINX
This driver can also be built as a module. If so, the module
will be called xilinx_i2c.
+config I2C_XILINX_KUNIT_TEST
+ bool "KUnit tests for Xilinx I2C register accesses" if !KUNIT_ALL_TESTS
+ depends on I2C_XILINX && KUNIT=y
+ default KUNIT_ALL_TESTS
+ help
+ Test the Xilinx I2C driver's register accessors with both byte orders
+ and with native-width or forced 32-bit accesses. KUnit is a kernel
+ unit testing framework. These tests use memory-backed registers and
+ do not require an I2C controller.
+
+ The tests are compiled into the driver to exercise its private
+ accessors, and follow the driver's built-in or module configuration.
+ If unsure, say N.
+
config I2C_XLP9XX
tristate "Cavium ThunderX2 I2C support"
depends on ARCH_THUNDER2 || COMPILE_TEST
diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index 8425a540f..6c0f04aaa 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -1650,3 +1650,7 @@ MODULE_ALIAS("platform:" DRIVER_NAME);
MODULE_AUTHOR("info@mocean-labs.com");
MODULE_DESCRIPTION("Xilinx I2C bus driver");
MODULE_LICENSE("GPL v2");
+
+#if IS_ENABLED(CONFIG_I2C_XILINX_KUNIT_TEST)
+#include "tests/i2c_xiic_kunit.c"
+#endif
diff --git a/drivers/i2c/busses/tests/i2c_xiic_kunit.c b/drivers/i2c/busses/tests/i2c_xiic_kunit.c
new file mode 100644
index 000000000..b4c818d8b
--- /dev/null
+++ b/drivers/i2c/busses/tests/i2c_xiic_kunit.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Included by i2c-xiic.c to exercise the driver's private accessors. */
+#include <kunit/test.h>
+
+struct xiic_access_param {
+ const char *name;
+ enum xiic_endian endianness;
+ bool force_32bit_access;
+};
+
+static const struct xiic_access_param xiic_access_params[] = {
+ { "native_little_endian", LITTLE, false },
+ { "native_big_endian", BIG, false },
+ { "word_little_endian", LITTLE, true },
+ { "word_big_endian", BIG, true },
+};
+
+KUNIT_ARRAY_PARAM_DESC(xiic_access, xiic_access_params, name);
+
+static void xiic_register_access(struct kunit *test)
+{
+ static const int offsets[] = {
+ XIIC_DGIER_OFFSET, XIIC_IISR_OFFSET, XIIC_IIER_OFFSET,
+ XIIC_RESETR_OFFSET, XIIC_CR_REG_OFFSET, XIIC_SR_REG_OFFSET,
+ XIIC_DTR_REG_OFFSET, XIIC_DRR_REG_OFFSET, XIIC_ADR_REG_OFFSET,
+ XIIC_TFO_REG_OFFSET, XIIC_RFO_REG_OFFSET, XIIC_TBA_REG_OFFSET,
+ XIIC_RFD_REG_OFFSET, XIIC_GPO_REG_OFFSET,
+ XIIC_TSUSTA_REG_OFFSET, XIIC_TSUSTO_REG_OFFSET,
+ XIIC_THDSTA_REG_OFFSET,
+ XIIC_TSUDAT_REG_OFFSET, XIIC_TBUF_REG_OFFSET,
+ XIIC_THIGH_REG_OFFSET, XIIC_TLOW_REG_OFFSET,
+ XIIC_THDDAT_REG_OFFSET,
+ };
+ const struct xiic_access_param *param = test->param_value;
+ struct xiic_i2c *i2c;
+ u32 expected;
+ u8 *mem;
+ size_t i;
+ int reg;
+
+ i2c = kunit_kzalloc(test, sizeof(*i2c), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, i2c);
+ mem = kunit_kmalloc(test, 0x200, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, mem);
+ /* Aligned RAM stands in for MMIO. No hardware is accessed. */
+ i2c->base = (void __force __iomem *)(mem + sizeof(u32));
+ i2c->endianness = param->endianness;
+ i2c->force_32bit_access = param->force_32bit_access;
+
+ for (i = 0; i < ARRAY_SIZE(offsets); i++) {
+ reg = offsets[i];
+ memset(mem, 0xa5, 0x200);
+
+ xiic_setreg8(i2c, reg, 0xc0);
+ expected = param->force_32bit_access ? 0xc0 : 0xa5a5a5c0;
+ KUNIT_EXPECT_EQ(test, (u32)xiic_getreg32(i2c, reg), expected);
+ KUNIT_EXPECT_EQ(test, xiic_getreg8(i2c, reg), (u8)0xc0);
+
+ xiic_setreg16(i2c, reg, 0x03ff);
+ expected = param->force_32bit_access ? 0x03ff : 0xa5a503ff;
+ KUNIT_EXPECT_EQ(test, (u32)xiic_getreg32(i2c, reg), expected);
+ KUNIT_EXPECT_EQ(test, xiic_getreg8(i2c, reg), (u8)0xff);
+
+ xiic_setreg32(i2c, reg, 0x12345678);
+ KUNIT_EXPECT_EQ(test, (u32)xiic_getreg32(i2c, reg),
+ 0x12345678U);
+ KUNIT_EXPECT_EQ(test, xiic_getreg8(i2c, reg), (u8)0x78);
+ KUNIT_EXPECT_EQ(test, mem[reg + sizeof(u32)],
+ (u8)(param->endianness == BIG ? 0x12 : 0x78));
+ KUNIT_EXPECT_EQ(test, mem[reg + sizeof(u32) + 3],
+ (u8)(param->endianness == BIG ? 0x78 : 0x12));
+ xiic_setreg32(i2c, reg, 0x89abcdefU);
+ KUNIT_EXPECT_EQ(test, (u32)xiic_getreg32(i2c, reg),
+ 0x89abcdefU);
+ KUNIT_EXPECT_EQ(test, xiic_getreg8(i2c, reg), (u8)0xef);
+ KUNIT_EXPECT_EQ(test, mem[reg + sizeof(u32) - 1], (u8)0xa5);
+ KUNIT_EXPECT_EQ(test, mem[reg + 2 * sizeof(u32)], (u8)0xa5);
+ }
+}
+
+static struct kunit_case xiic_access_test_cases[] = {
+ KUNIT_CASE_PARAM(xiic_register_access, xiic_access_gen_params),
+ {}
+};
+
+static struct kunit_suite xiic_access_test_suite = {
+ .name = "i2c_xiic",
+ .test_cases = xiic_access_test_cases,
+};
+
+kunit_test_suite(xiic_access_test_suite);
Re: [PATCH net-next v2 2/3] i2c: xiic: add KUnit coverage for register access modes
Author: netdev-bot+sashiko
Date: Mon, 16 Nov 2026 16:33
Date: Mon, 16 Nov 2026 16:33
137 lines
13265 bytes
13265 bytes
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The new KUnit test in
drivers/i2c/busses/tests/i2c_xiic_kunit.c (xiic_register_access(),…
--
i2c: xiic: add KUnit coverage for register access modes
This commit adds a KUnit suite that runs the xiic driver's private
xiic_setreg*() and xiic_getreg*() accessors against kmalloc-backed memory
instead of MMIO. It covers both byte orders, with native-width and forced
32-bit access. The test file is included directly into i2c-xiic.c and is
enabled by a new I2C_XILINX_KUNIT_TEST bool option that requires built-in
KUnit.
> diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
> index 275aaa7f35833..014281fa2be5c 100644
> --- a/drivers/i2c/busses/Kconfig
> +++ b/drivers/i2c/busses/Kconfig
> @@ -1322,6 +1322,20 @@ config I2C_XILINX
> This driver can also be built as a module. If so, the module
> will be called xilinx_i2c.
>
> +config I2C_XILINX_KUNIT_TEST
> + bool "KUnit tests for Xilinx I2C register accesses" if !KUNIT_ALL_TESTS
> + depends on I2C_XILINX && KUNIT=y
> + default KUNIT_ALL_TESTS
> + help
> + Test the Xilinx I2C driver's register accessors with both byte orders
> + and with native-width or forced 32-bit accesses. KUnit is a kernel
> + unit testing framework. These tests use memory-backed registers and
> + do not require an I2C controller.
[ ... ]
> diff --git a/drivers/i2c/busses/tests/i2c_xiic_kunit.c b/drivers/i2c/busses/tests/i2c_xiic_kunit.c
> new file mode 100644
> index 0000000000000..b4c818d8bac78
> --- /dev/null
> +++ b/drivers/i2c/busses/tests/i2c_xiic_kunit.c
[ ... ]
> +static void xiic_register_access(struct kunit *test)
> +{
[ ... ]
> + i2c = kunit_kzalloc(test, sizeof(*i2c), GFP_KERNEL);
> + KUNIT_ASSERT_NOT_NULL(test, i2c);
> + mem = kunit_kmalloc(test, 0x200, GFP_KERNEL);
> + KUNIT_ASSERT_NOT_NULL(test, mem);
> + /* Aligned RAM stands in for MMIO. No hardware is accessed. */
> + i2c->base = (void __force __iomem *)(mem + sizeof(u32));
[Severity: Medium]
Is it safe to push kmalloc memory through ioread*() and iowrite*() here?
xiic_setreg32(), xiic_getreg32() and the 8/16-bit helpers call
iowrite32(), ioread32be() and similar directly on i2c->base. Those
accessors are only required to work on cookies returned by ioremap() or
pci_iomap().
On x86-64 this works, because the generic iomap code treats these
addresses as plain MMIO. But I2C_XILINX_KUNIT_TEST only depends on
I2C_XILINX && KUNIT=y and defaults to KUNIT_ALL_TESTS, and I2C_XILINX only
needs HAS_IOMEM. As a result, the option can be enabled on architectures
where RAM can't be accessed this way.
On UML, tools/testing/kunit/configs/arch_uml.config sets
CONFIG_KUNIT_UML_PCI=y. That pulls in UML_PCI -> UML_IOMEM_EMULATION ->
INDIRECT_IOMEM and enables HAS_IOMEM. The accessors then end up in
lib/logic_iomem.c:
lib/logic_iomem.c:get_area() {
...
if (WARN_ON((a & IOREMAP_MASK) != IOREMAP_BIAS))
return NULL;
...
}
INDIRECT_IOMEM_FALLBACK is never selected, so the fallback
real_raw_read*() and real_raw_write*() helpers WARN a second time, return
~0 and drop the writes.
Wouldn't that mean two WARNs per accessor call? Across 22 offsets and 4
parameters that is about 2100 WARNs per run. Every expectation would fail,
and with panic_on_warn=1 the kernel would panic. A plain kunit.py run with
--kconfig_add CONFIG_I2C_XILINX=y looks like it would hit this.
On s390 with PCI, __raw_readl() and __raw_writel() map to zpci_read_u32()
and zpci_write_u32(). The test would then issue real zPCI load and store
instructions on a heap address. Without MIO, the address is decoded as an
iomap index plus a PCI offset:
arch/s390/pci/pci_insn.c:zpci_load_fh() {
...
struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)];
u64 req = ZPCI_CREATE_REQ(READ_ONCE(entry->fh), entry->bar, len);
return __zpci_load(data, req, ZPCI_OFFSET(addr));
}
Could this fail with zpci errors, or reach an unrelated function's BAR if
an iomap entry happens to match?
On sparc64, __raw_readl() in arch/sparc/include/asm/io_64.h uses lduwa
with ASI_PHYS_BYPASS_EC_E. That treats the kernel virtual pointer as a
physical address and bypasses the caches.
Because of this, the "No hardware is accessed" comment, the
"memory-backed registers" Kconfig help text and the changelog only hold
on architectures like x86-64. The rest of the series doesn't seem to
change this Kconfig entry or the test file.
There seem to be two ways to fix this. One is to restrict the option by
architecture, for example depends on !UML && !S390 && !SPARC64, or an
allow-list. The other is to route register access through a hook that the
test can replace with a memory-backed implementation. With either one, the
comment and help text should be updated to match.
> + i2c->endianness = param->endianness;
> + i2c->force_32bit_access = param->force_32bit_access;
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112163333.12523-1-ahmadexp%40gmail.com
Re: [PATCH net-next v2 2/3] i2c: xiic: add KUnit coverage for register access modes
Author: Ahmad Byagowi
Date: Mon, 16 Nov 2026 18:20
Date: Mon, 16 Nov 2026 18:20
9 lines
11398 bytes
11398 bytes
Agreed. A RAM buffer is not a portable MMIO mapping. The proposed v3 enables the RAM-backed test only on X86_64 with neither UML nor INDIRECT_IOMEM. The XIIC driver itself gains no architecture restriction. All four cases passed in an x86-64 QEMU guest. With XIIC and KUNIT_ALL_TESTS enabled, explicitly requesting the test on arm64, s390, SPARC64 and 64-bit UML with indirect I/O left it disabled. pw-bot: cr
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads