Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp450427pxb; Thu, 9 Sep 2021 04:53:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQN4k+gjwHwkFjpWx5jgS1jNskWBV69RA0Hu84mrTTl/7Afseg4D+QyPlfyLasLBJ/q8AC X-Received: by 2002:a17:906:dfe6:: with SMTP id lc6mr3079884ejc.210.1631188406131; Thu, 09 Sep 2021 04:53:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631188406; cv=none; d=google.com; s=arc-20160816; b=PIM8Zrwu/wddzBuEzNbKxSbWJmwmpO+sedBGX5fZynfZLMFniW4ZVAEcm9xNn5RK+a +/qOxLhFXc+GMia6gRmT4Z8DWuZsP339uG1CwGEzI8pI0Jcc8yvdqz2WGCI+ywpuOSFD 6NL+/kZnPA57xH4fdblPSJiKP5YYr1UlSUzjLe/9LRDhFwnJP5BKQLQ5OSD9+xvkYEwK CYBhLzU6usVDTO2+KZjBrWnJEQ5RE1EanynkhVQ9FoxVOyc6n5+rkm6D2I4P1OgXyOUS zNtdW9MjVuXRhIxUxo50HQpz44UEjnKkgAmM9wEdgavC2bcdmWFthsA3DyMEzEBxyOjL hFBg== 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=exJN/UvBksmrbSWGDe0ZlbLlikLPRCu5f6oBbMNIxSc=; b=BdOnzdjTndRv0RSK0Zn1kxWLTQf5xxPN0+N1VkfqimK2q7gUlYCwdf119L+kfD2aD8 Fjtdttzu5EVeINNj4j+Dy0V7yxUCz4rq0/hsQpT26lvcSa79MvD6ufNE3Xp6gPJkBudC v20uCAbrWmgHYQIv4R1xrKwNt0aFcG69f7zTO2aRtBw4fdHC5SGevFTyoLd5JxT3Y43B +44kJh5wm25btZHOKbyYeiwjJN2vTtr5R/qAtzLj8LMiN6Av+Xc9v+MZJ8lBvi4j/LnO MAjlFLkR7rAxTFazcltXUoi9clzlH9YxH2vm+rNRIxL9snKlfjo94dT0KFefvD3ejjc6 BO0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YjRhElXZ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ee26si2009545edb.511.2021.09.09.04.53.01; Thu, 09 Sep 2021 04:53:26 -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=@kernel.org header.s=k20201202 header.b=YjRhElXZ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236342AbhIILvq (ORCPT + 99 others); Thu, 9 Sep 2021 07:51:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:55124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239991AbhIILs6 (ORCPT ); Thu, 9 Sep 2021 07:48:58 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 073D46128C; Thu, 9 Sep 2021 11:43:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631187816; bh=PnYxjrbgltERqg7UPAvISCB0M4sb2EXeLVbFFERB1xY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YjRhElXZFFnhlvoy/D3q8NCU3PbB3aREKwAVjIuv82ORfTEYWILoR2jwIKfQbsysx gZLswFYzO4h9y5oBMXwpi8L2aIdHfkG5Nq5D9omHThXrAAJ7t2iR/gmLaM9sfTci18 HKNDP0Y3wLBoAvVqK8wN7eJE+8m0FRmMMfFNgSfgP+malbDCTxBTadXSbcrzWoALc0 /lmNV6thzhT1tBs0XzSlbeMgfzW6Uobjb6ng+FEzDL869aY4LtunstiwPfKI7d9HuA dTIKBDIGC7SRamsLe9cXU484644wj+S0F0/vFqS+1rjZMLrdKN9ESITA4TJ6HUKg6t X2MsV2ZInt9UQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mikko Perttunen , Mian Yousaf Kaukab , Greg Kroah-Hartman , Sasha Levin Subject: [PATCH AUTOSEL 5.14 116/252] misc: sram: Only map reserved areas in Tegra SYSRAM Date: Thu, 9 Sep 2021 07:38:50 -0400 Message-Id: <20210909114106.141462-116-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909114106.141462-1-sashal@kernel.org> References: <20210909114106.141462-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mikko Perttunen [ Upstream commit fec29bf04994b478a43a7e60e6dd5ac1f7cb53ae ] On Tegra186 and later, a portion of the SYSRAM may be reserved for use by TZ. Non-TZ memory accesses to this portion, including speculative accesses, trigger SErrors that bring down the system. This does also happen in practice occasionally (due to speculative accesses). To fix the issue, add a flag to the SRAM driver to only map the device tree-specified reserved areas depending on a flag set based on the compatibility string. This would not affect non-Tegra systems that rely on the entire thing being memory mapped. If 64K pages are being used, we cannot exactly map the 4K regions that are placed in SYSRAM - ioremap code instead aligns to closest 64K pages. However, since in practice the non-accessible memory area is 64K aligned, these mappings do not overlap with the non-accessible memory area and things work out. Reviewed-by: Mian Yousaf Kaukab Signed-off-by: Mikko Perttunen Link: https://lore.kernel.org/r/20210715103423.1811101-1-mperttunen@nvidia.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/misc/sram.c | 103 +++++++++++++++++++++++++++++++------------- drivers/misc/sram.h | 9 ++++ 2 files changed, 82 insertions(+), 30 deletions(-) diff --git a/drivers/misc/sram.c b/drivers/misc/sram.c index 93638ae2753a..4c26b19f5154 100644 --- a/drivers/misc/sram.c +++ b/drivers/misc/sram.c @@ -97,7 +97,24 @@ static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block, struct sram_partition *part = &sram->partition[sram->partitions]; mutex_init(&part->lock); - part->base = sram->virt_base + block->start; + + if (sram->config && sram->config->map_only_reserved) { + void __iomem *virt_base; + + if (sram->no_memory_wc) + virt_base = devm_ioremap_resource(sram->dev, &block->res); + else + virt_base = devm_ioremap_resource_wc(sram->dev, &block->res); + + if (IS_ERR(virt_base)) { + dev_err(sram->dev, "could not map SRAM at %pr\n", &block->res); + return PTR_ERR(virt_base); + } + + part->base = virt_base; + } else { + part->base = sram->virt_base + block->start; + } if (block->pool) { ret = sram_add_pool(sram, block, start, part); @@ -198,6 +215,7 @@ static int sram_reserve_regions(struct sram_dev *sram, struct resource *res) block->start = child_res.start - res->start; block->size = resource_size(&child_res); + block->res = child_res; list_add_tail(&block->list, &reserve_list); if (of_find_property(child, "export", NULL)) @@ -295,15 +313,17 @@ static int sram_reserve_regions(struct sram_dev *sram, struct resource *res) */ cur_size = block->start - cur_start; - dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n", - cur_start, cur_start + cur_size); + if (sram->pool) { + dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n", + cur_start, cur_start + cur_size); - ret = gen_pool_add_virt(sram->pool, - (unsigned long)sram->virt_base + cur_start, - res->start + cur_start, cur_size, -1); - if (ret < 0) { - sram_free_partitions(sram); - goto err_chunks; + ret = gen_pool_add_virt(sram->pool, + (unsigned long)sram->virt_base + cur_start, + res->start + cur_start, cur_size, -1); + if (ret < 0) { + sram_free_partitions(sram); + goto err_chunks; + } } /* next allocation after this reserved block */ @@ -331,40 +351,63 @@ static int atmel_securam_wait(void) 10000, 500000); } +static const struct sram_config atmel_securam_config = { + .init = atmel_securam_wait, +}; + +/* + * SYSRAM contains areas that are not accessible by the + * kernel, such as the first 256K that is reserved for TZ. + * Accesses to those areas (including speculative accesses) + * trigger SErrors. As such we must map only the areas of + * SYSRAM specified in the device tree. + */ +static const struct sram_config tegra_sysram_config = { + .map_only_reserved = true, +}; + static const struct of_device_id sram_dt_ids[] = { { .compatible = "mmio-sram" }, - { .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait }, + { .compatible = "atmel,sama5d2-securam", .data = &atmel_securam_config }, + { .compatible = "nvidia,tegra186-sysram", .data = &tegra_sysram_config }, + { .compatible = "nvidia,tegra194-sysram", .data = &tegra_sysram_config }, {} }; static int sram_probe(struct platform_device *pdev) { + const struct sram_config *config; struct sram_dev *sram; int ret; struct resource *res; - int (*init_func)(void); + + config = of_device_get_match_data(&pdev->dev); sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL); if (!sram) return -ENOMEM; sram->dev = &pdev->dev; + sram->no_memory_wc = of_property_read_bool(pdev->dev.of_node, "no-memory-wc"); + sram->config = config; + + if (!config || !config->map_only_reserved) { + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (sram->no_memory_wc) + sram->virt_base = devm_ioremap_resource(&pdev->dev, res); + else + sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res); + if (IS_ERR(sram->virt_base)) { + dev_err(&pdev->dev, "could not map SRAM registers\n"); + return PTR_ERR(sram->virt_base); + } - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); - if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc")) - sram->virt_base = devm_ioremap_resource(&pdev->dev, res); - else - sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res); - if (IS_ERR(sram->virt_base)) { - dev_err(&pdev->dev, "could not map SRAM registers\n"); - return PTR_ERR(sram->virt_base); + sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY), + NUMA_NO_NODE, NULL); + if (IS_ERR(sram->pool)) + return PTR_ERR(sram->pool); } - sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY), - NUMA_NO_NODE, NULL); - if (IS_ERR(sram->pool)) - return PTR_ERR(sram->pool); - sram->clk = devm_clk_get(sram->dev, NULL); if (IS_ERR(sram->clk)) sram->clk = NULL; @@ -378,15 +421,15 @@ static int sram_probe(struct platform_device *pdev) platform_set_drvdata(pdev, sram); - init_func = of_device_get_match_data(&pdev->dev); - if (init_func) { - ret = init_func(); + if (config && config->init) { + ret = config->init(); if (ret) goto err_free_partitions; } - dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n", - gen_pool_size(sram->pool) / 1024, sram->virt_base); + if (sram->pool) + dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n", + gen_pool_size(sram->pool) / 1024, sram->virt_base); return 0; @@ -405,7 +448,7 @@ static int sram_remove(struct platform_device *pdev) sram_free_partitions(sram); - if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool)) + if (sram->pool && gen_pool_avail(sram->pool) < gen_pool_size(sram->pool)) dev_err(sram->dev, "removed while SRAM allocated\n"); if (sram->clk) diff --git a/drivers/misc/sram.h b/drivers/misc/sram.h index 9c1d21ff7347..d2058d8c8f1d 100644 --- a/drivers/misc/sram.h +++ b/drivers/misc/sram.h @@ -5,6 +5,11 @@ #ifndef __SRAM_H #define __SRAM_H +struct sram_config { + int (*init)(void); + bool map_only_reserved; +}; + struct sram_partition { void __iomem *base; @@ -15,8 +20,11 @@ struct sram_partition { }; struct sram_dev { + const struct sram_config *config; + struct device *dev; void __iomem *virt_base; + bool no_memory_wc; struct gen_pool *pool; struct clk *clk; @@ -29,6 +37,7 @@ struct sram_reserve { struct list_head list; u32 start; u32 size; + struct resource res; bool export; bool pool; bool protect_exec; -- 2.30.2