From patchwork Thu Jan 19 19:18:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45940 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp502821wrn; Thu, 19 Jan 2023 11:21:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXuY1hgBZJR2UKGk6x0ybhtBrODFC/sJANXHe5SPfPQQk1oQDkX0Sj5c/Hl5AQL5kRbMR4qS X-Received: by 2002:a17:90a:f3cf:b0:229:557:8a49 with SMTP id ha15-20020a17090af3cf00b0022905578a49mr30453574pjb.7.1674156083317; Thu, 19 Jan 2023 11:21:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674156083; cv=none; d=google.com; s=arc-20160816; b=jB9nprq72LFVr1e/xlVQnZuiXm4T5W15s8OfQ95pMujYdpf4xNcXQFY1dQOZMGK9Vi 6cc4PLEbPHeysfmbTGWqKztvtEaS9+OKy1cc3JbnkAN95Cc1eznXZDdKWfcmzWT9+8SK 3FEYB6RoMt7WEkE10EcnfluXFIWONlx7RPwiZ7NF3fNgCUJgXqu0M3hamYyVFPLJracj IoNMSpYp/buU9w7SqininRWd3PokXA1ebGKdzqjPDHZitoCGsJ9T/gTuTDmGTXqxVnSI 51W1vSO3p8YJCl1No9lIbKfkKBfXkkq6Qw5Y2LEreKzR8LLcuMVKGxLBGUpHtZ5APJ12 4lUA== 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; bh=Q5TlwsA784Lxz3jyBYMQbqT6OTzKspMVWlP2LfiCt7c=; b=jUNUiZM/s6DyFF2pqQ5ZD6ZCq5JG3RyFNaTLHYz6UCo7mMR2pfZdMAHPZcx5jkIMff MznoqC5VAndZoFbIbzB2h9rvyQv2wenpohx30yEUvYy3v0GhCqYEOCXi4LFV2aGlr1nj RGp1bxFvGxYPGvlc6Xi8H9LnXoXsLKtUoJV0CzIdHNAaCRUIzKnEK3CRoHJ4NO23velD yqZY0ycZb9jqkw1yRRmCiM5k6795jSRbJmYQuQe1WL3XZWp8ZdoC0g6XoglxhJeP21LP Dami9QNz5Hl3nJsXY5UiHF1fA2V7XgCeexUOHPXOdq1HPKg3g2EY0yY/A+bXI9HRH/ll uJEg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d9-20020a631d09000000b004c5936dd6e1si21024293pgd.201.2023.01.19.11.21.11; Thu, 19 Jan 2023 11:21:23 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231394AbjASTUX (ORCPT + 99 others); Thu, 19 Jan 2023 14:20:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231565AbjASTUD (ORCPT ); Thu, 19 Jan 2023 14:20:03 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3752F9EE07 for ; Thu, 19 Jan 2023 11:19:11 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7B3E41758; Thu, 19 Jan 2023 11:19:23 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 312DD3F67D; Thu, 19 Jan 2023 11:18:41 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 1/8] iommu: Decouple iommu_present() from bus ops Date: Thu, 19 Jan 2023 19:18:19 +0000 Message-Id: <1fb168b22cbbb5c24162d29d2a9aca339cda2c72.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755479889109587454?= X-GMAIL-MSGID: =?utf-8?q?1755479889109587454?= Much as I'd like to remove iommu_present(), the final remaining users are proving stubbornly difficult to clean up, so kick that can down the road and just rework it to preserve the current behaviour without depending on bus ops. Signed-off-by: Robin Murphy --- drivers/iommu/iommu.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index b189ed345057..a77d58e1b976 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1871,9 +1871,24 @@ int bus_iommu_probe(struct bus_type *bus) return ret; } +static int __iommu_present(struct device *dev, void *unused) +{ + return device_iommu_mapped(dev); +} + +/** + * iommu_present() - make platform-specific assumptions about an IOMMU + * @bus: bus to check + * + * Do not use this function. You want device_iommu_mapped() instead. + * + * Return: true if some IOMMU is present for some device on the given bus. In + * general it may not be the only IOMMU, and it may not be for the device you + * are ultimately interested in. + */ bool iommu_present(struct bus_type *bus) { - return bus->iommu_ops != NULL; + return bus_for_each_dev(bus, NULL, NULL, __iommu_present) > 0; } EXPORT_SYMBOL_GPL(iommu_present); From patchwork Thu Jan 19 19:18:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp502859wrn; Thu, 19 Jan 2023 11:21:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXsKTEtzGl1eLXU0iZFa8dxXAz5sxBEe5z7zMDyoSVVdkpIpGLcprP5uY4Wqr7GEQqZ46s7G X-Received: by 2002:a17:902:b091:b0:194:8261:8018 with SMTP id p17-20020a170902b09100b0019482618018mr11618138plr.64.1674156087801; Thu, 19 Jan 2023 11:21:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674156087; cv=none; d=google.com; s=arc-20160816; b=zsvpWkU0QW4gLVzPV62OAtE1oZ/UV9/RBkMVM2FSUZqLhGNKcM+kSjx0hYtiyXbl6S Rpq+qxIFz4SK7PpYcpFiNG3Hc1F5awIuWRk6PBE12Z1DYw1Lnhda0vyjvJZPSf37+Iqi Hrx9Kzxsug6T53zTWRFoLBvaR+cGiQOLKhI3zRg2GjheObU0FsQGd3NtaFu1Ghqlz70f sbuckpC8sZx1wK4MZ/mOlGxBIEAaQxLOOVkOJhV2PoSJEoVZ8J238kwY57aiErYCbmN+ Am9kZZRdAt3eljDwaQxJlh36EGFWfOzZoepBPmoFWT3H9ivOa635YquZajGZ7CBVM8sa pwXQ== 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; bh=l0yKfan7rKQ4JHwy+Ftuy53caT/iVGmciuLT5ner/bk=; b=pisxNIP+hIPRF0pV2paI4zKqygUOugInOT+98b7kgz1T0jQsZXBLu044CBggn5pImV FzOQA1BI21U9SW7J427aPaNVNxzKQDPQZAIDP1Hw40bZd/9RcPEn8uWJUu27UNqQCzES W9jwTmbv/SAlrXDyLEX2QBfHmy19TuCu2Fhdx/v37QA+Tugy2P6zedj/gVjxBb/ZJsz4 7YKIbrQA1TWhYyFJUac7ILFX9EsNZ/YfQEZDsqZa7Bn8QIDGSL4qzwLnO/T6Cp6XBx1W S/7h5/ykSDHY/yN5wvtkbljD0qOEDgSjCJAsL7WT99sKY5dMJ5GaxyguL2yvuiRhbtKw utJg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j4-20020a170902da8400b00180680b8f1bsi16419002plx.264.2023.01.19.11.21.13; Thu, 19 Jan 2023 11:21:27 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231419AbjASTUZ (ORCPT + 99 others); Thu, 19 Jan 2023 14:20:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231577AbjASTUD (ORCPT ); Thu, 19 Jan 2023 14:20:03 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 37E819EE20 for ; Thu, 19 Jan 2023 11:19:12 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9188B1BB2; Thu, 19 Jan 2023 11:19:24 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2D00E3F67D; Thu, 19 Jan 2023 11:18:42 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 2/8] iommu: Validate that devices match domains Date: Thu, 19 Jan 2023 19:18:20 +0000 Message-Id: <3690e5e6b53f6bfecd56f2e0aa77d2915f2e2588.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755479893795675842?= X-GMAIL-MSGID: =?utf-8?q?1755479893795675842?= Before we can allow drivers to coexist, we need to make sure that one driver's domain ops can't misinterpret another driver's dev_iommu_priv data. To that end, add a token to the domain so we can remember how it was allocated - for now this may as well be the device ops. We can trust ourselves for internal default domain attachment, so add the check where it covers both the external attach interfaces. Signed-off-by: Robin Murphy --- drivers/iommu/iommu.c | 13 +++++++++---- include/linux/iommu.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index a77d58e1b976..bc53ffbba4de 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1941,20 +1941,22 @@ EXPORT_SYMBOL_GPL(iommu_set_fault_handler); static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, unsigned type) { + const struct iommu_ops *ops = bus ? bus->iommu_ops : NULL; struct iommu_domain *domain; - if (bus == NULL || bus->iommu_ops == NULL) + if (!ops) return NULL; - domain = bus->iommu_ops->domain_alloc(type); + domain = ops->domain_alloc(type); if (!domain) return NULL; domain->type = type; + domain->owner = ops; /* Assume all sizes by default; the driver may override this later */ - domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; + domain->pgsize_bitmap = ops->pgsize_bitmap; if (!domain->ops) - domain->ops = bus->iommu_ops->default_domain_ops; + domain->ops = ops->default_domain_ops; if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) { iommu_domain_free(domain); @@ -2128,6 +2130,9 @@ static int iommu_group_do_attach_device(struct device *dev, void *data) { struct iommu_domain *domain = data; + if (dev_iommu_ops(dev) != domain->owner) + return -EINVAL; + return __iommu_attach_device(domain, dev); } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index d37bf28faf82..35af9d4e3969 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -95,6 +95,7 @@ struct iommu_domain_geometry { struct iommu_domain { unsigned type; const struct iommu_domain_ops *ops; + const struct iommu_ops *owner; /* Whose domain_alloc we came from */ unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; From patchwork Thu Jan 19 19:18:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45943 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp502933wrn; Thu, 19 Jan 2023 11:21:39 -0800 (PST) X-Google-Smtp-Source: AMrXdXuNgPSW+SaH0vV2DJWylyrI2osaEs6Zxmgi6oGpKppgtY9emK3uGIb8zT7Eu0iXfboAC/I0 X-Received: by 2002:a62:aa0c:0:b0:589:8560:a39d with SMTP id e12-20020a62aa0c000000b005898560a39dmr12089535pff.0.1674156099099; Thu, 19 Jan 2023 11:21:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674156099; cv=none; d=google.com; s=arc-20160816; b=hpDmDy5KtFOHV4onJfeeq3xZxQh9ubKp/CAdImK1VZUtPgHh3WBs+OF8mvnhkf28Tj u5u6JITLWXCXUTPJgLmkUFL05PbZqeISQvYC1wJvvpyGLSYFRaTP1foMVnTd+OQdeF6R rAiL5lsO0hYTjtdC4cVIrJJDnQlQwPFVI9hPvBnFTJqJxKjveLtTNxirOLKDDFPaT+gz lhvi57Wq6t6LGv5RVn5WYqcRB4SrXqOnzNosMAg+0g+B57bbIPFF3lDdvhPvCXrAcqWq hKNSE7j1FQIRgLrk5tPvH05yme0weZhstdw1wV6Y4MhooRiRX7wthZisqhBxrYDOmf4M oI/Q== 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; bh=mM9swOownoqksOhwOCQWJojOsuqqxJ0Mswu4yl9ybkw=; b=ueJ9k1E+YpG6KEKBV2BthsbLsGkqz81/iqpP5vRb7cox1qlDta97r7pqr9HKqQtnYd g3o6Iury22AWGpaOC0TuZLxZhLy5MohLXlMFQ4v+YnbbiFjdYVw/3kC9xvYOFe6S3F8B cKse0BpXdcZbjd175eksGuuO1wlUdQmLp4oliznDzdk25AdBu3Mv9hckFxF3O5QZulsR 9TSalgy+l+cveCxjkODqhpkpuDLrJDT6OUebOqljswTEnZSKhSI8B2Ug3/qY0DMCpwcj iEbV9uYIqVzMpFFHNmvih9ag3KxpnpeVTvNKMoIMM4KqyC1nEZUWKG9PURK6kgCSlhto CFQQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k193-20020a6284ca000000b00557c5641be2si37253108pfd.248.2023.01.19.11.21.26; Thu, 19 Jan 2023 11:21:39 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231453AbjASTUe (ORCPT + 99 others); Thu, 19 Jan 2023 14:20:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230401AbjASTUH (ORCPT ); Thu, 19 Jan 2023 14:20:07 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2A9E69F3B6 for ; Thu, 19 Jan 2023 11:19:16 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AB47E1BCA; Thu, 19 Jan 2023 11:19:25 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 434B13F67D; Thu, 19 Jan 2023 11:18:43 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 3/8] iommu: Factor out a "first device in group" helper Date: Thu, 19 Jan 2023 19:18:21 +0000 Message-Id: <592bff75a7fc4d50d5b2435a09dfff19f1072973.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755479906079093377?= X-GMAIL-MSGID: =?utf-8?q?1755479906079093377?= This pattern for picking the first device out of the group list is repeated a few times now, so it's clearly worth factoring out. Signed-off-by: Robin Murphy --- drivers/iommu/iommu.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index bc53ffbba4de..5b37766a09e2 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1084,6 +1084,11 @@ void iommu_group_remove_device(struct device *dev) } EXPORT_SYMBOL_GPL(iommu_group_remove_device); +static struct device *iommu_group_first_dev(struct iommu_group *group) +{ + return list_first_entry(&group->devices, struct group_device, list)->dev; +} + static int iommu_group_device_count(struct iommu_group *group) { struct group_device *entry; @@ -2835,7 +2840,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, struct device *prev_dev, int type) { struct iommu_domain *prev_dom; - struct group_device *grp_dev; int ret, dev_def_dom; struct device *dev; @@ -2867,8 +2871,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, } /* Since group has only one device */ - grp_dev = list_first_entry(&group->devices, struct group_device, list); - dev = grp_dev->dev; + dev = iommu_group_first_dev(group); if (prev_dev != dev) { dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n"); @@ -2965,7 +2968,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, static ssize_t iommu_group_store_type(struct iommu_group *group, const char *buf, size_t count) { - struct group_device *grp_dev; struct device *dev; int ret, req_type; @@ -3000,8 +3002,7 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, } /* Since group has only one device */ - grp_dev = list_first_entry(&group->devices, struct group_device, list); - dev = grp_dev->dev; + dev = iommu_group_first_dev(group); get_device(dev); /* @@ -3126,21 +3127,18 @@ void iommu_device_unuse_default_domain(struct device *dev) static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) { - struct group_device *dev = - list_first_entry(&group->devices, struct group_device, list); + struct device *dev = iommu_group_first_dev(group); if (group->blocking_domain) return 0; - group->blocking_domain = - __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED); + group->blocking_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_BLOCKED); if (!group->blocking_domain) { /* * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED * create an empty domain instead. */ - group->blocking_domain = __iommu_domain_alloc( - dev->dev->bus, IOMMU_DOMAIN_UNMANAGED); + group->blocking_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_UNMANAGED); if (!group->blocking_domain) return -EINVAL; } From patchwork Thu Jan 19 19:18:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp502858wrn; Thu, 19 Jan 2023 11:21:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXu5pxWRmQSeg1YRJ/DjL0+4w+0qYGLULK7PhO3dpwGL5FYcJdYj/yk2Y7SE9RJZDY98/dn7 X-Received: by 2002:a17:90b:3d85:b0:229:d224:8c59 with SMTP id pq5-20020a17090b3d8500b00229d2248c59mr6092378pjb.22.1674156087787; Thu, 19 Jan 2023 11:21:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674156087; cv=none; d=google.com; s=arc-20160816; b=EOFCazbEUmhum9wphZJxDDpOYIs3RefbkL84B8ZaqkZrfm2/Yb2FPuVqf3Unr9fgRK iCbVQbih7rBq+yl04W07sxpZgm0KsbTmEptFfQQXhVdy6whOzBOdroLDb/brkJOu1mAs rdAZsxXcAkm1QZoHTQCaWYJ6KRlEH+EBPnur+XPsZCwpj53Lku0q04Jw2cHtOMcnBKXY O85MGKUcZj9jUj3qh+duMoa3J+MWxgZAQxaFySCLj/X7Vks8WtV0dqxpeVx2bXpe8sQr TZdAonX8ONdyNv2fcYawxfOy1gz2dhHY5IYgICcKmShwSV5sMb9QcCvIRP11KYhY18nf YMWg== 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; bh=f2Rka3a9w0bUQkbNabNWnfRsvn5qAD578RuU165vCiY=; b=IhM9JXsI1KaZV2KI81mtzajVs2GkcDQJL7FN6/++AOJbpinftQQDZTdGwXOTVwgLRQ NE8j+Tp3zz/aItqFXIdmesqB7cXgqJEJKGy0kHCGyn15S42Q7O6kONEWBXq1MxhCWPOC EcMCfZaiSouAEx4blKU+3H1ChcuKkGJ0JGh9a1wQ8BRmFcO164T/mp9ZVhFWtZL+sP3B tzJpSBBMLkz1qa26OnhAn/p25msSqfwurCkgr+d78NI1OlG+FJb3tKaUauBH3OK0BWx5 QGCaJbhIgd9rtUgJOgissoovMyJl6zYLi5h2gusHyCQ1aCphmNetUG89Y9Tl8Rm15JZ/ Mv0g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k37-20020a635625000000b004d0682f947fsi6441395pgb.166.2023.01.19.11.21.15; Thu, 19 Jan 2023 11:21:27 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231435AbjASTUa (ORCPT + 99 others); Thu, 19 Jan 2023 14:20:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231597AbjASTUF (ORCPT ); Thu, 19 Jan 2023 14:20:05 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 376A89EE12 for ; Thu, 19 Jan 2023 11:19:12 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BF9001BCB; Thu, 19 Jan 2023 11:19:26 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 5AAC73F67D; Thu, 19 Jan 2023 11:18:44 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 4/8] iommu: Switch __iommu_domain_alloc() to device ops Date: Thu, 19 Jan 2023 19:18:22 +0000 Message-Id: <25ea8128b9228f9893507ad5a764ff25db5961a0.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755479893563942235?= X-GMAIL-MSGID: =?utf-8?q?1755479893563942235?= In all the places we allocate default domains, we have (or can easily get hold of) a device from which to resolve the right IOMMU ops; only the public iommu_domain_alloc() interface actually depends on bus ops. Reworking the public API is a big enough mission in its own right, but in the meantime we can still decouple it from bus ops internally to move forward. Signed-off-by: Robin Murphy --- drivers/iommu/iommu.c | 57 ++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 5b37766a09e2..1a31d94adff5 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -88,7 +88,7 @@ static int iommu_bus_notifier(struct notifier_block *nb, unsigned long action, void *data); static int iommu_alloc_default_domain(struct iommu_group *group, struct device *dev); -static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, unsigned type); static int __iommu_attach_device(struct iommu_domain *domain, struct device *dev); @@ -1620,15 +1620,15 @@ static int iommu_get_def_domain_type(struct device *dev) return 0; } -static int iommu_group_alloc_default_domain(struct bus_type *bus, - struct iommu_group *group, +static int iommu_group_alloc_default_domain(struct iommu_group *group, + struct device *dev, unsigned int type) { struct iommu_domain *dom; - dom = __iommu_domain_alloc(bus, type); + dom = __iommu_domain_alloc(dev, type); if (!dom && type != IOMMU_DOMAIN_DMA) { - dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA); + dom = __iommu_domain_alloc(dev, IOMMU_DOMAIN_DMA); if (dom) pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA", type, group->name); @@ -1653,7 +1653,7 @@ static int iommu_alloc_default_domain(struct iommu_group *group, type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type; - return iommu_group_alloc_default_domain(dev->bus, group, type); + return iommu_group_alloc_default_domain(group, dev, type); } /** @@ -1766,8 +1766,7 @@ static int probe_get_default_domain_type(struct device *dev, void *data) return 0; } -static void probe_alloc_default_domain(struct bus_type *bus, - struct iommu_group *group) +static void probe_alloc_default_domain(struct iommu_group *group) { struct __group_domain_type gtype; @@ -1777,10 +1776,12 @@ static void probe_alloc_default_domain(struct bus_type *bus, __iommu_group_for_each_dev(group, >ype, probe_get_default_domain_type); - if (!gtype.type) + if (!gtype.type) { gtype.type = iommu_def_domain_type; + gtype.dev = iommu_group_first_dev(group); + } - iommu_group_alloc_default_domain(bus, group, gtype.type); + iommu_group_alloc_default_domain(group, gtype.dev, gtype.type); } @@ -1854,7 +1855,7 @@ int bus_iommu_probe(struct bus_type *bus) list_del_init(&group->entry); /* Try to allocate default domain */ - probe_alloc_default_domain(bus, group); + probe_alloc_default_domain(group); if (!group->default_domain) { mutex_unlock(&group->mutex); @@ -1943,15 +1944,12 @@ void iommu_set_fault_handler(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_fault_handler); -static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, unsigned type) { - const struct iommu_ops *ops = bus ? bus->iommu_ops : NULL; + const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; - if (!ops) - return NULL; - domain = ops->domain_alloc(type); if (!domain) return NULL; @@ -1970,9 +1968,28 @@ static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, return domain; } +static int __iommu_domain_alloc_dev(struct device *dev, void *data) +{ + struct device **alloc_dev = data; + + if (!device_iommu_mapped(dev)) + return 0; + + WARN_ONCE(*alloc_dev && dev_iommu_ops(dev) != dev_iommu_ops(*alloc_dev), + "Multiple IOMMU drivers present, which the public IOMMU API can't fully support yet. This may not work as expected, sorry!\n"); + + *alloc_dev = dev; + return 0; +} + struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) { - return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); + struct device *dev = NULL; + + if (bus_for_each_dev(bus, NULL, &dev, __iommu_domain_alloc_dev)) + return NULL; + + return __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANAGED); } EXPORT_SYMBOL_GPL(iommu_domain_alloc); @@ -2918,7 +2935,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, } /* Sets group->default_domain to the newly allocated domain */ - ret = iommu_group_alloc_default_domain(dev->bus, group, type); + ret = iommu_group_alloc_default_domain(group, dev, type); if (ret) goto out; @@ -3132,13 +3149,13 @@ static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) if (group->blocking_domain) return 0; - group->blocking_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_BLOCKED); + group->blocking_domain = __iommu_domain_alloc(dev, IOMMU_DOMAIN_BLOCKED); if (!group->blocking_domain) { /* * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED * create an empty domain instead. */ - group->blocking_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_UNMANAGED); + group->blocking_domain = __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANAGED); if (!group->blocking_domain) return -EINVAL; } From patchwork Thu Jan 19 19:18:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45944 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp503081wrn; Thu, 19 Jan 2023 11:21:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXvfFLQDNuCgM6QxtZ/+Hr3R/ULbhsKe6wkUPsT4v4F5XC51L7Ygu4g/TFJUCu2ppr51fD4T X-Received: by 2002:a62:19cb:0:b0:58d:b2eb:86ef with SMTP id 194-20020a6219cb000000b0058db2eb86efmr11454321pfz.9.1674156119669; Thu, 19 Jan 2023 11:21:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674156119; cv=none; d=google.com; s=arc-20160816; b=dkaX6zKoETBQ0l+RqnsdSOKXBLvG+kS1s+atSAY9QMgAGMr6nAn8GREMyxQvypr7n4 KbIbBAgnLhVrIcoIwrM6+tb3KOQe8RVSoa2ENucilqPiD5f58L/l7+aeWGodUBRNVfhE VExuMC4wy13N21Ctji2q+vo+NaC3HP45L9p486wT0Ifz5J/DpW4zROwoaegejB1YVnju huVx6e40Tgiua3atrMekaHNwFG5Npj1yXvFtBp9/fz16n3KmWHZQdHB0iZM+qjkAr3hB dXet2Axkd8Uc7pJaeblvmc+HlECqI+GKyFyc8XMCd4LdBMmmcnAU/WQPync+R3z4lYP0 4u/Q== 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; bh=QIGRBOEtobpEgcEjBeJGkmb4fV2/3LulrINwdgJB4bw=; b=HVFbXTVuB813qLsjnbgkw4mvqJMp09HOS2+aIjK6Xz+8Xkuio9G3zfZ/SUrkLAiBnx OYC3cX3XHItwl9sDjSBcqNIXSlQYdgPflpcCt7G4gM6el6J3eeTf1PN8/Hw2734D/6Rx PEGenkrWN8BIILpETjj3jL7jJswwLWv9dc7U82y0CcvwDzny1Ft0dDKp6pzmCoHIg7lM EsLg7m87+4ubiG+bErQZYxVGob0mM3X4cgOAZqDlEZl97o8XCRWI+f/H/gWAppfGtmkw m7OSdWzcLA4t/lzOxrL2fy5F8YdRETFD8VK0aNwi0kENTj2FXn+Aqk4VeADGIJ2rZUdz 1QBw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u70-20020a627949000000b00588c119c016si32600151pfc.135.2023.01.19.11.21.47; Thu, 19 Jan 2023 11:21:59 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231436AbjASTU5 (ORCPT + 99 others); Thu, 19 Jan 2023 14:20:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231447AbjASTUd (ORCPT ); Thu, 19 Jan 2023 14:20:33 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CEC6446726 for ; Thu, 19 Jan 2023 11:19:44 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E02CC106F; Thu, 19 Jan 2023 11:19:27 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 71A913F67D; Thu, 19 Jan 2023 11:18:45 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 5/8] iommu/arm-smmu: Don't register fwnode for legacy binding Date: Thu, 19 Jan 2023 19:18:23 +0000 Message-Id: <01c68ef4d924d3671016690fa946eab306a1de07.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755479927709875863?= X-GMAIL-MSGID: =?utf-8?q?1755479927709875863?= When using the legacy binding we bypass the of_xlate mechanism, so avoid registering the instance fwnodes which act as keys for that. This will help __iommu_probe_device() to retrieve the registered ops the same way as for x86 etc. when no fwspec has previously been set up by of_xlate. Signed-off-by: Robin Murphy --- drivers/iommu/arm/arm-smmu/arm-smmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 719fbca1fe52..607f06af01b6 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -2156,7 +2156,8 @@ static int arm_smmu_device_probe(struct platform_device *pdev) return err; } - err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); + err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, + using_legacy_binding ? NULL : dev); if (err) { dev_err(dev, "Failed to register iommu\n"); iommu_device_sysfs_remove(&smmu->iommu); From patchwork Thu Jan 19 19:18:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45945 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp503128wrn; Thu, 19 Jan 2023 11:22:06 -0800 (PST) X-Google-Smtp-Source: AMrXdXvjyy62iVdsYLoRV/r801XZUUHDJ9PYaz9BGkL+Wzak4Pgv9gu2vBryEpCS/lyT1VmRLKPF X-Received: by 2002:a17:902:f805:b0:194:9c13:2dde with SMTP id ix5-20020a170902f80500b001949c132ddemr12971206plb.23.1674156126278; Thu, 19 Jan 2023 11:22:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674156126; cv=none; d=google.com; s=arc-20160816; b=dUch+fRARa6OeKDzMWi9frDndTtSxjakp6KO1ZxFq6yyJARYQar/HVAMPtDIDVkJlp uf9dtiT82oxnm0xRxwqE3hjpfLXsR2eKe1/J5qpm7f/Ac8bbihtScnN82brirDaRgDrR BHGIxWhzotrPA08rCfD1HD6GasoM1viTU3IJdG7MLsIffr5ZrNG1RtmMlKjYO0IcHTIO 4h05cqhjuR5SUKwxvUdyD75O1jOFFx40D6kG+z3ckhEhcgB9hQ9Sy/PlgcquP3T00Rd3 tDoL4XVKRr1tYiEm5wpVVpxdV1S7fQSlq9sVMYPhmVucweyDiJYFAM9xyot3Dj/8RqOQ FO2g== 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; bh=Qclek6dyjiW0EHJAx1WRC7pXC2ZMc2CectKii0tBC40=; b=sA5Brv2+PUzMhb4BjluNJ1q3RMSleHs0cp/EICdF4u43/0DGhrsVUgZZLx2JkEJ/G2 KSf6sPYBB4hYF9Z6BFfUOOX2qXULkE1dsOgO3LWue77o1k28j5u2Gg6HYp8iFurkdpyc ycCubfUzD12HitA3t4uPMZ+5NCXp6QleSqhQt0nKeXwL4b+5sFLqkyj1tor2xFiHNfv8 wieCZdUYPPCMX2YNGRPANBvFmQayrdz/CEM6Laj9mi3B0G2pcB8Zyfw30iJRKlt1CMph 7QE8cFyE79OrsbDodAGtUv8a3zEMYv7qIswIJmPRp6ih4PsjaIb/DvMp9JUl5TIyZp3z /W8g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o17-20020a170902779100b00189005c48aesi36791314pll.108.2023.01.19.11.21.54; Thu, 19 Jan 2023 11:22:06 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230126AbjASTVA (ORCPT + 99 others); Thu, 19 Jan 2023 14:21:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231450AbjASTUd (ORCPT ); Thu, 19 Jan 2023 14:20:33 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0584E5B581 for ; Thu, 19 Jan 2023 11:19:44 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D5E561BF3; Thu, 19 Jan 2023 11:19:30 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 39BD23F67D; Thu, 19 Jan 2023 11:18:48 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, "Rafael J . Wysocki" , Greg Kroah-Hartman Subject: [PATCH 6/8] iommu: Retire bus ops Date: Thu, 19 Jan 2023 19:18:24 +0000 Message-Id: X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755479934690819405?= X-GMAIL-MSGID: =?utf-8?q?1755479934690819405?= With the rest of the API internals converted, it's time to finally tackle probe_device and how we bootstrap the per-device ops association to begin with. This ends up being disappointingly straightforward, since fwspec users are already doing it in order to find their of_xlate callback, and it works out that we can easily do the equivalent for other drivers too. Then shuffle the remaining awareness of iommu_ops into the couple of core headers that still need it, and breathe a sigh of relief... Ding dong the bus ops are gone! CC: Rafael J. Wysocki CC: Greg Kroah-Hartman CC: Christoph Hellwig Signed-off-by: Robin Murphy Acked-by: Greg Kroah-Hartman --- drivers/iommu/iommu.c | 27 ++++++++++++++++----------- include/acpi/acpi_bus.h | 2 ++ include/linux/device.h | 1 - include/linux/device/bus.h | 5 ----- include/linux/dma-map-ops.h | 1 + 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 1a31d94adff5..8997b8f2e79f 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -218,13 +218,6 @@ int iommu_device_register(struct iommu_device *iommu, /* We need to be able to take module references appropriately */ if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) return -EINVAL; - /* - * Temporarily enforce global restriction to a single driver. This was - * already the de-facto behaviour, since any possible combination of - * existing drivers would compete for at least the PCI or platform bus. - */ - if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops) - return -EBUSY; iommu->ops = ops; if (hwdev) @@ -234,10 +227,8 @@ int iommu_device_register(struct iommu_device *iommu, list_add_tail(&iommu->list, &iommu_device_list); spin_unlock(&iommu_device_lock); - for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) { - iommu_buses[i]->iommu_ops = ops; + for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) err = bus_iommu_probe(iommu_buses[i]); - } if (err) iommu_device_unregister(iommu); return err; @@ -303,12 +294,26 @@ static u32 dev_iommu_get_max_pasids(struct device *dev) static int __iommu_probe_device(struct device *dev, struct list_head *group_list) { - const struct iommu_ops *ops = dev->bus->iommu_ops; + const struct iommu_ops *ops; struct iommu_device *iommu_dev; + struct iommu_fwspec *fwspec; struct iommu_group *group; static DEFINE_MUTEX(iommu_probe_device_lock); int ret; + /* + * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU + * instances with non-NULL fwnodes, and client devices should have been + * identified with a fwspec by this point. For Intel/AMD/s390/PAMU we + * can assume a single active driver with global ops, and so grab those + * from any registered instance, cheekily co-opting the same mechanism. + */ + fwspec = dev_iommu_fwspec_get(dev); + if (fwspec && fwspec->ops) + ops = fwspec->ops; + else + ops = iommu_ops_from_fwnode(NULL); + if (!ops) return -ENODEV; /* diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index cd3b75e08ec3..067dde9291c9 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -614,6 +614,8 @@ struct acpi_pci_root { /* helper */ +struct iommu_ops; + bool acpi_dma_supported(const struct acpi_device *adev); enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev); int acpi_iommu_fwspec_init(struct device *dev, u32 id, diff --git a/include/linux/device.h b/include/linux/device.h index 44e3acae7b36..f7a7ecafedd3 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -41,7 +41,6 @@ struct class; struct subsys_private; struct device_node; struct fwnode_handle; -struct iommu_ops; struct iommu_group; struct dev_pin_info; struct dev_iommu; diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index d8b29ccd07e5..4ece3470112f 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -63,9 +63,6 @@ struct fwnode_handle; * this bus. * @pm: Power management operations of this bus, callback the specific * device driver's pm-ops. - * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU - * driver implementations to a bus and allow the driver to do - * bus-specific setup * @p: The private data of the driver core, only the driver core can * touch this. * @lock_key: Lock class key for use by the lock validator @@ -109,8 +106,6 @@ struct bus_type { const struct dev_pm_ops *pm; - const struct iommu_ops *iommu_ops; - struct subsys_private *p; struct lock_class_key lock_key; diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index d678afeb8a13..e8ebf0bf611b 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -10,6 +10,7 @@ #include struct cma; +struct iommu_ops; /* * Values for struct dma_map_ops.flags: From patchwork Thu Jan 19 19:18:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45947 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp509496wrn; Thu, 19 Jan 2023 11:37:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXufC+jbFxGXmo8eKswypgvCiVIzawSZcza/iGrKfiEd2W50RYMWI6tJYsJlm2LyJXzrqlan X-Received: by 2002:a17:906:a002:b0:858:cccd:7c6a with SMTP id p2-20020a170906a00200b00858cccd7c6amr12574514ejy.68.1674157040195; Thu, 19 Jan 2023 11:37:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674157040; cv=none; d=google.com; s=arc-20160816; b=D6Jf0NvauqX/8/Roh8Rrd4q2+XHBCblRczeqZsWxFM2BIpV8KFPDAKzDgnfZWjBn06 JY/Pe6PT/ju2s1jBWk+VslltB4sm/X2qyNwZd4TWJUyS259rd59B6C0y7O6IUBtA3iRM zFpnGbMVfn8zpRVSOhyWb8B2bL6ihxElrtZobPQw79+3nXFbV9uRyr1OGAPPsbB6YETw 4+ycv8roJK9CTMyTZWc27aqTPr1s/5lXWptSCu2CiAo9Vk/Lb7a/+QtT6aFvoZ6oW2sm B6gLgPv+EXzgtkAZxActEAgy2j2kPoI8qdF7rb7C/mCNzkKzVWTomweeRiOLCYddJsm9 RvhQ== 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; bh=9cMSqkmv36znYZcjaos08VvneCiowRBcYs64TVClU54=; b=sXgSra2I7MLF2JU9ytNisKj/F2vDkd7AfRHrjQTnn1UIV36O5dlWkOE8tpHjCtvBAY u5Kofcfy9mBy1OHDvIT9WpCBZzEFv5/rZ91sjN2qJv0CG1X9Z/VEiU+ppiXAmpCp0DDc FJCty2udw3w2n+Q8fBiE1g0SCTZ0QRBO/6BqjtlfXWWOJ6HDZNtsDY5Rf8kR+rL3j6jJ /D9L1NRDPqKrvkJCd4p2s2P8TEaDo669l1gdqr14dBdVVDARhnmkJkDaFZqV0ULwwcgc ZiVmxbGPTjaPWoAP7M1P2gOzzx2c6niDjXjrc1opBg83M+FWj/x5IGBxIGFj45hVvGGO uw/g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt33-20020a1709072da100b0087766403ecesi5360128ejc.960.2023.01.19.11.36.56; Thu, 19 Jan 2023 11:37:20 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231474AbjASTVM (ORCPT + 99 others); Thu, 19 Jan 2023 14:21:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231146AbjASTUo (ORCPT ); Thu, 19 Jan 2023 14:20:44 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C943C9AAA4 for ; Thu, 19 Jan 2023 11:19:46 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EC1101BF7; Thu, 19 Jan 2023 11:19:31 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8795C3F67D; Thu, 19 Jan 2023 11:18:49 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 7/8] iommu: Clean up open-coded ownership checks Date: Thu, 19 Jan 2023 19:18:25 +0000 Message-Id: <47109befd7a32d03bffe54192bf02f8c8a223858.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755480892965795602?= X-GMAIL-MSGID: =?utf-8?q?1755480892965795602?= Some drivers already implement their own defence against the possibility of being given someone else's device. Since this is now taken care of by the core code (and via a slightly different path from the original fwspec-based idea), let's clean them up. Signed-off-by: Robin Murphy --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 --- drivers/iommu/arm/arm-smmu/arm-smmu.c | 9 +-------- drivers/iommu/arm/arm-smmu/qcom_iommu.c | 16 +++------------- drivers/iommu/mtk_iommu.c | 7 +------ drivers/iommu/mtk_iommu_v1.c | 3 --- drivers/iommu/sprd-iommu.c | 8 +------- drivers/iommu/virtio-iommu.c | 3 --- 7 files changed, 6 insertions(+), 43 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index ab160198edd6..cb05d9771192 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2642,9 +2642,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) struct arm_smmu_master *master; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - if (!fwspec || fwspec->ops != &arm_smmu_ops) - return ERR_PTR(-ENODEV); - if (WARN_ON_ONCE(dev_iommu_priv_get(dev))) return ERR_PTR(-EBUSY); diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 607f06af01b6..235550db0d59 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -1118,11 +1118,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_device *smmu; int ret; - if (!fwspec || fwspec->ops != &arm_smmu_ops) { - dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); - return -ENXIO; - } - /* * FIXME: The arch/arm DMA API code tries to attach devices to its own * domains between of_xlate() and probe_device() - we have no way to cope @@ -1352,10 +1347,8 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) fwspec = dev_iommu_fwspec_get(dev); if (ret) goto out_free; - } else if (fwspec && fwspec->ops == &arm_smmu_ops) { - smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); } else { - return ERR_PTR(-ENODEV); + smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); } ret = -EINVAL; diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/arm-smmu/qcom_iommu.c index 270c3d9128ba..b2d3d309be1e 100644 --- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c +++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c @@ -79,16 +79,6 @@ static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom) static const struct iommu_ops qcom_iommu_ops; -static struct qcom_iommu_dev * to_iommu(struct device *dev) -{ - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - - if (!fwspec || fwspec->ops != &qcom_iommu_ops) - return NULL; - - return dev_iommu_priv_get(dev); -} - static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid) { struct qcom_iommu_dev *qcom_iommu = d->iommu; @@ -361,7 +351,7 @@ static void qcom_iommu_domain_free(struct iommu_domain *domain) static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) { - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); int ret; @@ -391,7 +381,7 @@ static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *de { struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); unsigned i; if (WARN_ON(!qcom_domain->iommu)) @@ -508,7 +498,7 @@ static bool qcom_iommu_capable(struct device *dev, enum iommu_cap cap) static struct iommu_device *qcom_iommu_probe_device(struct device *dev) { - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); struct device_link *link; if (!qcom_iommu) diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index 2badd6acfb23..005136a4cc36 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -784,16 +784,11 @@ static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain, static struct iommu_device *mtk_iommu_probe_device(struct device *dev) { struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - struct mtk_iommu_data *data; + struct mtk_iommu_data *data = dev_iommu_priv_get(dev); struct device_link *link; struct device *larbdev; unsigned int larbid, larbidx, i; - if (!fwspec || fwspec->ops != &mtk_iommu_ops) - return ERR_PTR(-ENODEV); /* Not a iommu client device */ - - data = dev_iommu_priv_get(dev); - if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) return &data->iommu; diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c index 69682ee068d2..dff8ea0af884 100644 --- a/drivers/iommu/mtk_iommu_v1.c +++ b/drivers/iommu/mtk_iommu_v1.c @@ -478,9 +478,6 @@ static struct iommu_device *mtk_iommu_v1_probe_device(struct device *dev) idx++; } - if (!fwspec || fwspec->ops != &mtk_iommu_v1_ops) - return ERR_PTR(-ENODEV); /* Not a iommu client device */ - data = dev_iommu_priv_get(dev); /* Link the consumer device with the smi-larb device(supplier) */ diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index 219bfa11f7f4..4cebccb6fc8b 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -373,13 +373,7 @@ static phys_addr_t sprd_iommu_iova_to_phys(struct iommu_domain *domain, static struct iommu_device *sprd_iommu_probe_device(struct device *dev) { - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - struct sprd_iommu_device *sdev; - - if (!fwspec || fwspec->ops != &sprd_iommu_ops) - return ERR_PTR(-ENODEV); - - sdev = dev_iommu_priv_get(dev); + struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev); return &sdev->iommu; } diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 5b8fe9bfa9a5..59f1abd6ee53 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -945,9 +945,6 @@ static struct iommu_device *viommu_probe_device(struct device *dev) struct viommu_dev *viommu = NULL; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - if (!fwspec || fwspec->ops != &viommu_ops) - return ERR_PTR(-ENODEV); - viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode); if (!viommu) return ERR_PTR(-ENODEV); From patchwork Thu Jan 19 19:18:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 45948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp510747wrn; Thu, 19 Jan 2023 11:40:33 -0800 (PST) X-Google-Smtp-Source: AMrXdXsC/R8Dp+BwdcahKYq5WMokPfPN5r7b6SqSqxw5UN/788J23h/saocBdZKyXLEwIQfzqRGP X-Received: by 2002:a62:5bc1:0:b0:58d:ac19:8950 with SMTP id p184-20020a625bc1000000b0058dac198950mr17207482pfb.33.1674157233153; Thu, 19 Jan 2023 11:40:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674157233; cv=none; d=google.com; s=arc-20160816; b=oYO+Qvm+I59FHrWOfjKvYFX3i5zhBINRhznqj0k/Y3o7F2yAP8H30wwhM2TBqyhsdM 21Ox3UoNQFpFWzSY10LaLhrbZvcDr2M8Ds0WQx/kSlIpWUq5VH6s0l7DhmWSSQoUdVmq +c3uon6qNhdmhYNbxTgjG/EVTGoD6Z0YfRdd07qFu2NVrKOAaFK860l+hOaH+wFtEPtd jiy+pUFtSrWkTyAwyeSSkg1m7kur3NYZn6IsReKLLXRJ4OA8jBoz8FCZerSb08y3mxsJ w6Cq3kUV3fyHeSbrTWGgBMwrQUwtbZn2Dv7IquoEgPT91iut9U+mrWJB5BXZWsxs4bnv pSxw== 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; bh=1EWR1SA6vG32MJYqBy8P1pG9eKeyYSuw23q0E+X9BgM=; b=yjZX83XaYyGiTef05uq87L5fp4r78+1FOXYBY85ZC8ReaMFHVJwpXKg5Pjf5k/tf3L v2rmsuCwiSJ6FzqeoBky5Ig+8g8E/ngFPCsfx+kNhMfnouaGtyDNvTSkU9ldD0l7CZRl aaRa+UBhhJtYIIFQ502zz2HBBW5U7nelPqCVfHJ4U1YtY/8K0sglWa7vRDEmck7ELKAx +RIJ2emrcwCT3tJ383wxurXOsBdrVfgB/pSIwU9MbLKs9brWjAeTnJiYXb+/clLjCszH 2/ycvL7AYa7iRUKKeIuV3+eG+9z4DSspc7PkwTCbMR3cCOUNcf7jYUDt9w+DDW4D0UtA OSuQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f7-20020a056a0022c700b0058de1c3118csi7451072pfj.325.2023.01.19.11.40.21; Thu, 19 Jan 2023 11:40:33 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231294AbjASTVO (ORCPT + 99 others); Thu, 19 Jan 2023 14:21:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231154AbjASTUo (ORCPT ); Thu, 19 Jan 2023 14:20:44 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BE63F966F7 for ; Thu, 19 Jan 2023 11:19:46 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0E3611C00; Thu, 19 Jan 2023 11:19:33 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9E1103F67D; Thu, 19 Jan 2023 11:18:50 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: hch@lst.de, jgg@nvidia.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 8/8] iommu: Pass device through ops->domain_alloc Date: Thu, 19 Jan 2023 19:18:26 +0000 Message-Id: <893b2ef069fad5276d015a7606c01a0da315f9da.1673978700.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755481094456871921?= X-GMAIL-MSGID: =?utf-8?q?1755481094456871921?= With __iommu_domain_alloc() now aware of a device, we can propagate that through to the drivers so that they can also pick up their instance data without having to wait until they see the device attach. Signed-off-by: Robin Murphy --- drivers/iommu/amd/iommu.c | 3 ++- drivers/iommu/apple-dart.c | 3 ++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 ++- drivers/iommu/arm/arm-smmu/arm-smmu.c | 3 ++- drivers/iommu/arm/arm-smmu/qcom_iommu.c | 3 ++- drivers/iommu/exynos-iommu.c | 3 ++- drivers/iommu/fsl_pamu_domain.c | 3 ++- drivers/iommu/intel/iommu.c | 3 ++- drivers/iommu/iommu.c | 4 ++-- drivers/iommu/ipmmu-vmsa.c | 3 ++- drivers/iommu/msm_iommu.c | 3 ++- drivers/iommu/mtk_iommu.c | 3 ++- drivers/iommu/mtk_iommu_v1.c | 3 ++- drivers/iommu/omap-iommu.c | 3 ++- drivers/iommu/rockchip-iommu.c | 3 ++- drivers/iommu/s390-iommu.c | 3 ++- drivers/iommu/sprd-iommu.c | 3 ++- drivers/iommu/sun50i-iommu.c | 3 ++- drivers/iommu/tegra-gart.c | 3 ++- drivers/iommu/tegra-smmu.c | 3 ++- drivers/iommu/virtio-iommu.c | 3 ++- include/linux/iommu.h | 3 ++- 22 files changed, 44 insertions(+), 23 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index cbeaab55c0db..f5bc61a4c3d4 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2084,7 +2084,8 @@ static struct protection_domain *protection_domain_alloc(unsigned int type) return NULL; } -static struct iommu_domain *amd_iommu_domain_alloc(unsigned type) +static struct iommu_domain *amd_iommu_domain_alloc(struct device *dev, + unsigned type) { struct protection_domain *domain; diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 4f4a323be0d0..95f3aa323a99 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -576,7 +576,8 @@ static void apple_dart_release_device(struct device *dev) kfree(cfg); } -static struct iommu_domain *apple_dart_domain_alloc(unsigned int type) +static struct iommu_domain *apple_dart_domain_alloc(struct device *dev, + unsigned int type) { struct apple_dart_domain *dart_domain; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index cb05d9771192..453e9dbf4920 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2005,7 +2005,8 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) } } -static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) +static struct iommu_domain *arm_smmu_domain_alloc(struct device *dev, + unsigned type) { struct arm_smmu_domain *smmu_domain; diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 235550db0d59..86c9c6df18ae 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -851,7 +851,8 @@ static void arm_smmu_destroy_domain_context(struct iommu_domain *domain) arm_smmu_rpm_put(smmu); } -static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) +static struct iommu_domain *arm_smmu_domain_alloc(struct device *dev, + unsigned type) { struct arm_smmu_domain *smmu_domain; diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/arm-smmu/qcom_iommu.c index b2d3d309be1e..7b9daacf1697 100644 --- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c +++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c @@ -309,7 +309,8 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, return ret; } -static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type) +static struct iommu_domain *qcom_iommu_domain_alloc(struct device *dev, + unsigned type) { struct qcom_iommu_domain *qcom_domain; diff --git a/drivers/iommu/exynos-iommu.c b/drivers/iommu/exynos-iommu.c index b0cde2211987..54f9d10a9fb1 100644 --- a/drivers/iommu/exynos-iommu.c +++ b/drivers/iommu/exynos-iommu.c @@ -816,7 +816,8 @@ static inline void exynos_iommu_set_pte(sysmmu_pte_t *ent, sysmmu_pte_t val) DMA_TO_DEVICE); } -static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type) +static struct iommu_domain *exynos_iommu_domain_alloc(struct device *dev, + unsigned type) { struct exynos_iommu_domain *domain; dma_addr_t handle; diff --git a/drivers/iommu/fsl_pamu_domain.c b/drivers/iommu/fsl_pamu_domain.c index 4408ac3c49b6..69578e574af0 100644 --- a/drivers/iommu/fsl_pamu_domain.c +++ b/drivers/iommu/fsl_pamu_domain.c @@ -192,7 +192,8 @@ static void fsl_pamu_domain_free(struct iommu_domain *domain) kmem_cache_free(fsl_pamu_domain_cache, dma_domain); } -static struct iommu_domain *fsl_pamu_domain_alloc(unsigned type) +static struct iommu_domain *fsl_pamu_domain_alloc(struct device *dev, + unsigned type) { struct fsl_dma_domain *dma_domain; diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 59df7e42fd53..590594a0d6e1 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4157,7 +4157,8 @@ static struct iommu_domain blocking_domain = { } }; -static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) +static struct iommu_domain *intel_iommu_domain_alloc(struct device *dev, + unsigned type) { struct dmar_domain *dmar_domain; struct iommu_domain *domain; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 8997b8f2e79f..521f6f780294 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1955,7 +1955,7 @@ static struct iommu_domain *__iommu_domain_alloc(struct device *dev, const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; - domain = ops->domain_alloc(type); + domain = ops->domain_alloc(dev, type); if (!domain) return NULL; @@ -3456,7 +3456,7 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; - domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); + domain = ops->domain_alloc(dev, IOMMU_DOMAIN_SVA); if (!domain) return NULL; diff --git a/drivers/iommu/ipmmu-vmsa.c b/drivers/iommu/ipmmu-vmsa.c index 76e51d8e3732..7bb6c2fd3214 100644 --- a/drivers/iommu/ipmmu-vmsa.c +++ b/drivers/iommu/ipmmu-vmsa.c @@ -567,7 +567,8 @@ static irqreturn_t ipmmu_irq(int irq, void *dev) * IOMMU Operations */ -static struct iommu_domain *ipmmu_domain_alloc(unsigned type) +static struct iommu_domain *ipmmu_domain_alloc(struct device *dev, + unsigned type) { struct ipmmu_vmsa_domain *domain; diff --git a/drivers/iommu/msm_iommu.c b/drivers/iommu/msm_iommu.c index c60624910872..1d9704e7c75f 100644 --- a/drivers/iommu/msm_iommu.c +++ b/drivers/iommu/msm_iommu.c @@ -302,7 +302,8 @@ static void __program_context(void __iomem *base, int ctx, SET_M(base, ctx, 1); } -static struct iommu_domain *msm_iommu_domain_alloc(unsigned type) +static struct iommu_domain *msm_iommu_domain_alloc(struct device *dev, + unsigned type) { struct msm_priv *priv; diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index 005136a4cc36..711716c8dcdf 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -632,7 +632,8 @@ static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom, return 0; } -static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type) +static struct iommu_domain *mtk_iommu_domain_alloc(struct device *dev, + unsigned type) { struct mtk_iommu_domain *dom; diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c index dff8ea0af884..3d1e0635f9b3 100644 --- a/drivers/iommu/mtk_iommu_v1.c +++ b/drivers/iommu/mtk_iommu_v1.c @@ -270,7 +270,8 @@ static int mtk_iommu_v1_domain_finalise(struct mtk_iommu_v1_data *data) return 0; } -static struct iommu_domain *mtk_iommu_v1_domain_alloc(unsigned type) +static struct iommu_domain *mtk_iommu_v1_domain_alloc(struct device *dev, + unsigned type) { struct mtk_iommu_v1_domain *dom; diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c index 2fd7702c6709..19a22ba1860d 100644 --- a/drivers/iommu/omap-iommu.c +++ b/drivers/iommu/omap-iommu.c @@ -1566,7 +1566,8 @@ static void omap_iommu_detach_dev(struct iommu_domain *domain, spin_unlock(&omap_domain->lock); } -static struct iommu_domain *omap_iommu_domain_alloc(unsigned type) +static struct iommu_domain *omap_iommu_domain_alloc(struct device *dev, + unsigned type) { struct omap_iommu_domain *omap_domain; diff --git a/drivers/iommu/rockchip-iommu.c b/drivers/iommu/rockchip-iommu.c index a68eadd64f38..50b17a096ea5 100644 --- a/drivers/iommu/rockchip-iommu.c +++ b/drivers/iommu/rockchip-iommu.c @@ -1057,7 +1057,8 @@ static int rk_iommu_attach_device(struct iommu_domain *domain, return ret; } -static struct iommu_domain *rk_iommu_domain_alloc(unsigned type) +static struct iommu_domain *rk_iommu_domain_alloc(struct device *dev, + unsigned type) { struct rk_iommu_domain *rk_domain; diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index ed33c6cce083..a56e4e282469 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -41,7 +41,8 @@ static bool s390_iommu_capable(struct device *dev, enum iommu_cap cap) } } -static struct iommu_domain *s390_domain_alloc(unsigned domain_type) +static struct iommu_domain *s390_domain_alloc(struct device *dev, + unsigned domain_type) { struct s390_domain *s390_domain; diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index 4cebccb6fc8b..32992cb07c4f 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -132,7 +132,8 @@ sprd_iommu_pgt_size(struct iommu_domain *domain) SPRD_IOMMU_PAGE_SHIFT) * sizeof(u32); } -static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type) +static struct iommu_domain *sprd_iommu_domain_alloc(struct device *dev, + unsigned int domain_type) { struct sprd_iommu_domain *dom; diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c index 5b585eace3d4..10c7eb9c5f11 100644 --- a/drivers/iommu/sun50i-iommu.c +++ b/drivers/iommu/sun50i-iommu.c @@ -667,7 +667,8 @@ static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain *domain, sun50i_iova_get_page_offset(iova); } -static struct iommu_domain *sun50i_iommu_domain_alloc(unsigned type) +static struct iommu_domain *sun50i_iommu_domain_alloc(struct device *dev, + unsigned type) { struct sun50i_iommu_domain *sun50i_domain; diff --git a/drivers/iommu/tegra-gart.c b/drivers/iommu/tegra-gart.c index ed53279d1106..75bcf191b091 100644 --- a/drivers/iommu/tegra-gart.c +++ b/drivers/iommu/tegra-gart.c @@ -141,7 +141,8 @@ static void gart_iommu_detach_dev(struct iommu_domain *domain, spin_unlock(&gart->dom_lock); } -static struct iommu_domain *gart_iommu_domain_alloc(unsigned type) +static struct iommu_domain *gart_iommu_domain_alloc(struct device *dev, + unsigned type) { struct iommu_domain *domain; diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c index 5b1af40221ec..c7c34d9f60ec 100644 --- a/drivers/iommu/tegra-smmu.c +++ b/drivers/iommu/tegra-smmu.c @@ -272,7 +272,8 @@ static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id) clear_bit(id, smmu->asids); } -static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type) +static struct iommu_domain *tegra_smmu_domain_alloc(struct device *dev, + unsigned type) { struct tegra_smmu_as *as; diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 59f1abd6ee53..7f397767a8e2 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -637,7 +637,8 @@ static void viommu_event_handler(struct virtqueue *vq) /* IOMMU API */ -static struct iommu_domain *viommu_domain_alloc(unsigned type) +static struct iommu_domain *viommu_domain_alloc(struct device *dev, + unsigned type) { struct viommu_domain *vdomain; diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 35af9d4e3969..3a8930d36046 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -252,7 +252,8 @@ struct iommu_ops { bool (*capable)(struct device *dev, enum iommu_cap); /* Domain allocation and freeing by the iommu driver */ - struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); + struct iommu_domain *(*domain_alloc)(struct device *dev, + unsigned iommu_domain_type); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev);