From: Xu Yilun <yilun.xu@intel.com>
To: Marco Pagani <marpagan@redhat.com>
Cc: Moritz Fischer <mdf@kernel.org>, Wu Hao <hao.wu@intel.com>,
Tom Rix <trix@redhat.com>,
linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org
Subject: Re: [RFC PATCH v5 2/4] fpga: add fake FPGA bridge
Date: Sat, 13 May 2023 23:46:01 +0800 [thread overview]
Message-ID: <ZF+wuXVRc0/o19Gg@yilunxu-OptiPlex-7050> (raw)
In-Reply-To: <20230511141922.437328-3-marpagan@redhat.com>
On 2023-05-11 at 16:19:20 +0200, Marco Pagani wrote:
> Add fake FPGA bridge platform driver with support functions. This module
> is part of the KUnit tests for the FPGA subsystem.
>
> Signed-off-by: Marco Pagani <marpagan@redhat.com>
> ---
> drivers/fpga/tests/fake-fpga-bridge.c | 203 ++++++++++++++++++++++++++
> drivers/fpga/tests/fake-fpga-bridge.h | 40 +++++
> 2 files changed, 243 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..e5db0a809ee3
> --- /dev/null
> +++ b/drivers/fpga/tests/fake-fpga-bridge.c
> @@ -0,0 +1,203 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Driver for the fake FPGA bridge
> + *
> + * Copyright (C) 2023 Red Hat, Inc.
> + *
> + * Author: Marco Pagani <marpagan@redhat.com>
> + */
> +
> +#include <linux/types.h>
> +#include <linux/device.h>
> +#include <linux/platform_device.h>
> +#include <linux/fpga/fpga-bridge.h>
> +#include <kunit/test.h>
> +
> +#include "fake-fpga-bridge.h"
> +
> +#define FAKE_FPGA_BRIDGE_DEV_NAME "fake_fpga_bridge"
> +
> +struct fake_bridge_pdata {
> + struct kunit *test;
> + struct fake_fpga_bridge_stats *stats;
> +};
> +
> +struct fake_bridge_priv {
> + bool state;
> + struct fake_bridge_pdata *pdata;
> +};
> +
> +static int op_enable_show(struct fpga_bridge *bridge)
> +{
> + struct fake_bridge_priv *priv;
> +
> + priv = bridge->priv;
> +
> + kunit_info(priv->pdata->test, "Fake FPGA bridge %u: enable_show\n",
> + bridge->dev.id);
> +
> + return priv->state;
> +}
> +
> +static int op_enable_set(struct fpga_bridge *bridge, bool enable)
> +{
> + struct fake_bridge_priv *priv;
> +
> + priv = bridge->priv;
> +
> + if (enable && !priv->state)
> + priv->pdata->stats->cycles_count++;
> +
> + priv->state = enable;
> + priv->pdata->stats->enable = priv->state;
> +
> + kunit_info(priv->pdata->test, "Fake FPGA bridge %u: enable_set: %d\n",
> + bridge->dev.id, enable);
> +
> + return 0;
> +}
> +
> +static void op_remove(struct fpga_bridge *bridge)
> +{
> + struct fake_bridge_priv *priv;
> +
> + priv = bridge->priv;
> +
> + kunit_info(priv->pdata->test, "Fake FPGA bridge %u: remove\n",
> + bridge->dev.id);
> +}
> +
> +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.
> + * @test: KUnit test context object.
> + * @parent: parent device.
> + *
> + * 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 kunit *test, struct device *parent)
> +{
> + struct fake_fpga_bridge *bridge_ctx;
> + struct fake_bridge_pdata pdata;
> + struct fake_bridge_priv *priv;
> + int ret;
> +
> + bridge_ctx = kunit_kzalloc(test, 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;
> + pdata.stats = &bridge_ctx->stats;
> + 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);
> +
> + priv = bridge_ctx->bridge->priv;
> + bridge_ctx->stats.enable = priv->state;
> +
> + kunit_info(test, "Fake FPGA bridge %u: registered\n",
> + bridge_ctx->bridge->dev.id);
> +
> + return bridge_ctx;
> +
> +err_pdev:
> + platform_device_put(bridge_ctx->pdev);
> +err_mem:
> + return ERR_PTR(ret);
> +}
> +EXPORT_SYMBOL_GPL(fake_fpga_bridge_register);
Same as fake_fpga_mgr.
Thanks,
Yilun
> +
> +/**
> + * 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;
> + u32 id;
> +
> + if (!bridge_ctx)
> + return;
> +
> + id = bridge_ctx->bridge->dev.id;
> + priv = bridge_ctx->bridge->priv;
> + test = priv->pdata->test;
> +
> + platform_device_unregister(bridge_ctx->pdev);
> +
> + kunit_info(test, "Fake FPGA bridge %u: unregistered\n", id);
> +}
> +EXPORT_SYMBOL_GPL(fake_fpga_bridge_unregister);
> +
> +static int fake_fpga_bridge_probe(struct platform_device *pdev)
> +{
> + struct device *dev;
> + struct fake_bridge_pdata *pdata;
> + struct fpga_bridge *bridge;
> + struct fake_bridge_priv *priv;
> +
> + dev = &pdev->dev;
> + pdata = dev_get_platdata(dev);
> +
> + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> +
> + priv->pdata = pdata;
> + priv->state = true;
> +
> + 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_LICENSE("GPL");
> diff --git a/drivers/fpga/tests/fake-fpga-bridge.h b/drivers/fpga/tests/fake-fpga-bridge.h
> new file mode 100644
> index 000000000000..3ecfab7e2890
> --- /dev/null
> +++ b/drivers/fpga/tests/fake-fpga-bridge.h
> @@ -0,0 +1,40 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Header file for the fake FPGA bridge
> + *
> + * Copyright (C) 2023 Red Hat, Inc.
> + *
> + * Author: Marco Pagani <marpagan@redhat.com>
> + */
> +
> +#ifndef __FPGA_FAKE_BRIDGE_H
> +#define __FPGA_FAKE_BRIDGE_H
> +
> +#include <linux/types.h>
> +#include <linux/platform_device.h>
> +#include <kunit/test.h>
> +
> +struct fake_fpga_bridge_stats {
> + bool enable;
> + u32 cycles_count;
> +};
> +
> +/**
> + * struct fake_fpga_bridge - fake FPGA bridge context data structure
> + *
> + * @bridge: FPGA bridge.
> + * @pdev: platform device of the FPGA bridge.
> + * @stats: info from the low level driver.
> + */
> +struct fake_fpga_bridge {
> + struct fpga_bridge *bridge;
> + struct platform_device *pdev;
> + struct fake_fpga_bridge_stats stats;
> +};
> +
> +struct fake_fpga_bridge *
> +fake_fpga_bridge_register(struct kunit *test, struct device *parent);
> +
> +void fake_fpga_bridge_unregister(struct fake_fpga_bridge *bridge_ctx);
> +
> +#endif /* __FPGA_FAKE_BRIDGE_H */
> --
> 2.40.1
>
next prev parent reply other threads:[~2023-05-13 7:46 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-11 14:19 [RFC PATCH v5 0/4] fpga: add initial KUnit tests for the subsystem Marco Pagani
2023-05-11 14:19 ` [RFC PATCH v5 1/4] fpga: add fake FPGA manager Marco Pagani
2023-05-13 15:44 ` Xu Yilun
2023-05-11 14:19 ` [RFC PATCH v5 2/4] fpga: add fake FPGA bridge Marco Pagani
2023-05-13 15:46 ` Xu Yilun [this message]
2023-05-11 14:19 ` [RFC PATCH v5 3/4] fpga: add fake FPGA region Marco Pagani
2023-05-13 16:05 ` Xu Yilun
2023-05-11 14:19 ` [RFC PATCH v5 4/4] fpga: add initial KUnit test suites Marco Pagani
2023-05-13 17:40 ` Xu Yilun
2023-05-15 17:24 ` Marco Pagani
2023-05-17 10:04 ` Xu Yilun
2023-05-18 12:43 ` Marco Pagani
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZF+wuXVRc0/o19Gg@yilunxu-OptiPlex-7050 \
--to=yilun.xu@intel.com \
--cc=hao.wu@intel.com \
--cc=linux-fpga@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marpagan@redhat.com \
--cc=mdf@kernel.org \
--cc=trix@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®