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 1308

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 425 markom
#include "sim-config.h"
28 1308 phoenix
#include "debug.h"
29 6 lampret
 
30 62 lampret
extern int cont_run;
31
 
32 6 lampret
/* Data MMU */
33
 
34 430 markom
inline unsigned long dmmu_simulate_tlb(unsigned long virtaddr, int write_access)
35 6 lampret
{
36 430 markom
  int set, way = -1;
37
  int i;
38
  unsigned long tagaddr;
39 456 simons
  unsigned long vpn, ppn;
40 572 simons
 
41 638 simons
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
42
    data_ci = (virtaddr >= 0x80000000);
43 430 markom
    return virtaddr;
44 638 simons
  }
45 430 markom
 
46
  /* Which set to check out? */
47
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
48
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
49 456 simons
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
50
 
51 430 markom
  /* Scan all ways and try to find a matching way. */
52
  for (i = 0; i < config.dmmu.nways; i++)
53 456 simons
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
54 430 markom
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
55
      way = i;
56 456 simons
 
57
   /* Did we find our tlb entry? */
58 430 markom
  if (way >= 0) { /* Yes, we did. */
59
    dmmu_stats.loads_tlbhit++;
60
    debug(5, "DTLB hit (virtaddr=%x).\n", virtaddr);
61
 
62
    /* Test for page fault */
63 600 simons
    if (mfspr (SPR_SR) & SPR_SR_SM) {
64 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
65
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
66 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
67
    } else {
68 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
69
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
70 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
71
    }
72
 
73
    /* Set LRUs */
74
    for (i = 0; i < config.dmmu.nways; i++)
75
      if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
76
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
77 886 simons
    setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, config.dmmu.nsets - 1);
78 430 markom
 
79 638 simons
    /* Check if page is cache inhibited */
80
    data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
81
 
82 884 markom
    runtime.sim.mem_cycles += config.dmmu.hitdelay;
83 456 simons
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
84
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
85 430 markom
  }
86
  else {  /* No, we didn't. */
87
    dmmu_stats.loads_tlbmiss++;
88
#if 0
89
    for (i = 0; i < config.dmmu.nways; i++)
90
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
91
        minway = i;
92
 
93
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
94
    for (i = 0; i < config.dmmu.nways; i++)
95
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
96
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
97
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
98
    setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
99
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
100
#endif
101
    except_handle(EXCEPT_DTLBMISS, virtaddr);
102
    /* if tlb refill implemented in HW */
103
    /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * config.dmmu.pagesize + (virtaddr % config.dmmu.pagesize); */
104 884 markom
    runtime.sim.mem_cycles += config.dmmu.missdelay;
105 430 markom
    return 0;
106
  }
107
}
108
 
109 1240 phoenix
/* DESC: try to find EA -> PA transaltion without changing
110
 *       any of precessor states. if this is not passible gives up
111
 *       (without triggering exceptions)
112
 *
113
 * PRMS: virtaddr     - EA for which to find translation
114
 *
115
 *       write_access - 0 ignore testing for write access
116
 *                      1 test for write access, if fails
117
 *                        do not return translation
118
 *
119
 *       through_dc   - 1 go through data cache
120
 *                      0 ignore data cache
121
 *
122
 * RTRN: 0            - no DMMU, DMMU disabled or ITLB miss
123
 *       else         - appropriate PA (note it DMMU is not present
124
 *                      PA === EA)
125
 */
126
unsigned long peek_into_dtlb(unsigned long virtaddr, int write_access,
127
                            int through_dc)
128
{
129
  int set, way = -1;
130
  int i;
131
  unsigned long tagaddr;
132
  unsigned long vpn, ppn;
133
 
134
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP)) {
135
    if (through_dc)
136
      data_ci = (virtaddr >= 0x80000000);
137
    return virtaddr;
138
  }
139
 
140
  /* Which set to check out? */
141
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
142
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
143
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
144
 
145
  /* Scan all ways and try to find a matching way. */
146
  for (i = 0; i < config.dmmu.nways; i++)
147
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
148
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
149
      way = i;
150
 
151
   /* Did we find our tlb entry? */
152
  if (way >= 0) { /* Yes, we did. */
153
    dmmu_stats.loads_tlbhit++;
154
    debug(5, "DTLB hit (virtaddr=%x).\n", virtaddr);
155
 
156
    /* Test for page fault */
157
    if (mfspr (SPR_SR) & SPR_SR_SM) {
158
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
159
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
160
 
161
        /* otherwise exception DPF would be raised */
162
        return(0);
163
    } else {
164
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
165
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
166
 
167
        /* otherwise exception DPF would be raised */
168
        return(0);
169
    }
170
 
171
    if (through_dc) {
172
      /* Check if page is cache inhibited */
173
      data_ci = (mfspr(SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_CI) == SPR_DTLBTR_CI;
174
    }
175
 
176
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
177
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
178
  }
179
  else {  /* No, we didn't. */
180
    return(0);
181
  }
182
 
183
  PRINTF("ERR, should never have happened\n");
184
  return(0);
185
}
186
 
187
 
188 430 markom
unsigned long dmmu_translate(unsigned long virtaddr, int write_access)
189
{
190
  unsigned long phyaddr = dmmu_simulate_tlb(virtaddr, write_access);
191 429 markom
 
192 997 markom
/*  PRINTF("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
193 429 markom
  return phyaddr;
194 6 lampret
}
195
 
196
 
197 62 lampret
void dtlb_info()
198 6 lampret
{
199 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
200 997 markom
    PRINTF("DMMU not implemented. Set UPR[DMP].\n");
201 429 markom
    return;
202
  }
203
 
204 997 markom
  PRINTF("Data MMU %dKB: ", config.dmmu.nsets * config.dmmu.entrysize * config.dmmu.nways / 1024);
205
  PRINTF("%d ways, %d sets, entry size %d bytes\n", config.dmmu.nways, config.dmmu.nsets, config.dmmu.entrysize);
206 6 lampret
}
207
 
208 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
209
    - increment DTLB read hit stats,
210 425 markom
    - set 'lru' at this way to config.dmmu.ustates - 1 and
211 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
212 62 lampret
    - check page access attributes and invoke DMMU page fault exception
213
      handler if necessary
214 6 lampret
   and if not:
215 62 lampret
    - increment DTLB read miss stats
216
    - find lru way and entry and invoke DTLB miss exception handler
217 425 markom
    - set 'lru' with config.dmmu.ustates - 1 and decrement 'lru' of other
218 6 lampret
      ways unless they have reached 0
219
*/
220
 
221 102 lampret
void dtlb_status(int start_set)
222 6 lampret
{
223 429 markom
  int set;
224
  int way;
225
  int end_set = config.dmmu.nsets;
226 62 lampret
 
227 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
228 997 markom
    PRINTF("DMMU not implemented. Set UPR[DMP].\n");
229 429 markom
    return;
230
  }
231 102 lampret
 
232 429 markom
  if ((start_set >= 0) && (start_set < end_set))
233
    end_set = start_set + 1;
234
  else
235
    start_set = 0;
236 62 lampret
 
237 997 markom
  if (start_set < end_set) PRINTF("\nDMMU: ");
238 429 markom
  /* Scan set(s) and way(s). */
239
  for (set = start_set; set < end_set; set++) {
240 997 markom
    PRINTF("\nSet %x: ", set);
241 429 markom
    for (way = 0; way < config.dmmu.nways; way++) {
242 997 markom
      PRINTF("  way %d: ", way);
243 1308 phoenix
      PRINTF("vpn=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
244
      PRINTF("lru=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
245
      PRINTF("pl1=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
246
      PRINTF("v=%lx ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
247 429 markom
 
248 1308 phoenix
      PRINTF("a=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
249
      PRINTF("d=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
250
      PRINTF("ure=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
251
      PRINTF("uwe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
252
      PRINTF("sre=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
253
      PRINTF("swe=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
254
      PRINTF("ppn=%lx ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
255 429 markom
    }
256
  }
257 997 markom
  if (start_set < end_set) PRINTF("\n");
258 6 lampret
}

powered by: WebSVN 2.1.0

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