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 600

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 541 markom
  extern int mem_cycles;
40 572 simons
 
41
  if (!(mfspr(SPR_SR) & SPR_SR_DME) || !testsprbits(SPR_UPR, SPR_UPR_DMP))
42 430 markom
    return virtaddr;
43
 
44
  /* Which set to check out? */
45
  set = (virtaddr / config.dmmu.pagesize) % config.dmmu.nsets;
46
  tagaddr = (virtaddr / config.dmmu.pagesize) / config.dmmu.nsets;
47 456 simons
  vpn = virtaddr / (config.dmmu.pagesize * config.dmmu.nsets);
48
 
49 430 markom
  /* Scan all ways and try to find a matching way. */
50
  for (i = 0; i < config.dmmu.nways; i++)
51 456 simons
    if (((mfspr(SPR_DTLBMR_BASE(i) + set) / (config.dmmu.pagesize * config.dmmu.nsets)) == vpn) &&
52 430 markom
        testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_V))
53
      way = i;
54 456 simons
 
55
   /* Did we find our tlb entry? */
56 430 markom
  if (way >= 0) { /* Yes, we did. */
57
    dmmu_stats.loads_tlbhit++;
58
    debug(5, "DTLB hit (virtaddr=%x).\n", virtaddr);
59
 
60
    /* Test for page fault */
61 600 simons
    if (mfspr (SPR_SR) & SPR_SR_SM) {
62 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SWE)
63
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_SRE))
64 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
65
    } else {
66 438 simons
      if ( write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_UWE)
67
       || !write_access && !(mfspr (SPR_DTLBTR_BASE(way) + set) & SPR_DTLBTR_URE))
68 430 markom
        except_handle(EXCEPT_DPF, virtaddr);
69
    }
70
 
71
    /* Set LRUs */
72
    for (i = 0; i < config.dmmu.nways; i++)
73
      if (testsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
74
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
75
    setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
76
 
77 541 markom
    mem_cycles += config.dmmu.hitdelay;
78 456 simons
    ppn = mfspr(SPR_DTLBTR_BASE(way) + set) / config.dmmu.pagesize;
79
    return (ppn * config.dmmu.pagesize) + (virtaddr % config.dmmu.pagesize);
80 430 markom
  }
81
  else {  /* No, we didn't. */
82
    int minlru = config.dmmu.ustates - 1;
83
    int minway = 0;
84
 
85
    dmmu_stats.loads_tlbmiss++;
86
#if 0
87
    for (i = 0; i < config.dmmu.nways; i++)
88
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
89
        minway = i;
90
 
91
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
92
    for (i = 0; i < config.dmmu.nways; i++)
93
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
94
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
95
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
96
    setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
97
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
98
#endif
99
    except_handle(EXCEPT_DTLBMISS, virtaddr);
100
    /* if tlb refill implemented in HW */
101
    /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * config.dmmu.pagesize + (virtaddr % config.dmmu.pagesize); */
102 541 markom
    mem_cycles += config.dmmu.missdelay;
103 430 markom
    return 0;
104
  }
105
}
106
 
107
unsigned long dmmu_translate(unsigned long virtaddr, int write_access)
108
{
109
  unsigned long phyaddr = dmmu_simulate_tlb(virtaddr, write_access);
110 429 markom
 
111
/*  printf("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
112
  return phyaddr;
113 6 lampret
}
114
 
115
 
116 62 lampret
void dtlb_info()
117 6 lampret
{
118 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
119
    printf("DMMU not implemented. Set UPR[DMP].\n");
120
    return;
121
  }
122
 
123
  printf("Data MMU %dKB: ", config.dmmu.nsets * config.dmmu.entrysize * config.dmmu.nways / 1024);
124
  printf("%d ways, %d sets, entry size %d bytes\n", config.dmmu.nways, config.dmmu.nsets, config.dmmu.entrysize);
125 6 lampret
}
126
 
127 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
128
    - increment DTLB read hit stats,
129 425 markom
    - set 'lru' at this way to config.dmmu.ustates - 1 and
130 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
131 62 lampret
    - check page access attributes and invoke DMMU page fault exception
132
      handler if necessary
133 6 lampret
   and if not:
134 62 lampret
    - increment DTLB read miss stats
135
    - find lru way and entry and invoke DTLB miss exception handler
136 425 markom
    - set 'lru' with config.dmmu.ustates - 1 and decrement 'lru' of other
137 6 lampret
      ways unless they have reached 0
138
*/
139
 
140 102 lampret
void dtlb_status(int start_set)
141 6 lampret
{
142 429 markom
  int set;
143
  int way;
144
  int end_set = config.dmmu.nsets;
145 62 lampret
 
146 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
147
    printf("DMMU not implemented. Set UPR[DMP].\n");
148
    return;
149
  }
150 102 lampret
 
151 429 markom
  if ((start_set >= 0) && (start_set < end_set))
152
    end_set = start_set + 1;
153
  else
154
    start_set = 0;
155 62 lampret
 
156 535 markom
  if (start_set < end_set) printf("\nDMMU: ");
157 429 markom
  /* Scan set(s) and way(s). */
158
  for (set = start_set; set < end_set; set++) {
159
    printf("\nSet %x: ", set);
160
    for (way = 0; way < config.dmmu.nways; way++) {
161
      printf("  way %d: ", way);
162
      printf("vpn=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
163
      printf("lru=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
164
      printf("pl1=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
165
      printf("v=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
166
 
167
      printf("a=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
168
      printf("d=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
169
      printf("ure=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
170
      printf("uwe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
171
      printf("sre=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
172
      printf("swe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
173
      printf("ppn=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
174
    }
175
  }
176 535 markom
  if (start_set < end_set) printf("\n");
177 6 lampret
}

powered by: WebSVN 2.1.0

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