Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp559138lqh; Tue, 7 May 2024 07:27:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWdCYW6+WW8wTMqpB7k/6FWqIMZU7hOPQLZ5OGTG6gtP5JUJ+CAMFARxlH3YD70ktouj4kDi5Mrx5egpqflLhIPSYfpHXSJdWcdqlMSTw== X-Google-Smtp-Source: AGHT+IEmNZy+n7daH9Em3crkOI0DwFTfyjV+57Lps3HMJi856bfpz2ql4HeNMlzc1btJ0mmZDMZg X-Received: by 2002:a17:906:6a1e:b0:a59:b8e2:a0cd with SMTP id qw30-20020a1709066a1e00b00a59b8e2a0cdmr5903465ejc.59.1715092045800; Tue, 07 May 2024 07:27:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715092045; cv=pass; d=google.com; s=arc-20160816; b=OHB2wWIIpdOTl5Lmb6VJBtEZWFzl+gKE04TvhAncxxT06Zwr6pEKKiYQSkQRzK9R/t cxcOK2ToK9cMxyMOR73Ijbg2ZrZ/hSU9ubQawZiTxISRqpi/A8sy2S7o6s27kfYCRNFs bJlnj3BZz7rRG8OhXl4cxNsEKymLqcLVrclO3SMy79oHBXh8bI6JTk5bKh8zeYHP3a9J bdi4FxhKmnGlZFPAzqAw13FCONrzTtSUQuCpWRZcxnH/LbuBCIN9jFXnEl+rBWO0Q22B KmleopsNLCjthp1a41j9mvZKihaOFSvtrn35s+SHluJXt7Rod2CXelHW2LmX8GeA6Qyy n6Cg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=QsG7aE0HnxZsvtjvosuXtYKV2huEonpRW6TKXMCUJnU=; fh=xg3XGU8XJIxAk/neY2IYfnMKeodPuGkt/ObQwm/X6iU=; b=ETuinWaooo1hnlJJQYkbMmuzl3/HRTeQ0pU2pDiYhlvGp+BcQgxNYzHGHT04HSd3b9 8XjheeMzgfn+ZDCvlQDLE6SnT36tkQ9fW2BtgVyQakjDqfNbTUBO9NxiBJOkcqCHxs9n ufmXnAQs2bkuuEsUSPJkk1C1vLKKmKligzFRM6OfvrG2m2tIGHpu8udd07sQGvnehI/i hSNfORckPXc7MxCy301kVc+dgX0nyXYrpOLA242VEyFlXs51omS+13lsHMzu7vBcwOhO SMDeSPaTqI8wULXE2MsqJG7sVzP9i+miO/T/vy5YjybpmC68bFyFb+7AEbbuVncSj/aE RrKw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=mjjOo1eE; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-171576-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171576-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p25-20020a170906141900b00a51c4d40bfbsi4901383ejc.1034.2024.05.07.07.27.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 07:27:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171576-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=mjjOo1eE; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-171576-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171576-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 443111F21798 for ; Tue, 7 May 2024 14:27:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3BAC6161313; Tue, 7 May 2024 14:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="mjjOo1eE" Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6AAF161902 for ; Tue, 7 May 2024 14:26:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715091984; cv=none; b=G8z3Mn17Ty/kTEFmcLRK8E0h1rTRjXmZ3RhInrNu+72Ej41G0viDBMX4tg+KsfguWoBnSHg/4ghaowwnUefDHzRKBbaPU4S/eooL3Wx9nWYGYg+CnN1iHv2rBtElt3zc7HFSrk9Ziij+4ujurPRL0Bu4h0m3xVVOXppLWqElslw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715091984; c=relaxed/simple; bh=p9LzzUVq84gigk4g1jMkvkAiSTYV+AnJqJecyq5AEds=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=aDbFYStaw1++dFcwrrJk1Fwpf3PmWjR0IYC9vVnsjFP3ZrqlCdqAopAy2/3SFnUpaUXsar/pLScAyR6sDZl917osaE64DZ0HS4DC8qKLwe/rZPPGTQo4ZlNGrgd3oXvhl4A4GQ0VqMPi+oyvYyxNmUqC99k0bCiUm4Qqt8LlbnQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=mjjOo1eE; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6f44b5e7f07so2268981b3a.2 for ; Tue, 07 May 2024 07:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1715091982; x=1715696782; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=QsG7aE0HnxZsvtjvosuXtYKV2huEonpRW6TKXMCUJnU=; b=mjjOo1eEpyGke4cQdaudCWJ/o3HM4QjmGS9ZQjwFrFoWtYIO6f/5XEXeMeT7tk/yg/ cUKrSnDcJCp7Ux3juRvMlfsVWtkXRmfs+T2qWXrhu4+E0yFBXOfuQEGHd9qxJA4Z0ZxG PIiF79Txcj03/7LOLpOLxBkeu45iuWhdcqeXJW5QJzVnrm7AHJPXRQhoTwwAIkjlIjUP 6e3jy3niKr8L2L2xIdu8KFH490mDEdTF/4NUPd4HHywquVOUGv35cD96TNuSqPizWiso 0Pt+KqjL2hGcb2e5kh4D3an0avnlrLK7CKQ8MhW1Oa71qOwVjTMIlp3n2T9cl3rqCDOQ WtBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715091982; x=1715696782; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QsG7aE0HnxZsvtjvosuXtYKV2huEonpRW6TKXMCUJnU=; b=epZHJ6Fkk8Rh+pnsJDf94mnv0DG1eHz9Ce/Y6sXYapX8BgmlPDi76hjpKfH1WeB4I3 UcAR7qt407EI9wsfADbvxEEIfA6meWKxRJasziau2akEIRNp0p9kJ6pOWd2KvDBVdZp8 jzPyhLwJAbCjkTkXKWuPMfU7ItdMmWSrjpqqZwLSn7lFfyMWIhK88qGd+w9Wf+4iH8Pj Pcn+Eu1sdJHAnuBx7AwC0GyhppwaK3ipG2Wq+/0ikeWiKJW3np18MJ52h98HMyLe8Nff jq67ZP8ZxdDLHBGgIxki+gKlr8ZeDusZeDg4N9HqYvLlae1IeB01tA1w70B4680zzvAZ ikvA== X-Forwarded-Encrypted: i=1; AJvYcCUpbX/tSQtWKKFJrE7CnTxaquHOkVLvKHbVL0GiRHSYRcTN+H+p3eDBA0gUzn/dODAoMkEkLV79A28AXyHr4rX2v+wOOO8/6qwn8KqH X-Gm-Message-State: AOJu0YwRw70wmae13Nm8SoIinHaO9UsrGLJgWBum97lxrKQxa1O1hDUZ bFPrMtXYCrQewaKgIBFQg3OxHHA2AdVMcHD67nDU/nhIeLIvuMgk9t+BXUcyfJU= X-Received: by 2002:a05:6a00:17a7:b0:6ec:e726:b6f5 with SMTP id s39-20020a056a0017a700b006ece726b6f5mr16072960pfg.26.1715091982078; Tue, 07 May 2024 07:26:22 -0700 (PDT) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id i22-20020aa79096000000b006f44bcbe7e3sm7687554pfa.201.2024.05.07.07.26.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 07:26:21 -0700 (PDT) From: Zong Li To: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, tjeznach@rivosinc.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, jgg@ziepe.ca, kevin.tian@intel.com, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-riscv@lists.infradead.org Cc: Zong Li Subject: [PATCH RFC RESEND 6/6] iommu/riscv: support nested iommu for flushing cache Date: Tue, 7 May 2024 22:26:00 +0800 Message-Id: <20240507142600.23844-7-zong.li@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240507142600.23844-1-zong.li@sifive.com> References: <20240507142600.23844-1-zong.li@sifive.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: This patch implements cache_invalidate_user operation for the userspace to flush the hardware caches for a nested domain through iommufd. Signed-off-by: Zong Li --- drivers/iommu/riscv/iommu.c | 91 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 9 ++++ 2 files changed, 100 insertions(+) diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index 7eda850df475..4dd58fe2242d 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -1522,9 +1522,100 @@ static void riscv_iommu_domain_free_nested(struct iommu_domain *domain) kfree(riscv_domain); } +static int riscv_iommu_fix_user_cmd(struct riscv_iommu_command *cmd, + unsigned int pscid, unsigned int gscid) +{ + u32 opcode = FIELD_GET(RISCV_IOMMU_CMD_OPCODE, cmd->dword0); + + switch (opcode) { + case RISCV_IOMMU_CMD_IOTINVAL_OPCODE: + u32 func = FIELD_GET(RISCV_IOMMU_CMD_FUNC, cmd->dword0); + + if (func != RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA && + func != RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA) { + pr_warn("The IOTINVAL function: 0x%x is not supported\n", + func); + return -EOPNOTSUPP; + } + + if (func == RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA) { + cmd->dword0 &= ~RISCV_IOMMU_CMD_FUNC; + cmd->dword0 |= FIELD_PREP(RISCV_IOMMU_CMD_FUNC, + RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA); + } + + cmd->dword0 &= ~(RISCV_IOMMU_CMD_IOTINVAL_PSCID | + RISCV_IOMMU_CMD_IOTINVAL_GSCID); + riscv_iommu_cmd_inval_set_pscid(cmd, pscid); + riscv_iommu_cmd_inval_set_gscid(cmd, gscid); + break; + case RISCV_IOMMU_CMD_IODIR_OPCODE: + /* + * Ensure the device ID is right. We expect that VMM has + * transferred the device ID to host's from guest's. + */ + break; + default: + pr_warn("The user command: 0x%x is not supported\n", opcode); + return -EOPNOTSUPP; + } + + return 0; +} + +static int riscv_iommu_cache_invalidate_user(struct iommu_domain *domain, + struct iommu_user_data_array *array) +{ + struct riscv_iommu_domain *riscv_domain = iommu_domain_to_riscv(domain); + struct riscv_iommu_device *iommu; + struct riscv_iommu_bond *bond; + struct riscv_iommu_command cmd; + struct iommu_hwpt_riscv_iommu_invalidate inv_info; + int ret, index; + + if (!riscv_domain) + return -EINVAL; + + /* Assume attached devices in the domain go through the same IOMMU device */ + spin_lock(&riscv_domain->lock); + list_for_each_entry_rcu(bond, &riscv_domain->bonds, list) { + if (bond->dev) { + iommu = dev_to_iommu(bond->dev); + break; + } + } + spin_unlock(&riscv_domain->lock); + + if (!iommu) + return -EINVAL; + + for (index = 0; index < array->entry_num; index++) { + ret = iommu_copy_struct_from_user_array(&inv_info, array, + IOMMU_HWPT_DATA_RISCV_IOMMU, + index, cmd); + if (ret) + break; + + ret = riscv_iommu_fix_user_cmd((struct riscv_iommu_command *)inv_info.cmd, + riscv_domain->pscid, + riscv_domain->s2->gscid); + if (ret == -EOPNOTSUPP) + continue; + + riscv_iommu_cmd_send(iommu, (struct riscv_iommu_command *)inv_info.cmd, 0); + riscv_iommu_cmd_iofence(&cmd); + riscv_iommu_cmd_send(iommu, &cmd, RISCV_IOMMU_QUEUE_TIMEOUT); + } + + array->entry_num = index; + + return ret; +} + static const struct iommu_domain_ops riscv_iommu_nested_domain_ops = { .attach_dev = riscv_iommu_attach_dev_nested, .free = riscv_iommu_domain_free_nested, + .cache_invalidate_user = riscv_iommu_cache_invalidate_user, }; static int diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index e10b6e236647..d93a8f11813d 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -689,6 +689,15 @@ struct iommu_hwpt_vtd_s1_invalidate { __u32 __reserved; }; +/** + * struct iommu_hwpt_riscv_iommu_invalidate - RISCV IOMMU cache invalidation + * (IOMMU_HWPT_TYPE_RISCV_IOMMU) + * @cmd: An array holds a command for cache invalidation + */ +struct iommu_hwpt_riscv_iommu_invalidate { + __aligned_u64 cmd[2]; +}; + /** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) -- 2.17.1