Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp8889193rwr; Thu, 11 May 2023 07:27:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ69EcBWR/uz/Yn0MevKbF54Hh5WNTBur2aHeWBBCGIliVqno6sswPI3V4KlvnubwgZ10svh X-Received: by 2002:a05:6a00:1a4f:b0:644:2718:dd40 with SMTP id h15-20020a056a001a4f00b006442718dd40mr21638399pfv.30.1683815236548; Thu, 11 May 2023 07:27:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683815236; cv=none; d=google.com; s=arc-20160816; b=v4NhOcGnyWYFbwmjgnn7Mky55GEQ68D5I7pRJADcOiBE4BEV3pVCLBvwsSpRpTFoix Oqaw29X4Mls9oFGXla6Rl/DtDzRQiSni8T8HN7Epc2uQ1W49iGgVV27pk8hKcyj+31Aj Hp8dewTcpjJTZ7JEEwvk/y+C2/hE2OCwX53YT/BKDvZ/KAGvFYxRt0Gn9tEzYf1kVHPz dJIKXGFDJJMbUMSAF8a+ixSPRqn8XOvXOS/M2PPIqeK5bxfKQwXHvmKm3I9xhCsMcEBy GPFhAdroz30zJ/WeQt6eRwyBk8Hxw9ipgAusmTUsHY4vhA5jXoXq8lWPWRon7UIsQVw3 JAbQ== 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=tQ5Ey0GxlvfOuwjDlz/RC7ExtmJfITLgsdhieZAj2To=; b=NtT/XsRtA8FdbQoNSLjOlwVLTTXbUKk3Oj8DMPgfmh9nztYYo2utE/kN491uXM2rsE uOzy7hWEDZuScy043s5u3gEGH4Crifd2pWAOLSvKi43/GEYpkE1AlVcm22r337QI5xa/ Qo2Pq2sGEpHbQqZgJz20k5XTN2odpbQo8F18/6pwFuXSJSZiMSAw7qbuV4BhDPuWK2nW 7Vwil2qjekSHGL4uAITC9trzgOnO7U2szpanx2y4FKPgAx7vSLOYHZ9YyR0Xplr7RREL uzPNzdzCd21XTpl0YXLXjy2tn9vhV+R169SKYfbz78oOxDin/giI8W2MsWN6o6GgedGs WE7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aSXm2XUw; 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 v202-20020a6361d3000000b0050f89421524si6729651pgb.319.2023.05.11.07.27.00; Thu, 11 May 2023 07:27:16 -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=aSXm2XUw; 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 S238305AbjEKOU7 (ORCPT + 99 others); Thu, 11 May 2023 10:20:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238200AbjEKOUt (ORCPT ); Thu, 11 May 2023 10:20:49 -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 677272711 for ; Thu, 11 May 2023 07:20:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683814804; 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=tQ5Ey0GxlvfOuwjDlz/RC7ExtmJfITLgsdhieZAj2To=; b=aSXm2XUwB6QacW6hEVrt6w+6OAiM+QSQG+7DAs+yKC3WNHu5D8z7AAyy/jh0G36GnQr3F/ sQTRyaSnjUoX/qA2h8t3ID4duyseZiHYt9MHAb6fCbR6lzUUDmc5ZVhpff5h6qr6i5Nvta Ly4xtawkXKpBZ/h6b9u1j2Ju9duZvmE= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-50-OPH85rrYOH2N3rYJ7EZTyA-1; Thu, 11 May 2023 10:20:03 -0400 X-MC-Unique: OPH85rrYOH2N3rYJ7EZTyA-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3f38fe55146so31487021cf.3 for ; Thu, 11 May 2023 07:20:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683814802; x=1686406802; 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=tQ5Ey0GxlvfOuwjDlz/RC7ExtmJfITLgsdhieZAj2To=; b=G0VXHkXA30t/6YKavICFk8ne0jcSGA61BgtaGqsl0DV/ZBqUYWtMqAS8FwgLpt4NaG mDnpg6aYvktQAPf18aRNMuJGSlAqepanjnbON7XbHLLYfNfX20CThCGBXirOnPUhePCO pGmD997sblCrvR4MvNcQBQNJNzKlk3pVPqYcAxe9gTnez1zKBf3TbR/gHlXlxT9wJ93s tly5V6Z6FgX2WYzAsP7K1dH9ZMT+zbXV5GM1+/pWYhWQiF1KhhKzlVxTnNXKEg6sz+qQ yH9IBBu3++8vRyZPs5uj3l01tlnAhDMJzDE9SBe0ahJrKPKQMYWOCnDOzTMUbTeNwMGv WJUA== X-Gm-Message-State: AC+VfDwAHy6NfeL4DkzG50xHYJrZOP8LUcheyiHWtCrby8JkzJr8D2vg eOr8JeGmMtIr/JIzq/z67josA7AKV3fVxDJYjYd3lsBUMJcAibFP2uPWUw+cqFn7Mn7ntjJBC/F 5xChhE2CbllC9hiHXv+oR4g4= X-Received: by 2002:a05:622a:1b92:b0:3f3:a36d:1bd with SMTP id bp18-20020a05622a1b9200b003f3a36d01bdmr11928283qtb.44.1683814802073; Thu, 11 May 2023 07:20:02 -0700 (PDT) X-Received: by 2002:a05:622a:1b92:b0:3f3:a36d:1bd with SMTP id bp18-20020a05622a1b9200b003f3a36d01bdmr11928248qtb.44.1683814801750; Thu, 11 May 2023 07:20:01 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-28-169.cust.vodafonedsl.it. [2.34.28.169]) by smtp.gmail.com with ESMTPSA id oo11-20020a05620a530b00b0074db94ed42fsm965516qkn.116.2023.05.11.07.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 May 2023 07:20:01 -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 v5 3/4] fpga: add fake FPGA region Date: Thu, 11 May 2023 16:19:21 +0200 Message-Id: <20230511141922.437328-4-marpagan@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230511141922.437328-1-marpagan@redhat.com> References: <20230511141922.437328-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 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 | 245 ++++++++++++++++++++++++++ drivers/fpga/tests/fake-fpga-region.h | 40 +++++ 2 files changed, 285 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..020c3ad13812 --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-region.c @@ -0,0 +1,245 @@ +// 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_pdata { + struct kunit *test; + struct fpga_manager *mgr; +}; + +struct bridge_elem { + struct fpga_bridge *bridge; + struct list_head node; +}; + +struct fake_region_priv { + struct list_head bridge_list; + struct fake_region_pdata *pdata; +}; + +/** + * fake_fpga_region_register() - register a fake FPGA region. + * @mgr: associated FPGA manager. + * @parent: parent device. + * @test: KUnit test context object. + * + * Return: pointer to a new fake FPGA region on success, an ERR_PTR() encoded + * error code on failure. + */ +struct fake_fpga_region * +fake_fpga_region_register(struct kunit *test, struct fpga_manager *mgr, + struct device *parent) +{ + struct fake_fpga_region *region_ctx; + struct fake_region_pdata pdata; + int ret; + + region_ctx = kunit_kzalloc(test, sizeof(*region_ctx), GFP_KERNEL); + if (!region_ctx) { + ret = -ENOMEM; + goto err_mem; + } + + region_ctx->pdev = platform_device_alloc(FAKE_FPGA_REGION_DEV_NAME, + PLATFORM_DEVID_AUTO); + if (!region_ctx->pdev) { + pr_err("Fake FPGA region device allocation failed\n"); + ret = -ENOMEM; + goto err_mem; + } + + pdata.mgr = mgr; + pdata.test = test; + platform_device_add_data(region_ctx->pdev, &pdata, sizeof(pdata)); + + region_ctx->pdev->dev.parent = parent; + ret = platform_device_add(region_ctx->pdev); + if (ret) { + pr_err("Fake FPGA region device add failed\n"); + goto err_pdev; + } + + region_ctx->region = platform_get_drvdata(region_ctx->pdev); + + kunit_info(test, "Fake FPGA region %u: registered\n", + region_ctx->region->dev.id); + + return region_ctx; + +err_pdev: + platform_device_put(region_ctx->pdev); +err_mem: + return ERR_PTR(ret); +} +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; + u32 id; + + if (!region_ctx) + return; + + id = region_ctx->region->dev.id; + priv = region_ctx->region->priv; + test = priv->pdata->test; + + platform_device_unregister(region_ctx->pdev); + + kunit_info(test, "Fake FPGA region %u: 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. + */ +int fake_fpga_region_add_bridge(struct fake_fpga_region *region_ctx, + struct fpga_bridge *bridge) +{ + struct fake_region_priv *priv; + struct bridge_elem *elem; + + priv = region_ctx->region->priv; + + elem = devm_kzalloc(®ion_ctx->pdev->dev, sizeof(*elem), GFP_KERNEL); + if (!elem) + return -ENOMEM; + + /* Add bridge to the list of bridges in the private context */ + elem->bridge = bridge; + list_add(&elem->node, &priv->bridge_list); + + kunit_info(priv->pdata->test, "Bridge: %u added to fake FPGA region: %u\n", + bridge->dev.id, region_ctx->region->dev.id); + + return 0; +} +EXPORT_SYMBOL_GPL(fake_fpga_region_add_bridge); + +int fake_fpga_region_program(struct fake_fpga_region *region_ctx) +{ + int ret; + + ret = fpga_region_program_fpga(region_ctx->region); + + /* fpga_region_program_fpga() already puts the bridges in case of errors */ + if (!ret) + fpga_bridges_put(®ion_ctx->region->bridge_list); + + return ret; +} +EXPORT_SYMBOL_GPL(fake_fpga_region_program); + +static int fake_region_get_bridges(struct fpga_region *region) +{ + struct fake_region_priv *priv; + struct bridge_elem *elem; + int ret; + + priv = region->priv; + + /* Copy the list of bridges from the private context to the region */ + list_for_each_entry(elem, &priv->bridge_list, node) { + ret = fpga_bridge_get_to_list(elem->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_pdata *pdata; + struct fake_region_priv *priv; + struct fpga_region_info info; + + dev = &pdev->dev; + pdata = dev_get_platdata(dev); + + if (!pdata) { + dev_err(&pdev->dev, "Fake FPGA region probe: 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); + + priv->pdata = pdata; + INIT_LIST_HEAD(&priv->bridge_list); + + 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_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_LICENSE("GPL"); diff --git a/drivers/fpga/tests/fake-fpga-region.h b/drivers/fpga/tests/fake-fpga-region.h new file mode 100644 index 000000000000..c72992cbb218 --- /dev/null +++ b/drivers/fpga/tests/fake-fpga-region.h @@ -0,0 +1,40 @@ +/* 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; +}; + +struct fake_fpga_region * +fake_fpga_region_register(struct kunit *test, struct fpga_manager *mgr, + struct device *parent); + +int fake_fpga_region_add_bridge(struct fake_fpga_region *region_ctx, + struct fpga_bridge *bridge); + +int fake_fpga_region_program(struct fake_fpga_region *region_ctx); + +void fake_fpga_region_unregister(struct fake_fpga_region *region_ctx); + +#endif /* __FPGA_FAKE_RGN_H */ -- 2.40.1