1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * QEMU LoongArch KVM 4 * 5 * Copyright (c) 2023 Loongson Technology Corporation Limited 6 */ 7 8 #include "qemu/osdep.h" 9 #include <sys/ioctl.h> 10 #include <linux/kvm.h> 11 12 #include "qemu/timer.h" 13 #include "qemu/error-report.h" 14 #include "qemu/main-loop.h" 15 #include "sysemu/sysemu.h" 16 #include "sysemu/kvm.h" 17 #include "sysemu/kvm_int.h" 18 #include "hw/pci/pci.h" 19 #include "exec/memattrs.h" 20 #include "exec/address-spaces.h" 21 #include "hw/boards.h" 22 #include "hw/irq.h" 23 #include "qemu/log.h" 24 #include "hw/loader.h" 25 #include "sysemu/runstate.h" 26 #include "cpu-csr.h" 27 #include "kvm_loongarch.h" 28 #include "trace.h" 29 30 static bool cap_has_mp_state; 31 const KVMCapabilityInfo kvm_arch_required_capabilities[] = { 32 KVM_CAP_LAST_INFO 33 }; 34 35 static int kvm_loongarch_get_regs_core(CPUState *cs) 36 { 37 int ret = 0; 38 int i; 39 struct kvm_regs regs; 40 CPULoongArchState *env = cpu_env(cs); 41 42 /* Get the current register set as KVM seems it */ 43 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); 44 if (ret < 0) { 45 trace_kvm_failed_get_regs_core(strerror(errno)); 46 return ret; 47 } 48 /* gpr[0] value is always 0 */ 49 env->gpr[0] = 0; 50 for (i = 1; i < 32; i++) { 51 env->gpr[i] = regs.gpr[i]; 52 } 53 54 env->pc = regs.pc; 55 return ret; 56 } 57 58 static int kvm_loongarch_put_regs_core(CPUState *cs) 59 { 60 int ret = 0; 61 int i; 62 struct kvm_regs regs; 63 CPULoongArchState *env = cpu_env(cs); 64 65 /* Set the registers based on QEMU's view of things */ 66 for (i = 0; i < 32; i++) { 67 regs.gpr[i] = env->gpr[i]; 68 } 69 70 regs.pc = env->pc; 71 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s); 72 if (ret < 0) { 73 trace_kvm_failed_put_regs_core(strerror(errno)); 74 } 75 76 return ret; 77 } 78 79 static int kvm_loongarch_get_csr(CPUState *cs) 80 { 81 int ret = 0; 82 CPULoongArchState *env = cpu_env(cs); 83 84 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_CRMD), 85 &env->CSR_CRMD); 86 87 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRMD), 88 &env->CSR_PRMD); 89 90 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_EUEN), 91 &env->CSR_EUEN); 92 93 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_MISC), 94 &env->CSR_MISC); 95 96 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ECFG), 97 &env->CSR_ECFG); 98 99 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ESTAT), 100 &env->CSR_ESTAT); 101 102 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ERA), 103 &env->CSR_ERA); 104 105 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_BADV), 106 &env->CSR_BADV); 107 108 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_BADI), 109 &env->CSR_BADI); 110 111 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_EENTRY), 112 &env->CSR_EENTRY); 113 114 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBIDX), 115 &env->CSR_TLBIDX); 116 117 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBEHI), 118 &env->CSR_TLBEHI); 119 120 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBELO0), 121 &env->CSR_TLBELO0); 122 123 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBELO1), 124 &env->CSR_TLBELO1); 125 126 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ASID), 127 &env->CSR_ASID); 128 129 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PGDL), 130 &env->CSR_PGDL); 131 132 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PGDH), 133 &env->CSR_PGDH); 134 135 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PGD), 136 &env->CSR_PGD); 137 138 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PWCL), 139 &env->CSR_PWCL); 140 141 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PWCH), 142 &env->CSR_PWCH); 143 144 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_STLBPS), 145 &env->CSR_STLBPS); 146 147 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_RVACFG), 148 &env->CSR_RVACFG); 149 150 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_CPUID), 151 &env->CSR_CPUID); 152 153 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRCFG1), 154 &env->CSR_PRCFG1); 155 156 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRCFG2), 157 &env->CSR_PRCFG2); 158 159 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRCFG3), 160 &env->CSR_PRCFG3); 161 162 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(0)), 163 &env->CSR_SAVE[0]); 164 165 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(1)), 166 &env->CSR_SAVE[1]); 167 168 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(2)), 169 &env->CSR_SAVE[2]); 170 171 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(3)), 172 &env->CSR_SAVE[3]); 173 174 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(4)), 175 &env->CSR_SAVE[4]); 176 177 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(5)), 178 &env->CSR_SAVE[5]); 179 180 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(6)), 181 &env->CSR_SAVE[6]); 182 183 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(7)), 184 &env->CSR_SAVE[7]); 185 186 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TID), 187 &env->CSR_TID); 188 189 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_CNTC), 190 &env->CSR_CNTC); 191 192 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TICLR), 193 &env->CSR_TICLR); 194 195 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_LLBCTL), 196 &env->CSR_LLBCTL); 197 198 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_IMPCTL1), 199 &env->CSR_IMPCTL1); 200 201 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_IMPCTL2), 202 &env->CSR_IMPCTL2); 203 204 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRENTRY), 205 &env->CSR_TLBRENTRY); 206 207 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRBADV), 208 &env->CSR_TLBRBADV); 209 210 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRERA), 211 &env->CSR_TLBRERA); 212 213 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRSAVE), 214 &env->CSR_TLBRSAVE); 215 216 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRELO0), 217 &env->CSR_TLBRELO0); 218 219 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRELO1), 220 &env->CSR_TLBRELO1); 221 222 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBREHI), 223 &env->CSR_TLBREHI); 224 225 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRPRMD), 226 &env->CSR_TLBRPRMD); 227 228 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(0)), 229 &env->CSR_DMW[0]); 230 231 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(1)), 232 &env->CSR_DMW[1]); 233 234 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(2)), 235 &env->CSR_DMW[2]); 236 237 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(3)), 238 &env->CSR_DMW[3]); 239 240 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TVAL), 241 &env->CSR_TVAL); 242 243 ret |= kvm_get_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TCFG), 244 &env->CSR_TCFG); 245 246 return ret; 247 } 248 249 static int kvm_loongarch_put_csr(CPUState *cs, int level) 250 { 251 int ret = 0; 252 CPULoongArchState *env = cpu_env(cs); 253 254 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_CRMD), 255 &env->CSR_CRMD); 256 257 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRMD), 258 &env->CSR_PRMD); 259 260 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_EUEN), 261 &env->CSR_EUEN); 262 263 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_MISC), 264 &env->CSR_MISC); 265 266 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ECFG), 267 &env->CSR_ECFG); 268 269 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ESTAT), 270 &env->CSR_ESTAT); 271 272 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ERA), 273 &env->CSR_ERA); 274 275 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_BADV), 276 &env->CSR_BADV); 277 278 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_BADI), 279 &env->CSR_BADI); 280 281 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_EENTRY), 282 &env->CSR_EENTRY); 283 284 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBIDX), 285 &env->CSR_TLBIDX); 286 287 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBEHI), 288 &env->CSR_TLBEHI); 289 290 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBELO0), 291 &env->CSR_TLBELO0); 292 293 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBELO1), 294 &env->CSR_TLBELO1); 295 296 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_ASID), 297 &env->CSR_ASID); 298 299 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PGDL), 300 &env->CSR_PGDL); 301 302 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PGDH), 303 &env->CSR_PGDH); 304 305 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PGD), 306 &env->CSR_PGD); 307 308 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PWCL), 309 &env->CSR_PWCL); 310 311 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PWCH), 312 &env->CSR_PWCH); 313 314 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_STLBPS), 315 &env->CSR_STLBPS); 316 317 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_RVACFG), 318 &env->CSR_RVACFG); 319 320 /* CPUID is constant after poweron, it should be set only once */ 321 if (level >= KVM_PUT_FULL_STATE) { 322 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_CPUID), 323 &env->CSR_CPUID); 324 } 325 326 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRCFG1), 327 &env->CSR_PRCFG1); 328 329 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRCFG2), 330 &env->CSR_PRCFG2); 331 332 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_PRCFG3), 333 &env->CSR_PRCFG3); 334 335 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(0)), 336 &env->CSR_SAVE[0]); 337 338 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(1)), 339 &env->CSR_SAVE[1]); 340 341 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(2)), 342 &env->CSR_SAVE[2]); 343 344 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(3)), 345 &env->CSR_SAVE[3]); 346 347 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(4)), 348 &env->CSR_SAVE[4]); 349 350 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(5)), 351 &env->CSR_SAVE[5]); 352 353 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(6)), 354 &env->CSR_SAVE[6]); 355 356 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_SAVE(7)), 357 &env->CSR_SAVE[7]); 358 359 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TID), 360 &env->CSR_TID); 361 362 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_CNTC), 363 &env->CSR_CNTC); 364 365 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TICLR), 366 &env->CSR_TICLR); 367 368 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_LLBCTL), 369 &env->CSR_LLBCTL); 370 371 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_IMPCTL1), 372 &env->CSR_IMPCTL1); 373 374 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_IMPCTL2), 375 &env->CSR_IMPCTL2); 376 377 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRENTRY), 378 &env->CSR_TLBRENTRY); 379 380 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRBADV), 381 &env->CSR_TLBRBADV); 382 383 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRERA), 384 &env->CSR_TLBRERA); 385 386 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRSAVE), 387 &env->CSR_TLBRSAVE); 388 389 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRELO0), 390 &env->CSR_TLBRELO0); 391 392 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRELO1), 393 &env->CSR_TLBRELO1); 394 395 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBREHI), 396 &env->CSR_TLBREHI); 397 398 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TLBRPRMD), 399 &env->CSR_TLBRPRMD); 400 401 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(0)), 402 &env->CSR_DMW[0]); 403 404 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(1)), 405 &env->CSR_DMW[1]); 406 407 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(2)), 408 &env->CSR_DMW[2]); 409 410 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_DMW(3)), 411 &env->CSR_DMW[3]); 412 /* 413 * timer cfg must be put at last since it is used to enable 414 * guest timer 415 */ 416 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TVAL), 417 &env->CSR_TVAL); 418 419 ret |= kvm_set_one_reg(cs, KVM_IOC_CSRID(LOONGARCH_CSR_TCFG), 420 &env->CSR_TCFG); 421 return ret; 422 } 423 424 static int kvm_loongarch_get_regs_fp(CPUState *cs) 425 { 426 int ret, i; 427 struct kvm_fpu fpu; 428 CPULoongArchState *env = cpu_env(cs); 429 430 ret = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu); 431 if (ret < 0) { 432 trace_kvm_failed_get_fpu(strerror(errno)); 433 return ret; 434 } 435 436 env->fcsr0 = fpu.fcsr; 437 for (i = 0; i < 32; i++) { 438 env->fpr[i].vreg.UD[0] = fpu.fpr[i].val64[0]; 439 env->fpr[i].vreg.UD[1] = fpu.fpr[i].val64[1]; 440 env->fpr[i].vreg.UD[2] = fpu.fpr[i].val64[2]; 441 env->fpr[i].vreg.UD[3] = fpu.fpr[i].val64[3]; 442 } 443 for (i = 0; i < 8; i++) { 444 env->cf[i] = fpu.fcc & 0xFF; 445 fpu.fcc = fpu.fcc >> 8; 446 } 447 448 return ret; 449 } 450 451 static int kvm_loongarch_put_regs_fp(CPUState *cs) 452 { 453 int ret, i; 454 struct kvm_fpu fpu; 455 CPULoongArchState *env = cpu_env(cs); 456 457 fpu.fcsr = env->fcsr0; 458 fpu.fcc = 0; 459 for (i = 0; i < 32; i++) { 460 fpu.fpr[i].val64[0] = env->fpr[i].vreg.UD[0]; 461 fpu.fpr[i].val64[1] = env->fpr[i].vreg.UD[1]; 462 fpu.fpr[i].val64[2] = env->fpr[i].vreg.UD[2]; 463 fpu.fpr[i].val64[3] = env->fpr[i].vreg.UD[3]; 464 } 465 466 for (i = 0; i < 8; i++) { 467 fpu.fcc |= env->cf[i] << (8 * i); 468 } 469 470 ret = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu); 471 if (ret < 0) { 472 trace_kvm_failed_put_fpu(strerror(errno)); 473 } 474 475 return ret; 476 } 477 478 void kvm_arch_reset_vcpu(CPULoongArchState *env) 479 { 480 env->mp_state = KVM_MP_STATE_RUNNABLE; 481 } 482 483 static int kvm_loongarch_get_mpstate(CPUState *cs) 484 { 485 int ret = 0; 486 struct kvm_mp_state mp_state; 487 CPULoongArchState *env = cpu_env(cs); 488 489 if (cap_has_mp_state) { 490 ret = kvm_vcpu_ioctl(cs, KVM_GET_MP_STATE, &mp_state); 491 if (ret) { 492 trace_kvm_failed_get_mpstate(strerror(errno)); 493 return ret; 494 } 495 env->mp_state = mp_state.mp_state; 496 } 497 498 return ret; 499 } 500 501 static int kvm_loongarch_put_mpstate(CPUState *cs) 502 { 503 int ret = 0; 504 struct kvm_mp_state mp_state = { 505 .mp_state = cpu_env(cs)->mp_state 506 }; 507 508 if (cap_has_mp_state) { 509 ret = kvm_vcpu_ioctl(cs, KVM_SET_MP_STATE, &mp_state); 510 if (ret) { 511 trace_kvm_failed_put_mpstate(strerror(errno)); 512 } 513 } 514 515 return ret; 516 } 517 518 static int kvm_loongarch_get_cpucfg(CPUState *cs) 519 { 520 int i, ret = 0; 521 uint64_t val; 522 CPULoongArchState *env = cpu_env(cs); 523 524 for (i = 0; i < 21; i++) { 525 ret = kvm_get_one_reg(cs, KVM_IOC_CPUCFG(i), &val); 526 if (ret < 0) { 527 trace_kvm_failed_get_cpucfg(strerror(errno)); 528 } 529 env->cpucfg[i] = (uint32_t)val; 530 } 531 return ret; 532 } 533 534 static int kvm_check_cpucfg2(CPUState *cs) 535 { 536 int ret; 537 uint64_t val; 538 struct kvm_device_attr attr = { 539 .group = KVM_LOONGARCH_VCPU_CPUCFG, 540 .attr = 2, 541 .addr = (uint64_t)&val, 542 }; 543 CPULoongArchState *env = cpu_env(cs); 544 545 ret = kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, &attr); 546 547 if (!ret) { 548 kvm_vcpu_ioctl(cs, KVM_GET_DEVICE_ATTR, &attr); 549 env->cpucfg[2] &= val; 550 551 if (FIELD_EX32(env->cpucfg[2], CPUCFG2, FP)) { 552 /* The FP minimal version is 1. */ 553 env->cpucfg[2] = FIELD_DP32(env->cpucfg[2], CPUCFG2, FP_VER, 1); 554 } 555 556 if (FIELD_EX32(env->cpucfg[2], CPUCFG2, LLFTP)) { 557 /* The LLFTP minimal version is 1. */ 558 env->cpucfg[2] = FIELD_DP32(env->cpucfg[2], CPUCFG2, LLFTP_VER, 1); 559 } 560 } 561 562 return ret; 563 } 564 565 static int kvm_loongarch_put_cpucfg(CPUState *cs) 566 { 567 int i, ret = 0; 568 CPULoongArchState *env = cpu_env(cs); 569 uint64_t val; 570 571 for (i = 0; i < 21; i++) { 572 if (i == 2) { 573 ret = kvm_check_cpucfg2(cs); 574 if (ret) { 575 return ret; 576 } 577 } 578 val = env->cpucfg[i]; 579 ret = kvm_set_one_reg(cs, KVM_IOC_CPUCFG(i), &val); 580 if (ret < 0) { 581 trace_kvm_failed_put_cpucfg(strerror(errno)); 582 } 583 } 584 return ret; 585 } 586 587 int kvm_arch_get_registers(CPUState *cs) 588 { 589 int ret; 590 591 ret = kvm_loongarch_get_regs_core(cs); 592 if (ret) { 593 return ret; 594 } 595 596 ret = kvm_loongarch_get_cpucfg(cs); 597 if (ret) { 598 return ret; 599 } 600 601 ret = kvm_loongarch_get_csr(cs); 602 if (ret) { 603 return ret; 604 } 605 606 ret = kvm_loongarch_get_regs_fp(cs); 607 if (ret) { 608 return ret; 609 } 610 611 ret = kvm_loongarch_get_mpstate(cs); 612 return ret; 613 } 614 615 int kvm_arch_put_registers(CPUState *cs, int level) 616 { 617 int ret; 618 619 ret = kvm_loongarch_put_regs_core(cs); 620 if (ret) { 621 return ret; 622 } 623 624 ret = kvm_loongarch_put_cpucfg(cs); 625 if (ret) { 626 return ret; 627 } 628 629 ret = kvm_loongarch_put_csr(cs, level); 630 if (ret) { 631 return ret; 632 } 633 634 ret = kvm_loongarch_put_regs_fp(cs); 635 if (ret) { 636 return ret; 637 } 638 639 ret = kvm_loongarch_put_mpstate(cs); 640 return ret; 641 } 642 643 static void kvm_loongarch_vm_stage_change(void *opaque, bool running, 644 RunState state) 645 { 646 int ret; 647 CPUState *cs = opaque; 648 LoongArchCPU *cpu = LOONGARCH_CPU(cs); 649 650 if (running) { 651 ret = kvm_set_one_reg(cs, KVM_REG_LOONGARCH_COUNTER, 652 &cpu->kvm_state_counter); 653 if (ret < 0) { 654 trace_kvm_failed_put_counter(strerror(errno)); 655 } 656 } else { 657 ret = kvm_get_one_reg(cs, KVM_REG_LOONGARCH_COUNTER, 658 &cpu->kvm_state_counter); 659 if (ret < 0) { 660 trace_kvm_failed_get_counter(strerror(errno)); 661 } 662 } 663 } 664 665 int kvm_arch_init_vcpu(CPUState *cs) 666 { 667 qemu_add_vm_change_state_handler(kvm_loongarch_vm_stage_change, cs); 668 return 0; 669 } 670 671 int kvm_arch_destroy_vcpu(CPUState *cs) 672 { 673 return 0; 674 } 675 676 unsigned long kvm_arch_vcpu_id(CPUState *cs) 677 { 678 return cs->cpu_index; 679 } 680 681 int kvm_arch_release_virq_post(int virq) 682 { 683 return 0; 684 } 685 686 int kvm_arch_msi_data_to_gsi(uint32_t data) 687 { 688 abort(); 689 } 690 691 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 692 uint64_t address, uint32_t data, PCIDevice *dev) 693 { 694 return 0; 695 } 696 697 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 698 int vector, PCIDevice *dev) 699 { 700 return 0; 701 } 702 703 void kvm_arch_init_irq_routing(KVMState *s) 704 { 705 } 706 707 int kvm_arch_get_default_type(MachineState *ms) 708 { 709 return 0; 710 } 711 712 int kvm_arch_init(MachineState *ms, KVMState *s) 713 { 714 cap_has_mp_state = kvm_check_extension(s, KVM_CAP_MP_STATE); 715 return 0; 716 } 717 718 int kvm_arch_irqchip_create(KVMState *s) 719 { 720 return 0; 721 } 722 723 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) 724 { 725 } 726 727 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) 728 { 729 return MEMTXATTRS_UNSPECIFIED; 730 } 731 732 int kvm_arch_process_async_events(CPUState *cs) 733 { 734 return cs->halted; 735 } 736 737 bool kvm_arch_stop_on_emulation_error(CPUState *cs) 738 { 739 return true; 740 } 741 742 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) 743 { 744 int ret = 0; 745 CPULoongArchState *env = cpu_env(cs); 746 MemTxAttrs attrs = {}; 747 748 attrs.requester_id = env_cpu(env)->cpu_index; 749 750 trace_kvm_arch_handle_exit(run->exit_reason); 751 switch (run->exit_reason) { 752 case KVM_EXIT_LOONGARCH_IOCSR: 753 address_space_rw(env->address_space_iocsr, 754 run->iocsr_io.phys_addr, 755 attrs, 756 run->iocsr_io.data, 757 run->iocsr_io.len, 758 run->iocsr_io.is_write); 759 break; 760 default: 761 ret = -1; 762 warn_report("KVM: unknown exit reason %d", run->exit_reason); 763 break; 764 } 765 return ret; 766 } 767 768 int kvm_loongarch_set_interrupt(LoongArchCPU *cpu, int irq, int level) 769 { 770 struct kvm_interrupt intr; 771 CPUState *cs = CPU(cpu); 772 773 if (level) { 774 intr.irq = irq; 775 } else { 776 intr.irq = -irq; 777 } 778 779 trace_kvm_set_intr(irq, level); 780 return kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr); 781 } 782 783 void kvm_arch_accel_class_init(ObjectClass *oc) 784 { 785 } 786