]> git.proxmox.com Git - qemu.git/blame - hw/ppce500_spin.c
cpu: Move cpu_index field to CPUState
[qemu.git] / hw / ppce500_spin.c
CommitLineData
5c145dac
AG
1/*
2 * QEMU PowerPC e500v2 ePAPR spinning code
3 *
4 * Copyright (C) 2011 Freescale Semiconductor, Inc. All rights reserved.
5 *
6 * Author: Alexander Graf, <agraf@suse.de>
7 *
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.
12 *
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.
17 *
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/>.
20 *
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.
23 *
24 * Specification:
25 *
26 * https://www.power.org/resources/downloads/Power_ePAPR_APPROVED_v1.1.pdf
27 *
28 */
29
30#include "hw.h"
9c17d615 31#include "sysemu/sysemu.h"
5c145dac 32#include "sysbus.h"
9c17d615 33#include "sysemu/kvm.h"
5c145dac
AG
34
35#define MAX_CPUS 32
36
37typedef struct spin_info {
38 uint64_t addr;
39 uint64_t r3;
40 uint32_t resv;
41 uint32_t pir;
42 uint64_t reserved;
7c7bb022 43} QEMU_PACKED SpinInfo;
5c145dac
AG
44
45typedef struct spin_state {
46 SysBusDevice busdev;
47 MemoryRegion iomem;
48 SpinInfo spin[MAX_CPUS];
49} SpinState;
50
51typedef struct spin_kick {
b6444a42 52 PowerPCCPU *cpu;
5c145dac
AG
53 SpinInfo *spin;
54} SpinKick;
55
56static void spin_reset(void *opaque)
57{
58 SpinState *s = opaque;
59 int i;
60
61 for (i = 0; i < MAX_CPUS; i++) {
62 SpinInfo *info = &s->spin[i];
63
64 info->pir = i;
65 info->r3 = i;
66 info->addr = 1;
67 }
68}
69
70/* Create -kernel TLB entries for BookE, linearly spanning 256MB. */
a8170e5e 71static inline hwaddr booke206_page_size_to_tlb(uint64_t size)
5c145dac
AG
72{
73 return (ffs(size >> 10) - 1) >> 1;
74}
75
e2684c0b 76static void mmubooke_create_initial_mapping(CPUPPCState *env,
5c145dac 77 target_ulong va,
a8170e5e
AK
78 hwaddr pa,
79 hwaddr len)
5c145dac
AG
80{
81 ppcmas_tlb_t *tlb = booke206_get_tlbm(env, 1, 0, 1);
a8170e5e 82 hwaddr size;
5c145dac
AG
83
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;
58f90f21 89 env->tlb_dirty = true;
5c145dac
AG
90}
91
92static void spin_kick(void *data)
93{
94 SpinKick *kick = data;
f324e766 95 CPUState *cpu = CPU(kick->cpu);
b6444a42 96 CPUPPCState *env = &kick->cpu->env;
5c145dac 97 SpinInfo *curspin = kick->spin;
a8170e5e
AK
98 hwaddr map_size = 64 * 1024 * 1024;
99 hwaddr map_start;
5c145dac
AG
100
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);
105 env->gpr[4] = 0;
106 env->gpr[5] = 0;
107 env->gpr[6] = 0;
108 env->gpr[7] = map_size;
109 env->gpr[8] = 0;
110 env->gpr[9] = 0;
111
112 map_start = ldq_p(&curspin->addr) & ~(map_size - 1);
113 mmubooke_create_initial_mapping(env, 0, map_start, map_size);
114
115 env->halted = 0;
116 env->exception_index = -1;
f324e766 117 cpu->stopped = false;
c08d7424 118 qemu_cpu_kick(cpu);
5c145dac
AG
119}
120
a8170e5e 121static void spin_write(void *opaque, hwaddr addr, uint64_t value,
5c145dac
AG
122 unsigned len)
123{
124 SpinState *s = opaque;
125 int env_idx = addr / sizeof(SpinInfo);
e2684c0b 126 CPUPPCState *env;
55e5c285 127 CPUState *cpu = NULL;
5c145dac
AG
128 SpinInfo *curspin = &s->spin[env_idx];
129 uint8_t *curspin_p = (uint8_t*)curspin;
130
131 for (env = first_cpu; env != NULL; env = env->next_cpu) {
55e5c285
AF
132 cpu = CPU(ppc_env_get_cpu(env));
133 if (cpu->cpu_index == env_idx) {
5c145dac
AG
134 break;
135 }
136 }
137
55e5c285 138 if (cpu == NULL) {
5c145dac
AG
139 /* Unknown CPU */
140 return;
141 }
142
55e5c285 143 if (cpu->cpu_index == 0) {
5c145dac
AG
144 /* primary CPU doesn't spin */
145 return;
146 }
147
148 curspin_p = &curspin_p[addr % sizeof(SpinInfo)];
149 switch (len) {
150 case 1:
151 stb_p(curspin_p, value);
152 break;
153 case 2:
154 stw_p(curspin_p, value);
155 break;
156 case 4:
157 stl_p(curspin_p, value);
158 break;
159 }
160
161 if (!(ldq_p(&curspin->addr) & 1)) {
162 /* run CPU */
163 SpinKick kick = {
b6444a42 164 .cpu = ppc_env_get_cpu(env),
5c145dac
AG
165 .spin = curspin,
166 };
167
f100f0b3 168 run_on_cpu(CPU(kick.cpu), spin_kick, &kick);
5c145dac
AG
169 }
170}
171
a8170e5e 172static uint64_t spin_read(void *opaque, hwaddr addr, unsigned len)
5c145dac
AG
173{
174 SpinState *s = opaque;
175 uint8_t *spin_p = &((uint8_t*)s->spin)[addr];
176
177 switch (len) {
178 case 1:
179 return ldub_p(spin_p);
180 case 2:
181 return lduw_p(spin_p);
182 case 4:
183 return ldl_p(spin_p);
184 default:
5f2c23e6 185 hw_error("ppce500: unexpected %s with len = %u", __func__, len);
5c145dac
AG
186 }
187}
188
b7c28f02 189static const MemoryRegionOps spin_rw_ops = {
5c145dac
AG
190 .read = spin_read,
191 .write = spin_write,
192 .endianness = DEVICE_BIG_ENDIAN,
193};
194
195static int ppce500_spin_initfn(SysBusDevice *dev)
196{
197 SpinState *s;
198
199 s = FROM_SYSBUS(SpinState, sysbus_from_qdev(dev));
200
201 memory_region_init_io(&s->iomem, &spin_rw_ops, s, "e500 spin pv device",
202 sizeof(SpinInfo) * MAX_CPUS);
750ecd44 203 sysbus_init_mmio(dev, &s->iomem);
5c145dac
AG
204
205 qemu_register_reset(spin_reset, s);
206
207 return 0;
208}
209
999e12bb
AL
210static void ppce500_spin_class_init(ObjectClass *klass, void *data)
211{
212 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
213
214 k->init = ppce500_spin_initfn;
215}
216
8c43a6f0 217static const TypeInfo ppce500_spin_info = {
39bffca2
AL
218 .name = "e500-spin",
219 .parent = TYPE_SYS_BUS_DEVICE,
220 .instance_size = sizeof(SpinState),
221 .class_init = ppce500_spin_class_init,
5c145dac
AG
222};
223
83f7d43a 224static void ppce500_spin_register_types(void)
5c145dac 225{
39bffca2 226 type_register_static(&ppce500_spin_info);
5c145dac 227}
83f7d43a
AF
228
229type_init(ppce500_spin_register_types)