From patchwork Thu Jul 13 16:27:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 120035 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1963263vqm; Thu, 13 Jul 2023 10:06:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlGc1cFJvwVJTDz1z2ai9jsU5rLQuWmHAoTbsF7cqcMDvE+6sCBYivV6Dtij3lk9Zj8UOn1c X-Received: by 2002:a05:6512:ea9:b0:4f8:752f:3722 with SMTP id bi41-20020a0565120ea900b004f8752f3722mr2051259lfb.5.1689267967790; Thu, 13 Jul 2023 10:06:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689267967; cv=none; d=google.com; s=arc-20160816; b=zy/jzIaEPp1cMFvU8QOASmjw7HBqNEziw/JUXV/mcUbvKloOdgdqZ4BpDywf2iSvqj 4+4pxnxCYcdSgxsu7opgDjbE0N8LOLCieKRffmaILGEasXckVwn1sJr+3A49j0Vxcu21 4UNK3quUohDYX9Mj2lbegwK/MLqnnJyqXXTLkP+ITLRM86i98OL6pjwn+L7iP7+8bBh9 0Rjy4Whwxu2S6X4bBaWPILjE+UaW7oxAXYZjKKayziD6iZaq+E3U9ZYaakBybml3cH/r 9y92ceTjee+EEPP3/FyfDryGsFxA3bbYPv9ClwuFFjNxoxRPaCfl2MIC2zeWxIvYVohb i7AQ== 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=LBY3AM8SBjilw9AXdaa+jKuWzCAP6c8/6drDGdm7qoQ=; fh=M37Ydd+BXMWH9O/XP+uN5l3PQrnD/ZjHRj9NyKzE96w=; b=soyC4JCtaZP6MZvfr0vt7TFSjEHFzLrUnyTCESMEBTOutrl17m60eOJgfqmKMFe1qN yJSOKbEegKGQvtPLBWL15orJWDmVfD5L3dJAzBBmNlgsAXBqdVyB3Q5n+0IOeedHbo80 9qO3pQFXvkAjo4ZT8jE69kyPbqMyK8xufEViOTHhXR31nLlCbVqBbP9fcbL5bMG9dH1x EX9QOLk8aHX6ufeUmEAhYhBttidGNW6cM4ERHaIFRi1paGgQKRKaxOfRAQaQiVoEc1+a LZ8G7ggxUsxUU80FIi+A/nB8UANccw0OVup6o2YL/VzKroByVFub9OnNE8X+v3MMNP+u yfhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Eh3HMWfi; 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 n8-20020aa7db48000000b0051df74b43f1si7028115edt.539.2023.07.13.10.05.34; Thu, 13 Jul 2023 10:06:07 -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=Eh3HMWfi; 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 S235194AbjGMQ2j (ORCPT + 99 others); Thu, 13 Jul 2023 12:28:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229682AbjGMQ2h (ORCPT ); Thu, 13 Jul 2023 12:28:37 -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 A99DF2738 for ; Thu, 13 Jul 2023 09:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689265667; 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=LBY3AM8SBjilw9AXdaa+jKuWzCAP6c8/6drDGdm7qoQ=; b=Eh3HMWfisbNI36detL4qXwPxAtTP2S1DJLh3HSLhBw2iT2Br1pdmYnouhh843pt9AjpOE/ R1xETFwhSHY8URkRP10DOQJdRn+ntAJGd+bTsKI5uiHY68ISKXhYd2DtXZSPNMsIXj8FM7 x8X92iwR4WEjiA4hrwp1GibXGeamDNY= Received: from mail-vs1-f72.google.com (mail-vs1-f72.google.com [209.85.217.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-QOmwSHhxNcmgbUZZMtALrw-1; Thu, 13 Jul 2023 12:27:46 -0400 X-MC-Unique: QOmwSHhxNcmgbUZZMtALrw-1 Received: by mail-vs1-f72.google.com with SMTP id ada2fe7eead31-4434d4d8cd4so151468137.2 for ; Thu, 13 Jul 2023 09:27:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689265666; x=1691857666; 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=LBY3AM8SBjilw9AXdaa+jKuWzCAP6c8/6drDGdm7qoQ=; b=h2iKCP/4aA6tbBRNhV3Drs+KyEt/MqBCmlGqrn0AZ31kZ7oRbQoIRR8Yr1RMh2uvUl ams1eJ+g+GTn9QS2v5Dx4ps/wGn3Me1SMI4aVM5NxPYAcvzXqxJLWS5DL4UCJ/mJugEM UGCxxBjsnSaiGjNJhIyIMB9z/eet2u00lPiJjD6QDLMpK9dzno4NWUFU4vkN1PWkhRjB rM/PGre4D+isGG/oGj2Quq0KFQO1wl+M5oo595LFeV9K/K4to5GjT+VDQ8Q+SnP9L5YZ VSGWRKyWtazGmB/IZfgcrSQ9t+T11TZf83zZXtaxPQMFK/4Nhsup1KqcYAboWmEHitUo KnHQ== X-Gm-Message-State: ABy/qLbi/95S3lrMJJssmcRLPiJmaAq4wE1GB21j4kJwL3+1HZYnfriI /mH9wimzF6+fn4DAK3D925oVD2aIaMUFEEBNiQ54XEAxoUmz6pBLx9DYZPtosPAk8ubc49zsEwe yuFhqcgRc8aLrh5MkXYzCdUY= X-Received: by 2002:a67:e2c1:0:b0:443:6ad6:7915 with SMTP id i1-20020a67e2c1000000b004436ad67915mr1335046vsm.27.1689265666089; Thu, 13 Jul 2023 09:27:46 -0700 (PDT) X-Received: by 2002:a67:e2c1:0:b0:443:6ad6:7915 with SMTP id i1-20020a67e2c1000000b004436ad67915mr1335030vsm.27.1689265665795; Thu, 13 Jul 2023 09:27:45 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-24-242.cust.vodafonedsl.it. [2.34.24.242]) by smtp.gmail.com with ESMTPSA id u17-20020a0cf1d1000000b00637873ff0f3sm3206582qvl.15.2023.07.13.09.27.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 09:27:45 -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 v9 1/4] fpga: add an initial KUnit suite for the FPGA Manager Date: Thu, 13 Jul 2023 18:27:28 +0200 Message-ID: <20230713162731.211669-2-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230713162731.211669-1-marpagan@redhat.com> References: <20230713162731.211669-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_BLOCKED,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771325848541033902 X-GMAIL-MSGID: 1771325848541033902 The suite tests the basic behaviors of the FPGA Manager including programming using a single contiguous buffer and a scatter gather table. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fpga-mgr-test.c | 329 +++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 drivers/fpga/tests/fpga-mgr-test.c diff --git a/drivers/fpga/tests/fpga-mgr-test.c b/drivers/fpga/tests/fpga-mgr-test.c new file mode 100644 index 000000000000..9f797986737a --- /dev/null +++ b/drivers/fpga/tests/fpga-mgr-test.c @@ -0,0 +1,329 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the FPGA Manager + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include +#include + +#define HEADER_FILL 'H' +#define IMAGE_FILL 'P' +#define IMAGE_BLOCK 1024 + +#define HEADER_SIZE IMAGE_BLOCK +#define IMAGE_SIZE (IMAGE_BLOCK * 4) + +struct mgr_stats { + bool header_match; + bool image_match; + u32 seq_num; + u32 op_parse_header_seq; + u32 op_write_init_seq; + u32 op_write_seq; + u32 op_write_sg_seq; + u32 op_write_complete_seq; + enum fpga_mgr_states op_parse_header_state; + enum fpga_mgr_states op_write_init_state; + enum fpga_mgr_states op_write_state; + enum fpga_mgr_states op_write_sg_state; + enum fpga_mgr_states op_write_complete_state; +}; + +struct mgr_ctx { + struct fpga_image_info *img_info; + struct fpga_manager *mgr; + struct platform_device *pdev; + struct mgr_stats stats; +}; + +/** + * init_test_buffer() - Allocate and initialize a test image in a buffer. + * @test: KUnit test context object. + * @count: image size in bytes. + * + * Return: pointer to the newly allocated image. + */ +static char *init_test_buffer(struct kunit *test, size_t count) +{ + char *buf; + + KUNIT_ASSERT_GE(test, count, HEADER_SIZE); + + buf = kunit_kzalloc(test, count, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + memset(buf, HEADER_FILL, HEADER_SIZE); + memset(buf + HEADER_SIZE, IMAGE_FILL, count - HEADER_SIZE); + + return buf; +} + +/* + * Check the image header. Do not return an error code if the image check fails + * since, in this case, it is a failure of the FPGA manager itself, not this + * op that tests it. + */ +static int op_parse_header(struct fpga_manager *mgr, struct fpga_image_info *info, + const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + size_t i; + + stats->op_parse_header_state = mgr->state; + stats->op_parse_header_seq = stats->seq_num++; + + /* Set header_size and data_size for later */ + info->header_size = HEADER_SIZE; + info->data_size = info->count - HEADER_SIZE; + + stats->header_match = true; + for (i = 0; i < info->header_size; i++) { + if (buf[i] != HEADER_FILL) { + stats->header_match = false; + goto out; + } + } + +out: + return 0; +} + +static int op_write_init(struct fpga_manager *mgr, struct fpga_image_info *info, + const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + + stats->op_write_init_state = mgr->state; + stats->op_write_init_seq = stats->seq_num++; + + return 0; +} + +/* + * Check the image data. As with, op_parse_header do not return an error code + * if the image check fails. + */ +static int op_write(struct fpga_manager *mgr, const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + size_t i; + + stats->op_write_state = mgr->state; + stats->op_write_seq = stats->seq_num++; + + stats->image_match = true; + for (i = 0; i < count; i++) { + if (buf[i] != IMAGE_FILL) { + stats->image_match = false; + goto out; + } + } + +out: + return 0; +} + +/* + * Check the image data, but first skip the header since write_sg will get + * the whole image in sg_table. As with op_write, do not return an error code + * if the image check fails. + */ +static int op_write_sg(struct fpga_manager *mgr, struct sg_table *sgt) +{ + struct mgr_stats *stats = mgr->priv; + struct sg_mapping_iter miter; + char *img; + size_t i; + + stats->op_write_sg_state = mgr->state; + stats->op_write_sg_seq = stats->seq_num++; + + stats->image_match = true; + sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG); + + if (!sg_miter_skip(&miter, HEADER_SIZE)) { + stats->image_match = false; + goto out; + } + + while (sg_miter_next(&miter)) { + img = miter.addr; + for (i = 0; i < miter.length; i++) { + if (img[i] != IMAGE_FILL) { + stats->image_match = false; + goto out; + } + } + } +out: + sg_miter_stop(&miter); + return 0; +} + +static int op_write_complete(struct fpga_manager *mgr, struct fpga_image_info *info) +{ + struct mgr_stats *stats = mgr->priv; + + stats->op_write_complete_state = mgr->state; + stats->op_write_complete_seq = stats->seq_num++; + + return 0; +} + +/* + * Fake FPGA manager that implements all ops required to check the programming + * sequence using a single contiguous buffer and a scatter gather table. + */ +static const struct fpga_manager_ops fake_mgr_ops = { + .skip_header = true, + .parse_header = op_parse_header, + .write_init = op_write_init, + .write = op_write, + .write_sg = op_write_sg, + .write_complete = op_write_complete, +}; + +static void fpga_mgr_test_get(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + struct fpga_manager *mgr; + + mgr = fpga_mgr_get(&ctx->pdev->dev); + KUNIT_EXPECT_PTR_EQ(test, mgr, ctx->mgr); + + fpga_mgr_put(ctx->mgr); +} + +static void fpga_mgr_test_lock(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + int ret; + + ret = fpga_mgr_lock(ctx->mgr); + KUNIT_EXPECT_EQ(test, ret, 0); + + ret = fpga_mgr_lock(ctx->mgr); + KUNIT_EXPECT_EQ(test, ret, -EBUSY); + + fpga_mgr_unlock(ctx->mgr); +} + +/* Check the programming sequence using an image in a buffer */ +static void fpga_mgr_test_img_load_buf(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + char *img_buf; + int ret; + + img_buf = init_test_buffer(test, IMAGE_SIZE); + + ctx->img_info->count = IMAGE_SIZE; + ctx->img_info->buf = img_buf; + + ret = fpga_mgr_load(ctx->mgr, ctx->img_info); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, ctx->stats.header_match); + KUNIT_EXPECT_TRUE(test, ctx->stats.image_match); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_parse_header_state, FPGA_MGR_STATE_PARSE_HEADER); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_state, FPGA_MGR_STATE_WRITE_INIT); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_state, FPGA_MGR_STATE_WRITE); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_state, FPGA_MGR_STATE_WRITE_COMPLETE); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_seq, ctx->stats.op_parse_header_seq + 1); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_seq, ctx->stats.op_parse_header_seq + 2); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_seq, ctx->stats.op_parse_header_seq + 3); +} + +/* Check the programming sequence using an image in a scatter gather table */ +static void fpga_mgr_test_img_load_sgt(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + struct sg_table *sgt; + char *img_buf; + int ret; + + img_buf = init_test_buffer(test, IMAGE_SIZE); + + sgt = kunit_kzalloc(test, sizeof(*sgt), GFP_KERNEL); + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + KUNIT_ASSERT_EQ(test, ret, 0); + sg_init_one(sgt->sgl, img_buf, IMAGE_SIZE); + + ctx->img_info->sgt = sgt; + + ret = fpga_mgr_load(ctx->mgr, ctx->img_info); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, ctx->stats.header_match); + KUNIT_EXPECT_TRUE(test, ctx->stats.image_match); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_parse_header_state, FPGA_MGR_STATE_PARSE_HEADER); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_state, FPGA_MGR_STATE_WRITE_INIT); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_sg_state, FPGA_MGR_STATE_WRITE); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_state, FPGA_MGR_STATE_WRITE_COMPLETE); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_seq, ctx->stats.op_parse_header_seq + 1); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_sg_seq, ctx->stats.op_parse_header_seq + 2); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_seq, ctx->stats.op_parse_header_seq + 3); + + sg_free_table(ctx->img_info->sgt); +} + +static int fpga_mgr_test_init(struct kunit *test) +{ + struct mgr_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + ctx->pdev = platform_device_register_simple("mgr_pdev", PLATFORM_DEVID_AUTO, NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->pdev); + + ctx->mgr = devm_fpga_mgr_register(&ctx->pdev->dev, "Fake FPGA Manager", &fake_mgr_ops, + &ctx->stats); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->mgr)); + + ctx->img_info = fpga_image_info_alloc(&ctx->pdev->dev); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->img_info); + + test->priv = ctx; + + return 0; +} + +static void fpga_mgr_test_exit(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + + fpga_image_info_free(ctx->img_info); + platform_device_unregister(ctx->pdev); +} + +static struct kunit_case fpga_mgr_test_cases[] = { + KUNIT_CASE(fpga_mgr_test_get), + KUNIT_CASE(fpga_mgr_test_lock), + KUNIT_CASE(fpga_mgr_test_img_load_buf), + KUNIT_CASE(fpga_mgr_test_img_load_sgt), + {} +}; + +static struct kunit_suite fpga_mgr_suite = { + .name = "fpga_mgr", + .init = fpga_mgr_test_init, + .exit = fpga_mgr_test_exit, + .test_cases = fpga_mgr_test_cases, +}; + +kunit_test_suite(fpga_mgr_suite); + +MODULE_LICENSE("GPL"); From patchwork Thu Jul 13 16:27:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 120046 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1964457vqm; Thu, 13 Jul 2023 10:07:46 -0700 (PDT) X-Google-Smtp-Source: APBJJlFd+rIxmcE/s1l3tjbdDD1aVWFeJBD5ry8+wisPbvb2UxeSl3n5BLdrE+cZFltkJmD+rIrL X-Received: by 2002:a2e:95d0:0:b0:2b6:9909:79bd with SMTP id y16-20020a2e95d0000000b002b6990979bdmr1849135ljh.24.1689268066577; Thu, 13 Jul 2023 10:07:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689268066; cv=none; d=google.com; s=arc-20160816; b=SX79VpArRAV8EANxfoRMj4jl1aV7Va6jJDhut86i3XzeH/ZRNW7P1JqCFotPkXJt8F kW77f8HtD/SVZ8RKpbj183i/hF4rDApFiMcpvhKrNiU7DTbpdqP9PzqPj94pUyolsPvI A9cxLH5MLoMMjK7mI+PlHNuMl3jfZ6ChvQ7ju7Bsl+AWxGU6n21rK2odwkPiop4wdG48 3UwReFeKdpTd9vYz8HpMRHrONpFryfwORDpGkHwzZO9RAJ98TynoOhoQ/GjwNHZgNtXv TFPuNS5G/9YNqOHMwxVFBdY6NTtFn6lP/EbYNiqM7dyG4nJXSUg2eUKrpb4CgRCZKT6f nrOQ== 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=8wZhu53F4F7gIlc8a4am6xpj3s/cLtV4xrOxNFzw7tE=; fh=M37Ydd+BXMWH9O/XP+uN5l3PQrnD/ZjHRj9NyKzE96w=; b=Dm56AiZXDEMuFZUQHKkLFG8LrDLnJ5cZy+iXEF+KswqLpMrfELQ26uuJejdsDoeYsv WsNzxdBvKzLmDiK0YAz3ybsm5m417Zr9xffUuKDt7LgsHcfUyxN3Ntm9Rj3AQu4n73Wx XYXI4uXGAT5FzmBSfpKH9pgQ2FWRjpyhgMrHRyG6DOKxuFVD4o54Kh+4f4tNlS3Nija0 cRXO+1ZfwSu5HrcPSrVC6NDaFB02waXxBLY/XxFGiEHBT3yKjgpsHiBETDgKrKYXC3+S 242pZaLzplEA7PeDM8yuSykcbkGzR4JSnPjOdM/Gl61JIS8UfkEtcfOwJPpLS+MYmhWz ULnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eq1xcY3i; 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 b11-20020a170906490b00b0099364d9f0easi7910089ejq.479.2023.07.13.10.07.18; Thu, 13 Jul 2023 10:07:46 -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=eq1xcY3i; 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 S235199AbjGMQ2l (ORCPT + 99 others); Thu, 13 Jul 2023 12:28:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232307AbjGMQ2i (ORCPT ); Thu, 13 Jul 2023 12:28:38 -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 E7DD8273B for ; Thu, 13 Jul 2023 09:27:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689265675; 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=8wZhu53F4F7gIlc8a4am6xpj3s/cLtV4xrOxNFzw7tE=; b=eq1xcY3i7UKx62RiYTn9XLpvFIlOY9b20jGR/zU3xeNPZyAKTX7fEFpCb2+flEi0ixH/S5 5q4G2ll4ksli1hCS61mjk48/KOm4UL7rR3xb5ZzyMk/AzqitvGurAaCxBoPc9/gc0rG0Y6 Bq0x6him6yxtINODPicsJKo+3/te3fo= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-498-8W8LH28yO6K9etXAGNZIgQ-1; Thu, 13 Jul 2023 12:27:54 -0400 X-MC-Unique: 8W8LH28yO6K9etXAGNZIgQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-635e0889cc5so9464796d6.2 for ; Thu, 13 Jul 2023 09:27:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689265673; x=1691857673; 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=8wZhu53F4F7gIlc8a4am6xpj3s/cLtV4xrOxNFzw7tE=; b=evtyipurf3DMkem6opNGggFATP6zkhFqBzboMj1kRzXgLeHB7a6clM0jwDThWv6lCM hX0Lsm+TfRK/Oa+MsKSJf9x946TosfUnGfgzOEdL1e4sU/qX8zoYiul/PFrLZXDnNCZs XA4T8cRkgZtgk6wocnYswSfHqiBjZ4inJzyVVRxiArl43advSKAdtIhHA7v0AhfsrsQ1 UpHvq2afsoAX862iLX8f/rXlJ2oRguVqkm4K+MfjnyVq4DjyOQHfYNKaCxP7tbriTQ8e sx5viHdyWLEnMWax+UA++VMYVcD3DxhyFV7JlBN8N8GW1QRIFV23T9v+bASAiD49iQ2S aW4g== X-Gm-Message-State: ABy/qLYvedg55fwO4AXZEqMBQy64EtDVmZo0i0bhSCnfVvozzI+xF8QI asH3PdG31eroypeNcWb/iFSN5AhZLMEtm5HRfJs15JNPw4pl8TaoKLbyACb7bbCJGBkzBhc82Rj 4P/JN6qBWZA9+IgO52091K1E= X-Received: by 2002:a0c:b2d7:0:b0:635:e261:798d with SMTP id d23-20020a0cb2d7000000b00635e261798dmr1972032qvf.52.1689265673634; Thu, 13 Jul 2023 09:27:53 -0700 (PDT) X-Received: by 2002:a0c:b2d7:0:b0:635:e261:798d with SMTP id d23-20020a0cb2d7000000b00635e261798dmr1972019qvf.52.1689265673407; Thu, 13 Jul 2023 09:27:53 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-24-242.cust.vodafonedsl.it. [2.34.24.242]) by smtp.gmail.com with ESMTPSA id u17-20020a0cf1d1000000b00637873ff0f3sm3206582qvl.15.2023.07.13.09.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 09:27:53 -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 v9 2/4] fpga: add an initial KUnit suite for the FPGA Bridge Date: Thu, 13 Jul 2023 18:27:29 +0200 Message-ID: <20230713162731.211669-3-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230713162731.211669-1-marpagan@redhat.com> References: <20230713162731.211669-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_BLOCKED,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771325951957429068 X-GMAIL-MSGID: 1771325951957429068 The suite tests the basic behaviors of the FPGA Bridge including the functions that operate on a list of bridges. Signed-off-by: Marco Pagani Acked-by: Xu Yilun --- drivers/fpga/tests/fpga-bridge-test.c | 175 ++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 drivers/fpga/tests/fpga-bridge-test.c diff --git a/drivers/fpga/tests/fpga-bridge-test.c b/drivers/fpga/tests/fpga-bridge-test.c new file mode 100644 index 000000000000..1d258002cdd7 --- /dev/null +++ b/drivers/fpga/tests/fpga-bridge-test.c @@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the FPGA Bridge + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include + +struct bridge_stats { + bool enable; +}; + +struct bridge_ctx { + struct fpga_bridge *bridge; + struct platform_device *pdev; + struct bridge_stats stats; +}; + +static int op_enable_set(struct fpga_bridge *bridge, bool enable) +{ + struct bridge_stats *stats = bridge->priv; + + stats->enable = enable; + + return 0; +} + +/* + * Fake FPGA bridge that implements only the enable_set op to track + * the state. + */ +static const struct fpga_bridge_ops fake_bridge_ops = { + .enable_set = op_enable_set, +}; + +/** + * register_test_bridge() - Register a fake FPGA bridge for testing. + * @test: KUnit test context object. + * + * Return: Context of the newly registered FPGA bridge. + */ +static struct bridge_ctx *register_test_bridge(struct kunit *test) +{ + struct bridge_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + ctx->pdev = platform_device_register_simple("bridge_pdev", PLATFORM_DEVID_AUTO, NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->pdev); + + ctx->bridge = fpga_bridge_register(&ctx->pdev->dev, "Fake FPGA bridge", &fake_bridge_ops, + &ctx->stats); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge)); + + return ctx; +} + +static void unregister_test_bridge(struct bridge_ctx *ctx) +{ + fpga_bridge_unregister(ctx->bridge); + platform_device_unregister(ctx->pdev); +} + +static void fpga_bridge_test_get(struct kunit *test) +{ + struct bridge_ctx *ctx = test->priv; + struct fpga_bridge *bridge; + + bridge = fpga_bridge_get(&ctx->pdev->dev, NULL); + KUNIT_EXPECT_PTR_EQ(test, bridge, ctx->bridge); + + bridge = fpga_bridge_get(&ctx->pdev->dev, NULL); + KUNIT_EXPECT_EQ(test, PTR_ERR(bridge), -EBUSY); + + fpga_bridge_put(ctx->bridge); +} + +static void fpga_bridge_test_toggle(struct kunit *test) +{ + struct bridge_ctx *ctx = test->priv; + int ret; + + ret = fpga_bridge_disable(ctx->bridge); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_FALSE(test, ctx->stats.enable); + + ret = fpga_bridge_enable(ctx->bridge); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_TRUE(test, ctx->stats.enable); +} + +/* Test the functions for getting and controlling a list of bridges */ +static void fpga_bridge_test_get_put_list(struct kunit *test) +{ + struct list_head bridge_list; + struct bridge_ctx *ctx_0, *ctx_1; + int ret; + + ctx_0 = test->priv; + ctx_1 = register_test_bridge(test); + + INIT_LIST_HEAD(&bridge_list); + + /* Get bridge 0 and add it to the list */ + ret = fpga_bridge_get_to_list(&ctx_0->pdev->dev, NULL, &bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_PTR_EQ(test, ctx_0->bridge, + list_first_entry_or_null(&bridge_list, struct fpga_bridge, node)); + + /* Get bridge 1 and add it to the list */ + ret = fpga_bridge_get_to_list(&ctx_1->pdev->dev, NULL, &bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_PTR_EQ(test, ctx_1->bridge, + list_first_entry_or_null(&bridge_list, struct fpga_bridge, node)); + + /* Disable an then enable both bridges from the list */ + ret = fpga_bridges_disable(&bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_FALSE(test, ctx_0->stats.enable); + KUNIT_EXPECT_FALSE(test, ctx_1->stats.enable); + + ret = fpga_bridges_enable(&bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, ctx_0->stats.enable); + KUNIT_EXPECT_TRUE(test, ctx_1->stats.enable); + + /* Put and remove both bridges from the list */ + fpga_bridges_put(&bridge_list); + + KUNIT_EXPECT_TRUE(test, list_empty(&bridge_list)); + + unregister_test_bridge(ctx_1); +} + +static int fpga_bridge_test_init(struct kunit *test) +{ + test->priv = register_test_bridge(test); + + return 0; +} + +static void fpga_bridge_test_exit(struct kunit *test) +{ + unregister_test_bridge(test->priv); +} + +static struct kunit_case fpga_bridge_test_cases[] = { + KUNIT_CASE(fpga_bridge_test_get), + KUNIT_CASE(fpga_bridge_test_toggle), + KUNIT_CASE(fpga_bridge_test_get_put_list), + {} +}; + +static struct kunit_suite fpga_bridge_suite = { + .name = "fpga_bridge", + .init = fpga_bridge_test_init, + .exit = fpga_bridge_test_exit, + .test_cases = fpga_bridge_test_cases, +}; + +kunit_test_suite(fpga_bridge_suite); + +MODULE_LICENSE("GPL"); From patchwork Thu Jul 13 16:27:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 120045 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1964363vqm; Thu, 13 Jul 2023 10:07:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlETwQjEFuhm1R4jJuD5LINVk2iuW9S/9uq19akW+egvciWYilzWOu4cEuHfcnBQRjU8mEJv X-Received: by 2002:a17:907:138f:b0:97d:2bcc:47d5 with SMTP id vs15-20020a170907138f00b0097d2bcc47d5mr1888162ejb.49.1689268058187; Thu, 13 Jul 2023 10:07:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689268058; cv=none; d=google.com; s=arc-20160816; b=uC3fEpj/UpFNxZf8UBs0ftO1jX2isXglhQTrGZ8naZhy8Ri8Gw5hDS3BMzIIQDPDzF YaArFJ6b9eiZ27kJYQNk+mKcNY7k9R0jHiaDNlqEIG9HBFB11IGB24l4IV4RlNZ6CxLt oNqae1J66BSN2LF+NXKGEj/jQxZ/VtzeNWUfFs1qfaXIAtlLDic3VOVgxyTBiEYkYPo2 tkxCv4EUoN9BFuapOfd1wjdUT8NpTqHLWS3N8LkRJZHDDA3dCmYD58simHCR6XZ/XyLY D+3dU+8y8jR/4zXEc05KkZIPsbSFJRibGKyGAh8V36L1ez9y/8IT8SoxZ04dven6odQ4 z9+g== 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=QKrzGanyiyC099mjlFA5dr2WMY6zoHlA/Ps7wwq3mKc=; fh=M37Ydd+BXMWH9O/XP+uN5l3PQrnD/ZjHRj9NyKzE96w=; b=qQL6yUF8fBhQIP0B38E99WDC8M2NWArxbXQaxFLlYj6zhzHHMRO53iEJmpMQz+Udv7 5kKGL+OhfXd2v9DD6gFMrTG+9FdyuVxABmpZWQqOVMUjueVwYY1CuoU8sgCwXnP6R1SC OBr1cZO6qIpFIVniL1hE/2lREorE0TrVYcwOyA4DopgoEhkBPeQ7HqdZWfKdoODpexyD oFJrnDsM/fLjcqqguPLzHZN6Yq/TeZHSTq6ReQ+fF2RtjzJki0sWN8o+2WpggaoJWPCT TkwMlQbJnAeE4+8KCepmie5IJ9ADx8G566ZkLBqIUd+0k34DZhI49Z030u53MOnxOVhi gyFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CE9zNZ1V; 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 jz13-20020a170906bb0d00b00992d730e99dsi7270747ejb.494.2023.07.13.10.07.11; Thu, 13 Jul 2023 10:07:38 -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=CE9zNZ1V; 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 S235189AbjGMQ2y (ORCPT + 99 others); Thu, 13 Jul 2023 12:28:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234845AbjGMQ2x (ORCPT ); Thu, 13 Jul 2023 12:28:53 -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 0C4E82D48 for ; Thu, 13 Jul 2023 09:28:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689265682; 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=QKrzGanyiyC099mjlFA5dr2WMY6zoHlA/Ps7wwq3mKc=; b=CE9zNZ1Vh1m/tadz+yFt5ilxcE0nOOc7Ro2lZjztDOIJqhjxTEpJZ5USjw9LgJ5wL8iaAh ja4fB7Y+2C9Vl1Ogif0QVvyc9R+8E1UtYgPWPdiK4WrWRrTQ2Ikn4TNEQT2YZhZ5VafyLS iivOdSm9oDLFzHFdg4VTTX8FNSIKgPE= Received: from mail-ua1-f72.google.com (mail-ua1-f72.google.com [209.85.222.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-278-uS_nGkuyPOWnyQ1E_6rU1w-1; Thu, 13 Jul 2023 12:28:00 -0400 X-MC-Unique: uS_nGkuyPOWnyQ1E_6rU1w-1 Received: by mail-ua1-f72.google.com with SMTP id a1e0cc1a2514c-78a5bbabf67so221771241.0 for ; Thu, 13 Jul 2023 09:28:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689265680; x=1691857680; 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=QKrzGanyiyC099mjlFA5dr2WMY6zoHlA/Ps7wwq3mKc=; b=VOyU20qf6QFlqQl7lHet1Z00+GHDGvGOZmYeTs0Qxsf/zSUl6J1qhUB8HYsOLKQZxr MISBOLBPK2+hVg9Rc/s+nuwn9e+at8b9jkY5TmS5GgaoKQVz6PJATc29cB+jCJjyDvY5 XYZLfYA3R2DfjeUYDIfJ2RrfTuT++rIuwU5SQ+sa9uNHP6wRT65/3gXKCQ4V4hiA5slC UE+3alzjvVrn87EUVAfaeIBzkrKKbR4yiK6KftjxCvo4zfHAIP4AZhuHiiALhG103O+b iOvK6v38P9FXIlVljs775WhERToHAu39diopB9uxkWh/z3T6BWSLRF5iy8CFuc2frWZv fvow== X-Gm-Message-State: ABy/qLZCuMOuJoYKndP4Rv3g7WyTD1zgklnBfECf9Nem+280Cw81NSaG tVaY+eKN6BflyPIHDa6lGGq38d045+QDgpCn7aduKQDa7TbfzuUgvwq0yFoPBScR3j2T1XfAyKk i046jlqOG+vuwLZsEDx/0eaw= X-Received: by 2002:a67:fa09:0:b0:443:a8dd:c416 with SMTP id i9-20020a67fa09000000b00443a8ddc416mr1504657vsq.8.1689265680371; Thu, 13 Jul 2023 09:28:00 -0700 (PDT) X-Received: by 2002:a67:fa09:0:b0:443:a8dd:c416 with SMTP id i9-20020a67fa09000000b00443a8ddc416mr1504647vsq.8.1689265680122; Thu, 13 Jul 2023 09:28:00 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-24-242.cust.vodafonedsl.it. [2.34.24.242]) by smtp.gmail.com with ESMTPSA id u17-20020a0cf1d1000000b00637873ff0f3sm3206582qvl.15.2023.07.13.09.27.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 09:27:59 -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 v9 3/4] fpga: add an initial KUnit suite for the FPGA Region Date: Thu, 13 Jul 2023 18:27:30 +0200 Message-ID: <20230713162731.211669-4-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230713162731.211669-1-marpagan@redhat.com> References: <20230713162731.211669-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_BLOCKED,RCVD_IN_MSPIKE_H4,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: INBOX X-GMAIL-THRID: 1771325943538356467 X-GMAIL-MSGID: 1771325943538356467 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..9f9d50ee7871 --- /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 FPGA 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 FPGA 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); +} + +/* + * FPGA 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"); From patchwork Thu Jul 13 16:27:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 120036 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1963302vqm; Thu, 13 Jul 2023 10:06:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlHOX0MuvyiAdxOWJRnRBHG3tZskDie3ry6We8Z08qC8OKPWcz6tE05WS02l41ge4o+SgUsj X-Received: by 2002:a17:906:ad5:b0:992:345e:8319 with SMTP id z21-20020a1709060ad500b00992345e8319mr1641337ejf.58.1689267970891; Thu, 13 Jul 2023 10:06:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689267970; cv=none; d=google.com; s=arc-20160816; b=jdnMmabHEPzAUpj59zc+99O0dsED2FNljRIyKgZeItY+W5t91V7mehi755yVN4o9oe 0itnlh3JsFC8o7HRmy7bV25D9kxXYuNVVqsOiY12zCmLHwJuIGX9HKxNEk8FtLrW5+la rBvdMOZSM7R3HzME8zQvDcp0WuWc7TOTp49jjdYFWYuBFOYJBE0EMXQz858O9DhlaU24 tM3RphfgY8SyufcOF4j2iNyjFaiOZotHIzThPmOZi8teM9WqHmJPIH7HsOEIM2+IgUiz YiBBLq+XK7jlxwhmHoubmJi5DVy1iY4YNpOlALKr3VoCsTI5C1ckHhEGltAXRi51EOVu tk+w== 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=wWVveJ30j+awpC6r3B537VP9iS5QEq83jfrgh734Zoc=; fh=M37Ydd+BXMWH9O/XP+uN5l3PQrnD/ZjHRj9NyKzE96w=; b=f7uhhHUM2XQAYSGiT0JaBeA0s5UeET5BVrozN1yRHmOsmNB4spdYCbL3/Rczg87Pcs Url18Yk3x5hX375Ze8n6PIfzc7cZP0oU0O6Vqi8rvatzk5fRaA4fCt4W/yd4u9cIrUO7 z1G6fI21O0bRXair6T33DFXtCQipq8As44H4gJjosDsyJVLoFSMCG1PXQbhM8U1l+fdH MIyJT5lr2yJ8i5VhIWY8gm7Jb3IWwID3A0aiyZHCty+8JO2k8tUlKFHx61OJyxT8pF/U 9ipKrC4f0LPfQWFAp0UON+VoM19NrdyIyEF8ddbrLj7MJP+xP3ksJATdnJVY1TdV9etZ /Jeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cIJif7q8; 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 lw27-20020a170906bcdb00b00992b75dedafsi7738593ejb.507.2023.07.13.10.05.37; Thu, 13 Jul 2023 10:06:10 -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=cIJif7q8; 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 S235243AbjGMQ3C (ORCPT + 99 others); Thu, 13 Jul 2023 12:29:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235222AbjGMQ26 (ORCPT ); Thu, 13 Jul 2023 12:28:58 -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 8FBF02D40 for ; Thu, 13 Jul 2023 09:28:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689265689; 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=wWVveJ30j+awpC6r3B537VP9iS5QEq83jfrgh734Zoc=; b=cIJif7q8EEYMJQi8b2sniU7ZABGeoH5QphxSg1h+D3iv67tv+1VtQU1ZDbUnVtfLGy6tv5 zxbHIkO9zINCuI98tekU+xp9dyu6XpzRUGSn8OU35Hd5H2gMMFKHFgL4skhlJmXP41BkWG W4LjWFBgDa/aLKvJ3A1kLcZGgIVoWik= Received: from mail-oo1-f71.google.com (mail-oo1-f71.google.com [209.85.161.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-568-q2m5ndA5NH6fTIsR1_sJyA-1; Thu, 13 Jul 2023 12:28:08 -0400 X-MC-Unique: q2m5ndA5NH6fTIsR1_sJyA-1 Received: by mail-oo1-f71.google.com with SMTP id 006d021491bc7-56364632e59so1382607eaf.1 for ; Thu, 13 Jul 2023 09:28:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689265688; x=1691857688; 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=wWVveJ30j+awpC6r3B537VP9iS5QEq83jfrgh734Zoc=; b=jKf3kKFeoi1PXjgIgEjnNM+8wuwnrZbsETR9XBxp5FZdaddQmDA7hLv25TX9BdBG9S uaVU6Xg3MO6nyyys/z/8u2xf1G/IOhqXcWGEqiVm21+gnEleTOI4aX+A+lvaDXGnBHhp LH+G8FHF7kY3L0IIadylsCvlhqBbzXDILwOVU76DK/EO2rX9Ap4qgM7K+Wk+dCTYXvOy 7fmwqqusQK+/UR/jOEaBy0K38mS9to19prwQWfCfZb8oHYAA3AFf1qdlWUOiZns5rRID Al45woA2PStWQQmeiQRQZHHB7d4MREcxyYZyQi7rpVaPcRquV2/0ZFZVeNVXhuCFDiGh KiYA== X-Gm-Message-State: ABy/qLZ5O6Oxz4u143yLCPJHu+JsPP2GBSfOyBGIU9jJokJa14Y3U/jd PN7tOg5TvJbRXpgG0nPNGNzvein5IoyyZlbv7d/91z3kh/Kfb5+Bel91yrOwYksBtKKqVBtOchv LxnwVWKMP96MS7tAMg45KK4M= X-Received: by 2002:a05:6358:5e0c:b0:12b:ed77:8b66 with SMTP id q12-20020a0563585e0c00b0012bed778b66mr2046491rwn.7.1689265687774; Thu, 13 Jul 2023 09:28:07 -0700 (PDT) X-Received: by 2002:a05:6358:5e0c:b0:12b:ed77:8b66 with SMTP id q12-20020a0563585e0c00b0012bed778b66mr2046469rwn.7.1689265687405; Thu, 13 Jul 2023 09:28:07 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-24-242.cust.vodafonedsl.it. [2.34.24.242]) by smtp.gmail.com with ESMTPSA id u17-20020a0cf1d1000000b00637873ff0f3sm3206582qvl.15.2023.07.13.09.28.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 09:28:07 -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 v9 4/4] fpga: add configuration for the FPGA KUnit test suites. Date: Thu, 13 Jul 2023 18:27:31 +0200 Message-ID: <20230713162731.211669-5-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230713162731.211669-1-marpagan@redhat.com> References: <20230713162731.211669-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_BLOCKED,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771325851811667518 X-GMAIL-MSGID: 1771325851811667518 Add configuration for the KUnit test suites for the core components of the FPGA subsystem. Signed-off-by: Marco Pagani Acked-by: Xu Yilun --- drivers/fpga/Kconfig | 2 ++ drivers/fpga/Makefile | 3 +++ drivers/fpga/tests/.kunitconfig | 5 +++++ drivers/fpga/tests/Kconfig | 11 +++++++++++ drivers/fpga/tests/Makefile | 6 ++++++ 5 files changed, 27 insertions(+) create mode 100644 drivers/fpga/tests/.kunitconfig create mode 100644 drivers/fpga/tests/Kconfig create mode 100644 drivers/fpga/tests/Makefile diff --git a/drivers/fpga/Kconfig b/drivers/fpga/Kconfig index 0a00763b9f28..2f689ac4ba3a 100644 --- a/drivers/fpga/Kconfig +++ b/drivers/fpga/Kconfig @@ -276,4 +276,6 @@ config FPGA_MGR_LATTICE_SYSCONFIG_SPI FPGA manager driver support for Lattice FPGAs programming over slave SPI sysCONFIG interface. +source "drivers/fpga/tests/Kconfig" + endif # FPGA diff --git a/drivers/fpga/Makefile b/drivers/fpga/Makefile index 72e554b4d2f7..352a2612623e 100644 --- a/drivers/fpga/Makefile +++ b/drivers/fpga/Makefile @@ -55,3 +55,6 @@ obj-$(CONFIG_FPGA_DFL_NIOS_INTEL_PAC_N3000) += dfl-n3000-nios.o # Drivers for FPGAs which implement DFL obj-$(CONFIG_FPGA_DFL_PCI) += dfl-pci.o + +# KUnit tests +obj-$(CONFIG_FPGA_KUNIT_TESTS) += tests/ diff --git a/drivers/fpga/tests/.kunitconfig b/drivers/fpga/tests/.kunitconfig new file mode 100644 index 000000000000..a1c2a2974c39 --- /dev/null +++ b/drivers/fpga/tests/.kunitconfig @@ -0,0 +1,5 @@ +CONFIG_KUNIT=y +CONFIG_FPGA=y +CONFIG_FPGA_REGION=y +CONFIG_FPGA_BRIDGE=y +CONFIG_FPGA_KUNIT_TESTS=y diff --git a/drivers/fpga/tests/Kconfig b/drivers/fpga/tests/Kconfig new file mode 100644 index 000000000000..e4a64815f16d --- /dev/null +++ b/drivers/fpga/tests/Kconfig @@ -0,0 +1,11 @@ +config FPGA_KUNIT_TESTS + tristate "KUnit test for the FPGA subsystem" if !KUNIT_ALL_TESTS + depends on FPGA && FPGA_REGION && FPGA_BRIDGE && KUNIT=y + default KUNIT_ALL_TESTS + help + This builds unit tests for the FPGA subsystem + + For more information on KUnit and unit tests in general, + please refer to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. diff --git a/drivers/fpga/tests/Makefile b/drivers/fpga/tests/Makefile new file mode 100644 index 000000000000..bb78215c645c --- /dev/null +++ b/drivers/fpga/tests/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for KUnit test suites for the FPGA subsystem +# + +obj-$(CONFIG_FPGA_KUNIT_TESTS) += fpga-mgr-test.o fpga-bridge-test.o fpga-region-test.o