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 102

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 102 lampret
        if (!getsprbits(SPR_UPR, SPR_UPR_IMP)) {
55
                printf("IMMU not implemented. Set UPR[IMP].\n");
56
                return;
57
        }
58
 
59 74 lampret
        printf("Insn MMU %dKB: ", ITLB_SETS * ITLB_ENTRY_SIZE * ITLB_WAYS / 1024);
60
        printf("%d ways, %d sets, entry size %d bytes\n", ITLB_WAYS, ITLB_SETS, ITLB_ENTRY_SIZE);
61
}
62
 
63
/* First check if virtual address is covered by ITLB and if it is:
64
    - increment ITLB read hit stats,
65
    - set 'lru' at this way to ITLB_USTATES - 1 and
66
      decrement 'lru' of other ways unless they have reached 0,
67
    - check page access attributes and invoke IMMU page fault exception
68
      handler if necessary
69
   and if not:
70
    - increment ITLB read miss stats
71
    - find lru way and entry and invoke ITLB miss exception handler
72
    - set 'lru' with ITLB_USTATES - 1 and decrement 'lru' of other
73
      ways unless they have reached 0
74
*/
75
 
76 102 lampret
void itlb_status(int start_set)
77 74 lampret
{
78
        int set;
79
        int way;
80
        int end_set = ITLB_SETS;
81
 
82 102 lampret
        if (!getsprbits(SPR_UPR, SPR_UPR_IMP)) {
83
                printf("IMMU not implemented. Set UPR[IMP].\n");
84
                return;
85
        }
86
 
87 74 lampret
        if ((start_set >= 0) && (start_set < end_set))
88
                end_set = start_set + 1;
89
        else
90
                start_set = 0;
91
 
92
        printf("\nIMMU: ");
93
        /* Scan set(s) and way(s). */
94
        for (set = start_set; set < end_set; set++) {
95
                printf("\nSet %x: ", set);
96
                for (way = 0; way < ITLB_WAYS; way++) {
97
                        printf("  way %d: ", way);
98
                        printf("vpn=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_VPN));
99
                        printf("lru=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU));
100
                        printf("pl1=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
101
                        printf("v=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_V));
102
 
103
                        printf("a=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_A));
104
                        printf("d=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
105
                        printf("ure=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_URE));
106
                        printf("uwe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UWE));
107
                        printf("sre=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SRE));
108
                        printf("swe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SWE));
109
                        printf("ppn=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
110
                }
111
        }
112
        printf("\n");
113
}
114
 
115
unsigned long immu_simulate_tlb(unsigned long virtaddr)
116
{
117
        int set, way = -1;
118
        int i;
119
        unsigned long tagaddr;
120
        unsigned long vpn;
121
 
122 102 lampret
        if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(getsprbits(SPR_UPR, SPR_UPR_IMP)))
123 74 lampret
                return virtaddr;
124
 
125
        /* Which set to check out? */
126
        set = (virtaddr / PAGE_SIZE) % ITLB_SETS;
127
        tagaddr = (virtaddr / PAGE_SIZE) / ITLB_SETS;
128
        vpn = virtaddr / PAGE_SIZE;
129
 
130
        /* Scan all ways and try to find a matching way. */
131
        for (i = 0; i < ITLB_WAYS; i++)
132
                if ((getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_VPN) == vpn) &&
133
                    getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
134
                        way = i;
135
 
136
        /* Did we find our tlb entry? */
137
        if (way >= 0) { /* Yes, we did. */
138
                immu_stats.fetch_tlbhit++;
139
                debug("ITLB hit (virtaddr=%x).\n", virtaddr);
140
 
141
                /* Set LRUs */
142
                for (i = 0; i < ITLB_WAYS; i++)
143
                        if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
144
                                setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
145
                setsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU, ITLB_USTATES - 1);
146
 
147
                return getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
148
        }
149
        else {  /* No, we didn't. */
150
                int minlru = ITLB_USTATES - 1;
151
                int minway = 0;
152
 
153
                immu_stats.fetch_tlbmiss++;
154
                cont_run=0;
155
#if 0
156
                for (i = 0; i < ITLB_WAYS; i++)
157
                        if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
158
                                minway = i;
159
 
160
                setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
161
                for (i = 0; i < ITLB_WAYS; i++)
162
                        if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
163
                                setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
164
                setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_LRU, ITLB_USTATES - 1);
165
                setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
166
                setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
167
#endif
168
                except_handle(EXCEPT_ITLBMISS, virtaddr);
169
                /* if tlb refill implemented in HW */
170
                /* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
171
                return 0;
172
        }
173
}

powered by: WebSVN 2.1.0

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