Skip to contentSkip to conversation

ARM SMMUv3 Emulation

RFC PATCH In review [email protected] 5 participants 16 messages Jan 11, 2016 – Apr 4, 2016

lore
Share & tools
lore
#0 lore raw

cover ARM SMMUv3 Emulation

From: Prem Mallappa <[email protected]>

Implementation Notes:

- SMMUv3 model, as per ARM SMMUv3 11.0 spec
- Works with Linux Kernel 4.4 SMMUv3 Driver By Will Deacon.
- Stage1 only
- only LPAE Translation tables supported
- BE for translation tables is not supported.
- Save/Restore not supported "YET".
- Broadcom variant added, not much different at the moment

Untested
- Stage2 only
- Stage1+Stage2 support

Future planned work:
- MSI(x) support

Test Case setup:
-------------------
- DPDK + VFIO (DPDK from git master)
- QEMU (master)
- Linux Kernel 4.5
- Bridge interface in Linux

1. Create Bridge interface
- create br0
sudo brctl addbr br0
- create tap0 tap1

	sudo ip tuntap add dev tap0 mode tap user ${USER}
	sudo ip tuntap add dev tap1 mode tap user ${USER}

- assign IP address to br0

	sudo ip addr add 20.40.60.80/24 dev br0

2. Launch QEMU (3 ethernet, one unused, the virtio-net is used for NFS)

     pmallapp> aarch64-softmmu/qemu-system-aarch64 -machine type=virt -cpu cortex-a57 \
		-m 4192 -smp 4 -nographic \
		-append 'console=ttyAMA0,38400n8 mem=4192M  root=/dev/nfs \
			nfsroot=10.0.2.2:/srv/nfsroot/v8,vers=3,nolock rw e1000e.IntMode=2 \
			ip=10.0.2.15:10.0.2.2:10.0.2.2:255.255.255.0:prems:eth0  ' \
		-kernel /home/pmallapp/work/vulcan/armsw/linux/build/arch/arm64/boot/Image \
		-chardev socket,id=serial0,host=localhost,port=5000,server,telnet \
		-serial chardev:serial0 \
		-chardev socket,id=mon0,host=localhost,port=6001,server,telnet,nowait \
		-monitor chardev:mon0 \
		-device pci-bridge,bus=pcie.0,multifunction=on,chassis_nr=2,msi=on,id=pcie.2 \
		-netdev user,id=eth0 \
		-device virtio-net-device,netdev=eth0 \
		-netdev tap,id=eth1,ifname=tap0,script=no,downscript=no \
		-device e1000,netdev=eth1,bus=pcie.2,addr=3 \
		-netdev tap,id=eth2,ifname=tap1,script=no,downscript=no \
		-device virtio-net-pci,netdev=eth2,vectors=0

3. Remove Device from Linux, Add to VFIO, create some reserved Huge pages

	# lspci -nn
	00:00.0 Host bridge [0600]: Red Hat, Inc. Device [1b36:0008]
	00:01.0 PCI bridge [0604]: Red Hat, Inc. QEMU PCI-PCI bridge [1b36:0001]
	00:02.0 Ethernet controller [0200]: Red Hat, Inc Virtio network device [1af4:1000]
	01:03.0 Ethernet controller [0200]: Intel Corporation 82540EM Gigabit Ethernet Controller [8086:100e] (rev 03)
	echo 0000:01:03.0 > /sys/bus/pci/devices/0000:01:03.0/driver/unbind
	echo 8086 100e > /sys/bus/pci/drivers/vfio-pci/new_id

	echo 300 > /proc/sys/vm/nr_hugepages (my case HugePageSize is 2MB)

4. Launch DPDK app, testpmd

	./testpmd -c 0xf  -w 0000:01:03.0 -- --rxq=1 --txq= rxd=32  \
		--txd=32 --mbuf-size=32 --total-num-mbufs=1025 --max-pkt-len=512 -i
	testpmd> set fwd icmpecho
	testpmd> start

5. From host ping any address in 20.40.60.80/24 range

	ping 20.40.60.90 -I br0 -c 10 -r

Prem Mallappa (4):
arm: smmu: ARM SMMUv3 emulation
hw: arm: Added smmuv3 files for build
hw: arm: Add SMMUv3 to virt platform
devicetree: Added new APIs to make use of more fdt functions

 default-configs/aarch64-softmmu.mak |    1 +
 device_tree.c                       |   35 +
 hw/arm/Makefile.objs                |    1 +
 hw/arm/smmuv3-internal.h            |  343 ++++++++
 hw/arm/smmuv3.c                     | 1530 +++++++++++++++++++++++++++++++++++
 hw/arm/virt.c                       |   62 ++
 include/hw/arm/smmuv3.h             |   39 +
 include/hw/arm/virt.h               |    2 +
 include/sysemu/device_tree.h        |   18 +
 9 files changed, 2031 insertions(+)
 create mode 100644 hw/arm/smmuv3-internal.h
 create mode 100644 hw/arm/smmuv3.c
 create mode 100644 include/hw/arm/smmuv3.h
Signature · 1 line
2.6.4
#2 lore raw

RFC PATCH 2/4 hw: arm: Added smmuv3 files for build

From: Prem Mallappa <[email protected]>

Added files to build system

---
 default-configs/aarch64-softmmu.mak | 1 +
 hw/arm/Makefile.objs                | 1 +
 2 files changed, 2 insertions(+)
default-configs/aarch64-softmmu.mak +1 -0
index 96dd994..4c69280 100644
@@ -4,3 +4,4 @@
 include arm-softmmu.mak
 
 CONFIG_XLNX_ZYNQMP=y
+CONFIG_ARM_SMMUV3=y
hw/arm/Makefile.objs +1 -0
index 2195b60..0b453ff 100644
@@ -15,3 +15,4 @@ obj-$(CONFIG_STM32F205_SOC) += stm32f205_soc.o
 obj-$(CONFIG_XLNX_ZYNQMP) += xlnx-zynqmp.o xlnx-ep108.o
 obj-$(CONFIG_FSL_IMX25) += fsl-imx25.o imx25_pdk.o
 obj-$(CONFIG_FSL_IMX31) += fsl-imx31.o kzm.o
+obj-$(CONFIG_ARM_SMMUV3) += smmuv3.o
Signature · 1 line
2.6.4
#3 lore raw

RFC PATCH 3/4 hw: arm: Add SMMUv3 to virt platform

In reply to [email protected]

From: Prem Mallappa <[email protected]>

---
 hw/arm/virt.c         | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/arm/virt.h |  2 ++
 2 files changed, 64 insertions(+)
hw/arm/virt.c +62 -0
index acc1fcb..5538175 100644
@@ -53,6 +53,7 @@
 #include "hw/smbios/smbios.h"
 #include "qapi/visitor.h"
 #include "standard-headers/linux/input.h"
+#include "hw/arm/smmuv3.h"
 
 /* Number of external interrupt lines to configure the GIC with */
 #define NUM_IRQS 256
@@ -72,6 +73,7 @@ typedef struct VirtBoardInfo {
     uint32_t clock_phandle;
     uint32_t gic_phandle;
     uint32_t v2m_phandle;
+    uint32_t smmu_phandle;
 } VirtBoardInfo;
 
 typedef struct {
@@ -122,6 +124,7 @@ static const MemMapEntry a15memmap[] = {
     [VIRT_RTC] =                { 0x09010000, 0x00001000 },
     [VIRT_FW_CFG] =             { 0x09020000, 0x00000018 },
     [VIRT_GPIO] =               { 0x09030000, 0x00001000 },
+    [VIRT_SMMU] =               { 0x09040000, 0x00020000 }, /* 128K, needed */
     [VIRT_MMIO] =               { 0x0a000000, 0x00000200 },
     /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
     [VIRT_PLATFORM_BUS] =       { 0x0c000000, 0x02000000 },
@@ -140,9 +143,19 @@ static const int a15irqmap[] = {
     [VIRT_GPIO] = 7,
     [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
     [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
+    [VIRT_SMMU] = 74,    /* ...to 74 + NUM_SMMU_IRQS - 1 */
     [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
 };
 
+static const struct smmuirq {
+    const char *name;
+} smmuirqmap[NUM_SMMU_IRQS] = {
+    [SMMU_IRQ_EVTQ] = {"eventq"},
+    [SMMU_IRQ_PRIQ] = {"priq"},
+    [SMMU_IRQ_CMD_SYNC] = {"cmdq-sync"},
+    [SMMU_IRQ_GERROR] = {"gerror"},
+};
+
 static VirtBoardInfo machines[] = {
     {
         .cpu_model = "cortex-a15",
@@ -787,6 +800,50 @@ static void create_pcie_irq_map(const VirtBoardInfo *vbi, uint32_t gic_phandle,
                            0x7           /* PCI irq */);
 }
 
+static void alloc_smmu_phandle(VirtBoardInfo *vbi)
+{
+    if (!vbi->smmu_phandle)
+        vbi->smmu_phandle = qemu_fdt_alloc_phandle(vbi->fdt);
+}
+
+static void create_smmu(VirtBoardInfo *vbi, qemu_irq *pic)
+{
+    int i;
+    char *smmu;
+    const char compat[] = "arm,smmu-v3";
+    int irq =  vbi->irqmap[VIRT_SMMU];
+    hwaddr base = vbi->memmap[VIRT_SMMU].base;
+    hwaddr size = vbi->memmap[VIRT_SMMU].size;
+    int type = GIC_FDT_IRQ_TYPE_SPI;
+
+    sysbus_create_varargs("smmuv3", base,
+                          pic[irq],
+                          pic[irq + 1],
+                          pic[irq + 2],
+                          pic[irq + 3],
+                          NULL);
+
+    smmu = g_strdup_printf("/smmuv3@%" PRIx64, base);
+    qemu_fdt_add_subnode(vbi->fdt, smmu);
+    qemu_fdt_setprop(vbi->fdt, smmu, "compatible", compat, sizeof(compat));
+    qemu_fdt_setprop_sized_cells(vbi->fdt, smmu, "reg", 2, base, 2, size);
+
+    for (i = 0; i < NUM_SMMU_IRQS; i++) {
+        qemu_fdt_appendprop_cells(vbi->fdt, smmu, "interrupts",
+                                  type, irq + i,
+                                  GIC_FDT_IRQ_FLAGS_LEVEL_HI);
+        qemu_fdt_appendprop_string(vbi->fdt, smmu, "interrupt-names",
+                                   smmuirqmap[i].name);
+    }
+
+    qemu_fdt_setprop_cell(vbi->fdt, smmu, "clocks", vbi->clock_phandle);
+    qemu_fdt_setprop_cell(vbi->fdt, smmu, "#iommu-cells", 0);
+    qemu_fdt_setprop_string(vbi->fdt, smmu, "clock-names", "apb_pclk");
+
+    qemu_fdt_setprop_cell(vbi->fdt, smmu, "phandle", vbi->smmu_phandle);
+    g_free(smmu);
+}
+
 static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
                         bool use_highmem)
 {
@@ -883,6 +940,7 @@ static void create_pcie(const VirtBoardInfo *vbi, qemu_irq *pic,
     }
 
     qemu_fdt_setprop_cell(vbi->fdt, nodename, "#interrupt-cells", 1);
+    qemu_fdt_setprop_cells(vbi->fdt, nodename, "iommus", vbi->smmu_phandle);
     create_pcie_irq_map(vbi, vbi->gic_phandle, irq, nodename);
 
     g_free(nodename);
@@ -1097,8 +1155,12 @@ static void machvirt_init(MachineState *machine)
 
     create_rtc(vbi, pic);
 
+    alloc_smmu_phandle(vbi);
+
     create_pcie(vbi, pic, vms->highmem);
 
+    create_smmu(vbi, pic);
+
     create_gpio(vbi, pic);
 
     /* Create mmio transports, so the user can create virtio backends
include/hw/arm/virt.h +2 -0
index 925faa7..76349ba 100644
@@ -34,6 +34,7 @@
 
 #define NUM_GICV2M_SPIS       64
 #define NUM_VIRTIO_TRANSPORTS 32
+#define NUM_SMMU_IRQS          4
 
 #define ARCH_TIMER_VIRT_IRQ   11
 #define ARCH_TIMER_S_EL1_IRQ  13
@@ -49,6 +50,7 @@ enum {
     VIRT_GIC_V2M,
     VIRT_GIC_ITS,
     VIRT_GIC_REDIST,
+    VIRT_SMMU,
     VIRT_UART,
     VIRT_MMIO,
     VIRT_RTC,
Signature · 1 line
2.6.4
#4 lore raw

RFC PATCH 4/4 devicetree: Added new APIs to make use of more fdt functions

In reply to [email protected]

From: Prem Mallappa <[email protected]>

SMMUv3 needs device tree entry like below

       interrupt-names = "gerror", "priq", "eventq", "cmdq-sync";

This patch introduces helper function to add entries like above

---
 device_tree.c                | 35 +++++++++++++++++++++++++++++++++++
 include/sysemu/device_tree.h | 18 ++++++++++++++++++
 2 files changed, 53 insertions(+)
device_tree.c +35 -0
index a9f5f8e..7690ff4 100644
@@ -146,6 +146,24 @@ int qemu_fdt_setprop(void *fdt, const char *node_path,
     return r;
 }
 
+int qemu_fdt_appendprop(void *fdt, const char *node_path,
+                     const char *property, const void *val, int size)
+{
+    int r;
+
+    r = fdt_appendprop(fdt, findnode_nofail(fdt, node_path), property,
+                       val, size);
+    if (r < 0) {
+        error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
+                     property, fdt_strerror(r));
+        exit(1);
+    }
+
+    return r;
+}
+
+
+
 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
                           const char *property, uint32_t val)
 {
@@ -168,6 +186,23 @@ int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
     return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
 }
 
+int qemu_fdt_appendprop_string(void *fdt, const char *node_path,
+                            const char *property, const char *string)
+{
+    int r;
+
+    r = fdt_appendprop_string(fdt, findnode_nofail(fdt, node_path),
+                              property, string);
+    if (r < 0) {
+        error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
+                     node_path, property, string, fdt_strerror(r));
+        exit(1);
+    }
+
+    return r;
+}
+
+
 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
                             const char *property, const char *string)
 {
include/sysemu/device_tree.h +18 -0
index 359e143..d5d32b4 100644
@@ -19,12 +19,16 @@ void *load_device_tree(const char *filename_path, int *sizep);
 
 int qemu_fdt_setprop(void *fdt, const char *node_path,
                      const char *property, const void *val, int size);
+int qemu_fdt_appendprop(void *fdt, const char *node_path,
+                     const char *property, const void *val, int size);
 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
                           const char *property, uint32_t val);
 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
                          const char *property, uint64_t val);
 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
                             const char *property, const char *string);
+int qemu_fdt_appendprop_string(void *fdt, const char *node_path,
+                               const char *property, const char *string);
 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
                              const char *property,
                              const char *target_node_path);
@@ -49,6 +53,20 @@ int qemu_fdt_add_subnode(void *fdt, const char *name);
                          sizeof(qdt_tmp));                                    \
     } while (0)
 
+
+#define qemu_fdt_appendprop_cells(fdt, node_path, property, ...)              \
+    do {                                                                      \
+        uint32_t qdt_tmp[] = { __VA_ARGS__ };                                 \
+        int i;                                                                \
+                                                                              \
+        for (i = 0; i < ARRAY_SIZE(qdt_tmp); i++) {                           \
+            qdt_tmp[i] = cpu_to_be32(qdt_tmp[i]);                             \
+        }                                                                     \
+        qemu_fdt_appendprop(fdt, node_path, property, qdt_tmp,                \
+                         sizeof(qdt_tmp));                                    \
+    } while (0)
+
+
 void qemu_fdt_dumpdtb(void *fdt, int size);
 
 /**
Signature · 1 line
2.6.4
#1 lore raw

RFC PATCH 1/4 arm: smmu: ARM SMMUv3 emulation

In reply to [email protected]

From: Prem Mallappa <[email protected]>

Implementation Notes:
- Implements ARM SMMUv3 version 11.0
- Works with SMMUv3 Driver in Linux Kernel 4.4
- Only LPAE mode Translation supported
- BE mode TT not supported Yet
- Stage1 only
- Suspend/Resume not yet supported
- Broadcom variant added, but not much difference at the moment

---
 hw/arm/smmuv3-internal.h |  343 +++++++++++
 hw/arm/smmuv3.c          | 1530 ++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/arm/smmuv3.h  |   39 ++
 3 files changed, 1912 insertions(+)
 create mode 100644 hw/arm/smmuv3-internal.h
 create mode 100644 hw/arm/smmuv3.c
 create mode 100644 include/hw/arm/smmuv3.h
hw/arm/smmuv3-internal.h new file +343 -0
new file mode 100644
index 0000000..54ef52a
@@ -0,0 +1,343 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) 2014-2015 Broadcom Corporation
+ *
+ * Author: Prem Mallappa <[email protected]>
+ *
+ */
+
+#ifndef HW_ARM_SMMU_V3_INTERNAL_H
+#define HW_ARM_SMMU_V3_INTERNAL_H
+
+/*****************************
+ * MMIO Register
+ *****************************/
+enum {
+    SMMU_REG_IDR0            = 0x0,
+
+#define SMMU_IDR0_S2P            (1 << 0)
+#define SMMU_IDR0_S1P            (1 << 1)
+#define SMMU_IDR0_TTF            (0x3 << 2)
+#define SMMU_IDR0_HTTU           (0x3 << 6)
+#define SMMU_IDR0_HYP            (1 << 9)
+#define SMMU_IDR0_ATS            (1 << 10)
+#define SMMU_IDR0_VMID16         (1 << 18)
+#define SMMU_IDR0_CD2L           (1 << 19)
+
+    SMMU_REG_IDR1            = 0x4,
+    SMMU_REG_IDR2            = 0x8,
+    SMMU_REG_IDR3            = 0xc,
+    SMMU_REG_IDR4            = 0x10,
+    SMMU_REG_IDR5            = 0x14,
+    SMMU_REG_IIDR            = 0x1c,
+    SMMU_REG_CR0             = 0x20,
+
+#define SMMU_CR0_SMMU_ENABLE (1 << 0)
+#define SMMU_CR0_PRIQ_ENABLE (1 << 1)
+#define SMMU_CR0_EVTQ_ENABLE (1 << 2)
+#define SMMU_CR0_CMDQ_ENABLE (1 << 3)
+#define SMMU_CR0_ATS_CHECK   (1 << 4)
+
+    SMMU_REG_CR0_ACK         = 0x24,
+    SMMU_REG_CR1             = 0x28,
+    SMMU_REG_CR2             = 0x2c,
+
+    SMMU_REG_STATUSR         = 0x40,
+
+    SMMU_REG_IRQ_CTRL        = 0x50,
+    SMMU_REG_IRQ_CTRL_ACK    = 0x54,
+
+#define SMMU_IRQ_CTRL_GERROR_EN (1 << 0)
+#define SMMU_IRQ_CTRL_EVENT_EN  (1 << 1)
+#define SMMU_IRQ_CTRL_PRI_EN    (1 << 2)
+
+    SMMU_REG_GERROR          = 0x60,
+
+#define SMMU_GERROR_CMDQ       (1 << 0)
+#define SMMU_GERROR_EVENTQ     (1 << 1)
+#define SMMU_GERROR_PRIQ       (1 << 2)
+#define SMMU_GERROR_MSI_CMDQ   (1 << 3)
+#define SMMU_GERROR_MSI_EVENTQ (1 << 4)
+#define SMMU_GERROR_MSI_PRIQ   (1 << 5)
+#define SMMU_GERROR_MSI_GERROR (1 << 6)
+#define SMMU_GERROR_SFM_ERR    (1 << 7)
+
+    SMMU_REG_GERRORN         = 0x64,
+    SMMU_REG_GERROR_IRQ_CFG0 = 0x68,
+    SMMU_REG_GERROR_IRQ_CFG1 = 0x70,
+    SMMU_REG_GERROR_IRQ_CFG2 = 0x74,
+
+    /* SMMU_BASE_RA Applies to STRTAB_BASE, CMDQ_BASE and EVTQ_BASE */
+#define SMMU_BASE_RA        (1ULL << 62)
+    SMMU_REG_STRTAB_BASE     = 0x80,
+    SMMU_REG_STRTAB_BASE_CFG = 0x88,
+
+    SMMU_REG_CMDQ_BASE       = 0x90,
+    SMMU_REG_CMDQ_PROD       = 0x98,
+    SMMU_REG_CMDQ_CONS       = 0x9c,
+    /* CMD Consumer (CONS) */
+#define SMMU_CMD_CONS_ERR_SHIFT        24
+#define SMMU_CMD_CONS_ERR_BITS         7
+
+    SMMU_REG_EVTQ_BASE       = 0xa0,
+    SMMU_REG_EVTQ_PROD       = 0xa8,
+    SMMU_REG_EVTQ_CONS       = 0xac,
+    SMMU_REG_EVTQ_IRQ_CFG0   = 0xb0,
+    SMMU_REG_EVTQ_IRQ_CFG1   = 0xb8,
+    SMMU_REG_EVTQ_IRQ_CFG2   = 0xbc,
+
+    SMMU_REG_PRIQ_BASE       = 0xc0,
+    SMMU_REG_PRIQ_PROD       = 0xc8,
+    SMMU_REG_PRIQ_CONS       = 0xcc,
+    SMMU_REG_PRIQ_IRQ_CFG0   = 0xd0,
+    SMMU_REG_PRIQ_IRQ_CFG1   = 0xd8,
+    SMMU_REG_PRIQ_IRQ_CFG2   = 0xdc,
+
+    SMMU_ID_REGS_OFFSET      = 0xfd0,
+
+    /* Secure registers are not used for now */
+    SMMU_SECURE_OFFSET       = 0x8000,
+};
+
+/*****************************
+ * STE fields
+ *****************************/
+#define STE_VALID(x)   extract32((x)->word[0], 0, 1) /* 0 */
+#define STE_CONFIG(x)  extract32((x)->word[0], 1, 3)
+enum {
+    STE_CONFIG_NONE      = 0,
+    STE_CONFIG_S1BY_S2BY = 4,   /* S1 Bypass, S2 Bypass */
+    STE_CONFIG_S1TR_S2BY,       /* S1 Translate, S2 Bypass */
+    STE_CONFIG_S1BY_S2TR,       /* S1 Bypass, S2 Translate */
+    STE_CONFIG_S1TR_S2TR,       /* S1 Translate, S2 Translate */
+};
+#define STE_S1FMT(x)   extract32((x)->word[0], 4, 2)
+#define STE_S1CDMAX(x) extract32((x)->word[1], 8, 2)
+#define STE_EATS(x)    extract32((x)->word[2], 28, 2)
+#define STE_STRW(x)    extract32((x)->word[2], 30, 2)
+#define STE_S2VMID(x)  extract32((x)->word[4], 0, 16) /* 4 */
+#define STE_S2T0SZ(x)  extract32((x)->word[5], 0, 6) /* 5 */
+#define STE_S2TG(x)    extract32((x)->word[5], 14, 2)
+#define STE_S2PS(x)    extract32((x)->word[5], 16, 3)
+#define STE_S2AA64(x)  extract32((x)->word[5], 19, 1)
+#define STE_S2HD(x)    extract32((x)->word[5], 24, 1)
+#define STE_S2HA(x)    extract32((x)->word[5], 25, 1)
+#define STE_S2S(x)     extract32((x)->word[5], 26, 1)
+#define STE_CTXPTR(x)                                           \
+    ({                                                          \
+        unsigned long addr;                                     \
+        addr = (uint64_t)extract32((x)->word[1], 0, 16) << 32;  \
+        addr |= (uint64_t)((x)->word[0] & 0xffffffc0);          \
+        addr;                                                   \
+    })
+
+#define STE_S2TTB(x)                                            \
+    ({                                                          \
+        unsigned long addr;                                     \
+        addr = (uint64_t)extract32((x)->word[7], 0, 16) << 32;  \
+        addr |= (uint64_t)((x)->word[6] & 0xfffffff0);          \
+        addr;                                                   \
+    })
+
+/*****************************
+ * CD fields
+ *****************************/
+#define CD_VALID(x)   extract32((x)->word[0], 30, 1)
+#define CD_ASID(x)    extract32((x)->word[1], 16, 16)
+#define CD_TTB(x, sel)                                  \
+    ({                                                  \
+        uint64_t hi, lo;                                \
+        hi = extract32((x)->word[(sel)*2 + 3], 0, 16);  \
+        hi <<= 32;                                      \
+        lo = (x)->word[(sel)*2+2] & ~0xf;               \
+        hi | lo;                                        \
+    })
+
+#define CD_TSZ(x, sel)   extract32((x)->word[0], (16*(sel)) + 0, 6)
+#define CD_TG(x, sel)    extract32((x)->word[0], (16*(sel)) + 6, 2)
+#define CD_EPD(x, sel)   extract32((x)->word[0], (16*(sel)) + 14, 1)
+
+#define CD_T0SZ(x)    CD_TSZ((x), 0)
+#define CD_T1SZ(x)    CD_TSZ((x), 1)
+#define CD_TG0(x)     CD_TG((x), 0)
+#define CD_TG1(x)     CD_TG((x), 1)
+#define CD_EPD0(x)    CD_EPD((x), 0)
+#define CD_EPD1(x)    CD_EPD((x), 1)
+#define CD_IPS(x)     extract32((x)->word[(1)], 0, 3)
+#define CD_AARCH64(x) extract32((x)->word[(1)], 9, 1)
+#define CD_TTB0(x)    CD_TTB((x), 0)
+#define CD_TTB1(x)    CD_TTB((x), 1)
+
+#define CDM_VALID(x)    ((x)->word[0] & 0x1)
+
+/*****************************
+ * Commands
+ *****************************/
+enum {
+    SMMU_CMD_PREFETCH_CONFIG = 0x01,
+    SMMU_CMD_PREFETCH_ADDR,
+    SMMU_CMD_CFGI_STE,
+    SMMU_CMD_CFGI_STE_RANGE,
+    SMMU_CMD_CFGI_CD,
+    SMMU_CMD_CFGI_CD_ALL,
+    SMMU_CMD_TLBI_NH_ALL     = 0x10,
+    SMMU_CMD_TLBI_NH_ASID,
+    SMMU_CMD_TLBI_NH_VA,
+    SMMU_CMD_TLBI_NH_VAA,
+    SMMU_CMD_TLBI_EL3_ALL    = 0x18,
+    SMMU_CMD_TLBI_EL3_VA     = 0x1a,
+    SMMU_CMD_TLBI_EL2_ALL    = 0x20,
+    SMMU_CMD_TLBI_EL2_ASID,
+    SMMU_CMD_TLBI_EL2_VA,
+    SMMU_CMD_TLBI_EL2_VAA,  /* 0x23 */
+    SMMU_CMD_TLBI_S12_VMALL  = 0x28,
+    SMMU_CMD_TLBI_S2_IPA     = 0x2a,
+    SMMU_CMD_TLBI_NSNH_ALL   = 0x30,
+    SMMU_CMD_ATC_INV         = 0x40,
+    SMMU_CMD_PRI_RESP,
+    SMMU_CMD_RESUME          = 0x44,
+    SMMU_CMD_STALL_TERM,
+    SMMU_CMD_SYNC,          /* 0x46 */
+};
+
+/*****************************
+ * CMDQ fields
+ *****************************/
+
+enum { /* Command Errors */
+    SMMU_CMD_ERR_NONE = 0,
+    SMMU_CMD_ERR_ILLEGAL,
+    SMMU_CMD_ERR_ABORT
+};
+
+enum { /* Command completion notification */
+    CMD_SYNC_SIG_NONE,
+    CMD_SYNC_SIG_IRQ,
+    CMD_SYNC_SIG_SEV,
+};
+
+#define CMD_TYPE(x)  extract32((x)->word[0], 0, 8)
+#define CMD_SEC(x)   extract32((x)->word[0], 9, 1)
+#define CMD_SEV(x)   extract32((x)->word[0], 10, 1)
+#define CMD_AC(x)    extract32((x)->word[0], 12, 1)
+#define CMD_AB(x)    extract32((x)->word[0], 13, 1)
+#define CMD_CS(x)    extract32((x)->word[0], 12, 2)
+#define CMD_SSID(x)  extract32((x)->word[0], 16, 16)
+#define CMD_SID(x)   ((x)->word[1])
+#define CMD_VMID(x)  extract32((x)->word[1], 0, 16)
+#define CMD_ASID(x)  extract32((x)->word[1], 16, 16)
+#define CMD_STAG(x)  extract32((x)->word[2], 0, 16)
+#define CMD_RESP(x)  extract32((x)->word[2], 11, 2)
+#define CMD_GRPID(x) extract32((x)->word[3], 0, 8)
+#define CMD_SIZE(x)  extract32((x)->word[3], 0, 16)
+#define CMD_LEAF(x)  extract32((x)->word[3], 0, 1)
+#define CMD_SPAN(x)  extract32((x)->word[3], 0, 5)
+#define CMD_ADDR(x) ({                                  \
+            uint64_t addr = (uint64_t)(x)->word[3];     \
+            addr <<= 32;                                \
+            addr |=  extract32((x)->word[3], 12, 20);   \
+            addr;                                       \
+        })
+
+/*****************************
+ * EVTQ fields
+ *****************************/
+#define EVT_Q_OVERFLOW        (1<<31)
+
+#define EVT_SET_TYPE(x, t)    deposit32((x)->word[0], 0, 8, t)
+#define EVT_SET_SID(x, s)     ((x)->word[1] =  s)
+#define EVT_SET_INPUT_ADDR(x, addr) ({                    \
+            (x)->word[5] = (uint32_t)(addr >> 32);        \
+            (x)->word[4] = (uint32_t)(addr & 0xffffffff); \
+            addr;                                         \
+        })
+
+/*****************************
+ * Events
+ *****************************/
+enum evt_err {
+    SMMU_EVT_F_UUT    = 0x1,
+    SMMU_EVT_C_BAD_SID,
+    SMMU_EVT_F_STE_FETCH,
+    SMMU_EVT_C_BAD_STE,
+    SMMU_EVT_F_BAD_ATS_REQ,
+    SMMU_EVT_F_STREAM_DISABLED,
+    SMMU_EVT_F_TRANS_FORBIDDEN,
+    SMMU_EVT_C_BAD_SSID,
+    SMMU_EVT_F_CD_FETCH,
+    SMMU_EVT_C_BAD_CD,
+    SMMU_EVT_F_WALK_EXT_ABRT,
+    SMMU_EVT_F_TRANS        = 0x10,
+    SMMU_EVT_F_ADDR_SZ,
+    SMMU_EVT_F_ACCESS,
+    SMMU_EVT_F_PERM,
+    SMMU_EVT_F_TLB_CONFLICT = 0x20,
+    SMMU_EVT_F_CFG_CONFLICT = 0x21,
+    SMMU_EVT_E_PAGE_REQ     = 0x24,
+};
+
+typedef enum evt_err SMMUEvtErr;
+
+
+/*****************************
+ * SMMU Data structures
+ *****************************/
+#define ARM_SMMU_FEAT_PASSID_SUPPORT  (1 << 24) /* Some random bits for now */
+#define ARM_SMMU_FEAT_CD_2LVL         (1 << 25)
+
+struct __smmu_data2 {
+    uint32_t word[2];
+};
+
+struct __smmu_data8 {
+    uint32_t word[8];
+};
+
+struct __smmu_data16 {
+    uint32_t word[16];
+};
+
+struct __smmu_data4 {
+    uint32_t word[4];
+};
+
+typedef struct __smmu_data2  STEDesc; /* STE Level 1 Descriptor */
+typedef struct __smmu_data16 Ste;     /* Stream Table Entry(STE) */
+typedef struct __smmu_data2  CDDesc;  /* CD Level 1 Descriptor */
+typedef struct __smmu_data16 Cd;      /* Context Descriptor(CD) */
+
+typedef struct __smmu_data4  Cmd; /* Command Entry */
+typedef struct __smmu_data8  Evt; /* Event Entry */
+typedef struct __smmu_data4  Pri; /* PRI entry */
+
+
+/*****************************
+ * Broadcom Specific register and bits
+ *****************************/
+#define SMMU_REG_CNTL         (0x410 << 2)
+#define SMMU_REG_CNTL_1       (0x411 << 2)
+#define SMMU_REG_INTERRUPT    (0x412 << 2)
+/* BIT encoding is same as SMMU_REG_INTERRUPT, except for last 4 bits */
+#define SMMU_REG_INTERRUPT_EN (0x413 << 2)
+
+#define SMMU_INTR_BMI_ERR  (1 << 6) /* Smmu BMI Rd Wr Error*/
+#define SMMU_INTR_BSI_ERR  (1 << 5) /* Smmu BSI Rd Wr Error*/
+#define SMMU_INTR_SBU_INTR (1 << 4) /* SBU interrupt 0 */
+#define SMMU_INTR_CMD_SYNC (1 << 3) /* CmdSync completion set to interrupt */
+#define SMMU_INTR_EVENT    (1 << 2) /* high till EventQ.PROD != EventQ.CONS */
+#define SMMU_INTR_PRI      (1 << 1) /* PriQ. high till PriQ.PROD != PriQ.CONS */
+#define SMMU_INTR_GERROR   (1 << 0) /* cleared when  GERRORN is written */
+
+#endif
hw/arm/smmuv3.c new file +1530 -0

1,530 lines not on this page · raw message

include/hw/arm/smmuv3.h new file +39 -0
new file mode 100644
index 0000000..d071edc
@@ -0,0 +1,39 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) 2014-2015 Broadcom Corporation
+ *
+ * Author: Prem Mallappa <[email protected]>
+ *
+ */
+#ifndef HW_ARM_SMMU_V3_H
+#define HW_ARM_SMMU_V3_H
+
+#define TYPE_SMMU_DEV_BASE "smmuv3-base"
+#define TYPE_SMMU_DEV      "smmuv3"
+#define TYPE_SMMU_BRCM_DEV "smmuv3-brcm"
+
+typedef enum {
+    SMMU_IMPL_ARM = 0x1,
+    SMMU_IMPL_BRCM,
+} SMMUImpl;
+
+typedef enum {
+    SMMU_IRQ_GERROR,
+    SMMU_IRQ_PRIQ,
+    SMMU_IRQ_EVTQ,
+    SMMU_IRQ_CMD_SYNC,
+} SMMUIrq;
+
+#endif
Signature · 1 line
2.6.4
#6 lore raw

Hi,

Quoted 2 lines

You seem to have sent three copies of this in the last few hours. Is there any difference between them?

Nope, my apologies for spamming your inbox,
Previous ones failed to post to _qemu-devel_, due to my office SMTP adding some incompatible bits.

Cheers,
/Prem

#7 lore raw
In reply to [email protected]
Quoted 12 lines · On 11 January 2016 at 14:16, <[email protected]> wrote:

From: Prem Mallappa <[email protected]>

Implementation Notes:

- SMMUv3 model, as per ARM SMMUv3 11.0 spec
- Works with Linux Kernel 4.4 SMMUv3 Driver By Will Deacon.
- Stage1 only
- only LPAE Translation tables supported
- BE for translation tables is not supported.
- Save/Restore not supported "YET".
- Broadcom variant added, not much different at the moment

Quoted 6 lines

Untested
- Stage2 only
- Stage1+Stage2 support

Future planned work:
- MSI(x) support

So, Xilinx also has an SMMU model (out of tree). Edgar, Alistair, would you care to have a look at this code and suggest whether one or the other is a better base to start from? (If there's not much difference then I'm inclined to start with the one that got posted to the list first :-))

thanks
-- PMM

#8 lore raw
Quoted 17 lines · Peter Maydell
Quoted 16 lines · On 11 January 2016 at 14:16, <[email protected]> wrote:

From: Prem Mallappa <[email protected]>

Implementation Notes:

- SMMUv3 model, as per ARM SMMUv3 11.0 spec
- Works with Linux Kernel 4.4 SMMUv3 Driver By Will Deacon.
- Stage1 only
- only LPAE Translation tables supported
- BE for translation tables is not supported.
- Save/Restore not supported "YET".
- Broadcom variant added, not much different at the moment

Untested
- Stage2 only
- Stage1+Stage2 support

Quoted 8 lines
Quoted 2 lines

Future planned work:
- MSI(x) support

So, Xilinx also has an SMMU model (out of tree). Edgar, Alistair, would you care to have a look at this code and suggest whether one or the other is a better base to start from? (If there's not much difference then I'm inclined to start with the one that got posted to the list first :-))

Edgar has done all of the SMMU work for Xilinx, he knows it the best. I'll let him comment on it.

For anyone interested you can see our implementation at:
https://github.com/Xilinx/qemu/blob/master/hw/misc/arm-smmu.c. It does use the register API that we have been trying to upstream.

Thanks,

Alistair

Quoted 4 lines

thanks
-- PMM

#9 lore raw
Quoted 7 lines

Edgar has done all of the SMMU work for Xilinx, he knows it the best. I'll let him comment on it.

For anyone interested you can see our implementation at:
https://github.com/Xilinx/qemu/blob/master/hw/misc/arm-smmu.c. It does use the register API that we have been trying to upstream.

Hi,
I took a quick look at the code, The Xilinx implements mmu-500 which is a SMMU- v2.
I am not very familiar with V2, however, the architecture and internal workings are different between v2 and v3. hence there are 2 different drivers in Linux for V2 and V3, and the code I posted is for SMMU-v3.

Cheers,
/Prem

#10 lore raw
Quoted 12 lines · Prem (Premachandra) Mallappa
Quoted 7 lines

Edgar has done all of the SMMU work for Xilinx, he knows it the best. I'll let him comment on it.

For anyone interested you can see our implementation at:
https://github.com/Xilinx/qemu/blob/master/hw/misc/arm-smmu.c. It does use the register API that we have been trying to upstream.

Hi,
I took a quick look at the code, The Xilinx implements mmu-500 which is a SMMU- v2.
I am not very familiar with V2, however, the architecture and internal workings are different between v2 and v3. hence there are 2 different drivers in Linux for V2 and V3, and the code I posted is for SMMU-v3.

Hi,

Yes, we did SMMUv2 but I think there is opportunity to reuse quite a bit of code.

I don't mind basing the implementation from the broadcom code but there are a few things that could be taken from our our code or at least re-implemented.

I haven't looked at the SMMUv3 at all but I'm guessing the translation tables are the same. We could share code between a v2 and a v3 SMMU and possibly even some of it with the CPU models.
Our translation code has been tested with S1 only, S2 only and S1 + S2. We've ran it with the Linux SMMU v2 driver, XEN SMMU v2 driver and our internal testsuites. This code could be a candidate for merging with your SMMU code as I noticed that the broadcom implementation is lacking some of the modes.

Another thing I noticed is that it doesn't seem like you've modelled the TBUs (these may be named differently in SMMUv3 terminology if they even exist)? Anyway, we need these as the address-spaces on the ZynqMP are not all the same for all TBUs.

Cheers,
Edgar

#11 lore raw

Hi Edgar,

Quoted 1 line

Yes, we did SMMUv2 but I think there is opportunity to reuse quite a bit of code.

I agree

Quoted 4 lines

I don't mind basing the implementation from the broadcom code but there are a few things that could be taken from our our code or at least re- implemented.

We can collaborate in refactoring common code.

Quoted 3 lines

I haven't looked at the SMMUv3 at all but I'm guessing the translation tables are the same. We could share code between a v2 and a v3 SMMU and possibly even some of it with the CPU models.

Yes TT are same w.r.t CPU, my code re-uses the CPU code but just little differently

Quoted 5 lines

Our translation code has been tested with S1 only, S2 only and S1 + S2. We've ran it with the Linux SMMU v2 driver, XEN SMMU v2 driver and our internal testsuites. This code could be a candidate for merging with your SMMU code as I noticed that the broadcom implementation is lacking some of the modes.

Yes, I have so far only able to test S1 only, I'll contact you off-list for setting up a Xen based S1+S2 testing. But I guess my hands are tied in this case with
S1+FastModels not (yet) having SMMUv3
And Qemu 64-bit ARM doesn't yet have KVM support, I'll be using Qemu v7
And it's a RFC version posted early to avoid duplicate effort and before digging too deep.

Quoted 4 lines

Another thing I noticed is that it doesn't seem like you've modelled the TBUs (these may be named differently in SMMUv3 terminology if they even exist)? Anyway, we need these as the address-spaces on the ZynqMP are not all the same for all TBUs.

No TLB cache so far, I have plans of implementing (or reusing code from your implementation, whichever works).

I look forward to having more discussion on this topic.

Cheers,
/Prem

#12 lore raw
In reply to [email protected]
Quoted 5 lines · On 11 January 2016 at 14:16, <[email protected]> wrote:

From: Prem Mallappa <[email protected]>

Implementation Notes:

- SMMUv3 model, as per ARM SMMUv3 11.0 spec

I haven't reviewed any of this yet, but 11.0 is not the current revision of the SMMUv3 spec, so you should probably start by updating your code to the current version. Otherwise code review will be full of "this isn't what the spec says"...

thanks
-- PMM

#13 lore raw

Hi Peter,

I have access till
"PRD03-GENC-010952 Version 11.0" updated 18/04/15.

infocenter.arm.com has only SMMUv2 made public

Please let me know if there is a new version available
- to public.
- through channels, I have to work with my organizational contact to get
to the new one.

I am not expecting any fundamental operation changes. However, I'll take a look if theres a new version.

Cheers,
/Prem

Quoted 15 lines · Peter Maydell
Quoted 5 lines · On 11 January 2016 at 14:16, <[email protected]> wrote:

From: Prem Mallappa <[email protected]>

Implementation Notes:

- SMMUv3 model, as per ARM SMMUv3 11.0 spec

I haven't reviewed any of this yet, but 11.0 is not the current revision of the SMMUv3 spec, so you should probably start by updating your code to the current version. Otherwise code review will be full of "this isn't what the spec says"...

thanks
-- PMM

Signature · 2 lines
Cheers,
/Prem
#14 lore raw
Quoted 7 lines · Prem Mallappa

Hi Peter,

I have access till
"PRD03-GENC-010952 Version 11.0" updated 18/04/15.

infocenter.arm.com has only SMMUv2 made public

Quoted 7 lines

Please let me know if there is a new version available
- to public.
- through channels, I have to work with my organizational contact to get to
the new one.

I am not expecting any fundamental operation changes. However, I'll take a look if theres a new version.

Hi; just wanted to ask if you'd got the updated SMMUv3 spec yet, and to check that there that we're in agreement that the next step here is that you would do a respin of your patches updated to the final specification. (That is, you're not blocked waiting for anything more from me or Edgar. Occasionally in these patch review threads there's a miscommunication and both sides think they're waiting for the other, so I just wanted to check that.)

thanks
-- PMM

#15 lore raw

Hi Peter,
Thanks for following up.

I got access to 16.0 version, I dont particularly see any difference in basic functionality.

I had been in touch with Edger, We agreed upon making usable code b/w smmuv2 and smmuv3 for now. Currently the code is in working state.

I also heard, there has been talks to merge code with CPU's page walk code. but till then we can make use of the current setup.

I'll be sending out v2 soon, just trying to write some Qtests to test 2-stage page walks.

Edger, if you already have written Qtest code please do share, it would be of tremendous help now.

Thanks

Quoted 30 lines · Peter Maydell
Quoted 10 lines · Prem Mallappa

Hi Peter,

I have access till
"PRD03-GENC-010952 Version 11.0" updated 18/04/15.

infocenter.arm.com has only SMMUv2 made public

Please let me know if there is a new version available
- to public.
- through channels, I have to work with my organizational contact to

get to

Quoted 3 lines

the new one.

I am not expecting any fundamental operation changes. However, I'll take

a

Quoted 1 line

look if theres a new version.

Hi; just wanted to ask if you'd got the updated SMMUv3 spec yet, and to check that there that we're in agreement that the next step here is that you would do a respin of your patches updated to the final specification. (That is, you're not blocked waiting for anything more from me or Edgar. Occasionally in these patch review threads there's a miscommunication and both sides think they're waiting for the other, so I just wanted to check that.)

thanks
-- PMM

Signature · 2 lines
Cheers,
/Prem