From nobody Sat Sep 21 05:29:09 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1569496961; cv=none; d=zoho.com; s=zohoarc; b=SbWUB3jrO53/o7961xfUZboBAhLjo1aojCPEucZiodt7KIArhEP22AANS9hr0K9sLbCrNNL6A3tlPcbKZGpbZesHS6ktkETu5Z0FWZ2GCFbJZkeBJaiUGKxbv3TU9HV4tNQIGxZ/eBFmYAyAfoSFb03KBGWWV63nKc0nDd5jlgU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569496961; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=V+vYUuguK7W/JhSQuxfUxy5bK63JsPGcF7opNQcOtKg=; b=A0/UlOObZBwdZcSr1B311dY6qm4oUs49gHvkSGF324rmSy+SK8MRGa8QiqGWly0g0+nNcHokH8iQRhFBGNUyfz5mkguHtQDlUdRKj70y7lgxo4lySqAUp9aFseP2Z3MRfTterZ8HGtANzY6v5PVTeXNzc2yn9zOGx4NGdwEh0Vg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 156949696151193.78679953493577; Thu, 26 Sep 2019 04:22:41 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRpt-0008SG-T3; Thu, 26 Sep 2019 11:21:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRps-0008Rd-Uw for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 11:21:24 +0000 Received: from mail-lj1-x244.google.com (unknown [2a00:1450:4864:20::244]) by localhost (Halon) with ESMTPS id b7492da2-e04f-11e9-bf31-bc764e2007e4; Thu, 26 Sep 2019 11:21:00 +0000 (UTC) Received: by mail-lj1-x244.google.com with SMTP id b20so1760940ljj.5 for ; Thu, 26 Sep 2019 04:20:59 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-22.209.223.85.sovam.net.ua. [85.223.209.22]) by smtp.gmail.com with ESMTPSA id n4sm526810lfi.63.2019.09.26.04.20.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Sep 2019 04:20:58 -0700 (PDT) X-Inumbo-ID: b7492da2-e04f-11e9-bf31-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ygXMAlCnrfkzZBZu6m4j35jgHRta1mO3lz0G6znyQH0=; b=L5zcFhAfaXRpm+Kzg/OWNZGwkMzhJXtXxDxnqLrzd0YBWte2T82pvZCrjFhUgBbkFW czk6sd83csCdg1ivUbietdJiV2DMZecRrHPMqljrxR83uoKT1U7vB7Zi2d6VAhLOZWL2 xV10miak2K8XHsfpMX4hpRhW3t5B8rKaTkMhxBm3WX8u2VQSnFO2UJJZLkol2zFHFtx6 JIgfUBqq5yCqahRxro62YtshZhULWd7wfDm5Qcg+auUwTNOE4Ap2DALYhwtvKA8UKD6G yn++wT/iOpxBdlpdvYH6TCJfAAjNWDLunwxw8bvBUf0XCnZDk54a+ufBvCM2ZA6jgiq4 lsvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ygXMAlCnrfkzZBZu6m4j35jgHRta1mO3lz0G6znyQH0=; b=nZ2k8VidJNCzmCt8i1RC86BVcxffX1eAlhPmfZyP99lphjKTtD6KYenjS9klw3ET+D gvuYJ7RQYbLAKn0DnaB+NvTzQHanVNetr+0yolsHsZBD+xfa6yCPuoHIA6AjYZN9Bqvg tzt4DVIXar6zN7eanVifCWnVKhaGAM37dxQXWObB82wP8c8DEhZxOmPFp3fXquRh5ji6 o6sDqsQHSwPO+TfGz9XxOGXRADmE7T9CPR3jm1bLFkqWdHPRTm/pHs7MWdw0TvlLMKO3 OeOMppH3CWtVutMbnB/UA6krGW0f8uvnWmFjbn5aO4b4uygtAmMYRIgaHjZsQO1EjKev C6yw== X-Gm-Message-State: APjAAAUmYqngwLImY+ZgAyiiJdRgrw0+DyYQbJ3lUAxr9hS5C1QFh2Wb E/xOZWjddOH4uKlB1FZ/IDlYGyfSqJM= X-Google-Smtp-Source: APXvYqwIYuLnmMwhb5mjdTfD3F6+GKI6A9DDx9gWy2PTsOIuaAW/77ffeS2Ucm9kez801S2tgY1v4A== X-Received: by 2002:a2e:4296:: with SMTP id h22mr2247567ljf.208.1569496858613; Thu, 26 Sep 2019 04:20:58 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Thu, 26 Sep 2019 14:20:31 +0300 Message-Id: <1569496834-7796-6-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> References: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V6 5/8] iommu/arm: Add lightweight iommu_fwspec support X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Oleksandr Tyshchenko , julien.grall@arm.com, sstabellini@kernel.org, volodymyr_babchuk@epam.com MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) From: Oleksandr Tyshchenko We need to have some abstract way to add new device to the IOMMU based on the generic IOMMU DT bindings [1] which can be used for both DT (right now) and ACPI (in future). For that reason we can borrow the idea used in Linux these days called "iommu_fwspec". Having this in, it will be possible to configure IOMMU master interfaces of the device (device IDs) from a single common place and avoid keeping almost identical look-up implementations in each IOMMU driver. There is no need to port the whole implementation of "iommu_fwspec" to Xen, we could, probably, end up with a much simpler solution, some "stripped down" version which fits our requirements. So, this patch adds the following: 1. A common structure "iommu_fwspec" to hold the the per-device firmware data 2. New member "iommu_fwspec" of struct device 3. Functions/helpers to deal with "dev->iommu_fwspec" It should be noted that in comparison of the original "iommu_fwspec" Xen's variant doesn't contain some fields, which are not really needed at the moment (ops, flag) and "iommu_fwnode" field was replaced by "iommu_dev" to avoid porting a lot of code (to support "fwnode_handle") with little benefit. The "iommu_fwspec" support is based on the Linux's commit: f74c2bb98776e2de508f4d607cd519873065118e "Linux 5.3-rc8" Subsequent patches will use of that support. [1] https://www.kernel.org/doc/Documentation/devicetree/bindings/iommu/iomm= u.txt Signed-off-by: Oleksandr Tyshchenko Acked-by: Julien Grall --- Changes V5 -> V6: - added Julien's A-b Changes V4 -> V5: - modified struct iommu_fwspec to use flexible array (uint32_t ids[]) - modified to use xzalloc_flex_struct() on the first allocation - added "const" to uint32_t *ids in iommu_fwspec_add_ids() Changes V3 -> V4: - modified iommu_fwspec_add_ids() to use new implementation of xrealloc_flex_struct() - mentioned exact Linux version we are based on - fixed Grammatical error Changes V2 -> V3: - added Copyright from Linux - ordered the headers alphabetically - removed check for not a NULL before calling xfree() - used unsigned for variables which can't be negative - removed #include from iommu.h - added check to iommu_fwspec_init() to not allow overriding - clarified comments in code - modified iommu_fwspec_add_ids() to use type-safe xrealloc_flex_struct= () --- xen/drivers/passthrough/arm/Makefile | 2 +- xen/drivers/passthrough/arm/iommu_fwspec.c | 97 ++++++++++++++++++++++++++= ++++ xen/include/asm-arm/device.h | 1 + xen/include/asm-arm/iommu_fwspec.h | 68 +++++++++++++++++++++ 4 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 xen/drivers/passthrough/arm/iommu_fwspec.c create mode 100644 xen/include/asm-arm/iommu_fwspec.h diff --git a/xen/drivers/passthrough/arm/Makefile b/xen/drivers/passthrough= /arm/Makefile index 4abb87a..5fbad45 100644 --- a/xen/drivers/passthrough/arm/Makefile +++ b/xen/drivers/passthrough/arm/Makefile @@ -1,2 +1,2 @@ -obj-y +=3D iommu.o iommu_helpers.o +obj-y +=3D iommu.o iommu_helpers.o iommu_fwspec.o obj-$(CONFIG_ARM_SMMU) +=3D smmu.o diff --git a/xen/drivers/passthrough/arm/iommu_fwspec.c b/xen/drivers/passt= hrough/arm/iommu_fwspec.c new file mode 100644 index 0000000..8c97c7c --- /dev/null +++ b/xen/drivers/passthrough/arm/iommu_fwspec.c @@ -0,0 +1,97 @@ +/* + * xen/drivers/passthrough/arm/iommu_fwspec.c + * + * Contains functions to maintain per-device firmware data + * + * Based on Linux's iommu_fwspec support you can find at: + * drivers/iommu/iommu.c + * + * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. + * + * Copyright (C) 2019 EPAM Systems Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms and conditions of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; If not, see . + */ + +#include +#include + +#include +#include + +int iommu_fwspec_init(struct device *dev, struct device *iommu_dev) +{ + struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); + + if ( fwspec ) + { + /* We expect the device to be protected by only one IOMMU. */ + if ( fwspec->iommu_dev !=3D iommu_dev ) + return -EINVAL; + + return 0; + } + + /* + * Allocate with ids[1] to avoid the re-allocation in the common case + * where a device has a single device ID. + */ + fwspec =3D xzalloc_flex_struct(struct iommu_fwspec, ids, 1); + if ( !fwspec ) + return -ENOMEM; + + fwspec->iommu_dev =3D iommu_dev; + dev_iommu_fwspec_set(dev, fwspec); + + return 0; +} + +void iommu_fwspec_free(struct device *dev) +{ + struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); + + xfree(fwspec); + dev_iommu_fwspec_set(dev, NULL); +} + +int iommu_fwspec_add_ids(struct device *dev, const uint32_t *ids, + unsigned int num_ids) +{ + struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); + unsigned int i; + + if ( !fwspec ) + return -EINVAL; + + fwspec =3D xrealloc_flex_struct(fwspec, ids, fwspec->num_ids + num_ids= ); + if ( !fwspec ) + return -ENOMEM; + + dev_iommu_fwspec_set(dev, fwspec); + + for ( i =3D 0; i < num_ids; i++ ) + fwspec->ids[fwspec->num_ids + i] =3D ids[i]; + + fwspec->num_ids +=3D num_ids; + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/include/asm-arm/device.h b/xen/include/asm-arm/device.h index ee1c3bc..ee7cff2 100644 --- a/xen/include/asm-arm/device.h +++ b/xen/include/asm-arm/device.h @@ -18,6 +18,7 @@ struct device struct dt_device_node *of_node; /* Used by drivers imported from Linux= */ #endif struct dev_archdata archdata; + struct iommu_fwspec *iommu_fwspec; /* per-device IOMMU instance data */ }; =20 typedef struct device device_t; diff --git a/xen/include/asm-arm/iommu_fwspec.h b/xen/include/asm-arm/iommu= _fwspec.h new file mode 100644 index 0000000..5cdb53f --- /dev/null +++ b/xen/include/asm-arm/iommu_fwspec.h @@ -0,0 +1,68 @@ +/* + * xen/include/asm-arm/iommu_fwspec.h + * + * Contains a common structure to hold the per-device firmware data and + * declaration of functions used to maintain that data + * + * Based on Linux's iommu_fwspec support you can find at: + * include/linux/iommu.h + * + * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. + * + * Copyright (C) 2019 EPAM Systems Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms and conditions of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; If not, see . + */ + +#ifndef __ARCH_ARM_IOMMU_FWSPEC_H__ +#define __ARCH_ARM_IOMMU_FWSPEC_H__ + +/* per-device IOMMU instance data */ +struct iommu_fwspec { + /* this device's IOMMU */ + struct device *iommu_dev; + /* IOMMU driver private data for this device */ + void *iommu_priv; + /* number of associated device IDs */ + unsigned int num_ids; + /* IDs which this device may present to the IOMMU */ + uint32_t ids[]; +}; + +int iommu_fwspec_init(struct device *dev, struct device *iommu_dev); +void iommu_fwspec_free(struct device *dev); +int iommu_fwspec_add_ids(struct device *dev, const uint32_t *ids, + unsigned int num_ids); + +static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) +{ + return dev->iommu_fwspec; +} + +static inline void dev_iommu_fwspec_set(struct device *dev, + struct iommu_fwspec *fwspec) +{ + dev->iommu_fwspec =3D fwspec; +} + +#endif /* __ARCH_ARM_IOMMU_FWSPEC_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.7.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel