[PATCH 1/4] Add loongarch cpu support

Xianglai Li posted 4 patches 11 months, 3 weeks ago
There is a newer version of this series
[PATCH 1/4] Add loongarch cpu support
Posted by Xianglai Li 11 months, 3 weeks ago
From: xianglai li <lixianglai@loongson.cn>

Add loongarch cpu support, Define new cpu type 'loongarch64'
and implement it's driver functions.

Signed-off-by: "Xianglai Li" <lixianglai@loongson.cn>
Signed-off-by: Xianglai Li <lixianglai@loongson.cn>
---
 src/cpu/cpu.c                |  2 +
 src/cpu/cpu_loongarch.c      | 80 ++++++++++++++++++++++++++++++++++++
 src/cpu/cpu_loongarch.h      | 25 +++++++++++
 src/cpu/meson.build          |  1 +
 src/qemu/qemu_capabilities.c |  2 +
 src/qemu/qemu_domain.c       |  4 ++
 src/util/virarch.c           |  2 +
 src/util/virarch.h           |  4 ++
 8 files changed, 120 insertions(+)
 create mode 100644 src/cpu/cpu_loongarch.c
 create mode 100644 src/cpu/cpu_loongarch.h

diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
index bc43aa4e93..1e7c879ca5 100644
--- a/src/cpu/cpu.c
+++ b/src/cpu/cpu.c
@@ -27,6 +27,7 @@
 #include "cpu_ppc64.h"
 #include "cpu_s390.h"
 #include "cpu_arm.h"
+#include "cpu_loongarch.h"
 #include "cpu_riscv64.h"
 #include "capabilities.h"
 
@@ -41,6 +42,7 @@ static struct cpuArchDriver *drivers[] = {
     &cpuDriverS390,
     &cpuDriverArm,
     &cpuDriverRiscv64,
+    &cpuDriverLoongArch,
 };
 
 
diff --git a/src/cpu/cpu_loongarch.c b/src/cpu/cpu_loongarch.c
new file mode 100644
index 0000000000..48f9fef5ea
--- /dev/null
+++ b/src/cpu/cpu_loongarch.c
@@ -0,0 +1,80 @@
+/*
+ * cpu_loongarch.c: CPU driver for 64-bit LOONGARCH CPUs
+ *
+ * Copyright (C) 2024 Loongson Technology.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "virlog.h"
+#include "viralloc.h"
+#include "cpu.h"
+#include "virstring.h"
+#include "cpu_map.h"
+#include "virbuffer.h"
+
+#define VIR_FROM_THIS VIR_FROM_CPU
+
+VIR_LOG_INIT("cpu.cpu_loongarch");
+
+static const virArch archs[] = { VIR_ARCH_LOONGARCH64 };
+
+static virCPUCompareResult
+virCPULoongArchCompare(virCPUDef *host G_GNUC_UNUSED,
+                       virCPUDef *cpu G_GNUC_UNUSED,
+                       bool failIncompatible G_GNUC_UNUSED)
+{
+    return VIR_CPU_COMPARE_IDENTICAL;
+}
+
+static int
+virCPULoongArchUpdate(virCPUDef *guest,
+                      const virCPUDef *host ATTRIBUTE_UNUSED,
+                      bool relative G_GNUC_UNUSED)
+{
+    /*
+     * - host-passthrough doesn't even get here
+     * - host-model is used for host CPU running in a compatibility mode and
+     *   it needs to remain unchanged
+     * - custom doesn't support any optional features, there's nothing to
+     *   update
+     */
+
+    if (guest->mode == VIR_CPU_MODE_CUSTOM)
+        guest->match = VIR_CPU_MATCH_EXACT;
+
+    return 0;
+}
+
+struct cpuArchDriver cpuDriverLoongArch = {
+    .name       = "LoongArch",
+    .arch       = archs,
+    .narch      = G_N_ELEMENTS(archs),
+    .compare    = virCPULoongArchCompare,
+    .decode     = NULL,
+    .encode     = NULL,
+    .dataFree   = NULL,
+    .baseline   = NULL,
+    .update     = virCPULoongArchUpdate,
+    .getModels  = NULL,
+};
diff --git a/src/cpu/cpu_loongarch.h b/src/cpu/cpu_loongarch.h
new file mode 100644
index 0000000000..4bc1c0cd8f
--- /dev/null
+++ b/src/cpu/cpu_loongarch.h
@@ -0,0 +1,25 @@
+/*
+ * cpu_loongarch.h: CPU driver for 64-bit LOONGARCH CPUs
+ *
+ * Copyright (C) 2024 Loongson Technology.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "cpu.h"
+
+extern struct cpuArchDriver cpuDriverLoongArch;
diff --git a/src/cpu/meson.build b/src/cpu/meson.build
index 55396903b9..141230e380 100644
--- a/src/cpu/meson.build
+++ b/src/cpu/meson.build
@@ -1,6 +1,7 @@
 cpu_sources = [
   'cpu.c',
   'cpu_arm.c',
+  'cpu_loongarch.c',
   'cpu_map.c',
   'cpu_ppc64.c',
   'cpu_riscv64.c',
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 83119e871a..f2339d6013 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2665,6 +2665,8 @@ static const char *preferredMachines[] =
     NULL, /* VIR_ARCH_ITANIUM (doesn't exist in QEMU any more) */
     "lm32-evr", /* VIR_ARCH_LM32 */
 
+    "virt", /* VIR_ARCH_LOONGARCH64 */
+
     "mcf5208evb", /* VIR_ARCH_M68K */
     "petalogix-s3adsp1800", /* VIR_ARCH_MICROBLAZE */
     "petalogix-s3adsp1800", /* VIR_ARCH_MICROBLAZEEL */
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 896aa8394f..0cea0b323a 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -4222,6 +4222,10 @@ qemuDomainDefAddDefaultDevices(virQEMUDriver *driver,
             addPCIRoot = true;
         break;
 
+    case VIR_ARCH_LOONGARCH64:
+        addPCIeRoot = true;
+        break;
+
     case VIR_ARCH_ARMV7B:
     case VIR_ARCH_CRIS:
     case VIR_ARCH_ITANIUM:
diff --git a/src/util/virarch.c b/src/util/virarch.c
index 01e520de73..74b7ec6f1b 100644
--- a/src/util/virarch.c
+++ b/src/util/virarch.c
@@ -51,6 +51,8 @@ static const struct virArchData {
     { "ia64",         64, VIR_ARCH_LITTLE_ENDIAN },
     { "lm32",         32, VIR_ARCH_BIG_ENDIAN },
 
+    { "loongarch64",  64, VIR_ARCH_LITTLE_ENDIAN },
+
     { "m68k",         32, VIR_ARCH_BIG_ENDIAN },
     { "microblaze",   32, VIR_ARCH_BIG_ENDIAN },
     { "microblazeel", 32, VIR_ARCH_LITTLE_ENDIAN},
diff --git a/src/util/virarch.h b/src/util/virarch.h
index 747f77c48e..c033e5c68d 100644
--- a/src/util/virarch.h
+++ b/src/util/virarch.h
@@ -36,6 +36,8 @@ typedef enum {
     VIR_ARCH_ITANIUM,      /* Itanium     64 LE https://en.wikipedia.org/wiki/Itanium */
     VIR_ARCH_LM32,         /* MilkyMist   32 BE https://en.wikipedia.org/wiki/Milkymist */
 
+    VIR_ARCH_LOONGARCH64,  /* LoongArch   64 LE */
+
     VIR_ARCH_M68K,         /* m68k        32 BE https://en.wikipedia.org/wiki/Motorola_68000_family */
     VIR_ARCH_MICROBLAZE,   /* Microblaze  32 BE https://en.wikipedia.org/wiki/MicroBlaze */
     VIR_ARCH_MICROBLAZEEL, /* Microblaze  32 LE https://en.wikipedia.org/wiki/MicroBlaze */
@@ -106,6 +108,8 @@ typedef enum {
 #define ARCH_IS_SH4(arch) ((arch) == VIR_ARCH_SH4 ||\
                            (arch) == VIR_ARCH_SH4EB)
 
+#define ARCH_IS_LOONGARCH(arch)  ((arch) == VIR_ARCH_LOONGARCH64)
+
 typedef enum {
     VIR_ARCH_LITTLE_ENDIAN,
     VIR_ARCH_BIG_ENDIAN,
-- 
2.39.1
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org