From patchwork Wed Jan 22 18:58:50 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Cartwright X-Patchwork-Id: 3524591 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 911709F2D6 for ; Wed, 22 Jan 2014 19:03:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5050920155 for ; Wed, 22 Jan 2014 19:03:54 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 34D2E20154 for ; Wed, 22 Jan 2014 19:03:53 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1W634l-00075N-39; Wed, 22 Jan 2014 19:02:59 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1W634X-00007F-9u; Wed, 22 Jan 2014 19:02:45 +0000 Received: from smtp.codeaurora.org ([198.145.11.231]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1W6344-0008Ud-04 for linux-arm-kernel@lists.infradead.org; Wed, 22 Jan 2014 19:02:18 +0000 Received: from smtp.codeaurora.org (localhost [127.0.0.1]) by smtp.codeaurora.org (Postfix) with ESMTP id 5E90313F115; Wed, 22 Jan 2014 19:01:56 +0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 486) id 51DAA13F25A; Wed, 22 Jan 2014 19:01:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from joshc.qualcomm.com (rrcs-67-52-129-61.west.biz.rr.com [67.52.129.61]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: joshc@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 9306F13F115; Wed, 22 Jan 2014 19:01:53 +0000 (UTC) Received: by joshc.qualcomm.com (Postfix, from userid 1000) id 78D4D61400; Wed, 22 Jan 2014 12:58:54 -0600 (CST) From: Josh Cartwright To: Grant Likely , Rob Herring Subject: [PATCH RFC 2/4] drivers: of: implement reserved-memory handling for dma Date: Wed, 22 Jan 2014 12:58:50 -0600 Message-Id: <1390417133-6650-3-git-send-email-joshc@codeaurora.org> X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1390417133-6650-1-git-send-email-joshc@codeaurora.org> References: <1390417133-6650-1-git-send-email-joshc@codeaurora.org> X-Virus-Scanned: ClamAV using ClamSMTP X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140122_140216_388231_8CAF4146 X-CRM114-Status: GOOD ( 21.07 ) X-Spam-Score: -2.5 (--) Cc: devicetree@vger.kernel.org, Laura Abbott , Benjamin Herrenschmidt , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Marek Szyprowski X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add support for handling 'shared-dma-pool' reserved-memory nodes. Cc: Benjamin Herrenschmidt Cc: Laura Abbott Cc: Marek Szyprowski Signed-off-by: Josh Cartwright --- drivers/of/Kconfig | 7 ++ drivers/of/Makefile | 1 + drivers/of/of_reserved_mem_dma.c | 178 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 drivers/of/of_reserved_mem_dma.c diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index aba13df..ca58fb3e 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig @@ -81,4 +81,11 @@ config OF_RESERVED_MEM help Helpers to allow for reservation of memory regions +config OF_RESERVED_MEM_DMA + depends on OF_RESERVED_MEM + depends on DMA_CMA || HAVE_GENERIC_DMA_COHERENT + def_bool y + help + Helpers for reserving memory regions for DMA use + endmenu # OF diff --git a/drivers/of/Makefile b/drivers/of/Makefile index ed9660a..6142227 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile @@ -10,3 +10,4 @@ obj-$(CONFIG_OF_PCI) += of_pci.o obj-$(CONFIG_OF_PCI_IRQ) += of_pci_irq.o obj-$(CONFIG_OF_MTD) += of_mtd.o obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o +obj-$(CONFIG_OF_RESERVED_MEM_DMA) += of_reserved_mem_dma.o diff --git a/drivers/of/of_reserved_mem_dma.c b/drivers/of/of_reserved_mem_dma.c new file mode 100644 index 0000000..e24f02d --- /dev/null +++ b/drivers/of/of_reserved_mem_dma.c @@ -0,0 +1,178 @@ +/* + * Device tree based initialization code for DMA reserved regions. + * + * Copyright (c) 2013, The Linux Foundation. All Rights Reserved. + * Copyright (c) 2013 Samsung Electronics Co., Ltd. + * http://www.samsung.com + * Author: Marek Szyprowski + * Author: Josh Cartwright + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License or (at your optional) any later version of the license. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void rmem_dma_device_init(struct reserved_mem *rmem, + struct platform_device *pdev, + struct of_phandle_args *args) +{ + dma_declare_coherent_memory(&pdev->dev, rmem->base, rmem->base, + rmem->size, DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE); +} + +static void rmem_dma_device_release(struct reserved_mem *rmem, + struct platform_device *pdev) +{ + dma_release_declared_memory(&pdev->dev); +} + +static const struct reserved_mem_ops rmem_dma_ops = { + .device_init = rmem_dma_device_init, + .device_release = rmem_dma_device_release, +}; + +static int __init rmem_dma_get_reg(unsigned long node, const char *uname, + phys_addr_t *base, phys_addr_t *size) +{ + unsigned long len; + __be32 *prop; + + prop = of_get_flat_dt_prop(node, "reg", &len); + if (!prop) + return -EINVAL; + + if (len < (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32)) { + pr_err("Malformed 'shared-dma-pool' reserved-mem node '%s'.\n", + uname); + return -EINVAL; + } + + *base = dt_mem_next_cell(dt_root_addr_cells, &prop); + *size = dt_mem_next_cell(dt_root_size_cells, &prop); + return 0; +} + +static int __init rmem_dma_get_size(unsigned long node, const char *uname, + phys_addr_t *size) +{ + unsigned long len; + __be32 *prop; + + prop = of_get_flat_dt_prop(node, "size", &len); + if (!prop) + return -EINVAL; + + if (len < dt_root_size_cells * sizeof(__be32)) { + pr_err("Malformed 'shared-dma-pool' reserved-mem node '%s'.\n", + uname); + return -EINVAL; + } + + *size = dt_mem_next_cell(dt_root_size_cells, &prop); + return 0; +} + +static void rmem_cma_device_init(struct reserved_mem *rmem, + struct platform_device *pdev, + struct of_phandle_args *args) +{ + dev_set_cma_area(&pdev->dev, rmem->cma); +} + +static const struct reserved_mem_ops rmem_cma_ops = { + .device_init = rmem_cma_device_init, +}; + +static int __init rmem_cma_reserve_area(struct reserved_mem *rmem, + unsigned long node, const char *uname) +{ + int err; + + err = dma_contiguous_reserve_area(rmem->size, rmem->base, 0, + &rmem->cma); + if (err) { + pr_err("Unable to setup CMA reserved-mem region for '%s'.\n", + uname); + return err; + } + + if (of_get_flat_dt_prop(node, "linux,cma-default", NULL)) + dma_contiguous_set_default(rmem->cma); + + rmem->ops = &rmem_cma_ops; + return 0; +} + +static int __init rmem_cma_setup(struct reserved_mem *rmem, + unsigned long node, + const char *uname) +{ + int err; + + err = rmem_dma_get_reg(node, uname, &rmem->base, &rmem->size); + if (!err) + goto out_done; + + rmem->base = 0; + err = rmem_dma_get_size(node, uname, &rmem->size); + if (err) + goto out_err; + +out_done: + return rmem_cma_reserve_area(rmem, node, uname); + +out_err: + pr_err("Malformed 'shared-dma-pool' node '%s'.\n", uname); + return err; +} + +static int __init rmem_dma_excl_setup(struct reserved_mem *rmem, + unsigned long node, + const char *uname) +{ + int err; + + err = rmem_dma_get_reg(node, uname, &rmem->base, &rmem->size); + if (!err) + goto out_done; + + err = rmem_dma_get_size(node, uname, &rmem->size); + if (err) + goto out_err; + + rmem->base = memblock_alloc_base(rmem->size, PAGE_SIZE, + MEMBLOCK_ALLOC_ANYWHERE); + memblock_free(rmem->base, rmem->size); + +out_done: + rmem->ops = &rmem_dma_ops; + return memblock_remove(rmem->base, rmem->size); + +out_err: + pr_err("Unable to setup reserved-mem region '%s' for DMA.\n", uname); + return err; +} + +static int __init rmem_dma_setup(struct reserved_mem *rmem, + unsigned long node, + const char *uname) +{ + if (IS_ENABLED(CONFIG_DMA_CMA) && + of_get_flat_dt_prop(node, "reusable", NULL)) + return rmem_cma_setup(rmem, node, uname); + + return rmem_dma_excl_setup(rmem, node, uname); +} +RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);