xref: /qemu/hw/loongarch/virt.c (revision e1092f765d9c0bf33762a03fe45e3d0de86c86a6)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * QEMU loongson 3a5000 develop board emulation
4  *
5  * Copyright (c) 2021 Loongson Technology Corporation Limited
6  */
7 #include "qemu/osdep.h"
8 #include "qemu/units.h"
9 #include "qemu/datadir.h"
10 #include "qapi/error.h"
11 #include "hw/boards.h"
12 #include "hw/char/serial-mm.h"
13 #include "system/kvm.h"
14 #include "system/tcg.h"
15 #include "system/system.h"
16 #include "system/qtest.h"
17 #include "system/runstate.h"
18 #include "system/reset.h"
19 #include "system/rtc.h"
20 #include "hw/loongarch/virt.h"
21 #include "exec/address-spaces.h"
22 #include "hw/irq.h"
23 #include "net/net.h"
24 #include "hw/loader.h"
25 #include "elf.h"
26 #include "hw/intc/loongarch_ipi.h"
27 #include "hw/intc/loongarch_extioi.h"
28 #include "hw/intc/loongarch_pch_pic.h"
29 #include "hw/intc/loongarch_pch_msi.h"
30 #include "hw/pci-host/ls7a.h"
31 #include "hw/pci-host/gpex.h"
32 #include "hw/misc/unimp.h"
33 #include "hw/loongarch/fw_cfg.h"
34 #include "target/loongarch/cpu.h"
35 #include "hw/firmware/smbios.h"
36 #include "qapi/qapi-visit-common.h"
37 #include "hw/acpi/generic_event_device.h"
38 #include "hw/mem/nvdimm.h"
39 #include "hw/platform-bus.h"
40 #include "hw/display/ramfb.h"
41 #include "hw/uefi/var-service-api.h"
42 #include "hw/mem/pc-dimm.h"
43 #include "system/tpm.h"
44 #include "system/block-backend.h"
45 #include "hw/block/flash.h"
46 #include "hw/virtio/virtio-iommu.h"
47 #include "qemu/error-report.h"
48 
49 static void virt_get_veiointc(Object *obj, Visitor *v, const char *name,
50                               void *opaque, Error **errp)
51 {
52     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
53     OnOffAuto veiointc = lvms->veiointc;
54 
55     visit_type_OnOffAuto(v, name, &veiointc, errp);
56 }
57 
58 static void virt_set_veiointc(Object *obj, Visitor *v, const char *name,
59                               void *opaque, Error **errp)
60 {
61     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
62 
63     visit_type_OnOffAuto(v, name, &lvms->veiointc, errp);
64 }
65 
66 static PFlashCFI01 *virt_flash_create1(LoongArchVirtMachineState *lvms,
67                                        const char *name,
68                                        const char *alias_prop_name)
69 {
70     DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
71 
72     qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE);
73     qdev_prop_set_uint8(dev, "width", 4);
74     qdev_prop_set_uint8(dev, "device-width", 2);
75     qdev_prop_set_bit(dev, "big-endian", false);
76     qdev_prop_set_uint16(dev, "id0", 0x89);
77     qdev_prop_set_uint16(dev, "id1", 0x18);
78     qdev_prop_set_uint16(dev, "id2", 0x00);
79     qdev_prop_set_uint16(dev, "id3", 0x00);
80     qdev_prop_set_string(dev, "name", name);
81     object_property_add_child(OBJECT(lvms), name, OBJECT(dev));
82     object_property_add_alias(OBJECT(lvms), alias_prop_name,
83                               OBJECT(dev), "drive");
84     return PFLASH_CFI01(dev);
85 }
86 
87 static void virt_flash_create(LoongArchVirtMachineState *lvms)
88 {
89     lvms->flash[0] = virt_flash_create1(lvms, "virt.flash0", "pflash0");
90     lvms->flash[1] = virt_flash_create1(lvms, "virt.flash1", "pflash1");
91 }
92 
93 static void virt_flash_map1(PFlashCFI01 *flash,
94                             hwaddr base, hwaddr size,
95                             MemoryRegion *sysmem)
96 {
97     DeviceState *dev = DEVICE(flash);
98     BlockBackend *blk;
99     hwaddr real_size = size;
100 
101     blk = pflash_cfi01_get_blk(flash);
102     if (blk) {
103         real_size = blk_getlength(blk);
104         assert(real_size && real_size <= size);
105     }
106 
107     assert(QEMU_IS_ALIGNED(real_size, VIRT_FLASH_SECTOR_SIZE));
108     assert(real_size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
109 
110     qdev_prop_set_uint32(dev, "num-blocks", real_size / VIRT_FLASH_SECTOR_SIZE);
111     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
112     memory_region_add_subregion(sysmem, base,
113                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
114 }
115 
116 static void virt_flash_map(LoongArchVirtMachineState *lvms,
117                            MemoryRegion *sysmem)
118 {
119     PFlashCFI01 *flash0 = lvms->flash[0];
120     PFlashCFI01 *flash1 = lvms->flash[1];
121 
122     virt_flash_map1(flash0, VIRT_FLASH0_BASE, VIRT_FLASH0_SIZE, sysmem);
123     virt_flash_map1(flash1, VIRT_FLASH1_BASE, VIRT_FLASH1_SIZE, sysmem);
124 }
125 
126 static void virt_build_smbios(LoongArchVirtMachineState *lvms)
127 {
128     MachineState *ms = MACHINE(lvms);
129     MachineClass *mc = MACHINE_GET_CLASS(lvms);
130     uint8_t *smbios_tables, *smbios_anchor;
131     size_t smbios_tables_len, smbios_anchor_len;
132     const char *product = "QEMU Virtual Machine";
133 
134     if (!lvms->fw_cfg) {
135         return;
136     }
137 
138     smbios_set_defaults("QEMU", product, mc->name);
139 
140     smbios_get_tables(ms, SMBIOS_ENTRY_POINT_TYPE_64,
141                       NULL, 0,
142                       &smbios_tables, &smbios_tables_len,
143                       &smbios_anchor, &smbios_anchor_len, &error_fatal);
144 
145     if (smbios_anchor) {
146         fw_cfg_add_file(lvms->fw_cfg, "etc/smbios/smbios-tables",
147                         smbios_tables, smbios_tables_len);
148         fw_cfg_add_file(lvms->fw_cfg, "etc/smbios/smbios-anchor",
149                         smbios_anchor, smbios_anchor_len);
150     }
151 }
152 
153 static void virt_done(Notifier *notifier, void *data)
154 {
155     LoongArchVirtMachineState *lvms = container_of(notifier,
156                                       LoongArchVirtMachineState, machine_done);
157     virt_build_smbios(lvms);
158     virt_acpi_setup(lvms);
159     virt_fdt_setup(lvms);
160 }
161 
162 static void virt_powerdown_req(Notifier *notifier, void *opaque)
163 {
164     LoongArchVirtMachineState *s;
165 
166     s = container_of(notifier, LoongArchVirtMachineState, powerdown_notifier);
167     acpi_send_event(s->acpi_ged, ACPI_POWER_DOWN_STATUS);
168 }
169 
170 static void memmap_add_entry(uint64_t address, uint64_t length, uint32_t type)
171 {
172     /* Ensure there are no duplicate entries. */
173     for (unsigned i = 0; i < memmap_entries; i++) {
174         assert(memmap_table[i].address != address);
175     }
176 
177     memmap_table = g_renew(struct memmap_entry, memmap_table,
178                            memmap_entries + 1);
179     memmap_table[memmap_entries].address = cpu_to_le64(address);
180     memmap_table[memmap_entries].length = cpu_to_le64(length);
181     memmap_table[memmap_entries].type = cpu_to_le32(type);
182     memmap_table[memmap_entries].reserved = 0;
183     memmap_entries++;
184 }
185 
186 static DeviceState *create_acpi_ged(DeviceState *pch_pic,
187                                     LoongArchVirtMachineState *lvms)
188 {
189     DeviceState *dev;
190     MachineState *ms = MACHINE(lvms);
191     MachineClass *mc = MACHINE_GET_CLASS(lvms);
192     uint32_t event = ACPI_GED_PWR_DOWN_EVT;
193 
194     if (ms->ram_slots) {
195         event |= ACPI_GED_MEM_HOTPLUG_EVT;
196     }
197 
198     if (mc->has_hotpluggable_cpus) {
199         event |= ACPI_GED_CPU_HOTPLUG_EVT;
200     }
201 
202     dev = qdev_new(TYPE_ACPI_GED);
203     qdev_prop_set_uint32(dev, "ged-event", event);
204     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
205 
206     /* ged event */
207     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, VIRT_GED_EVT_ADDR);
208     /* memory hotplug */
209     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, VIRT_GED_MEM_ADDR);
210     /* ged regs used for reset and power down */
211     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, VIRT_GED_REG_ADDR);
212 
213     if (mc->has_hotpluggable_cpus) {
214         sysbus_mmio_map(SYS_BUS_DEVICE(dev), 3, VIRT_GED_CPUHP_ADDR);
215     }
216 
217     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
218                        qdev_get_gpio_in(pch_pic, VIRT_SCI_IRQ - VIRT_GSI_BASE));
219     return dev;
220 }
221 
222 static DeviceState *create_platform_bus(DeviceState *pch_pic)
223 {
224     DeviceState *dev;
225     SysBusDevice *sysbus;
226     int i, irq;
227     MemoryRegion *sysmem = get_system_memory();
228 
229     dev = qdev_new(TYPE_PLATFORM_BUS_DEVICE);
230     dev->id = g_strdup(TYPE_PLATFORM_BUS_DEVICE);
231     qdev_prop_set_uint32(dev, "num_irqs", VIRT_PLATFORM_BUS_NUM_IRQS);
232     qdev_prop_set_uint32(dev, "mmio_size", VIRT_PLATFORM_BUS_SIZE);
233     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
234 
235     sysbus = SYS_BUS_DEVICE(dev);
236     for (i = 0; i < VIRT_PLATFORM_BUS_NUM_IRQS; i++) {
237         irq = VIRT_PLATFORM_BUS_IRQ - VIRT_GSI_BASE + i;
238         sysbus_connect_irq(sysbus, i, qdev_get_gpio_in(pch_pic, irq));
239     }
240 
241     memory_region_add_subregion(sysmem,
242                                 VIRT_PLATFORM_BUS_BASEADDRESS,
243                                 sysbus_mmio_get_region(sysbus, 0));
244     return dev;
245 }
246 
247 static void virt_devices_init(DeviceState *pch_pic,
248                                    LoongArchVirtMachineState *lvms)
249 {
250     MachineClass *mc = MACHINE_GET_CLASS(lvms);
251     DeviceState *gpex_dev;
252     SysBusDevice *d;
253     PCIBus *pci_bus;
254     MemoryRegion *ecam_alias, *ecam_reg, *pio_alias, *pio_reg;
255     MemoryRegion *mmio_alias, *mmio_reg;
256     int i;
257 
258     gpex_dev = qdev_new(TYPE_GPEX_HOST);
259     d = SYS_BUS_DEVICE(gpex_dev);
260     sysbus_realize_and_unref(d, &error_fatal);
261     pci_bus = PCI_HOST_BRIDGE(gpex_dev)->bus;
262     lvms->pci_bus = pci_bus;
263 
264     /* Map only part size_ecam bytes of ECAM space */
265     ecam_alias = g_new0(MemoryRegion, 1);
266     ecam_reg = sysbus_mmio_get_region(d, 0);
267     memory_region_init_alias(ecam_alias, OBJECT(gpex_dev), "pcie-ecam",
268                              ecam_reg, 0, VIRT_PCI_CFG_SIZE);
269     memory_region_add_subregion(get_system_memory(), VIRT_PCI_CFG_BASE,
270                                 ecam_alias);
271 
272     /* Map PCI mem space */
273     mmio_alias = g_new0(MemoryRegion, 1);
274     mmio_reg = sysbus_mmio_get_region(d, 1);
275     memory_region_init_alias(mmio_alias, OBJECT(gpex_dev), "pcie-mmio",
276                              mmio_reg, VIRT_PCI_MEM_BASE, VIRT_PCI_MEM_SIZE);
277     memory_region_add_subregion(get_system_memory(), VIRT_PCI_MEM_BASE,
278                                 mmio_alias);
279 
280     /* Map PCI IO port space. */
281     pio_alias = g_new0(MemoryRegion, 1);
282     pio_reg = sysbus_mmio_get_region(d, 2);
283     memory_region_init_alias(pio_alias, OBJECT(gpex_dev), "pcie-io", pio_reg,
284                              VIRT_PCI_IO_OFFSET, VIRT_PCI_IO_SIZE);
285     memory_region_add_subregion(get_system_memory(), VIRT_PCI_IO_BASE,
286                                 pio_alias);
287 
288     for (i = 0; i < PCI_NUM_PINS; i++) {
289         sysbus_connect_irq(d, i,
290                            qdev_get_gpio_in(pch_pic, 16 + i));
291         gpex_set_irq_num(GPEX_HOST(gpex_dev), i, 16 + i);
292     }
293 
294     /*
295      * Create uart fdt node in reverse order so that they appear
296      * in the finished device tree lowest address first
297      */
298     for (i = VIRT_UART_COUNT; i-- > 0;) {
299         hwaddr base = VIRT_UART_BASE + i * VIRT_UART_SIZE;
300         int irq = VIRT_UART_IRQ + i - VIRT_GSI_BASE;
301         serial_mm_init(get_system_memory(), base, 0,
302                        qdev_get_gpio_in(pch_pic, irq),
303                        115200, serial_hd(i), DEVICE_LITTLE_ENDIAN);
304     }
305 
306     /* Network init */
307     pci_init_nic_devices(pci_bus, mc->default_nic);
308 
309     /*
310      * There are some invalid guest memory access.
311      * Create some unimplemented devices to emulate this.
312      */
313     create_unimplemented_device("pci-dma-cfg", 0x1001041c, 0x4);
314     sysbus_create_simple("ls7a_rtc", VIRT_RTC_REG_BASE,
315                          qdev_get_gpio_in(pch_pic,
316                          VIRT_RTC_IRQ - VIRT_GSI_BASE));
317 
318     /* acpi ged */
319     lvms->acpi_ged = create_acpi_ged(pch_pic, lvms);
320     /* platform bus */
321     lvms->platform_bus_dev = create_platform_bus(pch_pic);
322 }
323 
324 static void virt_cpu_irq_init(LoongArchVirtMachineState *lvms)
325 {
326     int num;
327     MachineState *ms = MACHINE(lvms);
328     MachineClass *mc = MACHINE_GET_CLASS(ms);
329     const CPUArchIdList *possible_cpus;
330     CPUState *cs;
331     Error *err = NULL;
332 
333     /* cpu nodes */
334     possible_cpus = mc->possible_cpu_arch_ids(ms);
335     for (num = 0; num < possible_cpus->len; num++) {
336         cs = possible_cpus->cpus[num].cpu;
337         if (cs == NULL) {
338             continue;
339         }
340 
341         hotplug_handler_plug(HOTPLUG_HANDLER(lvms->ipi), DEVICE(cs), &err);
342         hotplug_handler_plug(HOTPLUG_HANDLER(lvms->extioi), DEVICE(cs), &err);
343     }
344 }
345 
346 static void virt_irq_init(LoongArchVirtMachineState *lvms)
347 {
348     DeviceState *pch_pic, *pch_msi;
349     DeviceState *ipi, *extioi;
350     SysBusDevice *d;
351     int i, start, num;
352 
353     /*
354      * Extended IRQ model.
355      *                                 |
356      * +-----------+     +-------------|--------+     +-----------+
357      * | IPI/Timer | --> | CPUINTC(0-3)|(4-255) | <-- | IPI/Timer |
358      * +-----------+     +-------------|--------+     +-----------+
359      *                         ^       |
360      *                         |
361      *                    +---------+
362      *                    | EIOINTC |
363      *                    +---------+
364      *                     ^       ^
365      *                     |       |
366      *              +---------+ +---------+
367      *              | PCH-PIC | | PCH-MSI |
368      *              +---------+ +---------+
369      *                ^      ^          ^
370      *                |      |          |
371      *         +--------+ +---------+ +---------+
372      *         | UARTs  | | Devices | | Devices |
373      *         +--------+ +---------+ +---------+
374      *
375      * Virt extended IRQ model.
376      *
377      *   +-----+    +---------------+     +-------+
378      *   | IPI |--> | CPUINTC(0-255)| <-- | Timer |
379      *   +-----+    +---------------+     +-------+
380      *                     ^
381      *                     |
382      *               +-----------+
383      *               | V-EIOINTC |
384      *               +-----------+
385      *                ^         ^
386      *                |         |
387      *         +---------+ +---------+
388      *         | PCH-PIC | | PCH-MSI |
389      *         +---------+ +---------+
390      *           ^      ^          ^
391      *           |      |          |
392      *    +--------+ +---------+ +---------+
393      *    | UARTs  | | Devices | | Devices |
394      *    +--------+ +---------+ +---------+
395      */
396 
397     /* Create IPI device */
398     ipi = qdev_new(TYPE_LOONGARCH_IPI);
399     lvms->ipi = ipi;
400     sysbus_realize_and_unref(SYS_BUS_DEVICE(ipi), &error_fatal);
401 
402     /* IPI iocsr memory region */
403     memory_region_add_subregion(&lvms->system_iocsr, SMP_IPI_MAILBOX,
404                    sysbus_mmio_get_region(SYS_BUS_DEVICE(ipi), 0));
405     memory_region_add_subregion(&lvms->system_iocsr, MAIL_SEND_ADDR,
406                    sysbus_mmio_get_region(SYS_BUS_DEVICE(ipi), 1));
407 
408     /* Create EXTIOI device */
409     extioi = qdev_new(TYPE_LOONGARCH_EXTIOI);
410     lvms->extioi = extioi;
411     if (virt_is_veiointc_enabled(lvms)) {
412         qdev_prop_set_bit(extioi, "has-virtualization-extension", true);
413     }
414     sysbus_realize_and_unref(SYS_BUS_DEVICE(extioi), &error_fatal);
415     memory_region_add_subregion(&lvms->system_iocsr, APIC_BASE,
416                     sysbus_mmio_get_region(SYS_BUS_DEVICE(extioi), 0));
417     if (virt_is_veiointc_enabled(lvms)) {
418         memory_region_add_subregion(&lvms->system_iocsr, EXTIOI_VIRT_BASE,
419                     sysbus_mmio_get_region(SYS_BUS_DEVICE(extioi), 1));
420     }
421 
422     virt_cpu_irq_init(lvms);
423     pch_pic = qdev_new(TYPE_LOONGARCH_PIC);
424     num = VIRT_PCH_PIC_IRQ_NUM;
425     qdev_prop_set_uint32(pch_pic, "pch_pic_irq_num", num);
426     d = SYS_BUS_DEVICE(pch_pic);
427     sysbus_realize_and_unref(d, &error_fatal);
428     memory_region_add_subregion(get_system_memory(), VIRT_IOAPIC_REG_BASE,
429                             sysbus_mmio_get_region(d, 0));
430     memory_region_add_subregion(get_system_memory(),
431                             VIRT_IOAPIC_REG_BASE + PCH_PIC_ROUTE_ENTRY_OFFSET,
432                             sysbus_mmio_get_region(d, 1));
433     memory_region_add_subregion(get_system_memory(),
434                             VIRT_IOAPIC_REG_BASE + PCH_PIC_INT_STATUS_LO,
435                             sysbus_mmio_get_region(d, 2));
436 
437     /* Connect pch_pic irqs to extioi */
438     for (i = 0; i < num; i++) {
439         qdev_connect_gpio_out(DEVICE(d), i, qdev_get_gpio_in(extioi, i));
440     }
441 
442     pch_msi = qdev_new(TYPE_LOONGARCH_PCH_MSI);
443     start   =  num;
444     num = EXTIOI_IRQS - start;
445     qdev_prop_set_uint32(pch_msi, "msi_irq_base", start);
446     qdev_prop_set_uint32(pch_msi, "msi_irq_num", num);
447     d = SYS_BUS_DEVICE(pch_msi);
448     sysbus_realize_and_unref(d, &error_fatal);
449     sysbus_mmio_map(d, 0, VIRT_PCH_MSI_ADDR_LOW);
450     for (i = 0; i < num; i++) {
451         /* Connect pch_msi irqs to extioi */
452         qdev_connect_gpio_out(DEVICE(d), i,
453                               qdev_get_gpio_in(extioi, i + start));
454     }
455 
456     virt_devices_init(pch_pic, lvms);
457 }
458 
459 static void virt_firmware_init(LoongArchVirtMachineState *lvms)
460 {
461     char *filename = MACHINE(lvms)->firmware;
462     char *bios_name = NULL;
463     int bios_size, i;
464     BlockBackend *pflash_blk0;
465     MemoryRegion *mr;
466 
467     lvms->bios_loaded = false;
468 
469     /* Map legacy -drive if=pflash to machine properties */
470     for (i = 0; i < ARRAY_SIZE(lvms->flash); i++) {
471         pflash_cfi01_legacy_drive(lvms->flash[i],
472                                   drive_get(IF_PFLASH, 0, i));
473     }
474 
475     virt_flash_map(lvms, get_system_memory());
476 
477     pflash_blk0 = pflash_cfi01_get_blk(lvms->flash[0]);
478 
479     if (pflash_blk0) {
480         if (filename) {
481             error_report("cannot use both '-bios' and '-drive if=pflash'"
482                          "options at once");
483             exit(1);
484         }
485         lvms->bios_loaded = true;
486         return;
487     }
488 
489     if (filename) {
490         bios_name = qemu_find_file(QEMU_FILE_TYPE_BIOS, filename);
491         if (!bios_name) {
492             error_report("Could not find ROM image '%s'", filename);
493             exit(1);
494         }
495 
496         mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(lvms->flash[0]), 0);
497         bios_size = load_image_mr(bios_name, mr);
498         if (bios_size < 0) {
499             error_report("Could not load ROM image '%s'", bios_name);
500             exit(1);
501         }
502         g_free(bios_name);
503         lvms->bios_loaded = true;
504     }
505 }
506 
507 static MemTxResult virt_iocsr_misc_write(void *opaque, hwaddr addr,
508                                          uint64_t val, unsigned size,
509                                          MemTxAttrs attrs)
510 {
511     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(opaque);
512     uint64_t features;
513 
514     switch (addr) {
515     case MISC_FUNC_REG:
516         if (!virt_is_veiointc_enabled(lvms)) {
517             return MEMTX_OK;
518         }
519 
520         features = address_space_ldl(&lvms->as_iocsr,
521                                      EXTIOI_VIRT_BASE + EXTIOI_VIRT_CONFIG,
522                                      attrs, NULL);
523         if (val & BIT_ULL(IOCSRM_EXTIOI_EN)) {
524             features |= BIT(EXTIOI_ENABLE);
525         }
526         if (val & BIT_ULL(IOCSRM_EXTIOI_INT_ENCODE)) {
527             features |= BIT(EXTIOI_ENABLE_INT_ENCODE);
528         }
529 
530         address_space_stl(&lvms->as_iocsr,
531                           EXTIOI_VIRT_BASE + EXTIOI_VIRT_CONFIG,
532                           features, attrs, NULL);
533         break;
534     default:
535         g_assert_not_reached();
536     }
537 
538     return MEMTX_OK;
539 }
540 
541 static MemTxResult virt_iocsr_misc_read(void *opaque, hwaddr addr,
542                                         uint64_t *data,
543                                         unsigned size, MemTxAttrs attrs)
544 {
545     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(opaque);
546     uint64_t ret = 0;
547     int features;
548 
549     switch (addr) {
550     case VERSION_REG:
551         ret = 0x11ULL;
552         break;
553     case FEATURE_REG:
554         ret = BIT(IOCSRF_MSI) | BIT(IOCSRF_EXTIOI) | BIT(IOCSRF_CSRIPI);
555         if (kvm_enabled()) {
556             ret |= BIT(IOCSRF_VM);
557         }
558         break;
559     case VENDOR_REG:
560         ret = 0x6e6f73676e6f6f4cULL; /* "Loongson" */
561         break;
562     case CPUNAME_REG:
563         ret = 0x303030354133ULL;     /* "3A5000" */
564         break;
565     case MISC_FUNC_REG:
566         if (!virt_is_veiointc_enabled(lvms)) {
567             ret |= BIT_ULL(IOCSRM_EXTIOI_EN);
568             break;
569         }
570 
571         features = address_space_ldl(&lvms->as_iocsr,
572                                      EXTIOI_VIRT_BASE + EXTIOI_VIRT_CONFIG,
573                                      attrs, NULL);
574         if (features & BIT(EXTIOI_ENABLE)) {
575             ret |= BIT_ULL(IOCSRM_EXTIOI_EN);
576         }
577         if (features & BIT(EXTIOI_ENABLE_INT_ENCODE)) {
578             ret |= BIT_ULL(IOCSRM_EXTIOI_INT_ENCODE);
579         }
580         break;
581     default:
582         g_assert_not_reached();
583     }
584 
585     *data = ret;
586     return MEMTX_OK;
587 }
588 
589 static const MemoryRegionOps virt_iocsr_misc_ops = {
590     .read_with_attrs  = virt_iocsr_misc_read,
591     .write_with_attrs = virt_iocsr_misc_write,
592     .endianness = DEVICE_LITTLE_ENDIAN,
593     .valid = {
594         .min_access_size = 4,
595         .max_access_size = 8,
596     },
597     .impl = {
598         .min_access_size = 8,
599         .max_access_size = 8,
600     },
601 };
602 
603 static void fw_cfg_add_memory(MachineState *ms)
604 {
605     hwaddr base, size, ram_size, gap;
606     int nb_numa_nodes, nodes;
607     NodeInfo *numa_info;
608 
609     ram_size = ms->ram_size;
610     base = VIRT_LOWMEM_BASE;
611     gap = VIRT_LOWMEM_SIZE;
612     nodes = nb_numa_nodes = ms->numa_state->num_nodes;
613     numa_info = ms->numa_state->nodes;
614     if (!nodes) {
615         nodes = 1;
616     }
617 
618     /* add fw_cfg memory map of node0 */
619     if (nb_numa_nodes) {
620         size = numa_info[0].node_mem;
621     } else {
622         size = ram_size;
623     }
624 
625     if (size >= gap) {
626         memmap_add_entry(base, gap, 1);
627         size -= gap;
628         base = VIRT_HIGHMEM_BASE;
629     }
630 
631     if (size) {
632         memmap_add_entry(base, size, 1);
633         base += size;
634     }
635 
636     if (nodes < 2) {
637         return;
638     }
639 
640     /* add fw_cfg memory map of other nodes */
641     if (numa_info[0].node_mem < gap && ram_size > gap) {
642         /*
643          * memory map for the maining nodes splited into two part
644          * lowram:  [base, +(gap - numa_info[0].node_mem))
645          * highram: [VIRT_HIGHMEM_BASE, +(ram_size - gap))
646          */
647         memmap_add_entry(base, gap - numa_info[0].node_mem, 1);
648         size = ram_size - gap;
649         base = VIRT_HIGHMEM_BASE;
650     } else {
651         size = ram_size - numa_info[0].node_mem;
652     }
653 
654     if (size) {
655         memmap_add_entry(base, size, 1);
656     }
657 }
658 
659 static void virt_init(MachineState *machine)
660 {
661     const char *cpu_model = machine->cpu_type;
662     MemoryRegion *address_space_mem = get_system_memory();
663     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(machine);
664     int i;
665     hwaddr base, size, ram_size = machine->ram_size;
666     MachineClass *mc = MACHINE_GET_CLASS(machine);
667     Object *cpuobj;
668 
669     if (!cpu_model) {
670         cpu_model = LOONGARCH_CPU_TYPE_NAME("la464");
671     }
672 
673     /* Create IOCSR space */
674     memory_region_init_io(&lvms->system_iocsr, OBJECT(machine), NULL,
675                           machine, "iocsr", UINT64_MAX);
676     address_space_init(&lvms->as_iocsr, &lvms->system_iocsr, "IOCSR");
677     memory_region_init_io(&lvms->iocsr_mem, OBJECT(machine),
678                           &virt_iocsr_misc_ops,
679                           machine, "iocsr_misc", 0x428);
680     memory_region_add_subregion(&lvms->system_iocsr, 0, &lvms->iocsr_mem);
681 
682     /* Init CPUs */
683     mc->possible_cpu_arch_ids(machine);
684     for (i = 0; i < machine->smp.cpus; i++) {
685         cpuobj = object_new(machine->cpu_type);
686         if (cpuobj == NULL) {
687             error_report("Fail to create object with type %s ",
688                          machine->cpu_type);
689             exit(EXIT_FAILURE);
690         }
691         qdev_realize_and_unref(DEVICE(cpuobj), NULL, &error_fatal);
692     }
693     fw_cfg_add_memory(machine);
694 
695     /* Node0 memory */
696     size = ram_size;
697     base = VIRT_LOWMEM_BASE;
698     if (size > VIRT_LOWMEM_SIZE) {
699         size = VIRT_LOWMEM_SIZE;
700     }
701 
702     memory_region_init_alias(&lvms->lowmem, NULL, "loongarch.lowram",
703                               machine->ram, base, size);
704     memory_region_add_subregion(address_space_mem, base, &lvms->lowmem);
705     base += size;
706     if (ram_size - size) {
707         base = VIRT_HIGHMEM_BASE;
708         memory_region_init_alias(&lvms->highmem, NULL, "loongarch.highram",
709                 machine->ram, VIRT_LOWMEM_BASE + size, ram_size - size);
710         memory_region_add_subregion(address_space_mem, base, &lvms->highmem);
711         base += ram_size - size;
712     }
713 
714     /* initialize device memory address space */
715     if (machine->ram_size < machine->maxram_size) {
716         ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
717 
718         if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
719             error_report("unsupported amount of memory slots: %"PRIu64,
720                          machine->ram_slots);
721             exit(EXIT_FAILURE);
722         }
723 
724         if (QEMU_ALIGN_UP(machine->maxram_size,
725                           TARGET_PAGE_SIZE) != machine->maxram_size) {
726             error_report("maximum memory size must by aligned to multiple of "
727                          "%d bytes", TARGET_PAGE_SIZE);
728             exit(EXIT_FAILURE);
729         }
730         machine_memory_devices_init(machine, base, device_mem_size);
731     }
732 
733     /* load the BIOS image. */
734     virt_firmware_init(lvms);
735 
736     /* fw_cfg init */
737     lvms->fw_cfg = virt_fw_cfg_init(ram_size, machine);
738     rom_set_fw(lvms->fw_cfg);
739     if (lvms->fw_cfg != NULL) {
740         fw_cfg_add_file(lvms->fw_cfg, "etc/memmap",
741                         memmap_table,
742                         sizeof(struct memmap_entry) * (memmap_entries));
743     }
744 
745     /* Initialize the IO interrupt subsystem */
746     virt_irq_init(lvms);
747     lvms->machine_done.notify = virt_done;
748     qemu_add_machine_init_done_notifier(&lvms->machine_done);
749      /* connect powerdown request */
750     lvms->powerdown_notifier.notify = virt_powerdown_req;
751     qemu_register_powerdown_notifier(&lvms->powerdown_notifier);
752 
753     lvms->bootinfo.ram_size = ram_size;
754     loongarch_load_kernel(machine, &lvms->bootinfo);
755 }
756 
757 static void virt_get_acpi(Object *obj, Visitor *v, const char *name,
758                           void *opaque, Error **errp)
759 {
760     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
761     OnOffAuto acpi = lvms->acpi;
762 
763     visit_type_OnOffAuto(v, name, &acpi, errp);
764 }
765 
766 static void virt_set_acpi(Object *obj, Visitor *v, const char *name,
767                                void *opaque, Error **errp)
768 {
769     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
770 
771     visit_type_OnOffAuto(v, name, &lvms->acpi, errp);
772 }
773 
774 static void virt_initfn(Object *obj)
775 {
776     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
777 
778     if (tcg_enabled()) {
779         lvms->veiointc = ON_OFF_AUTO_OFF;
780     }
781     lvms->acpi = ON_OFF_AUTO_AUTO;
782     lvms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6);
783     lvms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8);
784     virt_flash_create(lvms);
785 }
786 
787 static void virt_get_topo_from_index(MachineState *ms,
788                                      LoongArchCPUTopo *topo, int index)
789 {
790     topo->socket_id = index / (ms->smp.cores * ms->smp.threads);
791     topo->core_id = index / ms->smp.threads % ms->smp.cores;
792     topo->thread_id = index % ms->smp.threads;
793 }
794 
795 static unsigned int topo_align_up(unsigned int count)
796 {
797     g_assert(count >= 1);
798     count -= 1;
799     return BIT(count ? 32 - clz32(count) : 0);
800 }
801 
802 /*
803  * LoongArch Reference Manual Vol1, Chapter 7.4.12 CPU Identity
804  *  For CPU architecture, bit0 .. bit8 is valid for CPU id, max cpuid is 512
805  *  However for IPI/Eiointc interrupt controller, max supported cpu id for
806  *  irq routingis 256
807  *
808  *  Here max cpu id is 256 for virt machine
809  */
810 static int virt_get_arch_id_from_topo(MachineState *ms, LoongArchCPUTopo *topo)
811 {
812     int arch_id, threads, cores, sockets;
813 
814     threads = topo_align_up(ms->smp.threads);
815     cores = topo_align_up(ms->smp.cores);
816     sockets = topo_align_up(ms->smp.sockets);
817     if ((threads * cores * sockets) > 256) {
818         error_report("Exceeding max cpuid 256 with sockets[%d] cores[%d]"
819                      " threads[%d]", ms->smp.sockets, ms->smp.cores,
820                      ms->smp.threads);
821         exit(1);
822     }
823 
824     arch_id = topo->thread_id + topo->core_id * threads;
825     arch_id += topo->socket_id * threads * cores;
826     return arch_id;
827 }
828 
829 /* Find cpu slot in machine->possible_cpus by arch_id */
830 static CPUArchId *virt_find_cpu_slot(MachineState *ms, int arch_id)
831 {
832     int n;
833     for (n = 0; n < ms->possible_cpus->len; n++) {
834         if (ms->possible_cpus->cpus[n].arch_id == arch_id) {
835             return &ms->possible_cpus->cpus[n];
836         }
837     }
838 
839     return NULL;
840 }
841 
842 /* Find cpu slot for cold-plut CPU object where cpu is NULL */
843 static CPUArchId *virt_find_empty_cpu_slot(MachineState *ms)
844 {
845     int n;
846     for (n = 0; n < ms->possible_cpus->len; n++) {
847         if (ms->possible_cpus->cpus[n].cpu == NULL) {
848             return &ms->possible_cpus->cpus[n];
849         }
850     }
851 
852     return NULL;
853 }
854 
855 static void virt_cpu_pre_plug(HotplugHandler *hotplug_dev,
856                               DeviceState *dev, Error **errp)
857 {
858     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
859     MachineState *ms = MACHINE(OBJECT(hotplug_dev));
860     LoongArchCPU *cpu = LOONGARCH_CPU(dev);
861     CPUState *cs = CPU(dev);
862     CPUArchId *cpu_slot;
863     Error *err = NULL;
864     LoongArchCPUTopo topo;
865     int arch_id;
866 
867     if (lvms->acpi_ged) {
868         if ((cpu->thread_id < 0) || (cpu->thread_id >= ms->smp.threads)) {
869             error_setg(&err,
870                        "Invalid thread-id %u specified, must be in range 1:%u",
871                        cpu->thread_id, ms->smp.threads - 1);
872             goto out;
873         }
874 
875         if ((cpu->core_id < 0) || (cpu->core_id >= ms->smp.cores)) {
876             error_setg(&err,
877                        "Invalid core-id %u specified, must be in range 1:%u",
878                        cpu->core_id, ms->smp.cores - 1);
879             goto out;
880         }
881 
882         if ((cpu->socket_id < 0) || (cpu->socket_id >= ms->smp.sockets)) {
883             error_setg(&err,
884                        "Invalid socket-id %u specified, must be in range 1:%u",
885                        cpu->socket_id, ms->smp.sockets - 1);
886             goto out;
887         }
888 
889         topo.socket_id = cpu->socket_id;
890         topo.core_id = cpu->core_id;
891         topo.thread_id = cpu->thread_id;
892         arch_id =  virt_get_arch_id_from_topo(ms, &topo);
893         cpu_slot = virt_find_cpu_slot(ms, arch_id);
894         if (CPU(cpu_slot->cpu)) {
895             error_setg(&err,
896                        "cpu(id%d=%d:%d:%d) with arch-id %" PRIu64 " exists",
897                        cs->cpu_index, cpu->socket_id, cpu->core_id,
898                        cpu->thread_id, cpu_slot->arch_id);
899             goto out;
900         }
901     } else {
902         /* For cold-add cpu, find empty cpu slot */
903         cpu_slot = virt_find_empty_cpu_slot(ms);
904         topo.socket_id = cpu_slot->props.socket_id;
905         topo.core_id = cpu_slot->props.core_id;
906         topo.thread_id = cpu_slot->props.thread_id;
907         object_property_set_int(OBJECT(dev), "socket-id", topo.socket_id, NULL);
908         object_property_set_int(OBJECT(dev), "core-id", topo.core_id, NULL);
909         object_property_set_int(OBJECT(dev), "thread-id", topo.thread_id, NULL);
910     }
911 
912     cpu->env.address_space_iocsr = &lvms->as_iocsr;
913     cpu->phy_id = cpu_slot->arch_id;
914     cs->cpu_index = cpu_slot - ms->possible_cpus->cpus;
915     numa_cpu_pre_plug(cpu_slot, dev, &err);
916 out:
917     if (err) {
918         error_propagate(errp, err);
919     }
920 }
921 
922 static void virt_cpu_unplug_request(HotplugHandler *hotplug_dev,
923                                     DeviceState *dev, Error **errp)
924 {
925     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
926     Error *err = NULL;
927     LoongArchCPU *cpu = LOONGARCH_CPU(dev);
928     CPUState *cs = CPU(dev);
929 
930     if (cs->cpu_index == 0) {
931         error_setg(&err, "hot-unplug of boot cpu(id%d=%d:%d:%d) not supported",
932                    cs->cpu_index, cpu->socket_id,
933                    cpu->core_id, cpu->thread_id);
934         error_propagate(errp, err);
935         return;
936     }
937 
938     hotplug_handler_unplug_request(HOTPLUG_HANDLER(lvms->acpi_ged), dev, &err);
939     if (err) {
940         error_propagate(errp, err);
941     }
942 }
943 
944 static void virt_cpu_unplug(HotplugHandler *hotplug_dev,
945                             DeviceState *dev, Error **errp)
946 {
947     CPUArchId *cpu_slot;
948     Error *err = NULL;
949     LoongArchCPU *cpu = LOONGARCH_CPU(dev);
950     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
951 
952     /* Notify ipi and extioi irqchip to remove interrupt routing to CPU */
953     hotplug_handler_unplug(HOTPLUG_HANDLER(lvms->ipi), dev, &err);
954     if (err) {
955         error_propagate(errp, err);
956         return;
957     }
958 
959     hotplug_handler_unplug(HOTPLUG_HANDLER(lvms->extioi), dev, &err);
960     if (err) {
961         error_propagate(errp, err);
962         return;
963     }
964 
965     /* Notify acpi ged CPU removed */
966     hotplug_handler_unplug(HOTPLUG_HANDLER(lvms->acpi_ged), dev, &err);
967     if (err) {
968         error_propagate(errp, err);
969         return;
970     }
971 
972     cpu_slot = virt_find_cpu_slot(MACHINE(lvms), cpu->phy_id);
973     cpu_slot->cpu = NULL;
974     return;
975 }
976 
977 static void virt_cpu_plug(HotplugHandler *hotplug_dev,
978                           DeviceState *dev, Error **errp)
979 {
980     CPUArchId *cpu_slot;
981     LoongArchCPU *cpu = LOONGARCH_CPU(dev);
982     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
983     Error *err = NULL;
984 
985     cpu_slot = virt_find_cpu_slot(MACHINE(lvms), cpu->phy_id);
986     cpu_slot->cpu = CPU(dev);
987     if (lvms->ipi) {
988         hotplug_handler_plug(HOTPLUG_HANDLER(lvms->ipi), dev, &err);
989         if (err) {
990             error_propagate(errp, err);
991             return;
992         }
993     }
994 
995     if (lvms->extioi) {
996         hotplug_handler_plug(HOTPLUG_HANDLER(lvms->extioi), dev, &err);
997         if (err) {
998             error_propagate(errp, err);
999             return;
1000         }
1001     }
1002 
1003     if (lvms->acpi_ged) {
1004         hotplug_handler_plug(HOTPLUG_HANDLER(lvms->acpi_ged), dev, &err);
1005         if (err) {
1006             error_propagate(errp, err);
1007         }
1008     }
1009 
1010     return;
1011 }
1012 
1013 static bool memhp_type_supported(DeviceState *dev)
1014 {
1015     /* we only support pc dimm now */
1016     return object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) &&
1017            !object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1018 }
1019 
1020 static void virt_mem_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1021                                  Error **errp)
1022 {
1023     pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), errp);
1024 }
1025 
1026 static void virt_device_pre_plug(HotplugHandler *hotplug_dev,
1027                                             DeviceState *dev, Error **errp)
1028 {
1029     if (memhp_type_supported(dev)) {
1030         virt_mem_pre_plug(hotplug_dev, dev, errp);
1031     } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
1032         virt_cpu_pre_plug(hotplug_dev, dev, errp);
1033     }
1034 }
1035 
1036 static void virt_mem_unplug_request(HotplugHandler *hotplug_dev,
1037                                      DeviceState *dev, Error **errp)
1038 {
1039     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
1040 
1041     /* the acpi ged is always exist */
1042     hotplug_handler_unplug_request(HOTPLUG_HANDLER(lvms->acpi_ged), dev,
1043                                    errp);
1044 }
1045 
1046 static void virt_device_unplug_request(HotplugHandler *hotplug_dev,
1047                                           DeviceState *dev, Error **errp)
1048 {
1049     if (memhp_type_supported(dev)) {
1050         virt_mem_unplug_request(hotplug_dev, dev, errp);
1051     } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
1052         virt_cpu_unplug_request(hotplug_dev, dev, errp);
1053     }
1054 }
1055 
1056 static void virt_mem_unplug(HotplugHandler *hotplug_dev,
1057                              DeviceState *dev, Error **errp)
1058 {
1059     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
1060 
1061     hotplug_handler_unplug(HOTPLUG_HANDLER(lvms->acpi_ged), dev, errp);
1062     pc_dimm_unplug(PC_DIMM(dev), MACHINE(lvms));
1063     qdev_unrealize(dev);
1064 }
1065 
1066 static void virt_device_unplug(HotplugHandler *hotplug_dev,
1067                                           DeviceState *dev, Error **errp)
1068 {
1069     if (memhp_type_supported(dev)) {
1070         virt_mem_unplug(hotplug_dev, dev, errp);
1071     } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
1072         virt_cpu_unplug(hotplug_dev, dev, errp);
1073     }
1074 }
1075 
1076 static void virt_mem_plug(HotplugHandler *hotplug_dev,
1077                              DeviceState *dev, Error **errp)
1078 {
1079     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
1080 
1081     pc_dimm_plug(PC_DIMM(dev), MACHINE(lvms));
1082     hotplug_handler_plug(HOTPLUG_HANDLER(lvms->acpi_ged),
1083                          dev, &error_abort);
1084 }
1085 
1086 static void virt_device_plug_cb(HotplugHandler *hotplug_dev,
1087                                         DeviceState *dev, Error **errp)
1088 {
1089     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
1090     MachineClass *mc = MACHINE_GET_CLASS(lvms);
1091     PlatformBusDevice *pbus;
1092 
1093     if (device_is_dynamic_sysbus(mc, dev)) {
1094         if (lvms->platform_bus_dev) {
1095             pbus = PLATFORM_BUS_DEVICE(lvms->platform_bus_dev);
1096             platform_bus_link_device(pbus, SYS_BUS_DEVICE(dev));
1097         }
1098     } else if (memhp_type_supported(dev)) {
1099         virt_mem_plug(hotplug_dev, dev, errp);
1100     } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
1101         virt_cpu_plug(hotplug_dev, dev, errp);
1102     }
1103 }
1104 
1105 static HotplugHandler *virt_get_hotplug_handler(MachineState *machine,
1106                                                 DeviceState *dev)
1107 {
1108     MachineClass *mc = MACHINE_GET_CLASS(machine);
1109 
1110     if (device_is_dynamic_sysbus(mc, dev) ||
1111         object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU) ||
1112         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) ||
1113         memhp_type_supported(dev)) {
1114         return HOTPLUG_HANDLER(machine);
1115     }
1116     return NULL;
1117 }
1118 
1119 static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
1120 {
1121     int n, arch_id;
1122     unsigned int max_cpus = ms->smp.max_cpus;
1123     LoongArchCPUTopo topo;
1124 
1125     if (ms->possible_cpus) {
1126         assert(ms->possible_cpus->len == max_cpus);
1127         return ms->possible_cpus;
1128     }
1129 
1130     ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
1131                                   sizeof(CPUArchId) * max_cpus);
1132     ms->possible_cpus->len = max_cpus;
1133     for (n = 0; n < ms->possible_cpus->len; n++) {
1134         virt_get_topo_from_index(ms, &topo, n);
1135         arch_id = virt_get_arch_id_from_topo(ms, &topo);
1136         ms->possible_cpus->cpus[n].type = ms->cpu_type;
1137         ms->possible_cpus->cpus[n].arch_id = arch_id;
1138         ms->possible_cpus->cpus[n].vcpus_count = 1;
1139         ms->possible_cpus->cpus[n].props.has_socket_id = true;
1140         ms->possible_cpus->cpus[n].props.socket_id = topo.socket_id;
1141         ms->possible_cpus->cpus[n].props.has_core_id = true;
1142         ms->possible_cpus->cpus[n].props.core_id = topo.core_id;
1143         ms->possible_cpus->cpus[n].props.has_thread_id = true;
1144         ms->possible_cpus->cpus[n].props.thread_id = topo.thread_id;
1145     }
1146     return ms->possible_cpus;
1147 }
1148 
1149 static CpuInstanceProperties virt_cpu_index_to_props(MachineState *ms,
1150                                                      unsigned cpu_index)
1151 {
1152     MachineClass *mc = MACHINE_GET_CLASS(ms);
1153     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1154 
1155     assert(cpu_index < possible_cpus->len);
1156     return possible_cpus->cpus[cpu_index].props;
1157 }
1158 
1159 static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
1160 {
1161     int64_t socket_id;
1162 
1163     if (ms->numa_state->num_nodes) {
1164         socket_id = ms->possible_cpus->cpus[idx].props.socket_id;
1165         return socket_id % ms->numa_state->num_nodes;
1166     } else {
1167         return 0;
1168     }
1169 }
1170 
1171 static void virt_class_init(ObjectClass *oc, void *data)
1172 {
1173     MachineClass *mc = MACHINE_CLASS(oc);
1174     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1175 
1176     mc->init = virt_init;
1177     mc->default_cpu_type = LOONGARCH_CPU_TYPE_NAME("la464");
1178     mc->default_ram_id = "loongarch.ram";
1179     mc->desc = "QEMU LoongArch Virtual Machine";
1180     mc->max_cpus = LOONGARCH_MAX_CPUS;
1181     mc->is_default = 1;
1182     mc->default_kernel_irqchip_split = false;
1183     mc->block_default_type = IF_VIRTIO;
1184     mc->default_boot_order = "c";
1185     mc->no_cdrom = 1;
1186     mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
1187     mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
1188     mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
1189     mc->numa_mem_supported = true;
1190     mc->auto_enable_numa_with_memhp = true;
1191     mc->auto_enable_numa_with_memdev = true;
1192     mc->has_hotpluggable_cpus = true;
1193     mc->get_hotplug_handler = virt_get_hotplug_handler;
1194     mc->default_nic = "virtio-net-pci";
1195     hc->plug = virt_device_plug_cb;
1196     hc->pre_plug = virt_device_pre_plug;
1197     hc->unplug_request = virt_device_unplug_request;
1198     hc->unplug = virt_device_unplug;
1199 
1200     object_class_property_add(oc, "acpi", "OnOffAuto",
1201         virt_get_acpi, virt_set_acpi,
1202         NULL, NULL);
1203     object_class_property_set_description(oc, "acpi",
1204         "Enable ACPI");
1205     object_class_property_add(oc, "v-eiointc", "OnOffAuto",
1206         virt_get_veiointc, virt_set_veiointc,
1207         NULL, NULL);
1208     object_class_property_set_description(oc, "v-eiointc",
1209                             "Enable Virt Extend I/O Interrupt Controller.");
1210     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
1211     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_UEFI_VARS_SYSBUS);
1212 #ifdef CONFIG_TPM
1213     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
1214 #endif
1215 }
1216 
1217 static const TypeInfo virt_machine_types[] = {
1218     {
1219         .name           = TYPE_LOONGARCH_VIRT_MACHINE,
1220         .parent         = TYPE_MACHINE,
1221         .instance_size  = sizeof(LoongArchVirtMachineState),
1222         .class_init     = virt_class_init,
1223         .instance_init  = virt_initfn,
1224         .interfaces = (InterfaceInfo[]) {
1225          { TYPE_HOTPLUG_HANDLER },
1226          { }
1227         },
1228     }
1229 };
1230 
1231 DEFINE_TYPES(virt_machine_types)
1232