/
mmu.c
97 lines (80 loc) · 2.73 KB
/
mmu.c
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
/*
* Milkymist SoC (Software)
* Copyright (C) 2012 Yann Sionneau <yann.sionneau@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <hal/mmu.h>
#include <base/mmu.h>
#define DTLB_CTRL_FLUSH_CMD (0x3)
#define DTLB_CTRL_DISABLE_CMD (0x5)
#define DTLB_CTRL_ENABLE_CMD (0x9)
/* @vpfn : virtual page frame number
* @pfn : physical page frame number
*/
inline void mmu_dtlb_map(unsigned int vpfn, unsigned int pfn)
{
asm volatile ("ori %0, %0, 1\n\t"
"wcsr tlbvaddr, %0" :: "r"(vpfn) : );
asm volatile ("ori %0, %0, 1\n\t"
"wcsr tlbpaddr, %0"::"r"(pfn):);
asm volatile ("xor r11, r11, r11\n\t"
"ori r11, r11, 0x5\n\t"
"wcsr tlbctrl, r11" ::: "r11");
}
inline void mmu_dtlb_invalidate_line(unsigned int vaddr)
{
asm volatile ("ori %0, %0, 1\n\t"
"wcsr tlbvaddr, %0"::"r"(vaddr):);
asm volatile ("xor r11, r11, r11\n\t"
"ori r11, r11, 0x21\n\t"
"wcsr tlbctrl, r11" ::: "r11");
}
inline void mmu_dtlb_invalidate(void)
{
register unsigned int cmd = DTLB_CTRL_FLUSH_CMD;
asm volatile("wcsr tlbctrl, %0" :: "r"(cmd) : );
}
/* This function activates the MMU
* then reads from virtual address "vaddr"
* and store the result in temporary variable "data".
* Then MMU is disactivated and the content of "data"
* is returned.
*/
unsigned int read_word_with_mmu_enabled(unsigned int vaddr)
{
register unsigned int data, cmd1, cmd2;
cmd1 = DTLB_CTRL_ENABLE_CMD;
cmd2 = DTLB_CTRL_DISABLE_CMD;
asm volatile("wcsr tlbctrl, %2\n\t" // Activates the MMU
"xor r0, r0, r0\n\t"
"lw %0, (%1+0)\n\t" // Reads from virtual address "addr"
"wcsr tlbctrl, %3\n\t" // Disactivates the MMU
"xor r0, r0, r0\n\t" : "=&r"(data) : "r"(vaddr), "r"(cmd1), "r"(cmd2) :
);
return data;
}
unsigned int write_word_with_mmu_enabled(register unsigned int vaddr, register unsigned int data)
{
asm volatile(
"xor r11, r11, r11\n\t"
"ori r11, r11, 0x11\n\t"
"wcsr tlbctrl, r11\n\t" // Activates the MMU
"xor r0, r0, r0\n\t"
"sw (%0 + 0), %1\n\t" // Reads from virtual address "addr"
"xor r11, r11, r11\n\t"
"ori r11, r11, 0x9\n\t"
"wcsr tlbctrl, r11\n\t" // Disactivates the MMU
"xor r0, r0, r0\n\t" :: "r"(vaddr), "r"(data) : "r11"
);
}