From patchwork Fri Aug 2 16:39:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11073905 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CE37413AC for ; Fri, 2 Aug 2019 16:41:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BC9B6286B0 for ; Fri, 2 Aug 2019 16:41:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B098A286D0; Fri, 2 Aug 2019 16:41:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0479B286B0 for ; Fri, 2 Aug 2019 16:41:28 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1htab7-0004CB-Lf; Fri, 02 Aug 2019 16:40:05 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1htab6-00045X-JV for xen-devel@lists.xenproject.org; Fri, 02 Aug 2019 16:40:04 +0000 X-Inumbo-ID: 2c5f3edf-b544-11e9-8980-bc764e045a96 Received: from mail-lj1-x241.google.com (unknown [2a00:1450:4864:20::241]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 2c5f3edf-b544-11e9-8980-bc764e045a96; Fri, 02 Aug 2019 16:40:02 +0000 (UTC) Received: by mail-lj1-x241.google.com with SMTP id h10so1991972ljg.0 for ; Fri, 02 Aug 2019 09:40:02 -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=NwFO0JUoJVO1HNR1vM9cd7WLDQQW3WhdzeYYsj8V4JE=; b=oUJGHIsrM2oUNer+vweAYjHDxMewjF/WWi95Sbsvivg3oelzehvcPYEwvKBMypJuJn +QH7yldXLsTQ3bcoNV38+35dPcoCPm49DLvjcheckHVtfvWRTA8n4RgliRpHQLQKucXK 3F1rPV+ZkEIeMyAUhx4vq8kkU8h87CvDJ6IkpsRdPqx4dNVp0A1mLe+U4Xn26yyH0Kra fztfcDeMioD9yuT+ItKoclZxAoA8AEAVmmaTclHJgWb7JtgE14PcjXW3qpf4T83/ABuV Se7x84ahH8TfgdBsslpKxR1Ztz4fhIdPv+QhvqWtoYLfCN1fsfEFrwOisMhObc6CeHkd lD+Q== 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=NwFO0JUoJVO1HNR1vM9cd7WLDQQW3WhdzeYYsj8V4JE=; b=YHmXLbIY3uTEndmP/C/S2+p1YWWBsb44MkRSCSqz7q97PUsA5feyeRWChbIjc34nqt GI1jfj8/98hVhV6XpAW5coj1BYA5gPotnDOWdDCjv/SbNNvzx6HZ8lWZ2c5KRAvjo9jY C18CWSUyVGFppHc1XHS8dqE7xPeL2Q21pUbbg7xKBUQqLIJ8KIcBR1ueG72Aw0FYn0P1 b16enHkmKegnv17Wd25a2CdFwcgR+6BQtTsRRxoVPVdUDWjhvU6Mm+Ffg5DdXCdqK3sV htRHomfQRmLEIXzdiIQX4VZG/VQt84LsYAzEWph6dLYR6Va+DYpTQzPpHkvYLV1u8RBx DjUQ== X-Gm-Message-State: APjAAAXBENj+zKam1rE1fSSX5L8MWCFMc8i8huPxt++rqId7cJoo7Njb XJBcyZw8t496eOozRv0sma1HtQIJeDY= X-Google-Smtp-Source: APXvYqwLtf6T942/1TBvC/LrxsPIOY6a6JDAtge32hzILLYWefQUYUP56UmjKKjBXzUrsJgrkCPKzA== X-Received: by 2002:a2e:9643:: with SMTP id z3mr73065183ljh.43.1564764001224; Fri, 02 Aug 2019 09:40:01 -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 q6sm15378998lji.70.2019.08.02.09.40.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 02 Aug 2019 09:40:00 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Fri, 2 Aug 2019 19:39:43 +0300 Message-Id: <1564763985-20312-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1564763985-20312-1-git-send-email-olekstysh@gmail.com> References: <1564763985-20312-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V2 4/6] 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 MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Oleksandr Tyshchenko We need to have some abstract way to add new device to the IOMMU based on the generic IOMMU DT binding [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 identifical 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 requirments. 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 comparing with 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. Next patch in this series will make use of that support. [1] https://www.kernel.org/doc/Documentation/devicetree/bindings/iommu/iommu.txt Signed-off-by: Oleksandr Tyshchenko --- xen/drivers/passthrough/arm/Makefile | 2 +- xen/drivers/passthrough/arm/iommu_fwspec.c | 91 ++++++++++++++++++++++++++++++ xen/include/asm-arm/device.h | 1 + xen/include/asm-arm/iommu.h | 2 + xen/include/asm-arm/iommu_fwspec.h | 65 +++++++++++++++++++++ 5 files changed, 160 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..3474192 --- /dev/null +++ b/xen/drivers/passthrough/arm/iommu_fwspec.c @@ -0,0 +1,91 @@ +/* + * 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) 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 ) + return 0; + + fwspec = xzalloc(struct iommu_fwspec); + 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); + + if ( fwspec ) + { + xfree(fwspec); + dev_iommu_fwspec_set(dev, NULL); + } +} + +int iommu_fwspec_add_ids(struct device *dev, uint32_t *ids, int num_ids) +{ + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); + size_t size; + int i; + + if ( !fwspec ) + return -EINVAL; + + size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); + if ( size > sizeof(*fwspec) ) + { + fwspec = _xrealloc(fwspec, size, sizeof(void *)); + 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.h b/xen/include/asm-arm/iommu.h index 20d865e..1853bd9 100644 --- a/xen/include/asm-arm/iommu.h +++ b/xen/include/asm-arm/iommu.h @@ -14,6 +14,8 @@ #ifndef __ARCH_ARM_IOMMU_H__ #define __ARCH_ARM_IOMMU_H__ +#include + struct arch_iommu { /* Private information for the IOMMU drivers */ diff --git a/xen/include/asm-arm/iommu_fwspec.h b/xen/include/asm-arm/iommu_fwspec.h new file mode 100644 index 0000000..0676285 --- /dev/null +++ b/xen/include/asm-arm/iommu_fwspec.h @@ -0,0 +1,65 @@ +/* + * 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) 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 { + /* device which represents this IOMMU H/W */ + 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[1]; +}; + +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, uint32_t *ids, 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: + */