Skip to content

Commit 2da1968

Browse files
mmindgregkh
authored andcommitted
misc: sram: implement reserved sram areas
This implements support for defining reserved areas as subnodes, to keep the genpool from using these. Suggested-by: Rob Herring <robherring2@gmail.com> Signed-off-by: Heiko Stuebner <heiko@sntech.de> Tested-by: Ulrich Prinz <ulrich.prinz@googlemail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 96328cd commit 2da1968

1 file changed

Lines changed: 118 additions & 7 deletions

File tree

‎drivers/misc/sram.c‎

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <linux/err.h>
2525
#include <linux/io.h>
2626
#include <linux/of.h>
27+
#include <linux/of_address.h>
28+
#include <linux/list.h>
29+
#include <linux/list_sort.h>
2730
#include <linux/platform_device.h>
2831
#include <linux/slab.h>
2932
#include <linux/spinlock.h>
@@ -36,14 +39,35 @@ struct sram_dev {
3639
struct clk *clk;
3740
};
3841

42+
struct sram_reserve {
43+
struct list_head list;
44+
u32 start;
45+
u32 size;
46+
};
47+
48+
static int sram_reserve_cmp(void *priv, struct list_head *a,
49+
struct list_head *b)
50+
{
51+
struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
52+
struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
53+
54+
return ra->start - rb->start;
55+
}
56+
3957
static int sram_probe(struct platform_device *pdev)
4058
{
4159
void __iomem *virt_base;
4260
struct sram_dev *sram;
4361
struct resource *res;
44-
unsigned long size;
62+
struct device_node *np = pdev->dev.of_node, *child;
63+
unsigned long size, cur_start, cur_size;
64+
struct sram_reserve *rblocks, *block;
65+
struct list_head reserve_list;
66+
unsigned int nblocks;
4567
int ret;
4668

69+
INIT_LIST_HEAD(&reserve_list);
70+
4771
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4872
virt_base = devm_ioremap_resource(&pdev->dev, res);
4973
if (IS_ERR(virt_base))
@@ -65,19 +89,106 @@ static int sram_probe(struct platform_device *pdev)
6589
if (!sram->pool)
6690
return -ENOMEM;
6791

68-
ret = gen_pool_add_virt(sram->pool, (unsigned long)virt_base,
69-
res->start, size, -1);
70-
if (ret < 0) {
71-
if (sram->clk)
72-
clk_disable_unprepare(sram->clk);
73-
return ret;
92+
/*
93+
* We need an additional block to mark the end of the memory region
94+
* after the reserved blocks from the dt are processed.
95+
*/
96+
nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
97+
rblocks = kmalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
98+
if (!rblocks) {
99+
ret = -ENOMEM;
100+
goto err_alloc;
74101
}
75102

103+
block = &rblocks[0];
104+
for_each_available_child_of_node(np, child) {
105+
struct resource child_res;
106+
107+
ret = of_address_to_resource(child, 0, &child_res);
108+
if (ret < 0) {
109+
dev_err(&pdev->dev,
110+
"could not get address for node %s\n",
111+
child->full_name);
112+
goto err_chunks;
113+
}
114+
115+
if (child_res.start < res->start || child_res.end > res->end) {
116+
dev_err(&pdev->dev,
117+
"reserved block %s outside the sram area\n",
118+
child->full_name);
119+
ret = -EINVAL;
120+
goto err_chunks;
121+
}
122+
123+
block->start = child_res.start - res->start;
124+
block->size = resource_size(&child_res);
125+
list_add_tail(&block->list, &reserve_list);
126+
127+
dev_dbg(&pdev->dev, "found reserved block 0x%x-0x%x\n",
128+
block->start,
129+
block->start + block->size);
130+
131+
block++;
132+
}
133+
134+
/* the last chunk marks the end of the region */
135+
rblocks[nblocks - 1].start = size;
136+
rblocks[nblocks - 1].size = 0;
137+
list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
138+
139+
list_sort(NULL, &reserve_list, sram_reserve_cmp);
140+
141+
cur_start = 0;
142+
143+
list_for_each_entry(block, &reserve_list, list) {
144+
/* can only happen if sections overlap */
145+
if (block->start < cur_start) {
146+
dev_err(&pdev->dev,
147+
"block at 0x%x starts after current offset 0x%lx\n",
148+
block->start, cur_start);
149+
ret = -EINVAL;
150+
goto err_chunks;
151+
}
152+
153+
/* current start is in a reserved block, so continue after it */
154+
if (block->start == cur_start) {
155+
cur_start = block->start + block->size;
156+
continue;
157+
}
158+
159+
/*
160+
* allocate the space between the current starting
161+
* address and the following reserved block, or the
162+
* end of the region.
163+
*/
164+
cur_size = block->start - cur_start;
165+
166+
dev_dbg(&pdev->dev, "adding chunk 0x%lx-0x%lx\n",
167+
cur_start, cur_start + cur_size);
168+
ret = gen_pool_add_virt(sram->pool,
169+
(unsigned long)virt_base + cur_start,
170+
res->start + cur_start, cur_size, -1);
171+
if (ret < 0)
172+
goto err_chunks;
173+
174+
/* next allocation after this reserved block */
175+
cur_start = block->start + block->size;
176+
}
177+
178+
kfree(rblocks);
179+
76180
platform_set_drvdata(pdev, sram);
77181

78182
dev_dbg(&pdev->dev, "SRAM pool: %ld KiB @ 0x%p\n", size / 1024, virt_base);
79183

80184
return 0;
185+
186+
err_chunks:
187+
kfree(rblocks);
188+
err_alloc:
189+
if (sram->clk)
190+
clk_disable_unprepare(sram->clk);
191+
return ret;
81192
}
82193

83194
static int sram_remove(struct platform_device *pdev)

0 commit comments

Comments
 (0)