1
2
3
4
5
6
7
8
9
10
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/mm.h>
14#include <linux/kdev_t.h>
15#include <linux/major.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/pagemap.h>
20#include <linux/memblock.h>
21#include <linux/mount.h>
22#include <linux/blkdev.h>
23#include <linux/module.h>
24#include <linux/ioport.h>
25
26#include <asm/setup.h>
27#include <asm/machdep.h>
28#include <asm/page.h>
29#include <asm/pgtable.h>
30#include <asm/atarihw.h>
31#include <asm/atari_stram.h>
32#include <asm/io.h>
33
34
35
36
37
38
39
40
41
42
43static int kernel_in_stram;
44
45static struct resource stram_pool = {
46 .name = "ST-RAM Pool"
47};
48
49static unsigned long pool_size = 1024*1024;
50
51static unsigned long stram_virt_offset;
52
53static int __init atari_stram_setup(char *arg)
54{
55 if (!MACH_IS_ATARI)
56 return 0;
57
58 pool_size = memparse(arg, NULL);
59 return 0;
60}
61
62early_param("stram_pool", atari_stram_setup);
63
64
65
66
67
68
69void __init atari_stram_init(void)
70{
71 int i;
72
73
74
75
76
77 kernel_in_stram = (m68k_memory[0].addr == 0);
78
79 for (i = 0; i < m68k_num_memory; ++i) {
80 if (m68k_memory[i].addr == 0) {
81 return;
82 }
83 }
84
85
86 panic("atari_stram_init: no ST-RAM found!");
87}
88
89
90
91
92
93
94void __init atari_stram_reserve_pages(void *start_mem)
95{
96 if (kernel_in_stram) {
97 pr_debug("atari_stram pool: kernel in ST-RAM, using alloc_bootmem!\n");
98 stram_pool.start = (resource_size_t)memblock_alloc_low(pool_size,
99 PAGE_SIZE);
100 if (!stram_pool.start)
101 panic("%s: Failed to allocate %lu bytes align=%lx\n",
102 __func__, pool_size, PAGE_SIZE);
103
104 stram_pool.end = stram_pool.start + pool_size - 1;
105 request_resource(&iomem_resource, &stram_pool);
106 stram_virt_offset = 0;
107 pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
108 pool_size, &stram_pool);
109 pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
110 stram_virt_offset);
111 }
112}
113
114
115
116
117
118
119int __init atari_stram_map_pages(void)
120{
121 if (!kernel_in_stram) {
122
123
124
125 pr_debug("atari_stram pool: kernel not in ST-RAM, using ioremap!\n");
126 stram_pool.start = PAGE_SIZE;
127 stram_pool.end = stram_pool.start + pool_size - 1;
128 request_resource(&iomem_resource, &stram_pool);
129 stram_virt_offset = (unsigned long) ioremap(stram_pool.start,
130 resource_size(&stram_pool)) - stram_pool.start;
131 pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
132 pool_size, &stram_pool);
133 pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
134 stram_virt_offset);
135 }
136 return 0;
137}
138arch_initcall(atari_stram_map_pages);
139
140
141void *atari_stram_to_virt(unsigned long phys)
142{
143 return (void *)(phys + stram_virt_offset);
144}
145EXPORT_SYMBOL(atari_stram_to_virt);
146
147
148unsigned long atari_stram_to_phys(void *virt)
149{
150 return (unsigned long)(virt - stram_virt_offset);
151}
152EXPORT_SYMBOL(atari_stram_to_phys);
153
154
155void *atari_stram_alloc(unsigned long size, const char *owner)
156{
157 struct resource *res;
158 int error;
159
160 pr_debug("atari_stram_alloc: allocate %lu bytes\n", size);
161
162
163 size = PAGE_ALIGN(size);
164
165 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
166 if (!res)
167 return NULL;
168
169 res->name = owner;
170 error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX,
171 PAGE_SIZE, NULL, NULL);
172 if (error < 0) {
173 pr_err("atari_stram_alloc: allocate_resource() failed %d!\n",
174 error);
175 kfree(res);
176 return NULL;
177 }
178
179 pr_debug("atari_stram_alloc: returning %pR\n", res);
180 return atari_stram_to_virt(res->start);
181}
182EXPORT_SYMBOL(atari_stram_alloc);
183
184
185void atari_stram_free(void *addr)
186{
187 unsigned long start = atari_stram_to_phys(addr);
188 struct resource *res;
189 unsigned long size;
190
191 res = lookup_resource(&stram_pool, start);
192 if (!res) {
193 pr_err("atari_stram_free: trying to free nonexistent region "
194 "at %p\n", addr);
195 return;
196 }
197
198 size = resource_size(res);
199 pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr);
200 release_resource(res);
201 kfree(res);
202}
203EXPORT_SYMBOL(atari_stram_free);
204