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> |
| 23 | |
| 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> |
| 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) |
| 156 | reserve_bootmem (0, PAGE_SIZE); |
| 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 | } |
| 211 | |
| 212 | void atari_stram_free( void *addr ) |
| 213 | |
| 214 | { |
| 215 | BLOCK *block; |
| 216 | |
| 217 | DPRINTK( "atari_stram_free(addr=%p)\n", addr ); |
| 218 | |
| 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; |
| 223 | } |
| 224 | DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, " |
| 225 | "flags=%02x\n", block, block->size, block->owner, block->flags ); |
| 226 | |
Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 227 | if (!(block->flags & BLOCK_GFP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | goto fail; |
Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 229 | |
| 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)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | remove_region( block ); |
| 234 | return; |
| 235 | |
| 236 | fail: |
| 237 | printk( KERN_ERR "atari_stram_free: cannot free block at %p " |
| 238 | "(called from %p)\n", addr, __builtin_return_address(0) ); |
| 239 | } |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /* ------------------------------------------------------------------------ */ |
| 243 | /* Region Management */ |
| 244 | /* ------------------------------------------------------------------------ */ |
| 245 | |
| 246 | |
| 247 | /* insert a region into the alloced list (sorted) */ |
| 248 | static BLOCK *add_region( void *addr, unsigned long size ) |
| 249 | { |
| 250 | BLOCK **p, *n = NULL; |
| 251 | int i; |
| 252 | |
| 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; |
| 258 | } |
| 259 | } |
| 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; |
| 266 | } |
| 267 | if (!n) { |
| 268 | printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" ); |
| 269 | return( NULL ); |
| 270 | } |
| 271 | n->start = addr; |
| 272 | n->size = size; |
| 273 | |
| 274 | for( p = &alloc_list; *p; p = &((*p)->next) ) |
| 275 | if ((*p)->start > addr) break; |
| 276 | n->next = *p; |
| 277 | *p = n; |
| 278 | |
| 279 | return( n ); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /* find a region (by start addr) in the alloced list */ |
| 284 | static BLOCK *find_region( void *addr ) |
| 285 | { |
| 286 | BLOCK *p; |
| 287 | |
| 288 | for( p = alloc_list; p; p = p->next ) { |
| 289 | if (p->start == addr) |
| 290 | return( p ); |
| 291 | if (p->start > addr) |
| 292 | break; |
| 293 | } |
| 294 | return( NULL ); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /* remove a block from the alloced list */ |
| 299 | static int remove_region( BLOCK *block ) |
| 300 | { |
| 301 | BLOCK **p; |
| 302 | |
| 303 | for( p = &alloc_list; *p; p = &((*p)->next) ) |
| 304 | if (*p == block) break; |
| 305 | if (!*p) |
| 306 | return( 0 ); |
| 307 | |
| 308 | *p = block->next; |
| 309 | if (block->flags & BLOCK_KMALLOCED) |
| 310 | kfree( block ); |
| 311 | else |
| 312 | block->flags |= BLOCK_FREE; |
| 313 | return( 1 ); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | /* ------------------------------------------------------------------------ */ |
| 319 | /* /proc statistics file stuff */ |
| 320 | /* ------------------------------------------------------------------------ */ |
| 321 | |
| 322 | #ifdef DO_PROC |
| 323 | |
| 324 | #define PRINT_PROC(fmt,args...) len += sprintf( buf+len, fmt, ##args ) |
| 325 | |
| 326 | int get_stram_list( char *buf ) |
| 327 | { |
| 328 | int len = 0; |
| 329 | BLOCK *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Hugh Dickins | f9c98d0 | 2005-10-29 18:16:10 -0700 | [diff] [blame] | 331 | PRINT_PROC("Total ST-RAM: %8u kB\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | (stram_end - stram_start) >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | else |
| 344 | PRINT_PROC( "??)\n" ); |
| 345 | } |
| 346 | |
| 347 | return( len ); |
| 348 | } |
| 349 | |
| 350 | #endif |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * Local variables: |
| 355 | * c-indent-level: 4 |
| 356 | * tab-width: 4 |
| 357 | * End: |
| 358 | */ |