1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2009-2010 Freescale Semiconductor, Inc.
5 * Simple memory allocator abstraction for QorIQ (P1/P2) based Cache-SRAM
7 * Author: Vivek Mahajan <vivek.mahajan@freescale.com>
9 * This file is derived from the original work done
10 * by Sylvain Munaut for the Bestcomm SRAM allocator.
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/of_platform.h>
18 #include <linux/pgtable.h>
19 #include <asm/fsl_85xx_cache_sram.h>
21 #include "fsl_85xx_cache_ctlr.h"
23 struct mpc85xx_cache_sram
*cache_sram
;
25 void *mpc85xx_cache_sram_alloc(unsigned int size
,
26 phys_addr_t
*phys
, unsigned int align
)
31 if (unlikely(cache_sram
== NULL
))
34 if (!size
|| (size
> cache_sram
->size
) || (align
> cache_sram
->size
)) {
35 pr_err("%s(): size(=%x) or align(=%x) zero or too big\n",
36 __func__
, size
, align
);
40 if ((align
& (align
- 1)) || align
<= 1) {
41 pr_err("%s(): align(=%x) must be power of two and >1\n",
46 spin_lock_irqsave(&cache_sram
->lock
, flags
);
47 offset
= rh_alloc_align(cache_sram
->rh
, size
, align
, NULL
);
48 spin_unlock_irqrestore(&cache_sram
->lock
, flags
);
50 if (IS_ERR_VALUE(offset
))
53 *phys
= cache_sram
->base_phys
+ offset
;
55 return (unsigned char *)cache_sram
->base_virt
+ offset
;
57 EXPORT_SYMBOL(mpc85xx_cache_sram_alloc
);
59 void mpc85xx_cache_sram_free(void *ptr
)
64 spin_lock_irqsave(&cache_sram
->lock
, flags
);
65 rh_free(cache_sram
->rh
, ptr
- cache_sram
->base_virt
);
66 spin_unlock_irqrestore(&cache_sram
->lock
, flags
);
68 EXPORT_SYMBOL(mpc85xx_cache_sram_free
);
70 int __init
instantiate_cache_sram(struct platform_device
*dev
,
71 struct sram_parameters sram_params
)
76 dev_err(&dev
->dev
, "Already initialized cache-sram\n");
80 cache_sram
= kzalloc(sizeof(struct mpc85xx_cache_sram
), GFP_KERNEL
);
82 dev_err(&dev
->dev
, "Out of memory for cache_sram structure\n");
86 cache_sram
->base_phys
= sram_params
.sram_offset
;
87 cache_sram
->size
= sram_params
.sram_size
;
89 if (!request_mem_region(cache_sram
->base_phys
, cache_sram
->size
,
90 "fsl_85xx_cache_sram")) {
91 dev_err(&dev
->dev
, "%pOF: request memory failed\n",
97 cache_sram
->base_virt
= ioremap_coherent(cache_sram
->base_phys
,
99 if (!cache_sram
->base_virt
) {
100 dev_err(&dev
->dev
, "%pOF: ioremap_coherent failed\n",
106 cache_sram
->rh
= rh_create(sizeof(unsigned int));
107 if (IS_ERR(cache_sram
->rh
)) {
108 dev_err(&dev
->dev
, "%pOF: Unable to create remote heap\n",
110 ret
= PTR_ERR(cache_sram
->rh
);
114 rh_attach_region(cache_sram
->rh
, 0, cache_sram
->size
);
115 spin_lock_init(&cache_sram
->lock
);
117 dev_info(&dev
->dev
, "[base:0x%llx, size:0x%x] configured and loaded\n",
118 (unsigned long long)cache_sram
->base_phys
, cache_sram
->size
);
123 iounmap(cache_sram
->base_virt
);
126 release_mem_region(cache_sram
->base_phys
, cache_sram
->size
);
133 void remove_cache_sram(struct platform_device
*dev
)
137 rh_detach_region(cache_sram
->rh
, 0, cache_sram
->size
);
138 rh_destroy(cache_sram
->rh
);
140 iounmap(cache_sram
->base_virt
);
141 release_mem_region(cache_sram
->base_phys
, cache_sram
->size
);
146 dev_info(&dev
->dev
, "MPC85xx Cache-SRAM driver unloaded\n");