1*40b44316SDaniel Henrique Barboza /* 2*40b44316SDaniel Henrique Barboza * QTest testcase for RISC-V IOMMU 3*40b44316SDaniel Henrique Barboza * 4*40b44316SDaniel Henrique Barboza * Copyright (c) 2024 Ventana Micro Systems Inc. 5*40b44316SDaniel Henrique Barboza * 6*40b44316SDaniel Henrique Barboza * This work is licensed under the terms of the GNU GPL, version 2 or (at your 7*40b44316SDaniel Henrique Barboza * option) any later version. See the COPYING file in the top-level directory. 8*40b44316SDaniel Henrique Barboza * 9*40b44316SDaniel Henrique Barboza */ 10*40b44316SDaniel Henrique Barboza 11*40b44316SDaniel Henrique Barboza #include "qemu/osdep.h" 12*40b44316SDaniel Henrique Barboza #include "libqtest-single.h" 13*40b44316SDaniel Henrique Barboza #include "qemu/module.h" 14*40b44316SDaniel Henrique Barboza #include "libqos/qgraph.h" 15*40b44316SDaniel Henrique Barboza #include "libqos/riscv-iommu.h" 16*40b44316SDaniel Henrique Barboza #include "hw/pci/pci_regs.h" 17*40b44316SDaniel Henrique Barboza 18*40b44316SDaniel Henrique Barboza static uint32_t riscv_iommu_read_reg32(QRISCVIOMMU *r_iommu, int reg_offset) 19*40b44316SDaniel Henrique Barboza { 20*40b44316SDaniel Henrique Barboza return qpci_io_readl(&r_iommu->dev, r_iommu->reg_bar, reg_offset); 21*40b44316SDaniel Henrique Barboza } 22*40b44316SDaniel Henrique Barboza 23*40b44316SDaniel Henrique Barboza static uint64_t riscv_iommu_read_reg64(QRISCVIOMMU *r_iommu, int reg_offset) 24*40b44316SDaniel Henrique Barboza { 25*40b44316SDaniel Henrique Barboza return qpci_io_readq(&r_iommu->dev, r_iommu->reg_bar, reg_offset); 26*40b44316SDaniel Henrique Barboza } 27*40b44316SDaniel Henrique Barboza 28*40b44316SDaniel Henrique Barboza static void test_pci_config(void *obj, void *data, QGuestAllocator *t_alloc) 29*40b44316SDaniel Henrique Barboza { 30*40b44316SDaniel Henrique Barboza QRISCVIOMMU *r_iommu = obj; 31*40b44316SDaniel Henrique Barboza QPCIDevice *dev = &r_iommu->dev; 32*40b44316SDaniel Henrique Barboza uint16_t vendorid, deviceid, classid; 33*40b44316SDaniel Henrique Barboza 34*40b44316SDaniel Henrique Barboza vendorid = qpci_config_readw(dev, PCI_VENDOR_ID); 35*40b44316SDaniel Henrique Barboza deviceid = qpci_config_readw(dev, PCI_DEVICE_ID); 36*40b44316SDaniel Henrique Barboza classid = qpci_config_readw(dev, PCI_CLASS_DEVICE); 37*40b44316SDaniel Henrique Barboza 38*40b44316SDaniel Henrique Barboza g_assert_cmpuint(vendorid, ==, RISCV_IOMMU_PCI_VENDOR_ID); 39*40b44316SDaniel Henrique Barboza g_assert_cmpuint(deviceid, ==, RISCV_IOMMU_PCI_DEVICE_ID); 40*40b44316SDaniel Henrique Barboza g_assert_cmpuint(classid, ==, RISCV_IOMMU_PCI_DEVICE_CLASS); 41*40b44316SDaniel Henrique Barboza } 42*40b44316SDaniel Henrique Barboza 43*40b44316SDaniel Henrique Barboza static void test_reg_reset(void *obj, void *data, QGuestAllocator *t_alloc) 44*40b44316SDaniel Henrique Barboza { 45*40b44316SDaniel Henrique Barboza QRISCVIOMMU *r_iommu = obj; 46*40b44316SDaniel Henrique Barboza uint64_t cap; 47*40b44316SDaniel Henrique Barboza uint32_t reg; 48*40b44316SDaniel Henrique Barboza 49*40b44316SDaniel Henrique Barboza cap = riscv_iommu_read_reg64(r_iommu, RISCV_IOMMU_REG_CAP); 50*40b44316SDaniel Henrique Barboza g_assert_cmpuint(cap & RISCV_IOMMU_CAP_VERSION, ==, 0x10); 51*40b44316SDaniel Henrique Barboza 52*40b44316SDaniel Henrique Barboza reg = riscv_iommu_read_reg32(r_iommu, RISCV_IOMMU_REG_CQCSR); 53*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_CQCSR_CQEN, ==, 0); 54*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_CQCSR_CIE, ==, 0); 55*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_CQCSR_CQON, ==, 0); 56*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_CQCSR_BUSY, ==, 0); 57*40b44316SDaniel Henrique Barboza 58*40b44316SDaniel Henrique Barboza reg = riscv_iommu_read_reg32(r_iommu, RISCV_IOMMU_REG_FQCSR); 59*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_FQCSR_FQEN, ==, 0); 60*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_FQCSR_FIE, ==, 0); 61*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_FQCSR_FQON, ==, 0); 62*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_FQCSR_BUSY, ==, 0); 63*40b44316SDaniel Henrique Barboza 64*40b44316SDaniel Henrique Barboza reg = riscv_iommu_read_reg32(r_iommu, RISCV_IOMMU_REG_PQCSR); 65*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_PQCSR_PQEN, ==, 0); 66*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_PQCSR_PIE, ==, 0); 67*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_PQCSR_PQON, ==, 0); 68*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_PQCSR_BUSY, ==, 0); 69*40b44316SDaniel Henrique Barboza 70*40b44316SDaniel Henrique Barboza reg = riscv_iommu_read_reg32(r_iommu, RISCV_IOMMU_REG_DDTP); 71*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_DDTP_BUSY, ==, 0); 72*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg & RISCV_IOMMU_DDTP_MODE, ==, 73*40b44316SDaniel Henrique Barboza RISCV_IOMMU_DDTP_MODE_OFF); 74*40b44316SDaniel Henrique Barboza 75*40b44316SDaniel Henrique Barboza reg = riscv_iommu_read_reg32(r_iommu, RISCV_IOMMU_REG_IPSR); 76*40b44316SDaniel Henrique Barboza g_assert_cmpuint(reg, ==, 0); 77*40b44316SDaniel Henrique Barboza } 78*40b44316SDaniel Henrique Barboza 79*40b44316SDaniel Henrique Barboza static void register_riscv_iommu_test(void) 80*40b44316SDaniel Henrique Barboza { 81*40b44316SDaniel Henrique Barboza qos_add_test("pci_config", "riscv-iommu-pci", test_pci_config, NULL); 82*40b44316SDaniel Henrique Barboza qos_add_test("reg_reset", "riscv-iommu-pci", test_reg_reset, NULL); 83*40b44316SDaniel Henrique Barboza } 84*40b44316SDaniel Henrique Barboza 85*40b44316SDaniel Henrique Barboza libqos_init(register_riscv_iommu_test); 86