From patchwork Fri Feb 3 17:06:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 52569 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp958987wrn; Fri, 3 Feb 2023 09:15:45 -0800 (PST) X-Google-Smtp-Source: AK7set91rZfD2X9LlhF7XPR5W8Wi1a5Z34N66AbUm1M8pJP7BsCoNMLI9DCRHVTj52ecH/w3uUKR X-Received: by 2002:a17:907:cb20:b0:885:2eb5:68a2 with SMTP id um32-20020a170907cb2000b008852eb568a2mr9796962ejc.16.1675444545454; Fri, 03 Feb 2023 09:15:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675444545; cv=none; d=google.com; s=arc-20160816; b=WJgFhl9YJTDB3vvSRZjNwU6b4cEWTGVEmIQgXrYAQ/2bl+hwzkrPryutY81HBg9tne cveganQR+Et5nUPJ9Kj2eTZIcZg6+eR7pcQydzKcKouobbyZMVqW0UmgwvNDdtqblFiR xQajZY3O70ooidJO9cE/pU0qDS+k930Alq+WOdfCpjAoZ7tY/E7mrfw/TGAL7S2OHA7q Dcq7ifRW3kOdM1BRubbhU77ViLMqAwx+o+9pWjY061QOB/OR/SKVFM/OzPXrq814Tbqz y/ASH+VnKySCZuIIwosZPNc01D3Vqr5Rwk0zxksU4ir8xd1AdOb9P7TsdFkeMiiGfzSu YSpA== 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=aTqTPW1mDhdo6UVlHMTIqNJ0ko0wBmbKrV94jadhHOI=; b=D/oDNJ7ClvSLnGR3Bi7TxOjJ4FXNT2Kbvy+r5GU7cABKkKk6XtRfS72L6q9uUaZtk7 vft9UeLwbwmdArsiapAFxN3Jkjg/FRti9HyEvQRpBiXfoSWROSIQSRvZJx4JB361fdmA 1OQobVGxhbX5BiKdHeyOdP1QPvChkfJLRcnJAfXt3UkwoEDp25ubggQZrGJ97JBQDxBQ r7xoUmU8oqusWZpnrhDqSdpQKJf/MXtsdnwVo0VPpaEQ8sWUaYl3+SJLVTAyWlL+J1JW POxn6WfAsW6DUz9YQm6ZsYRs1/irsvBU+MXhkNS5jgfjp3ODF6P0c6Oqky49nHg9ZJ5S VeAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Gj0HKFF3; 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 fq18-20020a1709069d9200b008873dd1b69csi3306588ejc.286.2023.02.03.09.15.09; Fri, 03 Feb 2023 09:15:45 -0800 (PST) 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=Gj0HKFF3; 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 S233573AbjBCRIO (ORCPT + 99 others); Fri, 3 Feb 2023 12:08:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232916AbjBCRIL (ORCPT ); Fri, 3 Feb 2023 12:08:11 -0500 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 4C7699D584 for ; Fri, 3 Feb 2023 09:07:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675444043; 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=aTqTPW1mDhdo6UVlHMTIqNJ0ko0wBmbKrV94jadhHOI=; b=Gj0HKFF3zF6JRpnsVgntsHbjvbNNZA64yv6w7bSc91FktN6Qe2bNb1aMharu9JMYzowpdO w+pidjd7IScqNHzd7D+OxIbuu6/VcKgDuYetFYH6P6tKgPLGp/DdsXOQpS1jbWQqGzzXVz 5rKgEALCewqY30bm2x+4/LI5022t62w= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-249-iS1N0yTmPXapMryzbrFgPw-1; Fri, 03 Feb 2023 12:07:22 -0500 X-MC-Unique: iS1N0yTmPXapMryzbrFgPw-1 Received: by mail-qv1-f69.google.com with SMTP id p15-20020a0cf54f000000b0054fc21ff275so3041376qvm.4 for ; Fri, 03 Feb 2023 09:07:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=aTqTPW1mDhdo6UVlHMTIqNJ0ko0wBmbKrV94jadhHOI=; b=64BFhvXZj+fMgg7OsoItOX7wTaCDAororhRNVCERTHwtIxaD1iZT8aq8AfdyYKjBV2 CxVBf8zV6CmcBfheB+BJ0rR+LPtI3hQBooYBlqRv93GeFIhg3Ktdru52OJt7WsHQI/f1 lRUsDzHC2ixLNISYp4SKT4ZkfXiRbrNnw1BWfrDaHWF527kbQPXnGkAAasOAPIL3CSHe WpiDiTfrbAVtxuOceWe4Ui53BvU+4IkRHK78hsD5ll4xq7ugDjCQkTVKpARB6P1Em0Pe GxuyhuX/fudRFPBEDy3NPYTCgSCxCNJLe0qPOLOOy6YLWxjLltrA9XTC7Zwp/bcV8+YW qEJw== X-Gm-Message-State: AO0yUKUaI9buOWzZN21Eu3qq8k9C+WQ9NqXd3rTRvYfRYra+hKxrVOtD SxivSSFFUhjzWULGBsYHiNLbeBEqD1D0KAKf3Eq/U2S0XrVFw4Gpg4Dva1bHSeA6kumbsjWuEVY RmNIFYt5ZPNFe6eWFSZku5gY= X-Received: by 2002:a05:6214:5ed2:b0:537:6aea:124b with SMTP id mn18-20020a0562145ed200b005376aea124bmr16953969qvb.16.1675444041611; Fri, 03 Feb 2023 09:07:21 -0800 (PST) X-Received: by 2002:a05:6214:5ed2:b0:537:6aea:124b with SMTP id mn18-20020a0562145ed200b005376aea124bmr16953943qvb.16.1675444041288; Fri, 03 Feb 2023 09:07:21 -0800 (PST) Received: from klayman.redhat.com (net-2-34-30-183.cust.vodafonedsl.it. [2.34.30.183]) by smtp.gmail.com with ESMTPSA id b13-20020ac801cd000000b003a6a19ee4f0sm1884287qtg.33.2023.02.03.09.07.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Feb 2023 09:07:21 -0800 (PST) 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 4/4] fpga: add fake FPGA bridge Date: Fri, 3 Feb 2023 18:06:53 +0100 Message-Id: <20230203170653.414990-5-marpagan@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230203170653.414990-1-marpagan@redhat.com> References: <20230203170653.414990-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_H2,SPF_HELO_NONE,SPF_NONE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756830939670686844?= X-GMAIL-MSGID: =?utf-8?q?1756830939670686844?= 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 test suite for the FPGA subsystem. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fake-fpga-bridge.c | 214 ++++++++++++++++++++++++++ drivers/fpga/tests/fake-fpga-bridge.h | 36 +++++ 2 files changed, 250 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..1f3c8e4fbb6a --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-bridge.c @@ -0,0 +1,214 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for fake FPGA bridge + * + * Copyright (C) 2023 Red Hat, Inc. All rights reserved. + * + * 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) +{ +} + +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 + * @bridge_ctx: fake FPGA bridge context data structure. + * @test: KUnit test context object. + * + * Return: 0 if registration succeeded, an error code otherwise. + */ +int fake_fpga_bridge_register(struct fake_fpga_bridge *bridge_ctx, + struct kunit *test) +{ + struct fake_bridge_data pdata; + struct fake_bridge_priv *priv; + int ret; + + pdata.test = test; + + bridge_ctx->pdev = platform_device_alloc(FAKE_FPGA_BRIDGE_DEV_NAME, + PLATFORM_DEVID_AUTO); + if (IS_ERR(bridge_ctx->pdev)) { + pr_err("Fake FPGA bridge device allocation failed\n"); + return -ENOMEM; + } + + platform_device_add_data(bridge_ctx->pdev, &pdata, sizeof(pdata)); + + ret = platform_device_add(bridge_ctx->pdev); + if (ret) { + pr_err("Fake FPGA bridge device add failed\n"); + platform_device_put(bridge_ctx->pdev); + return ret; + } + + 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 0; +} +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; + + 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); + } +} +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->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..9de62d2f993b --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-bridge.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Header file for fake FPGA bridge + * + * Copyright (C) 2023 Red Hat, Inc. All rights reserved. + * + * 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; +}; + +int fake_fpga_bridge_register(struct fake_fpga_bridge *bridge_ctx, + 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 */