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 73

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 62 lampret
        printf("Data MMU %dKB: ", DTLB_SETS * DTLB_ENTRY_SIZE * DTLB_WAYS / 1024);
55
        printf("%d ways, %d sets, entry size %d bytes\n", DTLB_WAYS, DTLB_SETS, DTLB_ENTRY_SIZE);
56 6 lampret
}
57
 
58 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
59
    - increment DTLB read hit stats,
60
    - set 'lru' at this way to DTLB_USTATES - 1 and
61 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
62 62 lampret
    - check page access attributes and invoke DMMU page fault exception
63
      handler if necessary
64 6 lampret
   and if not:
65 62 lampret
    - increment DTLB read miss stats
66
    - find lru way and entry and invoke DTLB miss exception handler
67
    - set 'lru' with DTLB_USTATES - 1 and decrement 'lru' of other
68 6 lampret
      ways unless they have reached 0
69
*/
70
 
71 62 lampret
unsigned long dtlb_status(int start_set)
72 6 lampret
{
73 62 lampret
        int set;
74
        int way;
75
        int end_set = DTLB_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 73 lampret
        printf("\nDMMU: ");
83 62 lampret
        /* 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 < DTLB_WAYS; way++) {
87
                        printf("  way %d: ", way);
88 73 lampret
                        printf("vpn=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
89
                        printf("lru=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
90
                        printf("pl1=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
91
                        printf("v=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
92
 
93
                        printf("a=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
94
                        printf("d=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
95
                        printf("ure=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
96
                        printf("uwe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
97
                        printf("sre=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
98
                        printf("swe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
99
                        printf("ppn=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
100 62 lampret
                }
101 6 lampret
        }
102 62 lampret
        printf("\n");
103 6 lampret
}
104
 
105 62 lampret
unsigned long dmmu_simulate_tlb(unsigned long virtaddr)
106 6 lampret
{
107
        int set, way = -1;
108
        int i;
109
        unsigned long tagaddr;
110 62 lampret
        unsigned long vpn;
111
 
112
        if (! (mfspr(SPR_SR) & SPR_SR_DME))
113
                return virtaddr;
114
 
115 6 lampret
        /* Which set to check out? */
116 62 lampret
        set = (virtaddr / PAGE_SIZE) % DTLB_SETS;
117
        tagaddr = (virtaddr / PAGE_SIZE) / DTLB_SETS;
118 73 lampret
        vpn = virtaddr / PAGE_SIZE;
119 62 lampret
 
120 6 lampret
        /* Scan all ways and try to find a matching way. */
121 62 lampret
        for (i = 0; i < DTLB_WAYS; i++)
122
                if ((getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_VPN) == vpn) &&
123
                    getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
124 6 lampret
                        way = i;
125
 
126 62 lampret
        /* Did we find our tlb entry? */
127 6 lampret
        if (way >= 0) { /* Yes, we did. */
128 62 lampret
                dmmu_stats.loads_tlbhit++;
129
                debug("DTLB hit (virtaddr=%x).\n", virtaddr);
130
 
131
                /* Set LRUs */
132
                for (i = 0; i < DTLB_WAYS; i++)
133
                        if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
134
                                setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
135
                setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
136
 
137 73 lampret
                return getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE);
138 6 lampret
        }
139
        else {  /* No, we didn't. */
140 62 lampret
                int minlru = DTLB_USTATES - 1;
141 6 lampret
                int minway = 0;
142 62 lampret
 
143
                dmmu_stats.loads_tlbmiss++;
144
#if 0
145
                for (i = 0; i < DTLB_WAYS; i++)
146
                        if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
147 6 lampret
                                minway = i;
148
 
149 62 lampret
                setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
150
                for (i = 0; i < DTLB_WAYS; i++)
151
                        if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
152
                                setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
153
                setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, DTLB_USTATES - 1);
154
                setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
155
                setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
156
#endif
157
                except_handle(EXCEPT_DTLBMISS, virtaddr);
158
                /* if tlb refill implemented in HW */
159 73 lampret
                /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * PAGE_SIZE + (virtaddr % PAGE_SIZE); */
160 62 lampret
                return 0;
161 6 lampret
        }
162
}

powered by: WebSVN 2.1.0

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