OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mmu/] [immu.c] - Blame information for rev 74

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 74 lampret
/* immu.c -- Instruction MMU simulation
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* IMMU model (not functional yet, currently just copy of data cache). */
21
 
22
#include "immu.h"
23
#include "abstract.h"
24
#include "stats.h"
25
#include "sprs.h"
26
#include "except.h"
27
 
28
extern int cont_run;
29
 
30
/* Insn MMU */
31
 
32
unsigned long immu_translate(unsigned long virtaddr)
33
{
34
        unsigned long phyaddr = immu_simulate_tlb(virtaddr);
35
 
36
/*      printf("IMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
37
        return phyaddr;
38
}
39
 
40
/* Number of ITLB sets used (power of 2, max is 256) */
41
#define ITLB_SETS 16
42
 
43
/* Entry size in bytes (8 == two singlewords) */
44
#define ITLB_ENTRY_SIZE 8
45
 
46
/* Number of ITLB ways (1, 2, 3 etc., max is 4). */
47
#define ITLB_WAYS 2
48
 
49
/* Number of usage states (2, 3, 4 etc., max is 4). */
50
#define ITLB_USTATES 2
51
 
52
void itlb_info()
53
{
54
        printf("Insn MMU %dKB: ", ITLB_SETS * ITLB_ENTRY_SIZE * ITLB_WAYS / 1024);
55
        printf("%d ways, %d sets, entry size %d bytes\n", ITLB_WAYS, ITLB_SETS, ITLB_ENTRY_SIZE);
56
}
57
 
58
/* First check if virtual address is covered by ITLB and if it is:
59
    - increment ITLB read hit stats,
60
    - set 'lru' at this way to ITLB_USTATES - 1 and
61
      decrement 'lru' of other ways unless they have reached 0,
62
    - check page access attributes and invoke IMMU page fault exception
63
      handler if necessary
64
   and if not:
65
    - increment ITLB read miss stats
66
    - find lru way and entry and invoke ITLB miss exception handler
67
    - set 'lru' with ITLB_USTATES - 1 and decrement 'lru' of other
68
      ways unless they have reached 0
69
*/
70
 
71
unsigned long itlb_status(int start_set)
72
{
73
        int set;
74
        int way;
75
        int end_set = ITLB_SETS;
76
 
77
        if ((start_set >= 0) && (start_set < end_set))
78
                end_set = start_set + 1;
79
        else
80
                start_set = 0;
81
 
82
        printf("\nIMMU: ");
83
        /* Scan set(s) and way(s). */
84
        for (set = start_set; set < end_set; set++) {
85
                printf("\nSet %x: ", set);
86
                for (way = 0; way < ITLB_WAYS; way++) {
87
                        printf("  way %d: ", way);
88
                        printf("vpn=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_VPN));
89
                        printf("lru=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU));
90
                        printf("pl1=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
91
                        printf("v=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_V));
92
 
93
                        printf("a=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_A));
94
                        printf("d=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
95
                        printf("ure=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_URE));
96
                        printf("uwe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UWE));
97
                        printf("sre=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SRE));
98
                        printf("swe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SWE));
99
                        printf("ppn=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
100
                }
101
        }
102
        printf("\n");
103
}
104
 
105
unsigned long immu_simulate_tlb(unsigned long virtaddr)
106
{
107
        int set, way = -1;
108
        int i;
109
        unsigned long tagaddr;
110
        unsigned long vpn;
111
 
112
        if (! (mfspr(SPR_SR) & SPR_SR_IME))
113
                return virtaddr;
114
 
115
        /* Which set to check out? */
116
        set = (virtaddr / PAGE_SIZE) % ITLB_SETS;
117
        tagaddr = (virtaddr / PAGE_SIZE) / ITLB_SETS;
118
        vpn = virtaddr / PAGE_SIZE;
119
 
120
        /* Scan all ways and try to find a matching way. */
121
        for (i = 0; i < ITLB_WAYS; i++)
122
                if ((getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_VPN) == vpn) &&
123
                    getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
124
                        way = i;
125
 
126
        /* Did we find our tlb entry? */
127
        if (way >= 0) { /* Yes, we did. */
128
                immu_stats.fetch_tlbhit++;
129
                debug("ITLB hit (virtaddr=%x).\n", virtaddr);
130
 
131
                /* Set LRUs */
132
                for (i = 0; i < ITLB_WAYS; i++)
133
                        if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
134
                                setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
135
                setsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU, ITLB_USTATES - 1);
136
 
137
                return getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
138
        }
139
        else {  /* No, we didn't. */
140
                int minlru = ITLB_USTATES - 1;
141
                int minway = 0;
142
 
143
                immu_stats.fetch_tlbmiss++;
144
                cont_run=0;
145
#if 0
146
                for (i = 0; i < ITLB_WAYS; i++)
147
                        if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
148
                                minway = i;
149
 
150
                setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
151
                for (i = 0; i < ITLB_WAYS; i++)
152
                        if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
153
                                setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
154
                setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_LRU, ITLB_USTATES - 1);
155
                setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
156
                setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
157
#endif
158
                except_handle(EXCEPT_ITLBMISS, virtaddr);
159
                /* if tlb refill implemented in HW */
160
                /* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
161
                return 0;
162
        }
163
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.