Search
lxdream.org :: lxdream/src/mem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/mem.c
changeset 488:d19a63e47cd9
prev480:d28c2992f5ee
next490:1e0f9940e064
author nkeynes
date Sun Nov 04 05:07:49 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Test for presence of linux/cdrom.h at configure time, and only build the
linux driver if it's found successfully
view annotate diff log raw
     1 /**
     2  * $Id: mem.c,v 1.20 2007-11-04 01:03:00 nkeynes Exp $
     3  * mem.c is responsible for creating and maintaining the overall system memory
     4  * map, as visible from the SH4 processor. 
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #define MODULE mem_module
    20 #include <sys/types.h>
    21 #include <sys/mman.h>
    22 #include <sys/stat.h>
    23 #include <assert.h>
    24 #include <stdint.h>
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 #include <unistd.h>
    28 #include <fcntl.h>
    29 #include <errno.h>
    30 #include <string.h>
    31 #include <zlib.h>
    32 #include "dream.h"
    33 #include "mem.h"
    34 #include "mmio.h"
    35 #include "dreamcast.h"
    37 char **page_map = NULL;
    39 int mem_load(FILE *f);
    40 void mem_save(FILE *f);
    41 struct dreamcast_module mem_module =
    42     { "MEM", mem_init, mem_reset, NULL, NULL, NULL, mem_save, mem_load };
    44 struct mem_region mem_rgn[MAX_MEM_REGIONS];
    45 struct mmio_region *io_rgn[MAX_IO_REGIONS];
    46 struct mmio_region *P4_io[4096];
    48 int num_io_rgns = 0, num_mem_rgns = 0;
    50 void *mem_alloc_pages( int n )
    51 {
    52     void *mem = mmap( NULL, n * 4096,
    53                       PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    54     if( mem == MAP_FAILED ) {
    55         ERROR( "Memory allocation failure! (%s)", strerror(errno) );
    56         return NULL;
    57     }
    58     return mem;
    59 }
    62 void mem_init( void )
    63 {
    64     page_map = mmap( NULL, sizeof(char *) * PAGE_TABLE_ENTRIES,
    65                      PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0 );
    66     if( page_map == MAP_FAILED ) {
    67         ERROR( "Unable to allocate page map! (%s)", strerror(errno) );
    68         page_map = NULL;
    69         return;
    70     }
    72     memset( page_map, 0, sizeof(uint32_t) * PAGE_TABLE_ENTRIES );
    73 }
    75 void mem_reset( void )
    76 {
    77     /* Restore all mmio registers to their initial settings */
    78     int i, j;
    79     for( i=1; i<num_io_rgns; i++ ) {
    80         for( j=0; io_rgn[i]->ports[j].id != NULL; j++ ) {
    81             if( io_rgn[i]->ports[j].def_val != UNDEFINED &&
    82                 io_rgn[i]->ports[j].def_val != *io_rgn[i]->ports[j].val ) {
    83                 io_rgn[i]->io_write( io_rgn[i]->ports[j].offset,
    84                                     io_rgn[i]->ports[j].def_val );
    85             }
    86         }
    87     }
    88 }
    90 void mem_save( FILE *f ) 
    91 {
    92     int i;
    93     uint32_t len;
    95     /* All memory regions */
    96     fwrite( &num_mem_rgns, sizeof(num_mem_rgns), 1, f );
    97     for( i=0; i<num_mem_rgns; i++ ) {
    98 	fwrite_string( mem_rgn[i].name, f );
    99 	fwrite( &mem_rgn[i].base, sizeof(uint32_t), 1, f );
   100 	fwrite( &mem_rgn[i].flags, sizeof(int), 1, f );
   101 	fwrite( &mem_rgn[i].size, sizeof(uint32_t), 1, f );
   102 	if( mem_rgn[i].flags != MEM_FLAG_ROM )
   103 	    fwrite_gzip( mem_rgn[i].mem, mem_rgn[i].size, 1, f );
   104     }
   106     /* All MMIO regions */
   107     fwrite( &num_io_rgns, sizeof(num_io_rgns), 1, f );
   108     for( i=0; i<num_io_rgns; i++ ) {
   109 	fwrite_string( io_rgn[i]->id, f );
   110 	fwrite( &io_rgn[i]->base, sizeof( uint32_t ), 1, f );
   111 	len = 4096;
   112 	fwrite( &len, sizeof(len), 1, f );
   113 	fwrite_gzip( io_rgn[i]->mem, len, 1, f );
   114     }
   115 }
   117 int mem_load( FILE *f )
   118 {
   119     char tmp[64];
   120     uint32_t len;
   121     uint32_t base, size;
   122     int flags;
   123     int i;
   125     /* All memory regions */
   126     fread( &len, sizeof(len), 1, f );
   127     if( len != num_mem_rgns )
   128 	return -1;
   129     for( i=0; i<len; i++ ) {
   130 	fread_string( tmp, sizeof(tmp), f );
   131 	fread( &base, sizeof(base), 1, f );
   132 	fread( &flags, sizeof(flags), 1, f );
   133 	fread( &size, sizeof(size), 1, f );
   134 	if( strcmp( mem_rgn[i].name, tmp ) != 0 ||
   135 	    base != mem_rgn[i].base ||
   136 	    flags != mem_rgn[i].flags ||
   137 	    size != mem_rgn[i].size ) {
   138 	    ERROR( "Bad memory region %d %s", i, tmp );
   139 	    return -1;
   140 	}
   141 	if( flags != MEM_FLAG_ROM )
   142 	    fread_gzip( mem_rgn[i].mem, size, 1, f );
   143     }
   145     /* All MMIO regions */
   146     fread( &len, sizeof(len), 1, f );
   147     if( len != num_io_rgns ) 
   148 	return -1;
   149     for( i=0; i<len; i++ ) {
   150 	fread_string( tmp, sizeof(tmp), f );
   151 	fread( &base, sizeof(base), 1, f );
   152 	fread( &size, sizeof(size), 1, f );
   153 	if( strcmp( io_rgn[i]->id, tmp ) != 0 ||
   154 	    base != io_rgn[i]->base ||
   155 	    size != 4096 ) {
   156 	    ERROR( "Bad MMIO region %d %s", i, tmp );
   157 	    return -1;
   158 	}
   159 	fread_gzip( io_rgn[i]->mem, size, 1, f );
   160     }
   161     return 0;
   162 }
   164 int mem_save_block( const gchar *file, uint32_t start, uint32_t length )
   165 {
   166     char *region;
   167     int len = 4096, total = 0;
   168     uint32_t addr = start;
   169     FILE *f = fopen(file,"w");
   171     if( f == NULL )
   172 	return errno;
   174     while( total < length ) {
   175 	region = mem_get_region(addr);
   176 	len = 4096 - (addr & 0x0FFF);
   177 	if( len > (length-total) ) 
   178 	    len = (length-total);
   179 	if( fwrite( region, len, 1, f ) != 1 ) {
   180 	    ERROR( "Unexpected error: %d %d", len, errno );
   181 	    break;
   182 	}
   184 	addr += len;
   185 	total += len;
   186     }
   187     fclose( f );
   188     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   189     return 0;
   190 }
   192 int mem_load_block( const gchar *file, uint32_t start, uint32_t length )
   193 {
   194     char *region;
   195     int len = 4096, total = 0;
   196     uint32_t addr = start;
   197     struct stat st;
   198     FILE *f = fopen(file,"r");
   200     if( f == NULL ) {
   201 	ERROR( "Unable to load file '%s': %s", file, strerror(errno) );
   202 	return -1;
   203     }
   204     fstat( fileno(f), &st );
   205     if( length == 0 || length == -1 || length > st.st_size )
   206 	length = st.st_size;
   208     while( total < length ) {
   209 	region = mem_get_region(addr);
   210 	len = 4096 - (addr & 0x0FFF);
   211 	if( len > (length-total) ) 
   212 	    len = (length-total);
   213 	if( fread( region, len, 1, f ) != 1 ) {
   214 	    ERROR( "Unexpected error: %d %d", len, errno );
   215 	    break;
   216 	}
   218 	addr += len;
   219 	total += len;
   220     }
   221     fclose( f );
   222     INFO( "Loaded %d of %d bytes to %08X", total, length, start );
   223     return 0;
   224 }
   226 struct mem_region *mem_map_region( void *mem, uint32_t base, uint32_t size,
   227                                    const char *name, int flags, uint32_t repeat_offset,
   228 				   uint32_t repeat_until )
   229 {
   230     int i;
   231     mem_rgn[num_mem_rgns].base = base;
   232     mem_rgn[num_mem_rgns].size = size;
   233     mem_rgn[num_mem_rgns].flags = flags;
   234     mem_rgn[num_mem_rgns].name = name;
   235     mem_rgn[num_mem_rgns].mem = mem;
   236     num_mem_rgns++;
   238     do {
   239 	for( i=0; i<size>>PAGE_BITS; i++ )
   240 	    page_map[(base>>PAGE_BITS)+i] = mem + (i<<PAGE_BITS);
   241 	base += repeat_offset;	
   242     } while( base <= repeat_until );
   244     return &mem_rgn[num_mem_rgns-1];
   245 }
   247 void *mem_create_ram_region( uint32_t base, uint32_t size, const char *name )
   248 {
   249     return mem_create_repeating_ram_region( base, size, name, size, base );
   250 }
   252 void *mem_create_repeating_ram_region( uint32_t base, uint32_t size, const char *name,
   253 				       uint32_t repeat_offset, uint32_t repeat_until )
   254 {
   255     char *mem;
   257     assert( (base&0xFFFFF000) == base ); /* must be page aligned */
   258     assert( (size&0x00000FFF) == 0 );
   259     assert( num_mem_rgns < MAX_MEM_REGIONS );
   260     assert( page_map != NULL );
   262     mem = mem_alloc_pages( size>>PAGE_BITS );
   264     mem_map_region( mem, base, size, name, MEM_FLAG_RAM, repeat_offset, repeat_until );
   266     return mem;
   267 }
   269 void *mem_load_rom( const gchar *file, uint32_t base, uint32_t size, uint32_t crc,
   270 		    const gchar *region_name )
   271 {
   272     char *mem;
   273     uint32_t calc_crc;
   274     int status;
   276     mem = mem_get_region(base);
   277     if( mem == NULL ) {
   278 	mem = mmap( NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0 );
   279 	if( mem == MAP_FAILED ) {
   280 	    ERROR( "Unable to allocate ROM memory: %s (%s)", file, strerror(errno) );
   281 	    return NULL;
   282 	}
   283 	mem_map_region( mem, base, size, file, MEM_FLAG_ROM, size, base );
   284     } else {
   285 	mprotect( mem, size, PROT_READ|PROT_WRITE );
   286     }
   288     status = mem_load_block( file, base, size );
   289     mprotect( mem, size, PROT_READ );
   291     if( status == 0 ) {
   292 	/* CRC check only if we loaded something */
   293 	calc_crc = crc32(0L, (unsigned char *)mem, size);
   294 	if( calc_crc != crc ) {
   295 	    WARN( "Bios CRC Mismatch in %s: %08X (expected %08X)",
   296 		  file, calc_crc, crc);
   297 	}
   298     }
   300     return mem;
   301 }
   303 char *mem_get_region_by_name( const char *name )
   304 {
   305     int i;
   306     for( i=0; i<num_mem_rgns; i++ ) {
   307         if( strcmp( mem_rgn[i].name, name ) == 0 )
   308             return mem_rgn[i].mem;
   309     }
   310     return NULL;
   311 }
   313 void register_io_region( struct mmio_region *io )
   314 {
   315     int i;
   317     assert(io);
   318     io->mem = mem_alloc_pages(2);
   319     io->save_mem = io->mem + PAGE_SIZE;
   320     io->index = (struct mmio_port **)malloc(1024*sizeof(struct mmio_port *));
   321     io->trace_flag = 0;
   322     memset( io->index, 0, 1024*sizeof(struct mmio_port *) );
   323     for( i=0; io->ports[i].id != NULL; i++ ) {
   324         io->ports[i].val = (uint32_t *)(io->mem + io->ports[i].offset);
   325         *io->ports[i].val = io->ports[i].def_val;
   326         io->index[io->ports[i].offset>>2] = &io->ports[i];
   327     }
   328     memcpy( io->save_mem, io->mem, PAGE_SIZE );
   329     if( (io->base & 0xFF000000) == 0xFF000000 ) {
   330         /* P4 area (on-chip I/O channels */
   331         P4_io[(io->base&0x1FFFFFFF)>>19] = io;
   332     } else {
   333         page_map[io->base>>12] = (char *)num_io_rgns;
   334     }
   335     io_rgn[num_io_rgns] = io;
   336     num_io_rgns++;
   337 }
   339 void register_io_regions( struct mmio_region **io )
   340 {
   341     while( *io ) register_io_region( *io++ );
   342 }
   344 int mem_has_page( uint32_t addr )
   345 {
   346     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   347     return page != NULL;
   348 }
   350 char *mem_get_page( uint32_t addr )
   351 {
   352     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   353     return page;
   354 }
   356 char *mem_get_region( uint32_t addr )
   357 {
   358     char *page = page_map[ (addr & 0x1FFFFFFF) >> 12 ];
   359     if( ((uint32_t)page) < MAX_IO_REGIONS ) { /* IO Region */
   360         return NULL;
   361     } else {
   362         return page+(addr&0xFFF);
   363     }
   364 }
   366 struct mmio_region *mem_get_io_region( uint32_t addr )
   367 {
   368     if( addr > 0xFF000000 ) {
   369 	return P4_io[(addr&0x00FFFFFF)>>12];
   370     }
   371     char *page = page_map[(addr&0x1FFFFFFF)>>12];
   372     if( ((uint32_t)page) < MAX_IO_REGIONS ) {
   373 	return io_rgn[(uint32_t)page];
   374     } else {
   375 	return NULL;
   376     }
   377 }
   379 void mem_set_trace( uint32_t addr, int flag )
   380 {
   381     struct mmio_region *region = mem_get_io_region(addr);
   382     if( region != NULL )
   383 	region->trace_flag = flag;
   384 }
.