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
11 #include <linux/types.h>
12 #include <linux/kernel.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>
23 #include <linux/module.h>
25 #include <asm/setup.h>
26 #include <asm/machdep.h>
28 #include <asm/pgtable.h>
29 #include <asm/atarihw.h>
30 #include <asm/atari_stram.h>
32 #include <asm/semaphore.h>
37 #define DPRINTK(fmt,args...) printk( fmt, ##args )
39 #define DPRINTK(fmt,args...)
42 #if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
43 /* abbrev for the && above... */
45 #include <linux/proc_fs.h>
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:
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).
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
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
73 /* Start and end (virtual) of ST-RAM */
74 static void *stram_start, *stram_end;
76 /* set after memory_init() executed and allocations via start_mem aren't
78 static int mem_init_done;
80 /* set if kernel is in ST-RAM */
81 static int kernel_in_stram;
83 typedef struct stram_block {
84 struct stram_block *next;
91 /* values for flags field */
92 #define BLOCK_FREE 0x01 /* free structure in the BLOCKs pool */
93 #define BLOCK_KMALLOCED 0x02 /* structure allocated by kmalloc() */
94 #define BLOCK_GFP 0x08 /* block allocated with __get_dma_pages() */
96 /* list of allocated blocks */
97 static BLOCK *alloc_list;
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
104 static BLOCK static_blocks[N_STATIC_BLOCKS];
106 /***************************** Prototypes *****************************/
108 static BLOCK *add_region( void *addr, unsigned long size );
109 static BLOCK *find_region( void *addr );
110 static int remove_region( BLOCK *block );
112 /************************* End of Prototypes **************************/
115 /* ------------------------------------------------------------------------ */
116 /* Public Interface */
117 /* ------------------------------------------------------------------------ */
120 * This init function is called very early by atari/config.c
121 * It initializes some internal variables needed for stram_alloc()
123 void __init atari_stram_init(void)
127 /* initialize static blocks */
128 for( i = 0; i < N_STATIC_BLOCKS; ++i )
129 static_blocks[i].flags = BLOCK_FREE;
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);
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;
143 /* Should never come here! (There is always ST-Ram!) */
144 panic( "atari_stram_init: no ST-RAM found!" );
149 * This function is called from setup_arch() to reserve the pages needed for
152 void __init atari_stram_reserve_pages(void *start_mem)
154 /* 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);
161 void atari_stram_mem_init_hook (void)
168 * This is main public interface: somehow allocate a ST-RAM block
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.
176 * - 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 :-(
181 void *atari_stram_alloc(long size, const char *owner)
187 DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner);
190 return alloc_bootmem_low(size);
192 /* After mem_init(): can only resort to __get_dma_pages() */
193 addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size));
195 DPRINTK( "atari_stram_alloc: after mem_init, "
196 "get_pages=%p\n", 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 -- "
204 free_pages((unsigned long)addr, get_order(size));
207 block->owner = owner;
208 block->flags |= flags;
212 EXPORT_SYMBOL(atari_stram_alloc);
214 void atari_stram_free( void *addr )
219 DPRINTK( "atari_stram_free(addr=%p)\n", addr );
221 if (!(block = find_region( addr ))) {
222 printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "
223 "from %p\n", addr, __builtin_return_address(0) );
226 DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "
227 "flags=%02x\n", block, block->size, block->owner, block->flags );
229 if (!(block->flags & BLOCK_GFP))
232 DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n",
233 get_order(block->size));
234 free_pages((unsigned long)addr, get_order(block->size));
235 remove_region( block );
239 printk( KERN_ERR "atari_stram_free: cannot free block at %p "
240 "(called from %p)\n", addr, __builtin_return_address(0) );
242 EXPORT_SYMBOL(atari_stram_free);
245 /* ------------------------------------------------------------------------ */
246 /* Region Management */
247 /* ------------------------------------------------------------------------ */
250 /* insert a region into the alloced list (sorted) */
251 static BLOCK *add_region( void *addr, unsigned long size )
253 BLOCK **p, *n = NULL;
256 for( i = 0; i < N_STATIC_BLOCKS; ++i ) {
257 if (static_blocks[i].flags & BLOCK_FREE) {
258 n = &static_blocks[i];
263 if (!n && mem_init_done) {
264 /* if statics block pool exhausted and we can call kmalloc() already
265 * (after mem_init()), try that */
266 n = kmalloc( sizeof(BLOCK), GFP_KERNEL );
268 n->flags = BLOCK_KMALLOCED;
271 printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );
277 for( p = &alloc_list; *p; p = &((*p)->next) )
278 if ((*p)->start > addr) break;
286 /* find a region (by start addr) in the alloced list */
287 static BLOCK *find_region( void *addr )
291 for( p = alloc_list; p; p = p->next ) {
292 if (p->start == addr)
301 /* remove a block from the alloced list */
302 static int remove_region( BLOCK *block )
306 for( p = &alloc_list; *p; p = &((*p)->next) )
307 if (*p == block) break;
312 if (block->flags & BLOCK_KMALLOCED)
315 block->flags |= BLOCK_FREE;
321 /* ------------------------------------------------------------------------ */
322 /* /proc statistics file stuff */
323 /* ------------------------------------------------------------------------ */
327 #define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
329 int get_stram_list( char *buf )
334 PRINT_PROC("Total ST-RAM: %8u kB\n",
335 (stram_end - stram_start) >> 10);
336 PRINT_PROC( "Allocated regions:\n" );
337 for( p = alloc_list; p; p = p->next ) {
338 if (len + 50 >= PAGE_SIZE)
340 PRINT_PROC("0x%08lx-0x%08lx: %s (",
341 virt_to_phys(p->start),
342 virt_to_phys(p->start+p->size-1),
344 if (p->flags & BLOCK_GFP)
345 PRINT_PROC( "page-alloced)\n" );
347 PRINT_PROC( "??)\n" );