From patchwork Thu Aug 29 16:11:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 13783415 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21D9E1B3B1D for ; Thu, 29 Aug 2024 16:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948000; cv=none; b=vDp9YV68edrQCO1gB/VszFpnuooYxNM1PcinlHVr3puYchNNy/6GvyWzgt7DSkijxshQY7wd+K32DbLtUACRi/d5/pViN8MKgKkH3mEdd2kmayJ2o3C/QH241C40kJuw6vzxnotGtlSM36e0OCXI4deUUC/MJfOyZ1qvskmmHno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948000; c=relaxed/simple; bh=Pby3DcZyp14RhAJleSoL2znXIzFMNtZrCFUDqVhutQM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cAnO2noaetZ1MwkJPbESST8Y+xITyHTHp97a7MG9gqr/FBc0G68aP0O3j2DTZT2TywwSzRzIGffGJfxFZThFPMUFvurpstlS98PqsMaLUB9NLZoKqi9/rRc/orhMg5AGlT+tUcA0kR8Wiv6xA2aP3s0H8isjnx/zezTS7pQ/ZxE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gE7uyWZz; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gE7uyWZz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724947998; 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=BHimdIgejLVZPp9ejvAk3wHq7yXS/6u29ozuQ5c+vvI=; b=gE7uyWZzYn+9TLFfLuOpZ51loR4QOP36kilsKVplKspPHxuhls+ID/bwmHXOR++PJi7XPL 0yraxVAE0tu/UjyX5IYiGoj2pIyAEZ7IwkS12DOOKcAymheVmBBYzsA9ody5NkLAKSCf8E MxHrosn2kS21TaQ8zlKuhAv44WEnNVM= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-682-Dyel0zHIPsOwWpnsL2v1sA-1; Thu, 29 Aug 2024 12:13:17 -0400 X-MC-Unique: Dyel0zHIPsOwWpnsL2v1sA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5BF2F1956048; Thu, 29 Aug 2024 16:13:15 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.194.81]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 47AE619560AA; Thu, 29 Aug 2024 16:13:11 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, treding@nvidia.com, vbhadram@nvidia.com, jonathanh@nvidia.com, mperttunen@nvidia.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, alex.williamson@redhat.com, clg@redhat.com, alexandre.torgue@foss.st.com, joabreu@synopsys.com Cc: msalter@redhat.com Subject: [RFC PATCH 1/5] vfio_platform: Introduce vfio_platform_get_region helper Date: Thu, 29 Aug 2024 18:11:05 +0200 Message-ID: <20240829161302.607928-2-eric.auger@redhat.com> In-Reply-To: <20240829161302.607928-1-eric.auger@redhat.com> References: <20240829161302.607928-1-eric.auger@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Reset modules need to access some specific regions. It may easier and safer to refer to those regions using their name instead of relying on their index. So let's introduce a helper that looks for the struct vfio_platform_region with a given name. Signed-off-by: Eric Auger --- I don't know if reg names described in binding yaml are guaranteed to appear in the listed order. I guess no, hence the reg-names. In my case, for the host tegra234 dt node, regs I even observe regs that are not documented in the yaml: mac, xpcs, macsec-base, hypervisor whereas yaml only describes hypervisor, mac, xpcs --- drivers/vfio/platform/vfio_platform_common.c | 14 ++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c index e53757d1d095..6861f977fd5b 100644 --- a/drivers/vfio/platform/vfio_platform_common.c +++ b/drivers/vfio/platform/vfio_platform_common.c @@ -153,6 +153,7 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) vdev->regions[i].addr = res->start; vdev->regions[i].size = resource_size(res); vdev->regions[i].flags = 0; + vdev->regions[i].name = res->name; switch (resource_type(res)) { case IORESOURCE_MEM: @@ -188,6 +189,19 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) return -EINVAL; } +struct vfio_platform_region* +vfio_platform_get_region(struct vfio_platform_device *vdev, const char *name) +{ + int i; + + for (i = 0; i < vdev->num_regions; i++) { + if (!strcmp(vdev->regions[i].name, name)) + return &vdev->regions[i]; + } + return NULL; +} +EXPORT_SYMBOL_GPL(vfio_platform_get_region); + static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev) { int i; diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h index 8d8fab516849..20d67634bc41 100644 --- a/drivers/vfio/platform/vfio_platform_private.h +++ b/drivers/vfio/platform/vfio_platform_private.h @@ -37,6 +37,7 @@ struct vfio_platform_region { resource_size_t size; u32 flags; u32 type; + const char *name; #define VFIO_PLATFORM_REGION_TYPE_MMIO 1 #define VFIO_PLATFORM_REGION_TYPE_PIO 2 void __iomem *ioaddr; @@ -104,6 +105,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev, void __vfio_platform_register_reset(struct vfio_platform_reset_node *n); void vfio_platform_unregister_reset(const char *compat, vfio_platform_reset_fn_t fn); + +struct vfio_platform_region * +vfio_platform_get_region(struct vfio_platform_device *vdev, const char *name); + #define vfio_platform_register_reset(__compat, __reset) \ static struct vfio_platform_reset_node __reset ## _node = { \ .owner = THIS_MODULE, \ From patchwork Thu Aug 29 16:11:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 13783416 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8940F1B3B1D for ; Thu, 29 Aug 2024 16:13:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948010; cv=none; b=QV9gh2O4ZUpsckRJewZtdTOAHV+rdctWQASp3QmlIunGu3DUVIisquKEFxXSO+lL7f5/HwwzQsCSVpXdloJ5LbMxAFFsaidUkso6uEbwQvecL2YnitkYfroEHa5ztxwxTB1bwt2PPoajQXJ8iZw2f34mWhfZ6xNqJ4hpJrtIGiE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948010; c=relaxed/simple; bh=0yy5iUoKy2vrtGKhr9hc57BtnHqHx2r4t7m4HfQ00Pc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eOiwknedJkJ0UY5xIjZZVDy9JbPblvCc1SIXCpfshug54SybtDCsG1CmhnPGKwyR207cF+ik1fDUuBKlL7W60+xFnWmq+cTIRH1onQNxWf6b50A3xchMGXv/wnNf/jI/fpCNeY31MzxGEp5QpHk/Yak//lioljq8lrXXg/k5sFQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Z/16b3O8; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Z/16b3O8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724948007; 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=35i3askJsl1I70Dcy0TfHIaK8iQIge/3kjnUMPnNyR0=; b=Z/16b3O8sM2FcMjBxc1X/r3ftuvtXy+5l2RSxotj7cgbtQg+Z20Uf1U3kabfCbX7hjFy/Y 0CmD4WcnptrWkj78YY8lnGZffYvsFKaRZjxpvzdXuhN7Lnc++yzgc0RUzN569oFJIXXCc6 pxOz6j7FeWT5tYm9CxUXI5LQCp/FfZU= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-684-lSj4oPAcN3Wh95k1f_pyaQ-1; Thu, 29 Aug 2024 12:13:21 -0400 X-MC-Unique: lSj4oPAcN3Wh95k1f_pyaQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 68DEF19560A2; Thu, 29 Aug 2024 16:13:20 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.194.81]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D80861955BE3; Thu, 29 Aug 2024 16:13:15 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, treding@nvidia.com, vbhadram@nvidia.com, jonathanh@nvidia.com, mperttunen@nvidia.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, alex.williamson@redhat.com, clg@redhat.com, alexandre.torgue@foss.st.com, joabreu@synopsys.com Cc: msalter@redhat.com Subject: [RFC PATCH 2/5] vfio_platform: reset: Prepare for additional reset ops Date: Thu, 29 Aug 2024 18:11:06 +0200 Message-ID: <20240829161302.607928-3-eric.auger@redhat.com> In-Reply-To: <20240829161302.607928-1-eric.auger@redhat.com> References: <20240829161302.607928-1-eric.auger@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Reset modules currently offer a single reset function that gets called on open, close and VFIO_DEVICE_RESET ioctl. For more complex devices this infrastructure looks too simplistic. Indeed some resources may be needed from the open() until the close(), like clocks, resets. A single function does not allow that setup. So let's encapsulate the current reset function into an ops struct that will be soon extended with new open and close callbacks. Existing reset modules are adapted. Signed-off-by: Eric Auger --- .../platform/reset/vfio_platform_amdxgbe.c | 7 +++- .../reset/vfio_platform_calxedaxgmac.c | 7 +++- drivers/vfio/platform/vfio_platform_common.c | 36 ++++++++++--------- drivers/vfio/platform/vfio_platform_private.h | 30 ++++++++++------ 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c b/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c index abdca900802d..6d1b641480f4 100644 --- a/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c +++ b/drivers/vfio/platform/reset/vfio_platform_amdxgbe.c @@ -109,7 +109,12 @@ static int vfio_platform_amdxgbe_reset(struct vfio_platform_device *vdev) return 0; } -module_vfio_reset_handler("amd,xgbe-seattle-v1a", vfio_platform_amdxgbe_reset); +static const struct vfio_platform_reset_ops +vfio_platform_amdxgbe_reset_ops = { + .reset = vfio_platform_amdxgbe_reset, +}; + +module_vfio_reset_handler("amd,xgbe-seattle-v1a", vfio_platform_amdxgbe_reset_ops); MODULE_VERSION("0.1"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c b/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c index 63cc7f0b2e4a..392a8579f491 100644 --- a/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c +++ b/drivers/vfio/platform/reset/vfio_platform_calxedaxgmac.c @@ -66,7 +66,12 @@ static int vfio_platform_calxedaxgmac_reset(struct vfio_platform_device *vdev) return 0; } -module_vfio_reset_handler("calxeda,hb-xgmac", vfio_platform_calxedaxgmac_reset); +static const struct vfio_platform_reset_ops +vfio_platform_calxedaxgmac_reset_ops = { + .reset = vfio_platform_calxedaxgmac_reset, +}; + +module_vfio_reset_handler("calxeda,hb-xgmac", vfio_platform_calxedaxgmac_reset_ops); MODULE_VERSION(DRIVER_VERSION); MODULE_LICENSE("GPL v2"); diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c index 6861f977fd5b..3be08e58365b 100644 --- a/drivers/vfio/platform/vfio_platform_common.c +++ b/drivers/vfio/platform/vfio_platform_common.c @@ -28,23 +28,23 @@ static LIST_HEAD(reset_list); static DEFINE_MUTEX(driver_lock); -static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat, - struct module **module) +static const struct vfio_platform_reset_ops * +vfio_platform_lookup_reset(const char *compat, struct module **module) { + const struct vfio_platform_reset_ops *ops = NULL; struct vfio_platform_reset_node *iter; - vfio_platform_reset_fn_t reset_fn = NULL; mutex_lock(&driver_lock); list_for_each_entry(iter, &reset_list, link) { if (!strcmp(iter->compat, compat) && try_module_get(iter->owner)) { *module = iter->owner; - reset_fn = iter->of_reset; + ops = &iter->ops; break; } } mutex_unlock(&driver_lock); - return reset_fn; + return ops; } static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev, @@ -106,7 +106,7 @@ static bool vfio_platform_has_reset(struct vfio_platform_device *vdev) if (VFIO_PLATFORM_IS_ACPI(vdev)) return vfio_platform_acpi_has_reset(vdev); - return vdev->of_reset ? true : false; + return vdev->reset_ops ? true : false; } static int vfio_platform_get_reset(struct vfio_platform_device *vdev) @@ -114,15 +114,15 @@ static int vfio_platform_get_reset(struct vfio_platform_device *vdev) if (VFIO_PLATFORM_IS_ACPI(vdev)) return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT; - vdev->of_reset = vfio_platform_lookup_reset(vdev->compat, - &vdev->reset_module); - if (!vdev->of_reset) { + vdev->reset_ops = vfio_platform_lookup_reset(vdev->compat, + &vdev->reset_module); + if (!vdev->reset_ops) { request_module("vfio-reset:%s", vdev->compat); - vdev->of_reset = vfio_platform_lookup_reset(vdev->compat, - &vdev->reset_module); + vdev->reset_ops = vfio_platform_lookup_reset(vdev->compat, + &vdev->reset_module); } - return vdev->of_reset ? 0 : -ENOENT; + return vdev->reset_ops ? 0 : -ENOENT; } static void vfio_platform_put_reset(struct vfio_platform_device *vdev) @@ -130,7 +130,7 @@ static void vfio_platform_put_reset(struct vfio_platform_device *vdev) if (VFIO_PLATFORM_IS_ACPI(vdev)) return; - if (vdev->of_reset) + if (vdev->reset_ops) module_put(vdev->reset_module); } @@ -219,9 +219,9 @@ static int vfio_platform_call_reset(struct vfio_platform_device *vdev, if (VFIO_PLATFORM_IS_ACPI(vdev)) { dev_info(vdev->device, "reset\n"); return vfio_platform_acpi_call_reset(vdev, extra_dbg); - } else if (vdev->of_reset) { + } else if (vdev->reset_ops && vdev->reset_ops->reset) { dev_info(vdev->device, "reset\n"); - return vdev->of_reset(vdev); + return vdev->reset_ops->reset(vdev); } dev_warn(vdev->device, "no reset function found!\n"); @@ -686,13 +686,15 @@ void __vfio_platform_register_reset(struct vfio_platform_reset_node *node) EXPORT_SYMBOL_GPL(__vfio_platform_register_reset); void vfio_platform_unregister_reset(const char *compat, - vfio_platform_reset_fn_t fn) + struct vfio_platform_reset_ops ops) { struct vfio_platform_reset_node *iter, *temp; mutex_lock(&driver_lock); list_for_each_entry_safe(iter, temp, &reset_list, link) { - if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) { + if (!strcmp(iter->compat, compat) && + !memcmp(&iter->ops, &ops, + sizeof(struct vfio_platform_reset_ops))) { list_del(&iter->link); break; } diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h index 20d67634bc41..90c99d2e70f4 100644 --- a/drivers/vfio/platform/vfio_platform_private.h +++ b/drivers/vfio/platform/vfio_platform_private.h @@ -65,18 +65,26 @@ struct vfio_platform_device { struct resource* (*get_resource)(struct vfio_platform_device *vdev, int i); int (*get_irq)(struct vfio_platform_device *vdev, int i); - int (*of_reset)(struct vfio_platform_device *vdev); + const struct vfio_platform_reset_ops *reset_ops; bool reset_required; }; -typedef int (*vfio_platform_reset_fn_t)(struct vfio_platform_device *vdev); +/** + * struct vfio_platform_reset_ops - reset ops + * + * @reset: reset function (required) + */ +struct vfio_platform_reset_ops { + int (*reset)(struct vfio_platform_device *vdev); +}; + struct vfio_platform_reset_node { struct list_head link; char *compat; struct module *owner; - vfio_platform_reset_fn_t of_reset; + struct vfio_platform_reset_ops ops; }; int vfio_platform_init_common(struct vfio_platform_device *vdev); @@ -104,29 +112,29 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev, void __vfio_platform_register_reset(struct vfio_platform_reset_node *n); void vfio_platform_unregister_reset(const char *compat, - vfio_platform_reset_fn_t fn); + struct vfio_platform_reset_ops ops); struct vfio_platform_region * vfio_platform_get_region(struct vfio_platform_device *vdev, const char *name); -#define vfio_platform_register_reset(__compat, __reset) \ -static struct vfio_platform_reset_node __reset ## _node = { \ +#define vfio_platform_register_reset(__compat, __ops) \ +static struct vfio_platform_reset_node __ops ## _node = { \ .owner = THIS_MODULE, \ .compat = __compat, \ - .of_reset = __reset, \ + .ops = __ops, \ }; \ -__vfio_platform_register_reset(&__reset ## _node) +__vfio_platform_register_reset(&__ops ## _node) -#define module_vfio_reset_handler(compat, reset) \ +#define module_vfio_reset_handler(compat, ops) \ MODULE_ALIAS("vfio-reset:" compat); \ static int __init reset ## _module_init(void) \ { \ - vfio_platform_register_reset(compat, reset); \ + vfio_platform_register_reset(compat, ops); \ return 0; \ }; \ static void __exit reset ## _module_exit(void) \ { \ - vfio_platform_unregister_reset(compat, reset); \ + vfio_platform_unregister_reset(compat, ops); \ }; \ module_init(reset ## _module_init); \ module_exit(reset ## _module_exit) From patchwork Thu Aug 29 16:11:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 13783417 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E77591B5EC8 for ; Thu, 29 Aug 2024 16:13:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948014; cv=none; b=T60QqfNS4O04ymo3uwOSrHH1rld2NTBwIDUZKzGUQuFShlzrsQfDOwdeJua8IT1XHdBc2MFqmFFU7W6tT9abm9H5Ui9EEBBb6Zl5VXFZFng1dG0RfycwPQ4xIQurR0tX1i+173Lc3X0CYC0YSsTDI6+9VVhHk/3CSwVj4TrwzEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948014; c=relaxed/simple; bh=xPTFMuxVtPpAym4xaSVVqwn/CComwPllriz2X4FnyM0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qk7OC8jLja6Gg+QLiN5p3DG7V90wo0kKcWpShQIAOFugKaAAWbfb7zvpXLgNuUZ+oMQntHxQegc5LqzgjkKl1UxZj6n2YC6OanmSmD6wmJg1dxUbOCpiotZora29+vf3OTwWsl0WOUlQYqRDh7Anwz4S8PQxOTVCJHwAfAXCwt8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=IEeBfs6b; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IEeBfs6b" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724948012; 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=w+u00/1FYth14WBO1XmelUDfBU9Sqo7la7JVqYZPhE8=; b=IEeBfs6b5ebI+xnPp1wat22dz5sRhLmIiKjOCGeYuD1kaCknYzgLPJIH38pxmENQnHm7Qi 6TzpBl7MlKdLFxovxpwlY4MhI3hguzcY4XF6qOOUSCAywGhNqEL0tg8wF/zx5DEiPcPqh8 IEc5s4/IOzZf/WlTKkTwHqjrviXxZ6g= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-587-5vDtc5hMOcu9_lv2cUSCaw-1; Thu, 29 Aug 2024 12:13:26 -0400 X-MC-Unique: 5vDtc5hMOcu9_lv2cUSCaw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A803B1955D45; Thu, 29 Aug 2024 16:13:24 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.194.81]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8539C19560AA; Thu, 29 Aug 2024 16:13:20 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, treding@nvidia.com, vbhadram@nvidia.com, jonathanh@nvidia.com, mperttunen@nvidia.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, alex.williamson@redhat.com, clg@redhat.com, alexandre.torgue@foss.st.com, joabreu@synopsys.com Cc: msalter@redhat.com Subject: [RFC PATCH 3/5] vfio_platform: reset: Introduce new open and close callbacks Date: Thu, 29 Aug 2024 18:11:07 +0200 Message-ID: <20240829161302.607928-4-eric.auger@redhat.com> In-Reply-To: <20240829161302.607928-1-eric.auger@redhat.com> References: <20240829161302.607928-1-eric.auger@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Some devices may require resources such as clocks and resets which cannot be handled in the vfio_platform agnostic code. Let's add 2 new callbacks to handle those resources. Those new callbacks are optional, as opposed to the reset callback. In case they are implemented, both need to be. They are not implemented by the existing reset modules. Signed-off-by: Eric Auger --- drivers/vfio/platform/vfio_platform_common.c | 28 ++++++++++++++++++- drivers/vfio/platform/vfio_platform_private.h | 6 ++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c index 3be08e58365b..2174e402dc70 100644 --- a/drivers/vfio/platform/vfio_platform_common.c +++ b/drivers/vfio/platform/vfio_platform_common.c @@ -228,6 +228,23 @@ static int vfio_platform_call_reset(struct vfio_platform_device *vdev, return -EINVAL; } +static void vfio_platform_reset_module_close(struct vfio_platform_device *vpdev) +{ + if (VFIO_PLATFORM_IS_ACPI(vpdev)) + return; + if (vpdev->reset_ops && vpdev->reset_ops->close) + vpdev->reset_ops->close(vpdev); +} + +static int vfio_platform_reset_module_open(struct vfio_platform_device *vpdev) +{ + if (VFIO_PLATFORM_IS_ACPI(vpdev)) + return 0; + if (vpdev->reset_ops && vpdev->reset_ops->open) + return vpdev->reset_ops->open(vpdev); + return 0; +} + void vfio_platform_close_device(struct vfio_device *core_vdev) { struct vfio_platform_device *vdev = @@ -242,6 +259,7 @@ void vfio_platform_close_device(struct vfio_device *core_vdev) "reset driver is required and reset call failed in release (%d) %s\n", ret, extra_dbg ? extra_dbg : ""); } + vfio_platform_reset_module_close(vdev); pm_runtime_put(vdev->device); vfio_platform_regions_cleanup(vdev); vfio_platform_irq_cleanup(vdev); @@ -265,7 +283,13 @@ int vfio_platform_open_device(struct vfio_device *core_vdev) ret = pm_runtime_get_sync(vdev->device); if (ret < 0) - goto err_rst; + goto err_rst_open; + + ret = vfio_platform_reset_module_open(vdev); + if (ret) { + dev_info(vdev->device, "reset module load failed (%d)\n", ret); + goto err_rst_open; + } ret = vfio_platform_call_reset(vdev, &extra_dbg); if (ret && vdev->reset_required) { @@ -278,6 +302,8 @@ int vfio_platform_open_device(struct vfio_device *core_vdev) return 0; err_rst: + vfio_platform_reset_module_close(vdev); +err_rst_open: pm_runtime_put(vdev->device); vfio_platform_irq_cleanup(vdev); err_irq: diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h index 90c99d2e70f4..528b01c56de6 100644 --- a/drivers/vfio/platform/vfio_platform_private.h +++ b/drivers/vfio/platform/vfio_platform_private.h @@ -74,9 +74,13 @@ struct vfio_platform_device { * struct vfio_platform_reset_ops - reset ops * * @reset: reset function (required) + * @open: Called when the first fd is opened for this device (optional) + * @close: Called when the last fd is closed for this device (optional) */ struct vfio_platform_reset_ops { int (*reset)(struct vfio_platform_device *vdev); + int (*open)(struct vfio_platform_device *vdev); + void (*close)(struct vfio_platform_device *vdev); }; @@ -129,6 +133,8 @@ __vfio_platform_register_reset(&__ops ## _node) MODULE_ALIAS("vfio-reset:" compat); \ static int __init reset ## _module_init(void) \ { \ + if (!!ops.open ^ !!ops.close) \ + return -EINVAL; \ vfio_platform_register_reset(compat, ops); \ return 0; \ }; \ From patchwork Thu Aug 29 16:11:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 13783418 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 207641B5318 for ; Thu, 29 Aug 2024 16:13:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948022; cv=none; b=c7fje2d7+TCJGHEE8291GCTzWOjgiiVDLvzTIoVHFGQv76QStz1Xf4btDBCGBN42B8XNknL3QNYu5053/EM0pBEoo0RB4tBBYhQTYY500Zqo/omOX4ophpt88kpzmJhx1ozPDOuGRUx6/pWG2M7c0piEU2n6oFNEfPOcFWm1Mnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948022; c=relaxed/simple; bh=cOkyuS/zD7QL6cWoaY/bqydY0jYXRoKOvV7Ranrv/Jc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f/zgnqMIb9Gi8j2xhJk5aSeVcQHud9rKb5T0DqqkK4W/Y79LNtC2QtgiDHO6duXAWFjJ1CCj7Jf/xBQKJK8v5pNdjk4X97MWt6AUlWzITlGpyz4aLug6hCUSP4MNR7zyWGqcfkXKl0AJpvmLiy5H/EAkkmtgaXxshdbky3BeI/4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QRWMbX0C; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QRWMbX0C" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724948019; 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=PtSiYz+D15CV1ChqO1OH2LgkiaCj9sIiJpBCK1lkoa4=; b=QRWMbX0C6fNvrMdRdfR5bHeVt5ioBwIEvI7/80Pv3WGDR+bSS4fs+aOBXcXQgcjb8p68ME lHdgxiKOX7DasXvq2sW4iWMvHA/Wmqsaai/NA2Abu9C3/Ph46VeVTsksR1xPOZBIJY9w8i WjKIMG30VkE4Ff2iKYqG0MvcFMTXoHo= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-328-b-hN3lV4OrapPRMNLgy-NA-1; Thu, 29 Aug 2024 12:13:35 -0400 X-MC-Unique: b-hN3lV4OrapPRMNLgy-NA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 692D51955D4C; Thu, 29 Aug 2024 16:13:30 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.194.81]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0BC5F19560AA; Thu, 29 Aug 2024 16:13:24 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, treding@nvidia.com, vbhadram@nvidia.com, jonathanh@nvidia.com, mperttunen@nvidia.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, alex.williamson@redhat.com, clg@redhat.com, alexandre.torgue@foss.st.com, joabreu@synopsys.com Cc: msalter@redhat.com Subject: [RFC PATCH 4/5] vfio-platform: Add a new handle to store reset data Date: Thu, 29 Aug 2024 18:11:08 +0200 Message-ID: <20240829161302.607928-5-eric.auger@redhat.com> In-Reply-To: <20240829161302.607928-1-eric.auger@redhat.com> References: <20240829161302.607928-1-eric.auger@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Add a new field to store data used by the reset modules. Signed-off-by: Eric Auger --- drivers/vfio/platform/vfio_platform_private.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h index 528b01c56de6..0d689241be23 100644 --- a/drivers/vfio/platform/vfio_platform_private.h +++ b/drivers/vfio/platform/vfio_platform_private.h @@ -68,6 +68,8 @@ struct vfio_platform_device { const struct vfio_platform_reset_ops *reset_ops; bool reset_required; + /* This field can be used by reset driver to store some data */ + void *reset_opaque; }; /** From patchwork Thu Aug 29 16:11:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 13783419 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0B101B6544 for ; Thu, 29 Aug 2024 16:13:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948024; cv=none; b=h/4rlEMDmJJUQpd5eFSDqVu3eJHCO2uDOVrsA1etYbrX0FxDkDOhxmFa8jLWK/Yh6tkCDnv65Opjm6T98kgdn1hZ+RzwkmUtI1aSXZPOcgCz/u6hfg3XK48qsN+e3tasnVD5jluHlpfHpK/OM7Z5ElFQ4OecFFfxXkzkVCKKEdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724948024; c=relaxed/simple; bh=KCT/Im4Fvd9YLLTHIB3lZFem07rmpnb9pRKAFHkmiTg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OBjv0SzKsf+n2YLiB9Xmk8EsHzLfbPPCEDusUuWepXcZy2/0aaf5cJtVjx8Hs8LlNCBNIoKcTstonR1mDNleIoTakC6qZhux0DLyLw11y5z2xoDNIj5GDZuaIJkphHb2eOGJhJN0AwOrEY4t5zghEEv0Gx+7MZc+V4n9ShHyfdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=RWvrAfED; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RWvrAfED" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724948020; 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=9Yw/7hyVTVv1RznBSVo2rovLDwDqynFIB6TkY086V44=; b=RWvrAfEDozdZGtt1TJlObu3UXGC2FgDC2953Ogb5nk0+0LLiVfW/Fc9o2quRBzXxYwN+zi 7hnoDi6afcO+iC/uYupMLIz3MVV2wj50lTtps8ke1eNNQZuZSo3AV5YX0jVNVF1FNmMlbN nbF73AN41+TGOH0I6zpXkUoJWVZaZG8= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-558-oSfSuXTZNvm-Be-uwjs_XA-1; Thu, 29 Aug 2024 12:13:36 -0400 X-MC-Unique: oSfSuXTZNvm-Be-uwjs_XA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ED0051954B0A; Thu, 29 Aug 2024 16:13:34 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.194.81]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C1E7619560AA; Thu, 29 Aug 2024 16:13:30 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, treding@nvidia.com, vbhadram@nvidia.com, jonathanh@nvidia.com, mperttunen@nvidia.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, alex.williamson@redhat.com, clg@redhat.com, alexandre.torgue@foss.st.com, joabreu@synopsys.com Cc: msalter@redhat.com Subject: [RFC PATCH 5/5] vfio/platform: Add tegra234-mgbe vfio platform reset module Date: Thu, 29 Aug 2024 18:11:09 +0200 Message-ID: <20240829161302.607928-6-eric.auger@redhat.com> In-Reply-To: <20240829161302.607928-1-eric.auger@redhat.com> References: <20240829161302.607928-1-eric.auger@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 open and close callbacks take care of resources requested by the reset code, ie. clocks and reset. The actual reset function toggles the mac reset, disable mac ihterrupts, stop DMA requests and do a SW reset. Signed-off-by: Eric Auger --- drivers/vfio/platform/reset/Kconfig | 7 + drivers/vfio/platform/reset/Makefile | 2 + .../reset/vfio_platform_tegra234_mgbe.c | 245 ++++++++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c diff --git a/drivers/vfio/platform/reset/Kconfig b/drivers/vfio/platform/reset/Kconfig index dcc08dc145a5..3113fae21ebf 100644 --- a/drivers/vfio/platform/reset/Kconfig +++ b/drivers/vfio/platform/reset/Kconfig @@ -14,6 +14,13 @@ config VFIO_PLATFORM_AMDXGBE_RESET If you don't know what to do here, say N. +config VFIO_PLATFORM_TEGRA234_MGBE_RESET + tristate "VFIO support for NVidia tegra234 MGBE reset" + help + Enables the VFIO platform driver to handle reset for NVidia tegra234 mgbe + + If you don't know what to do here, say N. + config VFIO_PLATFORM_BCMFLEXRM_RESET tristate "VFIO support for Broadcom FlexRM reset" depends on ARCH_BCM_IPROC || COMPILE_TEST diff --git a/drivers/vfio/platform/reset/Makefile b/drivers/vfio/platform/reset/Makefile index 7294c5ea122e..5ebef71f61a0 100644 --- a/drivers/vfio/platform/reset/Makefile +++ b/drivers/vfio/platform/reset/Makefile @@ -1,7 +1,9 @@ # SPDX-License-Identifier: GPL-2.0 vfio-platform-calxedaxgmac-y := vfio_platform_calxedaxgmac.o vfio-platform-amdxgbe-y := vfio_platform_amdxgbe.o +vfio-platform-tegra234-mgbe-y := vfio_platform_tegra234_mgbe.o obj-$(CONFIG_VFIO_PLATFORM_CALXEDAXGMAC_RESET) += vfio-platform-calxedaxgmac.o obj-$(CONFIG_VFIO_PLATFORM_AMDXGBE_RESET) += vfio-platform-amdxgbe.o +obj-$(CONFIG_VFIO_PLATFORM_TEGRA234_MGBE_RESET) += vfio-platform-tegra234-mgbe.o obj-$(CONFIG_VFIO_PLATFORM_BCMFLEXRM_RESET) += vfio_platform_bcmflexrm.o diff --git a/drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c b/drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c new file mode 100644 index 000000000000..09b9e10be3ff --- /dev/null +++ b/drivers/vfio/platform/reset/vfio_platform_tegra234_mgbe.c @@ -0,0 +1,245 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * VFIO platform driver specialized for NVidia tegra234-mgbe reset + * Code is inspired from dwxgmac2_dma.c code + * + * Copyright (c) 2024 Red Hat, Inc. All rights reserved. + * Author: Eric Auger + */ + +#include +#include +#include +#include +#include + +#include "../vfio_platform_private.h" + +static const char *const mgbe_clks[] = { + "rx-pcs", "tx", "tx-pcs", "mac-divider", "mac", "mgbe", "ptp-ref", "mac" +}; + +struct tegra_mgbe { + struct clk_bulk_data *clks; + struct reset_control *mac_rst; + void __iomem *mac; +}; + +#define XGMAC_TX_CONFIG 0x00000000 +#define XGMAC_CONFIG_TE BIT(0) +#define XGMAC_RX_CONFIG 0x00000004 +#define XGMAC_CONFIG_RE BIT(0) +#define XGMAC_DMA_MODE 0x00003000 +#define XGMAC_SWR BIT(0) + +#define XGMAC_DMA_CH_INT_EN(x) (0x00003138 + (0x80 * (x))) +#define XGMAC_TIE BIT(0) +#define XGMAC_RIE BIT(6) +#define XGMAC_RBUE BIT(7) +#define XGMAC_DMA_INT_DEFAULT_RX (XGMAC_RBUE | XGMAC_RIE) +#define XGMAC_DMA_INT_DEFAULT_TX (XGMAC_TIE) + +#define XGMAC_DMA_CH_STATUS(x) (0x00003160 + (0x80 * (x))) +#define XGMAC_DMA_CH_RX_CONTROL(x) (0x00003108 + (0x80 * (x))) +#define XGMAC_RXST BIT(0) +#define XGMAC_DMA_CH_TX_CONTROL(x) (0x00003104 + (0x80 * (x))) +#define XGMAC_TXST BIT(0) + +#define XGMAC_INT_STATUS 0x000000b0 +#define XGMAC_INT_EN 0x000000b4 + +#define MGBE_WRAP_COMMON_INTR_ENABLE 0x8704 + +static int +toggle_reset(struct device *dev, const char *rst_str, struct reset_control *rst) +{ + int ret; + + ret = reset_control_assert(rst); + if (ret < 0) + dev_err(dev, "Failed to assert %s reset %d\n", + rst_str, ret); + usleep_range(2000, 4000); + + ret = reset_control_deassert(rst); + if (ret < 0) + dev_err(dev, "Failed to deassert %s reset %d\n", rst_str, ret); + usleep_range(2000, 4000); + return ret; +} + +static void stop_dma(void __iomem *mac, uint channel) +{ + u32 value; + + /* DMA Stop RX */ + value = readl(mac + XGMAC_DMA_CH_RX_CONTROL(channel)); + value &= ~XGMAC_RXST; + writel(value, mac + XGMAC_DMA_CH_RX_CONTROL(channel)); + + value = readl(mac + XGMAC_RX_CONFIG); + value &= ~XGMAC_CONFIG_RE; + writel(value, mac + XGMAC_RX_CONFIG); + + usleep_range(10, 15); + + /* DMA Stop TX */ + value = readl(mac + XGMAC_DMA_CH_TX_CONTROL(channel)); + value &= ~XGMAC_RXST; + writel(value, mac + XGMAC_DMA_CH_TX_CONTROL(channel)); + + value = readl(mac + XGMAC_TX_CONFIG); + value &= ~XGMAC_CONFIG_TE; + writel(value, mac + XGMAC_TX_CONFIG); + + usleep_range(10, 15); +} +static int dma_sw_reset(void __iomem *mac) +{ + u32 value; + + value = readl(mac + XGMAC_DMA_MODE); + writel(value | XGMAC_SWR, mac + XGMAC_DMA_MODE); + return readl_poll_timeout(mac + XGMAC_DMA_MODE, value, + !(value & XGMAC_SWR), 0, 100000); +} + +static void disable_dma_irq(void __iomem *mac, u32 channel) +{ + u32 intr_en, intr_status; + + intr_en = readl(mac + XGMAC_DMA_CH_INT_EN(channel)); + + intr_en &= ~XGMAC_DMA_INT_DEFAULT_RX; + intr_en &= ~XGMAC_DMA_INT_DEFAULT_TX; + writel(intr_en, mac + XGMAC_DMA_CH_INT_EN(channel)); + usleep_range(10, 15); + + intr_status = readl(mac + XGMAC_DMA_CH_STATUS(channel)); + writel(0, mac + XGMAC_DMA_CH_STATUS(channel)); +} + +static int prepare_enable_clocks(struct device *dev, struct clk_bulk_data **clocks) +{ + struct clk_bulk_data *clks; + int ret; + + clks = kcalloc(ARRAY_SIZE(mgbe_clks), sizeof(*clks), GFP_KERNEL); + if (!clks) + return -ENOMEM; + + for (int i = 0; i < ARRAY_SIZE(mgbe_clks); i++) + clks[i].id = mgbe_clks[i]; + + ret = clk_bulk_get(dev, ARRAY_SIZE(mgbe_clks), clks); + if (ret < 0) { + dev_err(dev, "Failed to get clocks %d\n", ret); + return ret; + } + + ret = clk_bulk_prepare_enable(ARRAY_SIZE(mgbe_clks), clks); + if (ret < 0) { + dev_err(dev, "Failed to prepare_enable clocks %d\n", ret); + clk_bulk_put(ARRAY_SIZE(mgbe_clks), clks); + return ret; + } + *clocks = clks; + return ret; +} + +static int vfio_platform_tegra234_mgbe_open(struct vfio_platform_device *vpdev) +{ + struct tegra_mgbe *mgbe; + struct vfio_platform_region *mac_regs; + struct vfio_device *vdev = &vpdev->vdev; + struct device *dev = vdev->dev; + int ret; + + mac_regs = vfio_platform_get_region(vpdev, "mac"); + if (!mac_regs) + return -EINVAL; + + mac_regs->ioaddr = ioremap(mac_regs->addr, mac_regs->size); + if (!mac_regs->ioaddr) + return -ENOMEM; + + mgbe = kmalloc(sizeof(struct tegra_mgbe), GFP_KERNEL); + if (!mgbe) { + ret = -ENOMEM; + goto iounmap; + } + + mgbe->mac = mac_regs->ioaddr; + + ret = prepare_enable_clocks(dev, &mgbe->clks); + if (ret) + goto res_err; + + mgbe->mac_rst = reset_control_get_exclusive(dev, "mac"); + if (IS_ERR(mgbe->mac_rst)) { + dev_err(dev, "Failed to get mac reset %ld\n", PTR_ERR(mgbe->mac_rst)); + ret = PTR_ERR(mgbe->mac_rst); + goto res_err; + } + vpdev->reset_opaque = mgbe; + return 0; +res_err: + kfree(mgbe); +iounmap: + iounmap(mac_regs->ioaddr); + + return ret; +} + +static void vfio_platform_tegra234_mgbe_close(struct vfio_platform_device *vpdev) +{ + struct tegra_mgbe *mgbe = vpdev->reset_opaque; + + /* iounmap is done in vfio_platform_common */ + reset_control_put(mgbe->mac_rst); + clk_bulk_disable_unprepare(ARRAY_SIZE(mgbe_clks), mgbe->clks); + clk_bulk_put(ARRAY_SIZE(mgbe_clks), mgbe->clks); + kfree(mgbe->clks); + vpdev->reset_opaque = NULL; +} + +static int vfio_platform_tegra234_mgbe_reset(struct vfio_platform_device *vpdev) +{ + struct tegra_mgbe *mgbe = vpdev->reset_opaque; + struct vfio_device *vdev = &vpdev->vdev; + struct device *dev = vdev->dev; + int ret; + + if (!mgbe) + return -ENODEV; + + toggle_reset(dev, "mac", mgbe->mac_rst); + + for (int i = 0; i < 10; i++) + disable_dma_irq(mgbe->mac, i); + + writel(0, mgbe->mac + MGBE_WRAP_COMMON_INTR_ENABLE); + + for (int i = 0; i < 10; i++) + stop_dma(mgbe->mac, i); + + ret = dma_sw_reset(mgbe->mac); + if (ret) + dev_err(dev, "Failed to reset the DMA %d\n", ret); + + return ret; +} + +static const struct vfio_platform_reset_ops +vfio_platform_tegra234_mgbe_reset_ops = { + .reset = vfio_platform_tegra234_mgbe_reset, + .open = vfio_platform_tegra234_mgbe_open, + .close = vfio_platform_tegra234_mgbe_close, +}; + +module_vfio_reset_handler("nvidia,tegra234-mgbe", vfio_platform_tegra234_mgbe_reset_ops); + +MODULE_VERSION("0.1"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Eric Auger "); +MODULE_DESCRIPTION("Reset support for NVidia tegra234 mgbe vfio platform device");