Amit Machhiwal <[email protected]> writes:

> Currently, when booting a compatibility-mode KVM guest (L1) on a PowerNV
> hypervisor (L0), the guest runs with the expected processor
> compatibility level. However, when booting a nested KVM guest (L2)
> inside the L1, QEMU derives the CPU model from the raw host PVR and
> attempts to run the nested guest at that level, instead of honoring the
> compatibility mode of the L1.
>
> Extend host CPU compatibility capability reporting to support nested
> virtualization on PowerNV systems (PAPR nested API v1).
>
> For nested API v2 (PowerVM), compatibility capabilities are served from
> the cached nested_capabilities value (populated at module init via
> kvmhv_nested_init() using the H_GUEST_GET_CAPABILITIES hcall). This
> information is not available on PowerNV systems.
>
> For nested API v1, derive the compatibility capabilities from the L1
> guest by reading the "cpu-version" property from the device tree, which
> reflects the effective (logical) processor compatibility level. Map this
> value to the corresponding compatibility capability bitmap using
> KVM-specific constants.
>
> The mapping is cumulative: a system running at a given compatibility
> level is assumed to also support older generations down the supported
> chain. Note that unlike KVM on PowerVM (nested API v2), KVM on PowerNV
> currently does not strictly enforce older generation compatibility modes
> for nested guests - the reported capabilities reflect what the host CPU
> can present, not what the hypervisor independently validates.
>
> Introduce a helper kvmppc_map_compat_capabilities() to translate CPU
> version values into KVM_PPC_COMPAT_CAP bits using a fallthrough switch,
> and integrate it into kvmppc_get_compat_caps(). The implementation
> applies masking to ensure only supported processor modes are exposed.
>
> This allows userspace to query host CPU compatibility modes on both
> KVM on PowerVM and on PowerNV platforms via the KVM_PPC_GET_COMPAT_CAPS
> ioctl.
>
> Suggested-by: Vaibhav Jain <[email protected]>
> Tested-by: Gautam Menghani <[email protected]>
> Reviewed-by: Gautam Menghani <[email protected]>
> Tested-by: Anushree Mathur <[email protected]>
> Signed-off-by: Amit Machhiwal <[email protected]>
> ---
>  arch/powerpc/kvm/book3s_hv.c | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
>
> diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
> index 152cd08a5b38..4d0307f55e06 100644
> --- a/arch/powerpc/kvm/book3s_hv.c
> +++ b/arch/powerpc/kvm/book3s_hv.c
> @@ -6523,20 +6523,56 @@ static bool kvmppc_hash_v3_possible(void)
>       return true;
>  }
>  
> +static int kvmppc_map_compat_capabilities(u32 cpu_version,
> +                                       unsigned long *capabilities)
> +{
> +     switch (cpu_version) {
> +     case PVR_ARCH_31_P11:
> +             *capabilities |= KVM_PPC_COMPAT_CAP_POWER11;
> +             fallthrough;
> +     case PVR_ARCH_31:
> +             *capabilities |= KVM_PPC_COMPAT_CAP_POWER10;
> +             fallthrough;
> +     case PVR_ARCH_300:
> +             *capabilities |= KVM_PPC_COMPAT_CAP_POWER9;
> +             break;
> +     default:
> +             return -EINVAL;
> +     }
> +
> +     return 0;
> +}
>  
>  static int kvmppc_get_compat_caps(struct kvm_ppc_compat_caps *host_caps)
>  {
> +     struct device_node *np;
>       unsigned long capabilities = 0;
>       long rc = -EINVAL;
> +     u32 cpu_version = 0;
>  
>       if (kvmhv_on_pseries()) {
>               if (kvmhv_is_nestedv2()) {
>                       WARN_ON_ONCE(!nested_capabilities);
>                       capabilities = nested_capabilities;
>                       rc = 0;
> +             } else {
> +                     for_each_node_by_type(np, "cpu") {
> +                             if (!of_property_read_u32(np, "cpu-version",
> +                                                       &cpu_version)) {
> +                                     of_node_put(np);
> +                                     break;
> +                             }
> +                     }

What happens when we don't have "cpu-version" DT property?
Check this commit
5a61ef74f269f2 ("powerpc/64s: Support new device tree binding for discovering 
CPU features")

And also why do we need to parse the DT properties again?
Shouldn't we check something like this?

        if (cpu_has_feature(CPU_FTR_P11_PVR))
                capabilities |= KVM_PPC_COMPAT_CAP_POWER11;
        if (cpu_has_feature(CPU_FTR_ARCH_31))
                capabilities |= KVM_PPC_COMPAT_CAP_POWER10;
        if (cpu_has_feature(CPU_FTR_ARCH_300))
                capabilities |= KVM_PPC_COMPAT_CAP_POWER9;
        

-ritesh

> +                     if (!cpu_version)
> +                             return -EINVAL;
> +                     rc = kvmppc_map_compat_capabilities(cpu_version,
> +                                                         &capabilities);
>               }
>       }
>  
> +     if (rc < 0)
> +             return rc;
> +
>       host_caps->compat_capabilities = capabilities & KVM_PPC_COMPAT_BITMASK;
>  
>       return rc;
> -- 
> 2.50.1 (Apple Git-155)

Reply via email to