cpu.c 6.4 KB
Newer Older
A
Andreas Färber 已提交
1 2 3
/*
 * QEMU MicroBlaze CPU
 *
4 5
 * Copyright (c) 2009 Edgar E. Iglesias
 * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
A
Andreas Färber 已提交
6
 * Copyright (c) 2012 SUSE LINUX Products GmbH
7
 * Copyright (c) 2009 Edgar E. Iglesias, Axis Communications AB.
A
Andreas Färber 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>
 */

#include "cpu.h"
#include "qemu-common.h"
26
#include "hw/qdev-properties.h"
27
#include "migration/vmstate.h"
A
Andreas Färber 已提交
28 29


30 31 32 33 34 35 36
static void mb_cpu_set_pc(CPUState *cs, vaddr value)
{
    MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);

    cpu->env.sregs[SR_PC] = value;
}

37 38 39 40 41
static bool mb_cpu_has_work(CPUState *cs)
{
    return cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI);
}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#ifndef CONFIG_USER_ONLY
static void microblaze_cpu_set_irq(void *opaque, int irq, int level)
{
    MicroBlazeCPU *cpu = opaque;
    CPUState *cs = CPU(cpu);
    int type = irq ? CPU_INTERRUPT_NMI : CPU_INTERRUPT_HARD;

    if (level) {
        cpu_interrupt(cs, type);
    } else {
        cpu_reset_interrupt(cs, type);
    }
}
#endif

A
Andreas Färber 已提交
57 58 59 60 61 62 63 64 65
/* CPUClass::reset() */
static void mb_cpu_reset(CPUState *s)
{
    MicroBlazeCPU *cpu = MICROBLAZE_CPU(s);
    MicroBlazeCPUClass *mcc = MICROBLAZE_CPU_GET_CLASS(cpu);
    CPUMBState *env = &cpu->env;

    mcc->parent_reset(s);

66
    memset(env, 0, offsetof(CPUMBState, pvr));
67
    env->res_addr = RES_ADDR_NONE;
68
    tlb_flush(s, 1);
69 70 71 72

    /* Disable stack protector.  */
    env->shr = ~0;

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#if defined(CONFIG_USER_ONLY)
    /* start in user mode with interrupts enabled.  */
    env->sregs[SR_MSR] = MSR_EE | MSR_IE | MSR_VM | MSR_UM;
#else
    env->sregs[SR_MSR] = 0;
    mmu_init(&env->mmu);
    env->mmu.c_mmu = 3;
    env->mmu.c_mmu_tlb_access = 3;
    env->mmu.c_mmu_zones = 16;
#endif
}

static void mb_cpu_realizefn(DeviceState *dev, Error **errp)
{
    CPUState *cs = CPU(dev);
    MicroBlazeCPUClass *mcc = MICROBLAZE_CPU_GET_CLASS(dev);
    MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
    CPUMBState *env = &cpu->env;

    qemu_init_vcpu(cs);

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    env->pvr.regs[0] = PVR0_PVR_FULL_MASK \
                       | PVR0_USE_BARREL_MASK \
                       | PVR0_USE_DIV_MASK \
                       | PVR0_USE_HW_MUL_MASK \
                       | PVR0_USE_EXC_MASK \
                       | PVR0_USE_ICACHE_MASK \
                       | PVR0_USE_DCACHE_MASK \
                       | PVR0_USE_MMU \
                       | (0xb << 8);
    env->pvr.regs[2] = PVR2_D_OPB_MASK \
                        | PVR2_D_LMB_MASK \
                        | PVR2_I_OPB_MASK \
                        | PVR2_I_LMB_MASK \
                        | PVR2_USE_MSR_INSTR \
                        | PVR2_USE_PCMP_INSTR \
                        | PVR2_USE_BARREL_MASK \
                        | PVR2_USE_DIV_MASK \
                        | PVR2_USE_HW_MUL_MASK \
                        | PVR2_USE_MUL64_MASK \
                        | PVR2_FPU_EXC_MASK \
                        | 0;
115

116
    env->pvr.regs[0] |= (cpu->cfg.stackprot ? PVR0_SPROT_MASK : 0) |
117
                        (cpu->cfg.use_fpu ? PVR0_USE_FPU_MASK : 0);
118

119 120
    env->pvr.regs[2] |= (cpu->cfg.use_fpu ? PVR2_USE_FPU_MASK : 0) |
                        (cpu->cfg.use_fpu > 1 ? PVR2_USE_FPU2_MASK : 0);
121

122 123 124
    env->pvr.regs[10] = 0x0c000000; /* Default to spartan 3a dsp family.  */
    env->pvr.regs[11] = PVR11_USE_MMU | (16 << 17);

125
    env->sregs[SR_PC] = cpu->cfg.base_vectors;
126

127 128 129
#if defined(CONFIG_USER_ONLY)
    env->pvr.regs[10] = 0x0c000000; /* Spartan 3a dsp.  */
#endif
130 131 132 133

    mcc->parent_realize(dev, errp);
}

134 135
static void mb_cpu_initfn(Object *obj)
{
136
    CPUState *cs = CPU(obj);
137 138
    MicroBlazeCPU *cpu = MICROBLAZE_CPU(obj);
    CPUMBState *env = &cpu->env;
139
    static bool tcg_initialized;
140

141
    cs->env_ptr = env;
142 143 144
    cpu_exec_init(env);

    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
145

146 147 148 149 150
#ifndef CONFIG_USER_ONLY
    /* Inbound IRQ and FIR lines */
    qdev_init_gpio_in(DEVICE(cpu), microblaze_cpu_set_irq, 2);
#endif

151 152 153 154
    if (tcg_enabled() && !tcg_initialized) {
        tcg_initialized = true;
        mb_tcg_init();
    }
155 156
}

157 158 159 160 161
static const VMStateDescription vmstate_mb_cpu = {
    .name = "cpu",
    .unmigratable = 1,
};

162
static Property mb_properties[] = {
163
    DEFINE_PROP_UINT32("base-vectors", MicroBlazeCPU, cfg.base_vectors, 0),
164
    DEFINE_PROP_BOOL("use-stack-protection", MicroBlazeCPU, cfg.stackprot,
165
                     false),
166 167 168 169
    /* If use-fpu > 0 - FPU is enabled
     * If use-fpu = 2 - Floating point conversion and square root instructions
     *                  are enabled
     */
170
    DEFINE_PROP_UINT8("use-fpu", MicroBlazeCPU, cfg.use_fpu, 2),
171 172 173
    DEFINE_PROP_END_OF_LIST(),
};

A
Andreas Färber 已提交
174 175
static void mb_cpu_class_init(ObjectClass *oc, void *data)
{
176
    DeviceClass *dc = DEVICE_CLASS(oc);
A
Andreas Färber 已提交
177 178 179
    CPUClass *cc = CPU_CLASS(oc);
    MicroBlazeCPUClass *mcc = MICROBLAZE_CPU_CLASS(oc);

180 181 182
    mcc->parent_realize = dc->realize;
    dc->realize = mb_cpu_realizefn;

A
Andreas Färber 已提交
183 184
    mcc->parent_reset = cc->reset;
    cc->reset = mb_cpu_reset;
185

186
    cc->has_work = mb_cpu_has_work;
187
    cc->do_interrupt = mb_cpu_do_interrupt;
188
    cc->cpu_exec_interrupt = mb_cpu_exec_interrupt;
189
    cc->dump_state = mb_cpu_dump_state;
190
    cc->set_pc = mb_cpu_set_pc;
191 192
    cc->gdb_read_register = mb_cpu_gdb_read_register;
    cc->gdb_write_register = mb_cpu_gdb_write_register;
193 194 195
#ifdef CONFIG_USER_ONLY
    cc->handle_mmu_fault = mb_cpu_handle_mmu_fault;
#else
196 197 198
    cc->do_unassigned_access = mb_cpu_unassigned_access;
    cc->get_phys_page_debug = mb_cpu_get_phys_page_debug;
#endif
199
    dc->vmsd = &vmstate_mb_cpu;
200
    dc->props = mb_properties;
201
    cc->gdb_num_core_regs = 32 + 5;
A
Andreas Färber 已提交
202 203 204 205 206 207
}

static const TypeInfo mb_cpu_type_info = {
    .name = TYPE_MICROBLAZE_CPU,
    .parent = TYPE_CPU,
    .instance_size = sizeof(MicroBlazeCPU),
208
    .instance_init = mb_cpu_initfn,
A
Andreas Färber 已提交
209 210 211 212 213 214 215 216 217 218
    .class_size = sizeof(MicroBlazeCPUClass),
    .class_init = mb_cpu_class_init,
};

static void mb_cpu_register_types(void)
{
    type_register_static(&mb_cpu_type_info);
}

type_init(mb_cpu_register_types)