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 638

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 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
    setsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
78
 
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 541 markom
    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
    int minlru = config.dmmu.ustates - 1;
88
    int minway = 0;
89
 
90
    dmmu_stats.loads_tlbmiss++;
91
#if 0
92
    for (i = 0; i < config.dmmu.nways; i++)
93
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) < minlru)
94
        minway = i;
95
 
96
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_VPN, vpn);
97
    for (i = 0; i < config.dmmu.nways; i++)
98
      if (getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU))
99
        setsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU, getsprbits(SPR_DTLBMR_BASE(i) + set, SPR_DTLBMR_LRU) - 1);
100
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_LRU, config.dmmu.ustates - 1);
101
    setsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN, vpn); /* 1 to 1 */
102
    setsprbits(SPR_DTLBMR_BASE(minway) + set, SPR_DTLBMR_V, 1);
103
#endif
104
    except_handle(EXCEPT_DTLBMISS, virtaddr);
105
    /* if tlb refill implemented in HW */
106
    /* return getsprbits(SPR_DTLBTR_BASE(minway) + set, SPR_DTLBTR_PPN) * config.dmmu.pagesize + (virtaddr % config.dmmu.pagesize); */
107 541 markom
    mem_cycles += config.dmmu.missdelay;
108 430 markom
    return 0;
109
  }
110
}
111
 
112
unsigned long dmmu_translate(unsigned long virtaddr, int write_access)
113
{
114
  unsigned long phyaddr = dmmu_simulate_tlb(virtaddr, write_access);
115 429 markom
 
116
/*  printf("DMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
117
  return phyaddr;
118 6 lampret
}
119
 
120
 
121 62 lampret
void dtlb_info()
122 6 lampret
{
123 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
124
    printf("DMMU not implemented. Set UPR[DMP].\n");
125
    return;
126
  }
127
 
128
  printf("Data MMU %dKB: ", config.dmmu.nsets * config.dmmu.entrysize * config.dmmu.nways / 1024);
129
  printf("%d ways, %d sets, entry size %d bytes\n", config.dmmu.nways, config.dmmu.nsets, config.dmmu.entrysize);
130 6 lampret
}
131
 
132 62 lampret
/* First check if virtual address is covered by DTLB and if it is:
133
    - increment DTLB read hit stats,
134 425 markom
    - set 'lru' at this way to config.dmmu.ustates - 1 and
135 6 lampret
      decrement 'lru' of other ways unless they have reached 0,
136 62 lampret
    - check page access attributes and invoke DMMU page fault exception
137
      handler if necessary
138 6 lampret
   and if not:
139 62 lampret
    - increment DTLB read miss stats
140
    - find lru way and entry and invoke DTLB miss exception handler
141 425 markom
    - set 'lru' with config.dmmu.ustates - 1 and decrement 'lru' of other
142 6 lampret
      ways unless they have reached 0
143
*/
144
 
145 102 lampret
void dtlb_status(int start_set)
146 6 lampret
{
147 429 markom
  int set;
148
  int way;
149
  int end_set = config.dmmu.nsets;
150 62 lampret
 
151 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_DMP)) {
152
    printf("DMMU not implemented. Set UPR[DMP].\n");
153
    return;
154
  }
155 102 lampret
 
156 429 markom
  if ((start_set >= 0) && (start_set < end_set))
157
    end_set = start_set + 1;
158
  else
159
    start_set = 0;
160 62 lampret
 
161 535 markom
  if (start_set < end_set) printf("\nDMMU: ");
162 429 markom
  /* Scan set(s) and way(s). */
163
  for (set = start_set; set < end_set; set++) {
164
    printf("\nSet %x: ", set);
165
    for (way = 0; way < config.dmmu.nways; way++) {
166
      printf("  way %d: ", way);
167
      printf("vpn=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_VPN));
168
      printf("lru=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_LRU));
169
      printf("pl1=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_PL1));
170
      printf("v=%x ", getsprbits(SPR_DTLBMR_BASE(way) + set, SPR_DTLBMR_V));
171
 
172
      printf("a=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_A));
173
      printf("d=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_D));
174
      printf("ure=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_URE));
175
      printf("uwe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_UWE));
176
      printf("sre=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SRE));
177
      printf("swe=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_SWE));
178
      printf("ppn=%x ", getsprbits(SPR_DTLBTR_BASE(way) + set, SPR_DTLBTR_PPN));
179
    }
180
  }
181 535 markom
  if (start_set < end_set) printf("\n");
182 6 lampret
}

powered by: WebSVN 2.1.0

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