blob: 22e0481a5f7b484b46fae64c638c4ec15efa569a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/m68k/atari/stram.c: Functions for ST-RAM allocations
3 *
4 * Copyright 1994-97 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
8 * for more details.
9 */
10
11#include <linux/config.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/kdev_t.h>
16#include <linux/major.h>
17#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/slab.h>
19#include <linux/vmalloc.h>
20#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/bootmem.h>
22#include <linux/mount.h>
23#include <linux/blkdev.h>
24
25#include <asm/setup.h>
26#include <asm/machdep.h>
27#include <asm/page.h>
28#include <asm/pgtable.h>
29#include <asm/atarihw.h>
30#include <asm/atari_stram.h>
31#include <asm/io.h>
32#include <asm/semaphore.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#undef DEBUG
35
36#ifdef DEBUG
37#define DPRINTK(fmt,args...) printk( fmt, ##args )
38#else
39#define DPRINTK(fmt,args...)
40#endif
41
42#if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
43/* abbrev for the && above... */
44#define DO_PROC
45#include <linux/proc_fs.h>
46#endif
47
Hugh Dickinsf9c98d02005-10-29 18:16:10 -070048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * ++roman:
50 *
51 * New version of ST-Ram buffer allocation. Instead of using the
52 * 1 MB - 4 KB that remain when the ST-Ram chunk starts at $1000
53 * (1 MB granularity!), such buffers are reserved like this:
54 *
55 * - If the kernel resides in ST-Ram anyway, we can take the buffer
56 * from behind the current kernel data space the normal way
57 * (incrementing start_mem).
58 *
59 * - If the kernel is in TT-Ram, stram_init() initializes start and
60 * end of the available region. Buffers are allocated from there
61 * and mem_init() later marks the such used pages as reserved.
62 * Since each TT-Ram chunk is at least 4 MB in size, I hope there
63 * won't be an overrun of the ST-Ram region by normal kernel data
64 * space.
65 *
66 * For that, ST-Ram may only be allocated while kernel initialization
67 * is going on, or exactly: before mem_init() is called. There is also
68 * no provision now for freeing ST-Ram buffers. It seems that isn't
69 * really needed.
70 *
71 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Start and end (virtual) of ST-RAM */
74static void *stram_start, *stram_end;
75
76/* set after memory_init() executed and allocations via start_mem aren't
77 * possible anymore */
78static int mem_init_done;
79
80/* set if kernel is in ST-RAM */
81static int kernel_in_stram;
82
83typedef struct stram_block {
84 struct stram_block *next;
85 void *start;
86 unsigned long size;
87 unsigned flags;
88 const char *owner;
89} BLOCK;
90
91/* values for flags field */
Hugh Dickinsf9c98d02005-10-29 18:16:10 -070092#define BLOCK_FREE 0x01 /* free structure in the BLOCKs pool */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define BLOCK_KMALLOCED 0x02 /* structure allocated by kmalloc() */
Hugh Dickinsf9c98d02005-10-29 18:16:10 -070094#define BLOCK_GFP 0x08 /* block allocated with __get_dma_pages() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* list of allocated blocks */
97static BLOCK *alloc_list;
98
99/* We can't always use kmalloc() to allocate BLOCK structures, since
100 * stram_alloc() can be called rather early. So we need some pool of
101 * statically allocated structures. 20 of them is more than enough, so in most
102 * cases we never should need to call kmalloc(). */
103#define N_STATIC_BLOCKS 20
104static BLOCK static_blocks[N_STATIC_BLOCKS];
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/***************************** Prototypes *****************************/
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static BLOCK *add_region( void *addr, unsigned long size );
109static BLOCK *find_region( void *addr );
110static int remove_region( BLOCK *block );
111
112/************************* End of Prototypes **************************/
113
114
115/* ------------------------------------------------------------------------ */
116/* Public Interface */
117/* ------------------------------------------------------------------------ */
118
119/*
120 * This init function is called very early by atari/config.c
121 * It initializes some internal variables needed for stram_alloc()
122 */
123void __init atari_stram_init(void)
124{
125 int i;
126
127 /* initialize static blocks */
128 for( i = 0; i < N_STATIC_BLOCKS; ++i )
129 static_blocks[i].flags = BLOCK_FREE;
130
131 /* determine whether kernel code resides in ST-RAM (then ST-RAM is the
132 * first memory block at virtual 0x0) */
133 stram_start = phys_to_virt(0);
134 kernel_in_stram = (stram_start == 0);
135
136 for( i = 0; i < m68k_num_memory; ++i ) {
137 if (m68k_memory[i].addr == 0) {
138 /* skip first 2kB or page (supervisor-only!) */
139 stram_end = stram_start + m68k_memory[i].size;
140 return;
141 }
142 }
143 /* Should never come here! (There is always ST-Ram!) */
144 panic( "atari_stram_init: no ST-RAM found!" );
145}
146
147
148/*
149 * This function is called from setup_arch() to reserve the pages needed for
150 * ST-RAM management.
151 */
152void __init atari_stram_reserve_pages(void *start_mem)
153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* always reserve first page of ST-RAM, the first 2 kB are
155 * supervisor-only! */
156 if (!kernel_in_stram)
157 reserve_bootmem (0, PAGE_SIZE);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void atari_stram_mem_init_hook (void)
162{
163 mem_init_done = 1;
164}
165
166
167/*
168 * This is main public interface: somehow allocate a ST-RAM block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * - If we're before mem_init(), we have to make a static allocation. The
171 * region is taken in the kernel data area (if the kernel is in ST-RAM) or
172 * from the start of ST-RAM (if the kernel is in TT-RAM) and added to the
173 * rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel
174 * address space in the latter case.
175 *
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700176 * - If mem_init() already has been called, try with __get_dma_pages().
177 * This has the disadvantage that it's very hard to get more than 1 page,
178 * and it is likely to fail :-(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
180 */
181void *atari_stram_alloc(long size, const char *owner)
182{
183 void *addr = NULL;
184 BLOCK *block;
185 int flags;
186
187 DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!mem_init_done)
190 return alloc_bootmem_low(size);
191 else {
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700192 /* After mem_init(): can only resort to __get_dma_pages() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size));
194 flags = BLOCK_GFP;
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700195 DPRINTK( "atari_stram_alloc: after mem_init, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 "get_pages=%p\n", addr );
197 }
198
199 if (addr) {
200 if (!(block = add_region( addr, size ))) {
201 /* out of memory for BLOCK structure :-( */
202 DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- "
203 "freeing again\n" );
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700204 free_pages((unsigned long)addr, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return( NULL );
206 }
207 block->owner = owner;
208 block->flags |= flags;
209 }
210 return( addr );
211}
212
213void atari_stram_free( void *addr )
214
215{
216 BLOCK *block;
217
218 DPRINTK( "atari_stram_free(addr=%p)\n", addr );
219
220 if (!(block = find_region( addr ))) {
221 printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "
222 "from %p\n", addr, __builtin_return_address(0) );
223 return;
224 }
225 DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "
226 "flags=%02x\n", block, block->size, block->owner, block->flags );
227
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700228 if (!(block->flags & BLOCK_GFP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto fail;
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700230
231 DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n",
232 get_order(block->size));
233 free_pages((unsigned long)addr, get_order(block->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 remove_region( block );
235 return;
236
237 fail:
238 printk( KERN_ERR "atari_stram_free: cannot free block at %p "
239 "(called from %p)\n", addr, __builtin_return_address(0) );
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/* ------------------------------------------------------------------------ */
244/* Region Management */
245/* ------------------------------------------------------------------------ */
246
247
248/* insert a region into the alloced list (sorted) */
249static BLOCK *add_region( void *addr, unsigned long size )
250{
251 BLOCK **p, *n = NULL;
252 int i;
253
254 for( i = 0; i < N_STATIC_BLOCKS; ++i ) {
255 if (static_blocks[i].flags & BLOCK_FREE) {
256 n = &static_blocks[i];
257 n->flags = 0;
258 break;
259 }
260 }
261 if (!n && mem_init_done) {
262 /* if statics block pool exhausted and we can call kmalloc() already
263 * (after mem_init()), try that */
264 n = kmalloc( sizeof(BLOCK), GFP_KERNEL );
265 if (n)
266 n->flags = BLOCK_KMALLOCED;
267 }
268 if (!n) {
269 printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );
270 return( NULL );
271 }
272 n->start = addr;
273 n->size = size;
274
275 for( p = &alloc_list; *p; p = &((*p)->next) )
276 if ((*p)->start > addr) break;
277 n->next = *p;
278 *p = n;
279
280 return( n );
281}
282
283
284/* find a region (by start addr) in the alloced list */
285static BLOCK *find_region( void *addr )
286{
287 BLOCK *p;
288
289 for( p = alloc_list; p; p = p->next ) {
290 if (p->start == addr)
291 return( p );
292 if (p->start > addr)
293 break;
294 }
295 return( NULL );
296}
297
298
299/* remove a block from the alloced list */
300static int remove_region( BLOCK *block )
301{
302 BLOCK **p;
303
304 for( p = &alloc_list; *p; p = &((*p)->next) )
305 if (*p == block) break;
306 if (!*p)
307 return( 0 );
308
309 *p = block->next;
310 if (block->flags & BLOCK_KMALLOCED)
311 kfree( block );
312 else
313 block->flags |= BLOCK_FREE;
314 return( 1 );
315}
316
317
318
319/* ------------------------------------------------------------------------ */
320/* /proc statistics file stuff */
321/* ------------------------------------------------------------------------ */
322
323#ifdef DO_PROC
324
325#define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
326
327int get_stram_list( char *buf )
328{
329 int len = 0;
330 BLOCK *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Hugh Dickinsf9c98d02005-10-29 18:16:10 -0700332 PRINT_PROC("Total ST-RAM: %8u kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 (stram_end - stram_start) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 PRINT_PROC( "Allocated regions:\n" );
335 for( p = alloc_list; p; p = p->next ) {
336 if (len + 50 >= PAGE_SIZE)
337 break;
338 PRINT_PROC("0x%08lx-0x%08lx: %s (",
339 virt_to_phys(p->start),
340 virt_to_phys(p->start+p->size-1),
341 p->owner);
342 if (p->flags & BLOCK_GFP)
343 PRINT_PROC( "page-alloced)\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 else
345 PRINT_PROC( "??)\n" );
346 }
347
348 return( len );
349}
350
351#endif
352
353
354/*
355 * Local variables:
356 * c-indent-level: 4
357 * tab-width: 4
358 * End:
359 */