1 // SPDX-License-Identifier: GPL-2.0-only
5 * Author: Scott Wood <scottwood@freescale.com>
7 * Copyright 2007-2008,2010 Freescale Semiconductor, Inc.
9 * Some parts derived from commproc.c/cpm2_common.c, which is:
10 * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
11 * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com>
12 * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
13 * 2006 (c) MontaVista Software, Inc.
14 * Vitaly Bordug <vbordug@ru.mvista.com>
16 #include <linux/genalloc.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/of_device.h>
20 #include <linux/spinlock.h>
21 #include <linux/export.h>
23 #include <linux/of_address.h>
24 #include <linux/slab.h>
26 #include <soc/fsl/qe/qe.h>
28 static struct gen_pool *muram_pool;
29 static DEFINE_SPINLOCK(cpm_muram_lock);
30 static void __iomem *muram_vbase;
31 static phys_addr_t muram_pbase;
34 struct list_head head;
39 static LIST_HEAD(muram_block_list);
41 /* max address size we deal with */
42 #define OF_MAX_ADDR_CELLS 4
43 #define GENPOOL_OFFSET (4096 * 8)
45 int cpm_muram_init(void)
47 struct device_node *np;
49 __be32 zero[OF_MAX_ADDR_CELLS] = {};
50 resource_size_t max = 0;
57 np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
59 /* try legacy bindings */
60 np = of_find_node_by_name(NULL, "data-only");
62 pr_err("Cannot find CPM muram data node");
68 muram_pool = gen_pool_create(0, -1);
70 pr_err("Cannot allocate memory pool for CPM/QE muram");
74 muram_pbase = of_translate_address(np, zero);
75 if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
76 pr_err("Cannot translate zero through CPM muram node");
81 while (of_address_to_resource(np, i++, &r) == 0) {
84 ret = gen_pool_add(muram_pool, r.start - muram_pbase +
85 GENPOOL_OFFSET, resource_size(&r), -1);
87 pr_err("QE: couldn't add muram to pool!\n");
92 muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
94 pr_err("Cannot map QE muram");
100 gen_pool_destroy(muram_pool);
107 * cpm_muram_alloc_common - cpm_muram_alloc common code
108 * @size: number of bytes to allocate
109 * @algo: algorithm for alloc.
110 * @data: data for genalloc's algorithm.
112 * This function returns a non-negative offset into the muram area, or
113 * a negative errno on failure.
115 static s32 cpm_muram_alloc_common(unsigned long size,
116 genpool_algo_t algo, void *data)
118 struct muram_block *entry;
121 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
124 start = gen_pool_alloc_algo(muram_pool, size, algo, data);
129 start = start - GENPOOL_OFFSET;
130 memset_io(cpm_muram_addr(start), 0, size);
131 entry->start = start;
133 list_add(&entry->head, &muram_block_list);
139 * cpm_muram_alloc - allocate the requested size worth of multi-user ram
140 * @size: number of bytes to allocate
141 * @align: requested alignment, in bytes
143 * This function returns a non-negative offset into the muram area, or
144 * a negative errno on failure.
145 * Use cpm_dpram_addr() to get the virtual address of the area.
146 * Use cpm_muram_free() to free the allocation.
148 s32 cpm_muram_alloc(unsigned long size, unsigned long align)
152 struct genpool_data_align muram_pool_data;
154 spin_lock_irqsave(&cpm_muram_lock, flags);
155 muram_pool_data.align = align;
156 start = cpm_muram_alloc_common(size, gen_pool_first_fit_align,
158 spin_unlock_irqrestore(&cpm_muram_lock, flags);
161 EXPORT_SYMBOL(cpm_muram_alloc);
164 * cpm_muram_free - free a chunk of multi-user ram
165 * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
167 void cpm_muram_free(s32 offset)
171 struct muram_block *tmp;
177 spin_lock_irqsave(&cpm_muram_lock, flags);
178 list_for_each_entry(tmp, &muram_block_list, head) {
179 if (tmp->start == offset) {
181 list_del(&tmp->head);
186 gen_pool_free(muram_pool, offset + GENPOOL_OFFSET, size);
187 spin_unlock_irqrestore(&cpm_muram_lock, flags);
189 EXPORT_SYMBOL(cpm_muram_free);
192 * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
193 * @offset: offset of allocation start address
194 * @size: number of bytes to allocate
195 * This function returns @offset if the area was available, a negative
197 * Use cpm_dpram_addr() to get the virtual address of the area.
198 * Use cpm_muram_free() to free the allocation.
200 s32 cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
204 struct genpool_data_fixed muram_pool_data_fixed;
206 spin_lock_irqsave(&cpm_muram_lock, flags);
207 muram_pool_data_fixed.offset = offset + GENPOOL_OFFSET;
208 start = cpm_muram_alloc_common(size, gen_pool_fixed_alloc,
209 &muram_pool_data_fixed);
210 spin_unlock_irqrestore(&cpm_muram_lock, flags);
213 EXPORT_SYMBOL(cpm_muram_alloc_fixed);
216 * cpm_muram_addr - turn a muram offset into a virtual address
217 * @offset: muram offset to convert
219 void __iomem *cpm_muram_addr(unsigned long offset)
221 return muram_vbase + offset;
223 EXPORT_SYMBOL(cpm_muram_addr);
225 unsigned long cpm_muram_offset(const void __iomem *addr)
227 return addr - muram_vbase;
229 EXPORT_SYMBOL(cpm_muram_offset);
232 * cpm_muram_dma - turn a muram virtual address into a DMA address
233 * @addr: virtual address from cpm_muram_addr() to convert
235 dma_addr_t cpm_muram_dma(void __iomem *addr)
237 return muram_pbase + (addr - muram_vbase);
239 EXPORT_SYMBOL(cpm_muram_dma);
242 * As cpm_muram_free, but takes the virtual address rather than the
245 void cpm_muram_free_addr(const void __iomem *addr)
249 cpm_muram_free(cpm_muram_offset(addr));
251 EXPORT_SYMBOL(cpm_muram_free_addr);