2 * Microblaze MMU emulation for qemu.
4 * Copyright (c) 2009 Edgar E. Iglesias
5 * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 static unsigned int tlb_decode_size(unsigned int f
)
27 static const unsigned int sizes
[] = {
28 1 * 1024, 4 * 1024, 16 * 1024, 64 * 1024, 256 * 1024,
29 1 * 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024
31 assert(f
< ARRAY_SIZE(sizes
));
35 static void mmu_flush_idx(CPUMBState
*env
, unsigned int idx
)
37 struct microblaze_mmu
*mmu
= &env
->mmu
;
38 unsigned int tlb_size
;
39 uint32_t tlb_tag
, end
, t
;
41 t
= mmu
->rams
[RAM_TAG
][idx
];
45 tlb_tag
= t
& TLB_EPN_MASK
;
46 tlb_size
= tlb_decode_size((t
& TLB_PAGESZ_MASK
) >> 7);
47 end
= tlb_tag
+ tlb_size
;
49 while (tlb_tag
< end
) {
50 tlb_flush_page(env
, tlb_tag
);
51 tlb_tag
+= TARGET_PAGE_SIZE
;
55 static void mmu_change_pid(CPUMBState
*env
, unsigned int newpid
)
57 struct microblaze_mmu
*mmu
= &env
->mmu
;
62 qemu_log("Illegal rpid=%x\n", newpid
);
64 for (i
= 0; i
< ARRAY_SIZE(mmu
->rams
[RAM_TAG
]); i
++) {
65 /* Lookup and decode. */
66 t
= mmu
->rams
[RAM_TAG
][i
];
68 if (mmu
->tids
[i
] && ((mmu
->regs
[MMU_R_PID
] & 0xff) == mmu
->tids
[i
]))
69 mmu_flush_idx(env
, i
);
74 /* rw - 0 = read, 1 = write, 2 = fetch. */
75 unsigned int mmu_translate(struct microblaze_mmu
*mmu
,
76 struct microblaze_mmu_lookup
*lu
,
77 target_ulong vaddr
, int rw
, int mmu_idx
)
79 unsigned int i
, hit
= 0;
80 unsigned int tlb_ex
= 0, tlb_wr
= 0, tlb_zsel
;
81 unsigned int tlb_size
;
82 uint32_t tlb_tag
, tlb_rpn
, mask
, t0
;
85 for (i
= 0; i
< ARRAY_SIZE(mmu
->rams
[RAM_TAG
]); i
++) {
88 /* Lookup and decode. */
89 t
= mmu
->rams
[RAM_TAG
][i
];
90 D(qemu_log("TLB %d valid=%d\n", i
, t
& TLB_VALID
));
92 tlb_size
= tlb_decode_size((t
& TLB_PAGESZ_MASK
) >> 7);
93 if (tlb_size
< TARGET_PAGE_SIZE
) {
94 qemu_log("%d pages not supported\n", tlb_size
);
98 mask
= ~(tlb_size
- 1);
99 tlb_tag
= t
& TLB_EPN_MASK
;
100 if ((vaddr
& mask
) != (tlb_tag
& mask
)) {
101 D(qemu_log("TLB %d vaddr=%x != tag=%x\n",
102 i
, vaddr
& mask
, tlb_tag
& mask
));
106 && ((mmu
->regs
[MMU_R_PID
] & 0xff) != mmu
->tids
[i
])) {
107 D(qemu_log("TLB %d pid=%x != tid=%x\n",
108 i
, mmu
->regs
[MMU_R_PID
], mmu
->tids
[i
]));
112 /* Bring in the data part. */
113 d
= mmu
->rams
[RAM_DATA
][i
];
117 /* Now let's see if there is a zone that overrides the protbits. */
118 tlb_zsel
= (d
>> 4) & 0xf;
119 t0
= mmu
->regs
[MMU_R_ZPR
] >> (30 - (tlb_zsel
* 2));
122 if (tlb_zsel
> mmu
->c_mmu_zones
) {
123 qemu_log("tlb zone select out of range! %d\n", tlb_zsel
);
124 t0
= 1; /* Ignore. */
127 if (mmu
->c_mmu
== 1) {
128 t0
= 1; /* Zones are disabled. */
133 if (mmu_idx
== MMU_USER_IDX
)
137 if (mmu_idx
!= MMU_USER_IDX
) {
150 lu
->prot
= PAGE_READ
;
152 lu
->prot
|= PAGE_WRITE
;
156 lu
->prot
|=PAGE_EXEC
;
161 tlb_rpn
= d
& TLB_RPN_MASK
;
173 D(qemu_log("MMU vaddr=%x rw=%d tlb_wr=%d tlb_ex=%d hit=%d\n",
174 vaddr
, rw
, tlb_wr
, tlb_ex
, hit
));
178 /* Writes/reads to the MMU's special regs end up here. */
179 uint32_t mmu_read(CPUMBState
*env
, uint32_t rn
)
184 if (env
->mmu
.c_mmu
< 2 || !env
->mmu
.c_mmu_tlb_access
) {
185 qemu_log("MMU access on MMU-less system\n");
190 /* Reads to HI/LO trig reads from the mmu rams. */
193 if (!(env
->mmu
.c_mmu_tlb_access
& 1)) {
194 qemu_log("Invalid access to MMU reg %d\n", rn
);
198 i
= env
->mmu
.regs
[MMU_R_TLBX
] & 0xff;
199 r
= env
->mmu
.rams
[rn
& 1][i
];
200 if (rn
== MMU_R_TLBHI
)
201 env
->mmu
.regs
[MMU_R_PID
] = env
->mmu
.tids
[i
];
205 if (!(env
->mmu
.c_mmu_tlb_access
& 1)) {
206 qemu_log("Invalid access to MMU reg %d\n", rn
);
209 r
= env
->mmu
.regs
[rn
];
212 r
= env
->mmu
.regs
[rn
];
215 D(qemu_log("%s rn=%d=%x\n", __func__
, rn
, r
));
219 void mmu_write(CPUMBState
*env
, uint32_t rn
, uint32_t v
)
222 D(qemu_log("%s rn=%d=%x old=%x\n", __func__
, rn
, v
, env
->mmu
.regs
[rn
]));
224 if (env
->mmu
.c_mmu
< 2 || !env
->mmu
.c_mmu_tlb_access
) {
225 qemu_log("MMU access on MMU-less system\n");
230 /* Writes to HI/LO trig writes to the mmu rams. */
233 i
= env
->mmu
.regs
[MMU_R_TLBX
] & 0xff;
234 if (rn
== MMU_R_TLBHI
) {
235 if (i
< 3 && !(v
& TLB_VALID
) && qemu_loglevel_mask(~0))
236 qemu_log("invalidating index %x at pc=%x\n",
237 i
, env
->sregs
[SR_PC
]);
238 env
->mmu
.tids
[i
] = env
->mmu
.regs
[MMU_R_PID
] & 0xff;
239 mmu_flush_idx(env
, i
);
241 env
->mmu
.rams
[rn
& 1][i
] = v
;
243 D(qemu_log("%s ram[%d][%d]=%x\n", __func__
, rn
& 1, i
, v
));
246 if (env
->mmu
.c_mmu_tlb_access
<= 1) {
247 qemu_log("Invalid access to MMU reg %d\n", rn
);
251 /* Changes to the zone protection reg flush the QEMU TLB.
252 Fortunately, these are very uncommon. */
253 if (v
!= env
->mmu
.regs
[rn
]) {
256 env
->mmu
.regs
[rn
] = v
;
259 if (env
->mmu
.c_mmu_tlb_access
<= 1) {
260 qemu_log("Invalid access to MMU reg %d\n", rn
);
264 if (v
!= env
->mmu
.regs
[rn
]) {
265 mmu_change_pid(env
, v
);
266 env
->mmu
.regs
[rn
] = v
;
271 struct microblaze_mmu_lookup lu
;
274 if (env
->mmu
.c_mmu_tlb_access
<= 1) {
275 qemu_log("Invalid access to MMU reg %d\n", rn
);
279 hit
= mmu_translate(&env
->mmu
, &lu
,
280 v
& TLB_EPN_MASK
, 0, cpu_mmu_index(env
));
282 env
->mmu
.regs
[MMU_R_TLBX
] = lu
.idx
;
284 env
->mmu
.regs
[MMU_R_TLBX
] |= 0x80000000;
288 env
->mmu
.regs
[rn
] = v
;
293 void mmu_init(struct microblaze_mmu
*mmu
)
296 for (i
= 0; i
< ARRAY_SIZE(mmu
->regs
); i
++) {