From patchwork Tue Sep 24 15:30:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11159135 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B6E0913B1 for ; Tue, 24 Sep 2019 15:33:04 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 868B720665 for ; Tue, 24 Sep 2019 15:33:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dO+KRciA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 868B720665 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iCmmb-0007wh-7g; Tue, 24 Sep 2019 15:31:17 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iCmmZ-0007w5-VM for xen-devel@lists.xenproject.org; Tue, 24 Sep 2019 15:31:15 +0000 X-Inumbo-ID: 4c6f58e6-dee0-11e9-bf31-bc764e2007e4 Received: from mail-lj1-x241.google.com (unknown [2a00:1450:4864:20::241]) by localhost (Halon) with ESMTPS id 4c6f58e6-dee0-11e9-bf31-bc764e2007e4; Tue, 24 Sep 2019 15:30:55 +0000 (UTC) Received: by mail-lj1-x241.google.com with SMTP id l21so2358121lje.4 for ; Tue, 24 Sep 2019 08:30:55 -0700 (PDT) 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=DrzTrZ/qnICiIH+wPomi5xqO8UIxGT+AUn3v3IM2ulg=; b=dO+KRciAASWiYHfB13nbiaml9Xjn4GpuQ5UDxN8pjZluEksYWjbLnve/VoApSK1Su3 DsuXD/vOaRz+7oYQIbf8/Fl/U7AkzKGjrJxnqXDYyVFV7FKsRXNvqJ2Rk9hJbG5fIKjR 6ZJjfcp/zeuzEmRDwcfo/hjTHDLgxrlU57eBaNpnjr84ZtYoUeYZq0wkWxZP0pvtlVxX YhfZmEL8guJoheFzzqj2p+0m/1cxDcW7WIIAvEDQLFubNTmX/h9/WjwH+c1asbemQkLf zalnLbmDnPYj/U2x1HlOkSWIv7CxV+wO6hQhjCnYSBUwfGCIxPOaT5Mop49o1VQiOjIZ 6fPg== 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=DrzTrZ/qnICiIH+wPomi5xqO8UIxGT+AUn3v3IM2ulg=; b=bDuhTJevJucqLmYm+yGc/TMqufghVNKXn1SMB33Odhcd2qb3ij6xSg4xeRP1eeKz6g Kmwu4ZwRuPDXeMUcnkgqNrFPFgVo9b5UkQmy9uMirRrYRTLjMXJSSLYAPAJxy8MRWzkV 4gHdb0KNhJGJyPCBF5PH2ELZT2kV+kJVvIyGPA8KYQoBiX+njuG2mz5Ow1Ee0MzXzFoM l1MzCXUnL++qNUC18/oMi/Kgcl72CFYXXPuHvm2DtjQUF/E8heUjdmf3FQg6k+PRe1sc aV7DpD3puJv49rrxa0m7Hq4sadPrKKxw8udeKISOF7UmrF9W7aymFVqqm7LjhnV/uK+l 6rfA== X-Gm-Message-State: APjAAAVCRB/2vIPKBarE0SkpqDzCD2hvl1+5S0o/gPr/oHbtR5RSrJdN ouN3IP9MG/Ih8M2MD6dXVtMrWCcWki8= X-Google-Smtp-Source: APXvYqykQIRaDNSy/2QhVoqUOcT4TJ1HacgneLc/5LdyKjHU9Z738bm99dRaesHhfSDIulUjxoV1JQ== X-Received: by 2002:a05:651c:1132:: with SMTP id e18mr2445509ljo.33.1569339054185; Tue, 24 Sep 2019 08:30:54 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-74.141.223.85.sovam.net.ua. [85.223.141.74]) by smtp.gmail.com with ESMTPSA id z128sm525818lfa.1.2019.09.24.08.30.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 Sep 2019 08:30:53 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Tue, 24 Sep 2019 18:30:24 +0300 Message-Id: <1569339027-19484-6-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1569339027-19484-1-git-send-email-olekstysh@gmail.com> References: <1569339027-19484-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V5 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, Jan Beulich MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" 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/iommu.txt Signed-off-by: Oleksandr Tyshchenko CC: Julien Grall CC: Jan Beulich Acked-by: Julien Grall --- 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 += iommu.o iommu_helpers.o +obj-y += iommu.o iommu_helpers.o iommu_fwspec.o obj-$(CONFIG_ARM_SMMU) += smmu.o diff --git a/xen/drivers/passthrough/arm/iommu_fwspec.c b/xen/drivers/passthrough/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 = dev_iommu_fwspec_get(dev); + + if ( fwspec ) + { + /* We expect the device to be protected by only one IOMMU. */ + if ( fwspec->iommu_dev != 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 = xzalloc_flex_struct(struct iommu_fwspec, ids, 1); + if ( !fwspec ) + return -ENOMEM; + + fwspec->iommu_dev = iommu_dev; + dev_iommu_fwspec_set(dev, fwspec); + + return 0; +} + +void iommu_fwspec_free(struct device *dev) +{ + struct iommu_fwspec *fwspec = 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 = dev_iommu_fwspec_get(dev); + unsigned int i; + + if ( !fwspec ) + return -EINVAL; + + fwspec = xrealloc_flex_struct(fwspec, ids, fwspec->num_ids + num_ids); + if ( !fwspec ) + return -ENOMEM; + + dev_iommu_fwspec_set(dev, fwspec); + + for ( i = 0; i < num_ids; i++ ) + fwspec->ids[fwspec->num_ids + i] = ids[i]; + + fwspec->num_ids += 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 */ }; 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 = 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: + */