2 * QEMU PowerPC e500v2 ePAPR spinning code
4 * Copyright (C) 2011 Freescale Semiconductor, Inc. All rights reserved.
6 * Author: Alexander Graf, <agraf@suse.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * This code is not really a device, but models an interface that usually
22 * firmware takes care of. It's used when QEMU plays the role of firmware.
26 * https://www.power.org/resources/downloads/Power_ePAPR_APPROVED_v1.1.pdf
31 #include "sysemu/sysemu.h"
32 #include "hw/sysbus.h"
33 #include "sysemu/kvm.h"
37 typedef struct spin_info
{
43 } QEMU_PACKED SpinInfo
;
45 #define TYPE_E500_SPIN "e500-spin"
46 #define E500_SPIN(obj) OBJECT_CHECK(SpinState, (obj), TYPE_E500_SPIN)
48 typedef struct SpinState
{
49 SysBusDevice parent_obj
;
52 SpinInfo spin
[MAX_CPUS
];
55 typedef struct spin_kick
{
60 static void spin_reset(void *opaque
)
62 SpinState
*s
= opaque
;
65 for (i
= 0; i
< MAX_CPUS
; i
++) {
66 SpinInfo
*info
= &s
->spin
[i
];
70 stq_p(&info
->addr
, 1);
74 /* Create -kernel TLB entries for BookE, linearly spanning 256MB. */
75 static inline hwaddr
booke206_page_size_to_tlb(uint64_t size
)
77 return ctz32(size
>> 10) >> 1;
80 static void mmubooke_create_initial_mapping(CPUPPCState
*env
,
85 ppcmas_tlb_t
*tlb
= booke206_get_tlbm(env
, 1, 0, 1);
88 size
= (booke206_page_size_to_tlb(len
) << MAS1_TSIZE_SHIFT
);
89 tlb
->mas1
= MAS1_VALID
| size
;
90 tlb
->mas2
= (va
& TARGET_PAGE_MASK
) | MAS2_M
;
91 tlb
->mas7_3
= pa
& TARGET_PAGE_MASK
;
92 tlb
->mas7_3
|= MAS3_UR
| MAS3_UW
| MAS3_UX
| MAS3_SR
| MAS3_SW
| MAS3_SX
;
93 env
->tlb_dirty
= true;
96 static void spin_kick(void *data
)
98 SpinKick
*kick
= data
;
99 CPUState
*cpu
= CPU(kick
->cpu
);
100 CPUPPCState
*env
= &kick
->cpu
->env
;
101 SpinInfo
*curspin
= kick
->spin
;
102 hwaddr map_size
= 64 * 1024 * 1024;
105 cpu_synchronize_state(cpu
);
106 stl_p(&curspin
->pir
, env
->spr
[SPR_PIR
]);
107 env
->nip
= ldq_p(&curspin
->addr
) & (map_size
- 1);
108 env
->gpr
[3] = ldq_p(&curspin
->r3
);
112 env
->gpr
[7] = map_size
;
116 map_start
= ldq_p(&curspin
->addr
) & ~(map_size
- 1);
117 mmubooke_create_initial_mapping(env
, 0, map_start
, map_size
);
120 cpu
->exception_index
= -1;
121 cpu
->stopped
= false;
125 static void spin_write(void *opaque
, hwaddr addr
, uint64_t value
,
128 SpinState
*s
= opaque
;
129 int env_idx
= addr
/ sizeof(SpinInfo
);
131 SpinInfo
*curspin
= &s
->spin
[env_idx
];
132 uint8_t *curspin_p
= (uint8_t*)curspin
;
134 cpu
= qemu_get_cpu(env_idx
);
140 if (cpu
->cpu_index
== 0) {
141 /* primary CPU doesn't spin */
145 curspin_p
= &curspin_p
[addr
% sizeof(SpinInfo
)];
148 stb_p(curspin_p
, value
);
151 stw_p(curspin_p
, value
);
154 stl_p(curspin_p
, value
);
158 if (!(ldq_p(&curspin
->addr
) & 1)) {
161 .cpu
= POWERPC_CPU(cpu
),
165 run_on_cpu(cpu
, spin_kick
, &kick
);
169 static uint64_t spin_read(void *opaque
, hwaddr addr
, unsigned len
)
171 SpinState
*s
= opaque
;
172 uint8_t *spin_p
= &((uint8_t*)s
->spin
)[addr
];
176 return ldub_p(spin_p
);
178 return lduw_p(spin_p
);
180 return ldl_p(spin_p
);
182 hw_error("ppce500: unexpected %s with len = %u", __func__
, len
);
186 static const MemoryRegionOps spin_rw_ops
= {
189 .endianness
= DEVICE_BIG_ENDIAN
,
192 static int ppce500_spin_initfn(SysBusDevice
*dev
)
194 SpinState
*s
= E500_SPIN(dev
);
196 memory_region_init_io(&s
->iomem
, OBJECT(s
), &spin_rw_ops
, s
,
197 "e500 spin pv device", sizeof(SpinInfo
) * MAX_CPUS
);
198 sysbus_init_mmio(dev
, &s
->iomem
);
200 qemu_register_reset(spin_reset
, s
);
205 static void ppce500_spin_class_init(ObjectClass
*klass
, void *data
)
207 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
209 k
->init
= ppce500_spin_initfn
;
212 static const TypeInfo ppce500_spin_info
= {
213 .name
= TYPE_E500_SPIN
,
214 .parent
= TYPE_SYS_BUS_DEVICE
,
215 .instance_size
= sizeof(SpinState
),
216 .class_init
= ppce500_spin_class_init
,
219 static void ppce500_spin_register_types(void)
221 type_register_static(&ppce500_spin_info
);
224 type_init(ppce500_spin_register_types
)