From patchwork Mon Nov 6 07:12:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Tina" X-Patchwork-Id: 161835 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2487809vqu; Sun, 5 Nov 2023 23:14:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IGu66dXHfB8bC3zU6OWMbHWbSVdBPa64ENRSzxmHZeogRjcM2tNqSK09GzokllJb3TznZv1 X-Received: by 2002:a17:903:30d4:b0:1c4:4462:f1bd with SMTP id s20-20020a17090330d400b001c44462f1bdmr19901409plc.35.1699254885727; Sun, 05 Nov 2023 23:14:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699254885; cv=none; d=google.com; s=arc-20160816; b=BFTYjn+5I8txh5C1FDKz8NaKRNIXpb60ry6kpl3YD8wqBCElyRSQeuumqArAZ2q7uH rDf7Xo4x3SKYXCcxlMny9uzNWqkRqpreOjdh/zDLvEruLWS/qM2tG3QDgleTIMt+f9gr BmZDbT9UiOsb18ou4v1oTzoHn/3kznov2jK23u763O2dhYBPmyji3g0AVUjz59B/p/ct Hyv24GI93Sz1xuAbGxpqYjIxorraFWdstLDVSLkAXUzG84gSWT7gSFsFEWexGZ0OWj3X qpwY6zsiFj5zGLEIajudQrzynI6zeYEWue9vYxE7GS9o+3Qd1CXlfGcY/9VdyAhEpZaC 0bJg== 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 :dkim-signature; bh=4OSmhB13o36GGCYPfP32Z4ZWypmqrm49R+/69lLR000=; fh=Q9/c0/1s/r5nQXfLb7W3lFTON2d/CYIL48MtbxJzpb8=; b=1E9yquIRwka6HdcmyBTlOZATe2xyBcyOJb1jxFY9KLTGzCOtT98JlSeTPG4BdXe9Zm qMRygBARJK8YYU889Mr13NmWps9T6/V5lBL7o7jE7uJWuQESS0VbOAZexPxso9JjxVwM L+VLRzRCJFETlr1twixqHnCRm7ny5iSQAq6qHTAb0mIYTVJdwCnwfXTwuOtypCCdm6gb +2jxOZwd4SpVWc3AFnqyqFVkjKARaXa3DDLT8EG3sYFWeh25VwJfEGjvZ/YFPkv+QMZ7 jF7qsrsVNjprGxEgKAEvLNf0XnQRzUmD/rSeY/z2TT3CaTbYglsYSGIW4LFNsAS03CHZ MsPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Yz3QYMGX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id o10-20020a170903300a00b001c61bd9db5csi6959259pla.317.2023.11.05.23.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 23:14:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Yz3QYMGX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id AA41980A2379; Sun, 5 Nov 2023 23:14:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229755AbjKFHOE (ORCPT + 35 others); Mon, 6 Nov 2023 02:14:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbjKFHOD (ORCPT ); Mon, 6 Nov 2023 02:14:03 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44A93D8 for ; Sun, 5 Nov 2023 23:14:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699254841; x=1730790841; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=c9GeMl676tMxuvRJHqXrxAMwFdaSUf5heq0NrXZPU0Q=; b=Yz3QYMGXBJaVza0EErGtDr++UqnzoYwipTPYBeeJZIjZITKxzCNvxyGV KKMiM9oe9bDJdynXu5iR4jDXxeE8W+vLpWHzrthCKCkcDOF1q8woWVMRT kyxVKLDI3dC5HsfrKiiElE4+lgelm0dCGwKJw5AWPpD1eXuiKUVAZl5i8 OQprKUnp0uHtkv8jhE5TW13W7zobrb5UwfKAPRX69pMGcS41kg11Ysd8l PShFL+qPhrtR/6mf2KjT6eBw4SVKDTf0f00AJ2gvk9FRwqeLmSxBiNr6B jExKqBxZgF4P6i6w3y2B2P9wXUbivZW8Qbh8UuDFFVryElB5cA4we0pft g==; X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="10759042" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="10759042" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:14:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="1093690868" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="1093690868" Received: from sqa-gate.sh.intel.com (HELO localhost.localdomain) ([10.239.48.212]) by fmsmga005.fm.intel.com with ESMTP; 05 Nov 2023 23:13:57 -0800 From: Tina Zhang To: Jean-Philippe Brucker , Kevin Tian , Lu Baolu , joro@8bytes.org, will@kernel.org, Yi Liu Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Tina Zhang Subject: [RFC PATCH 1/5] iommu/virtio-iommu: Correct the values of granule and nr_pages Date: Mon, 6 Nov 2023 02:12:22 -0500 Message-Id: <20231106071226.9656-2-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231106071226.9656-1-tina.zhang@intel.com> References: <20231106071226.9656-1-tina.zhang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sun, 05 Nov 2023 23:14:39 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781797891208579922 X-GMAIL-MSGID: 1781797891208579922 The value of granule is ilog2(pgsize). When the value of pgsize isn't a power of two, granule would make pgsize less than the actual size of pgsize. E.g., if pgsize = 0x6000 and granule = ilog2(gather->pgsize), then granule = 0xe. 2^0xe = 0x4000 makes the pgsize (0x4000) smaller than the actual pgsize (0x6000). Invalidating IOTLB with smaller range would lead to cache incoherence. So, roundup pgsize value to the nearest power of 2 to make sure the granule won't make pgsize less than the actual size. The value of "gather->end - gather->start + 1" also needs similar adjustment. Signed-off-by: Tina Zhang --- drivers/iommu/virtio-iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 08e310672e57..b1ceaac974e2 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -1289,8 +1289,8 @@ static void viommu_iotlb_sync(struct iommu_domain *domain, if (!gather->pgsize) return; - granule = ilog2(gather->pgsize); - nr_pages = (gather->end - gather->start + 1) >> granule; + granule = ilog2(__roundup_pow_of_two(gather->pgsize)); + nr_pages = __roundup_pow_of_two(gather->end - gather->start + 1) >> granule; req = (struct virtio_iommu_req_invalidate) { .head.type = VIRTIO_IOMMU_T_INVALIDATE, .inv_gran = cpu_to_le16(VIRTIO_IOMMU_INVAL_G_VA), From patchwork Mon Nov 6 07:12:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Tina" X-Patchwork-Id: 161837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2487957vqu; Sun, 5 Nov 2023 23:15:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IHij4EJ8PR8+gUGV9yLod02IjngXHfDpiPw5qY/S28wGS9ScSQ+iYmDsRzePRpb0mKnCHD0 X-Received: by 2002:a17:90b:3117:b0:280:c0:9d3f with SMTP id gc23-20020a17090b311700b0028000c09d3fmr26280673pjb.34.1699254908766; Sun, 05 Nov 2023 23:15:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699254908; cv=none; d=google.com; s=arc-20160816; b=V/5PI+P874cowVRmKIU1kC0Vt3cWZZUSd78SxPmEve+ZTaAKhX5Ip1qBiNCl2r5t0B hB61ocD8P6KtdW8RfCxyjJjYD4C0qka5c0BWppIw1/NLgDpbJXmQbSUB0Rf6WYhVC+Tg hHO62SAeV4Q/SNGMH7GEB4gkN+mOvuauqVi8D7xJOMaVlAvs9HeF0d2Tk5TV1WTzWIlg adM7KfhqPaueq7TxhPDtXHS5EC+BImMSOAWgcfR3iqfVAnQYmr2IrbBwS78dulb7a/CF VaYybjocwjvxEGJQR88/zGkQ45zOrfwVW4RrYT08U9EPvZmdgcOsCQkiON17G3ZVeZx6 VMpQ== 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 :dkim-signature; bh=2L3QWPxlbfqgOSDpN3yyrvQ1OErTrLW6iqdzR5DcBwI=; fh=Q9/c0/1s/r5nQXfLb7W3lFTON2d/CYIL48MtbxJzpb8=; b=f3vubweNQS705riArH3tNZUVwnH1A5APf5Ao3fAImSZwQRU7BZNNUEEePYfwzXSt0H dZB/gCan4TswCSXire2gK+9DXisnlot0KVoaYBYqwzhN5euDLzpMI4THt0a4XEG3fMc4 0qvF1Tk87chBdLARsdZvQWpGAa7WeIZqFjws6n1cEmwHwn5B4a0soteHNTx7oskxCz3W oNbhY4cm86yXhWCY6qmFBRPLSaVOga/0awAOP+/xy4EjftJ488XELNfHGpPBB09/rBnR KGUGgj/HZ87dr3FqmmViJk4cV3CfVun10fBBVVh4yw7ukJaOSZp8jKDGZMiX60sQSiEI t8rA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=a1qCtoE2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id pi11-20020a17090b1e4b00b00268293118efsi7800051pjb.47.2023.11.05.23.15.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 23:15:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=a1qCtoE2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id B705F80A2383; Sun, 5 Nov 2023 23:15:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230499AbjKFHOI (ORCPT + 35 others); Mon, 6 Nov 2023 02:14:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230469AbjKFHOG (ORCPT ); Mon, 6 Nov 2023 02:14:06 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7E31CC for ; Sun, 5 Nov 2023 23:14:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699254844; x=1730790844; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=S7SongTvx/p0pTy8okzpB1bVkyF2tFwb7xoxlsuTkxM=; b=a1qCtoE2Ieor2z5id0XlQi9RpyZ4vMSXb6MD9YURfRsCY7cCkSJuWk5S RuDjTsvm+uNO3+/uXlHK+sR+/hOvWVrg7Nn3cx5sMgl6Q5hYLDBOAJVhw z1K9CqunG7PJRWZgQTMQO88lxOV24U2qSMzraxBOvkktYaQNqb2/XKher IQRRbcbw3F1zJwW4JXHtk54yntRMDUMt/Odbsr8Cm5xRwPd+TwxI9f+5r t1FeP9utGfFxgjJRHmeQgg5Etz/KZ8BssHfvOQm5juqhm47yV2hUje+vu gpteEVAZLJN39CJqfpUPsCpCFNj5F6bfBtNzVRc2fVx9nSnFNHbdeIwDM w==; X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="10759052" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="10759052" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:14:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="1093690884" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="1093690884" Received: from sqa-gate.sh.intel.com (HELO localhost.localdomain) ([10.239.48.212]) by fmsmga005.fm.intel.com with ESMTP; 05 Nov 2023 23:14:00 -0800 From: Tina Zhang To: Jean-Philippe Brucker , Kevin Tian , Lu Baolu , joro@8bytes.org, will@kernel.org, Yi Liu Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Tina Zhang Subject: [RFC PATCH 2/5] iommu/vt-d: Add generic IO page table support Date: Mon, 6 Nov 2023 02:12:23 -0500 Message-Id: <20231106071226.9656-3-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231106071226.9656-1-tina.zhang@intel.com> References: <20231106071226.9656-1-tina.zhang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sun, 05 Nov 2023 23:15:05 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781797915254444458 X-GMAIL-MSGID: 1781797915254444458 Add basic hook up code to implement generic IO page table framework. Signed-off-by: Tina Zhang --- drivers/iommu/intel/Kconfig | 1 + drivers/iommu/intel/iommu.c | 94 +++++++++++++++++++++++++++++++++++++ drivers/iommu/intel/iommu.h | 7 +++ drivers/iommu/io-pgtable.c | 3 ++ include/linux/io-pgtable.h | 2 + 5 files changed, 107 insertions(+) diff --git a/drivers/iommu/intel/Kconfig b/drivers/iommu/intel/Kconfig index 2e56bd79f589..8334e7e50e69 100644 --- a/drivers/iommu/intel/Kconfig +++ b/drivers/iommu/intel/Kconfig @@ -15,6 +15,7 @@ config INTEL_IOMMU select DMA_OPS select IOMMU_API select IOMMU_IOVA + select IOMMU_IO_PGTABLE select NEED_DMA_MAP_STATE select DMAR_TABLE select SWIOTLB diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index dbcdf7b95b9f..80bd1993861c 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "iommu.h" #include "../dma-iommu.h" @@ -67,6 +68,20 @@ #define LEVEL_STRIDE (9) #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) +#define io_pgtable_cfg_to_dmar_pgtable(x) \ + container_of((x), struct dmar_io_pgtable, pgtbl_cfg) + +#define io_pgtable_to_dmar_pgtable(x) \ + container_of((x), struct dmar_io_pgtable, iop) + +#define io_pgtable_to_dmar_domain(x) \ + container_of(io_pgtable_to_dmar_pgtable(x), \ + struct dmar_domain, dmar_iop) + +#define io_pgtable_ops_to_dmar_domain(x) \ + container_of(io_pgtable_to_dmar_pgtable(io_pgtable_ops_to_pgtable(x)), \ + struct dmar_domain, dmar_iop) + static inline int agaw_to_level(int agaw) { return agaw + 2; @@ -5171,3 +5186,82 @@ int ecmd_submit_sync(struct intel_iommu *iommu, u8 ecmd, u64 oa, u64 ob) return ret; } + +static void flush_all(void *cookie) +{ +} + +static void flush_walk(unsigned long iova, size_t size, + size_t granule, void *cookie) +{ +} + +static void add_page(struct iommu_iotlb_gather *gather, + unsigned long iova, size_t granule, + void *cookie) +{ +} + +static const struct iommu_flush_ops flush_ops = { + .tlb_flush_all = flush_all, + .tlb_flush_walk = flush_walk, + .tlb_add_page = add_page, +}; + +static void free_pgtable(struct io_pgtable *iop) +{ + struct dmar_domain *dmar_domain = io_pgtable_to_dmar_domain(iop); + + if (dmar_domain->pgd) { + LIST_HEAD(freelist); + + domain_unmap(dmar_domain, 0, DOMAIN_MAX_PFN(dmar_domain->gaw), &freelist); + put_pages_list(&freelist); + } +} + +static int pgtable_map_pages(struct io_pgtable_ops *ops, unsigned long iova, + phys_addr_t paddr, size_t pgsize, size_t pgcount, + int iommu_prot, gfp_t gfp, size_t *mapped) +{ + struct dmar_domain *dmar_domain = io_pgtable_ops_to_dmar_domain(ops); + + return intel_iommu_map_pages(&dmar_domain->domain, iova, paddr, pgsize, + pgcount, iommu_prot, gfp, mapped); +} + +static size_t pgtable_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova, + size_t pgsize, size_t pgcount, + struct iommu_iotlb_gather *gather) +{ + struct dmar_domain *dmar_domain = io_pgtable_ops_to_dmar_domain(ops); + + return intel_iommu_unmap_pages(&dmar_domain->domain, iova, pgsize, + pgcount, gather); +} + +static phys_addr_t pgtable_iova_to_phys(struct io_pgtable_ops *ops, + unsigned long iova) +{ + struct dmar_domain *dmar_domain = io_pgtable_ops_to_dmar_domain(ops); + + return intel_iommu_iova_to_phys(&dmar_domain->domain, iova); +} + +static struct io_pgtable *alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) +{ + struct dmar_io_pgtable *pgtable = io_pgtable_cfg_to_dmar_pgtable(cfg); + + pgtable->iop.ops.map_pages = pgtable_map_pages; + pgtable->iop.ops.unmap_pages = pgtable_unmap_pages; + pgtable->iop.ops.iova_to_phys = pgtable_iova_to_phys; + + cfg->tlb = &flush_ops; + + return &pgtable->iop; +} + +struct io_pgtable_init_fns io_pgtable_intel_iommu_init_fns = { + .alloc = alloc_pgtable, + .free = free_pgtable, +}; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 8d0aac71c135..5207fea6477a 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -579,6 +580,11 @@ struct iommu_domain_info { * to VT-d spec, section 9.3 */ }; +struct dmar_io_pgtable { + struct io_pgtable_cfg pgtbl_cfg; + struct io_pgtable iop; +}; + struct dmar_domain { int nid; /* node id */ struct xarray iommu_array; /* Attached IOMMU array */ @@ -633,6 +639,7 @@ struct dmar_domain { struct iommu_domain domain; /* generic domain data structure for iommu core */ + struct dmar_io_pgtable dmar_iop; }; /* diff --git a/drivers/iommu/io-pgtable.c b/drivers/iommu/io-pgtable.c index 5755dee96a68..533b27557290 100644 --- a/drivers/iommu/io-pgtable.c +++ b/drivers/iommu/io-pgtable.c @@ -35,6 +35,9 @@ io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = { #ifdef CONFIG_IOMMU_IO_PGTABLE_VIRT [VIRT_IO_PGTABLE] = &io_pgtable_virt_init_fns, #endif +#ifdef CONFIG_INTEL_IOMMU + [INTEL_IOMMU] = &io_pgtable_intel_iommu_init_fns, +#endif }; struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt, diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h index bdcade2c4844..b2857c18f963 100644 --- a/include/linux/io-pgtable.h +++ b/include/linux/io-pgtable.h @@ -20,6 +20,7 @@ enum io_pgtable_fmt { APPLE_DART, APPLE_DART2, VIRT_IO_PGTABLE, + INTEL_IOMMU, IO_PGTABLE_NUM_FMTS, }; @@ -281,5 +282,6 @@ extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns; extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v2_init_fns; extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns; extern struct io_pgtable_init_fns io_pgtable_virt_init_fns; +extern struct io_pgtable_init_fns io_pgtable_intel_iommu_init_fns; #endif /* __IO_PGTABLE_H */ From patchwork Mon Nov 6 07:12:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Tina" X-Patchwork-Id: 161836 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2487846vqu; Sun, 5 Nov 2023 23:14:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjVbIvC4QNGydMbCb9WNSX0WBT5O+N5yyrQDZ6pHXZCvVPrVPMJcer3NRCWzEkKvaLOm08 X-Received: by 2002:a05:6358:5e14:b0:169:51f1:b097 with SMTP id q20-20020a0563585e1400b0016951f1b097mr28883586rwn.9.1699254891163; Sun, 05 Nov 2023 23:14:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699254891; cv=none; d=google.com; s=arc-20160816; b=qruBXoQQo/WjFChZgR5sk83z0dbB32ND28AEz8f9TNn4AvEG7w+B87357cCTMYLwv2 1QYNwmNJTF4cgHU2v7PEtJmv0tgmm0Y7kIRPCAaaE1WYLUSLo2/nvCHUbcWB9rQUfYUd 7Nhxyf6MIpuoO1TIsONxYzogTz83S28m04tn7rZKZpzH4FPK7Caa8qw8pIJU1xshrhaj oIpH+Jsz5OAfim/3NyAgF6SvvI68hC73mESyPRDiJNyq7zP7YQKaw5WLzE2036RmB/cp q44Khu2vXQef7Y1QzHCW9Q7TVXhiFtXTL9Jd/mma0EJu3Rbi0KDW0vBUEUAhPC9pUT5z LI0g== 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 :dkim-signature; bh=5WNSJDRH/exdpoDprUS3Iv17Tw6NE3O9LoXRfx312Ug=; fh=Q9/c0/1s/r5nQXfLb7W3lFTON2d/CYIL48MtbxJzpb8=; b=F1Qrat8tp/IF5fyDdrWdn7gxTkfP5AIa5mTwduWXnDX4z9EhVEqXWXrikyk4cRRpOO SO47B5hMtg4WfUg7mCDKKQsugqhUpBliJCFkBTmUUuu3f3stdyX09P73Kzw0DAiVov0p g+JyJ4tYt4Ko5goKlmw3YJ/w1xu4NKD6H/OBdxgxDkl8iEWFjS2FVa8tNCsa0c0jl1Pp HA/w1pZYwgBBbetUreLF5u8vSxG1SpPKOoUeNt/3OCvY6SDsZV7wBxnyxOYSSCxs65zA HjgRPBFOk4BM9+i5m5rvjUS1Ry796PRMgUHcN+O9gSKaja0ZUJDr27N+qH0Dd0EBgkjx PXQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=iCGJDlT8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id 4-20020a630204000000b005ab190f9504si7478853pgc.602.2023.11.05.23.14.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 23:14:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=iCGJDlT8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id B1206809C926; Sun, 5 Nov 2023 23:14:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230511AbjKFHOK (ORCPT + 35 others); Mon, 6 Nov 2023 02:14:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230469AbjKFHOJ (ORCPT ); Mon, 6 Nov 2023 02:14:09 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44D71D8 for ; Sun, 5 Nov 2023 23:14:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699254846; x=1730790846; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gWxXYuEpy8G84pCRxEr8zR2qYt2dBXJPyl6P3BJNrtQ=; b=iCGJDlT80R6YCHX5t8JBh5lfoEhLNwqldMKYrIszg6vl+2jAWpg5MPKj fv+avqXdwNJAvcnaROHzr7Uj+ktrVGazLbsl9UNRcUx0SF30HdDfCqlpR QywpLBbS6x8zTZK6oaS4ifXvKn87imaFo7sF9SANme1m9GXEIvud7r/Um /Sqh1MKpPmzSx/91yGbI4BNHot85N1WZLNvrsCXRCntnYJacqJbkhIM+v TW8pa2lCHZxn9reEIlKOAs/agXepX84ib4yGF5wuVS68x+Qh1+Ub1PsxX JtQfbovuLeU9g2z3YGPNvEm6YbT6xLNd8k+i+Skz1m5nSRVnounzUB339 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="10759058" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="10759058" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:14:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="1093690893" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="1093690893" Received: from sqa-gate.sh.intel.com (HELO localhost.localdomain) ([10.239.48.212]) by fmsmga005.fm.intel.com with ESMTP; 05 Nov 2023 23:14:02 -0800 From: Tina Zhang To: Jean-Philippe Brucker , Kevin Tian , Lu Baolu , joro@8bytes.org, will@kernel.org, Yi Liu Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Tina Zhang Subject: [RFC PATCH 3/5] iommu/io-pgtable: Introduce struct vtd_cfg Date: Mon, 6 Nov 2023 02:12:24 -0500 Message-Id: <20231106071226.9656-4-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231106071226.9656-1-tina.zhang@intel.com> References: <20231106071226.9656-1-tina.zhang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 05 Nov 2023 23:14:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781797896982537760 X-GMAIL-MSGID: 1781797896982537760 VT-d hardware cap/ecap information is needed for driver to generate VT-d format IO page table. Add struct vtd_cfg to keep the info. Signed-off-by: Tina Zhang --- include/linux/io-pgtable.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h index b2857c18f963..ae6a2e44b027 100644 --- a/include/linux/io-pgtable.h +++ b/include/linux/io-pgtable.h @@ -147,6 +147,11 @@ struct io_pgtable_cfg { u32 n_ttbrs; } apple_dart_cfg; + struct { + u64 cap_reg; + u64 ecap_reg; + } vtd_cfg; + struct { dma_addr_t pgd; } virt; From patchwork Mon Nov 6 07:12:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Tina" X-Patchwork-Id: 161838 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2488069vqu; Sun, 5 Nov 2023 23:15:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHpPyb86x/HuWimSQetamBnFEugPrlcTC9GPJiMkdb9CXgGjZvD8kZsbPkHNu4um9mTItw/ X-Received: by 2002:a05:6358:7e56:b0:169:8afa:ac90 with SMTP id p22-20020a0563587e5600b001698afaac90mr16608085rwm.11.1699254924598; Sun, 05 Nov 2023 23:15:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699254924; cv=none; d=google.com; s=arc-20160816; b=iAabJu+/vwt3kyDN+euO6YU+kmzyPyU5YzTHLescOqMEcWgIPbed5Z48+LCWSEtHN2 sTzrpsDa/xmJUicw/GHZ7/4fqr/cEBVsr4jlnU7jKNEk9JJOh4TW1Mb48dlQWxfWLd2B jsS1i/YPcrGZVJ6hv5Gid4/fvfQ+SHXPVXrMKSSsi8xXdFGK12/kTer4FLJBisRr04K1 g1umkhOHJZeDhi0ZIjK8xO58CCfo9wTWY3737XahTCykuPa3KtG7Q36FHmawD7Vjuhgo M8kf/4pgenwydjY2FCb5nZBFXrE2Cq6rr+LmJAV7XlF9bK876dwkV2w2v9x5RLD+/m8P ze4A== 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 :dkim-signature; bh=1CYmFG461IIaZ/r0U4N+4BudLM/SQcN3BQcQZ5CzZlE=; fh=Q9/c0/1s/r5nQXfLb7W3lFTON2d/CYIL48MtbxJzpb8=; b=QEIs4+p9m+w21v68WxiU+aed9Azn6tOiEqozsaDk2OxyxpuCHo8uaHLlhqZ1X+Yejw g6XGl5E2zCrGXrWObt8bDmGbVnFPXXCDQPEAF9z9Acg86CkeNZecjWgIqNwChlJBRwfT vCCyASyphBvRGSiOaJMVY5IfuQeg4YaoH0ShHiugSn6CszTiHaZcVy3QBZ7mOkUVm0QL vh+TYvUmWo7u4uCOANaZHVs5uOAVlJfsl5VuLpN9ZI6yJzlUb5qAaFuKlW4ZjqgyhlHr jTDxYleaaDcszOCfGpdhwm9tAu8+Ur8gzbBFMGShpLeYRfDp1jK1Q5ayfEcyYxWnYJuZ WH9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FDDD+bqu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id bj5-20020a056a02018500b005bd3c9a9528si9005061pgb.263.2023.11.05.23.15.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 23:15:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FDDD+bqu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 8394980A236E; Sun, 5 Nov 2023 23:15:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230512AbjKFHOO (ORCPT + 35 others); Mon, 6 Nov 2023 02:14:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230520AbjKFHOL (ORCPT ); Mon, 6 Nov 2023 02:14:11 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF7FA125 for ; Sun, 5 Nov 2023 23:14:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699254849; x=1730790849; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AMX3QyiSsHY62hL/1ZifmtDUB3BOUarR8RpIWdZzzu4=; b=FDDD+bquOdN9x9TArfJUiGpReYtymTnUV90mF95hYEZLClkSsEVGfJ7N PF33sL4AvuZ2olIVqSJVdWzNmwPl+0tkNRV4ZWpBVS5sJJ3rQfcQ/R3kE XD4UyKV5PSC22a4W6JzT/iOOi7r9II/JYVS/r3i0OJw8kJoZS1HHjTE0T JPhedcIZa6wJgf0v+wqEnF0MLW/YkcgKq+07dqhtm880kVI8f5FqniFpH 6nKReK6cL8V8/zwUgDVm/sxX81PGBauo+CCZvA8We6UDWopLEK3UaKLZt PegyURF0zzxfvNCDUr0Sn80WH+xOTU1S+Iv0WNgXbdOZqmYRJll4YR9jq w==; X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="10759067" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="10759067" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:14:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="1093690901" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="1093690901" Received: from sqa-gate.sh.intel.com (HELO localhost.localdomain) ([10.239.48.212]) by fmsmga005.fm.intel.com with ESMTP; 05 Nov 2023 23:14:05 -0800 From: Tina Zhang To: Jean-Philippe Brucker , Kevin Tian , Lu Baolu , joro@8bytes.org, will@kernel.org, Yi Liu Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Tina Zhang Subject: [RFC PATCH 4/5] iommu/vt-d: Adapt alloc_pgtable interface to be used by others Date: Mon, 6 Nov 2023 02:12:25 -0500 Message-Id: <20231106071226.9656-5-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231106071226.9656-1-tina.zhang@intel.com> References: <20231106071226.9656-1-tina.zhang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sun, 05 Nov 2023 23:15:10 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781797931635694497 X-GMAIL-MSGID: 1781797931635694497 The generic IO page table framework provides a set of interfaces for invoking IO page table operations. Other entity (e.g., virtio-iommu driver) can use the interface to ask VT-d driver to generate a VT-d format IO page table. This patch adds the support. Signed-off-by: Tina Zhang --- drivers/iommu/intel/iommu.c | 69 +++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 80bd1993861c..d714e780a031 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -5248,17 +5248,80 @@ static phys_addr_t pgtable_iova_to_phys(struct io_pgtable_ops *ops, return intel_iommu_iova_to_phys(&dmar_domain->domain, iova); } +static void __iommu_calculate_cfg(struct io_pgtable_cfg *cfg) +{ + unsigned long fl_sagaw, sl_sagaw, sagaw; + int agaw, addr_width; + + fl_sagaw = BIT(2) | (cap_fl5lp_support(cfg->vtd_cfg.cap_reg) ? BIT(3) : 0); + sl_sagaw = cap_sagaw(cfg->vtd_cfg.cap_reg); + sagaw = fl_sagaw & sl_sagaw; + + for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH); agaw >= 0; agaw--) { + if (test_bit(agaw, &sagaw)) + break; + } + + addr_width = agaw_to_width(agaw); + if (cfg->ias > addr_width) + cfg->ias = addr_width; + if (cfg->oas != addr_width) + cfg->oas = addr_width; +} + static struct io_pgtable *alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) { - struct dmar_io_pgtable *pgtable = io_pgtable_cfg_to_dmar_pgtable(cfg); + struct dmar_io_pgtable *pgtable; + struct dmar_domain *domain; + int adjust_width; + + /* Platform must have nested translation support */ + if (!ecap_nest(cfg->vtd_cfg.ecap_reg)) + return NULL; + + domain = kzalloc(sizeof(*domain), GFP_KERNEL); + if (!domain) + return NULL; + + domain->nid = NUMA_NO_NODE; + domain->use_first_level = true; + domain->has_iotlb_device = false; + INIT_LIST_HEAD(&domain->devices); + spin_lock_init(&domain->lock); + xa_init(&domain->iommu_array); + + /* calculate AGAW */ + __iommu_calculate_cfg(cfg); + domain->gaw = cfg->ias; + adjust_width = guestwidth_to_adjustwidth(domain->gaw); + domain->agaw = width_to_agaw(adjust_width); + + domain->iommu_coherency = ecap_smpwc(cfg->vtd_cfg.ecap_reg); + domain->force_snooping = true; + domain->iommu_superpage = cap_fl1gp_support(cfg->vtd_cfg.ecap_reg) ? 2 : 1; + domain->max_addr = 0; + + cfg->coherent_walk = domain->iommu_coherency; + + pgtable = &domain->dmar_iop; + /* always allocate the top pgd */ + domain->pgd = alloc_pgtable_page(domain->nid, GFP_KERNEL); + if (!domain->pgd) + goto out_free_domain; + domain_flush_cache(domain, domain->pgd, PAGE_SIZE); + + cfg->virt.pgd = virt_to_phys(domain->pgd); + cfg->tlb = &flush_ops; pgtable->iop.ops.map_pages = pgtable_map_pages; pgtable->iop.ops.unmap_pages = pgtable_unmap_pages; pgtable->iop.ops.iova_to_phys = pgtable_iova_to_phys; - cfg->tlb = &flush_ops; - return &pgtable->iop; + +out_free_domain: + kfree(domain); + return NULL; } struct io_pgtable_init_fns io_pgtable_intel_iommu_init_fns = { From patchwork Mon Nov 6 07:12:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Tina" X-Patchwork-Id: 161839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2488456vqu; Sun, 5 Nov 2023 23:16:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IHro2Pq7hS7hkEFUUBprQXTeXirIYEL3JcJA2MLX8LLylYq139HLyB8Gxy093uMAIoyf9/S X-Received: by 2002:a17:90a:5997:b0:280:37c3:3bcf with SMTP id l23-20020a17090a599700b0028037c33bcfmr13384390pji.13.1699254995350; Sun, 05 Nov 2023 23:16:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699254995; cv=none; d=google.com; s=arc-20160816; b=oXoIYP4zOfPbzrWWehK3+oCfPDZR+YlHh2OvcM5ZzUQLFY+AGbid8Q4of5IRt/oCgm fAtVj3QFo5CZSNbhZoR5mOlPXSVrccfHsbbvHJpxWcjcXB7NFGQ6qT/Be0x+uxYqLFxj AU/5HeEDYWrmKkbRmqdoEECOcsjXNjpr0RnKc6DRX86w/DbxKEPoVpE+LQ59i3ivTCrO dH7Ppgb/CWuvAKplBlXioGayxLRs9/GSepqUySI1vQUs7zRM8eyqdqjOdtq+jIo7mPn/ RmvdB5lblk6Qo0NGB0+U0VsNwZ4apgRp5U8+23y8aL8zhSWc+WMGfqfOvlc8rkVo4+C6 jFmg== 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 :dkim-signature; bh=VPARBhx4V2Q4IIVegwniKg9++M5XesASdaSiFU6LguY=; fh=Q9/c0/1s/r5nQXfLb7W3lFTON2d/CYIL48MtbxJzpb8=; b=viTKl/fKV6ihdZ3OZ1O01YCNXD8l+EorosSVkiIywHD6wqEsDLchdd3/H2kr7l6xns XXwro1UGY34Emel2hIsKUDzxgDqtvy/4FmvzYinJJnm1k70WwjPUGCyNHTI0CgZb+4ne srIdk0ekOm5dfRao1so/r+xpxFbTjbh1Fpna0Z1u7MIflu0fmYY8nd0lRtyW9/yzH7dq TEZ0AzeNG2NYN8iWwEmob8N+cyQ2DkYV4+AcDCtnu/L58P3Jtp+6N92TZYos2g+UWqqT F7phcDJtAu+FMg4mQs26ctyAwpHKnQcGJszVlpLISC8UvUyB1r9nXaBBiaElSsMsGnkW aQTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Pdvq3G9o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id ck5-20020a056a02090500b005859aec9406si8198861pgb.16.2023.11.05.23.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 23:16:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Pdvq3G9o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 6F6BF805F95C; Sun, 5 Nov 2023 23:16:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229841AbjKFHQB (ORCPT + 35 others); Mon, 6 Nov 2023 02:16:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230527AbjKFHOO (ORCPT ); Mon, 6 Nov 2023 02:14:14 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4311AD69 for ; Sun, 5 Nov 2023 23:14:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699254851; x=1730790851; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JXrcRndberQgWoGawVfq93WylWTuPjjrsB1zsCyDKUk=; b=Pdvq3G9oshsK/10S7GAfl+yak1B80KtayzriiEpXy+JD8IR8QmZarkj+ 1GooCPhIB0OoS04T0mB9PgJoaiPGjdxWCEoT714ujaABCR369/0Evj3Ot A6MnCHgwx0t3GrcK330ROjNsjD6c7fYeT6XIxmiz/1CrjRZARBWjWCC/F Mrmk/5YXn9vRQkQodY26VRH2ASWb085Jw5ywtk4u4mFouRIHK8XED0xkC Dt+Llx2haVlsbuGO4C1kRhn6iHg96ZfjGrNc8EjSupLwBZJE+ZgCSDE2C +jShQeNsmNGjCh/oAOKLYu4UIjZm22lBTFQFWAToT7oxcPkCyI+jVCdXH Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="10759075" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="10759075" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:14:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="1093690912" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="1093690912" Received: from sqa-gate.sh.intel.com (HELO localhost.localdomain) ([10.239.48.212]) by fmsmga005.fm.intel.com with ESMTP; 05 Nov 2023 23:14:07 -0800 From: Tina Zhang To: Jean-Philippe Brucker , Kevin Tian , Lu Baolu , joro@8bytes.org, will@kernel.org, Yi Liu Cc: virtualization@lists.linux-foundation.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Tina Zhang Subject: [RFC PATCH 5/5] iommu/virtio-iommu: Support attaching VT-d IO pgtable Date: Mon, 6 Nov 2023 02:12:26 -0500 Message-Id: <20231106071226.9656-6-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231106071226.9656-1-tina.zhang@intel.com> References: <20231106071226.9656-1-tina.zhang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 05 Nov 2023 23:16:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781798006086338851 X-GMAIL-MSGID: 1781798006086338851 Add VT-d IO page table support to ATTACH_TABLE request. Signed-off-by: Tina Zhang --- drivers/iommu/virtio-iommu.c | 23 +++++++++++++++++++++++ include/uapi/linux/virtio_iommu.h | 26 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index b1ceaac974e2..b02eeb1d27a4 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -991,12 +991,25 @@ static int viommu_attach_pgtable(struct viommu_domain *vdomain, }; /* TODO: bypass flag? */ + if (vdomain->bypass == true) + return 0; switch (fmt) { case VIRT_IO_PGTABLE: req.format = cpu_to_le16(VIRTIO_IOMMU_FORMAT_PGTF_VIRT); req.pgd = cpu_to_le64((u64)cfg->virt.pgd); break; + case INTEL_IOMMU: { + struct virtio_iommu_req_attach_pgt_vtd *vtd_req = + (struct virtio_iommu_req_attach_pgt_vtd *)&req; + + vtd_req->format = cpu_to_le16(VIRTIO_IOMMU_FORMAT_PGTF_VTD); + vtd_req->pgd = cpu_to_le64((u64)cfg->virt.pgd); + vtd_req->addr_width = cpu_to_le32(cfg->oas); + vtd_req->pasid = IOMMU_NO_PASID; + break; + } + default: return -EINVAL; }; @@ -1034,6 +1047,16 @@ static int viommu_setup_pgtable(struct viommu_domain *vdomain, case VIRTIO_IOMMU_FORMAT_PGTF_VIRT: fmt = VIRT_IO_PGTABLE; break; + case VIRTIO_IOMMU_FORMAT_PGTF_VTD: + { + struct virtio_iommu_probe_pgt_vtd *vtd_desc = + (struct virtio_iommu_probe_pgt_vtd *)desc; + + cfg.vtd_cfg.cap_reg = le64_to_cpu(vtd_desc->cap_reg); + cfg.vtd_cfg.ecap_reg = le64_to_cpu(vtd_desc->ecap_reg); + fmt = INTEL_IOMMU; + break; + } default: dev_warn(vdev->dev, "unsupported page table format 0x%x\n", le16_to_cpu(desc->format)); diff --git a/include/uapi/linux/virtio_iommu.h b/include/uapi/linux/virtio_iommu.h index 656be1f3d926..17e0d5fcdd54 100644 --- a/include/uapi/linux/virtio_iommu.h +++ b/include/uapi/linux/virtio_iommu.h @@ -139,6 +139,22 @@ struct virtio_iommu_req_attach_pgt_virt { struct virtio_iommu_req_tail tail; }; +/* Vt-d I/O Page Table Descriptor */ +struct virtio_iommu_req_attach_pgt_vtd { + struct virtio_iommu_req_head head; + __le32 domain; + __le32 endpoint; + __le32 flags; + __le16 format; + __u8 reserved[2]; + __le32 pasid; + __le64 pgd; + __le64 fl_flags; + __le32 addr_width; + __u8 reserved2[36]; + struct virtio_iommu_req_tail tail; +}; + #define VIRTIO_IOMMU_MAP_F_READ (1 << 0) #define VIRTIO_IOMMU_MAP_F_WRITE (1 << 1) #define VIRTIO_IOMMU_MAP_F_MMIO (1 << 2) @@ -224,6 +240,8 @@ struct virtio_iommu_probe_pasid_size { #define VIRTIO_IOMMU_FORMAT_PSTF_ARM_SMMU_V3 2 /* Virt I/O page table format */ #define VIRTIO_IOMMU_FORMAT_PGTF_VIRT 3 +/* VT-d I/O page table format */ +#define VIRTIO_IOMMU_FORMAT_PGTF_VTD 4 struct virtio_iommu_probe_table_format { struct virtio_iommu_probe_property head; @@ -231,6 +249,14 @@ struct virtio_iommu_probe_table_format { __u8 reserved[2]; }; +struct virtio_iommu_probe_pgt_vtd { + struct virtio_iommu_probe_property head; + __le16 format; + __u8 reserved[2]; + __le64 cap_reg; + __le64 ecap_reg; +}; + struct virtio_iommu_req_probe { struct virtio_iommu_req_head head; __le32 endpoint;