OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [mmu/] [immu.c] - Blame information for rev 429

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 74 lampret
/* immu.c -- Instruction MMU simulation
2
   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
/* IMMU model (not functional yet, currently just copy of data cache). */
21
 
22
#include "immu.h"
23
#include "abstract.h"
24
#include "stats.h"
25
#include "sprs.h"
26
#include "except.h"
27 425 markom
#include "sim-config.h"
28 74 lampret
 
29
extern int cont_run;
30
 
31
/* Insn MMU */
32
 
33
unsigned long immu_translate(unsigned long virtaddr)
34
{
35 429 markom
  unsigned long phyaddr = immu_simulate_tlb(virtaddr);
36
 
37
/*  printf("IMMU translate(%x) = %x\n", virtaddr, phyaddr);*/
38
  return phyaddr;
39 74 lampret
}
40
 
41
void itlb_info()
42
{
43 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
44
    printf("IMMU not implemented. Set UPR[IMP].\n");
45
    return;
46
  }
47 102 lampret
 
48 429 markom
  printf("Insn MMU %dKB: ", config.immu.nsets * config.immu.entrysize * config.immu.nways / 1024);
49
  printf("%d ways, %d sets, entry size %d bytes\n", config.immu.nways, config.immu.nsets, config.immu.entrysize);
50 74 lampret
}
51
 
52
/* First check if virtual address is covered by ITLB and if it is:
53
    - increment ITLB read hit stats,
54 425 markom
    - set 'lru' at this way to config.immu.ustates - 1 and
55 74 lampret
      decrement 'lru' of other ways unless they have reached 0,
56
    - check page access attributes and invoke IMMU page fault exception
57
      handler if necessary
58
   and if not:
59
    - increment ITLB read miss stats
60
    - find lru way and entry and invoke ITLB miss exception handler
61 425 markom
    - set 'lru' with config.immu.ustates - 1 and decrement 'lru' of other
62 74 lampret
      ways unless they have reached 0
63
*/
64
 
65 102 lampret
void itlb_status(int start_set)
66 74 lampret
{
67 429 markom
  int set;
68
  int way;
69
  int end_set = config.immu.nsets;
70 74 lampret
 
71 429 markom
  if (!testsprbits(SPR_UPR, SPR_UPR_IMP)) {
72
    printf("IMMU not implemented. Set UPR[IMP].\n");
73
    return;
74
  }
75 102 lampret
 
76 429 markom
  if ((start_set >= 0) && (start_set < end_set))
77
    end_set = start_set + 1;
78
  else
79
    start_set = 0;
80 74 lampret
 
81 429 markom
  printf("\nIMMU: ");
82
  /* Scan set(s) and way(s). */
83
  for (set = start_set; set < end_set; set++) {
84
    printf("\nSet %x: ", set);
85
    for (way = 0; way < config.immu.nways; way++) {
86
      printf("  way %d: ", way);
87
      printf("vpn=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_VPN));
88
      printf("lru=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU));
89
      printf("pl1=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_PL1));
90
      printf("v=%x ", getsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_V));
91
 
92
      printf("a=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_A));
93
      printf("d=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_D));
94
      printf("ure=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_URE));
95
      printf("uwe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_UWE));
96
      printf("sre=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SRE));
97
      printf("swe=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_SWE));
98
      printf("ppn=%x ", getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN));
99
    }
100
  }
101
  printf("\n");
102 74 lampret
}
103
 
104
unsigned long immu_simulate_tlb(unsigned long virtaddr)
105
{
106 429 markom
  int set, way = -1;
107
  int i;
108
  unsigned long tagaddr;
109
  unsigned long vpn;
110
 
111
  if (!(mfspr(SPR_SR) & SPR_SR_IME) || !(testsprbits(SPR_UPR, SPR_UPR_IMP)))
112
    return virtaddr;
113 74 lampret
 
114 429 markom
  /* Which set to check out? */
115
  set = (virtaddr / config.immu.pagesize) % config.immu.nsets;
116
  tagaddr = (virtaddr / config.immu.pagesize) / config.immu.nsets;
117
  vpn = virtaddr / config.immu.pagesize;
118 74 lampret
 
119 429 markom
  /* Scan all ways and try to find a matching way. */
120
  for (i = 0; i < config.immu.nways; i++)
121
    if ((getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_VPN) == vpn) &&
122
        testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_V))
123
      way = i;
124
 
125
  /* Did we find our tlb entry? */
126
  if (way >= 0) { /* Yes, we did. */
127
    immu_stats.fetch_tlbhit++;
128
    debug(5, "ITLB hit (virtaddr=%x).\n", virtaddr);
129 74 lampret
 
130 429 markom
    /* Set LRUs */
131
    for (i = 0; i < config.immu.nways; i++)
132
      if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
133
        setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
134
    setsprbits(SPR_ITLBMR_BASE(way) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
135 74 lampret
 
136 429 markom
    return getsprbits(SPR_ITLBTR_BASE(way) + set, SPR_ITLBTR_PPN) * config.immu.pagesize + (virtaddr % config.immu.pagesize);
137
  }
138
  else {  /* No, we didn't. */
139
    int minlru = config.immu.ustates - 1;
140
    int minway = 0;
141 74 lampret
 
142 429 markom
    immu_stats.fetch_tlbmiss++;
143 74 lampret
#if 0
144 429 markom
    for (i = 0; i < config.immu.nways; i++)
145
      if (getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) < minlru)
146
        minway = i;
147
 
148
    setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_VPN, vpn);
149
    for (i = 0; i < config.immu.nways; i++)
150
      if (testsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU))
151
        setsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU, getsprbits(SPR_ITLBMR_BASE(i) + set, SPR_ITLBMR_LRU) - 1);
152
    setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_LRU, config.immu.ustates - 1);
153
    setsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN, vpn); /* 1 to 1 */
154
    setsprbits(SPR_ITLBMR_BASE(minway) + set, SPR_ITLBMR_V, 1);
155 74 lampret
#endif
156 429 markom
    except_handle(EXCEPT_ITLBMISS, virtaddr);
157
    /* if tlb refill implemented in HW */
158
    /* return getsprbits(SPR_ITLBTR_BASE(minway) + set, SPR_ITLBTR_PPN) * config.immu.pagesize + (virtaddr % config.immu.pagesize); */
159
    return 0;
160
  }
161 74 lampret
}

powered by: WebSVN 2.1.0

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