From patchwork Fri Feb 21 12:25:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 25106 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f71.google.com (mail-pa0-f71.google.com [209.85.220.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 99C0020143 for ; Fri, 21 Feb 2014 12:26:50 +0000 (UTC) Received: by mail-pa0-f71.google.com with SMTP id kp14sf8787759pab.10 for ; Fri, 21 Feb 2014 04:26:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=hwuf33YyL8sfTsRwm2+zdmyQbIEIYCnZmimajTQvC9Q=; b=SCqxuQAr2UChgdIQzlQjmvqqrpIegsA+xRfaew8Azj0hz3TBiCe7sOgr8otlbUSK1u sM2yYl9kOf1ydmt33HMsMRC/+kzKUt5R79MNCl93dUOBT9OaIB/pgvMBfK+pVZ9c+9W2 riQwZm7tWDSbxFhfJX8tdHIvF/CymNhfz01o+0AEGAvOB56jsov5j0PFXa7jZxZy+kFT 7vxY/GlE0aAtlJhBJhIEhr/OruOn2s4gQ6xpPhPmDcfmM1p5MBIsAz9rbI3sVNiC7RW+ jnEE5alKtrF5HbnyDfccQZW6OMAsRkMETJBPIMst9fVKYlmzUR/HXR7Nw6ktJXpB/m6t EP6A== X-Gm-Message-State: ALoCoQn44O65POQME+S/Ccnqi4F0S08WAb/E5iMSKNyBOLvG6PMfo1+ZrbFgk9pPK8BNaw8b5woO X-Received: by 10.66.144.228 with SMTP id sp4mr3322363pab.5.1392985609622; Fri, 21 Feb 2014 04:26:49 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.23.80 with SMTP id 74ls994145qgo.61.gmail; Fri, 21 Feb 2014 04:26:49 -0800 (PST) X-Received: by 10.221.22.71 with SMTP id qv7mr4644623vcb.34.1392985609457; Fri, 21 Feb 2014 04:26:49 -0800 (PST) Received: from mail-ve0-f171.google.com (mail-ve0-f171.google.com [209.85.128.171]) by mx.google.com with ESMTPS id xn9si2922114vcb.41.2014.02.21.04.26.49 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 21 Feb 2014 04:26:49 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.171; Received: by mail-ve0-f171.google.com with SMTP id pa12so3166976veb.16 for ; Fri, 21 Feb 2014 04:26:49 -0800 (PST) X-Received: by 10.52.61.168 with SMTP id q8mr3847378vdr.40.1392985609363; Fri, 21 Feb 2014 04:26:49 -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 u4csp30049vcz; Fri, 21 Feb 2014 04:26:48 -0800 (PST) X-Received: by 10.66.217.169 with SMTP id oz9mr321094pac.20.1392985608471; Fri, 21 Feb 2014 04:26:48 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id oq4si7016556pbc.317.2014.02.21.04.26.47; Fri, 21 Feb 2014 04:26:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932108AbaBUM0X (ORCPT + 26 others); Fri, 21 Feb 2014 07:26:23 -0500 Received: from mailout2.w1.samsung.com ([210.118.77.12]:52959 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755421AbaBUM0S (ORCPT ); Fri, 21 Feb 2014 07:26:18 -0500 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout2.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N1C00EGHIJOYH90@mailout2.w1.samsung.com>; Fri, 21 Feb 2014 12:26:12 +0000 (GMT) X-AuditID: cbfec7f4-b7f796d000005a13-b7-530745e8e479 Received: from eusync3.samsung.com ( [203.254.199.213]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 4C.31.23059.8E547035; Fri, 21 Feb 2014 12:26:16 +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:16 +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 Cc: Marek Szyprowski , Benjamin Herrenschmidt , Arnd Bergmann , Michal Nazarewicz , Grant Likely , Tomasz Figa , Sascha Hauer , Laura Abbott , Rob Herring , Olof Johansson , Pawel Moll , Mark Rutland , Stephen Warren , Ian Campbell , Tomasz Figa , Kumar Gala , Nishanth Peethambaran , Marc , Josh Cartwright , Catalin Marinas , Will Deacon , Paul Mackerras Subject: [PATCH v5 08/11] drivers: dma-contiguous: add initialization from device tree Date: Fri, 21 Feb 2014 13:25:24 +0100 Message-id: <1392985527-6260-9-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: H4sIAAAAAAAAAyWRW0ySYRiA+35+/v+XRfsjqm/OXFFW1jwtL76c62jrq9Vm2kU1t0IltIEa pKU3MQSneIDoQmoeINELhppUnlZLGFM76zS1DA85lwfSSteWgUvy7nmf99nei5fhCIxkMJOZ dUuiyBLLRBSPfLPa8zFi5hSdHL3iP4z8xm4a/VBrOWixoQygWvd7LtJ7LVzU5WsHyFteTKKO pgWA3qm9NGorMdFoeXCSQI6pIS6yFFlJNNBZRaFGt4dGj2edBKof7ieQuUfDRb7KUPR6eIlC dncdjQyVjSTSvnDTyN/mINGcs4hEzaZ5Etk6fQDN/uwlj4Vge40d4L8rRoDn7xsAHqgoJ3DH Qw+Ny7waLnbYSij8Zeg5hZ9Y7+LR1SkOHuxVc3GTb47Aen80ru6sALjiqQ3gJUdo4uYrvPh0 iSwzT6KIOnKNl1E/MkHnmCLuFL4yAxXQhOlAEAPZWDhtUXHXeRvsG2umdIDHCNh6APsmtWB9 KCWgRjtKByqKjYG67zoqwELWAuBSDRmIOOw4BYv7y4jAYgt7Cf5xWf5HJBsGzQ/MZID57GlY 61lZ88zauZ2wyhgf0EEshm+7vtEBLVhLmopvGADfDDbYwFZJblqOMlUqj4lUiuXK3CxpZFq2 3AHW/7jcDuq641yAZYBoIz8HUskCrjhPmS93AchwREK+M4pOFvDTxfkFEkX2VUWuTKJ0AYIJ ClaBE2drq89e9Bzde7AlTmNPPNNaKr23S9ggy6+Z/vp7Ts4kUudv56l3qMa0KcE3Ez63OOGE ff8+aWyBQR/eGn5Z37pJPjN+8pGpJHW6Z09EyKeEzO0t0sXrhZILYy/7kgSHin6huIVeIe/c 8W778IdIq8draUNJnpFn1mUiZbeBEJHKDHHMAY5CKf4HUQ8J5qUCAAA= Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.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.128.171 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 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Refactor internal dma_contiguous_init_reserved_mem() function, which creates CMA area from previously reserved memory region and add support for handling 'shared-dma-pool' reserved-memory device tree nodes. Based on previous code provided by Josh Cartwright Signed-off-by: Marek Szyprowski --- drivers/base/dma-contiguous.c | 130 ++++++++++++++++++++++++++++++++++------- 1 file changed, 108 insertions(+), 22 deletions(-) diff --git a/drivers/base/dma-contiguous.c b/drivers/base/dma-contiguous.c index 165c2c299e57..5dfcc3743d9b 100644 --- a/drivers/base/dma-contiguous.c +++ b/drivers/base/dma-contiguous.c @@ -182,6 +182,49 @@ static int __init cma_init_reserved_areas(void) core_initcall(cma_init_reserved_areas); /** + * dma_contiguous_init_reserved_mem() - reserve custom contiguous area + * @size: Size of the reserved area (in bytes), + * @base: Base address of the reserved area optional, use 0 for any + * @limit: End address of the reserved memory (optional, 0 for any). + * @res_cma: Pointer to store the created cma region. + * + * This function reserves memory from early allocator. It should be + * called by arch specific code once the early allocator (memblock or bootmem) + * has been activated and all other subsystems have already allocated/reserved + * memory. This function allows to create custom reserved areas for specific + * devices. + */ +static int __init dma_contiguous_init_reserved_mem(phys_addr_t size, + phys_addr_t base, struct cma **res_cma) +{ + struct cma *cma = &cma_areas[cma_area_count]; + phys_addr_t alignment; + + /* Sanity checks */ + if (cma_area_count == ARRAY_SIZE(cma_areas)) { + pr_err("Not enough slots for CMA reserved regions!\n"); + return -ENOSPC; + } + + if (!size || !memblock_is_region_reserved(base, size)) + return -EINVAL; + + /* Sanitise input arguments */ + alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order); + if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size) + return -EINVAL; + + cma->base_pfn = PFN_DOWN(base); + cma->count = size >> PAGE_SHIFT; + *res_cma = cma; + cma_area_count++; + + /* Architecture specific contiguous memory fixup. */ + dma_contiguous_early_fixup(base, size); + return 0; +} + +/** * dma_contiguous_reserve_area() - reserve custom contiguous area * @size: Size of the reserved area (in bytes), * @base: Base address of the reserved area optional, use 0 for any @@ -197,7 +240,6 @@ core_initcall(cma_init_reserved_areas); int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, phys_addr_t limit, struct cma **res_cma) { - struct cma *cma = &cma_areas[cma_area_count]; phys_addr_t alignment; int ret = 0; @@ -205,12 +247,6 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, (unsigned long)size, (unsigned long)base, (unsigned long)limit); - /* Sanity checks */ - if (cma_area_count == ARRAY_SIZE(cma_areas)) { - pr_err("Not enough slots for CMA reserved regions!\n"); - return -ENOSPC; - } - if (!size) return -EINVAL; @@ -241,21 +277,12 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, } } - /* - * Each reserved area must be initialised later, when more kernel - * subsystems (like slab allocator) are available. - */ - cma->base_pfn = PFN_DOWN(base); - cma->count = size >> PAGE_SHIFT; - *res_cma = cma; - cma_area_count++; - - pr_info("CMA: reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M, - (unsigned long)base); - - /* Architecture specific contiguous memory fixup. */ - dma_contiguous_early_fixup(base, size); - return 0; + ret = dma_contiguous_init_reserved_mem(size, base, res_cma); + if (ret == 0) { + pr_info("CMA: reserved %ld MiB at %08lx\n", + (unsigned long)size / SZ_1M, (unsigned long)base); + return 0; + } err: pr_err("CMA: failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); return ret; @@ -357,3 +384,62 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages, return true; } + +/* + * Support for reserved memory regions defined in device tree + */ +#ifdef CONFIG_OF_RESERVED_MEM +#include +#include +#include + +#undef pr_fmt +#define pr_fmt(fmt) fmt + +static void rmem_cma_device_init(struct reserved_mem *rmem, + struct device *dev, struct of_phandle_args *args) +{ + struct cma *cma = rmem->priv; + dev_set_cma_area(dev, cma); +} + +static const struct reserved_mem_ops rmem_cma_ops = { + .device_init = rmem_cma_device_init, +}; + +static int __init rmem_cma_setup(struct reserved_mem *rmem, + unsigned long node, + const char *uname) +{ + phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order); + phys_addr_t mask = align - 1; + struct cma *cma; + int err; + + if (!of_get_flat_dt_prop(node, "reusable", NULL)) + return -EINVAL; + + if ((rmem->base & mask) || (rmem->size & mask)) { + pr_err("Reserved memory: incorrect alignment of CMA region\n"); + return -EINVAL; + } + + err = dma_contiguous_init_reserved_mem(rmem->size, rmem->base, &cma); + if (err) { + pr_err("Reserved memory: unable to setup CMA region\n"); + return err; + } + + if (of_get_flat_dt_prop(node, "linux,cma-default", NULL)) + dma_contiguous_set_default(cma); + + rmem->ops = &rmem_cma_ops; + rmem->priv = cma; + + pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n", + &rmem->base, (unsigned long)rmem->size / SZ_1M); + + return 0; +} +RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup); +#endif