Char: mxser_new, upgrade to 1.10
[linux-2.6/mini2440.git] / arch / m68k / atari / stram.c
blobbf4588cbe3711bca7125a1d1167541ec76fe0531
1 /*
2 * arch/m68k/atari/stram.c: Functions for ST-RAM allocations
4 * Copyright 1994-97 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
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 */
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/bootmem.h>
21 #include <linux/mount.h>
22 #include <linux/blkdev.h>
24 #include <asm/setup.h>
25 #include <asm/machdep.h>
26 #include <asm/page.h>
27 #include <asm/pgtable.h>
28 #include <asm/atarihw.h>
29 #include <asm/atari_stram.h>
30 #include <asm/io.h>
31 #include <asm/semaphore.h>
33 #undef DEBUG
35 #ifdef DEBUG
36 #define DPRINTK(fmt,args...) printk( fmt, ##args )
37 #else
38 #define DPRINTK(fmt,args...)
39 #endif
41 #if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
42 /* abbrev for the && above... */
43 #define DO_PROC
44 #include <linux/proc_fs.h>
45 #endif
48 * ++roman:
50 * New version of ST-Ram buffer allocation. Instead of using the
51 * 1 MB - 4 KB that remain when the ST-Ram chunk starts at $1000
52 * (1 MB granularity!), such buffers are reserved like this:
54 * - If the kernel resides in ST-Ram anyway, we can take the buffer
55 * from behind the current kernel data space the normal way
56 * (incrementing start_mem).
58 * - If the kernel is in TT-Ram, stram_init() initializes start and
59 * end of the available region. Buffers are allocated from there
60 * and mem_init() later marks the such used pages as reserved.
61 * Since each TT-Ram chunk is at least 4 MB in size, I hope there
62 * won't be an overrun of the ST-Ram region by normal kernel data
63 * space.
65 * For that, ST-Ram may only be allocated while kernel initialization
66 * is going on, or exactly: before mem_init() is called. There is also
67 * no provision now for freeing ST-Ram buffers. It seems that isn't
68 * really needed.
72 /* Start and end (virtual) of ST-RAM */
73 static void *stram_start, *stram_end;
75 /* set after memory_init() executed and allocations via start_mem aren't
76 * possible anymore */
77 static int mem_init_done;
79 /* set if kernel is in ST-RAM */
80 static int kernel_in_stram;
82 typedef struct stram_block {
83 struct stram_block *next;
84 void *start;
85 unsigned long size;
86 unsigned flags;
87 const char *owner;
88 } BLOCK;
90 /* values for flags field */
91 #define BLOCK_FREE 0x01 /* free structure in the BLOCKs pool */
92 #define BLOCK_KMALLOCED 0x02 /* structure allocated by kmalloc() */
93 #define BLOCK_GFP 0x08 /* block allocated with __get_dma_pages() */
95 /* list of allocated blocks */
96 static BLOCK *alloc_list;
98 /* We can't always use kmalloc() to allocate BLOCK structures, since
99 * stram_alloc() can be called rather early. So we need some pool of
100 * statically allocated structures. 20 of them is more than enough, so in most
101 * cases we never should need to call kmalloc(). */
102 #define N_STATIC_BLOCKS 20
103 static BLOCK static_blocks[N_STATIC_BLOCKS];
105 /***************************** Prototypes *****************************/
107 static BLOCK *add_region( void *addr, unsigned long size );
108 static BLOCK *find_region( void *addr );
109 static int remove_region( BLOCK *block );
111 /************************* End of Prototypes **************************/
114 /* ------------------------------------------------------------------------ */
115 /* Public Interface */
116 /* ------------------------------------------------------------------------ */
119 * This init function is called very early by atari/config.c
120 * It initializes some internal variables needed for stram_alloc()
122 void __init atari_stram_init(void)
124 int i;
126 /* initialize static blocks */
127 for( i = 0; i < N_STATIC_BLOCKS; ++i )
128 static_blocks[i].flags = BLOCK_FREE;
130 /* determine whether kernel code resides in ST-RAM (then ST-RAM is the
131 * first memory block at virtual 0x0) */
132 stram_start = phys_to_virt(0);
133 kernel_in_stram = (stram_start == 0);
135 for( i = 0; i < m68k_num_memory; ++i ) {
136 if (m68k_memory[i].addr == 0) {
137 /* skip first 2kB or page (supervisor-only!) */
138 stram_end = stram_start + m68k_memory[i].size;
139 return;
142 /* Should never come here! (There is always ST-Ram!) */
143 panic( "atari_stram_init: no ST-RAM found!" );
148 * This function is called from setup_arch() to reserve the pages needed for
149 * ST-RAM management.
151 void __init atari_stram_reserve_pages(void *start_mem)
153 /* always reserve first page of ST-RAM, the first 2 kB are
154 * supervisor-only! */
155 if (!kernel_in_stram)
156 reserve_bootmem (0, PAGE_SIZE);
160 void atari_stram_mem_init_hook (void)
162 mem_init_done = 1;
167 * This is main public interface: somehow allocate a ST-RAM block
169 * - If we're before mem_init(), we have to make a static allocation. The
170 * region is taken in the kernel data area (if the kernel is in ST-RAM) or
171 * from the start of ST-RAM (if the kernel is in TT-RAM) and added to the
172 * rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel
173 * address space in the latter case.
175 * - If mem_init() already has been called, try with __get_dma_pages().
176 * This has the disadvantage that it's very hard to get more than 1 page,
177 * and it is likely to fail :-(
180 void *atari_stram_alloc(long size, const char *owner)
182 void *addr = NULL;
183 BLOCK *block;
184 int flags;
186 DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner);
188 if (!mem_init_done)
189 return alloc_bootmem_low(size);
190 else {
191 /* After mem_init(): can only resort to __get_dma_pages() */
192 addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size));
193 flags = BLOCK_GFP;
194 DPRINTK( "atari_stram_alloc: after mem_init, "
195 "get_pages=%p\n", addr );
198 if (addr) {
199 if (!(block = add_region( addr, size ))) {
200 /* out of memory for BLOCK structure :-( */
201 DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- "
202 "freeing again\n" );
203 free_pages((unsigned long)addr, get_order(size));
204 return( NULL );
206 block->owner = owner;
207 block->flags |= flags;
209 return( addr );
212 void atari_stram_free( void *addr )
215 BLOCK *block;
217 DPRINTK( "atari_stram_free(addr=%p)\n", addr );
219 if (!(block = find_region( addr ))) {
220 printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "
221 "from %p\n", addr, __builtin_return_address(0) );
222 return;
224 DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "
225 "flags=%02x\n", block, block->size, block->owner, block->flags );
227 if (!(block->flags & BLOCK_GFP))
228 goto fail;
230 DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n",
231 get_order(block->size));
232 free_pages((unsigned long)addr, get_order(block->size));
233 remove_region( block );
234 return;
236 fail:
237 printk( KERN_ERR "atari_stram_free: cannot free block at %p "
238 "(called from %p)\n", addr, __builtin_return_address(0) );
242 /* ------------------------------------------------------------------------ */
243 /* Region Management */
244 /* ------------------------------------------------------------------------ */
247 /* insert a region into the alloced list (sorted) */
248 static BLOCK *add_region( void *addr, unsigned long size )
250 BLOCK **p, *n = NULL;
251 int i;
253 for( i = 0; i < N_STATIC_BLOCKS; ++i ) {
254 if (static_blocks[i].flags & BLOCK_FREE) {
255 n = &static_blocks[i];
256 n->flags = 0;
257 break;
260 if (!n && mem_init_done) {
261 /* if statics block pool exhausted and we can call kmalloc() already
262 * (after mem_init()), try that */
263 n = kmalloc( sizeof(BLOCK), GFP_KERNEL );
264 if (n)
265 n->flags = BLOCK_KMALLOCED;
267 if (!n) {
268 printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );
269 return( NULL );
271 n->start = addr;
272 n->size = size;
274 for( p = &alloc_list; *p; p = &((*p)->next) )
275 if ((*p)->start > addr) break;
276 n->next = *p;
277 *p = n;
279 return( n );
283 /* find a region (by start addr) in the alloced list */
284 static BLOCK *find_region( void *addr )
286 BLOCK *p;
288 for( p = alloc_list; p; p = p->next ) {
289 if (p->start == addr)
290 return( p );
291 if (p->start > addr)
292 break;
294 return( NULL );
298 /* remove a block from the alloced list */
299 static int remove_region( BLOCK *block )
301 BLOCK **p;
303 for( p = &alloc_list; *p; p = &((*p)->next) )
304 if (*p == block) break;
305 if (!*p)
306 return( 0 );
308 *p = block->next;
309 if (block->flags & BLOCK_KMALLOCED)
310 kfree( block );
311 else
312 block->flags |= BLOCK_FREE;
313 return( 1 );
318 /* ------------------------------------------------------------------------ */
319 /* /proc statistics file stuff */
320 /* ------------------------------------------------------------------------ */
322 #ifdef DO_PROC
324 #define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
326 int get_stram_list( char *buf )
328 int len = 0;
329 BLOCK *p;
331 PRINT_PROC("Total ST-RAM: %8u kB\n",
332 (stram_end - stram_start) >> 10);
333 PRINT_PROC( "Allocated regions:\n" );
334 for( p = alloc_list; p; p = p->next ) {
335 if (len + 50 >= PAGE_SIZE)
336 break;
337 PRINT_PROC("0x%08lx-0x%08lx: %s (",
338 virt_to_phys(p->start),
339 virt_to_phys(p->start+p->size-1),
340 p->owner);
341 if (p->flags & BLOCK_GFP)
342 PRINT_PROC( "page-alloced)\n" );
343 else
344 PRINT_PROC( "??)\n" );
347 return( len );
350 #endif
354 * Local variables:
355 * c-indent-level: 4
356 * tab-width: 4
357 * End: