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 62

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

powered by: WebSVN 2.1.0

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