Statistics
| Branch: | Revision:

root / target-microblaze / mmu.c @ 94a943ef

History | View | Annotate | Download (7.6 kB)

1 afeeceb0 Edgar E. Iglesias
/*
2 afeeceb0 Edgar E. Iglesias
 *  Microblaze MMU emulation for qemu.
3 afeeceb0 Edgar E. Iglesias
 *
4 afeeceb0 Edgar E. Iglesias
 *  Copyright (c) 2009 Edgar E. Iglesias
5 afeeceb0 Edgar E. Iglesias
 *
6 afeeceb0 Edgar E. Iglesias
 * This library is free software; you can redistribute it and/or
7 afeeceb0 Edgar E. Iglesias
 * modify it under the terms of the GNU Lesser General Public
8 afeeceb0 Edgar E. Iglesias
 * License as published by the Free Software Foundation; either
9 afeeceb0 Edgar E. Iglesias
 * version 2 of the License, or (at your option) any later version.
10 afeeceb0 Edgar E. Iglesias
 *
11 afeeceb0 Edgar E. Iglesias
 * This library is distributed in the hope that it will be useful,
12 afeeceb0 Edgar E. Iglesias
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 afeeceb0 Edgar E. Iglesias
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 afeeceb0 Edgar E. Iglesias
 * Lesser General Public License for more details.
15 afeeceb0 Edgar E. Iglesias
 *
16 afeeceb0 Edgar E. Iglesias
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 afeeceb0 Edgar E. Iglesias
 */
19 afeeceb0 Edgar E. Iglesias
#include <stdio.h>
20 afeeceb0 Edgar E. Iglesias
#include <stdlib.h>
21 afeeceb0 Edgar E. Iglesias
#include <assert.h>
22 afeeceb0 Edgar E. Iglesias
23 afeeceb0 Edgar E. Iglesias
#include "config.h"
24 afeeceb0 Edgar E. Iglesias
#include "cpu.h"
25 afeeceb0 Edgar E. Iglesias
#include "exec-all.h"
26 afeeceb0 Edgar E. Iglesias
27 afeeceb0 Edgar E. Iglesias
#define D(x)
28 afeeceb0 Edgar E. Iglesias
29 afeeceb0 Edgar E. Iglesias
static unsigned int tlb_decode_size(unsigned int f)
30 afeeceb0 Edgar E. Iglesias
{
31 afeeceb0 Edgar E. Iglesias
    static const unsigned int sizes[] = {
32 afeeceb0 Edgar E. Iglesias
        1 * 1024, 4 * 1024, 16 * 1024, 64 * 1024, 256 * 1024,
33 afeeceb0 Edgar E. Iglesias
        1 * 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024
34 afeeceb0 Edgar E. Iglesias
    };
35 afeeceb0 Edgar E. Iglesias
    assert(f < ARRAY_SIZE(sizes));
36 afeeceb0 Edgar E. Iglesias
    return sizes[f];
37 afeeceb0 Edgar E. Iglesias
}
38 afeeceb0 Edgar E. Iglesias
39 6b2fce90 Edgar E. Iglesias
static void mmu_flush_idx(CPUState *env, unsigned int idx)
40 afeeceb0 Edgar E. Iglesias
{
41 afeeceb0 Edgar E. Iglesias
    struct microblaze_mmu *mmu = &env->mmu;
42 afeeceb0 Edgar E. Iglesias
    unsigned int tlb_size;
43 afeeceb0 Edgar E. Iglesias
    uint32_t tlb_tag, end, t;
44 afeeceb0 Edgar E. Iglesias
45 afeeceb0 Edgar E. Iglesias
    t = mmu->rams[RAM_TAG][idx];
46 afeeceb0 Edgar E. Iglesias
    if (!(t & TLB_VALID))
47 afeeceb0 Edgar E. Iglesias
        return;
48 afeeceb0 Edgar E. Iglesias
49 afeeceb0 Edgar E. Iglesias
    tlb_tag = t & TLB_EPN_MASK;
50 afeeceb0 Edgar E. Iglesias
    tlb_size = tlb_decode_size((t & TLB_PAGESZ_MASK) >> 7);
51 afeeceb0 Edgar E. Iglesias
    end = tlb_tag + tlb_size;
52 afeeceb0 Edgar E. Iglesias
53 afeeceb0 Edgar E. Iglesias
    while (tlb_tag < end) {
54 afeeceb0 Edgar E. Iglesias
        tlb_flush_page(env, tlb_tag);
55 afeeceb0 Edgar E. Iglesias
        tlb_tag += TARGET_PAGE_SIZE;
56 afeeceb0 Edgar E. Iglesias
    }
57 afeeceb0 Edgar E. Iglesias
}
58 afeeceb0 Edgar E. Iglesias
59 afeeceb0 Edgar E. Iglesias
static void mmu_change_pid(CPUState *env, unsigned int newpid) 
60 afeeceb0 Edgar E. Iglesias
{
61 afeeceb0 Edgar E. Iglesias
    struct microblaze_mmu *mmu = &env->mmu;
62 afeeceb0 Edgar E. Iglesias
    unsigned int i;
63 afeeceb0 Edgar E. Iglesias
    unsigned int tlb_size;
64 afeeceb0 Edgar E. Iglesias
    uint32_t tlb_tag, mask, t;
65 afeeceb0 Edgar E. Iglesias
66 afeeceb0 Edgar E. Iglesias
    if (newpid & ~0xff)
67 afeeceb0 Edgar E. Iglesias
        qemu_log("Illegal rpid=%x\n", newpid);
68 afeeceb0 Edgar E. Iglesias
69 afeeceb0 Edgar E. Iglesias
    for (i = 0; i < ARRAY_SIZE(mmu->rams[RAM_TAG]); i++) {
70 afeeceb0 Edgar E. Iglesias
        /* Lookup and decode.  */
71 afeeceb0 Edgar E. Iglesias
        t = mmu->rams[RAM_TAG][i];
72 afeeceb0 Edgar E. Iglesias
        if (t & TLB_VALID) {
73 afeeceb0 Edgar E. Iglesias
            tlb_size = tlb_decode_size((t & TLB_PAGESZ_MASK) >> 7);
74 afeeceb0 Edgar E. Iglesias
            mask = ~(tlb_size - 1);
75 afeeceb0 Edgar E. Iglesias
76 afeeceb0 Edgar E. Iglesias
            tlb_tag = t & TLB_EPN_MASK;
77 afeeceb0 Edgar E. Iglesias
            if (mmu->tids[i] && ((mmu->regs[MMU_R_PID] & 0xff) == mmu->tids[i]))
78 afeeceb0 Edgar E. Iglesias
                mmu_flush_idx(env, i);
79 afeeceb0 Edgar E. Iglesias
        }
80 afeeceb0 Edgar E. Iglesias
    }
81 afeeceb0 Edgar E. Iglesias
}
82 afeeceb0 Edgar E. Iglesias
83 afeeceb0 Edgar E. Iglesias
/* rw - 0 = read, 1 = write, 2 = fetch.  */
84 afeeceb0 Edgar E. Iglesias
unsigned int mmu_translate(struct microblaze_mmu *mmu,
85 afeeceb0 Edgar E. Iglesias
                           struct microblaze_mmu_lookup *lu,
86 afeeceb0 Edgar E. Iglesias
                           target_ulong vaddr, int rw, int mmu_idx)
87 afeeceb0 Edgar E. Iglesias
{
88 afeeceb0 Edgar E. Iglesias
    unsigned int i, hit = 0;
89 afeeceb0 Edgar E. Iglesias
    unsigned int tlb_ex = 0, tlb_wr = 0, tlb_zsel;
90 afeeceb0 Edgar E. Iglesias
    unsigned int tlb_size;
91 afeeceb0 Edgar E. Iglesias
    uint32_t tlb_tag, tlb_rpn, mask, t0;
92 afeeceb0 Edgar E. Iglesias
93 afeeceb0 Edgar E. Iglesias
    lu->err = ERR_MISS;
94 afeeceb0 Edgar E. Iglesias
    for (i = 0; i < ARRAY_SIZE(mmu->rams[RAM_TAG]); i++) {
95 afeeceb0 Edgar E. Iglesias
        uint32_t t, d;
96 afeeceb0 Edgar E. Iglesias
97 afeeceb0 Edgar E. Iglesias
        /* Lookup and decode.  */
98 afeeceb0 Edgar E. Iglesias
        t = mmu->rams[RAM_TAG][i];
99 afeeceb0 Edgar E. Iglesias
        D(qemu_log("TLB %d valid=%d\n", i, t & TLB_VALID));
100 afeeceb0 Edgar E. Iglesias
        if (t & TLB_VALID) {
101 afeeceb0 Edgar E. Iglesias
            tlb_size = tlb_decode_size((t & TLB_PAGESZ_MASK) >> 7);
102 afeeceb0 Edgar E. Iglesias
            if (tlb_size < TARGET_PAGE_SIZE) {
103 afeeceb0 Edgar E. Iglesias
                qemu_log("%d pages not supported\n", tlb_size);
104 afeeceb0 Edgar E. Iglesias
                abort();
105 afeeceb0 Edgar E. Iglesias
            }
106 afeeceb0 Edgar E. Iglesias
107 afeeceb0 Edgar E. Iglesias
            mask = ~(tlb_size - 1);
108 afeeceb0 Edgar E. Iglesias
            tlb_tag = t & TLB_EPN_MASK;
109 afeeceb0 Edgar E. Iglesias
            if ((vaddr & mask) != (tlb_tag & mask)) {
110 afeeceb0 Edgar E. Iglesias
                D(qemu_log("TLB %d vaddr=%x != tag=%x\n",
111 afeeceb0 Edgar E. Iglesias
                           i, vaddr & mask, tlb_tag & mask));
112 afeeceb0 Edgar E. Iglesias
                continue;
113 afeeceb0 Edgar E. Iglesias
            }
114 afeeceb0 Edgar E. Iglesias
            if (mmu->tids[i]
115 afeeceb0 Edgar E. Iglesias
                && ((mmu->regs[MMU_R_PID] & 0xff) != mmu->tids[i])) {
116 afeeceb0 Edgar E. Iglesias
                D(qemu_log("TLB %d pid=%x != tid=%x\n",
117 afeeceb0 Edgar E. Iglesias
                           i, mmu->regs[MMU_R_PID], mmu->tids[i]));
118 afeeceb0 Edgar E. Iglesias
                continue;
119 afeeceb0 Edgar E. Iglesias
            }
120 afeeceb0 Edgar E. Iglesias
121 afeeceb0 Edgar E. Iglesias
            /* Bring in the data part.  */
122 afeeceb0 Edgar E. Iglesias
            d = mmu->rams[RAM_DATA][i];
123 afeeceb0 Edgar E. Iglesias
            tlb_ex = d & TLB_EX;
124 afeeceb0 Edgar E. Iglesias
            tlb_wr = d & TLB_WR;
125 afeeceb0 Edgar E. Iglesias
126 afeeceb0 Edgar E. Iglesias
            /* Now lets see if there is a zone that overrides the protbits.  */
127 afeeceb0 Edgar E. Iglesias
            tlb_zsel = (d >> 4) & 0xf;
128 afeeceb0 Edgar E. Iglesias
            t0 = mmu->regs[MMU_R_ZPR] >> (30 - (tlb_zsel * 2));
129 afeeceb0 Edgar E. Iglesias
            t0 &= 0x3;
130 afeeceb0 Edgar E. Iglesias
            switch (t0) {
131 afeeceb0 Edgar E. Iglesias
                case 0:
132 afeeceb0 Edgar E. Iglesias
                    if (mmu_idx == MMU_USER_IDX)
133 afeeceb0 Edgar E. Iglesias
                        continue;
134 afeeceb0 Edgar E. Iglesias
                    break;
135 afeeceb0 Edgar E. Iglesias
                case 2:
136 afeeceb0 Edgar E. Iglesias
                    if (mmu_idx != MMU_USER_IDX) {
137 afeeceb0 Edgar E. Iglesias
                        tlb_ex = 1;
138 afeeceb0 Edgar E. Iglesias
                        tlb_wr = 1;
139 afeeceb0 Edgar E. Iglesias
                    }
140 afeeceb0 Edgar E. Iglesias
                    break;
141 afeeceb0 Edgar E. Iglesias
                case 3:
142 afeeceb0 Edgar E. Iglesias
                    tlb_ex = 1;
143 afeeceb0 Edgar E. Iglesias
                    tlb_wr = 1;
144 afeeceb0 Edgar E. Iglesias
                    break;
145 afeeceb0 Edgar E. Iglesias
            }
146 afeeceb0 Edgar E. Iglesias
147 afeeceb0 Edgar E. Iglesias
148 afeeceb0 Edgar E. Iglesias
            lu->err = ERR_PROT;
149 afeeceb0 Edgar E. Iglesias
            lu->prot = PAGE_READ;
150 afeeceb0 Edgar E. Iglesias
            if (tlb_wr)
151 afeeceb0 Edgar E. Iglesias
                lu->prot |= PAGE_WRITE;
152 afeeceb0 Edgar E. Iglesias
            else if (rw == 1)
153 afeeceb0 Edgar E. Iglesias
                goto done;
154 afeeceb0 Edgar E. Iglesias
            if (tlb_ex)
155 afeeceb0 Edgar E. Iglesias
                lu->prot |=PAGE_EXEC;
156 afeeceb0 Edgar E. Iglesias
            else if (rw == 2) {
157 afeeceb0 Edgar E. Iglesias
                goto done;
158 afeeceb0 Edgar E. Iglesias
            }
159 afeeceb0 Edgar E. Iglesias
160 afeeceb0 Edgar E. Iglesias
            tlb_rpn = d & TLB_RPN_MASK;
161 afeeceb0 Edgar E. Iglesias
162 afeeceb0 Edgar E. Iglesias
            lu->vaddr = tlb_tag;
163 afeeceb0 Edgar E. Iglesias
            lu->paddr = tlb_rpn;
164 afeeceb0 Edgar E. Iglesias
            lu->size = tlb_size;
165 afeeceb0 Edgar E. Iglesias
            lu->err = ERR_HIT;
166 afeeceb0 Edgar E. Iglesias
            lu->idx = i;
167 afeeceb0 Edgar E. Iglesias
            hit = 1;
168 afeeceb0 Edgar E. Iglesias
            goto done;
169 afeeceb0 Edgar E. Iglesias
        }
170 afeeceb0 Edgar E. Iglesias
    }
171 afeeceb0 Edgar E. Iglesias
done:
172 afeeceb0 Edgar E. Iglesias
    D(qemu_log("MMU vaddr=%x rw=%d tlb_wr=%d tlb_ex=%d hit=%d\n",
173 afeeceb0 Edgar E. Iglesias
              vaddr, rw, tlb_wr, tlb_ex, hit));
174 afeeceb0 Edgar E. Iglesias
    return hit;
175 afeeceb0 Edgar E. Iglesias
}
176 afeeceb0 Edgar E. Iglesias
177 afeeceb0 Edgar E. Iglesias
/* Writes/reads to the MMU's special regs end up here.  */
178 afeeceb0 Edgar E. Iglesias
uint32_t mmu_read(CPUState *env, uint32_t rn)
179 afeeceb0 Edgar E. Iglesias
{
180 afeeceb0 Edgar E. Iglesias
    unsigned int i;
181 afeeceb0 Edgar E. Iglesias
    uint32_t r;
182 afeeceb0 Edgar E. Iglesias
183 afeeceb0 Edgar E. Iglesias
    switch (rn) {
184 afeeceb0 Edgar E. Iglesias
        /* Reads to HI/LO trig reads from the mmu rams.  */
185 afeeceb0 Edgar E. Iglesias
        case MMU_R_TLBLO:
186 afeeceb0 Edgar E. Iglesias
        case MMU_R_TLBHI:
187 afeeceb0 Edgar E. Iglesias
            i = env->mmu.regs[MMU_R_TLBX] & 0xff;
188 afeeceb0 Edgar E. Iglesias
            r = env->mmu.rams[rn & 1][i];
189 afeeceb0 Edgar E. Iglesias
            if (rn == MMU_R_TLBHI)
190 afeeceb0 Edgar E. Iglesias
                env->mmu.regs[MMU_R_PID] = env->mmu.tids[i];
191 afeeceb0 Edgar E. Iglesias
            break;
192 afeeceb0 Edgar E. Iglesias
        default:
193 afeeceb0 Edgar E. Iglesias
            r = env->mmu.regs[rn];
194 afeeceb0 Edgar E. Iglesias
            break;
195 afeeceb0 Edgar E. Iglesias
    }
196 afeeceb0 Edgar E. Iglesias
    D(qemu_log("%s rn=%d=%x\n", __func__, rn, r));
197 afeeceb0 Edgar E. Iglesias
    return r;
198 afeeceb0 Edgar E. Iglesias
}
199 afeeceb0 Edgar E. Iglesias
200 afeeceb0 Edgar E. Iglesias
void mmu_write(CPUState *env, uint32_t rn, uint32_t v)
201 afeeceb0 Edgar E. Iglesias
{
202 afeeceb0 Edgar E. Iglesias
    unsigned int i;
203 afeeceb0 Edgar E. Iglesias
    D(qemu_log("%s rn=%d=%x old=%x\n", __func__, rn, v, env->mmu.regs[rn]));
204 afeeceb0 Edgar E. Iglesias
205 afeeceb0 Edgar E. Iglesias
    switch (rn) {
206 afeeceb0 Edgar E. Iglesias
        /* Writes to HI/LO trig writes to the mmu rams.  */
207 afeeceb0 Edgar E. Iglesias
        case MMU_R_TLBLO:
208 afeeceb0 Edgar E. Iglesias
        case MMU_R_TLBHI:
209 afeeceb0 Edgar E. Iglesias
            i = env->mmu.regs[MMU_R_TLBX] & 0xff;
210 afeeceb0 Edgar E. Iglesias
            if (rn == MMU_R_TLBHI) {
211 afeeceb0 Edgar E. Iglesias
                if (i < 3 && !(v & TLB_VALID) && qemu_loglevel_mask(~0))
212 afeeceb0 Edgar E. Iglesias
                    qemu_log("invalidating index %x at pc=%x\n",
213 afeeceb0 Edgar E. Iglesias
                             i, env->sregs[SR_PC]);
214 afeeceb0 Edgar E. Iglesias
                env->mmu.tids[i] = env->mmu.regs[MMU_R_PID] & 0xff;
215 afeeceb0 Edgar E. Iglesias
                mmu_flush_idx(env, i);
216 afeeceb0 Edgar E. Iglesias
            }
217 afeeceb0 Edgar E. Iglesias
            env->mmu.rams[rn & 1][i] = v;
218 afeeceb0 Edgar E. Iglesias
219 afeeceb0 Edgar E. Iglesias
            D(qemu_log("%s ram[%d][%d]=%x\n", __func__, rn & 1, i, v));
220 afeeceb0 Edgar E. Iglesias
            break;
221 afeeceb0 Edgar E. Iglesias
        case MMU_R_ZPR:
222 d0f3654f Edgar E. Iglesias
            /* Changes to the zone protection reg flush the QEMU TLB.
223 d0f3654f Edgar E. Iglesias
               Fortunately, these are very uncommon.  */
224 d0f3654f Edgar E. Iglesias
            if (v != env->mmu.regs[rn]) {
225 d0f3654f Edgar E. Iglesias
                tlb_flush(env, 1);
226 d0f3654f Edgar E. Iglesias
            }
227 d0f3654f Edgar E. Iglesias
            env->mmu.regs[rn] = v;
228 d0f3654f Edgar E. Iglesias
            break;
229 afeeceb0 Edgar E. Iglesias
        case MMU_R_PID:
230 afeeceb0 Edgar E. Iglesias
            if (v != env->mmu.regs[rn]) {
231 afeeceb0 Edgar E. Iglesias
                mmu_change_pid(env, v);
232 afeeceb0 Edgar E. Iglesias
                env->mmu.regs[rn] = v;
233 afeeceb0 Edgar E. Iglesias
            }
234 afeeceb0 Edgar E. Iglesias
            break;
235 afeeceb0 Edgar E. Iglesias
        case MMU_R_TLBSX:
236 afeeceb0 Edgar E. Iglesias
        {
237 afeeceb0 Edgar E. Iglesias
            struct microblaze_mmu_lookup lu;
238 afeeceb0 Edgar E. Iglesias
            int hit;
239 afeeceb0 Edgar E. Iglesias
            hit = mmu_translate(&env->mmu, &lu,
240 afeeceb0 Edgar E. Iglesias
                                v & TLB_EPN_MASK, 0, cpu_mmu_index(env));
241 afeeceb0 Edgar E. Iglesias
            if (hit) {
242 afeeceb0 Edgar E. Iglesias
                env->mmu.regs[MMU_R_TLBX] = lu.idx;
243 afeeceb0 Edgar E. Iglesias
            } else
244 afeeceb0 Edgar E. Iglesias
                env->mmu.regs[MMU_R_TLBX] |= 0x80000000;
245 afeeceb0 Edgar E. Iglesias
            break;
246 afeeceb0 Edgar E. Iglesias
        }
247 afeeceb0 Edgar E. Iglesias
        default:
248 afeeceb0 Edgar E. Iglesias
            env->mmu.regs[rn] = v;
249 afeeceb0 Edgar E. Iglesias
            break;
250 afeeceb0 Edgar E. Iglesias
   }
251 afeeceb0 Edgar E. Iglesias
}
252 afeeceb0 Edgar E. Iglesias
253 afeeceb0 Edgar E. Iglesias
void mmu_init(struct microblaze_mmu *mmu)
254 afeeceb0 Edgar E. Iglesias
{
255 afeeceb0 Edgar E. Iglesias
    memset(mmu, 0, sizeof *mmu);
256 afeeceb0 Edgar E. Iglesias
}