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 425

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

powered by: WebSVN 2.1.0

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