Signed-off-by: Henning Schild <[email protected]>
---
 inmates/demos/arm/Makefile             |   5 +-
 inmates/demos/{x86 => }/ivshmem-demo.c | 114 +++++++++++++++++++++++++--------
 inmates/demos/x86/Makefile             |   4 +-
 3 files changed, 95 insertions(+), 28 deletions(-)
 rename inmates/demos/{x86 => }/ivshmem-demo.c (70%)

diff --git a/inmates/demos/arm/Makefile b/inmates/demos/arm/Makefile
index b0fda4e..76c8adf 100644
--- a/inmates/demos/arm/Makefile
+++ b/inmates/demos/arm/Makefile
@@ -12,9 +12,12 @@
 
 include $(INMATES_LIB)/Makefile.lib
 
-INMATES := gic-demo.bin uart-demo.bin
+INMATES := gic-demo.bin uart-demo.bin ivshmem-demo.bin
 
 gic-demo-y     := gic-demo.o
 uart-demo-y    := uart-demo.o
+ivshmem-demo-y := ../ivshmem-demo.o
+
+ccflags-y      += -DCONFIG_ARM
 
 $(eval $(call DECLARE_TARGETS,$(INMATES)))
diff --git a/inmates/demos/x86/ivshmem-demo.c b/inmates/demos/ivshmem-demo.c
similarity index 70%
rename from inmates/demos/x86/ivshmem-demo.c
rename to inmates/demos/ivshmem-demo.c
index 7361032..1655b0f 100644
--- a/inmates/demos/x86/ivshmem-demo.c
+++ b/inmates/demos/ivshmem-demo.c
@@ -10,6 +10,13 @@
  * the COPYING file in the top-level directory.
  */
 #include <inmate.h>
+#ifdef CONFIG_ARM
+#include <mach.h>
+#define PAGE_SIZE      (4 * 1024ULL)
+#define PAGE_MASK      (~(PAGE_SIZE - 1))
+#elif CONFIG_X86
+#define IRQ_VECTOR     32
+#endif
 
 #define VENDORID       0x1af4
 #define DEVICEID       0x1110
@@ -19,10 +26,7 @@
 
 #define JAILHOUSE_SHMEM_PROTO_UNDEFINED        0x0000
 
-#define IRQ_VECTOR     32
-
 #define MAX_NDEV       4
-#define UART_BASE      0x3F8
 
 static char str[32] = "Hello From IVSHMEM  ";
 static int ndevices;
@@ -32,9 +36,11 @@ struct ivshmem_dev_data {
        u16 bdf;
        u32 *registers;
        void *shmem;
-       u32 *msix_table;
        u64 shmemsz;
+#ifdef CONFIG_X86
        u64 bar2sz;
+       u32 *msix_table;
+#endif
 };
 
 static struct ivshmem_dev_data devs[MAX_NDEV];
@@ -54,6 +60,7 @@ static void pci_cfg_write64(u16 bdf, unsigned int addr, u64 
val)
        pci_write_config(bdf, addr, (u32)val, 4);
 }
 
+#ifdef CONFIG_X86
 static u64 get_bar_sz(u16 bdf, u8 barn)
 {
        u64 bar, tmp;
@@ -67,16 +74,18 @@ static u64 get_bar_sz(u16 bdf, u8 barn)
 
        return barsz;
 }
+#endif
 
 static void map_shmem_and_bars(struct ivshmem_dev_data *d)
 {
+#ifdef CONFIG_X86
        int cap = pci_find_cap(d->bdf, PCI_CAP_MSIX);
 
        if (cap < 0) {
                printk("IVSHMEM ERROR: device is not MSI-X capable\n");
                return;
        }
-
+#endif
        d->shmemsz = pci_cfg_read64(d->bdf, IVSHMEM_CFG_SHMEM_SZ);
        d->shmem = (void *)pci_cfg_read64(d->bdf, IVSHMEM_CFG_SHMEM_PTR);
 
@@ -85,14 +94,18 @@ static void map_shmem_and_bars(struct ivshmem_dev_data *d)
                & PAGE_MASK);
        pci_cfg_write64(d->bdf, PCI_CFG_BAR, (u64)d->registers);
        printk("IVSHMEM: bar0 is at %p\n", d->registers);
+#ifdef CONFIG_X86
        d->bar2sz = get_bar_sz(d->bdf, 2);
        d->msix_table = (u32 *)((u64)d->registers + PAGE_SIZE);
        pci_cfg_write64(d->bdf, PCI_CFG_BAR + 16, (u64)d->msix_table);
        printk("IVSHMEM: bar2 is at %p\n", d->msix_table);
+#endif
 
        pci_write_config(d->bdf, PCI_CFG_COMMAND,
                         (PCI_CMD_MEM | PCI_CMD_MASTER), 2);
+#ifdef CONFIG_X86
        map_range(d->shmem, d->shmemsz + PAGE_SIZE + d->bar2sz, MAP_UNCACHED);
+#endif
 }
 
 static int get_ivpos(struct ivshmem_dev_data *d)
@@ -107,20 +120,61 @@ static void send_irq(struct ivshmem_dev_data *d)
        mmio_write32(d->registers + 3, 1);
 }
 
+static void use_device(int i)
+{
+       struct ivshmem_dev_data *d;
+
+       if (i >= ndevices)
+               return;
+       d = devs + i;
+       ((char *)(d->shmem))[19]++;
+       send_irq(d);
+}
+
+#ifdef CONFIG_ARM
+static int irq_handler_dev;
+
+static void irq_handler(unsigned int irqn)
+#else
 static void irq_handler(void)
+#endif
 {
+#ifdef CONFIG_ARM
+       if (irqn == TIMER_IRQ) {
+               if (irq_handler_dev == ndevices - 1)
+                       irq_handler_dev = 0;
+               else
+                       irq_handler_dev++;
+               use_device(irq_handler_dev);
+               timer_start(timer_get_frequency());
+               return;
+       }
+#endif
        printk("IVSHMEM: got interrupt ... %d\n", irq_counter++);
 }
 
 void inmate_main(void)
 {
-       int i;
        int bdf = 0;
        unsigned int class_rev;
        struct ivshmem_dev_data *d;
-       volatile char *shmem;
+
+#ifdef CONFIG_X86
+       int i;
 
        int_init();
+#elif CONFIG_ARM
+       long long pci_cfg_base, irq;
+
+       pci_cfg_base = cmdline_parse_int("pci-cfg-base", -1);
+       irq = cmdline_parse_int("irq", -1);
+       gic_setup(irq_handler);
+       gic_enable_irq(TIMER_IRQ);
+       timer_start(timer_get_frequency());
+
+       if (pci_cfg_base != -1)
+               pci_set_mmio_base(pci_cfg_base);
+#endif
 
        while ((ndevices < MAX_NDEV) &&
               (-1 != (bdf = pci_find_device(VENDORID, DEVICEID, bdf)))) {
@@ -129,13 +183,13 @@ void inmate_main(void)
                       pci_read_config(bdf, PCI_CFG_DEVICE_ID, 2),
                       bdf >> 8, (bdf >> 3) & 0x1f, bdf & 0x3);
                class_rev = pci_read_config(bdf, 0x8, 4);
-               if (class_rev != (PCI_DEV_CLASS_OTHER << 24 |
-                                 JAILHOUSE_SHMEM_PROTO_UNDEFINED << 8)) {
-                       printk("IVSHMEM: class/revision %08x, not supported "
-                              "skipping device\n", class_rev);
-                       bdf++;
-                       continue;
-               }
+//             if (class_rev != (PCI_DEV_CLASS_OTHER << 24 |
+//                               JAILHOUSE_SHMEM_PROTO_UNDEFINED << 8)) {
+//                     printk("IVSHMEM: class/revision %08x, not supported "
+//                            "skipping device\n", class_rev);
+//                     bdf++;
+//                     continue;
+//             }
                ndevices++;
                d = devs + ndevices - 1;
                d->bdf = bdf;
@@ -144,27 +198,35 @@ void inmate_main(void)
                        get_ivpos(d));
 
                memcpy(d->shmem, str, 32);
-
+#ifdef CONFIG_X86
                int_set_handler(IRQ_VECTOR + ndevices - 1, irq_handler);
                pci_msix_set_vector(bdf, IRQ_VECTOR + ndevices - 1, 0);
+#elif CONFIG_ARM
+               if (irq != -1) {
+                       gic_enable_irq(irq + ndevices - 1);
+                       mmio_write32(d->registers, -1);
+               }
+#endif
                bdf++;
        }
 
-       if (!ndevices) {
-               printk("IVSHMEM: No PCI devices found .. nothing to do.\n");
-               goto out;
-       }
-
+#ifdef CONFIG_X86
        asm volatile("sti");
-       while (1) {
+       while (ndevices) {
                for (i = 0; i < ndevices; i++) {
-                       d = devs + i;
                        delay_us(1000*1000);
-                       shmem = d->shmem;
-                       shmem[19]++;
-                       send_irq(d);
+                       use_device(i);
                }
        }
-out:
+#endif
+       if (!ndevices)
+               printk("IVSHMEM: No PCI devices found .. nothing to do.\n");
+
+#ifdef CONFIG_X86
        asm volatile("hlt");
+#elif CONFIG_ARM
+       while (1)
+               asm volatile("wfi" : : : "memory");
+#endif
+
 }
diff --git a/inmates/demos/x86/Makefile b/inmates/demos/x86/Makefile
index 8defc67..89b0e86 100644
--- a/inmates/demos/x86/Makefile
+++ b/inmates/demos/x86/Makefile
@@ -20,9 +20,11 @@ apic-demo-y  := apic-demo.o
 ioapic-demo-y  := ioapic-demo.o
 pci-demo-y     := pci-demo.o
 e1000-demo-y   := e1000-demo.o
-ivshmem-demo-y := ivshmem-demo.o
+ivshmem-demo-y := ../ivshmem-demo.o
 smp-demo-y     := smp-demo.o
 
+ccflags-y      += -DCONFIG_X86
+
 $(eval $(call DECLARE_32_BIT,32-bit-demo))
 32-bit-demo-y  := 32-bit-demo.o
 
-- 
2.13.6

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to