1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for the FPGA Region
5 * Copyright (C) 2023 Red Hat, Inc.
10 #include <kunit/device.h>
11 #include <kunit/test.h>
12 #include <linux/fpga/fpga-bridge.h>
13 #include <linux/fpga/fpga-mgr.h>
14 #include <linux/fpga/fpga-region.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
28 struct fpga_manager *mgr;
29 struct device *mgr_dev;
30 struct fpga_bridge *bridge;
31 struct device *bridge_dev;
32 struct fpga_region *region;
33 struct device *region_dev;
34 struct bridge_stats bridge_stats;
35 struct mgr_stats mgr_stats;
38 static int op_write(struct fpga_manager *mgr, const char *buf, size_t count)
40 struct mgr_stats *stats = mgr->priv;
48 * Fake FPGA manager that implements only the write op to count the number
49 * of programming cycles. The internals of the programming sequence are
50 * tested in the Manager suite since they are outside the responsibility
53 static const struct fpga_manager_ops fake_mgr_ops = {
57 static int op_enable_set(struct fpga_bridge *bridge, bool enable)
59 struct bridge_stats *stats = bridge->priv;
61 if (!stats->enable && enable)
62 stats->cycles_count++;
64 stats->enable = enable;
70 * Fake FPGA bridge that implements only enable_set op to count the number
71 * of activation cycles.
73 static const struct fpga_bridge_ops fake_bridge_ops = {
74 .enable_set = op_enable_set,
77 static int fake_region_get_bridges(struct fpga_region *region)
79 struct fpga_bridge *bridge = region->priv;
81 return fpga_bridge_get_to_list(bridge->dev.parent, region->info, ®ion->bridge_list);
84 static int fake_region_match(struct device *dev, const void *data)
86 return dev->parent == data;
89 static void fpga_region_test_class_find(struct kunit *test)
91 struct test_ctx *ctx = test->priv;
92 struct fpga_region *region;
94 region = fpga_region_class_find(NULL, ctx->region_dev, fake_region_match);
95 KUNIT_EXPECT_PTR_EQ(test, region, ctx->region);
97 put_device(®ion->dev);
101 * FPGA Region programming test. The Region must call get_bridges() to get
102 * and control the bridges, and then the Manager for the actual programming.
104 static void fpga_region_test_program_fpga(struct kunit *test)
106 struct test_ctx *ctx = test->priv;
107 struct fpga_image_info *img_info;
111 img_info = fpga_image_info_alloc(ctx->mgr_dev);
112 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, img_info);
114 img_info->buf = img_buf;
115 img_info->count = sizeof(img_buf);
117 ctx->region->info = img_info;
118 ret = fpga_region_program_fpga(ctx->region);
119 KUNIT_ASSERT_EQ(test, ret, 0);
121 KUNIT_EXPECT_EQ(test, 1, ctx->mgr_stats.write_count);
122 KUNIT_EXPECT_EQ(test, 1, ctx->bridge_stats.cycles_count);
124 fpga_bridges_put(&ctx->region->bridge_list);
126 ret = fpga_region_program_fpga(ctx->region);
127 KUNIT_ASSERT_EQ(test, ret, 0);
129 KUNIT_EXPECT_EQ(test, 2, ctx->mgr_stats.write_count);
130 KUNIT_EXPECT_EQ(test, 2, ctx->bridge_stats.cycles_count);
132 fpga_bridges_put(&ctx->region->bridge_list);
134 fpga_image_info_free(img_info);
138 * The configuration used in this test suite uses a single bridge to
139 * limit the code under test to a single unit. The functions used by the
140 * Region for getting and controlling bridges are tested (with a list of
141 * multiple bridges) in the Bridge suite.
143 static int fpga_region_test_init(struct kunit *test)
145 struct test_ctx *ctx;
146 struct fpga_region_info region_info = { 0 };
148 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
149 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
151 ctx->mgr_dev = kunit_device_register(test, "fpga-manager-test-dev");
152 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->mgr_dev);
154 ctx->mgr = devm_fpga_mgr_register(ctx->mgr_dev, "Fake FPGA Manager",
155 &fake_mgr_ops, &ctx->mgr_stats);
156 KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->mgr));
158 ctx->bridge_dev = kunit_device_register(test, "fpga-bridge-test-dev");
159 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->bridge_dev);
161 ctx->bridge = fpga_bridge_register(ctx->bridge_dev, "Fake FPGA Bridge",
162 &fake_bridge_ops, &ctx->bridge_stats);
163 KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge));
165 ctx->bridge_stats.enable = true;
167 ctx->region_dev = kunit_device_register(test, "fpga-region-test-dev");
168 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->region_dev);
170 region_info.mgr = ctx->mgr;
171 region_info.priv = ctx->bridge;
172 region_info.get_bridges = fake_region_get_bridges;
174 ctx->region = fpga_region_register_full(ctx->region_dev, ®ion_info);
175 KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->region));
182 static void fpga_region_test_exit(struct kunit *test)
184 struct test_ctx *ctx = test->priv;
186 fpga_region_unregister(ctx->region);
187 kunit_device_unregister(test, ctx->region_dev);
189 fpga_bridge_unregister(ctx->bridge);
190 kunit_device_unregister(test, ctx->bridge_dev);
192 kunit_device_unregister(test, ctx->mgr_dev);
195 static struct kunit_case fpga_region_test_cases[] = {
196 KUNIT_CASE(fpga_region_test_class_find),
197 KUNIT_CASE(fpga_region_test_program_fpga),
201 static struct kunit_suite fpga_region_suite = {
203 .init = fpga_region_test_init,
204 .exit = fpga_region_test_exit,
205 .test_cases = fpga_region_test_cases,
208 kunit_test_suite(fpga_region_suite);
210 MODULE_LICENSE("GPL");