Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3913032rwe; Mon, 17 Apr 2023 05:34:43 -0700 (PDT) X-Google-Smtp-Source: AKy350a1jQ65s+pDW7nIkO+1r2XsrdfwLJLLhG5+nl7h1WCZWne+t0jlY0J2609n6YoFI22ysAcp X-Received: by 2002:a05:6a20:a11e:b0:ef:b596:2fb6 with SMTP id q30-20020a056a20a11e00b000efb5962fb6mr3855590pzk.36.1681734883630; Mon, 17 Apr 2023 05:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681734883; cv=none; d=google.com; s=arc-20160816; b=HQTApceLDT9gkqzQ8wPT48Fx1pxQx4vMgMMCD3803jR8pG8eyo65/FDZ5jx+GL+ppS RszfB99Qr5ZwAdxI6sgk3DriBIOanc0nMsDftu4OBrrQaL0ro4nSI65BSnjVpnYe6D/v 6StjR+YCZO6aS+C+ZdzY5O/I+0YO8wlP2EunTrZZtMmVMF+9lcCsYQWIC0dtHCj+Fp1/ V1WxN1ghnViPeLEK/qQnm6w+o0y4QhD9cfqjikUP2g1L5FKnh514eqWLf5+Q3qtIPR0U GaI9OSdaWFORAgPIkAtQcHL77ihCGxVcW0lrp7AKi81YTqjRKgmBrbRU1ok+X24wtog0 xf/Q== 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=p9EM7KUyJwVaEwxyA/Y+8HQEJUTbAzizsc3Hf5VvwRo=; b=zRMhbXKbulI3axQ21uaAqfPIUTF/tejXbDH3g3F3BmPvfPbXhFkOh7jHdMOolCTLBd QMceZL6Cewo7pQBkZnezjIq6GUU/U18FIejalBggE1yYfWjoIMEjmF/pODJ3Q8qb8tVe ykQDJESQ66coj4r0dmX3o5kAQAM03zmx2cRutnl7VbdekclEkwXb47do1g4jdvrr/QDw YbMSIhqI7pQeUgqBy7J1uM0DYGko9PQdVNGj2KVa0z8Uvp1M6fLYdVQ1AIBkmGzK6sSI 83KnOGxLGwzO7XHLtAIv1xShtZuRwyk6LAiIDlPjRzO2i3J3/TDTUEjGG/otJDhHleJT GyDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MYCAO6rW; 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 5-20020a620505000000b0063afd8a5b8dsi11065199pff.186.2023.04.17.05.34.29; Mon, 17 Apr 2023 05:34:43 -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=MYCAO6rW; 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 S230514AbjDQMZR (ORCPT + 99 others); Mon, 17 Apr 2023 08:25:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230502AbjDQMZN (ORCPT ); Mon, 17 Apr 2023 08:25:13 -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 61B3C44B0 for ; Mon, 17 Apr 2023 05:23:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681734199; 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=p9EM7KUyJwVaEwxyA/Y+8HQEJUTbAzizsc3Hf5VvwRo=; b=MYCAO6rWM9+NSl++7kt7jsSbm16sZdyML4PeI1DohJtQNEHlXEttr1EN7HQzZf4fq737FB ZbQ1j/mPZRYWEpUIsFb6JeAjcip/w9JGZWgvcOEKDJzI8lBwyJ4c7ebDce9CATGlbAQgvQ SxuX6y/xwe+lcd9JfdOiFwDcCg5lAQQ= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-443-wALICmA5OXedtF8MT-t0Dg-1; Mon, 17 Apr 2023 08:23:17 -0400 X-MC-Unique: wALICmA5OXedtF8MT-t0Dg-1 Received: by mail-ed1-f70.google.com with SMTP id 4fb4d7f45d1cf-505149e1a4eso10315652a12.1 for ; Mon, 17 Apr 2023 05:23:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681734196; x=1684326196; 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=p9EM7KUyJwVaEwxyA/Y+8HQEJUTbAzizsc3Hf5VvwRo=; b=baVZ72FcEvoLeuZaSowEKZTddnGsseyfjqOYlrw5EXa2zfQhMZElglvRI5uSB2Hn1e EnXjflgh+pPbsyx6xt6EA4KkmBXMZIKjWWA/25WDWW7GMpSf+VfDsoMUVWRdCzFU9fNm WtvLjf083NTWDbXts6dfav0aSKmrFRMGTIHvw6hN0VuK88AqGTdSdvb76FtaHkwsRoFv upB19Mb8/sJD91GqQ/hOceLhptynX97CeImBMHaHB5it92QbKHYEmbVz5jrCOtzKqvs4 DWTy3WGCj7lbeWA5iUD7hDy3dLVInXj6FkH1Zr7HMaCsAO6v+j5+Ss3mDrULnId/v1ER aINw== X-Gm-Message-State: AAQBX9dUuoYgRlOYj5W6cAOi9eentYcPZkoFszalP/pT6b06mR7olzsq dSxuHIpaTAhGQKLGXLIkVTODH0bRJytAyXrNWCJwI8i1VBVYA8PSR5yTHn2T04T+U9T0w8doSVi 3Oan3N/b5LqfnuOSv6Wbjbdw= X-Received: by 2002:aa7:cf88:0:b0:500:47ed:9784 with SMTP id z8-20020aa7cf88000000b0050047ed9784mr12990287edx.14.1681734196733; Mon, 17 Apr 2023 05:23:16 -0700 (PDT) X-Received: by 2002:aa7:cf88:0:b0:500:47ed:9784 with SMTP id z8-20020aa7cf88000000b0050047ed9784mr12990267edx.14.1681734196373; Mon, 17 Apr 2023 05:23:16 -0700 (PDT) Received: from klayman.. ([37.163.248.182]) by smtp.gmail.com with ESMTPSA id q21-20020aa7cc15000000b004fbf6b35a56sm5756618edt.76.2023.04.17.05.23.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 05:23:16 -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: [RFC PATCH v4 2/4] fpga: add fake FPGA bridge Date: Mon, 17 Apr 2023 14:23:06 +0200 Message-Id: <20230417122308.131453-3-marpagan@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417122308.131453-1-marpagan@redhat.com> References: <20230417122308.131453-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_H2,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 Add fake FPGA bridge driver with support functions. The driver includes a counter for the number of switching cycles. This module is part of the KUnit tests for the FPGA subsystem. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fake-fpga-bridge.c | 242 ++++++++++++++++++++++++++ drivers/fpga/tests/fake-fpga-bridge.h | 36 ++++ 2 files changed, 278 insertions(+) create mode 100644 drivers/fpga/tests/fake-fpga-bridge.c create mode 100644 drivers/fpga/tests/fake-fpga-bridge.h diff --git a/drivers/fpga/tests/fake-fpga-bridge.c b/drivers/fpga/tests/fake-fpga-bridge.c new file mode 100644 index 000000000000..6a62c04a75c0 --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-bridge.c @@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for the fake FPGA bridge + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include + +#include "fake-fpga-bridge.h" + +#define FAKE_FPGA_BRIDGE_DEV_NAME "fake_fpga_bridge" + +struct fake_bridge_priv { + int id; + bool enable; + int cycles_count; + struct kunit *test; +}; + +struct fake_bridge_data { + struct kunit *test; +}; + +static int op_enable_show(struct fpga_bridge *bridge) +{ + struct fake_bridge_priv *priv; + + priv = bridge->priv; + + if (priv->test) + kunit_info(priv->test, "Fake FPGA bridge %d: enable_show\n", + priv->id); + + return priv->enable; +} + +static int op_enable_set(struct fpga_bridge *bridge, bool enable) +{ + struct fake_bridge_priv *priv; + + priv = bridge->priv; + + if (enable && !priv->enable) + priv->cycles_count++; + + priv->enable = enable; + + if (priv->test) + kunit_info(priv->test, "Fake FPGA bridge %d: enable_set: %d\n", + priv->id, enable); + + return 0; +} + +static void op_remove(struct fpga_bridge *bridge) +{ + struct fake_bridge_priv *priv; + + priv = bridge->priv; + + if (priv->test) + kunit_info(priv->test, "Fake FPGA bridge: remove\n"); +} + +static const struct fpga_bridge_ops fake_fpga_bridge_ops = { + .enable_show = op_enable_show, + .enable_set = op_enable_set, + .fpga_bridge_remove = op_remove, +}; + +/** + * fake_fpga_bridge_register() - register a fake FPGA bridge. + * @parent: parent device. + * @test: KUnit test context object. + * + * Return: pointer to a new fake FPGA bridge on success, an ERR_PTR() + * encoded error code on failure. + */ +struct fake_fpga_bridge * +fake_fpga_bridge_register(struct device *parent, struct kunit *test) +{ + struct fake_fpga_bridge *bridge_ctx; + struct fake_bridge_data pdata; + struct fake_bridge_priv *priv; + int ret; + + bridge_ctx = kzalloc(sizeof(*bridge_ctx), GFP_KERNEL); + if (!bridge_ctx) { + ret = -ENOMEM; + goto err_mem; + } + + bridge_ctx->pdev = platform_device_alloc(FAKE_FPGA_BRIDGE_DEV_NAME, + PLATFORM_DEVID_AUTO); + if (!bridge_ctx->pdev) { + pr_err("Fake FPGA bridge device allocation failed\n"); + ret = -ENOMEM; + goto err_mem; + } + + pdata.test = test; + platform_device_add_data(bridge_ctx->pdev, &pdata, sizeof(pdata)); + + bridge_ctx->pdev->dev.parent = parent; + ret = platform_device_add(bridge_ctx->pdev); + if (ret) { + pr_err("Fake FPGA bridge device add failed\n"); + goto err_pdev; + } + + bridge_ctx->bridge = platform_get_drvdata(bridge_ctx->pdev); + + if (test) { + priv = bridge_ctx->bridge->priv; + kunit_info(test, "Fake FPGA bridge %d registered\n", priv->id); + } + + return bridge_ctx; + +err_pdev: + platform_device_put(bridge_ctx->pdev); + kfree(bridge_ctx); +err_mem: + return ERR_PTR(ret); +} +EXPORT_SYMBOL_GPL(fake_fpga_bridge_register); + +/** + * fake_fpga_bridge_unregister() - unregister a fake FPGA bridge. + * @bridge_ctx: fake FPGA bridge context data structure. + */ +void fake_fpga_bridge_unregister(struct fake_fpga_bridge *bridge_ctx) +{ + struct fake_bridge_priv *priv; + struct kunit *test; + int id; + + if (!bridge_ctx) + return; + + priv = bridge_ctx->bridge->priv; + test = priv->test; + id = priv->id; + + if (bridge_ctx->pdev) { + platform_device_unregister(bridge_ctx->pdev); + if (test) + kunit_info(test, "Fake FPGA bridge %d unregistered\n", id); + } + + kfree(bridge_ctx); +} +EXPORT_SYMBOL_GPL(fake_fpga_bridge_unregister); + +/** + * fake_fpga_bridge_get_state() - get state of a fake FPGA bridge. + * @bridge_ctx: fake FPGA bridge context data structure. + * + * Return: 1 if the bridge is enabled, 0 if disabled. + */ +int fake_fpga_bridge_get_state(const struct fake_fpga_bridge *bridge_ctx) +{ + return bridge_ctx->bridge->br_ops->enable_show(bridge_ctx->bridge); +} +EXPORT_SYMBOL_GPL(fake_fpga_bridge_get_state); + +/** + * fake_fpga_bridge_get_cycles_count() - get the number of switching cycles. + * @bridge_ctx: fake FPGA bridge context data structure. + * + * Return: number of switching cycles. + */ +int fake_fpga_bridge_get_cycles_count(const struct fake_fpga_bridge *bridge_ctx) +{ + struct fake_bridge_priv *priv; + + priv = bridge_ctx->bridge->priv; + + return priv->cycles_count; +} +EXPORT_SYMBOL_GPL(fake_fpga_bridge_get_cycles_count); + +static int fake_fpga_bridge_probe(struct platform_device *pdev) +{ + struct device *dev; + struct fpga_bridge *bridge; + struct fake_bridge_data *pdata; + struct fake_bridge_priv *priv; + static int id_count; + + dev = &pdev->dev; + pdata = dev_get_platdata(dev); + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->id = id_count++; + priv->enable = true; + + if (pdata) + priv->test = pdata->test; + + bridge = fpga_bridge_register(dev, "Fake FPGA Bridge", + &fake_fpga_bridge_ops, priv); + if (IS_ERR(bridge)) + return PTR_ERR(bridge); + + platform_set_drvdata(pdev, bridge); + + return 0; +} + +static int fake_fpga_bridge_remove(struct platform_device *pdev) +{ + struct fpga_bridge *bridge = platform_get_drvdata(pdev); + + fpga_bridge_unregister(bridge); + + return 0; +} + +static struct platform_driver fake_fpga_bridge_drv = { + .driver = { + .name = FAKE_FPGA_BRIDGE_DEV_NAME + }, + .probe = fake_fpga_bridge_probe, + .remove = fake_fpga_bridge_remove, +}; + +module_platform_driver(fake_fpga_bridge_drv); + +MODULE_AUTHOR("Marco Pagani "); +MODULE_DESCRIPTION("Fake FPGA Bridge"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/fpga/tests/fake-fpga-bridge.h b/drivers/fpga/tests/fake-fpga-bridge.h new file mode 100644 index 000000000000..a5acfdf18056 --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-bridge.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Header file for the fake FPGA bridge + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#ifndef __FPGA_FAKE_BRIDGE_H +#define __FPGA_FAKE_BRIDGE_H + +#include +#include + +/** + * struct fake_fpga_bridge - fake FPGA bridge context data structure + * + * @bridge: FPGA bridge. + * @pdev: platform device of the FPGA bridge. + */ +struct fake_fpga_bridge { + struct fpga_bridge *bridge; + struct platform_device *pdev; +}; + +struct fake_fpga_bridge * +fake_fpga_bridge_register(struct device *parent, struct kunit *test); + +void fake_fpga_bridge_unregister(struct fake_fpga_bridge *bridge_ctx); + +int fake_fpga_bridge_get_state(const struct fake_fpga_bridge *bridge_ctx); + +int fake_fpga_bridge_get_cycles_count(const struct fake_fpga_bridge *bridge_ctx); + +#endif /* __FPGA_FAKE_BRIDGE_H */ -- 2.39.2