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 713

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

powered by: WebSVN 2.1.0

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