Search
lxdream.org :: lxdream/src/sh4/mmu.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/mmu.c
changeset 586:2a3ba82cf243
prev550:a27e31340147
next597:87cbdf62aa35
author nkeynes
date Tue Jan 15 20:50:23 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Merged lxdream-mmu r570:596 to trunk
file annotate diff log raw
nkeynes@550
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@550
     3
 * 
nkeynes@550
     4
 * MMU implementation
nkeynes@550
     5
 *
nkeynes@550
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@550
     7
 *
nkeynes@550
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@550
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@550
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@550
    11
 * (at your option) any later version.
nkeynes@550
    12
 *
nkeynes@550
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@550
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@550
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@550
    16
 * GNU General Public License for more details.
nkeynes@550
    17
 */
nkeynes@550
    18
#define MODULE sh4_module
nkeynes@550
    19
nkeynes@550
    20
#include <stdio.h>
nkeynes@550
    21
#include "sh4/sh4mmio.h"
nkeynes@550
    22
#include "sh4/sh4core.h"
nkeynes@550
    23
#include "mem.h"
nkeynes@550
    24
nkeynes@586
    25
#define VMA_TO_EXT_ADDR(vma) ((vma)&0x1FFFFFFF)
nkeynes@586
    26
nkeynes@586
    27
/* The MMU (practically unique in the system) is allowed to raise exceptions
nkeynes@586
    28
 * directly, with a return code indicating that one was raised and the caller
nkeynes@586
    29
 * had better behave appropriately.
nkeynes@586
    30
 */
nkeynes@586
    31
#define RAISE_TLB_ERROR(code, vpn) \
nkeynes@586
    32
    MMIO_WRITE(MMU, TEA, vpn); \
nkeynes@586
    33
    MMIO_WRITE(MMU, PTEH, ((MMIO_READ(MMU, PTEH) & 0x000003FF) | (vpn&0xFFFFFC00))); \
nkeynes@586
    34
    sh4_raise_tlb_exception(code);
nkeynes@586
    35
nkeynes@586
    36
#define RAISE_MEM_ERROR(code, vpn) \
nkeynes@586
    37
    MMIO_WRITE(MMU, TEA, vpn); \
nkeynes@586
    38
    MMIO_WRITE(MMU, PTEH, ((MMIO_READ(MMU, PTEH) & 0x000003FF) | (vpn&0xFFFFFC00))); \
nkeynes@586
    39
    sh4_raise_exception(code);
nkeynes@586
    40
nkeynes@586
    41
#define RAISE_OTHER_ERROR(code) \
nkeynes@586
    42
    sh4_raise_exception(code);
nkeynes@586
    43
/**
nkeynes@586
    44
 * Abort with a non-MMU address error. Caused by user-mode code attempting
nkeynes@586
    45
 * to access privileged regions, or alignment faults.
nkeynes@586
    46
 */
nkeynes@586
    47
#define MMU_READ_ADDR_ERROR() RAISE_OTHER_ERROR(EXC_DATA_ADDR_READ)
nkeynes@586
    48
#define MMU_WRITE_ADDR_ERROR() RAISE_OTHER_ERROR(EXC_DATA_ADDR_WRITE)
nkeynes@586
    49
nkeynes@586
    50
#define MMU_TLB_READ_MISS_ERROR(vpn) RAISE_TLB_ERROR(EXC_TLB_MISS_READ, vpn)
nkeynes@586
    51
#define MMU_TLB_WRITE_MISS_ERROR(vpn) RAISE_TLB_ERROR(EXC_TLB_MISS_WRITE, vpn)
nkeynes@586
    52
#define MMU_TLB_INITIAL_WRITE_ERROR(vpn) RAISE_MEM_ERROR(EXC_INIT_PAGE_WRITE, vpn)
nkeynes@586
    53
#define MMU_TLB_READ_PROT_ERROR(vpn) RAISE_MEM_ERROR(EXC_TLB_PROT_READ, vpn)
nkeynes@586
    54
#define MMU_TLB_WRITE_PROT_ERROR(vpn) RAISE_MEM_ERROR(EXC_TLB_PROT_WRITE, vpn)
nkeynes@586
    55
#define MMU_TLB_MULTI_HIT_ERROR(vpn) sh4_raise_reset(EXC_TLB_MULTI_HIT); \
nkeynes@586
    56
    MMIO_WRITE(MMU, TEA, vpn); \
nkeynes@586
    57
    MMIO_WRITE(MMU, PTEH, ((MMIO_READ(MMU, PTEH) & 0x000003FF) | (vpn&0xFFFFFC00)));
nkeynes@586
    58
nkeynes@586
    59
nkeynes@550
    60
#define OCRAM_START (0x1C000000>>PAGE_BITS)
nkeynes@550
    61
#define OCRAM_END   (0x20000000>>PAGE_BITS)
nkeynes@550
    62
nkeynes@550
    63
#define ITLB_ENTRY_COUNT 4
nkeynes@550
    64
#define UTLB_ENTRY_COUNT 64
nkeynes@550
    65
nkeynes@550
    66
/* Entry address */
nkeynes@550
    67
#define TLB_VALID     0x00000100
nkeynes@550
    68
#define TLB_USERMODE  0x00000040
nkeynes@550
    69
#define TLB_WRITABLE  0x00000020
nkeynes@586
    70
#define TLB_USERWRITABLE (TLB_WRITABLE|TLB_USERMODE)
nkeynes@550
    71
#define TLB_SIZE_MASK 0x00000090
nkeynes@550
    72
#define TLB_SIZE_1K   0x00000000
nkeynes@550
    73
#define TLB_SIZE_4K   0x00000010
nkeynes@550
    74
#define TLB_SIZE_64K  0x00000080
nkeynes@550
    75
#define TLB_SIZE_1M   0x00000090
nkeynes@550
    76
#define TLB_CACHEABLE 0x00000008
nkeynes@550
    77
#define TLB_DIRTY     0x00000004
nkeynes@550
    78
#define TLB_SHARE     0x00000002
nkeynes@550
    79
#define TLB_WRITETHRU 0x00000001
nkeynes@550
    80
nkeynes@586
    81
#define MASK_1K  0xFFFFFC00
nkeynes@586
    82
#define MASK_4K  0xFFFFF000
nkeynes@586
    83
#define MASK_64K 0xFFFF0000
nkeynes@586
    84
#define MASK_1M  0xFFF00000
nkeynes@550
    85
nkeynes@550
    86
struct itlb_entry {
nkeynes@550
    87
    sh4addr_t vpn; // Virtual Page Number
nkeynes@550
    88
    uint32_t asid; // Process ID
nkeynes@586
    89
    uint32_t mask;
nkeynes@550
    90
    sh4addr_t ppn; // Physical Page Number
nkeynes@550
    91
    uint32_t flags;
nkeynes@550
    92
};
nkeynes@550
    93
nkeynes@550
    94
struct utlb_entry {
nkeynes@550
    95
    sh4addr_t vpn; // Virtual Page Number
nkeynes@586
    96
    uint32_t mask; // Page size mask
nkeynes@550
    97
    uint32_t asid; // Process ID
nkeynes@550
    98
    sh4addr_t ppn; // Physical Page Number
nkeynes@550
    99
    uint32_t flags;
nkeynes@550
   100
    uint32_t pcmcia; // extra pcmcia data - not used
nkeynes@550
   101
};
nkeynes@550
   102
nkeynes@550
   103
static struct itlb_entry mmu_itlb[ITLB_ENTRY_COUNT];
nkeynes@550
   104
static struct utlb_entry mmu_utlb[UTLB_ENTRY_COUNT];
nkeynes@550
   105
static uint32_t mmu_urc;
nkeynes@550
   106
static uint32_t mmu_urb;
nkeynes@550
   107
static uint32_t mmu_lrui;
nkeynes@586
   108
static uint32_t mmu_asid; // current asid
nkeynes@550
   109
nkeynes@550
   110
static sh4ptr_t cache = NULL;
nkeynes@550
   111
nkeynes@550
   112
static void mmu_invalidate_tlb();
nkeynes@550
   113
nkeynes@550
   114
nkeynes@586
   115
static uint32_t get_mask_for_flags( uint32_t flags )
nkeynes@586
   116
{
nkeynes@586
   117
    switch( flags & TLB_SIZE_MASK ) {
nkeynes@586
   118
    case TLB_SIZE_1K: return MASK_1K;
nkeynes@586
   119
    case TLB_SIZE_4K: return MASK_4K;
nkeynes@586
   120
    case TLB_SIZE_64K: return MASK_64K;
nkeynes@586
   121
    case TLB_SIZE_1M: return MASK_1M;
nkeynes@586
   122
    }
nkeynes@586
   123
}
nkeynes@586
   124
nkeynes@550
   125
int32_t mmio_region_MMU_read( uint32_t reg )
nkeynes@550
   126
{
nkeynes@550
   127
    switch( reg ) {
nkeynes@550
   128
    case MMUCR:
nkeynes@550
   129
	return MMIO_READ( MMU, MMUCR) | (mmu_urc<<10) | (mmu_urb<<18) | (mmu_lrui<<26);
nkeynes@550
   130
    default:
nkeynes@550
   131
	return MMIO_READ( MMU, reg );
nkeynes@550
   132
    }
nkeynes@550
   133
}
nkeynes@550
   134
nkeynes@550
   135
void mmio_region_MMU_write( uint32_t reg, uint32_t val )
nkeynes@550
   136
{
nkeynes@586
   137
    uint32_t tmp;
nkeynes@550
   138
    switch(reg) {
nkeynes@550
   139
    case PTEH:
nkeynes@550
   140
	val &= 0xFFFFFCFF;
nkeynes@586
   141
	if( (val & 0xFF) != mmu_asid ) {
nkeynes@586
   142
	    mmu_asid = val&0xFF;
nkeynes@586
   143
	    sh4_icache.page_vma = -1; // invalidate icache as asid has changed
nkeynes@586
   144
	}
nkeynes@550
   145
	break;
nkeynes@550
   146
    case PTEL:
nkeynes@550
   147
	val &= 0x1FFFFDFF;
nkeynes@550
   148
	break;
nkeynes@550
   149
    case PTEA:
nkeynes@550
   150
	val &= 0x0000000F;
nkeynes@550
   151
	break;
nkeynes@550
   152
    case MMUCR:
nkeynes@550
   153
	if( val & MMUCR_TI ) {
nkeynes@550
   154
	    mmu_invalidate_tlb();
nkeynes@550
   155
	}
nkeynes@550
   156
	mmu_urc = (val >> 10) & 0x3F;
nkeynes@550
   157
	mmu_urb = (val >> 18) & 0x3F;
nkeynes@550
   158
	mmu_lrui = (val >> 26) & 0x3F;
nkeynes@550
   159
	val &= 0x00000301;
nkeynes@586
   160
	tmp = MMIO_READ( MMU, MMUCR );
nkeynes@586
   161
	if( ((val ^ tmp) & MMUCR_AT) && sh4_is_using_xlat() ) {
nkeynes@586
   162
	    // AT flag has changed state - flush the xlt cache as all bets
nkeynes@586
   163
	    // are off now. We also need to force an immediate exit from the
nkeynes@586
   164
	    // current block
nkeynes@586
   165
	    MMIO_WRITE( MMU, MMUCR, val );
nkeynes@586
   166
	    sh4_translate_flush_cache();
nkeynes@586
   167
	}
nkeynes@550
   168
	break;
nkeynes@550
   169
    case CCR:
nkeynes@550
   170
	mmu_set_cache_mode( val & (CCR_OIX|CCR_ORA) );
nkeynes@550
   171
	break;
nkeynes@550
   172
    default:
nkeynes@550
   173
	break;
nkeynes@550
   174
    }
nkeynes@550
   175
    MMIO_WRITE( MMU, reg, val );
nkeynes@550
   176
}
nkeynes@550
   177
nkeynes@550
   178
nkeynes@550
   179
void MMU_init() 
nkeynes@550
   180
{
nkeynes@550
   181
    cache = mem_alloc_pages(2);
nkeynes@550
   182
}
nkeynes@550
   183
nkeynes@550
   184
void MMU_reset()
nkeynes@550
   185
{
nkeynes@550
   186
    mmio_region_MMU_write( CCR, 0 );
nkeynes@586
   187
    mmio_region_MMU_write( MMUCR, 0 );
nkeynes@550
   188
}
nkeynes@550
   189
nkeynes@550
   190
void MMU_save_state( FILE *f )
nkeynes@550
   191
{
nkeynes@550
   192
    fwrite( cache, 4096, 2, f );
nkeynes@550
   193
    fwrite( &mmu_itlb, sizeof(mmu_itlb), 1, f );
nkeynes@550
   194
    fwrite( &mmu_utlb, sizeof(mmu_utlb), 1, f );
nkeynes@586
   195
    fwrite( &mmu_urc, sizeof(mmu_urc), 1, f );
nkeynes@586
   196
    fwrite( &mmu_urb, sizeof(mmu_urb), 1, f );
nkeynes@586
   197
    fwrite( &mmu_lrui, sizeof(mmu_lrui), 1, f );
nkeynes@586
   198
    fwrite( &mmu_asid, sizeof(mmu_asid), 1, f );
nkeynes@550
   199
}
nkeynes@550
   200
nkeynes@550
   201
int MMU_load_state( FILE *f )
nkeynes@550
   202
{
nkeynes@550
   203
    /* Setup the cache mode according to the saved register value
nkeynes@550
   204
     * (mem_load runs before this point to load all MMIO data)
nkeynes@550
   205
     */
nkeynes@550
   206
    mmio_region_MMU_write( CCR, MMIO_READ(MMU, CCR) );
nkeynes@550
   207
    if( fread( cache, 4096, 2, f ) != 2 ) {
nkeynes@550
   208
	return 1;
nkeynes@550
   209
    }
nkeynes@550
   210
    if( fread( &mmu_itlb, sizeof(mmu_itlb), 1, f ) != 1 ) {
nkeynes@550
   211
	return 1;
nkeynes@550
   212
    }
nkeynes@550
   213
    if( fread( &mmu_utlb, sizeof(mmu_utlb), 1, f ) != 1 ) {
nkeynes@550
   214
	return 1;
nkeynes@550
   215
    }
nkeynes@586
   216
    if( fread( &mmu_urc, sizeof(mmu_urc), 1, f ) != 1 ) {
nkeynes@586
   217
	return 1;
nkeynes@586
   218
    }
nkeynes@586
   219
    if( fread( &mmu_urc, sizeof(mmu_urb), 1, f ) != 1 ) {
nkeynes@586
   220
	return 1;
nkeynes@586
   221
    }
nkeynes@586
   222
    if( fread( &mmu_lrui, sizeof(mmu_lrui), 1, f ) != 1 ) {
nkeynes@586
   223
	return 1;
nkeynes@586
   224
    }
nkeynes@586
   225
    if( fread( &mmu_asid, sizeof(mmu_asid), 1, f ) != 1 ) {
nkeynes@586
   226
    	return 1;
nkeynes@586
   227
    }
nkeynes@550
   228
    return 0;
nkeynes@550
   229
}
nkeynes@550
   230
nkeynes@550
   231
void mmu_set_cache_mode( int mode )
nkeynes@550
   232
{
nkeynes@550
   233
    uint32_t i;
nkeynes@550
   234
    switch( mode ) {
nkeynes@550
   235
        case MEM_OC_INDEX0: /* OIX=0 */
nkeynes@550
   236
            for( i=OCRAM_START; i<OCRAM_END; i++ )
nkeynes@550
   237
                page_map[i] = cache + ((i&0x02)<<(PAGE_BITS-1));
nkeynes@550
   238
            break;
nkeynes@550
   239
        case MEM_OC_INDEX1: /* OIX=1 */
nkeynes@550
   240
            for( i=OCRAM_START; i<OCRAM_END; i++ )
nkeynes@550
   241
                page_map[i] = cache + ((i&0x02000000)>>(25-PAGE_BITS));
nkeynes@550
   242
            break;
nkeynes@550
   243
        default: /* disabled */
nkeynes@550
   244
            for( i=OCRAM_START; i<OCRAM_END; i++ )
nkeynes@550
   245
                page_map[i] = NULL;
nkeynes@550
   246
            break;
nkeynes@550
   247
    }
nkeynes@550
   248
}
nkeynes@550
   249
nkeynes@550
   250
/* TLB maintanence */
nkeynes@550
   251
nkeynes@550
   252
/**
nkeynes@550
   253
 * LDTLB instruction implementation. Copies PTEH, PTEL and PTEA into the UTLB
nkeynes@550
   254
 * entry identified by MMUCR.URC. Does not modify MMUCR or the ITLB.
nkeynes@550
   255
 */
nkeynes@550
   256
void MMU_ldtlb()
nkeynes@550
   257
{
nkeynes@550
   258
    mmu_utlb[mmu_urc].vpn = MMIO_READ(MMU, PTEH) & 0xFFFFFC00;
nkeynes@550
   259
    mmu_utlb[mmu_urc].asid = MMIO_READ(MMU, PTEH) & 0x000000FF;
nkeynes@550
   260
    mmu_utlb[mmu_urc].ppn = MMIO_READ(MMU, PTEL) & 0x1FFFFC00;
nkeynes@550
   261
    mmu_utlb[mmu_urc].flags = MMIO_READ(MMU, PTEL) & 0x00001FF;
nkeynes@550
   262
    mmu_utlb[mmu_urc].pcmcia = MMIO_READ(MMU, PTEA);
nkeynes@586
   263
    mmu_utlb[mmu_urc].mask = get_mask_for_flags(mmu_utlb[mmu_urc].flags);
nkeynes@550
   264
}
nkeynes@550
   265
nkeynes@550
   266
static void mmu_invalidate_tlb()
nkeynes@550
   267
{
nkeynes@550
   268
    int i;
nkeynes@550
   269
    for( i=0; i<ITLB_ENTRY_COUNT; i++ ) {
nkeynes@550
   270
	mmu_itlb[i].flags &= (~TLB_VALID);
nkeynes@550
   271
    }
nkeynes@550
   272
    for( i=0; i<UTLB_ENTRY_COUNT; i++ ) {
nkeynes@550
   273
	mmu_utlb[i].flags &= (~TLB_VALID);
nkeynes@550
   274
    }
nkeynes@550
   275
}
nkeynes@550
   276
nkeynes@550
   277
#define ITLB_ENTRY(addr) ((addr>>7)&0x03)
nkeynes@550
   278
nkeynes@550
   279
int32_t mmu_itlb_addr_read( sh4addr_t addr )
nkeynes@550
   280
{
nkeynes@550
   281
    struct itlb_entry *ent = &mmu_itlb[ITLB_ENTRY(addr)];
nkeynes@550
   282
    return ent->vpn | ent->asid | (ent->flags & TLB_VALID);
nkeynes@550
   283
}
nkeynes@550
   284
int32_t mmu_itlb_data_read( sh4addr_t addr )
nkeynes@550
   285
{
nkeynes@550
   286
    struct itlb_entry *ent = &mmu_itlb[ITLB_ENTRY(addr)];
nkeynes@550
   287
    return ent->ppn | ent->flags;
nkeynes@550
   288
}
nkeynes@550
   289
nkeynes@550
   290
void mmu_itlb_addr_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   291
{
nkeynes@550
   292
    struct itlb_entry *ent = &mmu_itlb[ITLB_ENTRY(addr)];
nkeynes@550
   293
    ent->vpn = val & 0xFFFFFC00;
nkeynes@550
   294
    ent->asid = val & 0x000000FF;
nkeynes@550
   295
    ent->flags = (ent->flags & ~(TLB_VALID)) | (val&TLB_VALID);
nkeynes@550
   296
}
nkeynes@550
   297
nkeynes@550
   298
void mmu_itlb_data_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   299
{
nkeynes@550
   300
    struct itlb_entry *ent = &mmu_itlb[ITLB_ENTRY(addr)];
nkeynes@550
   301
    ent->ppn = val & 0x1FFFFC00;
nkeynes@550
   302
    ent->flags = val & 0x00001DA;
nkeynes@586
   303
    ent->mask = get_mask_for_flags(val);
nkeynes@550
   304
}
nkeynes@550
   305
nkeynes@550
   306
#define UTLB_ENTRY(addr) ((addr>>8)&0x3F)
nkeynes@550
   307
#define UTLB_ASSOC(addr) (addr&0x80)
nkeynes@550
   308
#define UTLB_DATA2(addr) (addr&0x00800000)
nkeynes@550
   309
nkeynes@550
   310
int32_t mmu_utlb_addr_read( sh4addr_t addr )
nkeynes@550
   311
{
nkeynes@550
   312
    struct utlb_entry *ent = &mmu_utlb[UTLB_ENTRY(addr)];
nkeynes@550
   313
    return ent->vpn | ent->asid | (ent->flags & TLB_VALID) |
nkeynes@550
   314
	((ent->flags & TLB_DIRTY)<<7);
nkeynes@550
   315
}
nkeynes@550
   316
int32_t mmu_utlb_data_read( sh4addr_t addr )
nkeynes@550
   317
{
nkeynes@550
   318
    struct utlb_entry *ent = &mmu_utlb[UTLB_ENTRY(addr)];
nkeynes@550
   319
    if( UTLB_DATA2(addr) ) {
nkeynes@550
   320
	return ent->pcmcia;
nkeynes@550
   321
    } else {
nkeynes@550
   322
	return ent->ppn | ent->flags;
nkeynes@550
   323
    }
nkeynes@550
   324
}
nkeynes@550
   325
nkeynes@586
   326
/**
nkeynes@586
   327
 * Find a UTLB entry for the associative TLB write - same as the normal
nkeynes@586
   328
 * lookup but ignores the valid bit.
nkeynes@586
   329
 */
nkeynes@586
   330
static inline mmu_utlb_lookup_assoc( uint32_t vpn, uint32_t asid )
nkeynes@586
   331
{
nkeynes@586
   332
    int result = -1;
nkeynes@586
   333
    unsigned int i;
nkeynes@586
   334
    for( i = 0; i < UTLB_ENTRY_COUNT; i++ ) {
nkeynes@586
   335
	if( (mmu_utlb[i].flags & TLB_VALID) &&
nkeynes@586
   336
	    ((mmu_utlb[i].flags & TLB_SHARE) || asid == mmu_utlb[i].asid) && 
nkeynes@586
   337
	    ((mmu_utlb[i].vpn ^ vpn) & mmu_utlb[i].mask) == 0 ) {
nkeynes@586
   338
	    if( result != -1 ) {
nkeynes@586
   339
		fprintf( stderr, "TLB Multi hit: %d %d\n", result, i );
nkeynes@586
   340
		return -2;
nkeynes@586
   341
	    }
nkeynes@586
   342
	    result = i;
nkeynes@586
   343
	}
nkeynes@586
   344
    }
nkeynes@586
   345
    return result;
nkeynes@586
   346
}
nkeynes@586
   347
nkeynes@586
   348
/**
nkeynes@586
   349
 * Find a ITLB entry for the associative TLB write - same as the normal
nkeynes@586
   350
 * lookup but ignores the valid bit.
nkeynes@586
   351
 */
nkeynes@586
   352
static inline mmu_itlb_lookup_assoc( uint32_t vpn, uint32_t asid )
nkeynes@586
   353
{
nkeynes@586
   354
    int result = -1;
nkeynes@586
   355
    unsigned int i;
nkeynes@586
   356
    for( i = 0; i < ITLB_ENTRY_COUNT; i++ ) {
nkeynes@586
   357
	if( (mmu_itlb[i].flags & TLB_VALID) &&
nkeynes@586
   358
	    ((mmu_itlb[i].flags & TLB_SHARE) || asid == mmu_itlb[i].asid) && 
nkeynes@586
   359
	    ((mmu_itlb[i].vpn ^ vpn) & mmu_itlb[i].mask) == 0 ) {
nkeynes@586
   360
	    if( result != -1 ) {
nkeynes@586
   361
		return -2;
nkeynes@586
   362
	    }
nkeynes@586
   363
	    result = i;
nkeynes@586
   364
	}
nkeynes@586
   365
    }
nkeynes@586
   366
    return result;
nkeynes@586
   367
}
nkeynes@586
   368
nkeynes@550
   369
void mmu_utlb_addr_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   370
{
nkeynes@550
   371
    if( UTLB_ASSOC(addr) ) {
nkeynes@586
   372
	int utlb = mmu_utlb_lookup_assoc( val, mmu_asid );
nkeynes@586
   373
	if( utlb >= 0 ) {
nkeynes@586
   374
	    struct utlb_entry *ent = &mmu_utlb[utlb];
nkeynes@586
   375
	    ent->flags = ent->flags & ~(TLB_DIRTY|TLB_VALID);
nkeynes@586
   376
	    ent->flags |= (val & TLB_VALID);
nkeynes@586
   377
	    ent->flags |= ((val & 0x200)>>7);
nkeynes@586
   378
	}
nkeynes@586
   379
nkeynes@586
   380
	int itlb = mmu_itlb_lookup_assoc( val, mmu_asid );
nkeynes@586
   381
	if( itlb >= 0 ) {
nkeynes@586
   382
	    struct itlb_entry *ent = &mmu_itlb[itlb];
nkeynes@586
   383
	    ent->flags = (ent->flags & (~TLB_VALID)) | (val & TLB_VALID);
nkeynes@586
   384
	}
nkeynes@586
   385
nkeynes@586
   386
	if( itlb == -2 || utlb == -2 ) {
nkeynes@586
   387
	    MMU_TLB_MULTI_HIT_ERROR(addr);
nkeynes@586
   388
	    return;
nkeynes@586
   389
	}
nkeynes@550
   390
    } else {
nkeynes@550
   391
	struct utlb_entry *ent = &mmu_utlb[UTLB_ENTRY(addr)];
nkeynes@550
   392
	ent->vpn = (val & 0xFFFFFC00);
nkeynes@550
   393
	ent->asid = (val & 0xFF);
nkeynes@550
   394
	ent->flags = (ent->flags & ~(TLB_DIRTY|TLB_VALID));
nkeynes@550
   395
	ent->flags |= (val & TLB_VALID);
nkeynes@550
   396
	ent->flags |= ((val & 0x200)>>7);
nkeynes@550
   397
    }
nkeynes@550
   398
}
nkeynes@550
   399
nkeynes@550
   400
void mmu_utlb_data_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   401
{
nkeynes@550
   402
    struct utlb_entry *ent = &mmu_utlb[UTLB_ENTRY(addr)];
nkeynes@550
   403
    if( UTLB_DATA2(addr) ) {
nkeynes@550
   404
	ent->pcmcia = val & 0x0000000F;
nkeynes@550
   405
    } else {
nkeynes@550
   406
	ent->ppn = (val & 0x1FFFFC00);
nkeynes@550
   407
	ent->flags = (val & 0x000001FF);
nkeynes@586
   408
	ent->mask = get_mask_for_flags(val);
nkeynes@550
   409
    }
nkeynes@550
   410
}
nkeynes@550
   411
nkeynes@550
   412
/* Cache access - not implemented */
nkeynes@550
   413
nkeynes@550
   414
int32_t mmu_icache_addr_read( sh4addr_t addr )
nkeynes@550
   415
{
nkeynes@550
   416
    return 0; // not implemented
nkeynes@550
   417
}
nkeynes@550
   418
int32_t mmu_icache_data_read( sh4addr_t addr )
nkeynes@550
   419
{
nkeynes@550
   420
    return 0; // not implemented
nkeynes@550
   421
}
nkeynes@550
   422
int32_t mmu_ocache_addr_read( sh4addr_t addr )
nkeynes@550
   423
{
nkeynes@550
   424
    return 0; // not implemented
nkeynes@550
   425
}
nkeynes@550
   426
int32_t mmu_ocache_data_read( sh4addr_t addr )
nkeynes@550
   427
{
nkeynes@550
   428
    return 0; // not implemented
nkeynes@550
   429
}
nkeynes@550
   430
nkeynes@550
   431
void mmu_icache_addr_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   432
{
nkeynes@550
   433
}
nkeynes@550
   434
nkeynes@550
   435
void mmu_icache_data_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   436
{
nkeynes@550
   437
}
nkeynes@550
   438
nkeynes@550
   439
void mmu_ocache_addr_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   440
{
nkeynes@550
   441
}
nkeynes@550
   442
nkeynes@550
   443
void mmu_ocache_data_write( sh4addr_t addr, uint32_t val )
nkeynes@550
   444
{
nkeynes@550
   445
}
nkeynes@586
   446
nkeynes@586
   447
/******************************************************************************/
nkeynes@586
   448
/*                        MMU TLB address translation                         */
nkeynes@586
   449
/******************************************************************************/
nkeynes@586
   450
nkeynes@586
   451
/**
nkeynes@586
   452
 * The translations are excessively complicated, but unfortunately it's a 
nkeynes@586
   453
 * complicated system. TODO: make this not be painfully slow.
nkeynes@586
   454
 */
nkeynes@586
   455
nkeynes@586
   456
/**
nkeynes@586
   457
 * Perform the actual utlb lookup w/ asid matching.
nkeynes@586
   458
 * Possible utcomes are:
nkeynes@586
   459
 *   0..63 Single match - good, return entry found
nkeynes@586
   460
 *   -1 No match - raise a tlb data miss exception
nkeynes@586
   461
 *   -2 Multiple matches - raise a multi-hit exception (reset)
nkeynes@586
   462
 * @param vpn virtual address to resolve
nkeynes@586
   463
 * @return the resultant UTLB entry, or an error.
nkeynes@586
   464
 */
nkeynes@586
   465
static inline int mmu_utlb_lookup_vpn_asid( uint32_t vpn )
nkeynes@586
   466
{
nkeynes@586
   467
    int result = -1;
nkeynes@586
   468
    unsigned int i;
nkeynes@586
   469
nkeynes@586
   470
    mmu_urc++;
nkeynes@586
   471
    if( mmu_urc == mmu_urb || mmu_urc == 0x40 ) {
nkeynes@586
   472
	mmu_urc = 0;
nkeynes@586
   473
    }
nkeynes@586
   474
nkeynes@586
   475
    for( i = 0; i < UTLB_ENTRY_COUNT; i++ ) {
nkeynes@586
   476
	if( (mmu_utlb[i].flags & TLB_VALID) &&
nkeynes@586
   477
	    ((mmu_utlb[i].flags & TLB_SHARE) || mmu_asid == mmu_utlb[i].asid) && 
nkeynes@586
   478
	    ((mmu_utlb[i].vpn ^ vpn) & mmu_utlb[i].mask) == 0 ) {
nkeynes@586
   479
	    if( result != -1 ) {
nkeynes@586
   480
		return -2;
nkeynes@586
   481
	    }
nkeynes@586
   482
	    result = i;
nkeynes@586
   483
	}
nkeynes@586
   484
    }
nkeynes@586
   485
    return result;
nkeynes@586
   486
}
nkeynes@586
   487
nkeynes@586
   488
/**
nkeynes@586
   489
 * Perform the actual utlb lookup matching on vpn only
nkeynes@586
   490
 * Possible utcomes are:
nkeynes@586
   491
 *   0..63 Single match - good, return entry found
nkeynes@586
   492
 *   -1 No match - raise a tlb data miss exception
nkeynes@586
   493
 *   -2 Multiple matches - raise a multi-hit exception (reset)
nkeynes@586
   494
 * @param vpn virtual address to resolve
nkeynes@586
   495
 * @return the resultant UTLB entry, or an error.
nkeynes@586
   496
 */
nkeynes@586
   497
static inline int mmu_utlb_lookup_vpn( uint32_t vpn )
nkeynes@586
   498
{
nkeynes@586
   499
    int result = -1;
nkeynes@586
   500
    unsigned int i;
nkeynes@586
   501
nkeynes@586
   502
    mmu_urc++;
nkeynes@586
   503
    if( mmu_urc == mmu_urb || mmu_urc == 0x40 ) {
nkeynes@586
   504
	mmu_urc = 0;
nkeynes@586
   505
    }
nkeynes@586
   506
nkeynes@586
   507
    for( i = 0; i < UTLB_ENTRY_COUNT; i++ ) {
nkeynes@586
   508
	if( (mmu_utlb[i].flags & TLB_VALID) &&
nkeynes@586
   509
	    ((mmu_utlb[i].vpn ^ vpn) & mmu_utlb[i].mask) == 0 ) {
nkeynes@586
   510
	    if( result != -1 ) {
nkeynes@586
   511
		return -2;
nkeynes@586
   512
	    }
nkeynes@586
   513
	    result = i;
nkeynes@586
   514
	}
nkeynes@586
   515
    }
nkeynes@586
   516
nkeynes@586
   517
    return result;
nkeynes@586
   518
}
nkeynes@586
   519
nkeynes@586
   520
/**
nkeynes@586
   521
 * Update the ITLB by replacing the LRU entry with the specified UTLB entry.
nkeynes@586
   522
 * @return the number (0-3) of the replaced entry.
nkeynes@586
   523
 */
nkeynes@586
   524
static int inline mmu_itlb_update_from_utlb( int entryNo )
nkeynes@586
   525
{
nkeynes@586
   526
    int replace;
nkeynes@586
   527
    /* Determine entry to replace based on lrui */
nkeynes@586
   528
    if( (mmu_lrui & 0x38) == 0x38 ) {
nkeynes@586
   529
	replace = 0;
nkeynes@586
   530
	mmu_lrui = mmu_lrui & 0x07;
nkeynes@586
   531
    } else if( (mmu_lrui & 0x26) == 0x06 ) {
nkeynes@586
   532
	replace = 1;
nkeynes@586
   533
	mmu_lrui = (mmu_lrui & 0x19) | 0x20;
nkeynes@586
   534
    } else if( (mmu_lrui & 0x15) == 0x01 ) {
nkeynes@586
   535
	replace = 2;
nkeynes@586
   536
	mmu_lrui = (mmu_lrui & 0x3E) | 0x14;
nkeynes@586
   537
    } else { // Note - gets invalid entries too
nkeynes@586
   538
	replace = 3;
nkeynes@586
   539
	mmu_lrui = (mmu_lrui | 0x0B);
nkeynes@586
   540
    } 
nkeynes@586
   541
nkeynes@586
   542
    mmu_itlb[replace].vpn = mmu_utlb[entryNo].vpn;
nkeynes@586
   543
    mmu_itlb[replace].mask = mmu_utlb[entryNo].mask;
nkeynes@586
   544
    mmu_itlb[replace].ppn = mmu_utlb[entryNo].ppn;
nkeynes@586
   545
    mmu_itlb[replace].asid = mmu_utlb[entryNo].asid;
nkeynes@586
   546
    mmu_itlb[replace].flags = mmu_utlb[entryNo].flags & 0x01DA;
nkeynes@586
   547
    return replace;
nkeynes@586
   548
}
nkeynes@586
   549
nkeynes@586
   550
/**
nkeynes@586
   551
 * Perform the actual itlb lookup w/ asid protection
nkeynes@586
   552
 * Possible utcomes are:
nkeynes@586
   553
 *   0..63 Single match - good, return entry found
nkeynes@586
   554
 *   -1 No match - raise a tlb data miss exception
nkeynes@586
   555
 *   -2 Multiple matches - raise a multi-hit exception (reset)
nkeynes@586
   556
 * @param vpn virtual address to resolve
nkeynes@586
   557
 * @return the resultant ITLB entry, or an error.
nkeynes@586
   558
 */
nkeynes@586
   559
static inline int mmu_itlb_lookup_vpn_asid( uint32_t vpn )
nkeynes@586
   560
{
nkeynes@586
   561
    int result = -1;
nkeynes@586
   562
    unsigned int i;
nkeynes@586
   563
nkeynes@586
   564
    for( i = 0; i < ITLB_ENTRY_COUNT; i++ ) {
nkeynes@586
   565
	if( (mmu_itlb[i].flags & TLB_VALID) &&
nkeynes@586
   566
	    ((mmu_itlb[i].flags & TLB_SHARE) || mmu_asid == mmu_itlb[i].asid) && 
nkeynes@586
   567
	    ((mmu_itlb[i].vpn ^ vpn) & mmu_itlb[i].mask) == 0 ) {
nkeynes@586
   568
	    if( result != -1 ) {
nkeynes@586
   569
		return -2;
nkeynes@586
   570
	    }
nkeynes@586
   571
	    result = i;
nkeynes@586
   572
	}
nkeynes@586
   573
    }
nkeynes@586
   574
nkeynes@586
   575
    if( result == -1 ) {
nkeynes@586
   576
	int utlbEntry = mmu_utlb_lookup_vpn_asid( vpn );
nkeynes@586
   577
	if( utlbEntry < 0 ) {
nkeynes@586
   578
	    return utlbEntry;
nkeynes@586
   579
	} else {
nkeynes@586
   580
	    return mmu_itlb_update_from_utlb( utlbEntry );
nkeynes@586
   581
	}
nkeynes@586
   582
    }
nkeynes@586
   583
nkeynes@586
   584
    switch( result ) {
nkeynes@586
   585
    case 0: mmu_lrui = (mmu_lrui & 0x07); break;
nkeynes@586
   586
    case 1: mmu_lrui = (mmu_lrui & 0x19) | 0x20; break;
nkeynes@586
   587
    case 2: mmu_lrui = (mmu_lrui & 0x3E) | 0x14; break;
nkeynes@586
   588
    case 3: mmu_lrui = (mmu_lrui | 0x0B); break;
nkeynes@586
   589
    }
nkeynes@586
   590
	
nkeynes@586
   591
    return result;
nkeynes@586
   592
}
nkeynes@586
   593
nkeynes@586
   594
/**
nkeynes@586
   595
 * Perform the actual itlb lookup on vpn only
nkeynes@586
   596
 * Possible utcomes are:
nkeynes@586
   597
 *   0..63 Single match - good, return entry found
nkeynes@586
   598
 *   -1 No match - raise a tlb data miss exception
nkeynes@586
   599
 *   -2 Multiple matches - raise a multi-hit exception (reset)
nkeynes@586
   600
 * @param vpn virtual address to resolve
nkeynes@586
   601
 * @return the resultant ITLB entry, or an error.
nkeynes@586
   602
 */
nkeynes@586
   603
static inline int mmu_itlb_lookup_vpn( uint32_t vpn )
nkeynes@586
   604
{
nkeynes@586
   605
    int result = -1;
nkeynes@586
   606
    unsigned int i;
nkeynes@586
   607
nkeynes@586
   608
    for( i = 0; i < ITLB_ENTRY_COUNT; i++ ) {
nkeynes@586
   609
	if( (mmu_itlb[i].flags & TLB_VALID) &&
nkeynes@586
   610
	    ((mmu_itlb[i].vpn ^ vpn) & mmu_itlb[i].mask) == 0 ) {
nkeynes@586
   611
	    if( result != -1 ) {
nkeynes@586
   612
		return -2;
nkeynes@586
   613
	    }
nkeynes@586
   614
	    result = i;
nkeynes@586
   615
	}
nkeynes@586
   616
    }
nkeynes@586
   617
nkeynes@586
   618
    if( result == -1 ) {
nkeynes@586
   619
	int utlbEntry = mmu_utlb_lookup_vpn( vpn );
nkeynes@586
   620
	if( utlbEntry < 0 ) {
nkeynes@586
   621
	    return utlbEntry;
nkeynes@586
   622
	} else {
nkeynes@586
   623
	    return mmu_itlb_update_from_utlb( utlbEntry );
nkeynes@586
   624
	}
nkeynes@586
   625
    }
nkeynes@586
   626
nkeynes@586
   627
    switch( result ) {
nkeynes@586
   628
    case 0: mmu_lrui = (mmu_lrui & 0x07); break;
nkeynes@586
   629
    case 1: mmu_lrui = (mmu_lrui & 0x19) | 0x20; break;
nkeynes@586
   630
    case 2: mmu_lrui = (mmu_lrui & 0x3E) | 0x14; break;
nkeynes@586
   631
    case 3: mmu_lrui = (mmu_lrui | 0x0B); break;
nkeynes@586
   632
    }
nkeynes@586
   633
	
nkeynes@586
   634
    return result;
nkeynes@586
   635
}
nkeynes@586
   636
nkeynes@586
   637
sh4addr_t mmu_vma_to_phys_read( sh4vma_t addr )
nkeynes@586
   638
{
nkeynes@586
   639
    uint32_t mmucr = MMIO_READ(MMU,MMUCR);
nkeynes@586
   640
    if( addr & 0x80000000 ) {
nkeynes@586
   641
	if( IS_SH4_PRIVMODE() ) {
nkeynes@586
   642
	    if( addr >= 0xE0000000 ) {
nkeynes@586
   643
		return addr; /* P4 - passthrough */
nkeynes@586
   644
	    } else if( addr < 0xC0000000 ) {
nkeynes@586
   645
		/* P1, P2 regions are pass-through (no translation) */
nkeynes@586
   646
		return VMA_TO_EXT_ADDR(addr);
nkeynes@586
   647
	    }
nkeynes@586
   648
	} else {
nkeynes@586
   649
	    if( addr >= 0xE0000000 && addr < 0xE4000000 &&
nkeynes@586
   650
		((mmucr&MMUCR_SQMD) == 0) ) {
nkeynes@586
   651
		/* Conditional user-mode access to the store-queue (no translation) */
nkeynes@586
   652
		return addr;
nkeynes@586
   653
	    }
nkeynes@586
   654
	    MMU_READ_ADDR_ERROR();
nkeynes@586
   655
	    return MMU_VMA_ERROR;
nkeynes@586
   656
	}
nkeynes@586
   657
    }
nkeynes@586
   658
    
nkeynes@586
   659
    if( (mmucr & MMUCR_AT) == 0 ) {
nkeynes@586
   660
	return VMA_TO_EXT_ADDR(addr);
nkeynes@586
   661
    }
nkeynes@586
   662
nkeynes@586
   663
    /* If we get this far, translation is required */
nkeynes@586
   664
    int entryNo;
nkeynes@586
   665
    if( ((mmucr & MMUCR_SV) == 0) || !IS_SH4_PRIVMODE() ) {
nkeynes@586
   666
	entryNo = mmu_utlb_lookup_vpn_asid( addr );
nkeynes@586
   667
    } else {
nkeynes@586
   668
	entryNo = mmu_utlb_lookup_vpn( addr );
nkeynes@586
   669
    }
nkeynes@586
   670
nkeynes@586
   671
    switch(entryNo) {
nkeynes@586
   672
    case -1:
nkeynes@586
   673
	MMU_TLB_READ_MISS_ERROR(addr);
nkeynes@586
   674
	return MMU_VMA_ERROR;
nkeynes@586
   675
    case -2:
nkeynes@586
   676
	MMU_TLB_MULTI_HIT_ERROR(addr);
nkeynes@586
   677
	return MMU_VMA_ERROR;
nkeynes@586
   678
    default:
nkeynes@586
   679
	if( (mmu_utlb[entryNo].flags & TLB_USERMODE) == 0 &&
nkeynes@586
   680
	    !IS_SH4_PRIVMODE() ) {
nkeynes@586
   681
	    /* protection violation */
nkeynes@586
   682
	    MMU_TLB_READ_PROT_ERROR(addr);
nkeynes@586
   683
	    return MMU_VMA_ERROR;
nkeynes@586
   684
	}
nkeynes@586
   685
nkeynes@586
   686
	/* finally generate the target address */
nkeynes@586
   687
	return (mmu_utlb[entryNo].ppn & mmu_utlb[entryNo].mask) | 
nkeynes@586
   688
	    (addr & (~mmu_utlb[entryNo].mask));
nkeynes@586
   689
    }
nkeynes@586
   690
}
nkeynes@586
   691
nkeynes@586
   692
sh4addr_t mmu_vma_to_phys_write( sh4vma_t addr )
nkeynes@586
   693
{
nkeynes@586
   694
    uint32_t mmucr = MMIO_READ(MMU,MMUCR);
nkeynes@586
   695
    if( addr & 0x80000000 ) {
nkeynes@586
   696
	if( IS_SH4_PRIVMODE() ) {
nkeynes@586
   697
	    if( addr >= 0xE0000000 ) {
nkeynes@586
   698
		return addr; /* P4 - passthrough */
nkeynes@586
   699
	    } else if( addr < 0xC0000000 ) {
nkeynes@586
   700
		/* P1, P2 regions are pass-through (no translation) */
nkeynes@586
   701
		return VMA_TO_EXT_ADDR(addr);
nkeynes@586
   702
	    }
nkeynes@586
   703
	} else {
nkeynes@586
   704
	    if( addr >= 0xE0000000 && addr < 0xE4000000 &&
nkeynes@586
   705
		((mmucr&MMUCR_SQMD) == 0) ) {
nkeynes@586
   706
		/* Conditional user-mode access to the store-queue (no translation) */
nkeynes@586
   707
		return addr;
nkeynes@586
   708
	    }
nkeynes@586
   709
	    MMU_WRITE_ADDR_ERROR();
nkeynes@586
   710
	    return MMU_VMA_ERROR;
nkeynes@586
   711
	}
nkeynes@586
   712
    }
nkeynes@586
   713
    
nkeynes@586
   714
    if( (mmucr & MMUCR_AT) == 0 ) {
nkeynes@586
   715
	return VMA_TO_EXT_ADDR(addr);
nkeynes@586
   716
    }
nkeynes@586
   717
nkeynes@586
   718
    /* If we get this far, translation is required */
nkeynes@586
   719
    int entryNo;
nkeynes@586
   720
    if( ((mmucr & MMUCR_SV) == 0) || !IS_SH4_PRIVMODE() ) {
nkeynes@586
   721
	entryNo = mmu_utlb_lookup_vpn_asid( addr );
nkeynes@586
   722
    } else {
nkeynes@586
   723
	entryNo = mmu_utlb_lookup_vpn( addr );
nkeynes@586
   724
    }
nkeynes@586
   725
nkeynes@586
   726
    switch(entryNo) {
nkeynes@586
   727
    case -1:
nkeynes@586
   728
	MMU_TLB_WRITE_MISS_ERROR(addr);
nkeynes@586
   729
	return MMU_VMA_ERROR;
nkeynes@586
   730
    case -2:
nkeynes@586
   731
	MMU_TLB_MULTI_HIT_ERROR(addr);
nkeynes@586
   732
	return MMU_VMA_ERROR;
nkeynes@586
   733
    default:
nkeynes@586
   734
	if( IS_SH4_PRIVMODE() ? ((mmu_utlb[entryNo].flags & TLB_WRITABLE) == 0)
nkeynes@586
   735
	    : ((mmu_utlb[entryNo].flags & TLB_USERWRITABLE) != TLB_USERWRITABLE) ) {
nkeynes@586
   736
	    /* protection violation */
nkeynes@586
   737
	    MMU_TLB_WRITE_PROT_ERROR(addr);
nkeynes@586
   738
	    return MMU_VMA_ERROR;
nkeynes@586
   739
	}
nkeynes@586
   740
nkeynes@586
   741
	if( (mmu_utlb[entryNo].flags & TLB_DIRTY) == 0 ) {
nkeynes@586
   742
	    MMU_TLB_INITIAL_WRITE_ERROR(addr);
nkeynes@586
   743
	    return MMU_VMA_ERROR;
nkeynes@586
   744
	}
nkeynes@586
   745
nkeynes@586
   746
	/* finally generate the target address */
nkeynes@586
   747
	return (mmu_utlb[entryNo].ppn & mmu_utlb[entryNo].mask) | 
nkeynes@586
   748
	    (addr & (~mmu_utlb[entryNo].mask));
nkeynes@586
   749
    }
nkeynes@586
   750
}
nkeynes@586
   751
nkeynes@586
   752
/**
nkeynes@586
   753
 * Update the icache for an untranslated address
nkeynes@586
   754
 */
nkeynes@586
   755
void mmu_update_icache_phys( sh4addr_t addr )
nkeynes@586
   756
{
nkeynes@586
   757
    if( (addr & 0x1C000000) == 0x0C000000 ) {
nkeynes@586
   758
	/* Main ram */
nkeynes@586
   759
	sh4_icache.page_vma = addr & 0xFF000000;
nkeynes@586
   760
	sh4_icache.page_ppa = 0x0C000000;
nkeynes@586
   761
	sh4_icache.mask = 0xFF000000;
nkeynes@586
   762
	sh4_icache.page = sh4_main_ram;
nkeynes@586
   763
    } else if( (addr & 0x1FE00000) == 0 ) {
nkeynes@586
   764
	/* BIOS ROM */
nkeynes@586
   765
	sh4_icache.page_vma = addr & 0xFFE00000;
nkeynes@586
   766
	sh4_icache.page_ppa = 0;
nkeynes@586
   767
	sh4_icache.mask = 0xFFE00000;
nkeynes@586
   768
	sh4_icache.page = mem_get_region(0);
nkeynes@586
   769
    } else {
nkeynes@586
   770
	/* not supported */
nkeynes@586
   771
	sh4_icache.page_vma = -1;
nkeynes@586
   772
    }
nkeynes@586
   773
}
nkeynes@586
   774
nkeynes@586
   775
/**
nkeynes@586
   776
 * Update the sh4_icache structure to describe the page(s) containing the
nkeynes@586
   777
 * given vma. If the address does not reference a RAM/ROM region, the icache
nkeynes@586
   778
 * will be invalidated instead.
nkeynes@586
   779
 * If AT is on, this method will raise TLB exceptions normally
nkeynes@586
   780
 * (hence this method should only be used immediately prior to execution of
nkeynes@586
   781
 * code), and otherwise will set the icache according to the matching TLB entry.
nkeynes@586
   782
 * If AT is off, this method will set the entire referenced RAM/ROM region in
nkeynes@586
   783
 * the icache.
nkeynes@586
   784
 * @return TRUE if the update completed (successfully or otherwise), FALSE
nkeynes@586
   785
 * if an exception was raised.
nkeynes@586
   786
 */
nkeynes@586
   787
gboolean mmu_update_icache( sh4vma_t addr )
nkeynes@586
   788
{
nkeynes@586
   789
    int entryNo;
nkeynes@586
   790
    if( IS_SH4_PRIVMODE()  ) {
nkeynes@586
   791
	if( addr & 0x80000000 ) {
nkeynes@586
   792
	    if( addr < 0xC0000000 ) {
nkeynes@586
   793
		/* P1, P2 and P4 regions are pass-through (no translation) */
nkeynes@586
   794
		mmu_update_icache_phys(addr);
nkeynes@586
   795
		return TRUE;
nkeynes@586
   796
	    } else if( addr >= 0xE0000000 && addr < 0xFFFFFF00 ) {
nkeynes@586
   797
		MMU_READ_ADDR_ERROR();
nkeynes@586
   798
		return FALSE;
nkeynes@586
   799
	    }
nkeynes@586
   800
	}
nkeynes@586
   801
    
nkeynes@586
   802
	uint32_t mmucr = MMIO_READ(MMU,MMUCR);
nkeynes@586
   803
	if( (mmucr & MMUCR_AT) == 0 ) {
nkeynes@586
   804
	    mmu_update_icache_phys(addr);
nkeynes@586
   805
	    return TRUE;
nkeynes@586
   806
	}
nkeynes@586
   807
nkeynes@586
   808
	entryNo = mmu_itlb_lookup_vpn( addr );
nkeynes@586
   809
    } else {
nkeynes@586
   810
	if( addr & 0x80000000 ) {
nkeynes@586
   811
	    MMU_READ_ADDR_ERROR();
nkeynes@586
   812
	    return FALSE;
nkeynes@586
   813
	}
nkeynes@586
   814
nkeynes@586
   815
	uint32_t mmucr = MMIO_READ(MMU,MMUCR);
nkeynes@586
   816
	if( (mmucr & MMUCR_AT) == 0 ) {
nkeynes@586
   817
	    mmu_update_icache_phys(addr);
nkeynes@586
   818
	    return TRUE;
nkeynes@586
   819
	}
nkeynes@586
   820
	
nkeynes@586
   821
	if( mmucr & MMUCR_SV ) {
nkeynes@586
   822
	    entryNo = mmu_itlb_lookup_vpn( addr );
nkeynes@586
   823
	} else {
nkeynes@586
   824
	    entryNo = mmu_itlb_lookup_vpn_asid( addr );
nkeynes@586
   825
	}
nkeynes@586
   826
	if( entryNo != -1 && (mmu_itlb[entryNo].flags & TLB_USERMODE) == 0 ) {
nkeynes@586
   827
	    MMU_TLB_READ_PROT_ERROR(addr);
nkeynes@586
   828
	    return FALSE;
nkeynes@586
   829
	}
nkeynes@586
   830
    }
nkeynes@586
   831
nkeynes@586
   832
    switch(entryNo) {
nkeynes@586
   833
    case -1:
nkeynes@586
   834
	MMU_TLB_READ_MISS_ERROR(addr);
nkeynes@586
   835
	return FALSE;
nkeynes@586
   836
    case -2:
nkeynes@586
   837
	MMU_TLB_MULTI_HIT_ERROR(addr);
nkeynes@586
   838
	return FALSE;
nkeynes@586
   839
    default:
nkeynes@586
   840
	sh4_icache.page_ppa = mmu_itlb[entryNo].ppn & mmu_itlb[entryNo].mask;
nkeynes@586
   841
	sh4_icache.page = mem_get_region( sh4_icache.page_ppa );
nkeynes@586
   842
	if( sh4_icache.page == NULL ) {
nkeynes@586
   843
	    sh4_icache.page_vma = -1;
nkeynes@586
   844
	} else {
nkeynes@586
   845
	    sh4_icache.page_vma = mmu_itlb[entryNo].vpn & mmu_itlb[entryNo].mask;
nkeynes@586
   846
	    sh4_icache.mask = mmu_itlb[entryNo].mask;
nkeynes@586
   847
	}
nkeynes@586
   848
	return TRUE;
nkeynes@586
   849
    }
nkeynes@586
   850
}
nkeynes@586
   851
nkeynes@586
   852
gboolean sh4_flush_store_queue( sh4addr_t addr )
nkeynes@586
   853
{
nkeynes@586
   854
    uint32_t mmucr = MMIO_READ(MMU,MMUCR);
nkeynes@586
   855
    int queue = (addr&0x20)>>2;
nkeynes@586
   856
    sh4ptr_t src = (sh4ptr_t)&sh4r.store_queue[queue];
nkeynes@586
   857
    sh4addr_t target;
nkeynes@586
   858
    /* Store queue operation */
nkeynes@586
   859
    if( mmucr & MMUCR_AT ) {
nkeynes@586
   860
	int entryNo;
nkeynes@586
   861
	if( ((mmucr & MMUCR_SV) == 0) || !IS_SH4_PRIVMODE() ) {
nkeynes@586
   862
	    entryNo = mmu_utlb_lookup_vpn_asid( addr );
nkeynes@586
   863
	} else {
nkeynes@586
   864
	    entryNo = mmu_utlb_lookup_vpn( addr );
nkeynes@586
   865
	}
nkeynes@586
   866
	switch(entryNo) {
nkeynes@586
   867
	case -1:
nkeynes@586
   868
	    MMU_TLB_WRITE_MISS_ERROR(addr);
nkeynes@586
   869
	    return FALSE;
nkeynes@586
   870
	case -2:
nkeynes@586
   871
	    MMU_TLB_MULTI_HIT_ERROR(addr);
nkeynes@586
   872
	    return FALSE;
nkeynes@586
   873
	default:
nkeynes@586
   874
	    if( IS_SH4_PRIVMODE() ? ((mmu_utlb[entryNo].flags & TLB_WRITABLE) == 0)
nkeynes@586
   875
		: ((mmu_utlb[entryNo].flags & TLB_USERWRITABLE) != TLB_USERWRITABLE) ) {
nkeynes@586
   876
		/* protection violation */
nkeynes@586
   877
		MMU_TLB_WRITE_PROT_ERROR(addr);
nkeynes@586
   878
		return FALSE;
nkeynes@586
   879
	    }
nkeynes@586
   880
	    
nkeynes@586
   881
	    if( (mmu_utlb[entryNo].flags & TLB_DIRTY) == 0 ) {
nkeynes@586
   882
		MMU_TLB_INITIAL_WRITE_ERROR(addr);
nkeynes@586
   883
		return FALSE;
nkeynes@586
   884
	    }
nkeynes@586
   885
	    
nkeynes@586
   886
	    /* finally generate the target address */
nkeynes@586
   887
	    target = ((mmu_utlb[entryNo].ppn & mmu_utlb[entryNo].mask) | 
nkeynes@586
   888
		      (addr & (~mmu_utlb[entryNo].mask))) & 0xFFFFFFE0;
nkeynes@586
   889
	}
nkeynes@586
   890
    } else {
nkeynes@586
   891
	uint32_t hi = (MMIO_READ( MMU, (queue == 0 ? QACR0 : QACR1) ) & 0x1C) << 24;
nkeynes@586
   892
	target = (addr&0x03FFFFE0) | hi;
nkeynes@586
   893
    }
nkeynes@586
   894
    mem_copy_to_sh4( target, src, 32 );
nkeynes@586
   895
    return TRUE;
nkeynes@586
   896
}
nkeynes@586
   897
.