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 1240

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

powered by: WebSVN 2.1.0

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