xref: /openbmc/linux/drivers/soc/fsl/qbman/dpaa_sys.c (revision b8d312aa)
1 /* Copyright 2017 NXP Semiconductor, Inc.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5  *     * Redistributions of source code must retain the above copyright
6  *	 notice, this list of conditions and the following disclaimer.
7  *     * Redistributions in binary form must reproduce the above copyright
8  *	 notice, this list of conditions and the following disclaimer in the
9  *	 documentation and/or other materials provided with the distribution.
10  *     * Neither the name of NXP Semiconductor nor the
11  *	 names of its contributors may be used to endorse or promote products
12  *	 derived from this software without specific prior written permission.
13  *
14  * ALTERNATIVELY, this software may be distributed under the terms of the
15  * GNU General Public License ("GPL") as published by the Free Software
16  * Foundation, either version 2 of that License or (at your option) any
17  * later version.
18  *
19  * THIS SOFTWARE IS PROVIDED BY NXP Semiconductor ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL NXP Semiconductor BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <linux/dma-mapping.h>
32 #include "dpaa_sys.h"
33 
34 /*
35  * Initialize a devices private memory region
36  */
37 int qbman_init_private_mem(struct device *dev, int idx, dma_addr_t *addr,
38 				size_t *size)
39 {
40 	int ret;
41 	struct device_node *mem_node;
42 	u64 size64;
43 
44 	ret = of_reserved_mem_device_init_by_idx(dev, dev->of_node, idx);
45 	if (ret) {
46 		dev_err(dev,
47 			"of_reserved_mem_device_init_by_idx(%d) failed 0x%x\n",
48 			idx, ret);
49 		return -ENODEV;
50 	}
51 	mem_node = of_parse_phandle(dev->of_node, "memory-region", 0);
52 	if (mem_node) {
53 		ret = of_property_read_u64(mem_node, "size", &size64);
54 		if (ret) {
55 			dev_err(dev, "of_address_to_resource fails 0x%x\n",
56 			        ret);
57 			return -ENODEV;
58 		}
59 		*size = size64;
60 	} else {
61 		dev_err(dev, "No memory-region found for index %d\n", idx);
62 		return -ENODEV;
63 	}
64 
65 	if (!dma_alloc_coherent(dev, *size, addr, 0)) {
66 		dev_err(dev, "DMA Alloc memory failed\n");
67 		return -ENODEV;
68 	}
69 
70 	/*
71 	 * Disassociate the reserved memory area from the device
72 	 * because a device can only have one DMA memory area. This
73 	 * should be fine since the memory is allocated and initialized
74 	 * and only ever accessed by the QBMan device from now on
75 	 */
76 	of_reserved_mem_device_release(dev);
77 	return 0;
78 }
79