From patchwork Fri Feb 21 12:25:18 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 25105 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pb0-f70.google.com (mail-pb0-f70.google.com [209.85.160.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1E99520143 for ; Fri, 21 Feb 2014 12:26:47 +0000 (UTC) Received: by mail-pb0-f70.google.com with SMTP id rq2sf8324520pbb.5 for ; Fri, 21 Feb 2014 04:26:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=I0s077nuQyA3dhI49Vt4OD77EPPCx8SNucPKE/Ui2SM=; b=NM2IAQiPraYTDtf6X2R7nDy1aMTIqE8Ys2TvNRGhEjd1dzSsfpMmdUk6e4xti+5VIv Od3pAjgqw5322U1aP7DJU4PzN2LQN0wGdbp5oEUHVqJfGs57RlRpGZA2UVlJEYKOsEQ/ B+MOH2RC67rHFF+s36+lTZxdl6ae5YGdRZpyKkKPS0luQ8s7OfBoJ1+93NgiHIrqzJzP o9JMlmhVZxz17s29K5IRNmzzQlCXom8LF1PAbqyGVrCZate/p6ApTPbDMXJqbikjirhg hh9uDUqOoFQxeWAAXai86TXNixY5pWtbabQR/slSW3YSCoR4rHIWbdqDl4REy1bL4bgL aZfA== X-Gm-Message-State: ALoCoQmuOMGpkorODMxqetlNlJ48SCMcqwggY2doQYa82en0fWqGKpT/xzeYNfG+gk0FZZXiUbI0 X-Received: by 10.66.66.196 with SMTP id h4mr3237381pat.22.1392985606776; Fri, 21 Feb 2014 04:26:46 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.40.11 with SMTP id w11ls940851qgw.42.gmail; Fri, 21 Feb 2014 04:26:46 -0800 (PST) X-Received: by 10.221.29.196 with SMTP id rz4mr4664304vcb.8.1392985606624; Fri, 21 Feb 2014 04:26:46 -0800 (PST) Received: from mail-vc0-f175.google.com (mail-vc0-f175.google.com [209.85.220.175]) by mx.google.com with ESMTPS id wg4si2922476vcb.38.2014.02.21.04.26.46 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 21 Feb 2014 04:26:46 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.175 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.175; Received: by mail-vc0-f175.google.com with SMTP id ij19so3196523vcb.34 for ; Fri, 21 Feb 2014 04:26:46 -0800 (PST) X-Received: by 10.52.246.227 with SMTP id xz3mr3838226vdc.95.1392985606534; Fri, 21 Feb 2014 04:26:46 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.174.196 with SMTP id u4csp30046vcz; Fri, 21 Feb 2014 04:26:45 -0800 (PST) X-Received: by 10.14.194.193 with SMTP id m41mr8200829een.76.1392985605376; Fri, 21 Feb 2014 04:26:45 -0800 (PST) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id h9si15645333eex.196.2014.02.21.04.26.44 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 21 Feb 2014 04:26:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1WGpAT-0004GC-6N; Fri, 21 Feb 2014 12:25:25 +0000 Received: from mailout3.w1.samsung.com ([210.118.77.13]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1WGpA3-0004D7-Ot for linaro-mm-sig@lists.linaro.org; Fri, 21 Feb 2014 12:24:59 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout3.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N1C00EEMIJL7R90@mailout3.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Fri, 21 Feb 2014 12:26:10 +0000 (GMT) X-AuditID: cbfec7f4-b7f796d000005a13-87-530745e1dd11 Received: from eusync3.samsung.com ( [203.254.199.213]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 09.21.23059.1E547035; Fri, 21 Feb 2014 12:26:09 +0000 (GMT) Received: from amdc1339.mshome.net ([106.116.147.30]) by eusync3.samsung.com (Oracle Communications Messaging Server 7u4-23.01 (7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0N1C001ZAIJD5M60@eusync3.samsung.com>; Fri, 21 Feb 2014 12:26:09 +0000 (GMT) From: Marek Szyprowski To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linaro-mm-sig@lists.linaro.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org Date: Fri, 21 Feb 2014 13:25:18 +0100 Message-id: <1392985527-6260-3-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1392985527-6260-1-git-send-email-m.szyprowski@samsung.com> References: <1392985527-6260-1-git-send-email-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAAyWRa0hTYRzGe8/OzjmOBqdl8SohMspCTCeUvaTEiKT3Qx+SQUFBNXV5walt TtSixlCbS+dchFZqiho4dsmZeCvQNVQyb2kK3kKlRJ1mbVnotJx+e/6/5wfPhz/DERjIQCYl PUumSJemCSke2b/T++X0XCwtEc3mB6BtYw+N1jUFHPTjdTFAr5yDXFTqquWiLm8bQK4SLYna rWsADWhcNGotqqCRZ2yOQPaFcS6qLawn0WhHJYUszhkavVnqJlDDxAiBanrzuchbHoQ+Trgp ZHbW0chQbiFRwXsnjbZb7SRa7i4kka1ihUSmDi9ASz/7SPExbK42A7y1aQR45akB4FF9CYHb X8zQuNiVz8V2UxGFp8ffUbi5/hGe2lng4LE+DRdbvcsELt0W4aoOPcD6tyaA3fagq4du8GIS ZWkp2TJFxIU7vOTmsioyc1iU49FOU2rQdFIHGAayZ2CTR6QDfrvxKByetVE6wGMEbAOAzTYD vX88IaDJUUj5LIqNhLpV3V72Z2sBdFeTPonDfqWgdqSY8BWH2Ztw2f2Z9i2Q7AloU9/3YT57 GZa2NNL7w8Gw0hjjw34shp+6FvewYFexalMNgF8DDpjAEZkqIVMZnySPDFdK5UpVelJ4Qobc Dva/6GkDdT3nHYBlgPAgPxNSEgFXmq3MlTsAZDhCf353BC0R8BOluXkyRcZthSpNpnQAgvEL VIOwa39ebliMGrHWq5knnsdFDQTCh2UuMRMi/peh6tyYGk2ZZ/625HwwZqVGDal/TV4KXjw3 KN961mmTWKvW1h9flNyyKOLoJZMjIHqTfzwk9spZJ9sy6c3D0dN5dKj2e5KYvOeWfDM8aCuK H5K3205FrDbt6M2/r+f0g7thQlKZLI0M5SiU0v/GgNSZowIAAA== Cc: Mark Rutland , Benjamin Herrenschmidt , Tomasz Figa , Will Deacon , Tomasz Figa , Paul Mackerras , Arnd Bergmann , Josh Cartwright , Catalin Marinas , Grant Likely , Ian Campbell , Pawel Moll , Stephen Warren , Sascha Hauer , Michal Nazarewicz , Rob Herring , Kumar Gala , Olof Johansson Subject: [Linaro-mm-sig] [PATCH v5 02/11] drivers: of: add initialization code for static reserved memory X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: linaro-mm-sig-bounces@lists.linaro.org Sender: linaro-mm-sig-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: m.szyprowski@samsung.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.175 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 This patch adds support for static (defined by 'reg' property) reserved memory regions declared in device tree. Memory blocks can be reliably reserved only during early boot. This must happen before the whole memory management subsystem is initialized, because we need to ensure that the given contiguous blocks are not yet allocated by kernel. Also it must happen before kernel mappings for the whole low memory are created, to ensure that there will be no mappings (for reserved blocks). Typically, all this happens before device tree structures are unflattened, so we need to get reserved memory layout directly from fdt. Based on previous code provided by Josh Cartwright Signed-off-by: Marek Szyprowski Acked-by: Grant Likely --- drivers/of/fdt.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/of_fdt.h | 3 ++ 2 files changed, 128 insertions(+) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 758b4f8b30b7..12809e20ef71 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -440,6 +440,113 @@ struct boot_param_header *initial_boot_params; #ifdef CONFIG_OF_EARLY_FLATTREE /** + * res_mem_reserve_reg() - reserve all memory described in 'reg' property + */ +static int __init __reserved_mem_reserve_reg(unsigned long node, + const char *uname) +{ + int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); + phys_addr_t base, size; + unsigned long len; + __be32 *prop; + int nomap; + + prop = of_get_flat_dt_prop(node, "reg", &len); + if (!prop) + return -ENOENT; + + if (len && len % t_len != 0) { + pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n", + uname); + return -EINVAL; + } + + nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; + + while (len > 0) { + base = dt_mem_next_cell(dt_root_addr_cells, &prop); + size = dt_mem_next_cell(dt_root_size_cells, &prop); + + if (base && size && + early_init_dt_reserve_memory_arch(base, size, nomap) == 0) + pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n", + uname, &base, (unsigned long)size / SZ_1M); + else + pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n", + uname, &base, (unsigned long)size / SZ_1M); + + len -= t_len; + } + return 0; +} + +static int __reserved_mem_check_root(unsigned long node) +{ + __be32 *prop; + + prop = of_get_flat_dt_prop(node, "#size-cells", NULL); + if (prop && be32_to_cpup(prop) != dt_root_size_cells) + return -EINVAL; + + prop = of_get_flat_dt_prop(node, "#address-cells", NULL); + if (prop && be32_to_cpup(prop) != dt_root_addr_cells) + return -EINVAL; + + prop = of_get_flat_dt_prop(node, "ranges", NULL); + if (!prop) + return -EINVAL; + return 0; +} + +/** + * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory + */ +static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname, + int depth, void *data) +{ + static int found; + const char *status; + + if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) { + if (__reserved_mem_check_root(node) != 0) { + pr_err("Reserved memory: unsupported node format, ignoring\n"); + /* break scan */ + return 1; + } + found = 1; + /* scan next node */ + return 0; + } else if (!found) { + /* scan next node */ + return 0; + } else if (found && depth < 2) { + /* scanning of /reserved-memory has been finished */ + return 1; + } + + status = of_get_flat_dt_prop(node, "status", NULL); + if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0) + return 0; + + __reserved_mem_reserve_reg(node, uname); + + /* scan next node */ + return 0; +} + +/** + * early_init_fdt_scan_reserved_mem() - create reserved memory regions + * + * This function grabs memory from early allocator for device exclusive use + * defined in device tree structures. It should be called by arch specific code + * once the early allocator (i.e. memblock) has been fully activated. + */ +void __init early_init_fdt_scan_reserved_mem(void) +{ + of_scan_flat_dt(__fdt_scan_reserved_mem, NULL); +} + +/** * of_scan_flat_dt - scan flattened tree blob and call callback on each. * @it: callback function * @data: context data pointer @@ -856,6 +963,16 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) memblock_add(base, size); } +int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, + phys_addr_t size, bool nomap) +{ + if (memblock_is_region_reserved(base, size)) + return -EBUSY; + if (nomap) + return memblock_remove(base, size); + return memblock_reserve(base, size); +} + /* * called from unflatten_device_tree() to bootstrap devicetree itself * Architectures can override this definition if memblock isn't used @@ -864,6 +981,14 @@ void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align) { return __va(memblock_alloc(size, align)); } +#else +int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, + phys_addr_t size, bool nomap) +{ + pr_error("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n", + base, size, nomap ? " (nomap)" : ""); + return -ENOSYS; +} #endif bool __init early_init_dt_scan(void *params) diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h index 2b77058a7335..8610ad8d77d2 100644 --- a/include/linux/of_fdt.h +++ b/include/linux/of_fdt.h @@ -98,7 +98,10 @@ extern int early_init_dt_scan_chosen(unsigned long node, const char *uname, int depth, void *data); extern int early_init_dt_scan_memory(unsigned long node, const char *uname, int depth, void *data); +extern void early_init_fdt_scan_reserved_mem(void); extern void early_init_dt_add_memory_arch(u64 base, u64 size); +extern int early_init_dt_reserve_memory_arch(phys_addr_t base, phys_addr_t size, + bool no_map); extern void * early_init_dt_alloc_memory_arch(u64 size, u64 align); extern u64 dt_mem_next_cell(int s, __be32 **cellp);