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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mmu/] [dmmu.c] - Blame information for rev 167

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

Line No. Rev Author Line
1 62 lampret
/* dmmu.c -- Data MMU simulation
2 6 lampret
   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
/* DMMU model (not functional yet, currently just copy of data cache). */
21
 
22
#include "dmmu.h"
23
#include "abstract.h"
24
#include "stats.h"
25 62 lampret
#include "sprs.h"
26
#include "except.h"
27 6 lampret
 
28 62 lampret
extern int cont_run;
29
 
30 6 lampret
/* Data MMU */
31
 
32
unsigned long dmmu_translate(unsigned long virtaddr)
33
{
34 62 lampret
        unsigned long phyaddr = dmmu_simulate_tlb(virtaddr);
35
 
36 73 lampret
/*      printf("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
37 62 lampret
        return phyaddr;
38 6 lampret
}
39
 
40 62 lampret
/* Number of DTLB sets used (power of 2, max is 256) */
41
#define DTLB_SETS 16
42 6 lampret
 
43 62 lampret
/* Entry size in bytes (8 == two singlewords) */
44
#define DTLB_ENTRY_SIZE 8
45 6 lampret
 
46 62 lampret
/* Number of DTLB ways (1, 2, 3 etc., max is 4). */
47
#define DTLB_WAYS 2
48 6 lampret
 
49 62 lampret
/* Number of usage states (2, 3, 4 etc., max is 4). */
50
#define DTLB_USTATES 2
51 6 lampret
 
52 62 lampret
void dtlb_info()
53 6 lampret
{
54 167 markom
        if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
55 102 lampret
                printf("DMMU not implemented. Set UPR[DMP].\n");
56
                return;
57
        }
58
 
59 62 lampret
        printf("Data MMU %dKB: ", DTLB_SETS * DTLB_ENTRY_SIZE * DTLB_WAYS / 1024);
60
        printf("%d ways, %d sets, entry size %d bytes\n", DTLB_WAYS, DTLB_SETS, DTLB_ENTRY_SIZE);
61 6 lampret
}
62
 
63 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
64
    - increment DTLB read hit stats,
65
    - set 'lru' at this way to DTLB_USTATES - 1 and
66 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
67 62 lampret
    - check page access attributes and invoke DMMU page fault exception
68
      handler if necessary
69 6 lampret
   and if not:
70 62 lampret
    - increment DTLB read miss stats
71
    - find lru way and entry and invoke DTLB miss exception handler
72
    - set 'lru' with DTLB_USTATES - 1 and decrement 'lru' of other
73 6 lampret
      ways unless they have reached 0
74
*/
75
 
76 102 lampret
void dtlb_status(int start_set)
77 6 lampret
{
78 62 lampret
        int set;
79
        int way;
80
        int end_set = DTLB_SETS;
81
 
82 167 markom
        if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
83 102 lampret
                printf("DMMU not implemented. Set UPR[DMP].\n");
84
                return;
85
        }
86
 
87 62 lampret
        if ((start_set >= 0) && (start_set < end_set))
88
                end_set = start_set + 1;
89
        else
90
                start_set = 0;
91
 
92 73 lampret
        printf("\nDMMU: ");
93 62 lampret
        /* 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 < DTLB_WAYS; way++) {
97
                        printf("  way %d: ", way);
98 73 lampret
                        printf("vpn=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
99
                        printf("lru=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
100
                        printf("pl1=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
101
                        printf("v=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
102
 
103
                        printf("a=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
104
                        printf("d=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
105
                        printf("ure=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
106
                        printf("uwe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
107
                        printf("sre=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
108
                        printf("swe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
109
                        printf("ppn=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
110 62 lampret
                }
111 6 lampret
        }
112 62 lampret
        printf("\n");
113 6 lampret
}
114
 
115 62 lampret
unsigned long dmmu_simulate_tlb(unsigned long virtaddr)
116 6 lampret
{
117
        int set, way = -1;
118
        int i;
119
        unsigned long tagaddr;
120 62 lampret
        unsigned long vpn;
121
 
122 167 markom
        if (!(mfspr(SPR_SR) & SPR_SR_DME) || (!testsprbits(SPR_SR, SPR_SR_DME)))
123 62 lampret
                return virtaddr;
124
 
125 6 lampret
        /* Which set to check out? */
126 62 lampret
        set = (virtaddr / PAGE_SIZE) % DTLB_SETS;
127
        tagaddr = (virtaddr / PAGE_SIZE) / DTLB_SETS;
128 73 lampret
        vpn = virtaddr / PAGE_SIZE;
129 62 lampret
 
130 6 lampret
        /* Scan all ways and try to find a matching way. */
131 62 lampret
        for (i = 0; i < DTLB_WAYS; i++)
132
                if ((getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_VPN) == vpn) &&
133 167 markom
                    testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
134 6 lampret
                        way = i;
135
 
136 62 lampret
        /* Did we find our tlb entry? */
137 6 lampret
        if (way >= 0) { /* Yes, we did. */
138 62 lampret
                dmmu_stats.loads_tlbhit++;
139
                debug("DTLB hit (virtaddr=%x).\n", virtaddr);
140
 
141
                /* Set LRUs */
142
                for (i = 0; i < DTLB_WAYS; i++)
143 167 markom
                        if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
144 62 lampret
                                setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
145
                setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
146
 
147 73 lampret
                return getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
148 6 lampret
        }
149
        else {  /* No, we didn't. */
150 62 lampret
                int minlru = DTLB_USTATES - 1;
151 6 lampret
                int minway = 0;
152 62 lampret
 
153
                dmmu_stats.loads_tlbmiss++;
154
#if 0
155
                for (i = 0; i < DTLB_WAYS; i++)
156
                        if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
157 6 lampret
                                minway = i;
158
 
159 62 lampret
                setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
160
                for (i = 0; i < DTLB_WAYS; i++)
161
                        if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
162
                                setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
163
                setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
164
                setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
165
                setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
166
#endif
167
                except_handle(EXCEPT_DTLBMISS, virtaddr);
168
                /* if tlb refill implemented in HW */
169 73 lampret
                /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
170 62 lampret
                return 0;
171 6 lampret
        }
172
}

powered by: WebSVN 2.1.0

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