Statistics
| Branch: | Revision:

root / hw / spapr_hcall.c @ 439a97cc

History | View | Annotate | Download (14.1 kB)

1 9fdf0c29 David Gibson
#include "sysemu.h"
2 9fdf0c29 David Gibson
#include "cpu.h"
3 9fdf0c29 David Gibson
#include "qemu-char.h"
4 f43e3525 David Gibson
#include "sysemu.h"
5 f43e3525 David Gibson
#include "qemu-char.h"
6 f43e3525 David Gibson
#include "exec-all.h"
7 ed120055 David Gibson
#include "exec.h"
8 ed120055 David Gibson
#include "helper_regs.h"
9 9fdf0c29 David Gibson
#include "hw/spapr.h"
10 9fdf0c29 David Gibson
11 f43e3525 David Gibson
#define HPTES_PER_GROUP 8
12 f43e3525 David Gibson
13 f43e3525 David Gibson
#define HPTE_V_SSIZE_SHIFT      62
14 f43e3525 David Gibson
#define HPTE_V_AVPN_SHIFT       7
15 f43e3525 David Gibson
#define HPTE_V_AVPN             0x3fffffffffffff80ULL
16 f43e3525 David Gibson
#define HPTE_V_AVPN_VAL(x)      (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
17 f43e3525 David Gibson
#define HPTE_V_COMPARE(x, y)    (!(((x) ^ (y)) & 0xffffffffffffff80UL))
18 f43e3525 David Gibson
#define HPTE_V_BOLTED           0x0000000000000010ULL
19 f43e3525 David Gibson
#define HPTE_V_LOCK             0x0000000000000008ULL
20 f43e3525 David Gibson
#define HPTE_V_LARGE            0x0000000000000004ULL
21 f43e3525 David Gibson
#define HPTE_V_SECONDARY        0x0000000000000002ULL
22 f43e3525 David Gibson
#define HPTE_V_VALID            0x0000000000000001ULL
23 f43e3525 David Gibson
24 f43e3525 David Gibson
#define HPTE_R_PP0              0x8000000000000000ULL
25 f43e3525 David Gibson
#define HPTE_R_TS               0x4000000000000000ULL
26 f43e3525 David Gibson
#define HPTE_R_KEY_HI           0x3000000000000000ULL
27 f43e3525 David Gibson
#define HPTE_R_RPN_SHIFT        12
28 f43e3525 David Gibson
#define HPTE_R_RPN              0x3ffffffffffff000ULL
29 f43e3525 David Gibson
#define HPTE_R_FLAGS            0x00000000000003ffULL
30 f43e3525 David Gibson
#define HPTE_R_PP               0x0000000000000003ULL
31 f43e3525 David Gibson
#define HPTE_R_N                0x0000000000000004ULL
32 f43e3525 David Gibson
#define HPTE_R_G                0x0000000000000008ULL
33 f43e3525 David Gibson
#define HPTE_R_M                0x0000000000000010ULL
34 f43e3525 David Gibson
#define HPTE_R_I                0x0000000000000020ULL
35 f43e3525 David Gibson
#define HPTE_R_W                0x0000000000000040ULL
36 f43e3525 David Gibson
#define HPTE_R_WIMG             0x0000000000000078ULL
37 f43e3525 David Gibson
#define HPTE_R_C                0x0000000000000080ULL
38 f43e3525 David Gibson
#define HPTE_R_R                0x0000000000000100ULL
39 f43e3525 David Gibson
#define HPTE_R_KEY_LO           0x0000000000000e00ULL
40 f43e3525 David Gibson
41 f43e3525 David Gibson
#define HPTE_V_1TB_SEG          0x4000000000000000ULL
42 f43e3525 David Gibson
#define HPTE_V_VRMA_MASK        0x4001ffffff000000ULL
43 f43e3525 David Gibson
44 f43e3525 David Gibson
#define HPTE_V_HVLOCK           0x40ULL
45 f43e3525 David Gibson
46 f43e3525 David Gibson
static inline int lock_hpte(void *hpte, target_ulong bits)
47 f43e3525 David Gibson
{
48 f43e3525 David Gibson
    uint64_t pteh;
49 f43e3525 David Gibson
50 f43e3525 David Gibson
    pteh = ldq_p(hpte);
51 f43e3525 David Gibson
52 f43e3525 David Gibson
    /* We're protected by qemu's global lock here */
53 f43e3525 David Gibson
    if (pteh & bits) {
54 f43e3525 David Gibson
        return 0;
55 f43e3525 David Gibson
    }
56 f43e3525 David Gibson
    stq_p(hpte, pteh | HPTE_V_HVLOCK);
57 f43e3525 David Gibson
    return 1;
58 f43e3525 David Gibson
}
59 f43e3525 David Gibson
60 f43e3525 David Gibson
static target_ulong compute_tlbie_rb(target_ulong v, target_ulong r,
61 f43e3525 David Gibson
                                     target_ulong pte_index)
62 f43e3525 David Gibson
{
63 f43e3525 David Gibson
    target_ulong rb, va_low;
64 f43e3525 David Gibson
65 f43e3525 David Gibson
    rb = (v & ~0x7fULL) << 16; /* AVA field */
66 f43e3525 David Gibson
    va_low = pte_index >> 3;
67 f43e3525 David Gibson
    if (v & HPTE_V_SECONDARY) {
68 f43e3525 David Gibson
        va_low = ~va_low;
69 f43e3525 David Gibson
    }
70 f43e3525 David Gibson
    /* xor vsid from AVA */
71 f43e3525 David Gibson
    if (!(v & HPTE_V_1TB_SEG)) {
72 f43e3525 David Gibson
        va_low ^= v >> 12;
73 f43e3525 David Gibson
    } else {
74 f43e3525 David Gibson
        va_low ^= v >> 24;
75 f43e3525 David Gibson
    }
76 f43e3525 David Gibson
    va_low &= 0x7ff;
77 f43e3525 David Gibson
    if (v & HPTE_V_LARGE) {
78 f43e3525 David Gibson
        rb |= 1;                         /* L field */
79 f43e3525 David Gibson
#if 0 /* Disable that P7 specific bit for now */
80 f43e3525 David Gibson
        if (r & 0xff000) {
81 f43e3525 David Gibson
            /* non-16MB large page, must be 64k */
82 f43e3525 David Gibson
            /* (masks depend on page size) */
83 f43e3525 David Gibson
            rb |= 0x1000;                /* page encoding in LP field */
84 f43e3525 David Gibson
            rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
85 f43e3525 David Gibson
            rb |= (va_low & 0xfe);       /* AVAL field */
86 f43e3525 David Gibson
        }
87 f43e3525 David Gibson
#endif
88 f43e3525 David Gibson
    } else {
89 f43e3525 David Gibson
        /* 4kB page */
90 f43e3525 David Gibson
        rb |= (va_low & 0x7ff) << 12;   /* remaining 11b of AVA */
91 f43e3525 David Gibson
    }
92 f43e3525 David Gibson
    rb |= (v >> 54) & 0x300;            /* B field */
93 f43e3525 David Gibson
    return rb;
94 f43e3525 David Gibson
}
95 f43e3525 David Gibson
96 f43e3525 David Gibson
static target_ulong h_enter(CPUState *env, sPAPREnvironment *spapr,
97 f43e3525 David Gibson
                            target_ulong opcode, target_ulong *args)
98 f43e3525 David Gibson
{
99 f43e3525 David Gibson
    target_ulong flags = args[0];
100 f43e3525 David Gibson
    target_ulong pte_index = args[1];
101 f43e3525 David Gibson
    target_ulong pteh = args[2];
102 f43e3525 David Gibson
    target_ulong ptel = args[3];
103 1235a9cf David Gibson
    target_ulong i;
104 f43e3525 David Gibson
    uint8_t *hpte;
105 f43e3525 David Gibson
106 f43e3525 David Gibson
    /* only handle 4k and 16M pages for now */
107 f43e3525 David Gibson
    if (pteh & HPTE_V_LARGE) {
108 f43e3525 David Gibson
#if 0 /* We don't support 64k pages yet */
109 f43e3525 David Gibson
        if ((ptel & 0xf000) == 0x1000) {
110 f43e3525 David Gibson
            /* 64k page */
111 f43e3525 David Gibson
        } else
112 f43e3525 David Gibson
#endif
113 f43e3525 David Gibson
        if ((ptel & 0xff000) == 0) {
114 f43e3525 David Gibson
            /* 16M page */
115 f43e3525 David Gibson
            /* lowest AVA bit must be 0 for 16M pages */
116 f43e3525 David Gibson
            if (pteh & 0x80) {
117 f43e3525 David Gibson
                return H_PARAMETER;
118 f43e3525 David Gibson
            }
119 f43e3525 David Gibson
        } else {
120 f43e3525 David Gibson
            return H_PARAMETER;
121 f43e3525 David Gibson
        }
122 f43e3525 David Gibson
    }
123 f43e3525 David Gibson
124 f43e3525 David Gibson
    /* FIXME: bounds check the pa? */
125 f43e3525 David Gibson
126 f43e3525 David Gibson
    /* Check WIMG */
127 f43e3525 David Gibson
    if ((ptel & HPTE_R_WIMG) != HPTE_R_M) {
128 f43e3525 David Gibson
        return H_PARAMETER;
129 f43e3525 David Gibson
    }
130 f43e3525 David Gibson
    pteh &= ~0x60ULL;
131 f43e3525 David Gibson
132 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
133 f43e3525 David Gibson
        return H_PARAMETER;
134 f43e3525 David Gibson
    }
135 f43e3525 David Gibson
    if (likely((flags & H_EXACT) == 0)) {
136 f43e3525 David Gibson
        pte_index &= ~7ULL;
137 f43e3525 David Gibson
        hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
138 f43e3525 David Gibson
        for (i = 0; ; ++i) {
139 f43e3525 David Gibson
            if (i == 8) {
140 f43e3525 David Gibson
                return H_PTEG_FULL;
141 f43e3525 David Gibson
            }
142 f43e3525 David Gibson
            if (((ldq_p(hpte) & HPTE_V_VALID) == 0) &&
143 f43e3525 David Gibson
                lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) {
144 f43e3525 David Gibson
                break;
145 f43e3525 David Gibson
            }
146 f43e3525 David Gibson
            hpte += HASH_PTE_SIZE_64;
147 f43e3525 David Gibson
        }
148 f43e3525 David Gibson
    } else {
149 f43e3525 David Gibson
        i = 0;
150 f43e3525 David Gibson
        hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
151 f43e3525 David Gibson
        if (!lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID)) {
152 f43e3525 David Gibson
            return H_PTEG_FULL;
153 f43e3525 David Gibson
        }
154 f43e3525 David Gibson
    }
155 f43e3525 David Gibson
    stq_p(hpte + (HASH_PTE_SIZE_64/2), ptel);
156 f43e3525 David Gibson
    /* eieio();  FIXME: need some sort of barrier for smp? */
157 f43e3525 David Gibson
    stq_p(hpte, pteh);
158 f43e3525 David Gibson
159 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
160 f43e3525 David Gibson
    args[0] = pte_index + i;
161 f43e3525 David Gibson
    return H_SUCCESS;
162 f43e3525 David Gibson
}
163 f43e3525 David Gibson
164 f43e3525 David Gibson
static target_ulong h_remove(CPUState *env, sPAPREnvironment *spapr,
165 f43e3525 David Gibson
                             target_ulong opcode, target_ulong *args)
166 f43e3525 David Gibson
{
167 f43e3525 David Gibson
    target_ulong flags = args[0];
168 f43e3525 David Gibson
    target_ulong pte_index = args[1];
169 f43e3525 David Gibson
    target_ulong avpn = args[2];
170 f43e3525 David Gibson
    uint8_t *hpte;
171 f43e3525 David Gibson
    target_ulong v, r, rb;
172 f43e3525 David Gibson
173 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
174 f43e3525 David Gibson
        return H_PARAMETER;
175 f43e3525 David Gibson
    }
176 f43e3525 David Gibson
177 f43e3525 David Gibson
    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
178 f43e3525 David Gibson
    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {
179 f43e3525 David Gibson
        /* We have no real concurrency in qemu soft-emulation, so we
180 f43e3525 David Gibson
         * will never actually have a contested lock */
181 f43e3525 David Gibson
        assert(0);
182 f43e3525 David Gibson
    }
183 f43e3525 David Gibson
184 f43e3525 David Gibson
    v = ldq_p(hpte);
185 f43e3525 David Gibson
    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
186 f43e3525 David Gibson
187 f43e3525 David Gibson
    if ((v & HPTE_V_VALID) == 0 ||
188 f43e3525 David Gibson
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn) ||
189 f43e3525 David Gibson
        ((flags & H_ANDCOND) && (v & avpn) != 0)) {
190 f43e3525 David Gibson
        stq_p(hpte, v & ~HPTE_V_HVLOCK);
191 f43e3525 David Gibson
        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
192 f43e3525 David Gibson
        return H_NOT_FOUND;
193 f43e3525 David Gibson
    }
194 f43e3525 David Gibson
    args[0] = v & ~HPTE_V_HVLOCK;
195 f43e3525 David Gibson
    args[1] = r;
196 f43e3525 David Gibson
    stq_p(hpte, 0);
197 f43e3525 David Gibson
    rb = compute_tlbie_rb(v, r, pte_index);
198 f43e3525 David Gibson
    ppc_tlb_invalidate_one(env, rb);
199 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
200 f43e3525 David Gibson
    return H_SUCCESS;
201 f43e3525 David Gibson
}
202 f43e3525 David Gibson
203 f43e3525 David Gibson
static target_ulong h_protect(CPUState *env, sPAPREnvironment *spapr,
204 f43e3525 David Gibson
                              target_ulong opcode, target_ulong *args)
205 f43e3525 David Gibson
{
206 f43e3525 David Gibson
    target_ulong flags = args[0];
207 f43e3525 David Gibson
    target_ulong pte_index = args[1];
208 f43e3525 David Gibson
    target_ulong avpn = args[2];
209 f43e3525 David Gibson
    uint8_t *hpte;
210 f43e3525 David Gibson
    target_ulong v, r, rb;
211 f43e3525 David Gibson
212 f43e3525 David Gibson
    if ((pte_index * HASH_PTE_SIZE_64) & ~env->htab_mask) {
213 f43e3525 David Gibson
        return H_PARAMETER;
214 f43e3525 David Gibson
    }
215 f43e3525 David Gibson
216 f43e3525 David Gibson
    hpte = env->external_htab + (pte_index * HASH_PTE_SIZE_64);
217 f43e3525 David Gibson
    while (!lock_hpte(hpte, HPTE_V_HVLOCK)) {
218 f43e3525 David Gibson
        /* We have no real concurrency in qemu soft-emulation, so we
219 f43e3525 David Gibson
         * will never actually have a contested lock */
220 f43e3525 David Gibson
        assert(0);
221 f43e3525 David Gibson
    }
222 f43e3525 David Gibson
223 f43e3525 David Gibson
    v = ldq_p(hpte);
224 f43e3525 David Gibson
    r = ldq_p(hpte + (HASH_PTE_SIZE_64/2));
225 f43e3525 David Gibson
226 f43e3525 David Gibson
    if ((v & HPTE_V_VALID) == 0 ||
227 f43e3525 David Gibson
        ((flags & H_AVPN) && (v & ~0x7fULL) != avpn)) {
228 f43e3525 David Gibson
        stq_p(hpte, v & ~HPTE_V_HVLOCK);
229 f43e3525 David Gibson
        assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
230 f43e3525 David Gibson
        return H_NOT_FOUND;
231 f43e3525 David Gibson
    }
232 f43e3525 David Gibson
233 f43e3525 David Gibson
    r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
234 f43e3525 David Gibson
           HPTE_R_KEY_HI | HPTE_R_KEY_LO);
235 f43e3525 David Gibson
    r |= (flags << 55) & HPTE_R_PP0;
236 f43e3525 David Gibson
    r |= (flags << 48) & HPTE_R_KEY_HI;
237 f43e3525 David Gibson
    r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
238 f43e3525 David Gibson
    rb = compute_tlbie_rb(v, r, pte_index);
239 f43e3525 David Gibson
    stq_p(hpte, v & ~HPTE_V_VALID);
240 f43e3525 David Gibson
    ppc_tlb_invalidate_one(env, rb);
241 f43e3525 David Gibson
    stq_p(hpte + (HASH_PTE_SIZE_64/2), r);
242 f43e3525 David Gibson
    /* Don't need a memory barrier, due to qemu's global lock */
243 f43e3525 David Gibson
    stq_p(hpte, v & ~HPTE_V_HVLOCK);
244 f43e3525 David Gibson
    assert(!(ldq_p(hpte) & HPTE_V_HVLOCK));
245 f43e3525 David Gibson
    return H_SUCCESS;
246 f43e3525 David Gibson
}
247 f43e3525 David Gibson
248 821303f5 David Gibson
static target_ulong h_set_dabr(CPUState *env, sPAPREnvironment *spapr,
249 821303f5 David Gibson
                               target_ulong opcode, target_ulong *args)
250 821303f5 David Gibson
{
251 821303f5 David Gibson
    /* FIXME: actually implement this */
252 821303f5 David Gibson
    return H_HARDWARE;
253 821303f5 David Gibson
}
254 821303f5 David Gibson
255 ed120055 David Gibson
#define FLAGS_REGISTER_VPA         0x0000200000000000ULL
256 ed120055 David Gibson
#define FLAGS_REGISTER_DTL         0x0000400000000000ULL
257 ed120055 David Gibson
#define FLAGS_REGISTER_SLBSHADOW   0x0000600000000000ULL
258 ed120055 David Gibson
#define FLAGS_DEREGISTER_VPA       0x0000a00000000000ULL
259 ed120055 David Gibson
#define FLAGS_DEREGISTER_DTL       0x0000c00000000000ULL
260 ed120055 David Gibson
#define FLAGS_DEREGISTER_SLBSHADOW 0x0000e00000000000ULL
261 ed120055 David Gibson
262 ed120055 David Gibson
#define VPA_MIN_SIZE           640
263 ed120055 David Gibson
#define VPA_SIZE_OFFSET        0x4
264 ed120055 David Gibson
#define VPA_SHARED_PROC_OFFSET 0x9
265 ed120055 David Gibson
#define VPA_SHARED_PROC_VAL    0x2
266 ed120055 David Gibson
267 ed120055 David Gibson
static target_ulong register_vpa(CPUState *env, target_ulong vpa)
268 ed120055 David Gibson
{
269 ed120055 David Gibson
    uint16_t size;
270 ed120055 David Gibson
    uint8_t tmp;
271 ed120055 David Gibson
272 ed120055 David Gibson
    if (vpa == 0) {
273 ed120055 David Gibson
        hcall_dprintf("Can't cope with registering a VPA at logical 0\n");
274 ed120055 David Gibson
        return H_HARDWARE;
275 ed120055 David Gibson
    }
276 ed120055 David Gibson
277 ed120055 David Gibson
    if (vpa % env->dcache_line_size) {
278 ed120055 David Gibson
        return H_PARAMETER;
279 ed120055 David Gibson
    }
280 ed120055 David Gibson
    /* FIXME: bounds check the address */
281 ed120055 David Gibson
282 ed120055 David Gibson
    size = lduw_phys(vpa + 0x4);
283 ed120055 David Gibson
284 ed120055 David Gibson
    if (size < VPA_MIN_SIZE) {
285 ed120055 David Gibson
        return H_PARAMETER;
286 ed120055 David Gibson
    }
287 ed120055 David Gibson
288 ed120055 David Gibson
    /* VPA is not allowed to cross a page boundary */
289 ed120055 David Gibson
    if ((vpa / 4096) != ((vpa + size - 1) / 4096)) {
290 ed120055 David Gibson
        return H_PARAMETER;
291 ed120055 David Gibson
    }
292 ed120055 David Gibson
293 ed120055 David Gibson
    env->vpa = vpa;
294 ed120055 David Gibson
295 ed120055 David Gibson
    tmp = ldub_phys(env->vpa + VPA_SHARED_PROC_OFFSET);
296 ed120055 David Gibson
    tmp |= VPA_SHARED_PROC_VAL;
297 ed120055 David Gibson
    stb_phys(env->vpa + VPA_SHARED_PROC_OFFSET, tmp);
298 ed120055 David Gibson
299 ed120055 David Gibson
    return H_SUCCESS;
300 ed120055 David Gibson
}
301 ed120055 David Gibson
302 ed120055 David Gibson
static target_ulong deregister_vpa(CPUState *env, target_ulong vpa)
303 ed120055 David Gibson
{
304 ed120055 David Gibson
    if (env->slb_shadow) {
305 ed120055 David Gibson
        return H_RESOURCE;
306 ed120055 David Gibson
    }
307 ed120055 David Gibson
308 ed120055 David Gibson
    if (env->dispatch_trace_log) {
309 ed120055 David Gibson
        return H_RESOURCE;
310 ed120055 David Gibson
    }
311 ed120055 David Gibson
312 ed120055 David Gibson
    env->vpa = 0;
313 ed120055 David Gibson
    return H_SUCCESS;
314 ed120055 David Gibson
}
315 ed120055 David Gibson
316 ed120055 David Gibson
static target_ulong register_slb_shadow(CPUState *env, target_ulong addr)
317 ed120055 David Gibson
{
318 ed120055 David Gibson
    uint32_t size;
319 ed120055 David Gibson
320 ed120055 David Gibson
    if (addr == 0) {
321 ed120055 David Gibson
        hcall_dprintf("Can't cope with SLB shadow at logical 0\n");
322 ed120055 David Gibson
        return H_HARDWARE;
323 ed120055 David Gibson
    }
324 ed120055 David Gibson
325 ed120055 David Gibson
    size = ldl_phys(addr + 0x4);
326 ed120055 David Gibson
    if (size < 0x8) {
327 ed120055 David Gibson
        return H_PARAMETER;
328 ed120055 David Gibson
    }
329 ed120055 David Gibson
330 ed120055 David Gibson
    if ((addr / 4096) != ((addr + size - 1) / 4096)) {
331 ed120055 David Gibson
        return H_PARAMETER;
332 ed120055 David Gibson
    }
333 ed120055 David Gibson
334 ed120055 David Gibson
    if (!env->vpa) {
335 ed120055 David Gibson
        return H_RESOURCE;
336 ed120055 David Gibson
    }
337 ed120055 David Gibson
338 ed120055 David Gibson
    env->slb_shadow = addr;
339 ed120055 David Gibson
340 ed120055 David Gibson
    return H_SUCCESS;
341 ed120055 David Gibson
}
342 ed120055 David Gibson
343 ed120055 David Gibson
static target_ulong deregister_slb_shadow(CPUState *env, target_ulong addr)
344 ed120055 David Gibson
{
345 ed120055 David Gibson
    env->slb_shadow = 0;
346 ed120055 David Gibson
    return H_SUCCESS;
347 ed120055 David Gibson
}
348 ed120055 David Gibson
349 ed120055 David Gibson
static target_ulong register_dtl(CPUState *env, target_ulong addr)
350 ed120055 David Gibson
{
351 ed120055 David Gibson
    uint32_t size;
352 ed120055 David Gibson
353 ed120055 David Gibson
    if (addr == 0) {
354 ed120055 David Gibson
        hcall_dprintf("Can't cope with DTL at logical 0\n");
355 ed120055 David Gibson
        return H_HARDWARE;
356 ed120055 David Gibson
    }
357 ed120055 David Gibson
358 ed120055 David Gibson
    size = ldl_phys(addr + 0x4);
359 ed120055 David Gibson
360 ed120055 David Gibson
    if (size < 48) {
361 ed120055 David Gibson
        return H_PARAMETER;
362 ed120055 David Gibson
    }
363 ed120055 David Gibson
364 ed120055 David Gibson
    if (!env->vpa) {
365 ed120055 David Gibson
        return H_RESOURCE;
366 ed120055 David Gibson
    }
367 ed120055 David Gibson
368 ed120055 David Gibson
    env->dispatch_trace_log = addr;
369 ed120055 David Gibson
    env->dtl_size = size;
370 ed120055 David Gibson
371 ed120055 David Gibson
    return H_SUCCESS;
372 ed120055 David Gibson
}
373 ed120055 David Gibson
374 ed120055 David Gibson
static target_ulong deregister_dtl(CPUState *emv, target_ulong addr)
375 ed120055 David Gibson
{
376 ed120055 David Gibson
    env->dispatch_trace_log = 0;
377 ed120055 David Gibson
    env->dtl_size = 0;
378 ed120055 David Gibson
379 ed120055 David Gibson
    return H_SUCCESS;
380 ed120055 David Gibson
}
381 ed120055 David Gibson
382 ed120055 David Gibson
static target_ulong h_register_vpa(CPUState *env, sPAPREnvironment *spapr,
383 ed120055 David Gibson
                                   target_ulong opcode, target_ulong *args)
384 ed120055 David Gibson
{
385 ed120055 David Gibson
    target_ulong flags = args[0];
386 ed120055 David Gibson
    target_ulong procno = args[1];
387 ed120055 David Gibson
    target_ulong vpa = args[2];
388 ed120055 David Gibson
    target_ulong ret = H_PARAMETER;
389 ed120055 David Gibson
    CPUState *tenv;
390 ed120055 David Gibson
391 ed120055 David Gibson
    for (tenv = first_cpu; tenv; tenv = tenv->next_cpu) {
392 ed120055 David Gibson
        if (tenv->cpu_index == procno) {
393 ed120055 David Gibson
            break;
394 ed120055 David Gibson
        }
395 ed120055 David Gibson
    }
396 ed120055 David Gibson
397 ed120055 David Gibson
    if (!tenv) {
398 ed120055 David Gibson
        return H_PARAMETER;
399 ed120055 David Gibson
    }
400 ed120055 David Gibson
401 ed120055 David Gibson
    switch (flags) {
402 ed120055 David Gibson
    case FLAGS_REGISTER_VPA:
403 ed120055 David Gibson
        ret = register_vpa(tenv, vpa);
404 ed120055 David Gibson
        break;
405 ed120055 David Gibson
406 ed120055 David Gibson
    case FLAGS_DEREGISTER_VPA:
407 ed120055 David Gibson
        ret = deregister_vpa(tenv, vpa);
408 ed120055 David Gibson
        break;
409 ed120055 David Gibson
410 ed120055 David Gibson
    case FLAGS_REGISTER_SLBSHADOW:
411 ed120055 David Gibson
        ret = register_slb_shadow(tenv, vpa);
412 ed120055 David Gibson
        break;
413 ed120055 David Gibson
414 ed120055 David Gibson
    case FLAGS_DEREGISTER_SLBSHADOW:
415 ed120055 David Gibson
        ret = deregister_slb_shadow(tenv, vpa);
416 ed120055 David Gibson
        break;
417 ed120055 David Gibson
418 ed120055 David Gibson
    case FLAGS_REGISTER_DTL:
419 ed120055 David Gibson
        ret = register_dtl(tenv, vpa);
420 ed120055 David Gibson
        break;
421 ed120055 David Gibson
422 ed120055 David Gibson
    case FLAGS_DEREGISTER_DTL:
423 ed120055 David Gibson
        ret = deregister_dtl(tenv, vpa);
424 ed120055 David Gibson
        break;
425 ed120055 David Gibson
    }
426 ed120055 David Gibson
427 ed120055 David Gibson
    return ret;
428 ed120055 David Gibson
}
429 ed120055 David Gibson
430 ed120055 David Gibson
static target_ulong h_cede(CPUState *env, sPAPREnvironment *spapr,
431 ed120055 David Gibson
                           target_ulong opcode, target_ulong *args)
432 ed120055 David Gibson
{
433 ed120055 David Gibson
    env->msr |= (1ULL << MSR_EE);
434 ed120055 David Gibson
    hreg_compute_hflags(env);
435 ed120055 David Gibson
    if (!cpu_has_work(env)) {
436 ed120055 David Gibson
        env->halted = 1;
437 ed120055 David Gibson
    }
438 ed120055 David Gibson
    return H_SUCCESS;
439 ed120055 David Gibson
}
440 ed120055 David Gibson
441 39ac8455 David Gibson
static target_ulong h_rtas(CPUState *env, sPAPREnvironment *spapr,
442 39ac8455 David Gibson
                           target_ulong opcode, target_ulong *args)
443 39ac8455 David Gibson
{
444 39ac8455 David Gibson
    target_ulong rtas_r3 = args[0];
445 39ac8455 David Gibson
    uint32_t token = ldl_phys(rtas_r3);
446 39ac8455 David Gibson
    uint32_t nargs = ldl_phys(rtas_r3 + 4);
447 39ac8455 David Gibson
    uint32_t nret = ldl_phys(rtas_r3 + 8);
448 39ac8455 David Gibson
449 39ac8455 David Gibson
    return spapr_rtas_call(spapr, token, nargs, rtas_r3 + 12,
450 39ac8455 David Gibson
                           nret, rtas_r3 + 12 + 4*nargs);
451 39ac8455 David Gibson
}
452 39ac8455 David Gibson
453 7d7ba3fe David Gibson
static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
454 7d7ba3fe David Gibson
static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX - KVMPPC_HCALL_BASE + 1];
455 9fdf0c29 David Gibson
456 9fdf0c29 David Gibson
void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
457 9fdf0c29 David Gibson
{
458 39ac8455 David Gibson
    spapr_hcall_fn *slot;
459 39ac8455 David Gibson
460 39ac8455 David Gibson
    if (opcode <= MAX_HCALL_OPCODE) {
461 39ac8455 David Gibson
        assert((opcode & 0x3) == 0);
462 9fdf0c29 David Gibson
463 39ac8455 David Gibson
        slot = &papr_hypercall_table[opcode / 4];
464 39ac8455 David Gibson
    } else {
465 39ac8455 David Gibson
        assert((opcode >= KVMPPC_HCALL_BASE) && (opcode <= KVMPPC_HCALL_MAX));
466 9fdf0c29 David Gibson
467 9fdf0c29 David Gibson
468 39ac8455 David Gibson
        slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
469 39ac8455 David Gibson
    }
470 9fdf0c29 David Gibson
471 39ac8455 David Gibson
    assert(!(*slot) || (fn == *slot));
472 39ac8455 David Gibson
    *slot = fn;
473 9fdf0c29 David Gibson
}
474 9fdf0c29 David Gibson
475 9fdf0c29 David Gibson
target_ulong spapr_hypercall(CPUState *env, target_ulong opcode,
476 9fdf0c29 David Gibson
                             target_ulong *args)
477 9fdf0c29 David Gibson
{
478 9fdf0c29 David Gibson
    if (msr_pr) {
479 9fdf0c29 David Gibson
        hcall_dprintf("Hypercall made with MSR[PR]=1\n");
480 9fdf0c29 David Gibson
        return H_PRIVILEGE;
481 9fdf0c29 David Gibson
    }
482 9fdf0c29 David Gibson
483 9fdf0c29 David Gibson
    if ((opcode <= MAX_HCALL_OPCODE)
484 9fdf0c29 David Gibson
        && ((opcode & 0x3) == 0)) {
485 39ac8455 David Gibson
        spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
486 39ac8455 David Gibson
487 39ac8455 David Gibson
        if (fn) {
488 39ac8455 David Gibson
            return fn(env, spapr, opcode, args);
489 39ac8455 David Gibson
        }
490 39ac8455 David Gibson
    } else if ((opcode >= KVMPPC_HCALL_BASE) &&
491 39ac8455 David Gibson
               (opcode <= KVMPPC_HCALL_MAX)) {
492 39ac8455 David Gibson
        spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
493 9fdf0c29 David Gibson
494 9fdf0c29 David Gibson
        if (fn) {
495 9fdf0c29 David Gibson
            return fn(env, spapr, opcode, args);
496 9fdf0c29 David Gibson
        }
497 9fdf0c29 David Gibson
    }
498 9fdf0c29 David Gibson
499 9fdf0c29 David Gibson
    hcall_dprintf("Unimplemented hcall 0x" TARGET_FMT_lx "\n", opcode);
500 9fdf0c29 David Gibson
    return H_FUNCTION;
501 9fdf0c29 David Gibson
}
502 f43e3525 David Gibson
503 f43e3525 David Gibson
static void hypercall_init(void)
504 f43e3525 David Gibson
{
505 f43e3525 David Gibson
    /* hcall-pft */
506 f43e3525 David Gibson
    spapr_register_hypercall(H_ENTER, h_enter);
507 f43e3525 David Gibson
    spapr_register_hypercall(H_REMOVE, h_remove);
508 f43e3525 David Gibson
    spapr_register_hypercall(H_PROTECT, h_protect);
509 39ac8455 David Gibson
510 821303f5 David Gibson
    /* hcall-dabr */
511 821303f5 David Gibson
    spapr_register_hypercall(H_SET_DABR, h_set_dabr);
512 821303f5 David Gibson
513 ed120055 David Gibson
    /* hcall-splpar */
514 ed120055 David Gibson
    spapr_register_hypercall(H_REGISTER_VPA, h_register_vpa);
515 ed120055 David Gibson
    spapr_register_hypercall(H_CEDE, h_cede);
516 ed120055 David Gibson
517 39ac8455 David Gibson
    /* qemu/KVM-PPC specific hcalls */
518 39ac8455 David Gibson
    spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
519 f43e3525 David Gibson
}
520 f43e3525 David Gibson
device_init(hypercall_init);