icc-kunit.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit tests for the Interconnect framework.
  4. *
  5. * Copyright (c) 2025 Kuan-Wei Chiu <visitorckw@gmail.com>
  6. *
  7. * This suite verifies the behavior of the interconnect core, including
  8. * topology construction, bandwidth aggregation, and path lifecycle.
  9. */
  10. #include <kunit/platform_device.h>
  11. #include <kunit/test.h>
  12. #include <linux/interconnect-provider.h>
  13. #include <linux/interconnect.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/overflow.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include "internal.h"
  20. enum {
  21. NODE_CPU,
  22. NODE_GPU,
  23. NODE_BUS,
  24. NODE_DDR,
  25. NODE_MAX
  26. };
  27. struct test_node_data {
  28. int id;
  29. const char *name;
  30. int num_links;
  31. int links[2];
  32. };
  33. /*
  34. * Static Topology:
  35. * CPU -\
  36. * -> BUS -> DDR
  37. * GPU -/
  38. */
  39. static const struct test_node_data test_topology[] = {
  40. { NODE_CPU, "cpu", 1, { NODE_BUS } },
  41. { NODE_GPU, "gpu", 1, { NODE_BUS } },
  42. { NODE_BUS, "bus", 1, { NODE_DDR } },
  43. { NODE_DDR, "ddr", 0, { } },
  44. };
  45. struct icc_test_priv {
  46. struct icc_provider provider;
  47. struct platform_device *pdev;
  48. struct icc_node *nodes[NODE_MAX];
  49. };
  50. static struct icc_node *get_node(struct icc_test_priv *priv, int id)
  51. {
  52. int idx = id - NODE_CPU;
  53. if (idx < 0 || idx >= ARRAY_SIZE(test_topology))
  54. return NULL;
  55. return priv->nodes[idx];
  56. }
  57. static int icc_test_set(struct icc_node *src, struct icc_node *dst)
  58. {
  59. return 0;
  60. }
  61. static int icc_test_aggregate(struct icc_node *node, u32 tag, u32 avg_bw,
  62. u32 peak_bw, u32 *agg_avg, u32 *agg_peak)
  63. {
  64. return icc_std_aggregate(node, tag, avg_bw, peak_bw, agg_avg, agg_peak);
  65. }
  66. static struct icc_node *icc_test_xlate(const struct of_phandle_args *spec, void *data)
  67. {
  68. return NULL;
  69. }
  70. static int icc_test_get_bw(struct icc_node *node, u32 *avg, u32 *peak)
  71. {
  72. *avg = 0;
  73. *peak = 0;
  74. return 0;
  75. }
  76. static int icc_test_init(struct kunit *test)
  77. {
  78. struct icc_test_priv *priv;
  79. struct icc_node *node;
  80. int i, j, ret;
  81. priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
  82. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
  83. test->priv = priv;
  84. priv->pdev = kunit_platform_device_alloc(test, "icc-test-dev", -1);
  85. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->pdev);
  86. KUNIT_ASSERT_EQ(test, kunit_platform_device_add(test, priv->pdev), 0);
  87. priv->provider.set = icc_test_set;
  88. priv->provider.aggregate = icc_test_aggregate;
  89. priv->provider.xlate = icc_test_xlate;
  90. priv->provider.get_bw = icc_test_get_bw;
  91. priv->provider.dev = &priv->pdev->dev;
  92. priv->provider.data = priv;
  93. INIT_LIST_HEAD(&priv->provider.nodes);
  94. ret = icc_provider_register(&priv->provider);
  95. KUNIT_ASSERT_EQ(test, ret, 0);
  96. for (i = 0; i < ARRAY_SIZE(test_topology); i++) {
  97. const struct test_node_data *data = &test_topology[i];
  98. node = icc_node_create(data->id);
  99. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, node);
  100. node->name = data->name;
  101. icc_node_add(node, &priv->provider);
  102. priv->nodes[i] = node;
  103. }
  104. for (i = 0; i < ARRAY_SIZE(test_topology); i++) {
  105. const struct test_node_data *data = &test_topology[i];
  106. struct icc_node *src = get_node(priv, data->id);
  107. for (j = 0; j < data->num_links; j++) {
  108. ret = icc_link_create(src, data->links[j]);
  109. KUNIT_ASSERT_EQ_MSG(test, ret, 0, "Failed to link %s->%d",
  110. src->name, data->links[j]);
  111. }
  112. }
  113. icc_sync_state(&priv->pdev->dev);
  114. return 0;
  115. }
  116. static void icc_test_exit(struct kunit *test)
  117. {
  118. struct icc_test_priv *priv = test->priv;
  119. icc_nodes_remove(&priv->provider);
  120. icc_provider_deregister(&priv->provider);
  121. }
  122. /*
  123. * Helper to construct a mock path.
  124. *
  125. * Because we are bypassing icc_get(), we must manually link the requests
  126. * to the nodes' req_list so that icc_std_aggregate() can discover them.
  127. */
  128. static struct icc_path *icc_test_create_path(struct kunit *test,
  129. struct icc_node **nodes, int num)
  130. {
  131. struct icc_path *path;
  132. int i;
  133. path = kunit_kzalloc(test, struct_size(path, reqs, num), GFP_KERNEL);
  134. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, path);
  135. path->num_nodes = num;
  136. for (i = 0; i < num; i++) {
  137. path->reqs[i].node = nodes[i];
  138. hlist_add_head(&path->reqs[i].req_node, &nodes[i]->req_list);
  139. }
  140. path->name = "mock-path";
  141. return path;
  142. }
  143. static void icc_test_destroy_path(struct kunit *test, struct icc_path *path)
  144. {
  145. int i;
  146. for (i = 0; i < path->num_nodes; i++)
  147. hlist_del(&path->reqs[i].req_node);
  148. kunit_kfree(test, path);
  149. }
  150. static void icc_test_topology_integrity(struct kunit *test)
  151. {
  152. struct icc_test_priv *priv = test->priv;
  153. struct icc_node *cpu = get_node(priv, NODE_CPU);
  154. struct icc_node *bus = get_node(priv, NODE_BUS);
  155. KUNIT_EXPECT_EQ(test, cpu->num_links, 1);
  156. KUNIT_EXPECT_PTR_EQ(test, cpu->links[0], bus);
  157. KUNIT_EXPECT_PTR_EQ(test, cpu->provider, &priv->provider);
  158. }
  159. static void icc_test_set_bw(struct kunit *test)
  160. {
  161. struct icc_test_priv *priv = test->priv;
  162. struct icc_path *path;
  163. struct icc_node *path_nodes[3];
  164. int ret;
  165. /* Path: CPU -> BUS -> DDR */
  166. path_nodes[0] = get_node(priv, NODE_CPU);
  167. path_nodes[1] = get_node(priv, NODE_BUS);
  168. path_nodes[2] = get_node(priv, NODE_DDR);
  169. path = icc_test_create_path(test, path_nodes, 3);
  170. ret = icc_enable(path);
  171. KUNIT_ASSERT_EQ(test, ret, 0);
  172. ret = icc_set_bw(path, 1000, 2000);
  173. KUNIT_EXPECT_EQ(test, ret, 0);
  174. KUNIT_EXPECT_EQ(test, path_nodes[0]->avg_bw, 1000);
  175. KUNIT_EXPECT_EQ(test, path_nodes[0]->peak_bw, 2000);
  176. KUNIT_EXPECT_EQ(test, path_nodes[1]->avg_bw, 1000);
  177. KUNIT_EXPECT_EQ(test, path_nodes[1]->peak_bw, 2000);
  178. icc_set_tag(path, 0xABC);
  179. KUNIT_EXPECT_EQ(test, path->reqs[0].tag, 0xABC);
  180. icc_disable(path);
  181. KUNIT_EXPECT_EQ(test, path_nodes[0]->avg_bw, 0);
  182. icc_test_destroy_path(test, path);
  183. }
  184. static void icc_test_aggregation(struct kunit *test)
  185. {
  186. struct icc_test_priv *priv = test->priv;
  187. struct icc_path *path_cpu, *path_gpu;
  188. struct icc_node *nodes_cpu[3], *nodes_gpu[2];
  189. struct icc_node *bus = get_node(priv, NODE_BUS);
  190. int ret;
  191. nodes_cpu[0] = get_node(priv, NODE_CPU);
  192. nodes_cpu[1] = bus;
  193. nodes_cpu[2] = get_node(priv, NODE_DDR);
  194. path_cpu = icc_test_create_path(test, nodes_cpu, 3);
  195. nodes_gpu[0] = get_node(priv, NODE_GPU);
  196. nodes_gpu[1] = bus;
  197. path_gpu = icc_test_create_path(test, nodes_gpu, 2);
  198. icc_enable(path_cpu);
  199. icc_enable(path_gpu);
  200. ret = icc_set_bw(path_cpu, 1000, 1000);
  201. KUNIT_EXPECT_EQ(test, ret, 0);
  202. KUNIT_EXPECT_EQ(test, bus->avg_bw, 1000);
  203. ret = icc_set_bw(path_gpu, 2000, 2000);
  204. KUNIT_EXPECT_EQ(test, ret, 0);
  205. /* Bus aggregates: CPU(1000) + GPU(2000) */
  206. KUNIT_EXPECT_EQ(test, bus->avg_bw, 3000);
  207. /* Peak aggregates: max(CPU, GPU) */
  208. KUNIT_EXPECT_EQ(test, bus->peak_bw, 2000);
  209. icc_test_destroy_path(test, path_cpu);
  210. icc_test_destroy_path(test, path_gpu);
  211. }
  212. static void icc_test_bulk_ops(struct kunit *test)
  213. {
  214. struct icc_test_priv *priv = test->priv;
  215. struct icc_node *nodes_cpu[3], *nodes_gpu[2];
  216. struct icc_bulk_data bulk[2];
  217. int ret;
  218. nodes_cpu[0] = get_node(priv, NODE_CPU);
  219. nodes_cpu[1] = get_node(priv, NODE_BUS);
  220. nodes_cpu[2] = get_node(priv, NODE_DDR);
  221. nodes_gpu[0] = get_node(priv, NODE_GPU);
  222. nodes_gpu[1] = get_node(priv, NODE_BUS);
  223. bulk[0].path = icc_test_create_path(test, nodes_cpu, 3);
  224. bulk[0].avg_bw = 500;
  225. bulk[0].peak_bw = 500;
  226. bulk[1].path = icc_test_create_path(test, nodes_gpu, 2);
  227. bulk[1].avg_bw = 600;
  228. bulk[1].peak_bw = 600;
  229. ret = icc_bulk_set_bw(2, bulk);
  230. KUNIT_EXPECT_EQ(test, ret, 0);
  231. /* Paths disabled, bandwidth should be 0 */
  232. KUNIT_EXPECT_EQ(test, get_node(priv, NODE_BUS)->avg_bw, 0);
  233. ret = icc_bulk_enable(2, bulk);
  234. KUNIT_EXPECT_EQ(test, ret, 0);
  235. /* Paths enabled, aggregation applies */
  236. KUNIT_EXPECT_EQ(test, get_node(priv, NODE_BUS)->avg_bw, 1100);
  237. icc_bulk_disable(2, bulk);
  238. KUNIT_EXPECT_EQ(test, get_node(priv, NODE_BUS)->avg_bw, 0);
  239. icc_test_destroy_path(test, bulk[0].path);
  240. icc_test_destroy_path(test, bulk[1].path);
  241. }
  242. static struct kunit_case icc_test_cases[] = {
  243. KUNIT_CASE(icc_test_topology_integrity),
  244. KUNIT_CASE(icc_test_set_bw),
  245. KUNIT_CASE(icc_test_aggregation),
  246. KUNIT_CASE(icc_test_bulk_ops),
  247. {}
  248. };
  249. static struct kunit_suite icc_test_suite = {
  250. .name = "interconnect",
  251. .init = icc_test_init,
  252. .exit = icc_test_exit,
  253. .test_cases = icc_test_cases,
  254. };
  255. kunit_test_suite(icc_test_suite);
  256. MODULE_AUTHOR("Kuan-Wei Chiu <visitorckw@gmail.com>");
  257. MODULE_DESCRIPTION("KUnit tests for the Interconnect framework");
  258. MODULE_LICENSE("GPL");