From patchwork Wed Aug 1 10:22:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kenneth Lee X-Patchwork-Id: 143267 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp680659ljj; Wed, 1 Aug 2018 03:24:06 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcwwWLZBdMdClpr6h8SzfjwUWV8b+C3GHmZjQRjXeEXvT7so/f1V/c1ORDq/LGP15TEMnhB X-Received: by 2002:a62:bd4:: with SMTP id 81-v6mr26284131pfl.67.1533119046492; Wed, 01 Aug 2018 03:24:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533119046; cv=none; d=google.com; s=arc-20160816; b=VK9PYfgdHiyjujp4ordPgDsm82TlqPkcLy2R2wDL0G8yI0DyX/bUd56pzVV3sR7MPX gGq0SsmnyGkpezUU/x7Xyp+dxJUOyY4DY7EvF/Dto4z1Gm+SF9CD4vqJ0AfxGsZ6Yj19 yT3h5G+Wl2wN0pbbnsFACshrJSikFdM+ESgPE7y2x+kflPssVBE2XhLYDacplLkJv4Qk yT2nVkbbk1kFHjEd/+H9iPshp7NhCcCdAbB8wAnJirVcxQ0g+COQBXytVh87fDYtXXTo 9ORQGGalDOAVdfzscmabEyAjPCn/nB4KJ5Fej7ZHx2nIkNI6wbAKeH/zfuaGF7K16mVK 96xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=cWIq5icRCbDxGht9xmQ01P5jvmLkE08F35LVVt7ALwc=; b=WDDsMK97rBQlclevruVi3juPdkK2eqslxL8pfX17OQAaIHQokDIMYEt5sS5WwvZ2uW mRODGiI8qNvwgNi9PW8nVujLRrUAQ6OEcfEq7WL3jWET98G5bp8z/YvNoc1FUq7MAWyI N3CUulxb2AQoFwHCGHYoEl6lGpkQ8csTSu3r6rHzn5aV4lsTHbZ9zUK5dLVxkXS/heog cMWwDZNlN7gGlnKKiCFhpJEzUTJre+cYyxBz8dqSJWOSdbXUF+3YtSUG0YiaA/55MdMf 7oc18PkjheuK/qH7Fg3BiJREV4EDsfRe+zEPOlw1hMtYVjYQ3KXR5VhRlXkFQ9DNvzLO 5BGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kaBgnlL5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p3-v6si10930668plk.295.2018.08.01.03.24.06; Wed, 01 Aug 2018 03:24:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kaBgnlL5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387713AbeHAMJJ (ORCPT + 31 others); Wed, 1 Aug 2018 08:09:09 -0400 Received: from mail-qt0-f194.google.com ([209.85.216.194]:38795 "EHLO mail-qt0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387528AbeHAMJI (ORCPT ); Wed, 1 Aug 2018 08:09:08 -0400 Received: by mail-qt0-f194.google.com with SMTP id y19-v6so19249550qto.5; Wed, 01 Aug 2018 03:24: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=cWIq5icRCbDxGht9xmQ01P5jvmLkE08F35LVVt7ALwc=; b=kaBgnlL5JfMglGejfXzVsdfNd5eh41QX4U8wI6PPauYfESoPOyF3CtT6Sr5zimBzAo vsiuzw33+3xhV8/NWrzmODWA8dwDPxjrGikYfhKJRGWR/LtsZsFqZrQuKi3VWtoMScD3 I9qb2jxYxFCvf3qKEJlA/YCJVy4ZG0QHbifuIggFh7K0Kg8HWZ3ZkfHuykEdtqSiIkW0 znrMJwjgNVbQaKGglmu6Y3LayfkP44eqarEIA4RPRQBRwK37HA390vcT+kB7/yPZIzrR sGR+qcaamvizbjJEqjmuU+jqaZYhxdWlUy7CP44HfFhgCUvFKvLWk6UkxjruBbKbPPUr /oXA== 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=cWIq5icRCbDxGht9xmQ01P5jvmLkE08F35LVVt7ALwc=; b=RYdXUTs2jllZFqiYi1RUk0o644A1J5XqqNhiQSRcAkEX43lGoos4JTeH9gcfuwXQuj j7XfapGIdG7vytZTTK7GDx9K5WxZxi0k3HhUCput5X2SvQ4HubMoB7/j/8u3A4sHBhJM UtXf9bcy7ZzPcTmMtogMNX3UC4P6txe7IF/vk0NMDJJDecR+yogt1B6M+MJlrC1VxNHD uKzxXgX8l26NHHcfj56I9EzxQKCYTDOWGFrOk8ZHHjp7ohiAtfSjammiidsn1iv2VznM 9fBUWDkMlapDvp+0NZCho0mFb9B9G1wg1c0E3eWaD1bfMffWbd9eyUNWqjzfykNAZ6dP m4GQ== X-Gm-Message-State: AOUpUlFTh1Pd6FkcEA0dQTzgA8ST5mIH9mjTXiBV+QYALnCplzpZkL1q ecxuoTJxZB2tBn3sxmiEIvU= X-Received: by 2002:ac8:43da:: with SMTP id w26-v6mr16382599qtn.137.1533119042468; Wed, 01 Aug 2018 03:24:02 -0700 (PDT) Received: from localhost.localdomain ([104.237.86.144]) by smtp.gmail.com with ESMTPSA id s19-v6sm11176890qtj.61.2018.08.01.03.23.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Aug 2018 03:24:02 -0700 (PDT) From: Kenneth Lee To: Jonathan Corbet , Herbert Xu , "David S . Miller" , Joerg Roedel , Alex Williamson , Kenneth Lee , Hao Fang , Zhou Wang , Zaibo Xu , Philippe Ombredanne , Greg Kroah-Hartman , Thomas Gleixner , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-accelerators@lists.ozlabs.org, Lu Baolu , Sanjay Kumar Cc: linuxarm@huawei.com Subject: [RFC PATCH 2/7] iommu: Add share domain interface in iommu for spimdev Date: Wed, 1 Aug 2018 18:22:16 +0800 Message-Id: <20180801102221.5308-3-nek.in.cn@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180801102221.5308-1-nek.in.cn@gmail.com> References: <20180801102221.5308-1-nek.in.cn@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kenneth Lee This patch add sharing interface for a iommu_group. The new interface: iommu_group_share_domain() iommu_group_unshare_domain() can be used by some virtual iommu_group (such as iommu_group for spimdev) to share their parent's iommu_group. When the domain of the group is shared, it cannot be changed before unshared. In the future, notification can be added if update is required. Signed-off-by: Kenneth Lee --- drivers/iommu/iommu.c | 28 +++++++++++++++++++++++++++- include/linux/iommu.h | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 63b37563db7e..a832aafe660d 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -54,6 +54,9 @@ struct iommu_group { int id; struct iommu_domain *default_domain; struct iommu_domain *domain; + atomic_t domain_shared_ref; /* Number of user of current domain. + * The domain cannot be modified if ref > 0 + */ }; struct group_device { @@ -353,6 +356,7 @@ struct iommu_group *iommu_group_alloc(void) return ERR_PTR(ret); } group->id = ret; + atomic_set(&group->domain_shared_ref, 0); ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, NULL, "%d", group->id); @@ -482,6 +486,25 @@ int iommu_group_set_name(struct iommu_group *group, const char *name) } EXPORT_SYMBOL_GPL(iommu_group_set_name); +struct iommu_domain *iommu_group_share_domain(struct iommu_group *group) +{ + /* the domain can be shared only when the default domain is used */ + /* todo: more shareable check */ + if (group->domain != group->default_domain) + return ERR_PTR(-EINVAL); + + atomic_inc(&group->domain_shared_ref); + return group->domain; +} +EXPORT_SYMBOL_GPL(iommu_group_share_domain); + +void iommu_group_unshare_domain(struct iommu_group *group) +{ + atomic_dec(&group->domain_shared_ref); + WARN_ON(atomic_read(&group->domain_shared_ref) < 0); +} +EXPORT_SYMBOL_GPL(iommu_group_unshare_domain); + static int iommu_group_create_direct_mappings(struct iommu_group *group, struct device *dev) { @@ -1401,7 +1424,8 @@ static int __iommu_attach_group(struct iommu_domain *domain, { int ret; - if (group->default_domain && group->domain != group->default_domain) + if ((group->default_domain && group->domain != group->default_domain) || + atomic_read(&group->domain_shared_ref) > 0) return -EBUSY; ret = __iommu_group_for_each_dev(group, domain, @@ -1438,6 +1462,8 @@ static void __iommu_detach_group(struct iommu_domain *domain, { int ret; + WARN_ON(atomic_read(&group->domain_shared_ref) > 0); + if (!group->default_domain) { __iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 19938ee6eb31..278d60e3ec39 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -349,6 +349,8 @@ extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr, void *data); extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr, void *data); +extern struct iommu_domain *iommu_group_share_domain(struct iommu_group *group); +extern void iommu_group_unshare_domain(struct iommu_group *group); /* Window handling function prototypes */ extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,