/*1* arch/m68k/atari/stram.c: Functions for ST-RAM allocations2*3* Copyright 1994-97 Roman Hodek <[email protected]>4*5* This file is subject to the terms and conditions of the GNU General Public6* License. See the file COPYING in the main directory of this archive7* for more details.8*/910#include <linux/types.h>11#include <linux/kernel.h>12#include <linux/mm.h>13#include <linux/kdev_t.h>14#include <linux/major.h>15#include <linux/init.h>16#include <linux/slab.h>17#include <linux/vmalloc.h>18#include <linux/pagemap.h>19#include <linux/bootmem.h>20#include <linux/mount.h>21#include <linux/blkdev.h>22#include <linux/module.h>2324#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>3132#undef DEBUG3334#ifdef DEBUG35#define DPRINTK(fmt,args...) printk( fmt, ##args )36#else37#define DPRINTK(fmt,args...)38#endif3940#if defined(CONFIG_PROC_FS) && defined(CONFIG_STRAM_PROC)41/* abbrev for the && above... */42#define DO_PROC43#include <linux/proc_fs.h>44#include <linux/seq_file.h>45#endif4647/*48* ++roman:49*50* New version of ST-Ram buffer allocation. Instead of using the51* 1 MB - 4 KB that remain when the ST-Ram chunk starts at $100052* (1 MB granularity!), such buffers are reserved like this:53*54* - If the kernel resides in ST-Ram anyway, we can take the buffer55* from behind the current kernel data space the normal way56* (incrementing start_mem).57*58* - If the kernel is in TT-Ram, stram_init() initializes start and59* end of the available region. Buffers are allocated from there60* 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 there62* won't be an overrun of the ST-Ram region by normal kernel data63* space.64*65* For that, ST-Ram may only be allocated while kernel initialization66* is going on, or exactly: before mem_init() is called. There is also67* no provision now for freeing ST-Ram buffers. It seems that isn't68* really needed.69*70*/7172/* Start and end (virtual) of ST-RAM */73static void *stram_start, *stram_end;7475/* set after memory_init() executed and allocations via start_mem aren't76* possible anymore */77static int mem_init_done;7879/* set if kernel is in ST-RAM */80static int kernel_in_stram;8182typedef struct stram_block {83struct stram_block *next;84void *start;85unsigned long size;86unsigned flags;87const char *owner;88} BLOCK;8990/* 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() */9495/* list of allocated blocks */96static BLOCK *alloc_list;9798/* We can't always use kmalloc() to allocate BLOCK structures, since99* stram_alloc() can be called rather early. So we need some pool of100* statically allocated structures. 20 of them is more than enough, so in most101* cases we never should need to call kmalloc(). */102#define N_STATIC_BLOCKS 20103static BLOCK static_blocks[N_STATIC_BLOCKS];104105/***************************** Prototypes *****************************/106107static BLOCK *add_region( void *addr, unsigned long size );108static BLOCK *find_region( void *addr );109static int remove_region( BLOCK *block );110111/************************* End of Prototypes **************************/112113114/* ------------------------------------------------------------------------ */115/* Public Interface */116/* ------------------------------------------------------------------------ */117118/*119* This init function is called very early by atari/config.c120* It initializes some internal variables needed for stram_alloc()121*/122void __init atari_stram_init(void)123{124int i;125126/* initialize static blocks */127for( i = 0; i < N_STATIC_BLOCKS; ++i )128static_blocks[i].flags = BLOCK_FREE;129130/* determine whether kernel code resides in ST-RAM (then ST-RAM is the131* first memory block at virtual 0x0) */132stram_start = phys_to_virt(0);133kernel_in_stram = (stram_start == 0);134135for( i = 0; i < m68k_num_memory; ++i ) {136if (m68k_memory[i].addr == 0) {137/* skip first 2kB or page (supervisor-only!) */138stram_end = stram_start + m68k_memory[i].size;139return;140}141}142/* Should never come here! (There is always ST-Ram!) */143panic( "atari_stram_init: no ST-RAM found!" );144}145146147/*148* This function is called from setup_arch() to reserve the pages needed for149* ST-RAM management.150*/151void __init atari_stram_reserve_pages(void *start_mem)152{153/* always reserve first page of ST-RAM, the first 2 kB are154* supervisor-only! */155if (!kernel_in_stram)156reserve_bootmem(0, PAGE_SIZE, BOOTMEM_DEFAULT);157158}159160void atari_stram_mem_init_hook (void)161{162mem_init_done = 1;163}164165166/*167* This is main public interface: somehow allocate a ST-RAM block168*169* - If we're before mem_init(), we have to make a static allocation. The170* region is taken in the kernel data area (if the kernel is in ST-RAM) or171* from the start of ST-RAM (if the kernel is in TT-RAM) and added to the172* rsvd_stram_* region. The ST-RAM is somewhere in the middle of kernel173* address space in the latter case.174*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 :-(178*179*/180void *atari_stram_alloc(long size, const char *owner)181{182void *addr = NULL;183BLOCK *block;184int flags;185186DPRINTK("atari_stram_alloc(size=%08lx,owner=%s)\n", size, owner);187188if (!mem_init_done)189return alloc_bootmem_low(size);190else {191/* After mem_init(): can only resort to __get_dma_pages() */192addr = (void *)__get_dma_pages(GFP_KERNEL, get_order(size));193flags = BLOCK_GFP;194DPRINTK( "atari_stram_alloc: after mem_init, "195"get_pages=%p\n", addr );196}197198if (addr) {199if (!(block = add_region( addr, size ))) {200/* out of memory for BLOCK structure :-( */201DPRINTK( "atari_stram_alloc: out of mem for BLOCK -- "202"freeing again\n" );203free_pages((unsigned long)addr, get_order(size));204return( NULL );205}206block->owner = owner;207block->flags |= flags;208}209return( addr );210}211EXPORT_SYMBOL(atari_stram_alloc);212213void atari_stram_free( void *addr )214215{216BLOCK *block;217218DPRINTK( "atari_stram_free(addr=%p)\n", addr );219220if (!(block = find_region( addr ))) {221printk( KERN_ERR "Attempt to free non-allocated ST-RAM block at %p "222"from %p\n", addr, __builtin_return_address(0) );223return;224}225DPRINTK( "atari_stram_free: found block (%p): size=%08lx, owner=%s, "226"flags=%02x\n", block, block->size, block->owner, block->flags );227228if (!(block->flags & BLOCK_GFP))229goto fail;230231DPRINTK("atari_stram_free: is kmalloced, order_size=%d\n",232get_order(block->size));233free_pages((unsigned long)addr, get_order(block->size));234remove_region( block );235return;236237fail:238printk( KERN_ERR "atari_stram_free: cannot free block at %p "239"(called from %p)\n", addr, __builtin_return_address(0) );240}241EXPORT_SYMBOL(atari_stram_free);242243244/* ------------------------------------------------------------------------ */245/* Region Management */246/* ------------------------------------------------------------------------ */247248249/* insert a region into the alloced list (sorted) */250static BLOCK *add_region( void *addr, unsigned long size )251{252BLOCK **p, *n = NULL;253int i;254255for( i = 0; i < N_STATIC_BLOCKS; ++i ) {256if (static_blocks[i].flags & BLOCK_FREE) {257n = &static_blocks[i];258n->flags = 0;259break;260}261}262if (!n && mem_init_done) {263/* if statics block pool exhausted and we can call kmalloc() already264* (after mem_init()), try that */265n = kmalloc( sizeof(BLOCK), GFP_KERNEL );266if (n)267n->flags = BLOCK_KMALLOCED;268}269if (!n) {270printk( KERN_ERR "Out of memory for ST-RAM descriptor blocks\n" );271return( NULL );272}273n->start = addr;274n->size = size;275276for( p = &alloc_list; *p; p = &((*p)->next) )277if ((*p)->start > addr) break;278n->next = *p;279*p = n;280281return( n );282}283284285/* find a region (by start addr) in the alloced list */286static BLOCK *find_region( void *addr )287{288BLOCK *p;289290for( p = alloc_list; p; p = p->next ) {291if (p->start == addr)292return( p );293if (p->start > addr)294break;295}296return( NULL );297}298299300/* remove a block from the alloced list */301static int remove_region( BLOCK *block )302{303BLOCK **p;304305for( p = &alloc_list; *p; p = &((*p)->next) )306if (*p == block) break;307if (!*p)308return( 0 );309310*p = block->next;311if (block->flags & BLOCK_KMALLOCED)312kfree( block );313else314block->flags |= BLOCK_FREE;315return( 1 );316}317318319320/* ------------------------------------------------------------------------ */321/* /proc statistics file stuff */322/* ------------------------------------------------------------------------ */323324#ifdef DO_PROC325326#define PRINT_PROC(fmt,args...) seq_printf( m, fmt, ##args )327328static int stram_proc_show(struct seq_file *m, void *v)329{330BLOCK *p;331332PRINT_PROC("Total ST-RAM: %8u kB\n",333(stram_end - stram_start) >> 10);334PRINT_PROC( "Allocated regions:\n" );335for( p = alloc_list; p; p = p->next ) {336PRINT_PROC("0x%08lx-0x%08lx: %s (",337virt_to_phys(p->start),338virt_to_phys(p->start+p->size-1),339p->owner);340if (p->flags & BLOCK_GFP)341PRINT_PROC( "page-alloced)\n" );342else343PRINT_PROC( "??)\n" );344}345346return 0;347}348349static int stram_proc_open(struct inode *inode, struct file *file)350{351return single_open(file, stram_proc_show, NULL);352}353354static const struct file_operations stram_proc_fops = {355.open = stram_proc_open,356.read = seq_read,357.llseek = seq_lseek,358.release = single_release,359};360361static int __init proc_stram_init(void)362{363proc_create("stram", 0, NULL, &stram_proc_fops);364return 0;365}366module_init(proc_stram_init);367#endif368369370/*371* Local variables:372* c-indent-level: 4373* tab-width: 4374* End:375*/376377378