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 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_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | 55 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_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS, 63 .pcr_level = PCR_COMPAT_2_06, 64 .max_vthreads = 4, 65 }, 66 { 67 .name = "power7+", 68 .pvr = CPU_POWERPC_LOGICAL_2_06_PLUS, 69 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS, 70 .pcr_level = PCR_COMPAT_2_06, 71 .max_vthreads = 4, 72 }, 73 { /* POWER8, ISA2.07 */ 74 .name = "power8", 75 .pvr = CPU_POWERPC_LOGICAL_2_07, 76 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07, 77 .pcr_level = PCR_COMPAT_2_07, 78 .max_vthreads = 8, 79 }, 80 { /* POWER9, ISA3.00 */ 81 .name = "power9", 82 .pvr = CPU_POWERPC_LOGICAL_3_00, 83 .pcr = PCR_COMPAT_3_00, 84 .pcr_level = PCR_COMPAT_3_00, 85 /* 86 * POWER9 hardware only supports 4 threads / core, but this 87 * limit is for guests. We need to support 8 vthreads/vcore 88 * on POWER9 for POWER8 compatibility guests, and it's very 89 * confusing if half of the threads disappear from the guest 90 * if it announces it's POWER9 aware at CAS time. 91 */ 92 .max_vthreads = 8, 93 }, 94 }; 95 96 static const CompatInfo *compat_by_pvr(uint32_t pvr) 97 { 98 int i; 99 100 for (i = 0; i < ARRAY_SIZE(compat_table); i++) { 101 if (compat_table[i].pvr == pvr) { 102 return &compat_table[i]; 103 } 104 } 105 return NULL; 106 } 107 108 bool ppc_check_compat(PowerPCCPU *cpu, uint32_t compat_pvr, 109 uint32_t min_compat_pvr, uint32_t max_compat_pvr) 110 { 111 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 112 const CompatInfo *compat = compat_by_pvr(compat_pvr); 113 const CompatInfo *min = compat_by_pvr(min_compat_pvr); 114 const CompatInfo *max = compat_by_pvr(max_compat_pvr); 115 116 #if !defined(CONFIG_USER_ONLY) 117 g_assert(cpu->vhyp); 118 #endif 119 g_assert(!min_compat_pvr || min); 120 g_assert(!max_compat_pvr || max); 121 122 if (!compat) { 123 /* Not a recognized logical PVR */ 124 return false; 125 } 126 if ((min && (compat < min)) || (max && (compat > max))) { 127 /* Outside specified range */ 128 return false; 129 } 130 if (!(pcc->pcr_supported & compat->pcr_level)) { 131 /* Not supported by this CPU */ 132 return false; 133 } 134 return true; 135 } 136 137 void ppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr, Error **errp) 138 { 139 const CompatInfo *compat = compat_by_pvr(compat_pvr); 140 CPUPPCState *env = &cpu->env; 141 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 142 uint64_t pcr; 143 144 if (!compat_pvr) { 145 pcr = 0; 146 } else if (!compat) { 147 error_setg(errp, "Unknown compatibility PVR 0x%08"PRIx32, compat_pvr); 148 return; 149 } else if (!ppc_check_compat(cpu, compat_pvr, 0, 0)) { 150 error_setg(errp, "Compatibility PVR 0x%08"PRIx32" not valid for CPU", 151 compat_pvr); 152 return; 153 } else { 154 pcr = compat->pcr; 155 } 156 157 cpu_synchronize_state(CPU(cpu)); 158 159 if (kvm_enabled() && cpu->compat_pvr != compat_pvr) { 160 int ret = kvmppc_set_compat(cpu, compat_pvr); 161 if (ret < 0) { 162 error_setg_errno(errp, -ret, 163 "Unable to set CPU compatibility mode in KVM"); 164 return; 165 } 166 } 167 168 cpu->compat_pvr = compat_pvr; 169 env->spr[SPR_PCR] = pcr & pcc->pcr_mask; 170 } 171 172 typedef struct { 173 uint32_t compat_pvr; 174 Error *err; 175 } SetCompatState; 176 177 static void do_set_compat(CPUState *cs, run_on_cpu_data arg) 178 { 179 PowerPCCPU *cpu = POWERPC_CPU(cs); 180 SetCompatState *s = arg.host_ptr; 181 182 ppc_set_compat(cpu, s->compat_pvr, &s->err); 183 } 184 185 void ppc_set_compat_all(uint32_t compat_pvr, Error **errp) 186 { 187 CPUState *cs; 188 189 CPU_FOREACH(cs) { 190 SetCompatState s = { 191 .compat_pvr = compat_pvr, 192 .err = NULL, 193 }; 194 195 run_on_cpu(cs, do_set_compat, RUN_ON_CPU_HOST_PTR(&s)); 196 197 if (s.err) { 198 error_propagate(errp, s.err); 199 return; 200 } 201 } 202 } 203 204 int ppc_compat_max_vthreads(PowerPCCPU *cpu) 205 { 206 const CompatInfo *compat = compat_by_pvr(cpu->compat_pvr); 207 int n_threads = CPU(cpu)->nr_threads; 208 209 if (cpu->compat_pvr) { 210 g_assert(compat); 211 n_threads = MIN(n_threads, compat->max_vthreads); 212 } 213 214 return n_threads; 215 } 216 217 static void ppc_compat_prop_get(Object *obj, Visitor *v, const char *name, 218 void *opaque, Error **errp) 219 { 220 uint32_t compat_pvr = *((uint32_t *)opaque); 221 const char *value; 222 223 if (!compat_pvr) { 224 value = ""; 225 } else { 226 const CompatInfo *compat = compat_by_pvr(compat_pvr); 227 228 g_assert(compat); 229 230 value = compat->name; 231 } 232 233 visit_type_str(v, name, (char **)&value, errp); 234 } 235 236 static void ppc_compat_prop_set(Object *obj, Visitor *v, const char *name, 237 void *opaque, Error **errp) 238 { 239 Error *local_err = NULL; 240 char *value; 241 uint32_t compat_pvr; 242 243 visit_type_str(v, name, &value, &local_err); 244 if (local_err) { 245 error_propagate(errp, local_err); 246 return; 247 } 248 249 if (strcmp(value, "") == 0) { 250 compat_pvr = 0; 251 } else { 252 int i; 253 const CompatInfo *compat = NULL; 254 255 for (i = 0; i < ARRAY_SIZE(compat_table); i++) { 256 if (strcmp(value, compat_table[i].name) == 0) { 257 compat = &compat_table[i]; 258 break; 259 260 } 261 } 262 263 if (!compat) { 264 error_setg(errp, "Invalid compatibility mode \"%s\"", value); 265 goto out; 266 } 267 compat_pvr = compat->pvr; 268 } 269 270 *((uint32_t *)opaque) = compat_pvr; 271 272 out: 273 g_free(value); 274 } 275 276 void ppc_compat_add_property(Object *obj, const char *name, 277 uint32_t *compat_pvr, const char *basedesc, 278 Error **errp) 279 { 280 Error *local_err = NULL; 281 gchar *namesv[ARRAY_SIZE(compat_table) + 1]; 282 gchar *names, *desc; 283 int i; 284 285 object_property_add(obj, name, "string", 286 ppc_compat_prop_get, ppc_compat_prop_set, NULL, 287 compat_pvr, &local_err); 288 if (local_err) { 289 goto out; 290 } 291 292 for (i = 0; i < ARRAY_SIZE(compat_table); i++) { 293 /* 294 * Have to discard const here, because g_strjoinv() takes 295 * (gchar **), not (const gchar **) :( 296 */ 297 namesv[i] = (gchar *)compat_table[i].name; 298 } 299 namesv[ARRAY_SIZE(compat_table)] = NULL; 300 301 names = g_strjoinv(", ", namesv); 302 desc = g_strdup_printf("%s. Valid values are %s.", basedesc, names); 303 object_property_set_description(obj, name, desc, &local_err); 304 305 g_free(names); 306 g_free(desc); 307 308 out: 309 error_propagate(errp, local_err); 310 } 311