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 886

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

powered by: WebSVN 2.1.0

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