From patchwork Thu Apr 11 11:22:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 2428251 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) by patchwork1.kernel.org (Postfix) with ESMTP id 60A863FD8C for ; Thu, 11 Apr 2013 12:59:02 +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 1UQFeu-00009s-QN; Thu, 11 Apr 2013 11:27:21 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UQFdC-0006JI-FH; Thu, 11 Apr 2013 11:25:30 +0000 Received: from mailout3.samsung.com ([203.254.224.33]) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UQFc3-0006Dk-Nf for linux-arm-kernel@lists.infradead.org; Thu, 11 Apr 2013 11:24:27 +0000 Received: from epcpsbgm1.samsung.com (epcpsbgm1 [203.254.230.26]) by mailout3.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0ML300EKH90IVUY0@mailout3.samsung.com> for linux-arm-kernel@lists.infradead.org; Thu, 11 Apr 2013 20:24:18 +0900 (KST) X-AuditID: cbfee61a-b7fa86d0000045ae-3a-51669d6269c7 Received: from epmmp2 ( [203.254.227.17]) by epcpsbgm1.samsung.com (EPCPMTA) with SMTP id 2A.B8.17838.26D96615; Thu, 11 Apr 2013 20:24:18 +0900 (KST) Received: from localhost.localdomain ([106.116.147.30]) by mmp2.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0ML3003KM8X0PU50@mmp2.samsung.com>; Thu, 11 Apr 2013 20:24:18 +0900 (KST) From: Marek Szyprowski To: linux-arm-kernel@lists.infradead.org, linaro-mm-sig@lists.linaro.org, devicetree-discuss@lists.ozlabs.org Subject: [RFC/PATCH v2 2/2] drivers: dma-contiguous: add initialization from device tree Date: Thu, 11 Apr 2013 13:22:10 +0200 Message-id: <1365679330-2514-3-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1365679330-2514-1-git-send-email-m.szyprowski@samsung.com> References: <1365679330-2514-1-git-send-email-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrBLMWRmVeSWpSXmKPExsVy+t9jQd2kuWmBBnu2a1j8nXSM3eLA7Ies Fq/ObGSzONv0ht1ie+cMdosvVx4yWWx6fI3VYu2Ru+wWG14eZLJYcLyF1eLPdDmLU9c/s1l8 //aNzeLv9k0sFofftLNarJ/xmsVBwOP3r0mMHpf7epk8ds66y+6xeUm9x+1/j5k9zs9YyOhx 5UQTq8e6P6+YPPr/Gnj0bVnF6PHzpU4AdxSXTUpqTmZZapG+XQJXxsPvz5kL3tpV/Pz2i72B 8YVRFyMnh4SAicSC/22sELaYxIV769m6GLk4hASmM0qcm93GDuG0M0m8evcHrIpNwFCi620X UBUHh4hAvsTqWbwgYWaBNhaJFxtYQGxhgSiJKw9uMYGUsAioSkzcngsS5hVwl1jTdgIsLCGg IDFnkg2IySngIXFnhjNIhRBQxc5THYwTGHkXMDKsYhRNLUguKE5KzzXUK07MLS7NS9dLzs/d xAgO72dSOxhXNlgcYhTgYFTi4T3RkBooxJpYVlyZe4hRgoNZSYSXcXZaoBBvSmJlVWpRfnxR aU5q8SFGaQ4WJXHeA63WgUIC6YklqdmpqQWpRTBZJg5OqQbG2U2LHx5ICBN55Vun9q0pbEZT g0fFw4pfK1ktXn5l0/Xgjdp3pKc3ovA72+p65iNfbf3kqp7JBP+d+C743pw1zyteuj6uChe8 u1v0y75TR61KNVd//R3cEsDJoMNdO8HDZH17/oEZbTdyqhd9qPyi68y1x+ToGpPACUePTqtK PvWsYMP5uFt3lFiKMxINtZiLihMBGW/psGsCAAA= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130411_072420_329658_02650CCB X-CRM114-Status: GOOD ( 22.23 ) X-Spam-Score: -7.5 (-------) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-7.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high trust [203.254.224.33 listed in list.dnswl.org] 1.7 KHOP_BIG_TO_CC Sent to 10+ recipients instaed of Bcc or a list -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -2.4 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Laura Abbott , Arnd Bergmann , Tomasz Figa , Nishanth Peethambaran , Michal Nazarewicz , Grant Likely , Marc , Kyungmin Park , Sylwester Nawrocki , Olof Johansson , Sascha Hauer , 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 Add device tree support for contiguous memory regions defined in device tree. Initialization is done in 2 steps. First, the contiguous memory is reserved, what happens very early, when only flattened device tree is available. Then on device initialization the corresponding cma regions are assigned to device structures. Signed-off-by: Marek Szyprowski Acked-by: Kyungmin Park --- .../devicetree/bindings/contiguous-memory.txt | 69 +++++++++++ arch/arm/boot/dts/skeleton.dtsi | 8 +- drivers/base/dma-contiguous.c | 124 ++++++++++++++++++++ 3 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/contiguous-memory.txt diff --git a/Documentation/devicetree/bindings/contiguous-memory.txt b/Documentation/devicetree/bindings/contiguous-memory.txt new file mode 100644 index 0000000..fa598bf --- /dev/null +++ b/Documentation/devicetree/bindings/contiguous-memory.txt @@ -0,0 +1,69 @@ +* Contiguous memory binding + +The /chosen/contiguous-memory node provides runtime configuration of +contiguous memory regions for Linux kernel. One can create such regions +for special usage by various device drivers. A good example are +contiguous memory allocations or memory sharing with other operating +system on the same hardware board. Those special memory regions might +depend on the board configuration and devices used on the target system. + +Parameters for each memory region can be encoded into the device tree +wit the following convention: + +contiguous-memory { + + (name): region@(base-address) { + reg = <(baseaddr) (size)>; + (linux,default-contiguous-region); + device = <&device_0 &device_1 ...> + }; +}; + +name: an name given to the defined region. +base-address: the base address of the defined region. +size: the size of the memory region (bytes). +linux,default-contiguous-region: property indicating that the region + is the default region for all contiguous memory + allocations, Linux specific (optional) +device: array of phandles to the client device nodes, which use + the given contiguous region + +* Example: + +This example defines a memory configuration containing 2 contiguous +regions for Linux kernel, one default of all device drivers (named +contig_mem, placed at 0x72000000, 64MiB) and one dedicated to the +framebuffer device (named display_mem, placed at 0x78000000, 16MiB). The +display_mem region is then assigned to fb@12300000 device for contiguous +memory allocation with Linux kernel drivers. + +The reason for creating a separate region for framebuffer device is to +match the framebuffer address of from configuration done by bootloader, +so once Linux kernel drivers starts, no glitches on the displayed boot +logo appears. + +/ { + /* ... */ + + chosen { + bootargs = /* ... */ + + contiguous-memory { + contig_mem: region@72000000 { + reg = <0x72000000 0x4000000>; + linux,default-contiguous-region; + }; + + display_mem: region@78000000 { + reg = <0x78000000 0x1000000>; + device = <&fb0>; + }; + }; + }; + + /* ... */ + + fb0: fb@12300000 { + status = "okay"; + }; +}; diff --git a/arch/arm/boot/dts/skeleton.dtsi b/arch/arm/boot/dts/skeleton.dtsi index b41d241..538a868 100644 --- a/arch/arm/boot/dts/skeleton.dtsi +++ b/arch/arm/boot/dts/skeleton.dtsi @@ -7,7 +7,13 @@ / { #address-cells = <1>; #size-cells = <1>; - chosen { }; + chosen { + contiguous-memory { + #address-cells = <1>; + #size-cells = <1>; + }; + }; aliases { }; memory { device_type = "memory"; reg = <0 0>; }; }; + diff --git a/drivers/base/dma-contiguous.c b/drivers/base/dma-contiguous.c index 01fe743..6a8abab 100644 --- a/drivers/base/dma-contiguous.c +++ b/drivers/base/dma-contiguous.c @@ -24,6 +24,9 @@ #include #include +#include +#include +#include #include #include #include @@ -37,6 +40,7 @@ struct cma { unsigned long base_pfn; unsigned long count; unsigned long *bitmap; + char full_name[32]; }; static DEFINE_MUTEX(cma_mutex); @@ -133,6 +137,53 @@ static __init int cma_activate_area(struct cma *cma) return 0; } +/*****************************************************************************/ + +#ifdef CONFIG_OF +int __init cma_fdt_scan(unsigned long node, const char *uname, + int depth, void *data) +{ + static int level; + phys_addr_t base, size; + unsigned long len; + struct cma *cma; + __be32 *prop; + + if (depth == 1 && strcmp(uname, "chosen") == 0) { + level = depth; + return 0; + } + + if (depth == 2 && strcmp(uname, "contiguous-memory") == 0) { + level = depth; + return 0; + } + + if (level != 2 || depth != 3 || strncmp(uname, "region@", 7) != 0) + return 0; + + prop = of_get_flat_dt_prop(node, "reg", &len); + if (!prop || (len != 2 * sizeof(unsigned long))) + return 0; + + base = be32_to_cpu(prop[0]); + size = be32_to_cpu(prop[1]); + + pr_info("Found %s, memory base %lx, size %ld MiB\n", uname, + (unsigned long)base, (unsigned long)size / SZ_1M); + + dma_contiguous_reserve_area(size, base, 0, &cma); + + strcpy(cma->full_name, uname); + + if (of_get_flat_dt_prop(node, "linux,default-contiguous-region", NULL)) { + + dma_contiguous_default_area = cma; + } + return 0; +} +#endif + /** * dma_contiguous_reserve() - reserve area(s) for contiguous memory handling * @limit: End address of the reserved memory (optional, 0 for any). @@ -149,6 +200,10 @@ void __init dma_contiguous_reserve(phys_addr_t limit) pr_debug("%s(limit %08lx)\n", __func__, (unsigned long)limit); +#ifdef CONFIG_OF + of_scan_flat_dt(cma_fdt_scan, NULL); +#endif + if (size_cmdline != -1) { sel_size = size_cmdline; } else { @@ -265,6 +320,71 @@ int __init dma_contiguous_add_device(struct device *dev, struct cma *cma) return 0; } +#ifdef CONFIG_OF +static struct cma_map { + struct cma *cma; + struct device_node *node; +} cma_maps[MAX_CMA_AREAS]; +static unsigned cma_map_count; + +static void cma_assign_device_from_dt(struct device *dev) +{ + int i; + for (i=0; iof_node) { + dev_set_cma_area(dev, cma_maps[i].cma); + pr_info("Assigned CMA %s to %s device\n", + cma_maps[i].cma->full_name, + dev_name(dev)); + } + } +} + +static int cma_device_init_notifier_call(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct device *dev = data; + if (event == BUS_NOTIFY_ADD_DEVICE && dev->of_node) + cma_assign_device_from_dt(dev); + return NOTIFY_DONE; +} + +static struct notifier_block cma_dev_init_nb = { + .notifier_call = cma_device_init_notifier_call, +}; + +void scan_cma_nodes(void) +{ + struct device_node *parent = of_find_node_by_path("/chosen/contiguous-memory"); + struct device_node *child; + + if (!parent) + return; + + for_each_child_of_node(parent, child) { + struct cma *cma = NULL; + int i; + + for (i=0; ifull_name, cma_areas[i].full_name)) + cma = &cma_areas[i]; + if (!cma) + continue; + + for (i=0;; i++) { + struct device_node *node; + node = of_parse_phandle(child, "device", i); + if (!node) + break; + + cma_maps[cma_map_count].cma = cma; + cma_maps[cma_map_count].node = node; + cma_map_count++; + } + } +} +#endif + static int __init cma_init_reserved_areas(void) { int i; @@ -275,6 +395,10 @@ static int __init cma_init_reserved_areas(void) return ret; } +#ifdef CONFIG_OF + scan_cma_nodes(); + bus_register_notifier(&platform_bus_type, &cma_dev_init_nb); +#endif return 0; } core_initcall(cma_init_reserved_areas);