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>
24 #include <asm/setup.h>
25 #include <asm/machdep.h>
27 #include <asm/pgtable.h>
28 #include <asm/atarihw.h>
29 #include <asm/atari_stram.h>
31 #include <asm/semaphore.h>
36 #define DPRINTK(fmt,args...) printk( fmt, ##args )
38 #define DPRINTK(fmt,args...)
41 #if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)
42 /* abbrev for the && above... */
44 #include <linux/proc_fs.h>
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
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
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
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
;
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)
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
;
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
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)
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
)
186 DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size
, owner
);
189 return alloc_bootmem_low(size
);
191 /* After mem_init(): can only resort to __get_dma_pages() */
192 addr
= (void *)__get_dma_pages(GFP_KERNEL
, get_order(size
));
194 DPRINTK( "atari_stram_alloc: after mem_init, "
195 "get_pages=%p\n", 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 -- "
203 free_pages((unsigned long)addr
, get_order(size
));
206 block
->owner
= owner
;
207 block
->flags
|= flags
;
212 void atari_stram_free( void *addr
)
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) );
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
))
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
);
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
;
253 for( i
= 0; i
< N_STATIC_BLOCKS
; ++i
) {
254 if (static_blocks
[i
].flags
& BLOCK_FREE
) {
255 n
= &static_blocks
[i
];
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
);
265 n
->flags
= BLOCK_KMALLOCED
;
268 printk( KERN_ERR
"Out of memory for ST-RAM descriptor blocks\n" );
274 for( p
= &alloc_list
; *p
; p
= &((*p
)->next
) )
275 if ((*p
)->start
> addr
) break;
283 /* find a region (by start addr) in the alloced list */
284 static BLOCK
*find_region( void *addr
)
288 for( p
= alloc_list
; p
; p
= p
->next
) {
289 if (p
->start
== addr
)
298 /* remove a block from the alloced list */
299 static int remove_region( BLOCK
*block
)
303 for( p
= &alloc_list
; *p
; p
= &((*p
)->next
) )
304 if (*p
== block
) break;
309 if (block
->flags
& BLOCK_KMALLOCED
)
312 block
->flags
|= BLOCK_FREE
;
318 /* ------------------------------------------------------------------------ */
319 /* /proc statistics file stuff */
320 /* ------------------------------------------------------------------------ */
324 #define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args )
326 int get_stram_list( char *buf
)
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
)
337 PRINT_PROC("0x%08lx-0x%08lx: %s (",
338 virt_to_phys(p
->start
),
339 virt_to_phys(p
->start
+p
->size
-1),
341 if (p
->flags
& BLOCK_GFP
)
342 PRINT_PROC( "page-alloced)\n" );
344 PRINT_PROC( "??)\n" );