Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp103873pxj; Thu, 3 Jun 2021 01:54:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmJITm6xG+09Kgf1NrBOaMt6Fc3Zv743oq5DVqG9yP7Fu2QxJh8XmpKZjOhRL/VHVnbigG X-Received: by 2002:a05:6402:1d2e:: with SMTP id dh14mr10464513edb.374.1622710493544; Thu, 03 Jun 2021 01:54:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622710493; cv=none; d=google.com; s=arc-20160816; b=bRGjyQi3Xfjjp+n1Tu15tcHZ2hx0FVlzeBMY/uBxveFpJ2y1xCqSZGPxqmN/RODAxE Wj3XJj7QwNX1iQfHXUBXfGfr4LYC9wJtXOEIUy5Yq81n9wX0iAHsw31hJB5SPqj5qvqx kIRBbuVJwx37YAkyyKSMIu+adfUWFI3sUsbPSod3cZzQPK7HOfbu42yXYcFqHfWbejRY WV7EAJif4k5L0DoKQxJuxvcQARZ/XsayBRrw2ylLfSTsx6IlnLvGevcXSP/m5qtcGk7o irmrxbmX2ZxDw5RuMMuDZoGohjkCPRGT4AeqaXMxSgZOW9p/al4+ADz3Cqix/P0nYSjo 9Ylg== 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:dkim-signature; bh=6dgROPTG2FgZPQhdtR6kWJqhz8OvQLzcBN89UwKC3T4=; b=fQuTcH3bB06vJo46zlJTmSsZnbH8GlnESdc/OjECMoWd3/TX8/a3b4TDNVTCBT0zF7 NhdyCwbeZ9gtRV+uZqEj1mWCAoF3yZaSCCT18NRYKWMv8usLxmB0SwDi43FOJm9+pqY+ nrYXy2FlGBWNQ0WH7FTq5mrYtZejxagydfwP3EjUZxV45CoCAVS4ON22C1dGHNV61ryY OFJjrzZ5w7x57LZ1y4dA7J1KbHmDfqZ5I/4UZ/hdAq736qGYXiwn+GAY6yR/pZOWCO3q fYseUJf2n9enCVQdjq+waJiX3Q6FcMZW656oCEP65PSyX6/fjaL9TQFahdgRrNjLq9We RfZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@svenpeter.dev header.s=fm1 header.b=YZBoHdD6; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=Evd9CJBa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=svenpeter.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hs26si1851569ejc.307.2021.06.03.01.54.08; Thu, 03 Jun 2021 01:54:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@svenpeter.dev header.s=fm1 header.b=YZBoHdD6; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=Evd9CJBa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=svenpeter.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229794AbhFCIwP (ORCPT + 99 others); Thu, 3 Jun 2021 04:52:15 -0400 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:47549 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbhFCIwO (ORCPT ); Thu, 3 Jun 2021 04:52:14 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailnew.nyi.internal (Postfix) with ESMTP id BD5D7580EC3; Thu, 3 Jun 2021 04:50:29 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Thu, 03 Jun 2021 04:50:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svenpeter.dev; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=6dgROPTG2FgZP QhdtR6kWJqhz8OvQLzcBN89UwKC3T4=; b=YZBoHdD6jqHwLwcR1DxQCnGR6tUwM n4gKQYSAEIf703dsJ7+6lC1pai5tjKhBAKZYUQ6kj2rky5h1+zmDx05jqstt19at tYycYwQazeAHd7zqDX3PV/IPTH+LSWEbHliJEYp98wmgzrZqliHbrHfwtjBdJkJy bgjE086l5oUUsprEFeTXW+3HuF5/rCatU3MctFESh0gwIAg+ZTQEZ81XCSxQaflq P36V8yegkT9X8mNSTs+A6fDOQbLw9DMchsBwMSyhg8NHD8bOg9T50LsNc0XEIqAn mOA7UEYGGiVd3Cj+J4MCq8dv91J854WUGJI9zjNCmLghtUyfjheTB2AJw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=6dgROPTG2FgZPQhdtR6kWJqhz8OvQLzcBN89UwKC3T4=; b=Evd9CJBa 2a338kJdBJd6FeLK4iOlVhZOmY4Jo+WSiBZOmxbE6rq/hxPkDNXNODUBCJr4S5Iw LVglMwZNGN/IO5pABevTIgcgTVN7aBDyx8fIVrMX/IgJLSJ/qxurufNfKNsU60Yf O5U7PUMwINJO5py3uapXisFi3zEc9P5CZspCHdDXkBq75eLLs9citLh/HaMmaByo kl9t9/PnaEkFwGesbJbjmxmi0hPPYI2xlkjVV2psMvW8fW1jj9UNjBiVDimlAgj8 Qc4E3+FEmkLdnZoRd8d5xiSkebZfYiRzSF4dFHBsW8oVIond4JXVV7vYXnz4sieI 8/J6hifWlwlIWg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdelledgtdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefuvhgvnhcu rfgvthgvrhcuoehsvhgvnhesshhvvghnphgvthgvrhdruggvvheqnecuggftrfgrthhtvg hrnheptedvkeetleeuffffhfekteetffeggffgveehieelueefvddtueffveevlefhfeej necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepshhvvg hnsehsvhgvnhhpvghtvghrrdguvghv X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 3 Jun 2021 04:50:27 -0400 (EDT) From: Sven Peter To: Will Deacon , Robin Murphy , Joerg Roedel , Rob Herring Cc: Sven Peter , Arnd Bergmann , devicetree@vger.kernel.org, Hector Martin , linux-kernel@vger.kernel.org, Marc Zyngier , Mohamed Mediouni , Stan Skowronek , linux-arm-kernel@lists.infradead.org, Mark Kettenis , iommu@lists.linux-foundation.org, Alexander Graf Subject: [PATCH v3 1/3] iommu: io-pgtable: add DART pagetable format Date: Thu, 3 Jun 2021 10:50:01 +0200 Message-Id: <20210603085003.50465-2-sven@svenpeter.dev> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20210603085003.50465-1-sven@svenpeter.dev> References: <20210603085003.50465-1-sven@svenpeter.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Apple's DART iommu uses a pagetable format that shares some similarities with the ones already implemented by io-pgtable.c. Add a new format variant to support the required differences so that we don't have to duplicate the pagetable handling code. Signed-off-by: Sven Peter --- drivers/iommu/io-pgtable-arm.c | 62 ++++++++++++++++++++++++++++++++++ drivers/iommu/io-pgtable.c | 1 + include/linux/io-pgtable.h | 7 ++++ 3 files changed, 70 insertions(+) diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 87def58e79b5..1dd5c45b4b5b 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -127,6 +127,9 @@ #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL +#define APPLE_DART_PTE_PROT_NO_WRITE (1<<7) +#define APPLE_DART_PTE_PROT_NO_READ (1<<8) + /* IOPTE accessors */ #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) @@ -381,6 +384,15 @@ static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, { arm_lpae_iopte pte; + if (data->iop.fmt == ARM_APPLE_DART) { + pte = 0; + if (!(prot & IOMMU_WRITE)) + pte |= APPLE_DART_PTE_PROT_NO_WRITE; + if (!(prot & IOMMU_READ)) + pte |= APPLE_DART_PTE_PROT_NO_READ; + return pte; + } + if (data->iop.fmt == ARM_64_LPAE_S1 || data->iop.fmt == ARM_32_LPAE_S1) { pte = ARM_LPAE_PTE_nG; @@ -1043,6 +1055,51 @@ arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) return NULL; } +static struct io_pgtable * +apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) +{ + struct arm_lpae_io_pgtable *data; + int i; + + if (cfg->oas > 36) + return NULL; + + data = arm_lpae_alloc_pgtable(cfg); + if (!data) + return NULL; + + /* + * Apple's DART always requires three levels with the first level being + * stored in four MMIO registers. We always concatenate the first and + * second level so that we only have to setup the MMIO registers once. + * This results in an effective two level pagetable. + */ + if (data->start_level < 1) + return NULL; + if (data->start_level == 1 && data->pgd_bits > 2) + return NULL; + if (data->start_level > 1) + data->pgd_bits = 0; + data->start_level = 2; + cfg->apple_dart_cfg.n_ttbrs = 1 << data->pgd_bits; + data->pgd_bits += data->bits_per_level; + + data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, + cfg); + if (!data->pgd) + goto out_free_data; + + for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i) + cfg->apple_dart_cfg.ttbr[i] = + virt_to_phys(data->pgd + i * ARM_LPAE_GRANULE(data)); + + return &data->iop; + +out_free_data: + kfree(data); + return NULL; +} + struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { .alloc = arm_64_lpae_alloc_pgtable_s1, .free = arm_lpae_free_pgtable, @@ -1068,6 +1125,11 @@ struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { .free = arm_lpae_free_pgtable, }; +struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = { + .alloc = apple_dart_alloc_pgtable, + .free = arm_lpae_free_pgtable, +}; + #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST static struct io_pgtable_cfg *cfg_cookie __initdata; diff --git a/drivers/iommu/io-pgtable.c b/drivers/iommu/io-pgtable.c index 6e9917ce980f..fd8e6bd6caf9 100644 --- a/drivers/iommu/io-pgtable.c +++ b/drivers/iommu/io-pgtable.c @@ -20,6 +20,7 @@ io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = { [ARM_64_LPAE_S1] = &io_pgtable_arm_64_lpae_s1_init_fns, [ARM_64_LPAE_S2] = &io_pgtable_arm_64_lpae_s2_init_fns, [ARM_MALI_LPAE] = &io_pgtable_arm_mali_lpae_init_fns, + [ARM_APPLE_DART] = &io_pgtable_apple_dart_init_fns, #endif #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S [ARM_V7S] = &io_pgtable_arm_v7s_init_fns, diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h index 4d40dfa75b55..a4bfac7f85f7 100644 --- a/include/linux/io-pgtable.h +++ b/include/linux/io-pgtable.h @@ -16,6 +16,7 @@ enum io_pgtable_fmt { ARM_V7S, ARM_MALI_LPAE, AMD_IOMMU_V1, + ARM_APPLE_DART, IO_PGTABLE_NUM_FMTS, }; @@ -136,6 +137,11 @@ struct io_pgtable_cfg { u64 transtab; u64 memattr; } arm_mali_lpae_cfg; + + struct { + u64 ttbr[4]; + u32 n_ttbrs; + } apple_dart_cfg; }; }; @@ -246,5 +252,6 @@ extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns; extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns; extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns; extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns; +extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns; #endif /* __IO_PGTABLE_H */ -- 2.25.1