Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22479190rwd; Fri, 30 Jun 2023 08:29:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlEKGFnv8nN50PvtOCCaLCAEQKKH4P083xGf/38q/Ds+nOUT2QU0m8qAJPCTxWq7YpLEe8uj X-Received: by 2002:a17:90b:30c3:b0:262:df79:6f81 with SMTP id hi3-20020a17090b30c300b00262df796f81mr2063170pjb.24.1688138981883; Fri, 30 Jun 2023 08:29:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688138981; cv=none; d=google.com; s=arc-20160816; b=Dfjjznmdd77UKDlJPz6ES51t7PCPYD33xhwDWTcrJOkSW0zJ0cuxEYkwE/A5l/Z3nQ qTs+9+gTXSJBjxxt0h4vTgeSUOtOeUqfB/w9DO9wvUTcUhPIItOx4ycUay1Q8JpCn0QX InjYb4Ck1int48UbmE6uM7ehi1J6SDq9PsUWv6agqnn400utfQFTCg3uiFZnabBJO1sO n8lggsWXT2ksGThfZs+NMvmdLuRkb9ZHkOPw0QnJzoIYJ4JxFkaxQao/8kRop7y6BIBY Y0qyKveuepjps5fxTfDNKV4bKCN1rFSUdouAeCTvZ0nQwBGKnsTBDmY9CsbbQ4FObLYo a0ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AZt43Ky3719pMlaApXqVMD6ZwhsTjVdiVPLbUGmOVRw=; fh=vL6Moim7ujQ71QOBXYMaaPnG1N9WYgskJqrZSTruGY4=; b=viPAdyIhAFRexdTK0adO6QYunMPDT3NAsDLmWYReqQo0L5/IzcdUcfJCwE8wqEuOVh /isLdUD+ZQwnBTPHxDdHE/+gdziPd5IkvuhsrTbf49U7pfZY66eovHfmp/BuW9ku13kL gOKocTeOWQqLyXsadMDY8BGkTHwtrdXs0GdFakdGk7dJDiPb2QmCB65ifMkBU5YOuzSA 74Lx/JL+n5deIp7AxmpAgpc6mXm9RDPmwRERT9Ksv2ciOVgfZMRwf7ZpQAb9y7z8Gl59 onANUiiNF8msfh9Ge1fNeuZhS3EJqrTVk+Cs3A1a4CyjZsAktlNPXFscc2vNGBVTYEJV eiPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=H8eNg4hr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 126-20020a630184000000b00553828d0361si12816732pgb.588.2023.06.30.08.29.29; Fri, 30 Jun 2023 08:29:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=H8eNg4hr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233017AbjF3P05 (ORCPT + 99 others); Fri, 30 Jun 2023 11:26:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231956AbjF3P0d (ORCPT ); Fri, 30 Jun 2023 11:26:33 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D300B2728 for ; Fri, 30 Jun 2023 08:25:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688138742; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AZt43Ky3719pMlaApXqVMD6ZwhsTjVdiVPLbUGmOVRw=; b=H8eNg4hrru7a6VAYb+vKrHitOeKotq0xmAF1cJ/ecW6uzE2eREjs94iXb3fNCbXD8tibZe YQiuLspneaE47zni0x+X841OA4g+xl6OW0rUUzfuKpsCCKCVFwqDtSU0lRg28zyvKENFKg ZxqW2S/viR5oqAR4slMxQNjhFxT4/E8= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-zanTvqVoP_2FzCbmPLJwCw-1; Fri, 30 Jun 2023 11:25:39 -0400 X-MC-Unique: zanTvqVoP_2FzCbmPLJwCw-1 Received: by mail-ed1-f69.google.com with SMTP id 4fb4d7f45d1cf-51dd16f823bso1170760a12.0 for ; Fri, 30 Jun 2023 08:25:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688138738; x=1690730738; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AZt43Ky3719pMlaApXqVMD6ZwhsTjVdiVPLbUGmOVRw=; b=WbUbZTM4btE9jA56gqaI11U3HvxCb3Vgin5s67eylN2tdTUMOP8MRbbLvezSWulRTE +FAvT1+CGLu2sbmpp6jZfuLCNbFiLUcw+zkvDGYJPqUkxBLBP10HzVHi/+8JX6orXiym xGRvWJGqepqdh4pgIuqRZfcylDFxCeF4cc2Fx7fOXcryDPXDMQr0sYwwfgEkHCFymkHu roO7WfZSLzbVFaW29yneiGiIDAua2Al3XNKj52N0WW51BSuB+yeq69T5fdTE7grPFOKW YNPulxOGd7eC0RCyT91R34e2378VruNuqPMMNazcijykZBF7AwnwNEWOime43gxPGt5W YIng== X-Gm-Message-State: ABy/qLb8+KkRqVXtR7BRVgyWp9/dCiK2zOnbwsl1wE+0HljdbcJjGs7l 8cazo7JiEB2iby1tbcUKOKyjmmbVc06qsW7Y13vJ/uaOsjq0CAzca5rdEjgVrOdVRIMMpGAM3C+ I/2nzDM3CXOm/mXQz3rgQVZs= X-Received: by 2002:a05:6402:284:b0:51d:b963:4bd3 with SMTP id l4-20020a056402028400b0051db9634bd3mr2448025edv.31.1688138738456; Fri, 30 Jun 2023 08:25:38 -0700 (PDT) X-Received: by 2002:a05:6402:284:b0:51d:b963:4bd3 with SMTP id l4-20020a056402028400b0051db9634bd3mr2448013edv.31.1688138738179; Fri, 30 Jun 2023 08:25:38 -0700 (PDT) Received: from klayman.station (net-2-34-24-242.cust.vodafonedsl.it. [2.34.24.242]) by smtp.gmail.com with ESMTPSA id k24-20020aa7c398000000b0051def9be785sm813230edq.85.2023.06.30.08.25.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 08:25:37 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix Cc: Marco Pagani , linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [PATCH v8 3/4] fpga: add an initial KUnit suite for the FPGA Region Date: Fri, 30 Jun 2023 17:25:06 +0200 Message-ID: <20230630152507.30219-4-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230630152507.30219-1-marpagan@redhat.com> References: <20230630152507.30219-1-marpagan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The suite tests the basic behaviors of the FPGA Region including the programming and the function for finding a specific region. Signed-off-by: Marco Pagani Acked-by: Xu Yilun --- drivers/fpga/tests/fpga-region-test.c | 211 ++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 drivers/fpga/tests/fpga-region-test.c diff --git a/drivers/fpga/tests/fpga-region-test.c b/drivers/fpga/tests/fpga-region-test.c new file mode 100644 index 000000000000..8a195d85a877 --- /dev/null +++ b/drivers/fpga/tests/fpga-region-test.c @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the FPGA Region + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include +#include +#include + +struct mgr_stats { + u32 write_count; +}; + +struct bridge_stats { + bool enable; + u32 cycles_count; +}; + +struct test_ctx { + struct fpga_manager *mgr; + struct platform_device *mgr_pdev; + struct fpga_bridge *bridge; + struct platform_device *bridge_pdev; + struct fpga_region *region; + struct platform_device *region_pdev; + struct bridge_stats bridge_stats; + struct mgr_stats mgr_stats; +}; + +static int op_write(struct fpga_manager *mgr, const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + + stats->write_count++; + + return 0; +} + +/* + * Fake Manager that implements only the write op to count the number of + * programming cycles. The internals of the programming sequence are + * tested in the Manager suite since they are outside the responsibility + * of the Region. + */ +static const struct fpga_manager_ops fake_mgr_ops = { + .write = op_write, +}; + +static int op_enable_set(struct fpga_bridge *bridge, bool enable) +{ + struct bridge_stats *stats = bridge->priv; + + if (!stats->enable && enable) + stats->cycles_count++; + + stats->enable = enable; + + return 0; +} + +/* + * Fake Bridge that implements only enable_set op to count the number of + * activation cycles. + */ +static const struct fpga_bridge_ops fake_bridge_ops = { + .enable_set = op_enable_set, +}; + +static int fake_region_get_bridges(struct fpga_region *region) +{ + struct fpga_bridge *bridge = region->priv; + + return fpga_bridge_get_to_list(bridge->dev.parent, region->info, ®ion->bridge_list); +} + +static int fake_region_match(struct device *dev, const void *data) +{ + return dev->parent == data; +} + +static void fpga_region_test_class_find(struct kunit *test) +{ + struct test_ctx *ctx = test->priv; + struct fpga_region *region; + + region = fpga_region_class_find(NULL, &ctx->region_pdev->dev, fake_region_match); + KUNIT_EXPECT_PTR_EQ(test, region, ctx->region); +} + +/* + * Region programming test. The Region must call get_bridges() to get and + * control the bridges, and then the Manager for the actual programming. + */ +static void fpga_region_test_program_fpga(struct kunit *test) +{ + struct test_ctx *ctx = test->priv; + struct fpga_image_info *img_info; + char img_buf[4]; + int ret; + + img_info = fpga_image_info_alloc(&ctx->mgr_pdev->dev); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, img_info); + + img_info->buf = img_buf; + img_info->count = sizeof(img_buf); + + ctx->region->info = img_info; + ret = fpga_region_program_fpga(ctx->region); + KUNIT_ASSERT_EQ(test, ret, 0); + + KUNIT_EXPECT_EQ(test, 1, ctx->mgr_stats.write_count); + KUNIT_EXPECT_EQ(test, 1, ctx->bridge_stats.cycles_count); + + fpga_bridges_put(&ctx->region->bridge_list); + + ret = fpga_region_program_fpga(ctx->region); + KUNIT_ASSERT_EQ(test, ret, 0); + + KUNIT_EXPECT_EQ(test, 2, ctx->mgr_stats.write_count); + KUNIT_EXPECT_EQ(test, 2, ctx->bridge_stats.cycles_count); + + fpga_bridges_put(&ctx->region->bridge_list); + + fpga_image_info_free(img_info); +} + +/* + * The configuration used in this test suite uses a single bridge to + * limit the code under test to a single unit. The functions used by the + * Region for getting and controlling bridges are tested (with a list of + * multiple bridges) in the Bridge suite. + */ +static int fpga_region_test_init(struct kunit *test) +{ + struct test_ctx *ctx; + struct fpga_region_info region_info = { 0 }; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + ctx->mgr_pdev = platform_device_register_simple("mgr_pdev", PLATFORM_DEVID_AUTO, NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->mgr_pdev); + + ctx->mgr = devm_fpga_mgr_register(&ctx->mgr_pdev->dev, "Fake FPGA Manager", &fake_mgr_ops, + &ctx->mgr_stats); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->mgr)); + + ctx->bridge_pdev = platform_device_register_simple("bridge_pdev", PLATFORM_DEVID_AUTO, + NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->bridge_pdev); + + ctx->bridge = fpga_bridge_register(&ctx->bridge_pdev->dev, "Fake FPGA Bridge", + &fake_bridge_ops, &ctx->bridge_stats); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge)); + + ctx->bridge_stats.enable = true; + + ctx->region_pdev = platform_device_register_simple("region_pdev", PLATFORM_DEVID_AUTO, + NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->region_pdev); + + region_info.mgr = ctx->mgr; + region_info.priv = ctx->bridge; + region_info.get_bridges = fake_region_get_bridges; + + ctx->region = fpga_region_register_full(&ctx->region_pdev->dev, ®ion_info); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->region)); + + test->priv = ctx; + + return 0; +} + +static void fpga_region_test_exit(struct kunit *test) +{ + struct test_ctx *ctx = test->priv; + + fpga_region_unregister(ctx->region); + platform_device_unregister(ctx->region_pdev); + + fpga_bridge_unregister(ctx->bridge); + platform_device_unregister(ctx->bridge_pdev); + + platform_device_unregister(ctx->mgr_pdev); +} + +static struct kunit_case fpga_region_test_cases[] = { + KUNIT_CASE(fpga_region_test_class_find), + KUNIT_CASE(fpga_region_test_program_fpga), + + {} +}; + +static struct kunit_suite fpga_region_suite = { + .name = "fpga_mgr", + .init = fpga_region_test_init, + .exit = fpga_region_test_exit, + .test_cases = fpga_region_test_cases, +}; + +kunit_test_suite(fpga_region_suite); + +MODULE_LICENSE("GPL"); -- 2.41.0