From patchwork Fri Jun 16 15:44:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 109228 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1447625vqr; Fri, 16 Jun 2023 09:03:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5fGxEUfAhwQKXkcNHkH/ST19BIO5BHN3XgvJkqzMNDSw7N9NyxOV29M3MCi9UP+OBRyzhn X-Received: by 2002:a17:902:7487:b0:1b0:307c:e6fe with SMTP id h7-20020a170902748700b001b0307ce6femr2121269pll.10.1686931391755; Fri, 16 Jun 2023 09:03:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686931391; cv=none; d=google.com; s=arc-20160816; b=ETdDqGKvYhMksasKcU04w2KDGaoyw4T15JqiwvrAE9fIzD8zLUfbEG0jZPpg8CDwUY 8qZ9gZMwpOzTRMSKXOCWUSS9yxRSMImZRGkur1EAFixXpYF86E6IrDkRm3785wyYdiKF Kw9TowRVJrbi7sZsCXPAuyUPjsu8GjAyJ/mOzR1zFTrrMbLN1GQPdCRerXP2FXVTEKi7 4OPHKRPDSTvxw8k6AdgFCAUms73qthCPUww9ttHLbjZsEmm49Oin43RJscWxKac4dUjj bYECphVEXViGgt546XgmKg0ZS2TjX4tj2L5YX2n6ko9D4D4aHC2brOI/JoKy8CGvZArn hD9g== 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=rk/GX/Fw44kAlHIKlENLzG4S82eDoKHx4BfoPDKJcsI=; b=pEfVapgkyjEPcfaruKh/vjb3kKg41xgYKelJuKRGc56It/wpIre+xFpEts9WqOexxD AzI0lNP68kD9pHJ5okHq6CFDVHbQQ6MhKGVt0eJKVFzGSe1007jau9XIx/nLXvcZFop+ nxX8Ym4JyYtm2eXwOKcp8NOIaRgXYqSMennwYk1aO4Ikx9Pzg1Eu/EFtPALQBdFOb5wl htiN/u/zllZe2kbxs4ccEvIAP+Eq4JVtJbNHN8J3JsKpCc9jXDDKXYCZ+fFuj3uX3ahj Yh91uCAA6+yZS7qUrdLRjDx8efDcSSia1yw3nl9MD3cHvFlcMQKOryZJYdpVnH/ExsHx SdXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WMqQ1Nry; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020a170902f54a00b001b23d02a0c7si2522916plf.272.2023.06.16.09.02.56; Fri, 16 Jun 2023 09:03:11 -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=WMqQ1Nry; 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 S1346117AbjFPPpj (ORCPT + 99 others); Fri, 16 Jun 2023 11:45:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345881AbjFPPpd (ORCPT ); Fri, 16 Jun 2023 11:45:33 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FFDC30D1 for ; Fri, 16 Jun 2023 08:44:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686930285; 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=rk/GX/Fw44kAlHIKlENLzG4S82eDoKHx4BfoPDKJcsI=; b=WMqQ1Nryrn0BAHDikYUZJQmW6r/rSSpjKxrcBxPY2v4PDmlZnmRNd39NaIIAG4WLXZSD6I B87N6c34rzHKJgh4jHBjZZ5VAgh6SaXs+DHoOi9XXleMKZt8WnSOumWHkbG4N7mgaIO3Te 7nMCKs6TRJJIK/RWFAzj1JgiLQfVcU8= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-22-YQh0AJLJMGWEivkPpMNNkw-1; Fri, 16 Jun 2023 11:44:43 -0400 X-MC-Unique: YQh0AJLJMGWEivkPpMNNkw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-762318ecdd1so95559585a.0 for ; Fri, 16 Jun 2023 08:44:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686930283; x=1689522283; 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=rk/GX/Fw44kAlHIKlENLzG4S82eDoKHx4BfoPDKJcsI=; b=VtyOJEY0+pHo00nnZoWwHOaCI9WQWDHzs6JxZ8RfSKNEaRowWWBzrmsuUQtp7bvQHV 4+bvqij5YNKa3PXmPe3eJQf4q4dW6Uotz37tJuH+vjk+FTw/77MmJP5hE3n+7Hhigll1 Z4jDw0b3GoNRPH5VC8bW19aiEoB0pMzISX05amlSyKjPFHaspU35zh/0rrZAoLwTUjJK a0eRutukMOWemJy7rXP+MBefd7PizaRhyxBbJVuEAKze8JMpe1Wy5zqN4moqlDR17Op8 wEJjKoAFvNHSct/BxZitroB3gEFYvaAaj2Wre8/d0X1WI8YalNWD9XURm6/nRdxchexV TlWw== X-Gm-Message-State: AC+VfDxEb/LiKJP9t/tsAQvQ6rWvZd+jx8Kmig5TkEJ87oU9yvBbAwFt TTw8tvLu6zkuC1HYVEMy0evluAriPNEzcLfJ4z8jFFbF7duftMO2oJeMFxMTDZkePpjWZOdl9yZ iSzCYM7h2LmckTJhbz1arDyMM+ZRjoD0= X-Received: by 2002:a05:620a:8b14:b0:75b:23a0:e7da with SMTP id qw20-20020a05620a8b1400b0075b23a0e7damr1926707qkn.59.1686930283070; Fri, 16 Jun 2023 08:44:43 -0700 (PDT) X-Received: by 2002:a05:620a:8b14:b0:75b:23a0:e7da with SMTP id qw20-20020a05620a8b1400b0075b23a0e7damr1926698qkn.59.1686930282819; Fri, 16 Jun 2023 08:44:42 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-28-201.cust.vodafonedsl.it. [2.34.28.201]) by smtp.gmail.com with ESMTPSA id 27-20020a05620a079b00b007607ecd58ecsm4436931qka.59.2023.06.16.08.44.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jun 2023 08:44:42 -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 v7 3/4] fpga: add an initial KUnit suite for the FPGA Region Date: Fri, 16 Jun 2023 17:44:04 +0200 Message-Id: <20230616154405.220502-4-marpagan@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230616154405.220502-1-marpagan@redhat.com> References: <20230616154405.220502-1-marpagan@redhat.com> MIME-Version: 1.0 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_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768875771237909741?= X-GMAIL-MSGID: =?utf-8?q?1768875771237909741?= 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..a502f3f2560d --- /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");