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 535

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

powered by: WebSVN 2.1.0

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