From patchwork Mon Jul 14 07:12:46 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 33554 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f71.google.com (mail-yh0-f71.google.com [209.85.213.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A523020CAD for ; Mon, 14 Jul 2014 07:33:53 +0000 (UTC) Received: by mail-yh0-f71.google.com with SMTP id 29sf11094394yhl.2 for ; Mon, 14 Jul 2014 00:33:53 -0700 (PDT) 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=axZEsJz2TMbYGALVWYHWJ1obtnxghdpYCW3+wwuSDFA=; b=mk872ff/sfUk5204vpSppntSPpWEj7uALEbym6fTtb4j4szrs1IS+pRGncBt13h3Bf JJ9WD8d/9rlf9UgFNNe5v2f3NeY11c+97MDLOm7NKiSzY2283TDJS1jD7WR0AV6FCTWE y7GGIMpMBVmeJmIBlIk/NY8QR/R6S3qjQdcqQYVb2zhBIQKFo87SzpuE4cLsZIZG3/k1 PyO9k8uUgSGhUKg3QDQrpXawNr6deNNitjyjA7yQGGhf5Rho2DZhgeDmBnBG8LaVouMG sV+weg/QNPSWXwq4MFJpHclT9tWPOgHZPCIfewryggTgQgvzauolDlBhW+TzbVLT6hFS DQ+A== X-Gm-Message-State: ALoCoQnZS5Y1z6uq9e8kMSQRiUFqKVdzCHjL5EDn/txrV8cn9fy43YtQJXU1h8gngB4f/kRfPAie X-Received: by 10.58.186.50 with SMTP id fh18mr7276735vec.34.1405323233502; Mon, 14 Jul 2014 00:33:53 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.48.112 with SMTP id n103ls874706qga.86.gmail; Mon, 14 Jul 2014 00:33:53 -0700 (PDT) X-Received: by 10.58.56.102 with SMTP id z6mr14147237vep.7.1405323233414; Mon, 14 Jul 2014 00:33:53 -0700 (PDT) Received: from mail-vc0-f175.google.com (mail-vc0-f175.google.com [209.85.220.175]) by mx.google.com with ESMTPS id tr2si5048132vdc.101.2014.07.14.00.33.53 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 14 Jul 2014 00:33:53 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.175 as permitted sender) client-ip=209.85.220.175; Received: by mail-vc0-f175.google.com with SMTP id hu12so1365134vcb.34 for ; Mon, 14 Jul 2014 00:33:53 -0700 (PDT) X-Received: by 10.220.187.134 with SMTP id cw6mr14681176vcb.3.1405323233332; Mon, 14 Jul 2014 00:33:53 -0700 (PDT) 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.221.37.5 with SMTP id tc5csp112928vcb; Mon, 14 Jul 2014 00:33:52 -0700 (PDT) X-Received: by 10.66.226.172 with SMTP id rt12mr5937491pac.101.1405323232251; Mon, 14 Jul 2014 00:33:52 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id wm6si8547651pab.102.2014.07.14.00.33.51; Mon, 14 Jul 2014 00:33:51 -0700 (PDT) Received-SPF: none (google.com: devicetree-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753413AbaGNHdr (ORCPT + 8 others); Mon, 14 Jul 2014 03:33:47 -0400 Received: from mailout3.w1.samsung.com ([210.118.77.13]:29175 "EHLO mailout3.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752724AbaGNHdn (ORCPT ); Mon, 14 Jul 2014 03:33:43 -0400 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 <0N8O00A1IYBSBG00@mailout3.w1.samsung.com>; Mon, 14 Jul 2014 08:33:29 +0100 (BST) X-AuditID: cbfec7f4-b7fac6d000006cfe-7c-53c387c92def Received: from eusync3.samsung.com ( [203.254.199.213]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id 91.84.27902.9C783C35; Mon, 14 Jul 2014 08:33:29 +0100 (BST) Received: from amdc1339.digital.local ([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 <0N8O00FFGYBLTX50@eusync3.samsung.com>; Mon, 14 Jul 2014 08:33:29 +0100 (BST) From: Marek Szyprowski To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Marek Szyprowski , Benjamin Herrenschmidt , linaro-mm-sig@lists.linaro.org, devicetree@vger.kernel.org, Arnd Bergmann , Michal Nazarewicz , Grant Likely , Tomasz Figa , Sascha Hauer , Laura Abbott , Nishanth Peethambaran , Marc , Josh Cartwright , Catalin Marinas , Will Deacon , Paul Mackerras , Jon Medhurst , Joonsoo Kim , "Aneesh Kumar K.V." , Andrew Morton Subject: [PATCH v2 4/4] drivers: dma-contiguous: add initialization from device tree Date: Mon, 14 Jul 2014 09:12:46 +0200 Message-id: <1405321966-28184-5-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.9.2 In-reply-to: <1405321966-28184-1-git-send-email-m.szyprowski@samsung.com> References: <1405321966-28184-1-git-send-email-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrNLMWRmVeSWpSXmKPExsVy+t/xq7on2w8HGzzdq2wxZ/0aNovHr+ex WPyddIzd4kNTK7PF+2U9jBbzj5xjtTjwZwejxcruZjaLneveMVqcbXrDbrG9cwa7xZcrD5ks Nj2+xmpxedccNou1R+6yW2x4eZDJYsHxFlaLP9PlLNYcWcxu8Xf7JhaL9TNes1gsnH+f3eLl xxMsDuIea+atYfT4/WsSo8flvl4mj52z7rJ79LxpYfXoenuFyePOtT1sHidm/GbxeHBoM4vH 5iX1Hrf/PWb2WPfnFZNH/18Dj7m7+hg9+rasYvT4vEkuQCCKyyYlNSezLLVI3y6BK+N9wwSW gpP6FdMePGRpYGxR72Lk5JAQMJH40PSbDcIWk7hwbz2QzcUhJLCUUeJLx3QWCKePSWJbbws7 SBWbgKFE19susA4RATeJf+sOgXUwCyxgkzj6/wcjSEJYIEzi4JMtzCA2i4CqxKVV7WDNvAIe EtfmXGKFWCcn8f/lCiYQm1PAU2Lj5G9gcSGgmsP79jBOYORdwMiwilE0tTS5oDgpPddQrzgx t7g0L10vOT93EyMkwr7sYFx8zOoQowAHoxIPb4XY4WAh1sSy4srcQ4wSHMxKIrzhbkAh3pTE yqrUovz4otKc1OJDjEwcnFINjFUlQSq1n0NuO3KcPcEm671UqPJdxo6tv/9OnWr9OyTswk+J SK3sZx0t67S+GpS/O/yqrXCqQCHLVoF/e76aHd93SLDD42KAxZ7u9RuWPe9U9dY+pvR/y4wv X1QCvZItHLf+id2YuW1WcUP/A85PyXGHeKeExZ6qT3tTuKiu2DuAs9chaceqpUosxRmJhlrM RcWJADLVCvGOAgAA Sender: devicetree-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: devicetree@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=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.175 as permitted sender) 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: , Add a function to create CMA region from previously reserved memory 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 | 60 +++++++++++++++++++++++++++++++++++++++++ include/linux/cma.h | 3 +++ mm/cma.c | 62 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 114 insertions(+), 11 deletions(-) diff --git a/drivers/base/dma-contiguous.c b/drivers/base/dma-contiguous.c index 6606abdf880c..0e480146fe05 100644 --- a/drivers/base/dma-contiguous.c +++ b/drivers/base/dma-contiguous.c @@ -211,3 +211,63 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages, { return cma_release(dev_get_cma_area(dev), pages, count); } + +/* + * 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 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) +{ + phys_addr_t align = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order); + phys_addr_t mask = align - 1; + unsigned long node = rmem->fdt_node; + struct cma *cma; + int err; + + if (!of_get_flat_dt_prop(node, "reusable", NULL) || + of_get_flat_dt_prop(node, "no-map", NULL)) + return -EINVAL; + + if ((rmem->base & mask) || (rmem->size & mask)) { + pr_err("Reserved memory: incorrect alignment of CMA region\n"); + return -EINVAL; + } + + err = cma_init_reserved_mem(rmem->base, rmem->size, 0, &cma); + if (err) { + pr_err("Reserved memory: unable to setup CMA region\n"); + return err; + } + /* Architecture specific contiguous memory fixup. */ + dma_contiguous_early_fixup(rmem->base, rmem->size); + + 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 diff --git a/include/linux/cma.h b/include/linux/cma.h index 32cab7a425f9..9a18a2b1934c 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -16,6 +16,9 @@ extern int __init cma_declare_contiguous(phys_addr_t size, phys_addr_t base, phys_addr_t limit, phys_addr_t alignment, unsigned int order_per_bit, bool fixed, struct cma **res_cma); +extern int cma_init_reserved_mem(phys_addr_t size, + phys_addr_t base, int order_per_bit, + struct cma **res_cma); extern struct page *cma_alloc(struct cma *cma, int count, unsigned int align); extern bool cma_release(struct cma *cma, struct page *pages, int count); #endif diff --git a/mm/cma.c b/mm/cma.c index 4b251b037e1b..b3d8b925ad34 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -140,6 +140,54 @@ static int __init cma_init_reserved_areas(void) core_initcall(cma_init_reserved_areas); /** + * cma_init_reserved_mem() - create custom contiguous area from reserved memory + * @base: Base address of the reserved area + * @size: Size of the reserved area (in bytes), + * @order_per_bit: Order of pages represented by one bit on bitmap. + * @res_cma: Pointer to store the created cma region. + * + * This function creates custom contiguous area from already reserved memory. + */ +int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size, + int order_per_bit, struct cma **res_cma) +{ + struct cma *cma; + 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; + + /* ensure minimal alignment requied by mm core */ + alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order); + + /* alignment should be aligned with order_per_bit */ + if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit)) + return -EINVAL; + + if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size) + return -EINVAL; + + /* + * Each reserved area must be initialised later, when more kernel + * subsystems (like slab allocator) are available. + */ + cma = &cma_areas[cma_area_count]; + cma->base_pfn = PFN_DOWN(base); + cma->count = size >> PAGE_SHIFT; + cma->order_per_bit = order_per_bit; + *res_cma = cma; + cma_area_count++; + + return 0; +} + +/** * cma_declare_contiguous() - reserve custom contiguous area * @base: Base address of the reserved area optional, use 0 for any * @size: Size of the reserved area (in bytes), @@ -162,7 +210,6 @@ int __init cma_declare_contiguous(phys_addr_t base, phys_addr_t alignment, unsigned int order_per_bit, bool fixed, struct cma **res_cma) { - struct cma *cma; int ret = 0; pr_debug("%s(size %lx, base %08lx, limit %08lx alignment %08lx)\n", @@ -214,16 +261,9 @@ int __init cma_declare_contiguous(phys_addr_t base, } } - /* - * Each reserved area must be initialised later, when more kernel - * subsystems (like slab allocator) are available. - */ - cma = &cma_areas[cma_area_count]; - cma->base_pfn = PFN_DOWN(base); - cma->count = size >> PAGE_SHIFT; - cma->order_per_bit = order_per_bit; - *res_cma = cma; - cma_area_count++; + ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma); + if (ret) + goto err; pr_info("Reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M, (unsigned long)base);