summaryrefslogtreecommitdiffstats
path: root/bsps/powerpc/haleakala/start/mmu_405.c
blob: ea45807b8f9072048f375a99016ef941786dd701 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* 
 *  Simple interface to the PowerPC 405 MMU
 *
 *   Michael Hamel ADInstruments 2008
 *
  */


#include <bsp.h>
#include <libcpu/powerpc-utility.h>
#include "mmu_405.h"
#include <inttypes.h>

/* #define qLogTLB */
/* #define qLogTLBDetails */


/*----------------------------- TLB handling -------------------------------- */
/* The following are in assembler in mmu_405asm.S  */
extern  void MMU_GetTLBEntry(uint8_t index, uint32_t* tagword, uint32_t* dataword, uint8_t* pid);
extern  void MMU_SetTLBEntry(uint8_t index, uint32_t hiword, uint32_t loword, uint8_t pid);
extern  void MMU_ClearTLBs(void);
extern  int16_t MMU_FindTLBEntry(uint32_t address);


enum { kNTLBs = 64 };    /* for 403GCX and 405 */

static bool    sFreeTLBs[kNTLBs];
static uint8_t  sLastIndex = 0;
static int    sNInUse = 0;

static void MMUFault(const char* what)
/* Used for all setup faults; these can't really be ignored */
{
  printk("\n>>>MMU fatal error %s\n",what);
  rtems_fatal_error_occurred(RTEMS_INTERNAL_ERROR);
}

static uint8_t AllocTLB(void)
{
  uint8_t index;
  
  index = sLastIndex;
  do {
    index++;
    if (index == kNTLBs)
      index = 0;
    if (index == sLastIndex)
      MMUFault("TLB table full");
  } while (! sFreeTLBs[index]);
  sFreeTLBs[index] = false;
  sLastIndex = index;
  sNInUse++;
  return index;
}

static void FreeTLB(uint8_t index)
{
  MMU_SetTLBEntry(index,0,0,0);
  sFreeTLBs[index] = true;
  sLastIndex = index-1;
  sNInUse--;
}


/*---------------------------- MMU operations ---------------------------------- */

int DataMissException(BSP_Exception_frame *f, unsigned int vector);
int InstructionMissException(BSP_Exception_frame *f, unsigned int vector);
int InstructionFetchException(BSP_Exception_frame *f, unsigned int vector);
void mmu_initialise(void);
int mmu_get_tlb_count(void);
uint8_t  mmu_new_processID(void);
uint8_t mmu_current_processID(void);

void
mmu_initialise(void)
/* Clear the TLBs and set up exception handlers for the MMU miss handlers */
{
  int i;
  
  MMU_ClearTLBs();
  for (i=0; i<kNTLBs; i++) {
    sFreeTLBs[i] = true;
    MMU_SetTLBEntry(i,0,0,0xFF);
  }
  ppc_exc_set_handler(ASM_ISI_VECTOR ,InstructionFetchException);
  ppc_exc_set_handler(ASM_BOOKE_ITLBMISS_VECTOR ,DataMissException);
  ppc_exc_set_handler(ASM_BOOKE_DTLBMISS_VECTOR ,InstructionMissException);
}

static void
MakeTLBEntries(uint32_t startAt, uint32_t nBytes, bool EX, bool WR, bool I, uint8_t PID)
{
  uint32_t mask, options, tagWord, dataWord;
  uint8_t   index, sizeCode, pid;
  
  if ((startAt & 0x3FF) != 0)
    MMUFault("TLB entry not on 1K boundary");
  if ((nBytes & 0x3FF) != 0)
    MMUFault("TLB size not on 1K boundary");
    
  options = 0;
  if (EX) options += 0x200;
  if (WR) options += 0x100;
  if (I) options += 5;
  
  #ifdef qLogTLB
    printk("TLB: make entries for $%X bytes from $%X..$%X PID %d",nBytes, startAt, startAt+nBytes-1, PID);
    if (EX) printk(" EX");
    if (WR) printk(" WR");
    if (I) printk(" I");
    printk("\n");
  #endif
  
  while (nBytes > 0) {
    /* Find the largest block we can base on this address */
    mask = 0x3FF;
    sizeCode = 0;
    while (mask < nBytes && ((startAt & mask)==0) && sizeCode < 8) {
      mask = (mask<<2) + 3;
      sizeCode++;
    }
    mask >>= 2;
    sizeCode--;
    
    /* Make a TLB entry describing this, ZSEL=0 */
    tagWord = startAt | (sizeCode<<7) | 0x40;
    dataWord = startAt | options;
    index = AllocTLB();
    MMU_SetTLBEntry( index , tagWord, dataWord, PID);
    
    {
      /* Paranoia: check that we can read that back... */
      uint8_t tdex, oldpid;
      
      oldpid = mmu_current_processID();
      mmu_set_processID(PID);
      tdex = MMU_FindTLBEntry(startAt);
      mmu_set_processID(oldpid);
      
      if (tdex != index) {
        printk(" Add TLB %d: At %" PRIx32 " for $%" PRIx32
               " sizecode %d tagWord $%" PRIx32 "  ",
               index, startAt, mask+1,sizeCode,tagWord);
        printk(" -- find failed, %d/%d!\n",tdex,index);
        MMU_GetTLBEntry(index, &tagWord, &dataWord, &pid);
        printk(" -- reads back $%" PRIx32 " : $%" PRIx32
               ", PID %d\n",tagWord,dataWord,pid);
      } else {
        #ifdef qLogTLBDetails
        printk(" Add TLB %d: At %X for $%X sizecode %d tagWord $%X\n",index, startAt, mask+1,sizeCode,tagWord);
        #endif
      }
    }
  
    /* Subtract block from startAddr and nBytes */
    mask++;    /* Convert to a byte count */
    startAt += mask;
    nBytes -= mask;
  }
  #ifdef qLogTLB
    printk(" %d in use\n",sNInUse);
  #endif
}

void
mmu_remove_space(uint32_t startAt, uint32_t endAt)
{
  int16_t index;
  int32_t size;
  uint32_t tagword, dataword, nBytes;
  uint8_t  pid, sCode;
  
  nBytes = endAt - startAt;
  
  #ifdef qLogTLB
  printk("TLB: delete entries for $%X bytes from $%X\n",nBytes,startAt);
  #endif
  
  while (nBytes > 0) {
    index = MMU_FindTLBEntry( (uint32_t)startAt );
    size = 1024;
    if (index >= 0) {
      MMU_GetTLBEntry(index, &tagword, &dataword, &pid);
      if ((tagword & 0x40) == 0)
        MMUFault("Undefine failed: redundant entries?");
      if ((tagword & 0xFFFFFC00) != (uint32_t)startAt)
        MMUFault("Undefine not on TLB boundary");
      FreeTLB(index);
      sCode = (tagword >> 7) & 7;
      while (sCode > 0) {
        size <<= 2;
        sCode--;
      }
      #ifdef qLogTLBDetails
      printk(" Free TLB %d: At %X for $%X\n",index, startAt, size);
      #endif
    }
    startAt += size;
    nBytes -= size;
  }
}

void
mmu_add_space(uint32_t startAddr, uint32_t endAddr, MMUAccessType permissions, uint8_t processID)
/* Convert accesstype to write-enable, executable, and cache-inhibit bits */
{
  bool EX, WR, I;
  
  EX = false;
  WR = false;
  I = false;
  switch (permissions) {
    case executable     : EX = true;  break;
    case readOnlyData     : break;
    case readOnlyNoCache   : I = true; break;
    case readWriteData     : WR = true; break;
    case readWriteNoCache   : WR = true; I= true; break;
    case readWriteExecutable: WR = true; EX = true; break;
  }
  MakeTLBEntries( (uint32_t)startAddr, (uint32_t)(endAddr-startAddr+1), EX, WR, I, processID);
}

int
mmu_get_tlb_count(void)
{
  return sNInUse;
}

/*---------------------------- CPU process ID handling ----------------------------------
 * Really dumb system where we just hand out sequential numbers and eventually fail
 * As long as we only use 8-9 processes this isn't a problem */

static uint8_t sNextPID = 1;

#define SPR_PID    0x3B1

uint8_t  mmu_new_processID(void)
{
  return sNextPID++;
}

void mmu_free_processID(uint8_t freeThis)
{
}

uint8_t mmu_current_processID(void)
{
  return PPC_SPECIAL_PURPOSE_REGISTER(SPR_PID);
}

uint8_t mmu_set_processID(uint8_t newID)
{
  uint8_t prev = mmu_current_processID();
  PPC_SET_SPECIAL_PURPOSE_REGISTER(SPR_PID,newID);
  return prev;
}


/* ------------------ Fault handlers ------------------ */

#define SPR_ESR    0x3D4
#define SPR_DEAR  0x3D5

enum { kESR_DST = 0x00800000 };

int DataMissException(BSP_Exception_frame *f, unsigned int vector)
{
  uint32_t addr, excSyn;
  
  addr = PPC_SPECIAL_PURPOSE_REGISTER(SPR_DEAR);
  excSyn  = PPC_SPECIAL_PURPOSE_REGISTER(SPR_ESR);
  if (excSyn & kESR_DST) printk("\n---Data write to $%" PRIx32
      " attempted at $%" PRIxPTR "\n",addr,f->EXC_SRR0);
            else printk("\n---Data read from $%" PRIx32 " attempted at $%"
                        PRIxPTR "\n",addr,f->EXC_SRR0);
  return -1;
}

int InstructionMissException(BSP_Exception_frame *f, unsigned int vector)
{
  printk("\n---Instruction fetch attempted from $%" PRIxPTR ", no TLB exists\n",
         f->EXC_SRR0);
  return -1;
}

int InstructionFetchException(BSP_Exception_frame *f, unsigned int vector)
{
  printk("\n---Instruction fetch attempted from $%" PRIxPTR
         ", TLB is no-execute\n",f->EXC_SRR0);
  return -1;
}