1 /* 2 * PowerPC CPU initialization for qemu. 3 * 4 * Copyright 2016, David Gibson, Red Hat Inc. <dgibson@redhat.com> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "sysemu/hw_accel.h" 22 #include "sysemu/kvm.h" 23 #include "kvm_ppc.h" 24 #include "sysemu/cpus.h" 25 #include "qemu/error-report.h" 26 #include "qapi/error.h" 27 #include "qapi/visitor.h" 28 #include "cpu-models.h" 29 30 typedef struct { 31 const char *name; 32 uint32_t pvr; 33 uint64_t pcr; 34 uint64_t pcr_level; 35 36 /* 37 * Maximum allowed virtual threads per virtual core 38 * 39 * This is to stop older guests getting confused by seeing more 40 * threads than they think the cpu can support. Usually it's 41 * equal to the number of threads supported on bare metal 42 * hardware, but not always (see POWER9). 43 */ 44 int max_vthreads; 45 } CompatInfo; 46 47 static const CompatInfo compat_table[] = { 48 /* 49 * Ordered from oldest to newest - the code relies on this 50 */ 51 { /* POWER6, ISA2.05 */ 52 .name = "power6", 53 .pvr = CPU_POWERPC_LOGICAL_2_05, 54 .pcr = PCR_COMPAT_3_10 | PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | 55 PCR_COMPAT_2_06 | PCR_COMPAT_2_05 | PCR_TM_DIS | PCR_VSX_DIS, 56 .pcr_level = PCR_COMPAT_2_05, 57 .max_vthreads = 2, 58 }, 59 { /* POWER7, ISA2.06 */ 60 .name = "power7", 61 .pvr = CPU_POWERPC_LOGICAL_2_06, 62 .pcr = PCR_COMPAT_3_10 | PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | 63 PCR_COMPAT_2_06 | PCR_TM_DIS, 64 .pcr_level = PCR_COMPAT_2_06, 65 .max_vthreads = 4, 66 }, 67 { 68 .name = "power7+", 69 .pvr = CPU_POWERPC_LOGICAL_2_06_PLUS, 70 .pcr = PCR_COMPAT_3_10 | PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | 71 PCR_COMPAT_2_06 | PCR_TM_DIS, 72 .pcr_level = PCR_COMPAT_2_06, 73 .max_vthreads = 4, 74 }, 75 { /* POWER8, ISA2.07 */ 76 .name = "power8", 77 .pvr = CPU_POWERPC_LOGICAL_2_07, 78 .pcr = PCR_COMPAT_3_10 | PCR_COMPAT_3_00 | PCR_COMPAT_2_07, 79 .pcr_level = PCR_COMPAT_2_07, 80 .max_vthreads = 8, 81 }, 82 { /* POWER9, ISA3.00 */ 83 .name = "power9", 84 .pvr = CPU_POWERPC_LOGICAL_3_00, 85 .pcr = PCR_COMPAT_3_10 | PCR_COMPAT_3_00, 86 .pcr_level = PCR_COMPAT_3_00, 87 /* 88 * POWER9 hardware only supports 4 threads / core, but this 89 * limit is for guests. We need to support 8 vthreads/vcore 90 * on POWER9 for POWER8 compatibility guests, and it's very 91 * confusing if half of the threads disappear from the guest 92 * if it announces it's POWER9 aware at CAS time. 93 */ 94 .max_vthreads = 8, 95 }, 96 { /* POWER10, ISA3.10 */ 97 .name = "power10", 98 .pvr = CPU_POWERPC_LOGICAL_3_10, 99 .pcr = PCR_COMPAT_3_10, 100 .pcr_level = PCR_COMPAT_3_10, 101 .max_vthreads = 8, 102 }, 103 { /* POWER11, ISA3.10 */ 104 .name = "power11", 105 .pvr = CPU_POWERPC_LOGICAL_3_10_P11, 106 .pcr = PCR_COMPAT_3_10, 107 .pcr_level = PCR_COMPAT_3_10, 108 .max_vthreads = 8, 109 }, 110 }; 111 112 static const CompatInfo *compat_by_pvr(uint32_t pvr) 113 { 114 int i; 115 116 for (i = 0; i < ARRAY_SIZE(compat_table); i++) { 117 if (compat_table[i].pvr == pvr) { 118 return &compat_table[i]; 119 } 120 } 121 return NULL; 122 } 123 124 static bool pcc_compat(PowerPCCPUClass *pcc, uint32_t compat_pvr, 125 uint32_t min_compat_pvr, uint32_t max_compat_pvr) 126 { 127 const CompatInfo *compat = compat_by_pvr(compat_pvr); 128 const CompatInfo *min = compat_by_pvr(min_compat_pvr); 129 const CompatInfo *max = compat_by_pvr(max_compat_pvr); 130 131 g_assert(!min_compat_pvr || min); 132 g_assert(!max_compat_pvr || max); 133 134 if (!compat) { 135 /* Not a recognized logical PVR */ 136 return false; 137 } 138 if ((min && (compat < min)) || (max && (compat > max))) { 139 /* Outside specified range */ 140 return false; 141 } 142 if (compat->pvr > pcc->spapr_logical_pvr) { 143 /* Older CPU cannot support a newer processor's compat mode */ 144 return false; 145 } 146 if (!(pcc->pcr_supported & compat->pcr_level)) { 147 /* Not supported by this CPU */ 148 return false; 149 } 150 return true; 151 } 152 153 bool ppc_check_compat(PowerPCCPU *cpu, uint32_t compat_pvr, 154 uint32_t min_compat_pvr, uint32_t max_compat_pvr) 155 { 156 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 157 158 #if !defined(CONFIG_USER_ONLY) 159 g_assert(cpu->vhyp); 160 #endif 161 162 return pcc_compat(pcc, compat_pvr, min_compat_pvr, max_compat_pvr); 163 } 164 165 bool ppc_type_check_compat(const char *cputype, uint32_t compat_pvr, 166 uint32_t min_compat_pvr, uint32_t max_compat_pvr) 167 { 168 PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(object_class_by_name(cputype)); 169 return pcc_compat(pcc, compat_pvr, min_compat_pvr, max_compat_pvr); 170 } 171 172 int ppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr, Error **errp) 173 { 174 const CompatInfo *compat = compat_by_pvr(compat_pvr); 175 CPUPPCState *env = &cpu->env; 176 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 177 uint64_t pcr; 178 179 if (!compat_pvr) { 180 pcr = 0; 181 } else if (!compat) { 182 error_setg(errp, "Unknown compatibility PVR 0x%08"PRIx32, compat_pvr); 183 return -EINVAL; 184 } else if (!ppc_check_compat(cpu, compat_pvr, 0, 0)) { 185 error_setg(errp, "Compatibility PVR 0x%08"PRIx32" not valid for CPU", 186 compat_pvr); 187 return -EINVAL; 188 } else { 189 pcr = compat->pcr; 190 } 191 192 cpu_synchronize_state(CPU(cpu)); 193 194 if (kvm_enabled() && cpu->compat_pvr != compat_pvr) { 195 int ret = kvmppc_set_compat(cpu, compat_pvr); 196 if (ret < 0) { 197 error_setg_errno(errp, -ret, 198 "Unable to set CPU compatibility mode in KVM"); 199 return ret; 200 } 201 } 202 203 cpu->compat_pvr = compat_pvr; 204 env->spr[SPR_PCR] = pcr & pcc->pcr_mask; 205 return 0; 206 } 207 208 typedef struct { 209 uint32_t compat_pvr; 210 Error **errp; 211 int ret; 212 } SetCompatState; 213 214 static void do_set_compat(CPUState *cs, run_on_cpu_data arg) 215 { 216 PowerPCCPU *cpu = POWERPC_CPU(cs); 217 SetCompatState *s = arg.host_ptr; 218 219 s->ret = ppc_set_compat(cpu, s->compat_pvr, s->errp); 220 } 221 222 int ppc_set_compat_all(uint32_t compat_pvr, Error **errp) 223 { 224 CPUState *cs; 225 226 CPU_FOREACH(cs) { 227 SetCompatState s = { 228 .compat_pvr = compat_pvr, 229 .errp = errp, 230 .ret = 0, 231 }; 232 233 run_on_cpu(cs, do_set_compat, RUN_ON_CPU_HOST_PTR(&s)); 234 235 if (s.ret < 0) { 236 return s.ret; 237 } 238 } 239 240 return 0; 241 } 242 243 /* To be used when the machine is not running */ 244 int ppc_init_compat_all(uint32_t compat_pvr, Error **errp) 245 { 246 CPUState *cs; 247 248 CPU_FOREACH(cs) { 249 PowerPCCPU *cpu = POWERPC_CPU(cs); 250 int ret; 251 252 ret = ppc_set_compat(cpu, compat_pvr, errp); 253 254 if (ret < 0) { 255 return ret; 256 } 257 } 258 259 return 0; 260 } 261 262 int ppc_compat_max_vthreads(PowerPCCPU *cpu) 263 { 264 const CompatInfo *compat = compat_by_pvr(cpu->compat_pvr); 265 int n_threads = CPU(cpu)->nr_threads; 266 267 if (cpu->compat_pvr) { 268 g_assert(compat); 269 n_threads = MIN(n_threads, compat->max_vthreads); 270 } 271 272 return n_threads; 273 } 274 275 static void ppc_compat_prop_get(Object *obj, Visitor *v, const char *name, 276 void *opaque, Error **errp) 277 { 278 uint32_t compat_pvr = *((uint32_t *)opaque); 279 const char *value; 280 281 if (!compat_pvr) { 282 value = ""; 283 } else { 284 const CompatInfo *compat = compat_by_pvr(compat_pvr); 285 286 g_assert(compat); 287 288 value = compat->name; 289 } 290 291 visit_type_str(v, name, (char **)&value, errp); 292 } 293 294 static void ppc_compat_prop_set(Object *obj, Visitor *v, const char *name, 295 void *opaque, Error **errp) 296 { 297 char *value; 298 uint32_t compat_pvr; 299 300 if (!visit_type_str(v, name, &value, errp)) { 301 return; 302 } 303 304 if (strcmp(value, "") == 0) { 305 compat_pvr = 0; 306 } else { 307 int i; 308 const CompatInfo *compat = NULL; 309 310 for (i = 0; i < ARRAY_SIZE(compat_table); i++) { 311 if (strcmp(value, compat_table[i].name) == 0) { 312 compat = &compat_table[i]; 313 break; 314 315 } 316 } 317 318 if (!compat) { 319 error_setg(errp, "Invalid compatibility mode \"%s\"", value); 320 goto out; 321 } 322 compat_pvr = compat->pvr; 323 } 324 325 *((uint32_t *)opaque) = compat_pvr; 326 327 out: 328 g_free(value); 329 } 330 331 void ppc_compat_add_property(Object *obj, const char *name, 332 uint32_t *compat_pvr, const char *basedesc) 333 { 334 gchar *namesv[ARRAY_SIZE(compat_table) + 1]; 335 gchar *names, *desc; 336 int i; 337 338 object_property_add(obj, name, "string", 339 ppc_compat_prop_get, ppc_compat_prop_set, NULL, 340 compat_pvr); 341 342 for (i = 0; i < ARRAY_SIZE(compat_table); i++) { 343 /* 344 * Have to discard const here, because g_strjoinv() takes 345 * (gchar **), not (const gchar **) :( 346 */ 347 namesv[i] = (gchar *)compat_table[i].name; 348 } 349 namesv[ARRAY_SIZE(compat_table)] = NULL; 350 351 names = g_strjoinv(", ", namesv); 352 desc = g_strdup_printf("%s. Valid values are %s.", basedesc, names); 353 object_property_set_description(obj, name, desc); 354 355 g_free(names); 356 g_free(desc); 357 } 358