From patchwork Fri Mar 10 17:04:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 67600 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp998719wrd; Fri, 10 Mar 2023 09:24:57 -0800 (PST) X-Google-Smtp-Source: AK7set+qtB5i6QJhChWr3V6sybdlvJWjt470aB/LsAOzStHF3A8bHj3B4EP6uGdSaG+NyD0NEYbo X-Received: by 2002:a17:902:c40b:b0:19e:8bfe:7d68 with SMTP id k11-20020a170902c40b00b0019e8bfe7d68mr34330511plk.11.1678469097450; Fri, 10 Mar 2023 09:24:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678469097; cv=none; d=google.com; s=arc-20160816; b=fYRT/wDgBHwS95L8LCLqhp52lwEr5CJUsclaVI9lC0FlM3lxpMOH6cjo1J9ACwWQWm pevSLoxGroP8xWOQOr8dkcVx6iApIfUxRYQgOeQ3xpyBXETcboO2WP3DS9/AIlWPtNIr RzriSyCrXU4fj6QcTF43hv/yBA15vNhBDOCbFZr9OFWxaxm+aqGgYNIsOG0hH/hQ4Q69 tH6fN0wcglQ3+Q+q7Rj2x6WkCq8dK5ChYGvfWtSC8zWbdlZ7+ohXuu8dRPQaESaoSeB+ dvKnObOF020G0a6gMfqQ1cFtqQUW8pUMrf2Y1DYuZZe3mgPZrT2nY7ChhSvbeF6l5tKY uQCQ== 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=JO3sx9KQ7oN1zllI/65sQBKcfWX+lgrDYeSnCW02Np8=; b=eAIWNArx7J6iZlznzYs5JaT7tkGiWzsLQ251joY1dkdmkcx3EHiEr1+D22VAjfCuWJ 4aqm1369cCi1O/QXYkYauttK+T8h3gigqTIUvEr8IU3LNAc5Fc3kPHymqvWkkHCBtoNz K61uRzqrGkKkm6iIgOrSx5wrVk7UMeIvQqyGXbZb38NmBaXyo3ACPnc4bURrZVd+0wTZ WE7OZl0AcbvY+ZcsGSxEGPuTva4eIDUvPe9Tr+6NZhdbhGXj6PQ5wWfwBc4StHODFbBG vj4qnvfJycXUlhFFoy5TqGhv1NklMAcPz7wbNcQJTKskXvmMtBtxmyN0dLOGLCl72nA6 vJAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QV6ylzYH; 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 y21-20020a1709027c9500b0019e8c3cd415si397227pll.260.2023.03.10.09.24.42; Fri, 10 Mar 2023 09:24:57 -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=QV6ylzYH; 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 S231364AbjCJRHT (ORCPT + 99 others); Fri, 10 Mar 2023 12:07:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231785AbjCJRGn (ORCPT ); Fri, 10 Mar 2023 12:06:43 -0500 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 CD6EB12DC20 for ; Fri, 10 Mar 2023 09:04:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678467863; 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=JO3sx9KQ7oN1zllI/65sQBKcfWX+lgrDYeSnCW02Np8=; b=QV6ylzYHdt/dN5tfq4rxkOApZOvWArtIt9EhjtXgYfj3gBf4AHTImUN+PNJXythUWhqVcn qMn48BpqM5xViQhYwVIWCIwQqr5K7lKNePHDHZNsVNpfVFcOWrM6CcDqKEgPa5/v9cXkE/ D/1MogTR4sJLWMBhPG9G18EHUOlXnSM= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-582-TveBDDJaPTOFE1ddtCPEFQ-1; Fri, 10 Mar 2023 12:04:22 -0500 X-MC-Unique: TveBDDJaPTOFE1ddtCPEFQ-1 Received: by mail-wm1-f71.google.com with SMTP id j6-20020a05600c1c0600b003eaf882cb85so2212118wms.9 for ; Fri, 10 Mar 2023 09:04:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678467861; 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=JO3sx9KQ7oN1zllI/65sQBKcfWX+lgrDYeSnCW02Np8=; b=DKU9eFt5FGfWKtHpGbzDGDAxnCpDxz6X2f9ArvrxqiRUtbd2mvovdYNDxaaCTghTG5 wmJwl2JVlKhQUjyS2fDDTi0+V2dwuDEdolq3iJiWNsfjlVS4ogG01DKtzqkFIzVRtDAn prWWCcWKf2z0EwmfEc3p/yKhdm9UcA9D/S1l/34ELHWNWzKRtgvcwNo//YyE4EIfsgb0 NGFqJ/8/kGV8a3QRad4kFuzVtM6D0OpCxKktO3XsOlEAw0F20Sp0LqESXpdSeMrAp9TD CmtUvEuHO4T7ERHGr8Jg+LYdm+zgxwLjCL7lzp60Q6FLtsVKw83/coKUB9qSwBNqlEWR am/w== X-Gm-Message-State: AO0yUKXkLE304qOSadHFhfn+VQ7EwKVou3zPhpnIFdRv8xZZNcvDvFjm 3ZbYcOnRMuSNR4t+nQRh2jkJ39fICUmOSGYRgZklNt9p1zRAJ/4xpOhCsEZtIqUpCHoAl5j4kT/ KhouCuHVJ9VoqeL7Ux+zf3aUJNztde7A= X-Received: by 2002:a05:600c:4ed0:b0:3e2:20c7:6553 with SMTP id g16-20020a05600c4ed000b003e220c76553mr3538245wmq.13.1678467860799; Fri, 10 Mar 2023 09:04:20 -0800 (PST) X-Received: by 2002:a05:600c:4ed0:b0:3e2:20c7:6553 with SMTP id g16-20020a05600c4ed000b003e220c76553mr3538218wmq.13.1678467860569; Fri, 10 Mar 2023 09:04:20 -0800 (PST) Received: from klayman.redhat.com (net-2-34-29-20.cust.vodafonedsl.it. [2.34.29.20]) by smtp.gmail.com with ESMTPSA id u10-20020a05600c00ca00b003eb192787bfsm417294wmm.25.2023.03.10.09.04.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 09:04:20 -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 v2 3/4] fpga: add fake FPGA region Date: Fri, 10 Mar 2023 18:04:11 +0100 Message-Id: <20230310170412.708363-4-marpagan@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230310170412.708363-1-marpagan@redhat.com> References: <20230310170412.708363-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=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?1760002411951573374?= X-GMAIL-MSGID: =?utf-8?q?1760002411951573374?= Add fake FPGA region platform driver with support functions. This module is part of the KUnit tests for the FPGA subsystem. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fake-fpga-region.c | 219 ++++++++++++++++++++++++++ drivers/fpga/tests/fake-fpga-region.h | 38 +++++ 2 files changed, 257 insertions(+) create mode 100644 drivers/fpga/tests/fake-fpga-region.c create mode 100644 drivers/fpga/tests/fake-fpga-region.h diff --git a/drivers/fpga/tests/fake-fpga-region.c b/drivers/fpga/tests/fake-fpga-region.c new file mode 100644 index 000000000000..54d0e564728b --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-region.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for the fake FPGA region + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "fake-fpga-region.h" + +#define FAKE_FPGA_REGION_DEV_NAME "fake_fpga_region" + +struct fake_region_priv { + int id; + struct kunit *test; + struct list_head bridge_list; +}; + +struct fake_region_data { + struct fpga_manager *mgr; + struct kunit *test; +}; + +/** + * fake_fpga_region_register() - register a fake FPGA region. + * @region_ctx: fake FPGA region context data structure. + * @mgr: associated FPGA manager. + * @parent: parent device. + * @test: KUnit test context object. + * + * Return: 0 if registration succeeded, an error code otherwise. + */ +int fake_fpga_region_register(struct fake_fpga_region *region_ctx, + struct fpga_manager *mgr, struct device *parent, + struct kunit *test) +{ + struct fake_region_data pdata; + struct fake_region_priv *priv; + int ret; + + pdata.mgr = mgr; + pdata.test = test; + + region_ctx->pdev = platform_device_alloc(FAKE_FPGA_REGION_DEV_NAME, + PLATFORM_DEVID_AUTO); + if (IS_ERR(region_ctx->pdev)) { + pr_err("Fake FPGA region device allocation failed\n"); + return -ENOMEM; + } + + region_ctx->pdev->dev.parent = parent; + platform_device_add_data(region_ctx->pdev, &pdata, sizeof(pdata)); + + ret = platform_device_add(region_ctx->pdev); + if (ret) { + pr_err("Fake FPGA region device add failed\n"); + platform_device_put(region_ctx->pdev); + return ret; + } + + region_ctx->region = platform_get_drvdata(region_ctx->pdev); + + if (test) { + priv = region_ctx->region->priv; + kunit_info(test, "Fake FPGA region %d registered\n", priv->id); + } + + return 0; +} +EXPORT_SYMBOL_GPL(fake_fpga_region_register); + +/** + * fake_fpga_region_unregister() - unregister a fake FPGA region. + * @region_ctx: fake FPGA region context data structure. + */ +void fake_fpga_region_unregister(struct fake_fpga_region *region_ctx) +{ + struct fake_region_priv *priv; + struct kunit *test; + int id; + + if (!region_ctx) + return; + + priv = region_ctx->region->priv; + test = priv->test; + id = priv->id; + + if (region_ctx->pdev) { + platform_device_unregister(region_ctx->pdev); + if (test) + kunit_info(test, "Fake FPGA region %d unregistered\n", id); + } +} +EXPORT_SYMBOL_GPL(fake_fpga_region_unregister); + +/** + * fake_fpga_region_add_bridge() - add a bridge to a fake FPGA region. + * @region_ctx: fake FPGA region context data structure. + * @bridge: FPGA bridge. + * + * Return: 0 if registration succeeded, an error code otherwise. + */ +void fake_fpga_region_add_bridge(struct fake_fpga_region *region_ctx, + struct fpga_bridge *bridge) +{ + struct fake_region_priv *priv; + + priv = region_ctx->region->priv; + + /* Add bridge to the list of bridges in the private context */ + list_add(&bridge->node, &priv->bridge_list); + + if (priv->test) + kunit_info(priv->test, "Bridge added to fake FPGA region %d\n", + priv->id); +} +EXPORT_SYMBOL_GPL(fake_fpga_region_add_bridge); + +static int fake_region_get_bridges(struct fpga_region *region) +{ + struct fake_region_priv *priv; + struct fpga_bridge *bridge, *tmp; + int ret; + + priv = region->priv; + + list_for_each_entry_safe(bridge, tmp, &priv->bridge_list, node) { + list_del(&bridge->node); + ret = fpga_bridge_get_to_list(bridge->dev.parent, + region->info, + ®ion->bridge_list); + if (ret) + break; + } + + return ret; +} + +static int fake_fpga_region_probe(struct platform_device *pdev) +{ + struct device *dev; + struct fpga_region *region; + struct fpga_manager *mgr; + struct fake_region_data *pdata; + struct fake_region_priv *priv; + struct fpga_region_info info; + static int id_count; + + dev = &pdev->dev; + pdata = dev_get_platdata(dev); + + if (!pdata) { + dev_err(&pdev->dev, "Missing platform data\n"); + return -EINVAL; + } + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + mgr = fpga_mgr_get(pdata->mgr->dev.parent); + if (IS_ERR(mgr)) + return PTR_ERR(mgr); + + INIT_LIST_HEAD(&priv->bridge_list); + priv->id = id_count++; + priv->test = pdata->test; + + memset(&info, 0, sizeof(info)); + info.priv = priv; + info.mgr = mgr; + info.get_bridges = fake_region_get_bridges; + + region = fpga_region_register_full(dev, &info); + if (IS_ERR(region)) { + fpga_mgr_put(mgr); + return PTR_ERR(region); + } + + platform_set_drvdata(pdev, region); + + return 0; +} + +static int fake_fpga_region_remove(struct platform_device *pdev) +{ + struct fpga_region *region = platform_get_drvdata(pdev); + struct fpga_manager *mgr = region->mgr; + + fpga_mgr_put(mgr); + fpga_bridges_put(®ion->bridge_list); + fpga_region_unregister(region); + + return 0; +} + +static struct platform_driver fake_fpga_region_drv = { + .driver = { + .name = FAKE_FPGA_REGION_DEV_NAME + }, + .probe = fake_fpga_region_probe, + .remove = fake_fpga_region_remove, +}; + +module_platform_driver(fake_fpga_region_drv); + +MODULE_AUTHOR("Marco Pagani "); +MODULE_DESCRIPTION("Fake FPGA Bridge"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/fpga/tests/fake-fpga-region.h b/drivers/fpga/tests/fake-fpga-region.h new file mode 100644 index 000000000000..9268ca335662 --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-region.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Header file for the fake FPGA region + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#ifndef __FPGA_FAKE_RGN_H +#define __FPGA_FAKE_RGN_H + +#include +#include +#include +#include + +/** + * struct fake_fpga_region - fake FPGA region context data structure + * + * @region: FPGA region. + * @pdev: platform device of the FPGA region. + */ +struct fake_fpga_region { + struct fpga_region *region; + struct platform_device *pdev; +}; + +int fake_fpga_region_register(struct fake_fpga_region *region_ctx, + struct fpga_manager *mgr, struct device *parent, + struct kunit *test); + +void fake_fpga_region_add_bridge(struct fake_fpga_region *region_ctx, + struct fpga_bridge *bridge); + +void fake_fpga_region_unregister(struct fake_fpga_region *region_ctx); + +#endif /* __FPGA_FAKE_RGN_H */