From nobody Tue Dec 16 16:38:01 2025 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 579083242A4 for ; Wed, 10 Dec 2025 18:01:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765389681; cv=none; b=lh3aio56y7PztOL5a6/y7qa1D2LJXK733U1MziJ+6MC2FZpz+xEjeMwuAO8vpcg+41KOpfPwf4ojq4D1/US8sc0YdpIYhoHnx1UzLhONCVF7IWoagb2rk01h1919uPbQfrLMJHwZnQqYi/cmvBWigM0c7xQXJCX5l5r6jDIgoz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765389681; c=relaxed/simple; bh=0CRpciQkOV9hX55q69tWXDzJdDSlmqGFwfhnOcfQyFQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=CNvkCN1NYio669amnJD4gs16fZpBmNwlbOaHtDzHc02ZOhCBwZK3qBA2WLEp9dhBdMDLTD9IG6AukKYdqLz/2WpYXd0uHykHINXlt23lpIboj0xiRzqezwiE0g9QWbQepcxxuKKdjUEN+ctb0Mm+HNSv7apWwd6Cdz+FiKF+uUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ApwgGde5; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ApwgGde5" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7ba55660769so50385b3a.1 for ; Wed, 10 Dec 2025 10:01:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765389678; x=1765994478; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6OYRHQWa3xiEJx4SHKHAZrrHc9alAZAUbl4tlBhztlY=; b=ApwgGde5ga24gCyRBCRtuK3va2FF1ShLw5/1qUZf7U4F/rgyCj3lmGTMNqyNn5yMiw f8f0Npmh9U7jSmAmcPyZ5HSObDQ9H5gIzQz1aOmoQCdKWXbb8xJd2k+b7+um+19raHEF LUn50RLKXOTWq9tNs/2+geU4lYJop4rgXsQ0sHA/2gN23ZEy91Ox1aN/77Nnpict1E1r nshZ0SP30iZace3kxoo0sRwDtxdxlThs/Minx4YToO8lieq81OqAgDe+xOfMFWEKH/nm wnV09pFKN/h1gGGiCI0+HZwyaythO+rmwVcR5TKmfcoSYOMv0AIKOgWkRDmWS+EzJPpY XAkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765389678; x=1765994478; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=6OYRHQWa3xiEJx4SHKHAZrrHc9alAZAUbl4tlBhztlY=; b=kjlEfy6jYSuvNUn3mFoJEMTel5t+ECJ2hK3VNqF54pTILxWigT0sbZSVK8zpLgG73O lbi9utgDsxHeFGbXflRGwdAH9/PzZGLbpyrzu/VI0BBD14I+CJSGNumFKuMZauqqlb0l rG6BcVRBE9pMqz1WPOHBEzz6CCp0aG6eVGe9uxFLMFAacAOO3NeEomyO7G/JKn8r/s5Y ZHvEMcU4Exm3zYLqtgJvuX1vBxpeium0pbY1UoksZxWRo8yCEm1idOmVuKZDLb72X4sg FlAhb3pG5kX2LegAk0/WdFu+v2stMIajBg4qWGlWErXyQkJEN51+hlElYQ4NcUQSJ7UC M5fA== X-Forwarded-Encrypted: i=1; AJvYcCU9/3dvzXxtbhJocmqjPLt60eJKXoDaWvfoTTfVKp5cl0PQBqtuUl1M7ENy2OXipR+dnhaH+5E53HQtUEk=@vger.kernel.org X-Gm-Message-State: AOJu0YxPE/WKil6h9PvRmU4FAx/0i9iI6LER7NGUHd+tQsr10Da8Kwgj k7LclQvdatQOc8ZJGcm9ZM5g/YItiCpbqjkCSqITrEA9se8LDDwYHqph7x6fwQ== X-Gm-Gg: AY/fxX5NiUejuaus71A4MKspz/mu8+ZkOdXdR4fdatRqP0cJmUKwOiQWmeKI1q2zwu5 uRuvu4fyNzFisT1vsqC6tTQP+XhqKuuoHZtSiUAUgfuUYfb9BGR+oYdG0fREeOOcIsPMDv0xghZ EeyuWTZqcQKxeNNLmUui3qDT19U3sm3bgLv84I6Ak7Ara2UbC9xyE1HNQ6Ks4rzLyPA9VxI8ucZ nVw79DuR7F1Xr8ZL+WN1kx1jPpOSM2IshW7jb5l/ygNJ4u1G/cpVc2rsVMymG2uGV8GyR/AmudP WqZ8BLlq/n+CSo+cvWQMf0x/WyByJay4UuYfGPNkTJoE3/OdnUB2ucigN7d7j9AnA9Ia8nGt54B h3FgF+4KidSC3cfuCEwXBO7nUqEXvsimJe9/DjUN8vp8UKpNPYu+rtcc10nJNJLpFxaQJclaHNm 1aj3snSLRVq1EQDu/Q+tI7qzWITcaRu2FR0ovmDZeRVCRtPnL1ecF+MaRneE3st+r8SCWfBf1YQ h0CZmsieVntf2vCSk9irYVT X-Google-Smtp-Source: AGHT+IFaGfQ/nIyAlOjpZun6rhBG69hgbULLUO2of3xW8vx4vmAcuobEfmn/Tz8RxqBpffLbTkS0Xw== X-Received: by 2002:a05:6a00:4f85:b0:7e8:4471:8cd with SMTP id d2e1a72fcca58-7f22ee57122mr2528975b3a.46.1765389676997; Wed, 10 Dec 2025 10:01:16 -0800 (PST) Received: from visitorckw-work01.c.googlers.com.com (14.250.194.35.bc.googleusercontent.com. [35.194.250.14]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7f4c585cde0sm159391b3a.69.2025.12.10.10.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Dec 2025 10:01:16 -0800 (PST) From: Kuan-Wei Chiu To: djakov@kernel.org Cc: jserv@ccns.ncku.edu.tw, marscheng@google.com, wllee@google.com, aarontian@google.com, hsuanting@google.com, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH] interconnect: Add kunit tests for core functionality Date: Wed, 10 Dec 2025 18:00:58 +0000 Message-ID: <20251210180058.2115010-1-visitorckw@gmail.com> X-Mailer: git-send-email 2.52.0.223.gf5cc29aaa4-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The interconnect framework currently lacks in-tree unit tests to verify the core logic in isolation. This makes it difficult to validate regression stability when modifying the provider/consumer APIs or aggregation logic. Introduce a kunit test suite that verifies the fundamental behavior of the subsystem. The tests cover: - Provider API (node creation, linking, topology construction). - Consumer API (path enabling/disabling, bandwidth requests). - Standard aggregation logic (accumulating bandwidth across links). - Bulk operations for setting bandwidth on multiple paths. The suite simulates a simple SoC topology with multiple masters and a shared bus to validate traffic aggregation behavior in a controlled software environment, without requiring specific hardware or Device Tree support. Signed-off-by: Kuan-Wei Chiu --- Build and kunit tests passed drivers/interconnect/Kconfig | 14 ++ drivers/interconnect/Makefile | 2 + drivers/interconnect/icc-kunit.c | 315 +++++++++++++++++++++++++++++++ 3 files changed, 331 insertions(+) create mode 100644 drivers/interconnect/icc-kunit.c diff --git a/drivers/interconnect/Kconfig b/drivers/interconnect/Kconfig index f2e49bd97d31..882dcb0b4a5b 100644 --- a/drivers/interconnect/Kconfig +++ b/drivers/interconnect/Kconfig @@ -22,4 +22,18 @@ config INTERCONNECT_CLK help Support for wrapping clocks into the interconnect nodes. =20 +config INTERCONNECT_KUNIT_TEST + tristate "KUnit tests for Interconnect framework" + depends on KUNIT + default KUNIT_ALL_TESTS + help + This builds the KUnit test suite for the generic system interconnect + framework. + + The tests cover the core functionality of the interconnect subsystem, + including provider/consumer APIs, topology management, and bandwidth + aggregation logic. + + If unsure, say N. + endif diff --git a/drivers/interconnect/Makefile b/drivers/interconnect/Makefile index b0a9a6753b9d..dc4c7b657c9d 100644 --- a/drivers/interconnect/Makefile +++ b/drivers/interconnect/Makefile @@ -10,3 +10,5 @@ obj-$(CONFIG_INTERCONNECT_QCOM) +=3D qcom/ obj-$(CONFIG_INTERCONNECT_SAMSUNG) +=3D samsung/ =20 obj-$(CONFIG_INTERCONNECT_CLK) +=3D icc-clk.o + +obj-$(CONFIG_INTERCONNECT_KUNIT_TEST) +=3D icc-kunit.o diff --git a/drivers/interconnect/icc-kunit.c b/drivers/interconnect/icc-ku= nit.c new file mode 100644 index 000000000000..2178487f9527 --- /dev/null +++ b/drivers/interconnect/icc-kunit.c @@ -0,0 +1,315 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the Interconnect framework. + * + * Copyright (c) 2025 Kuan-Wei Chiu + * + * This suite verifies the behavior of the interconnect core, including + * topology construction, bandwidth aggregation, and path lifecycle. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "internal.h" + +enum { + NODE_CPU =3D 100, + NODE_GPU, + NODE_BUS, + NODE_DDR, + NODE_MAX +}; + +struct test_node_data { + int id; + const char *name; + int num_links; + int links[2]; +}; + +/* + * Static Topology: + * CPU -\ + * -> BUS -> DDR + * GPU -/ + */ +static const struct test_node_data test_topology[] =3D { + { NODE_CPU, "cpu", 1, { NODE_BUS } }, + { NODE_GPU, "gpu", 1, { NODE_BUS } }, + { NODE_BUS, "bus", 1, { NODE_DDR } }, + { NODE_DDR, "ddr", 0, { } }, +}; + +struct icc_test_priv { + struct icc_provider provider; + struct platform_device *pdev; + struct icc_node *nodes[NODE_MAX]; +}; + +static struct icc_node *get_node(struct icc_test_priv *priv, int id) +{ + int idx =3D id - NODE_CPU; + + if (idx < 0 || idx >=3D ARRAY_SIZE(test_topology)) + return NULL; + return priv->nodes[idx]; +} + +static int icc_test_set(struct icc_node *src, struct icc_node *dst) +{ + return 0; +} + +static int icc_test_aggregate(struct icc_node *node, u32 tag, u32 avg_bw, + u32 peak_bw, u32 *agg_avg, u32 *agg_peak) +{ + return icc_std_aggregate(node, tag, avg_bw, peak_bw, agg_avg, agg_peak); +} + +static struct icc_node *icc_test_xlate(const struct of_phandle_args *spec,= void *data) +{ + return NULL; +} + +static int icc_test_init(struct kunit *test) +{ + struct icc_test_priv *priv; + struct icc_node *node; + int i, j, ret; + + priv =3D kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv); + test->priv =3D priv; + + priv->pdev =3D kunit_platform_device_alloc(test, "icc-test-dev", -1); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->pdev); + KUNIT_ASSERT_EQ(test, kunit_platform_device_add(test, priv->pdev), 0); + + priv->provider.set =3D icc_test_set; + priv->provider.aggregate =3D icc_test_aggregate; + priv->provider.xlate =3D icc_test_xlate; + priv->provider.dev =3D &priv->pdev->dev; + priv->provider.data =3D priv; + INIT_LIST_HEAD(&priv->provider.nodes); + + ret =3D icc_provider_register(&priv->provider); + KUNIT_ASSERT_EQ(test, ret, 0); + + for (i =3D 0; i < ARRAY_SIZE(test_topology); i++) { + const struct test_node_data *data =3D &test_topology[i]; + + node =3D icc_node_create(data->id); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node); + + node->name =3D data->name; + icc_node_add(node, &priv->provider); + priv->nodes[i] =3D node; + } + + for (i =3D 0; i < ARRAY_SIZE(test_topology); i++) { + const struct test_node_data *data =3D &test_topology[i]; + struct icc_node *src =3D get_node(priv, data->id); + + for (j =3D 0; j < data->num_links; j++) { + ret =3D icc_link_create(src, data->links[j]); + KUNIT_ASSERT_EQ_MSG(test, ret, 0, "Failed to link %s->%d", + src->name, data->links[j]); + } + } + + icc_sync_state(&priv->pdev->dev); + + return 0; +} + +static void icc_test_exit(struct kunit *test) +{ + struct icc_test_priv *priv =3D test->priv; + + icc_nodes_remove(&priv->provider); + icc_provider_deregister(&priv->provider); +} + +/* + * Helper to construct a mock path. + * + * Because we are bypassing icc_get(), we must manually link the requests + * to the nodes' req_list so that icc_std_aggregate() can discover them. + */ +static struct icc_path *icc_test_create_path(struct kunit *test, + struct icc_node **nodes, int num) +{ + struct icc_path *path; + int i; + + path =3D kunit_kzalloc(test, struct_size(path, reqs, num), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, path); + + path->num_nodes =3D num; + for (i =3D 0; i < num; i++) { + path->reqs[i].node =3D nodes[i]; + hlist_add_head(&path->reqs[i].req_node, &nodes[i]->req_list); + } + path->name =3D "mock-path"; + + return path; +} + +static void icc_test_destroy_path(struct kunit *test, struct icc_path *pat= h) +{ + int i; + + for (i =3D 0; i < path->num_nodes; i++) + hlist_del(&path->reqs[i].req_node); + + kunit_kfree(test, path); +} + +static void icc_test_topology_integrity(struct kunit *test) +{ + struct icc_test_priv *priv =3D test->priv; + struct icc_node *cpu =3D get_node(priv, NODE_CPU); + struct icc_node *bus =3D get_node(priv, NODE_BUS); + + KUNIT_EXPECT_EQ(test, cpu->num_links, 1); + KUNIT_EXPECT_PTR_EQ(test, cpu->links[0], bus); + KUNIT_EXPECT_PTR_EQ(test, cpu->provider, &priv->provider); +} + +static void icc_test_set_bw(struct kunit *test) +{ + struct icc_test_priv *priv =3D test->priv; + struct icc_path *path; + struct icc_node *path_nodes[3]; + int ret; + + /* Path: CPU -> BUS -> DDR */ + path_nodes[0] =3D get_node(priv, NODE_CPU); + path_nodes[1] =3D get_node(priv, NODE_BUS); + path_nodes[2] =3D get_node(priv, NODE_DDR); + + path =3D icc_test_create_path(test, path_nodes, 3); + + ret =3D icc_enable(path); + KUNIT_ASSERT_EQ(test, ret, 0); + + ret =3D icc_set_bw(path, 1000, 2000); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_EQ(test, path_nodes[0]->avg_bw, 1000); + KUNIT_EXPECT_EQ(test, path_nodes[0]->peak_bw, 2000); + KUNIT_EXPECT_EQ(test, path_nodes[1]->avg_bw, 1000); + KUNIT_EXPECT_EQ(test, path_nodes[1]->peak_bw, 2000); + + icc_set_tag(path, 0xABC); + KUNIT_EXPECT_EQ(test, path->reqs[0].tag, 0xABC); + + icc_disable(path); + KUNIT_EXPECT_EQ(test, path_nodes[0]->avg_bw, 0); + + icc_test_destroy_path(test, path); +} + +static void icc_test_aggregation(struct kunit *test) +{ + struct icc_test_priv *priv =3D test->priv; + struct icc_path *path_cpu, *path_gpu; + struct icc_node *nodes_cpu[3], *nodes_gpu[2]; + struct icc_node *bus =3D get_node(priv, NODE_BUS); + int ret; + + nodes_cpu[0] =3D get_node(priv, NODE_CPU); + nodes_cpu[1] =3D bus; + nodes_cpu[2] =3D get_node(priv, NODE_DDR); + path_cpu =3D icc_test_create_path(test, nodes_cpu, 3); + + nodes_gpu[0] =3D get_node(priv, NODE_GPU); + nodes_gpu[1] =3D bus; + path_gpu =3D icc_test_create_path(test, nodes_gpu, 2); + + icc_enable(path_cpu); + icc_enable(path_gpu); + + ret =3D icc_set_bw(path_cpu, 1000, 1000); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_EQ(test, bus->avg_bw, 1000); + + ret =3D icc_set_bw(path_gpu, 2000, 2000); + KUNIT_EXPECT_EQ(test, ret, 0); + + /* Bus aggregates: CPU(1000) + GPU(2000) */ + KUNIT_EXPECT_EQ(test, bus->avg_bw, 3000); + /* Peak aggregates: max(CPU, GPU) */ + KUNIT_EXPECT_EQ(test, bus->peak_bw, 2000); + + icc_test_destroy_path(test, path_cpu); + icc_test_destroy_path(test, path_gpu); +} + +static void icc_test_bulk_ops(struct kunit *test) +{ + struct icc_test_priv *priv =3D test->priv; + struct icc_node *nodes_cpu[3], *nodes_gpu[2]; + struct icc_bulk_data bulk[2]; + int ret; + + nodes_cpu[0] =3D get_node(priv, NODE_CPU); + nodes_cpu[1] =3D get_node(priv, NODE_BUS); + nodes_cpu[2] =3D get_node(priv, NODE_DDR); + + nodes_gpu[0] =3D get_node(priv, NODE_GPU); + nodes_gpu[1] =3D get_node(priv, NODE_BUS); + + bulk[0].path =3D icc_test_create_path(test, nodes_cpu, 3); + bulk[0].avg_bw =3D 500; + bulk[0].peak_bw =3D 500; + + bulk[1].path =3D icc_test_create_path(test, nodes_gpu, 2); + bulk[1].avg_bw =3D 600; + bulk[1].peak_bw =3D 600; + + ret =3D icc_bulk_set_bw(2, bulk); + KUNIT_EXPECT_EQ(test, ret, 0); + /* Paths disabled, bandwidth should be 0 */ + KUNIT_EXPECT_EQ(test, get_node(priv, NODE_BUS)->avg_bw, 0); + + ret =3D icc_bulk_enable(2, bulk); + KUNIT_EXPECT_EQ(test, ret, 0); + /* Paths enabled, aggregation applies */ + KUNIT_EXPECT_EQ(test, get_node(priv, NODE_BUS)->avg_bw, 1100); + + icc_bulk_disable(2, bulk); + KUNIT_EXPECT_EQ(test, get_node(priv, NODE_BUS)->avg_bw, 0); + + icc_test_destroy_path(test, bulk[0].path); + icc_test_destroy_path(test, bulk[1].path); +} + +static struct kunit_case icc_test_cases[] =3D { + KUNIT_CASE(icc_test_topology_integrity), + KUNIT_CASE(icc_test_set_bw), + KUNIT_CASE(icc_test_aggregation), + KUNIT_CASE(icc_test_bulk_ops), + {} +}; + +static struct kunit_suite icc_test_suite =3D { + .name =3D "interconnect", + .init =3D icc_test_init, + .exit =3D icc_test_exit, + .test_cases =3D icc_test_cases, +}; + +kunit_test_suite(icc_test_suite); + +MODULE_AUTHOR("Kuan-Wei Chiu "); +MODULE_DESCRIPTION("KUnit tests for the Interconnect framework"); +MODULE_LICENSE("GPL"); --=20 2.52.0.223.gf5cc29aaa4-goog