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"
33 #include "sysemu/kvm.h"
37 typedef struct spin_info
{
43 } QEMU_PACKED SpinInfo
;
45 typedef struct spin_state
{
48 SpinInfo spin
[MAX_CPUS
];
51 typedef struct spin_kick
{
56 static void spin_reset(void *opaque
)
58 SpinState
*s
= opaque
;
61 for (i
= 0; i
< MAX_CPUS
; i
++) {
62 SpinInfo
*info
= &s
->spin
[i
];
70 /* Create -kernel TLB entries for BookE, linearly spanning 256MB. */
71 static inline hwaddr
booke206_page_size_to_tlb(uint64_t size
)
73 return (ffs(size
>> 10) - 1) >> 1;
76 static void mmubooke_create_initial_mapping(CPUPPCState
*env
,
81 ppcmas_tlb_t
*tlb
= booke206_get_tlbm(env
, 1, 0, 1);
84 size
= (booke206_page_size_to_tlb(len
) << MAS1_TSIZE_SHIFT
);
85 tlb
->mas1
= MAS1_VALID
| size
;
86 tlb
->mas2
= (va
& TARGET_PAGE_MASK
) | MAS2_M
;
87 tlb
->mas7_3
= pa
& TARGET_PAGE_MASK
;
88 tlb
->mas7_3
|= MAS3_UR
| MAS3_UW
| MAS3_UX
| MAS3_SR
| MAS3_SW
| MAS3_SX
;
89 env
->tlb_dirty
= true;
92 static void spin_kick(void *data
)
94 SpinKick
*kick
= data
;
95 CPUState
*cpu
= CPU(kick
->cpu
);
96 CPUPPCState
*env
= &kick
->cpu
->env
;
97 SpinInfo
*curspin
= kick
->spin
;
98 hwaddr map_size
= 64 * 1024 * 1024;
101 cpu_synchronize_state(env
);
102 stl_p(&curspin
->pir
, env
->spr
[SPR_PIR
]);
103 env
->nip
= ldq_p(&curspin
->addr
) & (map_size
- 1);
104 env
->gpr
[3] = ldq_p(&curspin
->r3
);
108 env
->gpr
[7] = map_size
;
112 map_start
= ldq_p(&curspin
->addr
) & ~(map_size
- 1);
113 mmubooke_create_initial_mapping(env
, 0, map_start
, map_size
);
116 env
->exception_index
= -1;
117 cpu
->stopped
= false;
121 static void spin_write(void *opaque
, hwaddr addr
, uint64_t value
,
124 SpinState
*s
= opaque
;
125 int env_idx
= addr
/ sizeof(SpinInfo
);
127 SpinInfo
*curspin
= &s
->spin
[env_idx
];
128 uint8_t *curspin_p
= (uint8_t*)curspin
;
130 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
131 if (env
->cpu_index
== env_idx
) {
141 if (!env
->cpu_index
) {
142 /* primary CPU doesn't spin */
146 curspin_p
= &curspin_p
[addr
% sizeof(SpinInfo
)];
149 stb_p(curspin_p
, value
);
152 stw_p(curspin_p
, value
);
155 stl_p(curspin_p
, value
);
159 if (!(ldq_p(&curspin
->addr
) & 1)) {
162 .cpu
= ppc_env_get_cpu(env
),
166 run_on_cpu(CPU(kick
.cpu
), spin_kick
, &kick
);
170 static uint64_t spin_read(void *opaque
, hwaddr addr
, unsigned len
)
172 SpinState
*s
= opaque
;
173 uint8_t *spin_p
= &((uint8_t*)s
->spin
)[addr
];
177 return ldub_p(spin_p
);
179 return lduw_p(spin_p
);
181 return ldl_p(spin_p
);
183 hw_error("ppce500: unexpected %s with len = %u", __func__
, len
);
187 static const MemoryRegionOps spin_rw_ops
= {
190 .endianness
= DEVICE_BIG_ENDIAN
,
193 static int ppce500_spin_initfn(SysBusDevice
*dev
)
197 s
= FROM_SYSBUS(SpinState
, sysbus_from_qdev(dev
));
199 memory_region_init_io(&s
->iomem
, &spin_rw_ops
, s
, "e500 spin pv device",
200 sizeof(SpinInfo
) * MAX_CPUS
);
201 sysbus_init_mmio(dev
, &s
->iomem
);
203 qemu_register_reset(spin_reset
, s
);
208 static void ppce500_spin_class_init(ObjectClass
*klass
, void *data
)
210 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
212 k
->init
= ppce500_spin_initfn
;
215 static TypeInfo ppce500_spin_info
= {
217 .parent
= TYPE_SYS_BUS_DEVICE
,
218 .instance_size
= sizeof(SpinState
),
219 .class_init
= ppce500_spin_class_init
,
222 static void ppce500_spin_register_types(void)
224 type_register_static(&ppce500_spin_info
);
227 type_init(ppce500_spin_register_types
)