qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Bibo Mao <maobibo@loongson.cn>
To: lixianglai <lixianglai@loongson.cn>, Song Gao <gaosong@loongson.cn>
Cc: Jiaxun Yang <jiaxun.yang@flygoat.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH 3/3] target/loongarch: Add host CPU model in kvm mode
Date: Thu, 4 Dec 2025 10:58:34 +0800	[thread overview]
Message-ID: <edadf032-db5c-e844-8313-f970bbc35b9f@loongson.cn> (raw)
In-Reply-To: <5dc53523-af38-d9da-a089-d341b098fdeb@loongson.cn>



On 2025/12/4 上午9:57, lixianglai wrote:
> Hi Bibo Mao:
>> Host CPU model is basically the same with max CPU model, except Product
>> ID and CPU model name. With host CPU model, Product ID comes from
>> cpucfg0 and CPU model comes from /proc/cpuinfo.
>>
>> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
>> ---
>>   target/loongarch/cpu.c | 94 ++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 94 insertions(+)
>>
>> diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
>> index 8b8723a343..c7f52adeb9 100644
>> --- a/target/loongarch/cpu.c
>> +++ b/target/loongarch/cpu.c
>> @@ -429,6 +429,97 @@ static void loongarch_max_initfn(Object *obj)
>>       }
>>   }
>> +#if defined(CONFIG_KVM)
>> +static int read_cpuinfo(const char *field, char *value, int len)
>> +{
>> +    FILE *f;
>> +    int ret = -1;
>> +    int field_len = strlen(field);
>> +    char line[512];
>> +
>> +    f = fopen("/proc/cpuinfo", "r");
>> +    if (!f) {
>> +        return -1;
>> +    }
>> +
>> +    do {
>> +        if (!fgets(line, sizeof(line), f)) {
>> +            break;
>> +        }
>> +        if (!strncmp(line, field, field_len)) {
>> +            strncpy(value, line, len);
>> +            ret = 0;
>> +            break;
>> +        }
>> +    } while (*line);
>> +
>> +    fclose(f);
>> +
>> +    return ret;
>> +}
>> +
>> +static uint64_t get_host_cpu_model(void)
>> +{
>> +    char line[512];
>> +    char *ns;
>> +    static uint64_t cpuid;
>> +
>> +    if (cpuid) {
>> +        return cpuid;
>> +    }
>> +
>> +    if (read_cpuinfo("Model Name", line, sizeof(line))) {
>> +        return 0;
>> +    }
>> +
>> +    ns = strchr(line, ':');
>> +    if (!ns) {
>> +        return 0;
>> +    }
>> +
>> +    ns = strstr(ns, "Loongson-");
>> +    if (!ns) {
>> +        return 0;
>> +    }
>> +
>> +    ns += strlen("Loongson-");
>> +    memccpy((void *)&cpuid, ns, 0, 8);
>> +    return cpuid;
>> +}
>> +
>> +static uint32_t get_host_cpucfg(int number)
>> +{
>> +    unsigned int data = 0;
>> +
>> +#ifdef __loongarch__
>> +    asm volatile("cpucfg %[val], %[reg]"
>> +                 : [val] "=r" (data)
>> +                 : [reg] "r" (number)
>> +                 : "memory");
>> +#endif
>> +
>> +    return data;
>> +}
>> +
>> +static void loongarch_host_initfn(Object *obj)
>> +{
>> +    uint32_t data;
>> +    uint64_t cpuid;
>> +    LoongArchCPU *cpu = LOONGARCH_CPU(obj);
>> +
>> +    loongarch_max_initfn(obj);
>> +    data = get_host_cpucfg(0);
>> +    if (data) {
>> +        cpu->env.cpucfg[0] = data;
>> +    }
>> +
> 
> Does the feature bit on cpucfg[2] also need to be consistent with the 
> host feature bit?
> Otherwise, some features might be lost.
well, will check the detailed bits with CPUCFG2 in next version.

   bit10 LVZ will be disabled since nested KVM is not supported
   bit18-20/bit24 deeps on whether it is supported on KVM host and QEMU
   bit25-30 atomic instruction should be the same with host, it is 
irrelative with KVM.

Regards
Bibo Mao
> 
> Thanks!
> Xianglai.
> 
> 
>> +    cpuid = get_host_cpu_model();
>> +    if (cpuid) {
>> +        cpu->env.cpu_id = cpuid;
>> +    }
>> +}
>> +#endif
>> +
>>   static void loongarch_cpu_reset_hold(Object *obj, ResetType type)
>>   {
>>       uint8_t tlb_ps;
>> @@ -780,6 +871,9 @@ static const TypeInfo loongarch_cpu_type_infos[] = {
>>       DEFINE_LOONGARCH_CPU_TYPE(64, "la464", loongarch_la464_initfn),
>>       DEFINE_LOONGARCH_CPU_TYPE(32, "la132", loongarch_la132_initfn),
>>       DEFINE_LOONGARCH_CPU_TYPE(64, "max", loongarch_max_initfn),
>> +#if defined(CONFIG_KVM)
>> +    DEFINE_LOONGARCH_CPU_TYPE(64, "host", loongarch_host_initfn),
>> +#endif
>>   };
>>   DEFINE_TYPES(loongarch_cpu_type_infos)



      reply	other threads:[~2025-12-04  3:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-06  8:40 [PATCH 0/3] target/loongarch: Add host CPU type support Bibo Mao
2025-11-06  8:40 ` [PATCH 1/3] target/loongarch: Add detailed information with CPU Product ID Bibo Mao
2025-11-06  8:40 ` [PATCH 2/3] target/loongarch: Add generic CPU model information Bibo Mao
2025-11-06  8:40 ` [PATCH 3/3] target/loongarch: Add host CPU model in kvm mode Bibo Mao
2025-12-04  1:57   ` lixianglai
2025-12-04  2:58     ` Bibo Mao [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=edadf032-db5c-e844-8313-f970bbc35b9f@loongson.cn \
    --to=maobibo@loongson.cn \
    --cc=gaosong@loongson.cn \
    --cc=jiaxun.yang@flygoat.com \
    --cc=lixianglai@loongson.cn \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).