From patchwork Sat Mar 20 15:19:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Peter X-Patchwork-Id: 12152523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72EC8C433C1 for ; Sat, 20 Mar 2021 15:21:22 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E2F7261965 for ; Sat, 20 Mar 2021 15:21:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E2F7261965 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=svenpeter.dev Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:Reply-To:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-ID: Subject:Cc:From:To:Date:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Te5XvYYOB6J1QBGzdUIYHN4JGkJRbSaI3v+2TsySE5Q=; b=dBJIvLsC2pKziVkxQCPSz03hw tQz8YahAhgYEo92FLbOlEzwdw5nGUFlvH81dg3V6d3GpIzZ1sGVJHE+Rxlq8oswjtfpoLHy8jB3h1 Bc31RiZN0KGfUkPzPzoRyL2lfrNhl5gIQFgCInBBNr6ZOCt5J5BCVAP4gEnUdTjc/LtuIJaji1HeN IbAkrWNHulbqH5OEfB+ejAzvNjjVV1RY4qThT4MNdEzTrMDfGQB7ByYvkfZmPEtVZm2qGq/kfGXls LZFd8DfV2v5INDr8yDxLsdG/3aBHtFDoO8N8gHdKl49EVETMBGrhYCRlaaynxiVLEpbcGmP5QhllD it/Ik+OOg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lNdOX-008yIC-Dk; Sat, 20 Mar 2021 15:20:05 +0000 Received: from mail-40134.protonmail.ch ([185.70.40.134]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lNdOS-008yHb-Eu for linux-arm-kernel@lists.infradead.org; Sat, 20 Mar 2021 15:20:03 +0000 Date: Sat, 20 Mar 2021 15:19:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svenpeter.dev; s=protonmail3; t=1616253599; bh=UmBJ19FRNdQXRczXhAbcIfSvSxmkFnUO3Bq5JKvNzm4=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=fsOp8RQJauPiKI+aSzOXutFeZjwRHtDBIoMZCO44lpdoyPTgn38/PDhPAwXA9C4Eo qG13iUY5rRpS2WrrF4Z/67tO1l7tTZo6qR+6gJ/Ju2OG35nS5Ftt7dr9Rjxx647uLl eKue8xA3PtyNAi5seCk6ZGbdxbv+d9CdVl+OmMTlnr9Q0H4mhv6O3W1/AerEXroMRT +tXFhO/FXdC5YQdnBejMvFR+ZwMcE2+20nUgUDsbRF2WRXC8ZIi/UBlnVsaVVY5gpF MLpH5AILG+0axnQtDjgF7feZiGkA9qfILeWJJcoVgset9eC3HVz3GIF84ThxWV7OOI UzrJ19V3e8Prg== To: iommu@lists.linux-foundation.org From: Sven Peter Cc: Joerg Roedel , Will Deacon , Robin Murphy , Rob Herring , Arnd Bergmann , Hector Martin , Mark Kettenis , Marc Zyngier , Mohamed Mediouni , Stan Skowronek , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Sven Peter Subject: [PATCH 1/3] iommu: io-pgtable: add DART pagetable format Message-ID: <20210320151903.60759-2-sven@svenpeter.dev> In-Reply-To: <20210320151903.60759-1-sven@svenpeter.dev> References: <20210320151903.60759-1-sven@svenpeter.dev> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210320_152000_633385_1AE81BD5 X-CRM114-Status: GOOD ( 12.93 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sven Peter Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Apple's DART iommu uses a pagetable format that's very similar to the ones already implemented by io-pgtable.c. Add a new format variant to support the required differences. Signed-off-by: Sven Peter --- drivers/iommu/Kconfig | 13 +++++++ drivers/iommu/io-pgtable-arm.c | 70 ++++++++++++++++++++++++++++++++++ drivers/iommu/io-pgtable.c | 3 ++ include/linux/io-pgtable.h | 6 +++ 4 files changed, 92 insertions(+) -- 2.25.1 diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 192ef8f61310..3c95c8524abe 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -39,6 +39,19 @@ config IOMMU_IO_PGTABLE_LPAE sizes at both stage-1 and stage-2, as well as address spaces up to 48-bits in size. +config IOMMU_IO_PGTABLE_APPLE_DART + bool "Apple DART Descriptor Format" + select IOMMU_IO_PGTABLE + select IOMMU_IO_PGTABLE_LPAE + depends on ARM64 || (COMPILE_TEST && !GENERIC_ATOMIC64) + help + Enable support for the Apple DART iommu pagetable format. + This format is a variant of the ARMv7/v8 Long Descriptor + Format specific to Apple's iommu found in their SoCs. + + Say Y here if you have a Apple SoC like the M1 which + contains DART iommus. + config IOMMU_IO_PGTABLE_LPAE_SELFTEST bool "LPAE selftests" depends on IOMMU_IO_PGTABLE_LPAE diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 87def58e79b5..18674469313d 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -127,6 +127,10 @@ #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL +/* APPLE_DART_PTE_PROT_NO_WRITE actually maps to ARM_LPAE_PTE_AP_RDONLY */ +#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 +385,17 @@ static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, { arm_lpae_iopte pte; +#ifdef CONFIG_IOMMU_IO_PGTABLE_APPLE_DART + 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; + } +#endif + if (data->iop.fmt == ARM_64_LPAE_S1 || data->iop.fmt == ARM_32_LPAE_S1) { pte = ARM_LPAE_PTE_nG; @@ -1043,6 +1058,54 @@ arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) return NULL; } +#ifdef CONFIG_IOMMU_IO_PGTABLE_APPLE_DART +static struct io_pgtable * +apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) +{ + struct arm_lpae_io_pgtable *data; + + if (cfg->ias > 38) + return NULL; + if (cfg->oas > 36) + return NULL; + + if (!cfg->coherent_walk) + return NULL; + + cfg->pgsize_bitmap &= SZ_16K; + if (!cfg->pgsize_bitmap) + return NULL; + + data = arm_lpae_alloc_pgtable(cfg); + if (!data) + return NULL; + + /* + * the hardware only supports this specific three level pagetable layout with + * the first level being encoded into four hardware registers + */ + data->start_level = ARM_LPAE_MAX_LEVELS - 2; + data->pgd_bits = 13; + data->bits_per_level = 11; + + data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, + cfg); + if (!data->pgd) + goto out_free_data; + + cfg->apple_dart_cfg.pgd[0] = virt_to_phys(data->pgd); + cfg->apple_dart_cfg.pgd[1] = virt_to_phys(data->pgd + 0x4000); + cfg->apple_dart_cfg.pgd[2] = virt_to_phys(data->pgd + 0x8000); + cfg->apple_dart_cfg.pgd[3] = virt_to_phys(data->pgd + 0xc000); + + return &data->iop; + +out_free_data: + kfree(data); + return NULL; +} +#endif + 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 +1131,13 @@ struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { .free = arm_lpae_free_pgtable, }; +#ifdef CONFIG_IOMMU_IO_PGTABLE_APPLE_DART +struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = { + .alloc = apple_dart_alloc_pgtable, + .free = arm_lpae_free_pgtable, +}; +#endif + #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..d86590b0673a 100644 --- a/drivers/iommu/io-pgtable.c +++ b/drivers/iommu/io-pgtable.c @@ -27,6 +27,9 @@ io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = { #ifdef CONFIG_AMD_IOMMU [AMD_IOMMU_V1] = &io_pgtable_amd_iommu_v1_init_fns, #endif +#ifdef CONFIG_IOMMU_IO_PGTABLE_APPLE_DART + [ARM_APPLE_DART] = &io_pgtable_apple_dart_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 a4c9ca2c31f1..19d9b631d319 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,10 @@ struct io_pgtable_cfg { u64 transtab; u64 memattr; } arm_mali_lpae_cfg; + + struct { + u64 pgd[4]; + } apple_dart_cfg; }; }; @@ -250,5 +255,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 */