Re: [PATCH V16 5/6] hw/mips: Add Loongson-3 machine support

2020-11-03 Thread Huacai Chen
Hi, Philippe,

On Wed, Nov 4, 2020 at 4:23 AM Philippe Mathieu-Daudé  wrote:
>
> Hi Huacai,
>
> On 10/30/20 11:25 AM, Huacai Chen wrote:
> > Add Loongson-3 based machine support, it use liointc as the interrupt
> > controler and use GPEX as the pci controller. Currently it can work with
> > both TCG and KVM.
> >
> > As the machine model is not based on any exiting physical hardware, the
> > name of the machine is "loongson3-virt". It may be superseded in future
> > by a real machine model. If this happens, then a regular deprecation
> > procedure shall occur for "loongson3-virt" machine.
> >
> > We now already have a full functional Linux kernel (based on Linux-5.4.x
> > LTS, the kvm host side and guest side have both been upstream for Linux-
> > 5.9, but Linux-5.9 has not been released yet) here:
> >
> > https://github.com/chenhuacai/linux
> >
> > Of course the upstream kernel is also usable (though it is "unstable"
> > now):
> >
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> >
> > How to use QEMU/Loongson-3?
> > 1, Download kernel source from the above URL;
> > 2, Build a kernel with arch/mips/configs/loongson3_defconfig;
> > 3, Boot a Loongson-3A4000 host with this kernel (for KVM mode);
> > 4, Build QEMU-master with this patchset;
> > 5, modprobe kvm (only necessary for KVM mode);
> > 6, Use QEMU with TCG:
> >qemu-system-mips64el -M loongson3-virt,accel=tcg -cpu 
> > Loongson-3A1000 -kernel  -append ...
> >Use QEMU with KVM:
> >qemu-system-mips64el -M loongson3-virt,accel=kvm -cpu 
> > Loongson-3A4000 -kernel  -append ...
> >
> >The "-cpu" parameter is optional here and QEMU will use the correct type 
> > for TCG/KVM automatically.
> >
> > Signed-off-by: Huacai Chen 
> > Co-developed-by: Jiaxun Yang 
> > Signed-off-by: Jiaxun Yang 
> > ---
> >  default-configs/devices/mips64el-softmmu.mak |   1 +
> >  hw/mips/Kconfig  |  12 +
> >  hw/mips/loongson3_virt.c | 620 
> > +++
> >  hw/mips/meson.build  |   2 +-
> >  4 files changed, 634 insertions(+), 1 deletion(-)
> >  create mode 100644 hw/mips/loongson3_virt.c
> ...
> > +static inline void loongson3_virt_devices_init(MachineState *machine, 
> > DeviceState *pic)
> > +{
> > +int i;
> > +qemu_irq irq;
> > +PCIBus *pci_bus;
> > +DeviceState *dev;
> > +MemoryRegion *pio_alias;
> > +MemoryRegion *mmio_alias, *mmio_reg;
> > +MemoryRegion *ecam_alias, *ecam_reg;
> > +
> > +dev = qdev_new(TYPE_GPEX_HOST);
> > +sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), _fatal);
> > +pci_bus = PCI_HOST_BRIDGE(dev)->bus;
> > +
> > +ecam_alias = g_new0(MemoryRegion, 1);
> > +ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
> > +memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
> > + ecam_reg, 0, 
> > virt_memmap[VIRT_PCIE_ECAM].size);
> > +memory_region_add_subregion(get_system_memory(),
> > +virt_memmap[VIRT_PCIE_ECAM].base, 
> > ecam_alias);
> > +
> > +mmio_alias = g_new0(MemoryRegion, 1);
> > +mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
> > +memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
> > + mmio_reg, virt_memmap[VIRT_PCIE_MMIO].base,
> > + virt_memmap[VIRT_PCIE_MMIO].size);
> > +memory_region_add_subregion(get_system_memory(),
> > +virt_memmap[VIRT_PCIE_MMIO].base, 
> > mmio_alias);
> > +
> > +pio_alias = g_new0(MemoryRegion, 1);
> > +memory_region_init_alias(pio_alias, OBJECT(dev), "pcie-pio",
> > + get_system_io(), 0, 
> > virt_memmap[VIRT_PCIE_PIO].size);
> > +memory_region_add_subregion(get_system_memory(),
> > +virt_memmap[VIRT_PCIE_PIO].base, 
> > pio_alias);
> > +sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, 
> > virt_memmap[VIRT_PCIE_PIO].base);
> > +
> > +for (i = 0; i < GPEX_NUM_IRQS; i++) {
> > +irq = qdev_get_gpio_in(pic, PCIE_IRQ_BASE + i);
> > +sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
> > +gpex_set_irq_num(GPEX_HOST(dev), i, PCIE_IRQ_BASE + i);
> > +}
> > +
> > +pci_vga_init(pci_bus);
> > +
> > +if (defaults_enabled()) {
> > +pci_create_simple(pci_bus, -1, "pci-ohci");
> > +usb_create_simple(usb_bus_find(-1), "usb-kbd");
> > +usb_create_simple(usb_bus_find(-1), "usb-tablet");
> > +}
> > +
> > +for (i = 0; i < nb_nics; i++) {
> > +NICInfo *nd = _table[i];
> > +
> > +if (!nd->model) {
> > +nd->model = g_strdup("virtio");
> > +}
> > +
> > +pci_nic_init_nofail(nd, pci_bus, nd->model, NULL);
> > +}
> > +}
> > +
> > +static void mips_loongson3_virt_init(MachineState *machine)
> > +{
> > +int i;
> > +long bios_size;
> > +MIPSCPU *cpu;
> > +Clock 

Re: [PATCH V16 5/6] hw/mips: Add Loongson-3 machine support

2020-11-03 Thread Philippe Mathieu-Daudé
Hi Huacai,

On 10/30/20 11:25 AM, Huacai Chen wrote:
> Add Loongson-3 based machine support, it use liointc as the interrupt
> controler and use GPEX as the pci controller. Currently it can work with
> both TCG and KVM.
> 
> As the machine model is not based on any exiting physical hardware, the
> name of the machine is "loongson3-virt". It may be superseded in future
> by a real machine model. If this happens, then a regular deprecation
> procedure shall occur for "loongson3-virt" machine.
> 
> We now already have a full functional Linux kernel (based on Linux-5.4.x
> LTS, the kvm host side and guest side have both been upstream for Linux-
> 5.9, but Linux-5.9 has not been released yet) here:
> 
> https://github.com/chenhuacai/linux
> 
> Of course the upstream kernel is also usable (though it is "unstable"
> now):
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> 
> How to use QEMU/Loongson-3?
> 1, Download kernel source from the above URL;
> 2, Build a kernel with arch/mips/configs/loongson3_defconfig;
> 3, Boot a Loongson-3A4000 host with this kernel (for KVM mode);
> 4, Build QEMU-master with this patchset;
> 5, modprobe kvm (only necessary for KVM mode);
> 6, Use QEMU with TCG:
>qemu-system-mips64el -M loongson3-virt,accel=tcg -cpu Loongson-3A1000 
> -kernel  -append ...
>Use QEMU with KVM:
>qemu-system-mips64el -M loongson3-virt,accel=kvm -cpu Loongson-3A4000 
> -kernel  -append ...
> 
>The "-cpu" parameter is optional here and QEMU will use the correct type 
> for TCG/KVM automatically.
> 
> Signed-off-by: Huacai Chen 
> Co-developed-by: Jiaxun Yang 
> Signed-off-by: Jiaxun Yang 
> ---
>  default-configs/devices/mips64el-softmmu.mak |   1 +
>  hw/mips/Kconfig  |  12 +
>  hw/mips/loongson3_virt.c | 620 
> +++
>  hw/mips/meson.build  |   2 +-
>  4 files changed, 634 insertions(+), 1 deletion(-)
>  create mode 100644 hw/mips/loongson3_virt.c
...
> +static inline void loongson3_virt_devices_init(MachineState *machine, 
> DeviceState *pic)
> +{
> +int i;
> +qemu_irq irq;
> +PCIBus *pci_bus;
> +DeviceState *dev;
> +MemoryRegion *pio_alias;
> +MemoryRegion *mmio_alias, *mmio_reg;
> +MemoryRegion *ecam_alias, *ecam_reg;
> +
> +dev = qdev_new(TYPE_GPEX_HOST);
> +sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), _fatal);
> +pci_bus = PCI_HOST_BRIDGE(dev)->bus;
> +
> +ecam_alias = g_new0(MemoryRegion, 1);
> +ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
> +memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
> + ecam_reg, 0, virt_memmap[VIRT_PCIE_ECAM].size);
> +memory_region_add_subregion(get_system_memory(),
> +virt_memmap[VIRT_PCIE_ECAM].base, 
> ecam_alias);
> +
> +mmio_alias = g_new0(MemoryRegion, 1);
> +mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
> +memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
> + mmio_reg, virt_memmap[VIRT_PCIE_MMIO].base,
> + virt_memmap[VIRT_PCIE_MMIO].size);
> +memory_region_add_subregion(get_system_memory(),
> +virt_memmap[VIRT_PCIE_MMIO].base, 
> mmio_alias);
> +
> +pio_alias = g_new0(MemoryRegion, 1);
> +memory_region_init_alias(pio_alias, OBJECT(dev), "pcie-pio",
> + get_system_io(), 0, 
> virt_memmap[VIRT_PCIE_PIO].size);
> +memory_region_add_subregion(get_system_memory(),
> +virt_memmap[VIRT_PCIE_PIO].base, pio_alias);
> +sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, virt_memmap[VIRT_PCIE_PIO].base);
> +
> +for (i = 0; i < GPEX_NUM_IRQS; i++) {
> +irq = qdev_get_gpio_in(pic, PCIE_IRQ_BASE + i);
> +sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
> +gpex_set_irq_num(GPEX_HOST(dev), i, PCIE_IRQ_BASE + i);
> +}
> +
> +pci_vga_init(pci_bus);
> +
> +if (defaults_enabled()) {
> +pci_create_simple(pci_bus, -1, "pci-ohci");
> +usb_create_simple(usb_bus_find(-1), "usb-kbd");
> +usb_create_simple(usb_bus_find(-1), "usb-tablet");
> +}
> +
> +for (i = 0; i < nb_nics; i++) {
> +NICInfo *nd = _table[i];
> +
> +if (!nd->model) {
> +nd->model = g_strdup("virtio");
> +}
> +
> +pci_nic_init_nofail(nd, pci_bus, nd->model, NULL);
> +}
> +}
> +
> +static void mips_loongson3_virt_init(MachineState *machine)
> +{
> +int i;
> +long bios_size;
> +MIPSCPU *cpu;
> +Clock *cpuclk;
> +CPUMIPSState *env;
> +DeviceState *liointc;
> +char *filename;
> +const char *kernel_cmdline = machine->kernel_cmdline;
> +const char *kernel_filename = machine->kernel_filename;
> +const char *initrd_filename = machine->initrd_filename;
> +ram_addr_t ram_size = machine->ram_size;
> +

[PATCH V16 5/6] hw/mips: Add Loongson-3 machine support

2020-10-30 Thread Huacai Chen
Add Loongson-3 based machine support, it use liointc as the interrupt
controler and use GPEX as the pci controller. Currently it can work with
both TCG and KVM.

As the machine model is not based on any exiting physical hardware, the
name of the machine is "loongson3-virt". It may be superseded in future
by a real machine model. If this happens, then a regular deprecation
procedure shall occur for "loongson3-virt" machine.

We now already have a full functional Linux kernel (based on Linux-5.4.x
LTS, the kvm host side and guest side have both been upstream for Linux-
5.9, but Linux-5.9 has not been released yet) here:

https://github.com/chenhuacai/linux

Of course the upstream kernel is also usable (though it is "unstable"
now):

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

How to use QEMU/Loongson-3?
1, Download kernel source from the above URL;
2, Build a kernel with arch/mips/configs/loongson3_defconfig;
3, Boot a Loongson-3A4000 host with this kernel (for KVM mode);
4, Build QEMU-master with this patchset;
5, modprobe kvm (only necessary for KVM mode);
6, Use QEMU with TCG:
   qemu-system-mips64el -M loongson3-virt,accel=tcg -cpu Loongson-3A1000 
-kernel  -append ...
   Use QEMU with KVM:
   qemu-system-mips64el -M loongson3-virt,accel=kvm -cpu Loongson-3A4000 
-kernel  -append ...

   The "-cpu" parameter is optional here and QEMU will use the correct type for 
TCG/KVM automatically.

Signed-off-by: Huacai Chen 
Co-developed-by: Jiaxun Yang 
Signed-off-by: Jiaxun Yang 
---
 default-configs/devices/mips64el-softmmu.mak |   1 +
 hw/mips/Kconfig  |  12 +
 hw/mips/loongson3_virt.c | 620 +++
 hw/mips/meson.build  |   2 +-
 4 files changed, 634 insertions(+), 1 deletion(-)
 create mode 100644 hw/mips/loongson3_virt.c

diff --git a/default-configs/devices/mips64el-softmmu.mak 
b/default-configs/devices/mips64el-softmmu.mak
index 9f8a3ef..26c660a 100644
--- a/default-configs/devices/mips64el-softmmu.mak
+++ b/default-configs/devices/mips64el-softmmu.mak
@@ -3,6 +3,7 @@
 include mips-softmmu-common.mak
 CONFIG_IDE_VIA=y
 CONFIG_FULOONG=y
+CONFIG_LOONGSON3V=y
 CONFIG_ATI_VGA=y
 CONFIG_RTL8139_PCI=y
 CONFIG_JAZZ=y
diff --git a/hw/mips/Kconfig b/hw/mips/Kconfig
index 67d39c5..6562b34 100644
--- a/hw/mips/Kconfig
+++ b/hw/mips/Kconfig
@@ -45,6 +45,18 @@ config FULOONG
 bool
 select PCI_BONITO
 
+config LOONGSON3V
+bool
+select PCKBD
+select SERIAL
+select GOLDFISH_RTC
+select LOONGSON_LIOINTC
+select PCI_DEVICES
+select PCI_EXPRESS_GENERIC_BRIDGE
+select VIRTIO_VGA
+select QXL if SPICE
+select MSI_NONBROKEN
+
 config MIPS_CPS
 bool
 select PTIMER
diff --git a/hw/mips/loongson3_virt.c b/hw/mips/loongson3_virt.c
new file mode 100644
index 000..76580db
--- /dev/null
+++ b/hw/mips/loongson3_virt.c
@@ -0,0 +1,614 @@
+/*
+ * Generic Loongson-3 Platform support
+ *
+ * Copyright (c) 2018-2020 Huacai Chen (che...@lemote.com)
+ * Copyright (c) 2018-2020 Jiaxun Yang 
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+/*
+ * Generic virtualized PC Platform based on Loongson-3 CPU (MIPS64R2 with
+ * extensions, 800~2000MHz)
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "qemu/units.h"
+#include "qemu/cutils.h"
+#include "qapi/error.h"
+#include "cpu.h"
+#include "elf.h"
+#include "kvm_mips.h"
+#include "hw/boards.h"
+#include "hw/char/serial.h"
+#include "hw/mips/mips.h"
+#include "hw/mips/cpudevs.h"
+#include "hw/mips/fw_cfg.h"
+#include "hw/mips/loongson3_bootp.h"
+#include "hw/misc/unimp.h"
+#include "hw/intc/i8259.h"
+#include "hw/loader.h"
+#include "hw/isa/superio.h"
+#include "hw/pci/msi.h"
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_host.h"
+#include "hw/pci-host/gpex.h"
+#include "hw/rtc/mc146818rtc.h"
+#include "hw/usb.h"
+#include "net/net.h"
+#include "exec/address-spaces.h"
+#include "sysemu/kvm.h"
+#include "sysemu/qtest.h"
+#include "sysemu/reset.h"
+#include "sysemu/runstate.h"
+#include "qemu/log.h"
+#include "qemu/error-report.h"
+
+#define PM_CNTL_MODE  0x10
+
+#define LOONGSON_MAX_VCPUS  16
+
+/*
+ * Loongson-3's virtual machine BIOS can be obtained here:
+ * 1, https://github.com/loongson-community/firmware-nonfree
+ * 2,