| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> | 
|  | 18 | #include <linux/vmalloc.h> | 
|  | 19 | #include <linux/pagemap.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/bootmem.h> | 
|  | 21 | #include <linux/mount.h> | 
|  | 22 | #include <linux/blkdev.h> | 
| Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 23 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #undef DEBUG | 
|  | 34 |  | 
|  | 35 | #ifdef DEBUG | 
|  | 36 | #define	DPRINTK(fmt,args...) printk( fmt, ##args ) | 
|  | 37 | #else | 
|  | 38 | #define DPRINTK(fmt,args...) | 
|  | 39 | #endif | 
|  | 40 |  | 
|  | 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 | 
|  | 46 |  | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 47 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * ++roman: | 
|  | 49 | * | 
|  | 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: | 
|  | 53 | * | 
|  | 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). | 
|  | 57 | * | 
|  | 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. | 
|  | 64 | * | 
|  | 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. | 
|  | 69 | * | 
|  | 70 | */ | 
|  | 71 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* Start and end (virtual) of ST-RAM */ | 
|  | 73 | static void *stram_start, *stram_end; | 
|  | 74 |  | 
|  | 75 | /* set after memory_init() executed and allocations via start_mem aren't | 
|  | 76 | * possible anymore */ | 
|  | 77 | static int mem_init_done; | 
|  | 78 |  | 
|  | 79 | /* set if kernel is in ST-RAM */ | 
|  | 80 | static int kernel_in_stram; | 
|  | 81 |  | 
|  | 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; | 
|  | 89 |  | 
|  | 90 | /* values for flags field */ | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 91 | #define BLOCK_FREE	0x01	/* free structure in the BLOCKs pool */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #define BLOCK_KMALLOCED	0x02	/* structure allocated by kmalloc() */ | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 93 | #define BLOCK_GFP	0x08	/* block allocated with __get_dma_pages() */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | /* list of allocated blocks */ | 
|  | 96 | static BLOCK *alloc_list; | 
|  | 97 |  | 
|  | 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]; | 
|  | 104 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /***************************** Prototypes *****************************/ | 
|  | 106 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static BLOCK *add_region( void *addr, unsigned long size ); | 
|  | 108 | static BLOCK *find_region( void *addr ); | 
|  | 109 | static int remove_region( BLOCK *block ); | 
|  | 110 |  | 
|  | 111 | /************************* End of Prototypes **************************/ | 
|  | 112 |  | 
|  | 113 |  | 
|  | 114 | /* ------------------------------------------------------------------------ */ | 
|  | 115 | /*							   Public Interface								*/ | 
|  | 116 | /* ------------------------------------------------------------------------ */ | 
|  | 117 |  | 
|  | 118 | /* | 
|  | 119 | * This init function is called very early by atari/config.c | 
|  | 120 | * It initializes some internal variables needed for stram_alloc() | 
|  | 121 | */ | 
|  | 122 | void __init atari_stram_init(void) | 
|  | 123 | { | 
|  | 124 | int i; | 
|  | 125 |  | 
|  | 126 | /* initialize static blocks */ | 
|  | 127 | for( i = 0; i < N_STATIC_BLOCKS; ++i ) | 
|  | 128 | static_blocks[i].flags = BLOCK_FREE; | 
|  | 129 |  | 
|  | 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); | 
|  | 134 |  | 
|  | 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; | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 | /* Should never come here! (There is always ST-Ram!) */ | 
|  | 143 | panic( "atari_stram_init: no ST-RAM found!" ); | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 |  | 
|  | 147 | /* | 
|  | 148 | * This function is called from setup_arch() to reserve the pages needed for | 
|  | 149 | * ST-RAM management. | 
|  | 150 | */ | 
|  | 151 | void __init atari_stram_reserve_pages(void *start_mem) | 
|  | 152 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* always reserve first page of ST-RAM, the first 2 kB are | 
|  | 154 | * supervisor-only! */ | 
|  | 155 | if (!kernel_in_stram) | 
| Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 156 | reserve_bootmem(0, PAGE_SIZE, BOOTMEM_DEFAULT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
|  | 160 | void atari_stram_mem_init_hook (void) | 
|  | 161 | { | 
|  | 162 | mem_init_done = 1; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 |  | 
|  | 166 | /* | 
|  | 167 | * This is main public interface: somehow allocate a ST-RAM block | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * | 
|  | 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. | 
|  | 174 | * | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 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 :-( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * | 
|  | 179 | */ | 
|  | 180 | void *atari_stram_alloc(long size, const char *owner) | 
|  | 181 | { | 
|  | 182 | void *addr = NULL; | 
|  | 183 | BLOCK *block; | 
|  | 184 | int flags; | 
|  | 185 |  | 
|  | 186 | DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner); | 
|  | 187 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (!mem_init_done) | 
|  | 189 | return alloc_bootmem_low(size); | 
|  | 190 | else { | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 191 | /* After mem_init(): can only resort to __get_dma_pages() */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size)); | 
|  | 193 | flags = BLOCK_GFP; | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 194 | DPRINTK( "atari_stram_alloc: after mem_init, " | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | "get_pages=%p\n", addr ); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 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" ); | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 203 | free_pages((unsigned long)addr, get_order(size)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return( NULL ); | 
|  | 205 | } | 
|  | 206 | block->owner = owner; | 
|  | 207 | block->flags |= flags; | 
|  | 208 | } | 
|  | 209 | return( addr ); | 
|  | 210 | } | 
| Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 211 | EXPORT_SYMBOL(atari_stram_alloc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 |  | 
|  | 213 | void 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 Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 228 | if (!(block->flags & BLOCK_GFP)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | goto fail; | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 230 |  | 
|  | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | 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 | } | 
| Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 241 | EXPORT_SYMBOL(atari_stram_free); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 |  | 
|  | 244 | /* ------------------------------------------------------------------------ */ | 
|  | 245 | /*							  Region Management								*/ | 
|  | 246 | /* ------------------------------------------------------------------------ */ | 
|  | 247 |  | 
|  | 248 |  | 
|  | 249 | /* insert a region into the alloced list (sorted) */ | 
|  | 250 | static BLOCK *add_region( void *addr, unsigned long size ) | 
|  | 251 | { | 
|  | 252 | BLOCK **p, *n = NULL; | 
|  | 253 | int i; | 
|  | 254 |  | 
|  | 255 | for( i = 0; i < N_STATIC_BLOCKS; ++i ) { | 
|  | 256 | if (static_blocks[i].flags & BLOCK_FREE) { | 
|  | 257 | n = &static_blocks[i]; | 
|  | 258 | n->flags = 0; | 
|  | 259 | break; | 
|  | 260 | } | 
|  | 261 | } | 
|  | 262 | if (!n && mem_init_done) { | 
|  | 263 | /* if statics block pool exhausted and we can call kmalloc() already | 
|  | 264 | * (after mem_init()), try that */ | 
|  | 265 | n = kmalloc( sizeof(BLOCK), GFP_KERNEL ); | 
|  | 266 | if (n) | 
|  | 267 | n->flags = BLOCK_KMALLOCED; | 
|  | 268 | } | 
|  | 269 | if (!n) { | 
|  | 270 | printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" ); | 
|  | 271 | return( NULL ); | 
|  | 272 | } | 
|  | 273 | n->start = addr; | 
|  | 274 | n->size  = size; | 
|  | 275 |  | 
|  | 276 | for( p = &alloc_list; *p; p = &((*p)->next) ) | 
|  | 277 | if ((*p)->start > addr) break; | 
|  | 278 | n->next = *p; | 
|  | 279 | *p = n; | 
|  | 280 |  | 
|  | 281 | return( n ); | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 |  | 
|  | 285 | /* find a region (by start addr) in the alloced list */ | 
|  | 286 | static BLOCK *find_region( void *addr ) | 
|  | 287 | { | 
|  | 288 | BLOCK *p; | 
|  | 289 |  | 
|  | 290 | for( p = alloc_list; p; p = p->next ) { | 
|  | 291 | if (p->start == addr) | 
|  | 292 | return( p ); | 
|  | 293 | if (p->start > addr) | 
|  | 294 | break; | 
|  | 295 | } | 
|  | 296 | return( NULL ); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 |  | 
|  | 300 | /* remove a block from the alloced list */ | 
|  | 301 | static int remove_region( BLOCK *block ) | 
|  | 302 | { | 
|  | 303 | BLOCK **p; | 
|  | 304 |  | 
|  | 305 | for( p = &alloc_list; *p; p = &((*p)->next) ) | 
|  | 306 | if (*p == block) break; | 
|  | 307 | if (!*p) | 
|  | 308 | return( 0 ); | 
|  | 309 |  | 
|  | 310 | *p = block->next; | 
|  | 311 | if (block->flags & BLOCK_KMALLOCED) | 
|  | 312 | kfree( block ); | 
|  | 313 | else | 
|  | 314 | block->flags |= BLOCK_FREE; | 
|  | 315 | return( 1 ); | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 |  | 
|  | 319 |  | 
|  | 320 | /* ------------------------------------------------------------------------ */ | 
|  | 321 | /*						 /proc statistics file stuff						*/ | 
|  | 322 | /* ------------------------------------------------------------------------ */ | 
|  | 323 |  | 
|  | 324 | #ifdef DO_PROC | 
|  | 325 |  | 
|  | 326 | #define	PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args ) | 
|  | 327 |  | 
|  | 328 | int get_stram_list( char *buf ) | 
|  | 329 | { | 
|  | 330 | int len = 0; | 
|  | 331 | BLOCK *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 |  | 
| Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 333 | PRINT_PROC("Total ST-RAM:      %8u kB\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | (stram_end - stram_start) >> 10); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | PRINT_PROC( "Allocated regions:\n" ); | 
|  | 336 | for( p = alloc_list; p; p = p->next ) { | 
|  | 337 | if (len + 50 >= PAGE_SIZE) | 
|  | 338 | break; | 
|  | 339 | PRINT_PROC("0x%08lx-0x%08lx: %s (", | 
|  | 340 | virt_to_phys(p->start), | 
|  | 341 | virt_to_phys(p->start+p->size-1), | 
|  | 342 | p->owner); | 
|  | 343 | if (p->flags & BLOCK_GFP) | 
|  | 344 | PRINT_PROC( "page-alloced)\n" ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | else | 
|  | 346 | PRINT_PROC( "??)\n" ); | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | return( len ); | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | #endif | 
|  | 353 |  | 
|  | 354 |  | 
|  | 355 | /* | 
|  | 356 | * Local variables: | 
|  | 357 | *  c-indent-level: 4 | 
|  | 358 | *  tab-width: 4 | 
|  | 359 | * End: | 
|  | 360 | */ |